blob: 9c0a3713a599f9ade8ed17dc1058b4a8b068a4cd [file] [log] [blame]
DRC9b28def2011-05-21 14:37:15 +00001/*
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05002 * Copyright (C)2009-2015, 2017 D. R. Commander. All Rights Reserved.
DRC2e7b76b2009-04-03 12:04:24 +00003 *
DRC9b28def2011-05-21 14:37:15 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
DRC2e7b76b2009-04-03 12:04:24 +00006 *
DRC9b28def2011-05-21 14:37:15 +00007 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
DRC2e7b76b2009-04-03 12:04:24 +000027 */
28
DRC3a1bb352011-05-24 09:15:44 +000029#ifndef __TURBOJPEG_H__
30#define __TURBOJPEG_H__
31
DRC9b28def2011-05-21 14:37:15 +000032#if defined(_WIN32) && defined(DLLDEFINE)
Leon Scroggins III3993b372018-07-16 10:43:45 -040033#define DLLEXPORT __declspec(dllexport)
DRC2e7b76b2009-04-03 12:04:24 +000034#else
35#define DLLEXPORT
36#endif
DRC2e7b76b2009-04-03 12:04:24 +000037#define DLLCALL
38
DRC2e7b76b2009-04-03 12:04:24 +000039
DRC9b28def2011-05-21 14:37:15 +000040/**
41 * @addtogroup TurboJPEG
42 * TurboJPEG API. This API provides an interface for generating, decoding, and
43 * transforming planar YUV and JPEG images in memory.
44 *
DRC493be612014-08-10 20:12:17 +000045 * @anchor YUVnotes
46 * YUV Image Format Notes
47 * ----------------------
48 * Technically, the JPEG format uses the YCbCr colorspace (which is technically
49 * not a colorspace but a color transform), but per the convention of the
50 * digital video community, the TurboJPEG API uses "YUV" to refer to an image
51 * format consisting of Y, Cb, and Cr image planes.
52 *
53 * Each plane is simply a 2D array of bytes, each byte representing the value
54 * of one of the components (Y, Cb, or Cr) at a particular location in the
DRC40dd3142014-08-17 12:23:49 +000055 * image. The width and height of each plane are determined by the image
56 * width, height, and level of chrominance subsampling. The luminance plane
57 * width is the image width padded to the nearest multiple of the horizontal
58 * subsampling factor (2 in the case of 4:2:0 and 4:2:2, 4 in the case of
59 * 4:1:1, 1 in the case of 4:4:4 or grayscale.) Similarly, the luminance plane
60 * height is the image height padded to the nearest multiple of the vertical
61 * subsampling factor (2 in the case of 4:2:0 or 4:4:0, 1 in the case of 4:4:4
62 * or grayscale.) This is irrespective of any additional padding that may be
63 * specified as an argument to the various YUV functions. The chrominance
64 * plane width is equal to the luminance plane width divided by the horizontal
65 * subsampling factor, and the chrominance plane height is equal to the
66 * luminance plane height divided by the vertical subsampling factor.
DRC493be612014-08-10 20:12:17 +000067 *
68 * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
69 * used, then the luminance plane would be 36 x 35 bytes, and each of the
70 * chrominance planes would be 18 x 35 bytes. If you specify a line padding of
71 * 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes, and
72 * each of the chrominance planes would be 20 x 35 bytes.
DRC779bd682014-08-10 18:30:52 +000073 *
DRC9b28def2011-05-21 14:37:15 +000074 * @{
75 */
DRC2e7b76b2009-04-03 12:04:24 +000076
DRC0a325192011-03-02 09:22:41 +000077
DRC9b28def2011-05-21 14:37:15 +000078/**
79 * The number of chrominance subsampling options
80 */
Leon Scroggins III3993b372018-07-16 10:43:45 -040081#define TJ_NUMSAMP 6
DRCfbb67472010-11-24 04:02:37 +000082
DRC9b28def2011-05-21 14:37:15 +000083/**
84 * Chrominance subsampling options.
DRCb2c47452013-08-23 06:38:59 +000085 * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
86 * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
87 * the Cb and Cr (chrominance) components can be discarded or averaged together
88 * to produce a smaller image with little perceptible loss of image clarity
89 * (the human eye is more sensitive to small changes in brightness than to
90 * small changes in color.) This is called "chrominance subsampling".
DRC9b28def2011-05-21 14:37:15 +000091 */
Leon Scroggins III3993b372018-07-16 10:43:45 -040092enum TJSAMP {
DRC9b28def2011-05-21 14:37:15 +000093 /**
94 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
95 * YUV image will contain one chrominance component for every pixel in the
96 * source image.
97 */
Leon Scroggins III3993b372018-07-16 10:43:45 -040098 TJSAMP_444 = 0,
DRC9b28def2011-05-21 14:37:15 +000099 /**
100 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
101 * chrominance component for every 2x1 block of pixels in the source image.
102 */
DRC25b995a2011-05-21 15:34:54 +0000103 TJSAMP_422,
DRC9b28def2011-05-21 14:37:15 +0000104 /**
105 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
106 * chrominance component for every 2x2 block of pixels in the source image.
107 */
DRC25b995a2011-05-21 15:34:54 +0000108 TJSAMP_420,
DRC9b28def2011-05-21 14:37:15 +0000109 /**
110 * Grayscale. The JPEG or YUV image will contain no chrominance components.
111 */
DRC25b995a2011-05-21 15:34:54 +0000112 TJSAMP_GRAY,
DRC9b28def2011-05-21 14:37:15 +0000113 /**
114 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
115 * chrominance component for every 1x2 block of pixels in the source image.
DRC779bd682014-08-10 18:30:52 +0000116 *
117 * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
DRC9b28def2011-05-21 14:37:15 +0000118 */
DRC1f3635c2013-08-18 10:19:00 +0000119 TJSAMP_440,
120 /**
121 * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
122 * chrominance component for every 4x1 block of pixels in the source image.
123 * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
124 * same size as those compressed with 4:2:0 subsampling, and in the
125 * aggregate, both subsampling methods produce approximately the same
126 * perceptual quality. However, 4:1:1 is better able to reproduce sharp
DRC779bd682014-08-10 18:30:52 +0000127 * horizontal features.
128 *
129 * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
DRC1f3635c2013-08-18 10:19:00 +0000130 */
131 TJSAMP_411
DRC890f1e02011-02-26 22:02:37 +0000132};
133
DRC9b28def2011-05-21 14:37:15 +0000134/**
135 * MCU block width (in pixels) for a given level of chrominance subsampling.
136 * MCU block sizes:
137 * - 8x8 for no subsampling or grayscale
138 * - 16x8 for 4:2:2
139 * - 8x16 for 4:4:0
DRC1f3635c2013-08-18 10:19:00 +0000140 * - 16x16 for 4:2:0
141 * - 32x8 for 4:1:1
DRC9b28def2011-05-21 14:37:15 +0000142 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400143static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32 };
DRC890f1e02011-02-26 22:02:37 +0000144
DRC9b28def2011-05-21 14:37:15 +0000145/**
146 * MCU block height (in pixels) for a given level of chrominance subsampling.
147 * MCU block sizes:
148 * - 8x8 for no subsampling or grayscale
149 * - 16x8 for 4:2:2
150 * - 8x16 for 4:4:0
DRC1f3635c2013-08-18 10:19:00 +0000151 * - 16x16 for 4:2:0
152 * - 32x8 for 4:1:1
DRC9b28def2011-05-21 14:37:15 +0000153 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400154static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8 };
DRC9b28def2011-05-21 14:37:15 +0000155
156
157/**
158 * The number of pixel formats
159 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400160#define TJ_NUMPF 12
DRC9b28def2011-05-21 14:37:15 +0000161
162/**
163 * Pixel formats
DRC9b28def2011-05-21 14:37:15 +0000164 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400165enum TJPF {
DRC9b28def2011-05-21 14:37:15 +0000166 /**
167 * RGB pixel format. The red, green, and blue components in the image are
168 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
169 * address within each pixel.
170 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400171 TJPF_RGB = 0,
DRC9b28def2011-05-21 14:37:15 +0000172 /**
173 * BGR pixel format. The red, green, and blue components in the image are
174 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
175 * address within each pixel.
176 */
DRC25b995a2011-05-21 15:34:54 +0000177 TJPF_BGR,
DRC9b28def2011-05-21 14:37:15 +0000178 /**
179 * RGBX pixel format. The red, green, and blue components in the image are
180 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
DRC67ce3b22011-12-19 02:21:03 +0000181 * address within each pixel. The X component is ignored when compressing
182 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000183 */
DRC25b995a2011-05-21 15:34:54 +0000184 TJPF_RGBX,
DRC9b28def2011-05-21 14:37:15 +0000185 /**
186 * BGRX pixel format. The red, green, and blue components in the image are
187 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
DRC67ce3b22011-12-19 02:21:03 +0000188 * address within each pixel. The X component is ignored when compressing
189 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000190 */
DRC25b995a2011-05-21 15:34:54 +0000191 TJPF_BGRX,
DRC9b28def2011-05-21 14:37:15 +0000192 /**
193 * XBGR pixel format. The red, green, and blue components in the image are
194 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
DRC67ce3b22011-12-19 02:21:03 +0000195 * address within each pixel. The X component is ignored when compressing
196 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000197 */
DRC25b995a2011-05-21 15:34:54 +0000198 TJPF_XBGR,
DRC9b28def2011-05-21 14:37:15 +0000199 /**
200 * XRGB pixel format. The red, green, and blue components in the image are
201 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
DRC67ce3b22011-12-19 02:21:03 +0000202 * address within each pixel. The X component is ignored when compressing
203 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000204 */
DRC25b995a2011-05-21 15:34:54 +0000205 TJPF_XRGB,
DRC9b28def2011-05-21 14:37:15 +0000206 /**
207 * Grayscale pixel format. Each 1-byte pixel represents a luminance
208 * (brightness) level from 0 to 255.
209 */
DRC67ce3b22011-12-19 02:21:03 +0000210 TJPF_GRAY,
211 /**
212 * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
213 * decompressing, the X component is guaranteed to be 0xFF, which can be
214 * interpreted as an opaque alpha channel.
215 */
216 TJPF_RGBA,
217 /**
218 * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
219 * decompressing, the X component is guaranteed to be 0xFF, which can be
220 * interpreted as an opaque alpha channel.
221 */
222 TJPF_BGRA,
223 /**
224 * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
225 * decompressing, the X component is guaranteed to be 0xFF, which can be
226 * interpreted as an opaque alpha channel.
227 */
228 TJPF_ABGR,
229 /**
230 * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
231 * decompressing, the X component is guaranteed to be 0xFF, which can be
232 * interpreted as an opaque alpha channel.
233 */
DRCcd7c3e62013-08-23 02:49:25 +0000234 TJPF_ARGB,
235 /**
DRCb2c47452013-08-23 06:38:59 +0000236 * CMYK pixel format. Unlike RGB, which is an additive color model used
237 * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
238 * color model used primarily for printing. In the CMYK color model, the
239 * value of each color component typically corresponds to an amount of cyan,
240 * magenta, yellow, or black ink that is applied to a white background. In
241 * order to convert between CMYK and RGB, it is necessary to use a color
242 * management system (CMS.) A CMS will attempt to map colors within the
243 * printer's gamut to perceptually similar colors in the display's gamut and
244 * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
245 * be defined with a simple formula. Thus, such a conversion is out of scope
246 * for a codec library. However, the TurboJPEG API allows for compressing
247 * CMYK pixels into a YCCK JPEG image (see #TJCS_YCCK) and decompressing YCCK
248 * JPEG images into CMYK pixels.
DRCcd7c3e62013-08-23 02:49:25 +0000249 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400250 TJPF_CMYK,
251 /**
252 * Unknown pixel format. Currently this is only used by #tjLoadImage().
253 */
254 TJPF_UNKNOWN = -1
DRC9b28def2011-05-21 14:37:15 +0000255};
256
DRCcd7c3e62013-08-23 02:49:25 +0000257/**
DRC38cb1ec2013-08-23 04:45:43 +0000258 * Red offset (in bytes) for a given pixel format. This specifies the number
259 * of bytes that the red component is offset from the start of the pixel. For
260 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
Leon Scroggins III3993b372018-07-16 10:43:45 -0400261 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>. This
262 * will be -1 if the pixel format does not have a red component.
DRC38cb1ec2013-08-23 04:45:43 +0000263 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400264static const int tjRedOffset[TJ_NUMPF] = {
265 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
266};
DRC38cb1ec2013-08-23 04:45:43 +0000267/**
268 * Green offset (in bytes) for a given pixel format. This specifies the number
269 * of bytes that the green component is offset from the start of the pixel.
270 * For instance, if a pixel of format TJ_BGRX is stored in
271 * <tt>char pixel[]</tt>, then the green component will be
Leon Scroggins III3993b372018-07-16 10:43:45 -0400272 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>. This will be -1 if the pixel format
273 * does not have a green component.
DRC38cb1ec2013-08-23 04:45:43 +0000274 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400275static const int tjGreenOffset[TJ_NUMPF] = {
276 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
277};
DRC38cb1ec2013-08-23 04:45:43 +0000278/**
279 * Blue offset (in bytes) for a given pixel format. This specifies the number
280 * of bytes that the Blue component is offset from the start of the pixel. For
281 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
Leon Scroggins III3993b372018-07-16 10:43:45 -0400282 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>. This
283 * will be -1 if the pixel format does not have a blue component.
DRC38cb1ec2013-08-23 04:45:43 +0000284 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400285static const int tjBlueOffset[TJ_NUMPF] = {
286 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
287};
DRC38cb1ec2013-08-23 04:45:43 +0000288/**
Leon Scroggins III3993b372018-07-16 10:43:45 -0400289 * Alpha offset (in bytes) for a given pixel format. This specifies the number
290 * of bytes that the Alpha component is offset from the start of the pixel.
291 * For instance, if a pixel of format TJ_BGRA is stored in
292 * <tt>char pixel[]</tt>, then the alpha component will be
293 * <tt>pixel[tjAlphaOffset[TJ_BGRA]]</tt>. This will be -1 if the pixel format
294 * does not have an alpha component.
DRC38cb1ec2013-08-23 04:45:43 +0000295 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400296static const int tjAlphaOffset[TJ_NUMPF] = {
297 -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
298};
299/**
300 * Pixel size (in bytes) for a given pixel format
301 */
302static const int tjPixelSize[TJ_NUMPF] = {
303 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
304};
DRC38cb1ec2013-08-23 04:45:43 +0000305
306
307/**
DRCcd7c3e62013-08-23 02:49:25 +0000308 * The number of JPEG colorspaces
309 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400310#define TJ_NUMCS 5
DRCcd7c3e62013-08-23 02:49:25 +0000311
312/**
313 * JPEG colorspaces
314 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400315enum TJCS {
DRCcd7c3e62013-08-23 02:49:25 +0000316 /**
317 * RGB colorspace. When compressing the JPEG image, the R, G, and B
318 * components in the source image are reordered into image planes, but no
319 * colorspace conversion or subsampling is performed. RGB JPEG images can be
320 * decompressed to any of the extended RGB pixel formats or grayscale, but
321 * they cannot be decompressed to YUV images.
322 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400323 TJCS_RGB = 0,
DRCcd7c3e62013-08-23 02:49:25 +0000324 /**
325 * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
326 * mathematical transformation of RGB designed solely for storage and
327 * transmission. YCbCr images must be converted to RGB before they can
328 * actually be displayed. In the YCbCr colorspace, the Y (luminance)
329 * component represents the black & white portion of the original image, and
330 * the Cb and Cr (chrominance) components represent the color portion of the
331 * original image. Originally, the analog equivalent of this transformation
332 * allowed the same signal to drive both black & white and color televisions,
333 * but JPEG images use YCbCr primarily because it allows the color data to be
334 * optionally subsampled for the purposes of reducing bandwidth or disk
335 * space. YCbCr is the most common JPEG colorspace, and YCbCr JPEG images
336 * can be compressed from and decompressed to any of the extended RGB pixel
337 * formats or grayscale, or they can be decompressed to YUV planar images.
338 */
339 TJCS_YCbCr,
340 /**
341 * Grayscale colorspace. The JPEG image retains only the luminance data (Y
342 * component), and any color data from the source image is discarded.
343 * Grayscale JPEG images can be compressed from and decompressed to any of
344 * the extended RGB pixel formats or grayscale, or they can be decompressed
345 * to YUV planar images.
346 */
347 TJCS_GRAY,
348 /**
349 * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
350 * components in the source image are reordered into image planes, but no
351 * colorspace conversion or subsampling is performed. CMYK JPEG images can
352 * only be decompressed to CMYK pixels.
353 */
354 TJCS_CMYK,
355 /**
356 * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
357 * rather a mathematical transformation of CMYK designed solely for storage
358 * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
359 * reversibly transformed into YCCK, and as with YCbCr, the chrominance
360 * components in the YCCK pixels can be subsampled without incurring major
361 * perceptual loss. YCCK JPEG images can only be compressed from and
362 * decompressed to CMYK pixels.
363 */
364 TJCS_YCCK
365};
366
367
DRC9b28def2011-05-21 14:37:15 +0000368/**
DRC9b28def2011-05-21 14:37:15 +0000369 * The uncompressed source/destination image is stored in bottom-up (Windows,
370 * OpenGL) order, not top-down (X11) order.
371 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400372#define TJFLAG_BOTTOMUP 2
DRC9b28def2011-05-21 14:37:15 +0000373/**
DRC01fdcc32013-04-26 08:41:25 +0000374 * When decompressing an image that was compressed using chrominance
375 * subsampling, use the fastest chrominance upsampling algorithm available in
376 * the underlying codec. The default is to use smooth upsampling, which
377 * creates a smooth transition between neighboring chrominance components in
378 * order to reduce upsampling artifacts in the decompressed image.
DRC9b28def2011-05-21 14:37:15 +0000379 */
DRC25b995a2011-05-21 15:34:54 +0000380#define TJFLAG_FASTUPSAMPLE 256
DRC9b28def2011-05-21 14:37:15 +0000381/**
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500382 * Disable buffer (re)allocation. If passed to one of the JPEG compression or
383 * transform functions, this flag will cause those functions to generate an
384 * error if the JPEG image buffer is invalid or too small rather than
385 * attempting to allocate or reallocate that buffer. This reproduces the
386 * behavior of earlier versions of TurboJPEG.
DRC9b28def2011-05-21 14:37:15 +0000387 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400388#define TJFLAG_NOREALLOC 1024
DRC73d74c12012-06-29 23:46:38 +0000389/**
390 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
DRC3b219822013-08-18 09:31:45 +0000391 * default if this flag is not specified is implementation-specific. For
392 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
393 * algorithm by default when compressing, because this has been shown to have
394 * only a very slight effect on accuracy, but it uses the accurate algorithm
395 * when decompressing, because this has been shown to have a larger effect.
DRC73d74c12012-06-29 23:46:38 +0000396 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400397#define TJFLAG_FASTDCT 2048
DRC73d74c12012-06-29 23:46:38 +0000398/**
399 * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
DRC3b219822013-08-18 09:31:45 +0000400 * The default if this flag is not specified is implementation-specific. For
401 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
402 * algorithm by default when compressing, because this has been shown to have
403 * only a very slight effect on accuracy, but it uses the accurate algorithm
404 * when decompressing, because this has been shown to have a larger effect.
DRC73d74c12012-06-29 23:46:38 +0000405 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400406#define TJFLAG_ACCURATEDCT 4096
407/**
408 * Immediately discontinue the current compression/decompression/transform
409 * operation if the underlying codec throws a warning (non-fatal error). The
410 * default behavior is to allow the operation to complete unless a fatal error
411 * is encountered.
412 */
413#define TJFLAG_STOPONWARNING 8192
414/**
415 * Use progressive entropy coding in JPEG images generated by the compression
416 * and transform functions. Progressive entropy coding will generally improve
417 * compression relative to baseline entropy coding (the default), but it will
418 * reduce compression and decompression performance considerably.
419 */
420#define TJFLAG_PROGRESSIVE 16384
421
422
423/**
424 * The number of error codes
425 */
426#define TJ_NUMERR 2
427
428/**
429 * Error codes
430 */
431enum TJERR {
432 /**
433 * The error was non-fatal and recoverable, but the image may still be
434 * corrupt.
435 */
436 TJERR_WARNING = 0,
437 /**
438 * The error was fatal and non-recoverable.
439 */
440 TJERR_FATAL
441};
DRC9b28def2011-05-21 14:37:15 +0000442
443
444/**
DRC01fdcc32013-04-26 08:41:25 +0000445 * The number of transform operations
DRC9b28def2011-05-21 14:37:15 +0000446 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400447#define TJ_NUMXOP 8
DRC9b28def2011-05-21 14:37:15 +0000448
449/**
DRC25b995a2011-05-21 15:34:54 +0000450 * Transform operations for #tjTransform()
DRC9b28def2011-05-21 14:37:15 +0000451 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400452enum TJXOP {
DRC9b28def2011-05-21 14:37:15 +0000453 /**
454 * Do not transform the position of the image pixels
455 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400456 TJXOP_NONE = 0,
DRC9b28def2011-05-21 14:37:15 +0000457 /**
458 * Flip (mirror) image horizontally. This transform is imperfect if there
DRC25b995a2011-05-21 15:34:54 +0000459 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000460 */
DRC25b995a2011-05-21 15:34:54 +0000461 TJXOP_HFLIP,
DRC9b28def2011-05-21 14:37:15 +0000462 /**
463 * Flip (mirror) image vertically. This transform is imperfect if there are
DRC25b995a2011-05-21 15:34:54 +0000464 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000465 */
DRC25b995a2011-05-21 15:34:54 +0000466 TJXOP_VFLIP,
DRC9b28def2011-05-21 14:37:15 +0000467 /**
468 * Transpose image (flip/mirror along upper left to lower right axis.) This
469 * transform is always perfect.
470 */
DRC25b995a2011-05-21 15:34:54 +0000471 TJXOP_TRANSPOSE,
DRC9b28def2011-05-21 14:37:15 +0000472 /**
473 * Transverse transpose image (flip/mirror along upper right to lower left
474 * axis.) This transform is imperfect if there are any partial MCU blocks in
DRC25b995a2011-05-21 15:34:54 +0000475 * the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000476 */
DRC25b995a2011-05-21 15:34:54 +0000477 TJXOP_TRANSVERSE,
DRC9b28def2011-05-21 14:37:15 +0000478 /**
479 * Rotate image clockwise by 90 degrees. This transform is imperfect if
480 * there are any partial MCU blocks on the bottom edge (see
DRC25b995a2011-05-21 15:34:54 +0000481 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000482 */
DRC25b995a2011-05-21 15:34:54 +0000483 TJXOP_ROT90,
DRC9b28def2011-05-21 14:37:15 +0000484 /**
485 * Rotate image 180 degrees. This transform is imperfect if there are any
DRC25b995a2011-05-21 15:34:54 +0000486 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000487 */
DRC25b995a2011-05-21 15:34:54 +0000488 TJXOP_ROT180,
DRC9b28def2011-05-21 14:37:15 +0000489 /**
490 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
491 * if there are any partial MCU blocks on the right edge (see
DRC25b995a2011-05-21 15:34:54 +0000492 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000493 */
DRC25b995a2011-05-21 15:34:54 +0000494 TJXOP_ROT270
DRC9b28def2011-05-21 14:37:15 +0000495};
496
497
498/**
DRC25b995a2011-05-21 15:34:54 +0000499 * This option will cause #tjTransform() to return an error if the transform is
DRC9b28def2011-05-21 14:37:15 +0000500 * not perfect. Lossless transforms operate on MCU blocks, whose size depends
501 * on the level of chrominance subsampling used (see #tjMCUWidth
502 * and #tjMCUHeight.) If the image's width or height is not evenly divisible
503 * by the MCU block size, then there will be partial MCU blocks on the right
504 * and/or bottom edges. It is not possible to move these partial MCU blocks to
505 * the top or left of the image, so any transform that would require that is
506 * "imperfect." If this option is not specified, then any partial MCU blocks
507 * that cannot be transformed will be left in place, which will create
508 * odd-looking strips on the right or bottom edge of the image.
509 */
DRC25b995a2011-05-21 15:34:54 +0000510#define TJXOPT_PERFECT 1
DRC9b28def2011-05-21 14:37:15 +0000511/**
DRC25b995a2011-05-21 15:34:54 +0000512 * This option will cause #tjTransform() to discard any partial MCU blocks that
DRC9b28def2011-05-21 14:37:15 +0000513 * cannot be transformed.
514 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400515#define TJXOPT_TRIM 2
DRC9b28def2011-05-21 14:37:15 +0000516/**
DRC25b995a2011-05-21 15:34:54 +0000517 * This option will enable lossless cropping. See #tjTransform() for more
DRC9b28def2011-05-21 14:37:15 +0000518 * information.
519 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400520#define TJXOPT_CROP 4
DRC9b28def2011-05-21 14:37:15 +0000521/**
522 * This option will discard the color data in the input image and produce
523 * a grayscale output image.
DRC9b28def2011-05-21 14:37:15 +0000524 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400525#define TJXOPT_GRAY 8
DRC7bf04d32011-09-17 00:18:31 +0000526/**
527 * This option will prevent #tjTransform() from outputting a JPEG image for
528 * this particular transform (this can be used in conjunction with a custom
529 * filter to capture the transformed DCT coefficients without transcoding
530 * them.)
531 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400532#define TJXOPT_NOOUTPUT 16
533/**
534 * This option will enable progressive entropy coding in the output image
535 * generated by this particular transform. Progressive entropy coding will
536 * generally improve compression relative to baseline entropy coding (the
537 * default), but it will reduce compression and decompression performance
538 * considerably.
539 */
540#define TJXOPT_PROGRESSIVE 32
541/**
542 * This option will prevent #tjTransform() from copying any extra markers
543 * (including EXIF and ICC profile data) from the source image to the output
544 * image.
545 */
546#define TJXOPT_COPYNONE 64
DRC9b28def2011-05-21 14:37:15 +0000547
548
549/**
550 * Scaling factor
551 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400552typedef struct {
DRC9b28def2011-05-21 14:37:15 +0000553 /**
554 * Numerator
555 */
556 int num;
557 /**
558 * Denominator
559 */
560 int denom;
561} tjscalingfactor;
562
563/**
564 * Cropping region
565 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400566typedef struct {
DRC9b28def2011-05-21 14:37:15 +0000567 /**
568 * The left boundary of the cropping region. This must be evenly divisible
569 * by the MCU block width (see #tjMCUWidth.)
570 */
571 int x;
572 /**
573 * The upper boundary of the cropping region. This must be evenly divisible
574 * by the MCU block height (see #tjMCUHeight.)
575 */
576 int y;
577 /**
578 * The width of the cropping region. Setting this to 0 is the equivalent of
579 * setting it to the width of the source JPEG image - x.
580 */
581 int w;
582 /**
583 * The height of the cropping region. Setting this to 0 is the equivalent of
584 * setting it to the height of the source JPEG image - y.
585 */
586 int h;
DRC0a079692011-03-02 09:27:49 +0000587} tjregion;
588
DRC9b28def2011-05-21 14:37:15 +0000589/**
590 * Lossless transform
591 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400592typedef struct tjtransform {
DRC9b28def2011-05-21 14:37:15 +0000593 /**
594 * Cropping region
595 */
596 tjregion r;
597 /**
DRC25b995a2011-05-21 15:34:54 +0000598 * One of the @ref TJXOP "transform operations"
DRC9b28def2011-05-21 14:37:15 +0000599 */
600 int op;
601 /**
DRC25b995a2011-05-21 15:34:54 +0000602 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
DRC9b28def2011-05-21 14:37:15 +0000603 */
604 int options;
DRC7bf04d32011-09-17 00:18:31 +0000605 /**
DRCf5467112011-09-20 05:02:19 +0000606 * Arbitrary data that can be accessed within the body of the callback
607 * function
608 */
609 void *data;
610 /**
DRC7bf04d32011-09-17 00:18:31 +0000611 * A callback function that can be used to modify the DCT coefficients
612 * after they are losslessly transformed but before they are transcoded to a
DRCcd7c3e62013-08-23 02:49:25 +0000613 * new JPEG image. This allows for custom filters or other transformations
614 * to be applied in the frequency domain.
DRC7bf04d32011-09-17 00:18:31 +0000615 *
DRCf5467112011-09-20 05:02:19 +0000616 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
DRC9d77dad2014-08-12 15:06:30 +0000617 * this pointer is not guaranteed to be valid once the callback returns, so
618 * applications wishing to hand off the DCT coefficients to another function
619 * or library should make a copy of them within the body of the callback.)
620 *
DRCf5467112011-09-20 05:02:19 +0000621 * @param arrayRegion #tjregion structure containing the width and height of
DRC9d77dad2014-08-12 15:06:30 +0000622 * the array pointed to by <tt>coeffs</tt> as well as its offset relative to
623 * the component plane. TurboJPEG implementations may choose to split each
624 * component plane into multiple DCT coefficient arrays and call the callback
625 * function once for each array.
626 *
DRCf5467112011-09-20 05:02:19 +0000627 * @param planeRegion #tjregion structure containing the width and height of
DRC9d77dad2014-08-12 15:06:30 +0000628 * the component plane to which <tt>coeffs</tt> belongs
629 *
DRCf5467112011-09-20 05:02:19 +0000630 * @param componentID ID number of the component plane to which
DRC9d77dad2014-08-12 15:06:30 +0000631 * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of 0, 1,
632 * and 2 in typical JPEG images.)
633 *
DRCf5467112011-09-20 05:02:19 +0000634 * @param transformID ID number of the transformed image to which
DRC9d77dad2014-08-12 15:06:30 +0000635 * <tt>coeffs</tt> belongs. This is the same as the index of the transform
636 * in the <tt>transforms</tt> array that was passed to #tjTransform().
637 *
DRCf5467112011-09-20 05:02:19 +0000638 * @param transform a pointer to a #tjtransform structure that specifies the
DRC9d77dad2014-08-12 15:06:30 +0000639 * parameters and/or cropping region for this transform
DRC7bf04d32011-09-17 00:18:31 +0000640 *
641 * @return 0 if the callback was successful, or -1 if an error occurred.
642 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400643 int (*customFilter) (short *coeffs, tjregion arrayRegion,
644 tjregion planeRegion, int componentIndex,
645 int transformIndex, struct tjtransform *transform);
DRC0a079692011-03-02 09:27:49 +0000646} tjtransform;
647
DRC9b28def2011-05-21 14:37:15 +0000648/**
649 * TurboJPEG instance handle
650 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400651typedef void *tjhandle;
DRC2e7b76b2009-04-03 12:04:24 +0000652
DRC9b28def2011-05-21 14:37:15 +0000653
654/**
655 * Pad the given width to the nearest 32-bit boundary
656 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400657#define TJPAD(width) (((width) + 3) & (~3))
DRC9b28def2011-05-21 14:37:15 +0000658
659/**
DRC25b995a2011-05-21 15:34:54 +0000660 * Compute the scaled value of <tt>dimension</tt> using the given scaling
661 * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
DRC1a45b812014-05-09 18:06:58 +0000662 * scalingFactor)</tt>.
DRC9b28def2011-05-21 14:37:15 +0000663 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400664#define TJSCALED(dimension, scalingFactor) \
665 ((dimension * scalingFactor.num + scalingFactor.denom - 1) / \
666 scalingFactor.denom)
DRC9b28def2011-05-21 14:37:15 +0000667
DRC2e7b76b2009-04-03 12:04:24 +0000668
669#ifdef __cplusplus
670extern "C" {
671#endif
672
DRC2e7b76b2009-04-03 12:04:24 +0000673
DRC9b28def2011-05-21 14:37:15 +0000674/**
675 * Create a TurboJPEG compressor instance.
676 *
677 * @return a handle to the newly-created instance, or NULL if an error
Leon Scroggins III3993b372018-07-16 10:43:45 -0400678 * occurred (see #tjGetErrorStr2().)
DRC9b28def2011-05-21 14:37:15 +0000679 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400680DLLEXPORT tjhandle tjInitCompress(void);
DRC2e7b76b2009-04-03 12:04:24 +0000681
682
DRC9b28def2011-05-21 14:37:15 +0000683/**
DRCcd7c3e62013-08-23 02:49:25 +0000684 * Compress an RGB, grayscale, or CMYK image into a JPEG image.
DRC9b28def2011-05-21 14:37:15 +0000685 *
686 * @param handle a handle to a TurboJPEG compressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +0000687 *
DRCcd7c3e62013-08-23 02:49:25 +0000688 * @param srcBuf pointer to an image buffer containing RGB, grayscale, or
Alex Naidis6eb7d372016-10-16 23:10:08 +0200689 * CMYK pixels to be compressed
DRC9d77dad2014-08-12 15:06:30 +0000690 *
DRC9b28def2011-05-21 14:37:15 +0000691 * @param width width (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +0000692 *
DRC40dd3142014-08-17 12:23:49 +0000693 * @param pitch bytes per line in the source image. Normally, this should be
DRC9d77dad2014-08-12 15:06:30 +0000694 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
695 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
696 * is padded to the nearest 32-bit boundary, as is the case for Windows
697 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
698 * Setting this parameter to 0 is the equivalent of setting it to
699 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
700 *
DRC9b28def2011-05-21 14:37:15 +0000701 * @param height height (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +0000702 *
DRC25b995a2011-05-21 15:34:54 +0000703 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9d77dad2014-08-12 15:06:30 +0000704 * "Pixel formats".)
705 *
DRC9b28def2011-05-21 14:37:15 +0000706 * @param jpegBuf address of a pointer to an image buffer that will receive the
DRC9d77dad2014-08-12 15:06:30 +0000707 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
708 * to accommodate the size of the JPEG image. Thus, you can choose to:
709 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
710 * let TurboJPEG grow the buffer as needed,
711 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
712 * for you, or
713 * -# pre-allocate the buffer to a "worst case" size determined by calling
714 * #tjBufSize(). This should ensure that the buffer never has to be
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500715 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
DRC9d77dad2014-08-12 15:06:30 +0000716 * .
717 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
718 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
719 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
720 * it may have changed.
721 *
DRC80803ae2011-12-15 13:12:59 +0000722 * @param jpegSize pointer to an unsigned long variable that holds the size of
DRC9d77dad2014-08-12 15:06:30 +0000723 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
724 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
725 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
DRCfe80ec22014-08-21 15:51:47 +0000726 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
727 * reused from a previous call to one of the JPEG compression functions, then
728 * <tt>*jpegSize</tt> is ignored.
DRC9d77dad2014-08-12 15:06:30 +0000729 *
DRC9b28def2011-05-21 14:37:15 +0000730 * @param jpegSubsamp the level of chrominance subsampling to be used when
DRC9d77dad2014-08-12 15:06:30 +0000731 * generating the JPEG image (see @ref TJSAMP
732 * "Chrominance subsampling options".)
733 *
DRC9b28def2011-05-21 14:37:15 +0000734 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
DRC9d77dad2014-08-12 15:06:30 +0000735 * 100 = best)
736 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500737 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +0000738 * "flags"
DRC9b28def2011-05-21 14:37:15 +0000739 *
Leon Scroggins III3993b372018-07-16 10:43:45 -0400740 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
741 * and #tjGetErrorCode().)
DRC2e7b76b2009-04-03 12:04:24 +0000742*/
Leon Scroggins III3993b372018-07-16 10:43:45 -0400743DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
744 int width, int pitch, int height, int pixelFormat,
745 unsigned char **jpegBuf, unsigned long *jpegSize,
746 int jpegSubsamp, int jpegQual, int flags);
DRC2e7b76b2009-04-03 12:04:24 +0000747
DRCb28fc572011-02-22 06:41:29 +0000748
DRC9b28def2011-05-21 14:37:15 +0000749/**
DRC779bd682014-08-10 18:30:52 +0000750 * Compress a YUV planar image into a JPEG image.
751 *
DRC910a3572013-10-30 23:02:57 +0000752 * @param handle a handle to a TurboJPEG compressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +0000753 *
DRC493be612014-08-10 20:12:17 +0000754 * @param srcBuf pointer to an image buffer containing a YUV planar image to be
DRC9d77dad2014-08-12 15:06:30 +0000755 * compressed. The size of this buffer should match the value returned by
756 * #tjBufSizeYUV2() for the given image width, height, padding, and level of
757 * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
758 * stored sequentially in the source buffer (refer to @ref YUVnotes
Alex Naidis6eb7d372016-10-16 23:10:08 +0200759 * "YUV Image Format Notes".)
DRC9d77dad2014-08-12 15:06:30 +0000760 *
DRC493be612014-08-10 20:12:17 +0000761 * @param width width (in pixels) of the source image. If the width is not an
DRC9d77dad2014-08-12 15:06:30 +0000762 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
763 * buffer copy will be performed within TurboJPEG.
764 *
DRC910a3572013-10-30 23:02:57 +0000765 * @param pad the line padding used in the source image. For instance, if each
DRC9d77dad2014-08-12 15:06:30 +0000766 * line in each plane of the YUV image is padded to the nearest multiple of 4
767 * bytes, then <tt>pad</tt> should be set to 4.
768 *
DRC493be612014-08-10 20:12:17 +0000769 * @param height height (in pixels) of the source image. If the height is not
DRC9d77dad2014-08-12 15:06:30 +0000770 * an even multiple of the MCU block height (see #tjMCUHeight), then an
771 * intermediate buffer copy will be performed within TurboJPEG.
772 *
DRC910a3572013-10-30 23:02:57 +0000773 * @param subsamp the level of chrominance subsampling used in the source
DRC9d77dad2014-08-12 15:06:30 +0000774 * image (see @ref TJSAMP "Chrominance subsampling options".)
775 *
DRC910a3572013-10-30 23:02:57 +0000776 * @param jpegBuf address of a pointer to an image buffer that will receive the
DRC9d77dad2014-08-12 15:06:30 +0000777 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
778 * accommodate the size of the JPEG image. Thus, you can choose to:
779 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
780 * let TurboJPEG grow the buffer as needed,
781 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
782 * for you, or
783 * -# pre-allocate the buffer to a "worst case" size determined by calling
784 * #tjBufSize(). This should ensure that the buffer never has to be
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500785 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
DRC9d77dad2014-08-12 15:06:30 +0000786 * .
787 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
788 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
789 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
790 * it may have changed.
791 *
DRC910a3572013-10-30 23:02:57 +0000792 * @param jpegSize pointer to an unsigned long variable that holds the size of
DRC9d77dad2014-08-12 15:06:30 +0000793 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
794 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
795 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
DRCfe80ec22014-08-21 15:51:47 +0000796 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
797 * reused from a previous call to one of the JPEG compression functions, then
798 * <tt>*jpegSize</tt> is ignored.
DRC9d77dad2014-08-12 15:06:30 +0000799 *
DRC910a3572013-10-30 23:02:57 +0000800 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
DRC9d77dad2014-08-12 15:06:30 +0000801 * 100 = best)
802 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500803 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +0000804 * "flags"
DRC910a3572013-10-30 23:02:57 +0000805 *
Leon Scroggins III3993b372018-07-16 10:43:45 -0400806 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
807 * and #tjGetErrorCode().)
DRC910a3572013-10-30 23:02:57 +0000808*/
Leon Scroggins III3993b372018-07-16 10:43:45 -0400809DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
810 int width, int pad, int height, int subsamp,
811 unsigned char **jpegBuf,
812 unsigned long *jpegSize, int jpegQual,
813 int flags);
DRC910a3572013-10-30 23:02:57 +0000814
815
816/**
DRCaecea382014-08-11 18:05:41 +0000817 * Compress a set of Y, U (Cb), and V (Cr) image planes into a JPEG image.
818 *
819 * @param handle a handle to a TurboJPEG compressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +0000820 *
DRCaecea382014-08-11 18:05:41 +0000821 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
DRC9d77dad2014-08-12 15:06:30 +0000822 * (or just a Y plane, if compressing a grayscale image) that contain a YUV
823 * image to be compressed. These planes can be contiguous or non-contiguous in
DRC40dd3142014-08-17 12:23:49 +0000824 * memory. The size of each plane should match the value returned by
825 * #tjPlaneSizeYUV() for the given image width, height, strides, and level of
826 * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
Alex Naidis6eb7d372016-10-16 23:10:08 +0200827 * for more details.
DRC9d77dad2014-08-12 15:06:30 +0000828 *
DRCaecea382014-08-11 18:05:41 +0000829 * @param width width (in pixels) of the source image. If the width is not an
DRC9d77dad2014-08-12 15:06:30 +0000830 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
831 * buffer copy will be performed within TurboJPEG.
832 *
DRCaecea382014-08-11 18:05:41 +0000833 * @param strides an array of integers, each specifying the number of bytes per
DRC9d77dad2014-08-12 15:06:30 +0000834 * line in the corresponding plane of the YUV source image. Setting the stride
DRC40dd3142014-08-17 12:23:49 +0000835 * for any plane to 0 is the same as setting it to the plane width (see
836 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
DRC7a8c53e2015-06-19 16:07:14 +0000837 * the strides for all planes will be set to their respective plane widths.
DRC40dd3142014-08-17 12:23:49 +0000838 * You can adjust the strides in order to specify an arbitrary amount of line
839 * padding in each plane or to create a JPEG image from a subregion of a larger
840 * YUV planar image.
DRC9d77dad2014-08-12 15:06:30 +0000841 *
DRCaecea382014-08-11 18:05:41 +0000842 * @param height height (in pixels) of the source image. If the height is not
DRC9d77dad2014-08-12 15:06:30 +0000843 * an even multiple of the MCU block height (see #tjMCUHeight), then an
844 * intermediate buffer copy will be performed within TurboJPEG.
845 *
DRCaecea382014-08-11 18:05:41 +0000846 * @param subsamp the level of chrominance subsampling used in the source
DRC9d77dad2014-08-12 15:06:30 +0000847 * image (see @ref TJSAMP "Chrominance subsampling options".)
848 *
DRCaecea382014-08-11 18:05:41 +0000849 * @param jpegBuf address of a pointer to an image buffer that will receive the
DRC9d77dad2014-08-12 15:06:30 +0000850 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
851 * accommodate the size of the JPEG image. Thus, you can choose to:
852 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
853 * let TurboJPEG grow the buffer as needed,
854 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
855 * for you, or
856 * -# pre-allocate the buffer to a "worst case" size determined by calling
857 * #tjBufSize(). This should ensure that the buffer never has to be
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500858 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
DRC9d77dad2014-08-12 15:06:30 +0000859 * .
860 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
861 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
862 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
863 * it may have changed.
864 *
DRCaecea382014-08-11 18:05:41 +0000865 * @param jpegSize pointer to an unsigned long variable that holds the size of
DRC9d77dad2014-08-12 15:06:30 +0000866 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
867 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
868 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
DRCfe80ec22014-08-21 15:51:47 +0000869 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
870 * reused from a previous call to one of the JPEG compression functions, then
871 * <tt>*jpegSize</tt> is ignored.
DRC9d77dad2014-08-12 15:06:30 +0000872 *
DRCaecea382014-08-11 18:05:41 +0000873 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
DRC9d77dad2014-08-12 15:06:30 +0000874 * 100 = best)
875 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500876 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +0000877 * "flags"
DRCaecea382014-08-11 18:05:41 +0000878 *
Leon Scroggins III3993b372018-07-16 10:43:45 -0400879 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
880 * and #tjGetErrorCode().)
DRCaecea382014-08-11 18:05:41 +0000881*/
Leon Scroggins III3993b372018-07-16 10:43:45 -0400882DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
883 const unsigned char **srcPlanes,
884 int width, const int *strides,
885 int height, int subsamp,
886 unsigned char **jpegBuf,
887 unsigned long *jpegSize, int jpegQual,
888 int flags);
DRCaecea382014-08-11 18:05:41 +0000889
890
891/**
DRC9b28def2011-05-21 14:37:15 +0000892 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
DRC9b49f0e2011-07-12 03:17:23 +0000893 * the given parameters. The number of bytes returned by this function is
894 * larger than the size of the uncompressed source image. The reason for this
895 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
DRC01fdcc32013-04-26 08:41:25 +0000896 * for a very high-quality JPEG image with very high-frequency content to
DRC9b49f0e2011-07-12 03:17:23 +0000897 * expand rather than compress when converted to the JPEG format. Such images
898 * represent a very rare corner case, but since there is no way to predict the
899 * size of a JPEG image prior to compression, the corner case has to be
900 * handled.
DRC9b28def2011-05-21 14:37:15 +0000901 *
DRC40dd3142014-08-17 12:23:49 +0000902 * @param width width (in pixels) of the image
DRC9d77dad2014-08-12 15:06:30 +0000903 *
DRC40dd3142014-08-17 12:23:49 +0000904 * @param height height (in pixels) of the image
DRC9d77dad2014-08-12 15:06:30 +0000905 *
DRC9b49f0e2011-07-12 03:17:23 +0000906 * @param jpegSubsamp the level of chrominance subsampling to be used when
DRC9d77dad2014-08-12 15:06:30 +0000907 * generating the JPEG image (see @ref TJSAMP
908 * "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000909 *
910 * @return the maximum size of the buffer (in bytes) required to hold the
911 * image, or -1 if the arguments are out of bounds.
912 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400913DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
DRC2e7b76b2009-04-03 12:04:24 +0000914
DRCb28fc572011-02-22 06:41:29 +0000915
DRC9b28def2011-05-21 14:37:15 +0000916/**
917 * The size of the buffer (in bytes) required to hold a YUV planar image with
918 * the given parameters.
919 *
DRC40dd3142014-08-17 12:23:49 +0000920 * @param width width (in pixels) of the image
DRC9d77dad2014-08-12 15:06:30 +0000921 *
DRCf610d612013-04-26 10:33:29 +0000922 * @param pad the width of each line in each plane of the image is padded to
DRC9d77dad2014-08-12 15:06:30 +0000923 * the nearest multiple of this number of bytes (must be a power of 2.)
924 *
DRC40dd3142014-08-17 12:23:49 +0000925 * @param height height (in pixels) of the image
DRC9d77dad2014-08-12 15:06:30 +0000926 *
DRC9b49f0e2011-07-12 03:17:23 +0000927 * @param subsamp level of chrominance subsampling in the image (see
DRC9d77dad2014-08-12 15:06:30 +0000928 * @ref TJSAMP "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000929 *
930 * @return the size of the buffer (in bytes) required to hold the image, or
931 * -1 if the arguments are out of bounds.
932 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400933DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height,
934 int subsamp);
DRCf3cf9732011-02-22 00:16:14 +0000935
DRCb28fc572011-02-22 06:41:29 +0000936
DRC9b28def2011-05-21 14:37:15 +0000937/**
DRC40dd3142014-08-17 12:23:49 +0000938 * The size of the buffer (in bytes) required to hold a YUV image plane with
939 * the given parameters.
940 *
941 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
942 *
943 * @param width width (in pixels) of the YUV image. NOTE: this is the width of
944 * the whole image, not the plane width.
945 *
946 * @param stride bytes per line in the image plane. Setting this to 0 is the
947 * equivalent of setting it to the plane width.
948 *
949 * @param height height (in pixels) of the YUV image. NOTE: this is the height
950 * of the whole image, not the plane height.
951 *
952 * @param subsamp level of chrominance subsampling in the image (see
953 * @ref TJSAMP "Chrominance subsampling options".)
954 *
955 * @return the size of the buffer (in bytes) required to hold the YUV image
956 * plane, or -1 if the arguments are out of bounds.
957 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400958DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
959 int height, int subsamp);
DRC40dd3142014-08-17 12:23:49 +0000960
961
962/**
963 * The plane width of a YUV image plane with the given parameters. Refer to
964 * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
965 *
966 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
967 *
968 * @param width width (in pixels) of the YUV image
969 *
970 * @param subsamp level of chrominance subsampling in the image (see
971 * @ref TJSAMP "Chrominance subsampling options".)
972 *
973 * @return the plane width of a YUV image plane with the given parameters, or
974 * -1 if the arguments are out of bounds.
975 */
976DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
977
978
979/**
980 * The plane height of a YUV image plane with the given parameters. Refer to
981 * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
982 *
983 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
984 *
985 * @param height height (in pixels) of the YUV image
986 *
987 * @param subsamp level of chrominance subsampling in the image (see
988 * @ref TJSAMP "Chrominance subsampling options".)
989 *
990 * @return the plane height of a YUV image plane with the given parameters, or
991 * -1 if the arguments are out of bounds.
992 */
993DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
994
995
996/**
DRC9b28def2011-05-21 14:37:15 +0000997 * Encode an RGB or grayscale image into a YUV planar image. This function
DRC34dca052014-02-28 09:17:14 +0000998 * uses the accelerated color conversion routines in the underlying
DRCf610d612013-04-26 10:33:29 +0000999 * codec but does not execute any of the other steps in the JPEG compression
DRC7a8c53e2015-06-19 16:07:14 +00001000 * process.
DRC9b28def2011-05-21 14:37:15 +00001001 *
1002 * @param handle a handle to a TurboJPEG compressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001003 *
DRC9b28def2011-05-21 14:37:15 +00001004 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
Alex Naidis6eb7d372016-10-16 23:10:08 +02001005 * to be encoded
DRC9d77dad2014-08-12 15:06:30 +00001006 *
DRC9b28def2011-05-21 14:37:15 +00001007 * @param width width (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +00001008 *
DRC40dd3142014-08-17 12:23:49 +00001009 * @param pitch bytes per line in the source image. Normally, this should be
DRC9d77dad2014-08-12 15:06:30 +00001010 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
1011 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
1012 * is padded to the nearest 32-bit boundary, as is the case for Windows
1013 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
1014 * Setting this parameter to 0 is the equivalent of setting it to
1015 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1016 *
DRC9b28def2011-05-21 14:37:15 +00001017 * @param height height (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +00001018 *
DRC25b995a2011-05-21 15:34:54 +00001019 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9d77dad2014-08-12 15:06:30 +00001020 * "Pixel formats".)
1021 *
DRC80803ae2011-12-15 13:12:59 +00001022 * @param dstBuf pointer to an image buffer that will receive the YUV image.
DRC9d77dad2014-08-12 15:06:30 +00001023 * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
1024 * on the image width, height, padding, and level of chrominance subsampling.
1025 * The Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
1026 * buffer (refer to @ref YUVnotes "YUV Image Format Notes".)
1027 *
DRCf610d612013-04-26 10:33:29 +00001028 * @param pad the width of each line in each plane of the YUV image will be
DRC9d77dad2014-08-12 15:06:30 +00001029 * padded to the nearest multiple of this number of bytes (must be a power of
1030 * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
1031 * 4.
1032 *
DRC9b28def2011-05-21 14:37:15 +00001033 * @param subsamp the level of chrominance subsampling to be used when
DRC9d77dad2014-08-12 15:06:30 +00001034 * generating the YUV image (see @ref TJSAMP
1035 * "Chrominance subsampling options".) To generate images suitable for X
1036 * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
1037 * image compatible with the I420 (AKA "YUV420P") format.
1038 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001039 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001040 * "flags"
DRC9b28def2011-05-21 14:37:15 +00001041 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001042 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1043 * and #tjGetErrorCode().)
DRC84241602011-02-25 02:08:23 +00001044*/
Leon Scroggins III3993b372018-07-16 10:43:45 -04001045DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
1046 int width, int pitch, int height, int pixelFormat,
1047 unsigned char *dstBuf, int pad, int subsamp,
1048 int flags);
DRC84241602011-02-25 02:08:23 +00001049
1050
DRC9b28def2011-05-21 14:37:15 +00001051/**
DRCaecea382014-08-11 18:05:41 +00001052 * Encode an RGB or grayscale image into separate Y, U (Cb), and V (Cr) image
1053 * planes. This function uses the accelerated color conversion routines in the
1054 * underlying codec but does not execute any of the other steps in the JPEG
1055 * compression process.
1056 *
1057 * @param handle a handle to a TurboJPEG compressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001058 *
DRCaecea382014-08-11 18:05:41 +00001059 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
Alex Naidis6eb7d372016-10-16 23:10:08 +02001060 * to be encoded
DRC9d77dad2014-08-12 15:06:30 +00001061 *
DRCaecea382014-08-11 18:05:41 +00001062 * @param width width (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +00001063 *
DRC40dd3142014-08-17 12:23:49 +00001064 * @param pitch bytes per line in the source image. Normally, this should be
DRC9d77dad2014-08-12 15:06:30 +00001065 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
1066 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
1067 * is padded to the nearest 32-bit boundary, as is the case for Windows
1068 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
1069 * Setting this parameter to 0 is the equivalent of setting it to
1070 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1071 *
DRCaecea382014-08-11 18:05:41 +00001072 * @param height height (in pixels) of the source image
DRC9d77dad2014-08-12 15:06:30 +00001073 *
DRCaecea382014-08-11 18:05:41 +00001074 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9d77dad2014-08-12 15:06:30 +00001075 * "Pixel formats".)
1076 *
DRCaecea382014-08-11 18:05:41 +00001077 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
DRC9d77dad2014-08-12 15:06:30 +00001078 * (or just a Y plane, if generating a grayscale image) that will receive the
1079 * encoded image. These planes can be contiguous or non-contiguous in memory.
DRC40dd3142014-08-17 12:23:49 +00001080 * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1081 * on the image width, height, strides, and level of chrominance subsampling.
1082 * Refer to @ref YUVnotes "YUV Image Format Notes" for more details.
DRC9d77dad2014-08-12 15:06:30 +00001083 *
DRCaecea382014-08-11 18:05:41 +00001084 * @param strides an array of integers, each specifying the number of bytes per
DRC9d77dad2014-08-12 15:06:30 +00001085 * line in the corresponding plane of the output image. Setting the stride for
DRC40dd3142014-08-17 12:23:49 +00001086 * any plane to 0 is the same as setting it to the plane width (see
1087 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1088 * the strides for all planes will be set to their respective plane widths.
1089 * You can adjust the strides in order to add an arbitrary amount of line
1090 * padding to each plane or to encode an RGB or grayscale image into a
1091 * subregion of a larger YUV planar image.
DRC9d77dad2014-08-12 15:06:30 +00001092 *
DRCaecea382014-08-11 18:05:41 +00001093 * @param subsamp the level of chrominance subsampling to be used when
DRC9d77dad2014-08-12 15:06:30 +00001094 * generating the YUV image (see @ref TJSAMP
1095 * "Chrominance subsampling options".) To generate images suitable for X
1096 * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
1097 * image compatible with the I420 (AKA "YUV420P") format.
1098 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001099 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001100 * "flags"
DRCaecea382014-08-11 18:05:41 +00001101 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001102 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1103 * and #tjGetErrorCode().)
DRCaecea382014-08-11 18:05:41 +00001104*/
Leon Scroggins III3993b372018-07-16 10:43:45 -04001105DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
1106 int width, int pitch, int height,
1107 int pixelFormat, unsigned char **dstPlanes,
1108 int *strides, int subsamp, int flags);
DRCaecea382014-08-11 18:05:41 +00001109
1110
1111/**
DRC9b28def2011-05-21 14:37:15 +00001112 * Create a TurboJPEG decompressor instance.
1113 *
1114 * @return a handle to the newly-created instance, or NULL if an error
Leon Scroggins III3993b372018-07-16 10:43:45 -04001115 * occurred (see #tjGetErrorStr2().)
DRC2e7b76b2009-04-03 12:04:24 +00001116*/
Leon Scroggins III3993b372018-07-16 10:43:45 -04001117DLLEXPORT tjhandle tjInitDecompress(void);
DRC2e7b76b2009-04-03 12:04:24 +00001118
1119
DRC9b28def2011-05-21 14:37:15 +00001120/**
1121 * Retrieve information about a JPEG image without decompressing it.
1122 *
1123 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001124 *
Alex Naidis6eb7d372016-10-16 23:10:08 +02001125 * @param jpegBuf pointer to a buffer containing a JPEG image
DRC9d77dad2014-08-12 15:06:30 +00001126 *
DRC9b28def2011-05-21 14:37:15 +00001127 * @param jpegSize size of the JPEG image (in bytes)
DRC9d77dad2014-08-12 15:06:30 +00001128 *
DRC80803ae2011-12-15 13:12:59 +00001129 * @param width pointer to an integer variable that will receive the width (in
DRC9d77dad2014-08-12 15:06:30 +00001130 * pixels) of the JPEG image
1131 *
DRC80803ae2011-12-15 13:12:59 +00001132 * @param height pointer to an integer variable that will receive the height
DRC9d77dad2014-08-12 15:06:30 +00001133 * (in pixels) of the JPEG image
1134 *
DRC80803ae2011-12-15 13:12:59 +00001135 * @param jpegSubsamp pointer to an integer variable that will receive the
DRC40dd3142014-08-17 12:23:49 +00001136 * level of chrominance subsampling used when the JPEG image was compressed
1137 * (see @ref TJSAMP "Chrominance subsampling options".)
DRC9d77dad2014-08-12 15:06:30 +00001138 *
DRCcd7c3e62013-08-23 02:49:25 +00001139 * @param jpegColorspace pointer to an integer variable that will receive one
DRC9d77dad2014-08-12 15:06:30 +00001140 * of the JPEG colorspace constants, indicating the colorspace of the JPEG
1141 * image (see @ref TJCS "JPEG colorspaces".)
DRC9b28def2011-05-21 14:37:15 +00001142 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001143 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1144 * and #tjGetErrorCode().)
DRC2e7b76b2009-04-03 12:04:24 +00001145*/
Leon Scroggins III3993b372018-07-16 10:43:45 -04001146DLLEXPORT int tjDecompressHeader3(tjhandle handle,
1147 const unsigned char *jpegBuf,
1148 unsigned long jpegSize, int *width,
1149 int *height, int *jpegSubsamp,
1150 int *jpegColorspace);
DRC2e7b76b2009-04-03 12:04:24 +00001151
1152
DRC9b28def2011-05-21 14:37:15 +00001153/**
1154 * Returns a list of fractional scaling factors that the JPEG decompressor in
1155 * this implementation of TurboJPEG supports.
1156 *
1157 * @param numscalingfactors pointer to an integer variable that will receive
DRC9d77dad2014-08-12 15:06:30 +00001158 * the number of elements in the list
DRC9b28def2011-05-21 14:37:15 +00001159 *
1160 * @return a pointer to a list of fractional scaling factors, or NULL if an
Leon Scroggins III3993b372018-07-16 10:43:45 -04001161 * error is encountered (see #tjGetErrorStr2().)
DRCb28fc572011-02-22 06:41:29 +00001162*/
Leon Scroggins III3993b372018-07-16 10:43:45 -04001163DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
DRCb28fc572011-02-22 06:41:29 +00001164
1165
DRC9b28def2011-05-21 14:37:15 +00001166/**
DRCcd7c3e62013-08-23 02:49:25 +00001167 * Decompress a JPEG image to an RGB, grayscale, or CMYK image.
DRC9b28def2011-05-21 14:37:15 +00001168 *
1169 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001170 *
Alex Naidis6eb7d372016-10-16 23:10:08 +02001171 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
DRC9d77dad2014-08-12 15:06:30 +00001172 *
DRC9b28def2011-05-21 14:37:15 +00001173 * @param jpegSize size of the JPEG image (in bytes)
DRC9d77dad2014-08-12 15:06:30 +00001174 *
DRC80803ae2011-12-15 13:12:59 +00001175 * @param dstBuf pointer to an image buffer that will receive the decompressed
DRC9d77dad2014-08-12 15:06:30 +00001176 * image. This buffer should normally be <tt>pitch * scaledHeight</tt> bytes
1177 * in size, where <tt>scaledHeight</tt> can be determined by calling
1178 * #TJSCALED() with the JPEG image height and one of the scaling factors
1179 * returned by #tjGetScalingFactors(). The <tt>dstBuf</tt> pointer may also be
1180 * used to decompress into a specific region of a larger buffer.
1181 *
DRC9b28def2011-05-21 14:37:15 +00001182 * @param width desired width (in pixels) of the destination image. If this is
DRC9d77dad2014-08-12 15:06:30 +00001183 * different than the width of the JPEG image being decompressed, then
1184 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1185 * possible image that will fit within the desired width. If <tt>width</tt> is
1186 * set to 0, then only the height will be considered when determining the
1187 * scaled image size.
1188 *
DRC40dd3142014-08-17 12:23:49 +00001189 * @param pitch bytes per line in the destination image. Normally, this is
DRC9d77dad2014-08-12 15:06:30 +00001190 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed image
1191 * is unpadded, else <tt>#TJPAD(scaledWidth * #tjPixelSize[pixelFormat])</tt>
1192 * if each line of the decompressed image is padded to the nearest 32-bit
1193 * boundary, as is the case for Windows bitmaps. (NOTE: <tt>scaledWidth</tt>
1194 * can be determined by calling #TJSCALED() with the JPEG image width and one
1195 * of the scaling factors returned by #tjGetScalingFactors().) You can also be
1196 * clever and use the pitch parameter to skip lines, etc. Setting this
1197 * parameter to 0 is the equivalent of setting it to
1198 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>.
1199 *
DRC9b28def2011-05-21 14:37:15 +00001200 * @param height desired height (in pixels) of the destination image. If this
DRC9d77dad2014-08-12 15:06:30 +00001201 * is different than the height of the JPEG image being decompressed, then
1202 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1203 * possible image that will fit within the desired height. If <tt>height</tt>
1204 * is set to 0, then only the width will be considered when determining the
1205 * scaled image size.
1206 *
DRC9b28def2011-05-21 14:37:15 +00001207 * @param pixelFormat pixel format of the destination image (see @ref
DRC9d77dad2014-08-12 15:06:30 +00001208 * TJPF "Pixel formats".)
1209 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001210 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001211 * "flags"
DRC9b28def2011-05-21 14:37:15 +00001212 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001213 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1214 * and #tjGetErrorCode().)
DRC9b28def2011-05-21 14:37:15 +00001215 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001216DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
1217 unsigned long jpegSize, unsigned char *dstBuf,
1218 int width, int pitch, int height, int pixelFormat,
1219 int flags);
DRC2e7b76b2009-04-03 12:04:24 +00001220
1221
DRC9b28def2011-05-21 14:37:15 +00001222/**
1223 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
1224 * decompression but leaves out the color conversion step, so a planar YUV
DRC493be612014-08-10 20:12:17 +00001225 * image is generated instead of an RGB image.
DRC9b28def2011-05-21 14:37:15 +00001226 *
1227 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001228 *
Alex Naidis6eb7d372016-10-16 23:10:08 +02001229 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
DRC9d77dad2014-08-12 15:06:30 +00001230 *
DRC9b28def2011-05-21 14:37:15 +00001231 * @param jpegSize size of the JPEG image (in bytes)
DRC9d77dad2014-08-12 15:06:30 +00001232 *
DRC80803ae2011-12-15 13:12:59 +00001233 * @param dstBuf pointer to an image buffer that will receive the YUV image.
DRC9d77dad2014-08-12 15:06:30 +00001234 * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
1235 * on the image width, height, padding, and level of subsampling. The Y,
1236 * U (Cb), and V (Cr) image planes will be stored sequentially in the buffer
1237 * (refer to @ref YUVnotes "YUV Image Format Notes".)
1238 *
DRCf610d612013-04-26 10:33:29 +00001239 * @param width desired width (in pixels) of the YUV image. If this is
DRC9d77dad2014-08-12 15:06:30 +00001240 * different than the width of the JPEG image being decompressed, then
1241 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1242 * possible image that will fit within the desired width. If <tt>width</tt> is
1243 * set to 0, then only the height will be considered when determining the
1244 * scaled image size. If the scaled width is not an even multiple of the MCU
1245 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1246 * performed within TurboJPEG.
1247 *
DRCf610d612013-04-26 10:33:29 +00001248 * @param pad the width of each line in each plane of the YUV image will be
DRC9d77dad2014-08-12 15:06:30 +00001249 * padded to the nearest multiple of this number of bytes (must be a power of
1250 * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
1251 * 4.
1252 *
DRCf610d612013-04-26 10:33:29 +00001253 * @param height desired height (in pixels) of the YUV image. If this is
DRC9d77dad2014-08-12 15:06:30 +00001254 * different than the height of the JPEG image being decompressed, then
1255 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1256 * possible image that will fit within the desired height. If <tt>height</tt>
1257 * is set to 0, then only the width will be considered when determining the
1258 * scaled image size. If the scaled height is not an even multiple of the MCU
1259 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1260 * performed within TurboJPEG.
1261 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001262 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001263 * "flags"
DRC9b28def2011-05-21 14:37:15 +00001264 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001265 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1266 * and #tjGetErrorCode().)
DRC9b28def2011-05-21 14:37:15 +00001267 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001268DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
1269 unsigned long jpegSize, unsigned char *dstBuf,
1270 int width, int pad, int height, int flags);
DRC84241602011-02-25 02:08:23 +00001271
1272
DRC9b28def2011-05-21 14:37:15 +00001273/**
DRCaecea382014-08-11 18:05:41 +00001274 * Decompress a JPEG image into separate Y, U (Cb), and V (Cr) image
1275 * planes. This function performs JPEG decompression but leaves out the color
1276 * conversion step, so a planar YUV image is generated instead of an RGB image.
1277 *
1278 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001279 *
Alex Naidis6eb7d372016-10-16 23:10:08 +02001280 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
DRC9d77dad2014-08-12 15:06:30 +00001281 *
DRCaecea382014-08-11 18:05:41 +00001282 * @param jpegSize size of the JPEG image (in bytes)
DRC9d77dad2014-08-12 15:06:30 +00001283 *
DRCaecea382014-08-11 18:05:41 +00001284 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
DRC9d77dad2014-08-12 15:06:30 +00001285 * (or just a Y plane, if decompressing a grayscale image) that will receive
1286 * the YUV image. These planes can be contiguous or non-contiguous in memory.
DRC40dd3142014-08-17 12:23:49 +00001287 * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1288 * on the scaled image width, scaled image height, strides, and level of
1289 * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
1290 * for more details.
DRC9d77dad2014-08-12 15:06:30 +00001291 *
DRCaecea382014-08-11 18:05:41 +00001292 * @param width desired width (in pixels) of the YUV image. If this is
DRC9d77dad2014-08-12 15:06:30 +00001293 * different than the width of the JPEG image being decompressed, then
1294 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1295 * possible image that will fit within the desired width. If <tt>width</tt> is
1296 * set to 0, then only the height will be considered when determining the
1297 * scaled image size. If the scaled width is not an even multiple of the MCU
1298 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1299 * performed within TurboJPEG.
1300 *
DRCaecea382014-08-11 18:05:41 +00001301 * @param strides an array of integers, each specifying the number of bytes per
DRC9d77dad2014-08-12 15:06:30 +00001302 * line in the corresponding plane of the output image. Setting the stride for
DRC40dd3142014-08-17 12:23:49 +00001303 * any plane to 0 is the same as setting it to the scaled plane width (see
1304 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1305 * the strides for all planes will be set to their respective scaled plane
1306 * widths. You can adjust the strides in order to add an arbitrary amount of
1307 * line padding to each plane or to decompress the JPEG image into a subregion
1308 * of a larger YUV planar image.
DRC9d77dad2014-08-12 15:06:30 +00001309 *
DRCaecea382014-08-11 18:05:41 +00001310 * @param height desired height (in pixels) of the YUV image. If this is
DRC9d77dad2014-08-12 15:06:30 +00001311 * different than the height of the JPEG image being decompressed, then
1312 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1313 * possible image that will fit within the desired height. If <tt>height</tt>
1314 * is set to 0, then only the width will be considered when determining the
1315 * scaled image size. If the scaled height is not an even multiple of the MCU
1316 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1317 * performed within TurboJPEG.
1318 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001319 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001320 * "flags"
DRCaecea382014-08-11 18:05:41 +00001321 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001322 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1323 * and #tjGetErrorCode().)
DRCaecea382014-08-11 18:05:41 +00001324 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001325DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
1326 const unsigned char *jpegBuf,
1327 unsigned long jpegSize,
1328 unsigned char **dstPlanes, int width,
1329 int *strides, int height, int flags);
DRCaecea382014-08-11 18:05:41 +00001330
1331
1332/**
DRC34dca052014-02-28 09:17:14 +00001333 * Decode a YUV planar image into an RGB or grayscale image. This function
1334 * uses the accelerated color conversion routines in the underlying
1335 * codec but does not execute any of the other steps in the JPEG decompression
DRC493be612014-08-10 20:12:17 +00001336 * process.
DRC34dca052014-02-28 09:17:14 +00001337 *
1338 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001339 *
DRC34dca052014-02-28 09:17:14 +00001340 * @param srcBuf pointer to an image buffer containing a YUV planar image to be
DRC9d77dad2014-08-12 15:06:30 +00001341 * decoded. The size of this buffer should match the value returned by
1342 * #tjBufSizeYUV2() for the given image width, height, padding, and level of
1343 * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
1344 * stored sequentially in the source buffer (refer to @ref YUVnotes
Alex Naidis6eb7d372016-10-16 23:10:08 +02001345 * "YUV Image Format Notes".)
DRC9d77dad2014-08-12 15:06:30 +00001346 *
DRC34dca052014-02-28 09:17:14 +00001347 * @param pad Use this parameter to specify that the width of each line in each
DRC9d77dad2014-08-12 15:06:30 +00001348 * plane of the YUV source image is padded to the nearest multiple of this
1349 * number of bytes (must be a power of 2.)
1350 *
DRC34dca052014-02-28 09:17:14 +00001351 * @param subsamp the level of chrominance subsampling used in the YUV source
DRC9d77dad2014-08-12 15:06:30 +00001352 * image (see @ref TJSAMP "Chrominance subsampling options".)
1353 *
DRC34dca052014-02-28 09:17:14 +00001354 * @param dstBuf pointer to an image buffer that will receive the decoded
DRC9d77dad2014-08-12 15:06:30 +00001355 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1356 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1357 * specific region of a larger buffer.
1358 *
DRC34dca052014-02-28 09:17:14 +00001359 * @param width width (in pixels) of the source and destination images
DRC9d77dad2014-08-12 15:06:30 +00001360 *
DRC40dd3142014-08-17 12:23:49 +00001361 * @param pitch bytes per line in the destination image. Normally, this should
DRC9d77dad2014-08-12 15:06:30 +00001362 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1363 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1364 * of the destination image should be padded to the nearest 32-bit boundary, as
1365 * is the case for Windows bitmaps. You can also be clever and use the pitch
1366 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1367 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1368 *
DRC34dca052014-02-28 09:17:14 +00001369 * @param height height (in pixels) of the source and destination images
DRC9d77dad2014-08-12 15:06:30 +00001370 *
DRC34dca052014-02-28 09:17:14 +00001371 * @param pixelFormat pixel format of the destination image (see @ref TJPF
DRC9d77dad2014-08-12 15:06:30 +00001372 * "Pixel formats".)
1373 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001374 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001375 * "flags"
DRC34dca052014-02-28 09:17:14 +00001376 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001377 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1378 * and #tjGetErrorCode().)
DRC34dca052014-02-28 09:17:14 +00001379 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001380DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
1381 int pad, int subsamp, unsigned char *dstBuf,
1382 int width, int pitch, int height, int pixelFormat,
1383 int flags);
DRC34dca052014-02-28 09:17:14 +00001384
1385
1386/**
DRCaecea382014-08-11 18:05:41 +00001387 * Decode a set of Y, U (Cb), and V (Cr) image planes into an RGB or grayscale
1388 * image. This function uses the accelerated color conversion routines in the
1389 * underlying codec but does not execute any of the other steps in the JPEG
1390 * decompression process.
1391 *
1392 * @param handle a handle to a TurboJPEG decompressor or transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001393 *
DRCaecea382014-08-11 18:05:41 +00001394 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
DRC9d77dad2014-08-12 15:06:30 +00001395 * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1396 * to be decoded. These planes can be contiguous or non-contiguous in memory.
DRC40dd3142014-08-17 12:23:49 +00001397 * The size of each plane should match the value returned by #tjPlaneSizeYUV()
1398 * for the given image width, height, strides, and level of chrominance
1399 * subsampling. Refer to @ref YUVnotes "YUV Image Format Notes" for more
Alex Naidis6eb7d372016-10-16 23:10:08 +02001400 * details.
DRC9d77dad2014-08-12 15:06:30 +00001401 *
DRCaecea382014-08-11 18:05:41 +00001402 * @param strides an array of integers, each specifying the number of bytes per
DRC9d77dad2014-08-12 15:06:30 +00001403 * line in the corresponding plane of the YUV source image. Setting the stride
DRC40dd3142014-08-17 12:23:49 +00001404 * for any plane to 0 is the same as setting it to the plane width (see
1405 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1406 * the strides for all planes will be set to their respective plane widths.
1407 * You can adjust the strides in order to specify an arbitrary amount of line
1408 * padding in each plane or to decode a subregion of a larger YUV planar image.
DRC9d77dad2014-08-12 15:06:30 +00001409 *
DRCaecea382014-08-11 18:05:41 +00001410 * @param subsamp the level of chrominance subsampling used in the YUV source
DRC9d77dad2014-08-12 15:06:30 +00001411 * image (see @ref TJSAMP "Chrominance subsampling options".)
1412 *
DRCaecea382014-08-11 18:05:41 +00001413 * @param dstBuf pointer to an image buffer that will receive the decoded
DRC9d77dad2014-08-12 15:06:30 +00001414 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1415 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1416 * specific region of a larger buffer.
1417 *
DRCaecea382014-08-11 18:05:41 +00001418 * @param width width (in pixels) of the source and destination images
DRC9d77dad2014-08-12 15:06:30 +00001419 *
DRC40dd3142014-08-17 12:23:49 +00001420 * @param pitch bytes per line in the destination image. Normally, this should
DRC9d77dad2014-08-12 15:06:30 +00001421 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1422 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1423 * of the destination image should be padded to the nearest 32-bit boundary, as
1424 * is the case for Windows bitmaps. You can also be clever and use the pitch
1425 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1426 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1427 *
DRCaecea382014-08-11 18:05:41 +00001428 * @param height height (in pixels) of the source and destination images
DRC9d77dad2014-08-12 15:06:30 +00001429 *
DRCaecea382014-08-11 18:05:41 +00001430 * @param pixelFormat pixel format of the destination image (see @ref TJPF
DRC9d77dad2014-08-12 15:06:30 +00001431 * "Pixel formats".)
1432 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001433 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001434 * "flags"
DRCaecea382014-08-11 18:05:41 +00001435 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001436 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1437 * and #tjGetErrorCode().)
DRCaecea382014-08-11 18:05:41 +00001438 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001439DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
1440 const unsigned char **srcPlanes,
1441 const int *strides, int subsamp,
1442 unsigned char *dstBuf, int width, int pitch,
1443 int height, int pixelFormat, int flags);
DRCaecea382014-08-11 18:05:41 +00001444
1445
1446/**
DRC9b28def2011-05-21 14:37:15 +00001447 * Create a new TurboJPEG transformer instance.
1448 *
1449 * @return a handle to the newly-created instance, or NULL if an error
Leon Scroggins III3993b372018-07-16 10:43:45 -04001450 * occurred (see #tjGetErrorStr2().)
DRC9b28def2011-05-21 14:37:15 +00001451 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001452DLLEXPORT tjhandle tjInitTransform(void);
DRC890f1e02011-02-26 22:02:37 +00001453
1454
DRC9b28def2011-05-21 14:37:15 +00001455/**
1456 * Losslessly transform a JPEG image into another JPEG image. Lossless
DRC40dd3142014-08-17 12:23:49 +00001457 * transforms work by moving the raw DCT coefficients from one JPEG image
1458 * structure to another without altering the values of the coefficients. While
1459 * this is typically faster than decompressing the image, transforming it, and
DRC9b28def2011-05-21 14:37:15 +00001460 * re-compressing it, lossless transforms are not free. Each lossless
DRC01fdcc32013-04-26 08:41:25 +00001461 * transform requires reading and performing Huffman decoding on all of the
1462 * coefficients in the source image, regardless of the size of the destination
1463 * image. Thus, this function provides a means of generating multiple
1464 * transformed images from the same source or applying multiple
1465 * transformations simultaneously, in order to eliminate the need to read the
1466 * source coefficients multiple times.
DRC9b28def2011-05-21 14:37:15 +00001467 *
1468 * @param handle a handle to a TurboJPEG transformer instance
DRC9d77dad2014-08-12 15:06:30 +00001469 *
DRC40dd3142014-08-17 12:23:49 +00001470 * @param jpegBuf pointer to a buffer containing the JPEG source image to
Alex Naidis6eb7d372016-10-16 23:10:08 +02001471 * transform
DRC9d77dad2014-08-12 15:06:30 +00001472 *
DRC40dd3142014-08-17 12:23:49 +00001473 * @param jpegSize size of the JPEG source image (in bytes)
DRC9d77dad2014-08-12 15:06:30 +00001474 *
DRC9b28def2011-05-21 14:37:15 +00001475 * @param n the number of transformed JPEG images to generate
DRC9d77dad2014-08-12 15:06:30 +00001476 *
DRC9b28def2011-05-21 14:37:15 +00001477 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
DRC9d77dad2014-08-12 15:06:30 +00001478 * will receive a JPEG image that has been transformed using the parameters in
1479 * <tt>transforms[i]</tt>. TurboJPEG has the ability to reallocate the JPEG
1480 * buffer to accommodate the size of the JPEG image. Thus, you can choose to:
1481 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
1482 * let TurboJPEG grow the buffer as needed,
1483 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the buffer
1484 * for you, or
1485 * -# pre-allocate the buffer to a "worst case" size determined by calling
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001486 * #tjBufSize() with the transformed or cropped width and height. Under normal
1487 * circumstances, this should ensure that the buffer never has to be
1488 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.) Note,
1489 * however, that there are some rare cases (such as transforming images with a
1490 * large amount of embedded EXIF or ICC profile data) in which the output image
1491 * will be larger than the worst-case size, and #TJFLAG_NOREALLOC cannot be
1492 * used in those cases.
DRC9d77dad2014-08-12 15:06:30 +00001493 * .
1494 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to the size of
1495 * your pre-allocated buffer. In any case, unless you have set
1496 * #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> upon return
1497 * from this function, as it may have changed.
1498 *
DRC80803ae2011-12-15 13:12:59 +00001499 * @param dstSizes pointer to an array of n unsigned long variables that will
DRC9d77dad2014-08-12 15:06:30 +00001500 * receive the actual sizes (in bytes) of each transformed JPEG image. If
1501 * <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
1502 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon return,
1503 * <tt>dstSizes[i]</tt> will contain the size of the JPEG image (in bytes.)
1504 *
DRC01fdcc32013-04-26 08:41:25 +00001505 * @param transforms pointer to an array of n #tjtransform structures, each of
DRC9d77dad2014-08-12 15:06:30 +00001506 * which specifies the transform parameters and/or cropping region for the
1507 * corresponding transformed output image.
1508 *
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001509 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
DRC40dd3142014-08-17 12:23:49 +00001510 * "flags"
DRC9b28def2011-05-21 14:37:15 +00001511 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001512 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1513 * and #tjGetErrorCode().)
DRC9b28def2011-05-21 14:37:15 +00001514 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001515DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
1516 unsigned long jpegSize, int n,
1517 unsigned char **dstBufs, unsigned long *dstSizes,
1518 tjtransform *transforms, int flags);
DRC890f1e02011-02-26 22:02:37 +00001519
1520
DRC9b28def2011-05-21 14:37:15 +00001521/**
1522 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
1523 *
1524 * @param handle a handle to a TurboJPEG compressor, decompressor or
DRC9d77dad2014-08-12 15:06:30 +00001525 * transformer instance
DRC9b28def2011-05-21 14:37:15 +00001526 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001527 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
DRC9b28def2011-05-21 14:37:15 +00001528 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001529DLLEXPORT int tjDestroy(tjhandle handle);
DRC2e7b76b2009-04-03 12:04:24 +00001530
1531
DRC9b28def2011-05-21 14:37:15 +00001532/**
DRC6b76f752011-05-24 16:52:47 +00001533 * Allocate an image buffer for use with TurboJPEG. You should always use
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001534 * this function to allocate the JPEG destination buffer(s) for the compression
1535 * and transform functions unless you are disabling automatic buffer
DRC6b76f752011-05-24 16:52:47 +00001536 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
1537 *
1538 * @param bytes the number of bytes to allocate
DRC1a45b812014-05-09 18:06:58 +00001539 *
DRC6b76f752011-05-24 16:52:47 +00001540 * @return a pointer to a newly-allocated buffer with the specified number of
DRC40dd3142014-08-17 12:23:49 +00001541 * bytes.
DRCd4411072011-05-24 17:00:15 +00001542 *
1543 * @sa tjFree()
DRC6b76f752011-05-24 16:52:47 +00001544 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001545DLLEXPORT unsigned char *tjAlloc(int bytes);
1546
1547
1548/**
1549 * Load an uncompressed image from disk into memory.
1550 *
1551 * @param filename name of a file containing an uncompressed image in Windows
1552 * BMP or PBMPLUS (PPM/PGM) format
1553 *
1554 * @param width pointer to an integer variable that will receive the width (in
1555 * pixels) of the uncompressed image
1556 *
1557 * @param align row alignment of the image buffer to be returned (must be a
1558 * power of 2.) For instance, setting this parameter to 4 will cause all rows
1559 * in the image buffer to be padded to the nearest 32-bit boundary, and setting
1560 * this parameter to 1 will cause all rows in the image buffer to be unpadded.
1561 *
1562 * @param height pointer to an integer variable that will receive the height
1563 * (in pixels) of the uncompressed image
1564 *
1565 * @param pixelFormat pointer to an integer variable that specifies or will
1566 * receive the pixel format of the uncompressed image buffer. The behavior of
1567 * #tjLoadImage() will vary depending on the value of <tt>*pixelFormat</tt>
1568 * passed to the function:
1569 * - @ref TJPF_UNKNOWN : The uncompressed image buffer returned by the function
1570 * will use the most optimal pixel format for the file type, and
1571 * <tt>*pixelFormat</tt> will contain the ID of this pixel format upon
1572 * successful return from the function.
1573 * - @ref TJPF_GRAY : Only PGM files and 8-bit BMP files with a grayscale
1574 * colormap can be loaded.
1575 * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1576 * converted using a quick & dirty algorithm that is suitable only for testing
1577 * purposes (proper conversion between CMYK and other formats requires a color
1578 * management system.)
1579 * - Other @ref TJPF "pixel formats" : The uncompressed image buffer will use
1580 * the specified pixel format, and pixel format conversion will be performed if
1581 * necessary.
1582 *
1583 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1584 * "flags".
1585 *
1586 * @return a pointer to a newly-allocated buffer containing the uncompressed
1587 * image, converted to the chosen pixel format and with the chosen row
1588 * alignment, or NULL if an error occurred (see #tjGetErrorStr2().) This
1589 * buffer should be freed using #tjFree().
1590 */
1591DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
1592 int align, int *height, int *pixelFormat,
1593 int flags);
1594
1595
1596/**
1597 * Save an uncompressed image from memory to disk.
1598 *
1599 * @param filename name of a file to which to save the uncompressed image.
1600 * The image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format,
1601 * depending on the file extension.
1602 *
1603 * @param buffer pointer to an image buffer containing RGB, grayscale, or
1604 * CMYK pixels to be saved
1605 *
1606 * @param width width (in pixels) of the uncompressed image
1607 *
1608 * @param pitch bytes per line in the image buffer. Setting this parameter to
1609 * 0 is the equivalent of setting it to
1610 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1611 *
1612 * @param height height (in pixels) of the uncompressed image
1613 *
1614 * @param pixelFormat pixel format of the image buffer (see @ref TJPF
1615 * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
1616 * image will be stored in PGM or 8-bit (indexed color) BMP format. Otherwise,
1617 * the image will be stored in PPM or 24-bit BMP format. If this parameter
1618 * is set to @ref TJPF_CMYK, then the CMYK pixels will be converted to RGB
1619 * using a quick & dirty algorithm that is suitable only for testing (proper
1620 * conversion between CMYK and other formats requires a color management
1621 * system.)
1622 *
1623 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1624 * "flags".
1625 *
1626 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1627 */
1628DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
1629 int width, int pitch, int height, int pixelFormat,
1630 int flags);
DRC6b76f752011-05-24 16:52:47 +00001631
1632
1633/**
1634 * Free an image buffer previously allocated by TurboJPEG. You should always
1635 * use this function to free JPEG destination buffer(s) that were automatically
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05001636 * (re)allocated by the compression and transform functions or that were
1637 * manually allocated using #tjAlloc().
DRC6b76f752011-05-24 16:52:47 +00001638 *
1639 * @param buffer address of the buffer to free
DRCd4411072011-05-24 17:00:15 +00001640 *
1641 * @sa tjAlloc()
DRC6b76f752011-05-24 16:52:47 +00001642 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001643DLLEXPORT void tjFree(unsigned char *buffer);
DRC6b76f752011-05-24 16:52:47 +00001644
1645
1646/**
DRC9b28def2011-05-21 14:37:15 +00001647 * Returns a descriptive error message explaining why the last command failed.
1648 *
Leon Scroggins III3993b372018-07-16 10:43:45 -04001649 * @param handle a handle to a TurboJPEG compressor, decompressor, or
1650 * transformer instance, or NULL if the error was generated by a global
1651 * function (but note that retrieving the error message for a global function
1652 * is not thread-safe.)
1653 *
DRC9b28def2011-05-21 14:37:15 +00001654 * @return a descriptive error message explaining why the last command failed.
1655 */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001656DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
1657
1658
1659/**
1660 * Returns a code indicating the severity of the last error. See
1661 * @ref TJERR "Error codes".
1662 *
1663 * @param handle a handle to a TurboJPEG compressor, decompressor or
1664 * transformer instance
1665 *
1666 * @return a code indicating the severity of the last error. See
1667 * @ref TJERR "Error codes".
1668 */
1669DLLEXPORT int tjGetErrorCode(tjhandle handle);
DRC2e7b76b2009-04-03 12:04:24 +00001670
DRC9b28def2011-05-21 14:37:15 +00001671
DRC07e982d2013-10-31 07:11:39 +00001672/* Deprecated functions and macros */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001673#define TJFLAG_FORCEMMX 8
1674#define TJFLAG_FORCESSE 16
1675#define TJFLAG_FORCESSE2 32
1676#define TJFLAG_FORCESSE3 128
DRC07e982d2013-10-31 07:11:39 +00001677
1678
DRC9b28def2011-05-21 14:37:15 +00001679/* Backward compatibility functions and macros (nothing to see here) */
Leon Scroggins III3993b372018-07-16 10:43:45 -04001680#define NUMSUBOPT TJ_NUMSAMP
1681#define TJ_444 TJSAMP_444
1682#define TJ_422 TJSAMP_422
1683#define TJ_420 TJSAMP_420
1684#define TJ_411 TJSAMP_420
1685#define TJ_GRAYSCALE TJSAMP_GRAY
DRC9b28def2011-05-21 14:37:15 +00001686
Leon Scroggins III3993b372018-07-16 10:43:45 -04001687#define TJ_BGR 1
1688#define TJ_BOTTOMUP TJFLAG_BOTTOMUP
1689#define TJ_FORCEMMX TJFLAG_FORCEMMX
1690#define TJ_FORCESSE TJFLAG_FORCESSE
1691#define TJ_FORCESSE2 TJFLAG_FORCESSE2
1692#define TJ_ALPHAFIRST 64
1693#define TJ_FORCESSE3 TJFLAG_FORCESSE3
1694#define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
1695#define TJ_YUV 512
DRC9b28def2011-05-21 14:37:15 +00001696
Leon Scroggins III3993b372018-07-16 10:43:45 -04001697DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
DRC9b49f0e2011-07-12 03:17:23 +00001698
Leon Scroggins III3993b372018-07-16 10:43:45 -04001699DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
DRC9b49f0e2011-07-12 03:17:23 +00001700
Leon Scroggins III3993b372018-07-16 10:43:45 -04001701DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
DRCf610d612013-04-26 10:33:29 +00001702
Leon Scroggins III3993b372018-07-16 10:43:45 -04001703DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
1704 int pitch, int height, int pixelSize,
1705 unsigned char *dstBuf, unsigned long *compressedSize,
1706 int jpegSubsamp, int jpegQual, int flags);
DRC9b28def2011-05-21 14:37:15 +00001707
Leon Scroggins III3993b372018-07-16 10:43:45 -04001708DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
1709 int pitch, int height, int pixelSize,
1710 unsigned char *dstBuf, int subsamp, int flags);
DRC9b28def2011-05-21 14:37:15 +00001711
Leon Scroggins III3993b372018-07-16 10:43:45 -04001712DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
1713 int pitch, int height, int pixelFormat,
1714 unsigned char *dstBuf, int subsamp, int flags);
DRCf610d612013-04-26 10:33:29 +00001715
Leon Scroggins III3993b372018-07-16 10:43:45 -04001716DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
1717 unsigned long jpegSize, int *width,
1718 int *height);
DRC9b28def2011-05-21 14:37:15 +00001719
Leon Scroggins III3993b372018-07-16 10:43:45 -04001720DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
1721 unsigned long jpegSize, int *width,
1722 int *height, int *jpegSubsamp);
DRCcd7c3e62013-08-23 02:49:25 +00001723
Leon Scroggins III3993b372018-07-16 10:43:45 -04001724DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
1725 unsigned long jpegSize, unsigned char *dstBuf,
1726 int width, int pitch, int height, int pixelSize,
1727 int flags);
DRC9b28def2011-05-21 14:37:15 +00001728
Leon Scroggins III3993b372018-07-16 10:43:45 -04001729DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
1730 unsigned long jpegSize, unsigned char *dstBuf,
1731 int flags);
1732
1733DLLEXPORT char *tjGetErrorStr(void);
DRCf610d612013-04-26 10:33:29 +00001734
DRC9b28def2011-05-21 14:37:15 +00001735
1736/**
1737 * @}
1738 */
1739
DRC2e7b76b2009-04-03 12:04:24 +00001740#ifdef __cplusplus
1741}
1742#endif
DRC3a1bb352011-05-24 09:15:44 +00001743
1744#endif