blob: c2f6b5141deaa32fa4461d7e9819ea1ecd07c278 [file] [log] [blame]
hbono@chromium.org98626972011-08-03 03:13:08 +00001/*
Jonathan Wright24e31052021-04-26 12:10:48 +01002 * Copyright (C)2009-2015, 2017, 2020-2021 D. R. Commander.
3 * All Rights Reserved.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00004 *
hbono@chromium.org98626972011-08-03 03:13:08 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00007 *
hbono@chromium.org98626972011-08-03 03:13:08 +00008 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of the libjpeg-turbo Project nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000028 */
29
hbono@chromium.org98626972011-08-03 03:13:08 +000030#ifndef __TURBOJPEG_H__
31#define __TURBOJPEG_H__
32
33#if defined(_WIN32) && defined(DLLDEFINE)
Chris Blumecca8c4d2019-03-01 01:09:50 -080034#define DLLEXPORT __declspec(dllexport)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000035#else
36#define DLLEXPORT
37#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000038#define DLLCALL
39
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000040
hbono@chromium.org98626972011-08-03 03:13:08 +000041/**
42 * @addtogroup TurboJPEG
43 * TurboJPEG API. This API provides an interface for generating, decoding, and
44 * transforming planar YUV and JPEG images in memory.
45 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -040046 * @anchor YUVnotes
47 * YUV Image Format Notes
48 * ----------------------
49 * Technically, the JPEG format uses the YCbCr colorspace (which is technically
50 * not a colorspace but a color transform), but per the convention of the
51 * digital video community, the TurboJPEG API uses "YUV" to refer to an image
52 * format consisting of Y, Cb, and Cr image planes.
53 *
54 * Each plane is simply a 2D array of bytes, each byte representing the value
55 * of one of the components (Y, Cb, or Cr) at a particular location in the
56 * image. The width and height of each plane are determined by the image
57 * width, height, and level of chrominance subsampling. The luminance plane
58 * width is the image width padded to the nearest multiple of the horizontal
59 * subsampling factor (2 in the case of 4:2:0 and 4:2:2, 4 in the case of
60 * 4:1:1, 1 in the case of 4:4:4 or grayscale.) Similarly, the luminance plane
61 * height is the image height padded to the nearest multiple of the vertical
62 * subsampling factor (2 in the case of 4:2:0 or 4:4:0, 1 in the case of 4:4:4
63 * or grayscale.) This is irrespective of any additional padding that may be
64 * specified as an argument to the various YUV functions. The chrominance
65 * plane width is equal to the luminance plane width divided by the horizontal
66 * subsampling factor, and the chrominance plane height is equal to the
67 * luminance plane height divided by the vertical subsampling factor.
68 *
69 * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
70 * used, then the luminance plane would be 36 x 35 bytes, and each of the
71 * chrominance planes would be 18 x 35 bytes. If you specify a line padding of
72 * 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes, and
73 * each of the chrominance planes would be 20 x 35 bytes.
74 *
hbono@chromium.org98626972011-08-03 03:13:08 +000075 * @{
76 */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000077
hbono@chromium.org98626972011-08-03 03:13:08 +000078
79/**
80 * The number of chrominance subsampling options
81 */
Chris Blumecca8c4d2019-03-01 01:09:50 -080082#define TJ_NUMSAMP 6
hbono@chromium.org98626972011-08-03 03:13:08 +000083
84/**
85 * Chrominance subsampling options.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040086 * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
87 * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
88 * the Cb and Cr (chrominance) components can be discarded or averaged together
89 * to produce a smaller image with little perceptible loss of image clarity
90 * (the human eye is more sensitive to small changes in brightness than to
91 * small changes in color.) This is called "chrominance subsampling".
hbono@chromium.org98626972011-08-03 03:13:08 +000092 */
Chris Blumecca8c4d2019-03-01 01:09:50 -080093enum TJSAMP {
hbono@chromium.org98626972011-08-03 03:13:08 +000094 /**
95 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
96 * YUV image will contain one chrominance component for every pixel in the
97 * source image.
98 */
Chris Blumecca8c4d2019-03-01 01:09:50 -080099 TJSAMP_444 = 0,
hbono@chromium.org98626972011-08-03 03:13:08 +0000100 /**
101 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
102 * chrominance component for every 2x1 block of pixels in the source image.
103 */
104 TJSAMP_422,
105 /**
106 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
107 * chrominance component for every 2x2 block of pixels in the source image.
108 */
109 TJSAMP_420,
110 /**
111 * Grayscale. The JPEG or YUV image will contain no chrominance components.
112 */
113 TJSAMP_GRAY,
114 /**
115 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
116 * chrominance component for every 1x2 block of pixels in the source image.
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400117 *
118 * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
hbono@chromium.org98626972011-08-03 03:13:08 +0000119 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400120 TJSAMP_440,
121 /**
122 * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
123 * chrominance component for every 4x1 block of pixels in the source image.
124 * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
125 * same size as those compressed with 4:2:0 subsampling, and in the
126 * aggregate, both subsampling methods produce approximately the same
127 * perceptual quality. However, 4:1:1 is better able to reproduce sharp
128 * horizontal features.
129 *
130 * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
131 */
132 TJSAMP_411
hbono@chromium.org98626972011-08-03 03:13:08 +0000133};
134
135/**
136 * MCU block width (in pixels) for a given level of chrominance subsampling.
137 * MCU block sizes:
138 * - 8x8 for no subsampling or grayscale
139 * - 16x8 for 4:2:2
140 * - 8x16 for 4:4:0
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400141 * - 16x16 for 4:2:0
142 * - 32x8 for 4:1:1
hbono@chromium.org98626972011-08-03 03:13:08 +0000143 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800144static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32 };
hbono@chromium.org98626972011-08-03 03:13:08 +0000145
146/**
147 * MCU block height (in pixels) for a given level of chrominance subsampling.
148 * MCU block sizes:
149 * - 8x8 for no subsampling or grayscale
150 * - 16x8 for 4:2:2
151 * - 8x16 for 4:4:0
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400152 * - 16x16 for 4:2:0
153 * - 32x8 for 4:1:1
hbono@chromium.org98626972011-08-03 03:13:08 +0000154 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800155static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8 };
hbono@chromium.org98626972011-08-03 03:13:08 +0000156
157
158/**
159 * The number of pixel formats
160 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800161#define TJ_NUMPF 12
hbono@chromium.org98626972011-08-03 03:13:08 +0000162
163/**
164 * Pixel formats
165 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800166enum TJPF {
hbono@chromium.org98626972011-08-03 03:13:08 +0000167 /**
168 * RGB pixel format. The red, green, and blue components in the image are
169 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
170 * address within each pixel.
171 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800172 TJPF_RGB = 0,
hbono@chromium.org98626972011-08-03 03:13:08 +0000173 /**
174 * BGR pixel format. The red, green, and blue components in the image are
175 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
176 * address within each pixel.
177 */
178 TJPF_BGR,
179 /**
180 * RGBX pixel format. The red, green, and blue components in the image are
181 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000182 * address within each pixel. The X component is ignored when compressing
183 * and undefined when decompressing.
hbono@chromium.org98626972011-08-03 03:13:08 +0000184 */
185 TJPF_RGBX,
186 /**
187 * BGRX pixel format. The red, green, and blue components in the image are
188 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000189 * address within each pixel. The X component is ignored when compressing
190 * and undefined when decompressing.
hbono@chromium.org98626972011-08-03 03:13:08 +0000191 */
192 TJPF_BGRX,
193 /**
194 * XBGR pixel format. The red, green, and blue components in the image are
195 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000196 * address within each pixel. The X component is ignored when compressing
197 * and undefined when decompressing.
hbono@chromium.org98626972011-08-03 03:13:08 +0000198 */
199 TJPF_XBGR,
200 /**
201 * XRGB pixel format. The red, green, and blue components in the image are
202 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000203 * address within each pixel. The X component is ignored when compressing
204 * and undefined when decompressing.
hbono@chromium.org98626972011-08-03 03:13:08 +0000205 */
206 TJPF_XRGB,
207 /**
208 * Grayscale pixel format. Each 1-byte pixel represents a luminance
209 * (brightness) level from 0 to 255.
210 */
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000211 TJPF_GRAY,
212 /**
213 * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
214 * decompressing, the X component is guaranteed to be 0xFF, which can be
215 * interpreted as an opaque alpha channel.
216 */
217 TJPF_RGBA,
218 /**
219 * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
220 * decompressing, the X component is guaranteed to be 0xFF, which can be
221 * interpreted as an opaque alpha channel.
222 */
223 TJPF_BGRA,
224 /**
225 * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
226 * decompressing, the X component is guaranteed to be 0xFF, which can be
227 * interpreted as an opaque alpha channel.
228 */
229 TJPF_ABGR,
230 /**
231 * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
232 * decompressing, the X component is guaranteed to be 0xFF, which can be
233 * interpreted as an opaque alpha channel.
234 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400235 TJPF_ARGB,
236 /**
237 * CMYK pixel format. Unlike RGB, which is an additive color model used
238 * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
239 * color model used primarily for printing. In the CMYK color model, the
240 * value of each color component typically corresponds to an amount of cyan,
241 * magenta, yellow, or black ink that is applied to a white background. In
242 * order to convert between CMYK and RGB, it is necessary to use a color
243 * management system (CMS.) A CMS will attempt to map colors within the
244 * printer's gamut to perceptually similar colors in the display's gamut and
245 * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
246 * be defined with a simple formula. Thus, such a conversion is out of scope
247 * for a codec library. However, the TurboJPEG API allows for compressing
248 * CMYK pixels into a YCCK JPEG image (see #TJCS_YCCK) and decompressing YCCK
249 * JPEG images into CMYK pixels.
250 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800251 TJPF_CMYK,
252 /**
253 * Unknown pixel format. Currently this is only used by #tjLoadImage().
254 */
255 TJPF_UNKNOWN = -1
hbono@chromium.org98626972011-08-03 03:13:08 +0000256};
257
258/**
259 * Red offset (in bytes) for a given pixel format. This specifies the number
260 * of bytes that the red component is offset from the start of the pixel. For
261 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
Chris Blumecca8c4d2019-03-01 01:09:50 -0800262 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>. This
263 * will be -1 if the pixel format does not have a red component.
hbono@chromium.org98626972011-08-03 03:13:08 +0000264 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800265static const int tjRedOffset[TJ_NUMPF] = {
266 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
267};
hbono@chromium.org98626972011-08-03 03:13:08 +0000268/**
269 * Green offset (in bytes) for a given pixel format. This specifies the number
270 * of bytes that the green component is offset from the start of the pixel.
271 * For instance, if a pixel of format TJ_BGRX is stored in
272 * <tt>char pixel[]</tt>, then the green component will be
Chris Blumecca8c4d2019-03-01 01:09:50 -0800273 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>. This will be -1 if the pixel format
274 * does not have a green component.
hbono@chromium.org98626972011-08-03 03:13:08 +0000275 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800276static const int tjGreenOffset[TJ_NUMPF] = {
277 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
278};
hbono@chromium.org98626972011-08-03 03:13:08 +0000279/**
280 * Blue offset (in bytes) for a given pixel format. This specifies the number
281 * of bytes that the Blue component is offset from the start of the pixel. For
282 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
Chris Blumecca8c4d2019-03-01 01:09:50 -0800283 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>. This
284 * will be -1 if the pixel format does not have a blue component.
hbono@chromium.org98626972011-08-03 03:13:08 +0000285 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800286static const int tjBlueOffset[TJ_NUMPF] = {
287 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
288};
hbono@chromium.org98626972011-08-03 03:13:08 +0000289/**
Chris Blumecca8c4d2019-03-01 01:09:50 -0800290 * Alpha offset (in bytes) for a given pixel format. This specifies the number
291 * of bytes that the Alpha component is offset from the start of the pixel.
292 * For instance, if a pixel of format TJ_BGRA is stored in
293 * <tt>char pixel[]</tt>, then the alpha component will be
294 * <tt>pixel[tjAlphaOffset[TJ_BGRA]]</tt>. This will be -1 if the pixel format
295 * does not have an alpha component.
hbono@chromium.org98626972011-08-03 03:13:08 +0000296 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800297static const int tjAlphaOffset[TJ_NUMPF] = {
298 -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
299};
300/**
301 * Pixel size (in bytes) for a given pixel format
302 */
303static const int tjPixelSize[TJ_NUMPF] = {
304 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
305};
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400306
307
308/**
309 * The number of JPEG colorspaces
310 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800311#define TJ_NUMCS 5
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400312
313/**
314 * JPEG colorspaces
315 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800316enum TJCS {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400317 /**
318 * RGB colorspace. When compressing the JPEG image, the R, G, and B
319 * components in the source image are reordered into image planes, but no
320 * colorspace conversion or subsampling is performed. RGB JPEG images can be
321 * decompressed to any of the extended RGB pixel formats or grayscale, but
322 * they cannot be decompressed to YUV images.
323 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800324 TJCS_RGB = 0,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400325 /**
326 * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
327 * mathematical transformation of RGB designed solely for storage and
328 * transmission. YCbCr images must be converted to RGB before they can
329 * actually be displayed. In the YCbCr colorspace, the Y (luminance)
330 * component represents the black & white portion of the original image, and
331 * the Cb and Cr (chrominance) components represent the color portion of the
332 * original image. Originally, the analog equivalent of this transformation
333 * allowed the same signal to drive both black & white and color televisions,
334 * but JPEG images use YCbCr primarily because it allows the color data to be
335 * optionally subsampled for the purposes of reducing bandwidth or disk
336 * space. YCbCr is the most common JPEG colorspace, and YCbCr JPEG images
337 * can be compressed from and decompressed to any of the extended RGB pixel
338 * formats or grayscale, or they can be decompressed to YUV planar images.
339 */
340 TJCS_YCbCr,
341 /**
342 * Grayscale colorspace. The JPEG image retains only the luminance data (Y
343 * component), and any color data from the source image is discarded.
344 * Grayscale JPEG images can be compressed from and decompressed to any of
345 * the extended RGB pixel formats or grayscale, or they can be decompressed
346 * to YUV planar images.
347 */
348 TJCS_GRAY,
349 /**
350 * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
351 * components in the source image are reordered into image planes, but no
352 * colorspace conversion or subsampling is performed. CMYK JPEG images can
353 * only be decompressed to CMYK pixels.
354 */
355 TJCS_CMYK,
356 /**
357 * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
358 * rather a mathematical transformation of CMYK designed solely for storage
359 * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
360 * reversibly transformed into YCCK, and as with YCbCr, the chrominance
361 * components in the YCCK pixels can be subsampled without incurring major
362 * perceptual loss. YCCK JPEG images can only be compressed from and
363 * decompressed to CMYK pixels.
364 */
365 TJCS_YCCK
366};
hbono@chromium.org98626972011-08-03 03:13:08 +0000367
368
369/**
370 * The uncompressed source/destination image is stored in bottom-up (Windows,
371 * OpenGL) order, not top-down (X11) order.
372 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800373#define TJFLAG_BOTTOMUP 2
hbono@chromium.org98626972011-08-03 03:13:08 +0000374/**
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000375 * When decompressing an image that was compressed using chrominance
376 * subsampling, use the fastest chrominance upsampling algorithm available in
377 * the underlying codec. The default is to use smooth upsampling, which
378 * creates a smooth transition between neighboring chrominance components in
379 * order to reduce upsampling artifacts in the decompressed image.
hbono@chromium.org98626972011-08-03 03:13:08 +0000380 */
381#define TJFLAG_FASTUPSAMPLE 256
382/**
Chris Blumecca8c4d2019-03-01 01:09:50 -0800383 * Disable buffer (re)allocation. If passed to one of the JPEG compression or
384 * transform functions, this flag will cause those functions to generate an
385 * error if the JPEG image buffer is invalid or too small rather than
386 * attempting to allocate or reallocate that buffer. This reproduces the
387 * behavior of earlier versions of TurboJPEG.
hbono@chromium.org98626972011-08-03 03:13:08 +0000388 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800389#define TJFLAG_NOREALLOC 1024
hbono@chromium.org11e6ee92012-07-19 06:04:44 +0000390/**
391 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000392 * default if this flag is not specified is implementation-specific. For
393 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
394 * algorithm by default when compressing, because this has been shown to have
395 * only a very slight effect on accuracy, but it uses the accurate algorithm
396 * when decompressing, because this has been shown to have a larger effect.
hbono@chromium.org11e6ee92012-07-19 06:04:44 +0000397 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800398#define TJFLAG_FASTDCT 2048
hbono@chromium.org11e6ee92012-07-19 06:04:44 +0000399/**
400 * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000401 * The default if this flag is not specified is implementation-specific. For
402 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
403 * algorithm by default when compressing, because this has been shown to have
404 * only a very slight effect on accuracy, but it uses the accurate algorithm
405 * when decompressing, because this has been shown to have a larger effect.
hbono@chromium.org11e6ee92012-07-19 06:04:44 +0000406 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800407#define TJFLAG_ACCURATEDCT 4096
408/**
409 * Immediately discontinue the current compression/decompression/transform
410 * operation if the underlying codec throws a warning (non-fatal error). The
411 * default behavior is to allow the operation to complete unless a fatal error
412 * is encountered.
413 */
414#define TJFLAG_STOPONWARNING 8192
415/**
416 * Use progressive entropy coding in JPEG images generated by the compression
417 * and transform functions. Progressive entropy coding will generally improve
418 * compression relative to baseline entropy coding (the default), but it will
419 * reduce compression and decompression performance considerably.
420 */
421#define TJFLAG_PROGRESSIVE 16384
Jonathan Wright24e31052021-04-26 12:10:48 +0100422/**
423 * Limit the number of progressive JPEG scans that the decompression and
424 * transform functions will process. If a progressive JPEG image contains an
425 * unreasonably large number of scans, then this flag will cause the
426 * decompression and transform functions to return an error. The primary
427 * purpose of this is to allow security-critical applications to guard against
428 * an exploit of the progressive JPEG format described in
429 * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
430 */
431#define TJFLAG_LIMITSCANS 32768
Chris Blumecca8c4d2019-03-01 01:09:50 -0800432
433
434/**
435 * The number of error codes
436 */
437#define TJ_NUMERR 2
438
439/**
440 * Error codes
441 */
442enum TJERR {
443 /**
444 * The error was non-fatal and recoverable, but the image may still be
445 * corrupt.
446 */
447 TJERR_WARNING = 0,
448 /**
449 * The error was fatal and non-recoverable.
450 */
451 TJERR_FATAL
452};
hbono@chromium.org98626972011-08-03 03:13:08 +0000453
454
455/**
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000456 * The number of transform operations
hbono@chromium.org98626972011-08-03 03:13:08 +0000457 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800458#define TJ_NUMXOP 8
hbono@chromium.org98626972011-08-03 03:13:08 +0000459
460/**
461 * Transform operations for #tjTransform()
462 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800463enum TJXOP {
hbono@chromium.org98626972011-08-03 03:13:08 +0000464 /**
465 * Do not transform the position of the image pixels
466 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800467 TJXOP_NONE = 0,
hbono@chromium.org98626972011-08-03 03:13:08 +0000468 /**
469 * Flip (mirror) image horizontally. This transform is imperfect if there
470 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
471 */
472 TJXOP_HFLIP,
473 /**
474 * Flip (mirror) image vertically. This transform is imperfect if there are
475 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
476 */
477 TJXOP_VFLIP,
478 /**
479 * Transpose image (flip/mirror along upper left to lower right axis.) This
480 * transform is always perfect.
481 */
482 TJXOP_TRANSPOSE,
483 /**
484 * Transverse transpose image (flip/mirror along upper right to lower left
485 * axis.) This transform is imperfect if there are any partial MCU blocks in
486 * the image (see #TJXOPT_PERFECT.)
487 */
488 TJXOP_TRANSVERSE,
489 /**
490 * Rotate image clockwise by 90 degrees. This transform is imperfect if
491 * there are any partial MCU blocks on the bottom edge (see
492 * #TJXOPT_PERFECT.)
493 */
494 TJXOP_ROT90,
495 /**
496 * Rotate image 180 degrees. This transform is imperfect if there are any
497 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
498 */
499 TJXOP_ROT180,
500 /**
501 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
502 * if there are any partial MCU blocks on the right edge (see
503 * #TJXOPT_PERFECT.)
504 */
505 TJXOP_ROT270
506};
507
508
509/**
510 * This option will cause #tjTransform() to return an error if the transform is
511 * not perfect. Lossless transforms operate on MCU blocks, whose size depends
512 * on the level of chrominance subsampling used (see #tjMCUWidth
513 * and #tjMCUHeight.) If the image's width or height is not evenly divisible
514 * by the MCU block size, then there will be partial MCU blocks on the right
515 * and/or bottom edges. It is not possible to move these partial MCU blocks to
516 * the top or left of the image, so any transform that would require that is
517 * "imperfect." If this option is not specified, then any partial MCU blocks
518 * that cannot be transformed will be left in place, which will create
519 * odd-looking strips on the right or bottom edge of the image.
520 */
521#define TJXOPT_PERFECT 1
522/**
523 * This option will cause #tjTransform() to discard any partial MCU blocks that
524 * cannot be transformed.
525 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800526#define TJXOPT_TRIM 2
hbono@chromium.org98626972011-08-03 03:13:08 +0000527/**
528 * This option will enable lossless cropping. See #tjTransform() for more
529 * information.
530 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800531#define TJXOPT_CROP 4
hbono@chromium.org98626972011-08-03 03:13:08 +0000532/**
533 * This option will discard the color data in the input image and produce
534 * a grayscale output image.
535 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800536#define TJXOPT_GRAY 8
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000537/**
538 * This option will prevent #tjTransform() from outputting a JPEG image for
539 * this particular transform (this can be used in conjunction with a custom
540 * filter to capture the transformed DCT coefficients without transcoding
541 * them.)
542 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800543#define TJXOPT_NOOUTPUT 16
544/**
545 * This option will enable progressive entropy coding in the output image
546 * generated by this particular transform. Progressive entropy coding will
547 * generally improve compression relative to baseline entropy coding (the
548 * default), but it will reduce compression and decompression performance
549 * considerably.
550 */
551#define TJXOPT_PROGRESSIVE 32
552/**
553 * This option will prevent #tjTransform() from copying any extra markers
554 * (including EXIF and ICC profile data) from the source image to the output
555 * image.
556 */
557#define TJXOPT_COPYNONE 64
hbono@chromium.org98626972011-08-03 03:13:08 +0000558
559
560/**
561 * Scaling factor
562 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800563typedef struct {
hbono@chromium.org98626972011-08-03 03:13:08 +0000564 /**
565 * Numerator
566 */
567 int num;
568 /**
569 * Denominator
570 */
571 int denom;
572} tjscalingfactor;
573
574/**
575 * Cropping region
576 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800577typedef struct {
hbono@chromium.org98626972011-08-03 03:13:08 +0000578 /**
579 * The left boundary of the cropping region. This must be evenly divisible
580 * by the MCU block width (see #tjMCUWidth.)
581 */
582 int x;
583 /**
584 * The upper boundary of the cropping region. This must be evenly divisible
585 * by the MCU block height (see #tjMCUHeight.)
586 */
587 int y;
588 /**
589 * The width of the cropping region. Setting this to 0 is the equivalent of
590 * setting it to the width of the source JPEG image - x.
591 */
592 int w;
593 /**
594 * The height of the cropping region. Setting this to 0 is the equivalent of
595 * setting it to the height of the source JPEG image - y.
596 */
597 int h;
598} tjregion;
599
600/**
601 * Lossless transform
602 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800603typedef struct tjtransform {
hbono@chromium.org98626972011-08-03 03:13:08 +0000604 /**
605 * Cropping region
606 */
607 tjregion r;
608 /**
609 * One of the @ref TJXOP "transform operations"
610 */
611 int op;
612 /**
613 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
614 */
615 int options;
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000616 /**
617 * Arbitrary data that can be accessed within the body of the callback
618 * function
619 */
620 void *data;
621 /**
622 * A callback function that can be used to modify the DCT coefficients
623 * after they are losslessly transformed but before they are transcoded to a
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000624 * new JPEG image. This allows for custom filters or other transformations
625 * to be applied in the frequency domain.
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000626 *
627 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400628 * this pointer is not guaranteed to be valid once the callback returns, so
629 * applications wishing to hand off the DCT coefficients to another function
630 * or library should make a copy of them within the body of the callback.)
631 *
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000632 * @param arrayRegion #tjregion structure containing the width and height of
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400633 * the array pointed to by <tt>coeffs</tt> as well as its offset relative to
634 * the component plane. TurboJPEG implementations may choose to split each
635 * component plane into multiple DCT coefficient arrays and call the callback
636 * function once for each array.
637 *
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000638 * @param planeRegion #tjregion structure containing the width and height of
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400639 * the component plane to which <tt>coeffs</tt> belongs
640 *
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000641 * @param componentID ID number of the component plane to which
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400642 * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of 0, 1,
643 * and 2 in typical JPEG images.)
644 *
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000645 * @param transformID ID number of the transformed image to which
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400646 * <tt>coeffs</tt> belongs. This is the same as the index of the transform
647 * in the <tt>transforms</tt> array that was passed to #tjTransform().
648 *
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000649 * @param transform a pointer to a #tjtransform structure that specifies the
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400650 * parameters and/or cropping region for this transform
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000651 *
652 * @return 0 if the callback was successful, or -1 if an error occurred.
653 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800654 int (*customFilter) (short *coeffs, tjregion arrayRegion,
655 tjregion planeRegion, int componentIndex,
656 int transformIndex, struct tjtransform *transform);
hbono@chromium.org98626972011-08-03 03:13:08 +0000657} tjtransform;
658
659/**
660 * TurboJPEG instance handle
661 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800662typedef void *tjhandle;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000663
hbono@chromium.org98626972011-08-03 03:13:08 +0000664
665/**
666 * Pad the given width to the nearest 32-bit boundary
667 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800668#define TJPAD(width) (((width) + 3) & (~3))
hbono@chromium.org98626972011-08-03 03:13:08 +0000669
670/**
671 * Compute the scaled value of <tt>dimension</tt> using the given scaling
672 * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400673 * scalingFactor)</tt>.
hbono@chromium.org98626972011-08-03 03:13:08 +0000674 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800675#define TJSCALED(dimension, scalingFactor) \
676 ((dimension * scalingFactor.num + scalingFactor.denom - 1) / \
677 scalingFactor.denom)
hbono@chromium.org98626972011-08-03 03:13:08 +0000678
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000679
680#ifdef __cplusplus
681extern "C" {
682#endif
683
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000684
hbono@chromium.org98626972011-08-03 03:13:08 +0000685/**
686 * Create a TurboJPEG compressor instance.
687 *
688 * @return a handle to the newly-created instance, or NULL if an error
Chris Blumecca8c4d2019-03-01 01:09:50 -0800689 * occurred (see #tjGetErrorStr2().)
hbono@chromium.org98626972011-08-03 03:13:08 +0000690 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800691DLLEXPORT tjhandle tjInitCompress(void);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000692
693
hbono@chromium.org98626972011-08-03 03:13:08 +0000694/**
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400695 * Compress an RGB, grayscale, or CMYK image into a JPEG image.
hbono@chromium.org98626972011-08-03 03:13:08 +0000696 *
697 * @param handle a handle to a TurboJPEG compressor or transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400698 *
699 * @param srcBuf pointer to an image buffer containing RGB, grayscale, or
700 * CMYK pixels to be compressed
701 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000702 * @param width width (in pixels) of the source image
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400703 *
704 * @param pitch bytes per line in the source image. Normally, this should be
705 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
706 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
707 * is padded to the nearest 32-bit boundary, as is the case for Windows
708 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
709 * Setting this parameter to 0 is the equivalent of setting it to
710 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
711 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000712 * @param height height (in pixels) of the source image
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400713 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000714 * @param pixelFormat pixel format of the source image (see @ref TJPF
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400715 * "Pixel formats".)
716 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000717 * @param jpegBuf address of a pointer to an image buffer that will receive the
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400718 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
719 * to accommodate the size of the JPEG image. Thus, you can choose to:
720 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
721 * let TurboJPEG grow the buffer as needed,
722 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
723 * for you, or
724 * -# pre-allocate the buffer to a "worst case" size determined by calling
725 * #tjBufSize(). This should ensure that the buffer never has to be
Chris Blumecca8c4d2019-03-01 01:09:50 -0800726 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400727 * .
728 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
729 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
730 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
731 * it may have changed.
732 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000733 * @param jpegSize pointer to an unsigned long variable that holds the size of
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400734 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
735 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
736 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
737 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
738 * reused from a previous call to one of the JPEG compression functions, then
739 * <tt>*jpegSize</tt> is ignored.
740 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000741 * @param jpegSubsamp the level of chrominance subsampling to be used when
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400742 * generating the JPEG image (see @ref TJSAMP
743 * "Chrominance subsampling options".)
744 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000745 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400746 * 100 = best)
747 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800748 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400749 * "flags"
hbono@chromium.org98626972011-08-03 03:13:08 +0000750 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800751 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
752 * and #tjGetErrorCode().)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000753*/
Chris Blumecca8c4d2019-03-01 01:09:50 -0800754DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
755 int width, int pitch, int height, int pixelFormat,
756 unsigned char **jpegBuf, unsigned long *jpegSize,
757 int jpegSubsamp, int jpegQual, int flags);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000758
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000759
hbono@chromium.org98626972011-08-03 03:13:08 +0000760/**
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400761 * Compress a YUV planar image into a JPEG image.
762 *
763 * @param handle a handle to a TurboJPEG compressor or transformer instance
764 *
765 * @param srcBuf pointer to an image buffer containing a YUV planar image to be
766 * compressed. The size of this buffer should match the value returned by
767 * #tjBufSizeYUV2() for the given image width, height, padding, and level of
768 * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
769 * stored sequentially in the source buffer (refer to @ref YUVnotes
770 * "YUV Image Format Notes".)
771 *
772 * @param width width (in pixels) of the source image. If the width is not an
773 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
774 * buffer copy will be performed within TurboJPEG.
775 *
776 * @param pad the line padding used in the source image. For instance, if each
777 * line in each plane of the YUV image is padded to the nearest multiple of 4
778 * bytes, then <tt>pad</tt> should be set to 4.
779 *
780 * @param height height (in pixels) of the source image. If the height is not
781 * an even multiple of the MCU block height (see #tjMCUHeight), then an
782 * intermediate buffer copy will be performed within TurboJPEG.
783 *
784 * @param subsamp the level of chrominance subsampling used in the source
785 * image (see @ref TJSAMP "Chrominance subsampling options".)
786 *
787 * @param jpegBuf address of a pointer to an image buffer that will receive the
788 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
789 * accommodate the size of the JPEG image. Thus, you can choose to:
790 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
791 * let TurboJPEG grow the buffer as needed,
792 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
793 * for you, or
794 * -# pre-allocate the buffer to a "worst case" size determined by calling
795 * #tjBufSize(). This should ensure that the buffer never has to be
Chris Blumecca8c4d2019-03-01 01:09:50 -0800796 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400797 * .
798 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
799 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
800 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
801 * it may have changed.
802 *
803 * @param jpegSize pointer to an unsigned long variable that holds the size of
804 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
805 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
806 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
807 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
808 * reused from a previous call to one of the JPEG compression functions, then
809 * <tt>*jpegSize</tt> is ignored.
810 *
811 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
812 * 100 = best)
813 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800814 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400815 * "flags"
816 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800817 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
818 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400819*/
Chris Blumecca8c4d2019-03-01 01:09:50 -0800820DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
821 int width, int pad, int height, int subsamp,
822 unsigned char **jpegBuf,
823 unsigned long *jpegSize, int jpegQual,
824 int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400825
826
827/**
828 * Compress a set of Y, U (Cb), and V (Cr) image planes into a JPEG image.
829 *
830 * @param handle a handle to a TurboJPEG compressor or transformer instance
831 *
832 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
833 * (or just a Y plane, if compressing a grayscale image) that contain a YUV
834 * image to be compressed. These planes can be contiguous or non-contiguous in
835 * memory. The size of each plane should match the value returned by
836 * #tjPlaneSizeYUV() for the given image width, height, strides, and level of
837 * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
838 * for more details.
839 *
840 * @param width width (in pixels) of the source image. If the width is not an
841 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
842 * buffer copy will be performed within TurboJPEG.
843 *
844 * @param strides an array of integers, each specifying the number of bytes per
845 * line in the corresponding plane of the YUV source image. Setting the stride
846 * for any plane to 0 is the same as setting it to the plane width (see
847 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
848 * the strides for all planes will be set to their respective plane widths.
849 * You can adjust the strides in order to specify an arbitrary amount of line
850 * padding in each plane or to create a JPEG image from a subregion of a larger
851 * YUV planar image.
852 *
853 * @param height height (in pixels) of the source image. If the height is not
854 * an even multiple of the MCU block height (see #tjMCUHeight), then an
855 * intermediate buffer copy will be performed within TurboJPEG.
856 *
857 * @param subsamp the level of chrominance subsampling used in the source
858 * image (see @ref TJSAMP "Chrominance subsampling options".)
859 *
860 * @param jpegBuf address of a pointer to an image buffer that will receive the
861 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
862 * accommodate the size of the JPEG image. Thus, you can choose to:
863 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
864 * let TurboJPEG grow the buffer as needed,
865 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
866 * for you, or
867 * -# pre-allocate the buffer to a "worst case" size determined by calling
868 * #tjBufSize(). This should ensure that the buffer never has to be
Chris Blumecca8c4d2019-03-01 01:09:50 -0800869 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400870 * .
871 * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
872 * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
873 * you should always check <tt>*jpegBuf</tt> upon return from this function, as
874 * it may have changed.
875 *
876 * @param jpegSize pointer to an unsigned long variable that holds the size of
877 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
878 * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
879 * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
880 * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
881 * reused from a previous call to one of the JPEG compression functions, then
882 * <tt>*jpegSize</tt> is ignored.
883 *
884 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
885 * 100 = best)
886 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800887 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400888 * "flags"
889 *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800890 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
891 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400892*/
Chris Blumecca8c4d2019-03-01 01:09:50 -0800893DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
894 const unsigned char **srcPlanes,
895 int width, const int *strides,
896 int height, int subsamp,
897 unsigned char **jpegBuf,
898 unsigned long *jpegSize, int jpegQual,
899 int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400900
901
902/**
hbono@chromium.org98626972011-08-03 03:13:08 +0000903 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
904 * the given parameters. The number of bytes returned by this function is
905 * larger than the size of the uncompressed source image. The reason for this
906 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000907 * for a very high-quality JPEG image with very high-frequency content to
hbono@chromium.org98626972011-08-03 03:13:08 +0000908 * expand rather than compress when converted to the JPEG format. Such images
909 * represent a very rare corner case, but since there is no way to predict the
910 * size of a JPEG image prior to compression, the corner case has to be
911 * handled.
912 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400913 * @param width width (in pixels) of the image
914 *
915 * @param height height (in pixels) of the image
916 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000917 * @param jpegSubsamp the level of chrominance subsampling to be used when
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400918 * generating the JPEG image (see @ref TJSAMP
919 * "Chrominance subsampling options".)
hbono@chromium.org98626972011-08-03 03:13:08 +0000920 *
921 * @return the maximum size of the buffer (in bytes) required to hold the
922 * image, or -1 if the arguments are out of bounds.
923 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800924DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000925
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000926
hbono@chromium.org98626972011-08-03 03:13:08 +0000927/**
928 * The size of the buffer (in bytes) required to hold a YUV planar image with
929 * the given parameters.
930 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400931 * @param width width (in pixels) of the image
932 *
933 * @param pad the width of each line in each plane of the image is padded to
934 * the nearest multiple of this number of bytes (must be a power of 2.)
935 *
936 * @param height height (in pixels) of the image
937 *
hbono@chromium.org98626972011-08-03 03:13:08 +0000938 * @param subsamp level of chrominance subsampling in the image (see
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400939 * @ref TJSAMP "Chrominance subsampling options".)
hbono@chromium.org98626972011-08-03 03:13:08 +0000940 *
941 * @return the size of the buffer (in bytes) required to hold the image, or
942 * -1 if the arguments are out of bounds.
943 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800944DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height,
945 int subsamp);
hbono@chromium.org98626972011-08-03 03:13:08 +0000946
947
948/**
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400949 * The size of the buffer (in bytes) required to hold a YUV image plane with
950 * the given parameters.
951 *
952 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
953 *
954 * @param width width (in pixels) of the YUV image. NOTE: this is the width of
955 * the whole image, not the plane width.
956 *
957 * @param stride bytes per line in the image plane. Setting this to 0 is the
958 * equivalent of setting it to the plane width.
959 *
960 * @param height height (in pixels) of the YUV image. NOTE: this is the height
961 * of the whole image, not the plane height.
962 *
963 * @param subsamp level of chrominance subsampling in the image (see
964 * @ref TJSAMP "Chrominance subsampling options".)
965 *
966 * @return the size of the buffer (in bytes) required to hold the YUV image
967 * plane, or -1 if the arguments are out of bounds.
968 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800969DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
970 int height, int subsamp);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400971
972
973/**
974 * The plane width of a YUV image plane with the given parameters. Refer to
975 * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
976 *
977 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
978 *
979 * @param width width (in pixels) of the YUV image
980 *
981 * @param subsamp level of chrominance subsampling in the image (see
982 * @ref TJSAMP "Chrominance subsampling options".)
983 *
984 * @return the plane width of a YUV image plane with the given parameters, or
985 * -1 if the arguments are out of bounds.
986 */
987DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
988
989
990/**
991 * The plane height of a YUV image plane with the given parameters. Refer to
992 * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
993 *
994 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
995 *
996 * @param height height (in pixels) of the YUV image
997 *
998 * @param subsamp level of chrominance subsampling in the image (see
999 * @ref TJSAMP "Chrominance subsampling options".)
1000 *
1001 * @return the plane height of a YUV image plane with the given parameters, or
1002 * -1 if the arguments are out of bounds.
1003 */
1004DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
1005
1006
1007/**
hbono@chromium.org98626972011-08-03 03:13:08 +00001008 * Encode an RGB or grayscale image into a YUV planar image. This function
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001009 * uses the accelerated color conversion routines in the underlying
1010 * codec but does not execute any of the other steps in the JPEG compression
1011 * process.
hbono@chromium.org98626972011-08-03 03:13:08 +00001012 *
1013 * @param handle a handle to a TurboJPEG compressor or transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001014 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001015 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001016 * to be encoded
1017 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001018 * @param width width (in pixels) of the source image
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001019 *
1020 * @param pitch bytes per line in the source image. Normally, this should be
1021 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
1022 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
1023 * is padded to the nearest 32-bit boundary, as is the case for Windows
1024 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
1025 * Setting this parameter to 0 is the equivalent of setting it to
1026 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1027 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001028 * @param height height (in pixels) of the source image
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001029 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001030 * @param pixelFormat pixel format of the source image (see @ref TJPF
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001031 * "Pixel formats".)
1032 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001033 * @param dstBuf pointer to an image buffer that will receive the YUV image.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001034 * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
1035 * on the image width, height, padding, and level of chrominance subsampling.
1036 * The Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
1037 * buffer (refer to @ref YUVnotes "YUV Image Format Notes".)
1038 *
1039 * @param pad the width of each line in each plane of the YUV image will be
1040 * padded to the nearest multiple of this number of bytes (must be a power of
1041 * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
1042 * 4.
1043 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001044 * @param subsamp the level of chrominance subsampling to be used when
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001045 * generating the YUV image (see @ref TJSAMP
1046 * "Chrominance subsampling options".) To generate images suitable for X
1047 * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
1048 * image compatible with the I420 (AKA "YUV420P") format.
1049 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001050 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001051 * "flags"
hbono@chromium.org98626972011-08-03 03:13:08 +00001052 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001053 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1054 * and #tjGetErrorCode().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001055*/
Chris Blumecca8c4d2019-03-01 01:09:50 -08001056DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
1057 int width, int pitch, int height, int pixelFormat,
1058 unsigned char *dstBuf, int pad, int subsamp,
1059 int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001060
1061
1062/**
1063 * Encode an RGB or grayscale image into separate Y, U (Cb), and V (Cr) image
1064 * planes. This function uses the accelerated color conversion routines in the
1065 * underlying codec but does not execute any of the other steps in the JPEG
1066 * compression process.
1067 *
1068 * @param handle a handle to a TurboJPEG compressor or transformer instance
1069 *
1070 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
1071 * to be encoded
1072 *
1073 * @param width width (in pixels) of the source image
1074 *
1075 * @param pitch bytes per line in the source image. Normally, this should be
1076 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
1077 * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
1078 * is padded to the nearest 32-bit boundary, as is the case for Windows
1079 * bitmaps. You can also be clever and use this parameter to skip lines, etc.
1080 * Setting this parameter to 0 is the equivalent of setting it to
1081 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1082 *
1083 * @param height height (in pixels) of the source image
1084 *
1085 * @param pixelFormat pixel format of the source image (see @ref TJPF
1086 * "Pixel formats".)
1087 *
1088 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1089 * (or just a Y plane, if generating a grayscale image) that will receive the
1090 * encoded image. These planes can be contiguous or non-contiguous in memory.
1091 * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1092 * on the image width, height, strides, and level of chrominance subsampling.
1093 * Refer to @ref YUVnotes "YUV Image Format Notes" for more details.
1094 *
1095 * @param strides an array of integers, each specifying the number of bytes per
1096 * line in the corresponding plane of the output image. Setting the stride for
1097 * any plane to 0 is the same as setting it to the plane width (see
1098 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1099 * the strides for all planes will be set to their respective plane widths.
1100 * You can adjust the strides in order to add an arbitrary amount of line
1101 * padding to each plane or to encode an RGB or grayscale image into a
1102 * subregion of a larger YUV planar image.
1103 *
1104 * @param subsamp the level of chrominance subsampling to be used when
1105 * generating the YUV image (see @ref TJSAMP
1106 * "Chrominance subsampling options".) To generate images suitable for X
1107 * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
1108 * image compatible with the I420 (AKA "YUV420P") format.
1109 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001110 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001111 * "flags"
1112 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001113 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1114 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001115*/
Chris Blumecca8c4d2019-03-01 01:09:50 -08001116DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
1117 int width, int pitch, int height,
1118 int pixelFormat, unsigned char **dstPlanes,
1119 int *strides, int subsamp, int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001120
1121
1122/**
1123 * Create a TurboJPEG decompressor instance.
1124 *
1125 * @return a handle to the newly-created instance, or NULL if an error
Chris Blumecca8c4d2019-03-01 01:09:50 -08001126 * occurred (see #tjGetErrorStr2().)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001127*/
Chris Blumecca8c4d2019-03-01 01:09:50 -08001128DLLEXPORT tjhandle tjInitDecompress(void);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001129
1130
hbono@chromium.org98626972011-08-03 03:13:08 +00001131/**
1132 * Retrieve information about a JPEG image without decompressing it.
1133 *
1134 * @param handle a handle to a TurboJPEG decompressor or transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001135 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001136 * @param jpegBuf pointer to a buffer containing a JPEG image
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001137 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001138 * @param jpegSize size of the JPEG image (in bytes)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001139 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001140 * @param width pointer to an integer variable that will receive the width (in
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001141 * pixels) of the JPEG image
1142 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001143 * @param height pointer to an integer variable that will receive the height
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001144 * (in pixels) of the JPEG image
1145 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001146 * @param jpegSubsamp pointer to an integer variable that will receive the
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001147 * level of chrominance subsampling used when the JPEG image was compressed
1148 * (see @ref TJSAMP "Chrominance subsampling options".)
1149 *
1150 * @param jpegColorspace pointer to an integer variable that will receive one
1151 * of the JPEG colorspace constants, indicating the colorspace of the JPEG
1152 * image (see @ref TJCS "JPEG colorspaces".)
hbono@chromium.org98626972011-08-03 03:13:08 +00001153 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001154 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1155 * and #tjGetErrorCode().)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001156*/
Chris Blumecca8c4d2019-03-01 01:09:50 -08001157DLLEXPORT int tjDecompressHeader3(tjhandle handle,
1158 const unsigned char *jpegBuf,
1159 unsigned long jpegSize, int *width,
1160 int *height, int *jpegSubsamp,
1161 int *jpegColorspace);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001162
1163
hbono@chromium.org98626972011-08-03 03:13:08 +00001164/**
1165 * Returns a list of fractional scaling factors that the JPEG decompressor in
1166 * this implementation of TurboJPEG supports.
1167 *
1168 * @param numscalingfactors pointer to an integer variable that will receive
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001169 * the number of elements in the list
hbono@chromium.org98626972011-08-03 03:13:08 +00001170 *
1171 * @return a pointer to a list of fractional scaling factors, or NULL if an
Chris Blumecca8c4d2019-03-01 01:09:50 -08001172 * error is encountered (see #tjGetErrorStr2().)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001173*/
Chris Blumecca8c4d2019-03-01 01:09:50 -08001174DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001175
1176
hbono@chromium.org98626972011-08-03 03:13:08 +00001177/**
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001178 * Decompress a JPEG image to an RGB, grayscale, or CMYK image.
hbono@chromium.org98626972011-08-03 03:13:08 +00001179 *
1180 * @param handle a handle to a TurboJPEG decompressor or transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001181 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001182 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001183 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001184 * @param jpegSize size of the JPEG image (in bytes)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001185 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001186 * @param dstBuf pointer to an image buffer that will receive the decompressed
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001187 * image. This buffer should normally be <tt>pitch * scaledHeight</tt> bytes
1188 * in size, where <tt>scaledHeight</tt> can be determined by calling
1189 * #TJSCALED() with the JPEG image height and one of the scaling factors
1190 * returned by #tjGetScalingFactors(). The <tt>dstBuf</tt> pointer may also be
1191 * used to decompress into a specific region of a larger buffer.
1192 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001193 * @param width desired width (in pixels) of the destination image. If this is
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001194 * different than the width of the JPEG image being decompressed, then
1195 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1196 * possible image that will fit within the desired width. If <tt>width</tt> is
1197 * set to 0, then only the height will be considered when determining the
1198 * scaled image size.
1199 *
1200 * @param pitch bytes per line in the destination image. Normally, this is
1201 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed image
1202 * is unpadded, else <tt>#TJPAD(scaledWidth * #tjPixelSize[pixelFormat])</tt>
1203 * if each line of the decompressed image is padded to the nearest 32-bit
1204 * boundary, as is the case for Windows bitmaps. (NOTE: <tt>scaledWidth</tt>
1205 * can be determined by calling #TJSCALED() with the JPEG image width and one
1206 * of the scaling factors returned by #tjGetScalingFactors().) You can also be
1207 * clever and use the pitch parameter to skip lines, etc. Setting this
1208 * parameter to 0 is the equivalent of setting it to
1209 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>.
1210 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001211 * @param height desired height (in pixels) of the destination image. If this
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001212 * is different than the height of the JPEG image being decompressed, then
1213 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1214 * possible image that will fit within the desired height. If <tt>height</tt>
1215 * is set to 0, then only the width will be considered when determining the
1216 * scaled image size.
1217 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001218 * @param pixelFormat pixel format of the destination image (see @ref
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001219 * TJPF "Pixel formats".)
1220 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001221 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001222 * "flags"
hbono@chromium.org98626972011-08-03 03:13:08 +00001223 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001224 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1225 * and #tjGetErrorCode().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001226 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001227DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
1228 unsigned long jpegSize, unsigned char *dstBuf,
1229 int width, int pitch, int height, int pixelFormat,
1230 int flags);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001231
1232
hbono@chromium.org98626972011-08-03 03:13:08 +00001233/**
1234 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
1235 * decompression but leaves out the color conversion step, so a planar YUV
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001236 * image is generated instead of an RGB image.
hbono@chromium.org98626972011-08-03 03:13:08 +00001237 *
1238 * @param handle a handle to a TurboJPEG decompressor or transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001239 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001240 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001241 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001242 * @param jpegSize size of the JPEG image (in bytes)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001243 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001244 * @param dstBuf pointer to an image buffer that will receive the YUV image.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001245 * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
1246 * on the image width, height, padding, and level of subsampling. The Y,
1247 * U (Cb), and V (Cr) image planes will be stored sequentially in the buffer
1248 * (refer to @ref YUVnotes "YUV Image Format Notes".)
1249 *
1250 * @param width desired width (in pixels) of the YUV image. If this is
1251 * different than the width of the JPEG image being decompressed, then
1252 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1253 * possible image that will fit within the desired width. If <tt>width</tt> is
1254 * set to 0, then only the height will be considered when determining the
1255 * scaled image size. If the scaled width is not an even multiple of the MCU
1256 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1257 * performed within TurboJPEG.
1258 *
1259 * @param pad the width of each line in each plane of the YUV image will be
1260 * padded to the nearest multiple of this number of bytes (must be a power of
1261 * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
1262 * 4.
1263 *
1264 * @param height desired height (in pixels) of the YUV image. If this is
1265 * different than the height of the JPEG image being decompressed, then
1266 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1267 * possible image that will fit within the desired height. If <tt>height</tt>
1268 * is set to 0, then only the width will be considered when determining the
1269 * scaled image size. If the scaled height is not an even multiple of the MCU
1270 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1271 * performed within TurboJPEG.
1272 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001273 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001274 * "flags"
hbono@chromium.org98626972011-08-03 03:13:08 +00001275 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001276 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1277 * and #tjGetErrorCode().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001278 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001279DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
1280 unsigned long jpegSize, unsigned char *dstBuf,
1281 int width, int pad, int height, int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001282
1283
1284/**
1285 * Decompress a JPEG image into separate Y, U (Cb), and V (Cr) image
1286 * planes. This function performs JPEG decompression but leaves out the color
1287 * conversion step, so a planar YUV image is generated instead of an RGB image.
1288 *
1289 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1290 *
1291 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
1292 *
1293 * @param jpegSize size of the JPEG image (in bytes)
1294 *
1295 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1296 * (or just a Y plane, if decompressing a grayscale image) that will receive
1297 * the YUV image. These planes can be contiguous or non-contiguous in memory.
1298 * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1299 * on the scaled image width, scaled image height, strides, and level of
1300 * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
1301 * for more details.
1302 *
1303 * @param width desired width (in pixels) of the YUV image. If this is
1304 * different than the width of the JPEG image being decompressed, then
1305 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1306 * possible image that will fit within the desired width. If <tt>width</tt> is
1307 * set to 0, then only the height will be considered when determining the
1308 * scaled image size. If the scaled width is not an even multiple of the MCU
1309 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1310 * performed within TurboJPEG.
1311 *
1312 * @param strides an array of integers, each specifying the number of bytes per
1313 * line in the corresponding plane of the output image. Setting the stride for
1314 * any plane to 0 is the same as setting it to the scaled plane width (see
1315 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1316 * the strides for all planes will be set to their respective scaled plane
1317 * widths. You can adjust the strides in order to add an arbitrary amount of
1318 * line padding to each plane or to decompress the JPEG image into a subregion
1319 * of a larger YUV planar image.
1320 *
1321 * @param height desired height (in pixels) of the YUV image. If this is
1322 * different than the height of the JPEG image being decompressed, then
1323 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1324 * possible image that will fit within the desired height. If <tt>height</tt>
1325 * is set to 0, then only the width will be considered when determining the
1326 * scaled image size. If the scaled height is not an even multiple of the MCU
1327 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1328 * performed within TurboJPEG.
1329 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001330 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001331 * "flags"
1332 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001333 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1334 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001335 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001336DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
1337 const unsigned char *jpegBuf,
1338 unsigned long jpegSize,
1339 unsigned char **dstPlanes, int width,
1340 int *strides, int height, int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001341
1342
1343/**
1344 * Decode a YUV planar image into an RGB or grayscale image. This function
1345 * uses the accelerated color conversion routines in the underlying
1346 * codec but does not execute any of the other steps in the JPEG decompression
1347 * process.
1348 *
1349 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1350 *
1351 * @param srcBuf pointer to an image buffer containing a YUV planar image to be
1352 * decoded. The size of this buffer should match the value returned by
1353 * #tjBufSizeYUV2() for the given image width, height, padding, and level of
1354 * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
1355 * stored sequentially in the source buffer (refer to @ref YUVnotes
1356 * "YUV Image Format Notes".)
1357 *
1358 * @param pad Use this parameter to specify that the width of each line in each
1359 * plane of the YUV source image is padded to the nearest multiple of this
1360 * number of bytes (must be a power of 2.)
1361 *
1362 * @param subsamp the level of chrominance subsampling used in the YUV source
1363 * image (see @ref TJSAMP "Chrominance subsampling options".)
1364 *
1365 * @param dstBuf pointer to an image buffer that will receive the decoded
1366 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1367 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1368 * specific region of a larger buffer.
1369 *
1370 * @param width width (in pixels) of the source and destination images
1371 *
1372 * @param pitch bytes per line in the destination image. Normally, this should
1373 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1374 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1375 * of the destination image should be padded to the nearest 32-bit boundary, as
1376 * is the case for Windows bitmaps. You can also be clever and use the pitch
1377 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1378 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1379 *
1380 * @param height height (in pixels) of the source and destination images
1381 *
1382 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1383 * "Pixel formats".)
1384 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001385 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001386 * "flags"
1387 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001388 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1389 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001390 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001391DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
1392 int pad, int subsamp, unsigned char *dstBuf,
1393 int width, int pitch, int height, int pixelFormat,
1394 int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001395
1396
1397/**
1398 * Decode a set of Y, U (Cb), and V (Cr) image planes into an RGB or grayscale
1399 * image. This function uses the accelerated color conversion routines in the
1400 * underlying codec but does not execute any of the other steps in the JPEG
1401 * decompression process.
1402 *
1403 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1404 *
1405 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1406 * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1407 * to be decoded. These planes can be contiguous or non-contiguous in memory.
1408 * The size of each plane should match the value returned by #tjPlaneSizeYUV()
1409 * for the given image width, height, strides, and level of chrominance
1410 * subsampling. Refer to @ref YUVnotes "YUV Image Format Notes" for more
1411 * details.
1412 *
1413 * @param strides an array of integers, each specifying the number of bytes per
1414 * line in the corresponding plane of the YUV source image. Setting the stride
1415 * for any plane to 0 is the same as setting it to the plane width (see
1416 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1417 * the strides for all planes will be set to their respective plane widths.
1418 * You can adjust the strides in order to specify an arbitrary amount of line
1419 * padding in each plane or to decode a subregion of a larger YUV planar image.
1420 *
1421 * @param subsamp the level of chrominance subsampling used in the YUV source
1422 * image (see @ref TJSAMP "Chrominance subsampling options".)
1423 *
1424 * @param dstBuf pointer to an image buffer that will receive the decoded
1425 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1426 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1427 * specific region of a larger buffer.
1428 *
1429 * @param width width (in pixels) of the source and destination images
1430 *
1431 * @param pitch bytes per line in the destination image. Normally, this should
1432 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1433 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1434 * of the destination image should be padded to the nearest 32-bit boundary, as
1435 * is the case for Windows bitmaps. You can also be clever and use the pitch
1436 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1437 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1438 *
1439 * @param height height (in pixels) of the source and destination images
1440 *
1441 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1442 * "Pixel formats".)
1443 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001444 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001445 * "flags"
1446 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001447 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1448 * and #tjGetErrorCode().)
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001449 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001450DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
1451 const unsigned char **srcPlanes,
1452 const int *strides, int subsamp,
1453 unsigned char *dstBuf, int width, int pitch,
1454 int height, int pixelFormat, int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001455
1456
1457/**
1458 * Create a new TurboJPEG transformer instance.
1459 *
1460 * @return a handle to the newly-created instance, or NULL if an error
Chris Blumecca8c4d2019-03-01 01:09:50 -08001461 * occurred (see #tjGetErrorStr2().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001462 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001463DLLEXPORT tjhandle tjInitTransform(void);
hbono@chromium.org98626972011-08-03 03:13:08 +00001464
1465
1466/**
1467 * Losslessly transform a JPEG image into another JPEG image. Lossless
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001468 * transforms work by moving the raw DCT coefficients from one JPEG image
1469 * structure to another without altering the values of the coefficients. While
1470 * this is typically faster than decompressing the image, transforming it, and
hbono@chromium.org98626972011-08-03 03:13:08 +00001471 * re-compressing it, lossless transforms are not free. Each lossless
noel@chromium.org3395bcc2014-04-14 06:56:00 +00001472 * transform requires reading and performing Huffman decoding on all of the
1473 * coefficients in the source image, regardless of the size of the destination
1474 * image. Thus, this function provides a means of generating multiple
1475 * transformed images from the same source or applying multiple
1476 * transformations simultaneously, in order to eliminate the need to read the
1477 * source coefficients multiple times.
hbono@chromium.org98626972011-08-03 03:13:08 +00001478 *
1479 * @param handle a handle to a TurboJPEG transformer instance
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001480 *
1481 * @param jpegBuf pointer to a buffer containing the JPEG source image to
1482 * transform
1483 *
1484 * @param jpegSize size of the JPEG source image (in bytes)
1485 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001486 * @param n the number of transformed JPEG images to generate
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001487 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001488 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001489 * will receive a JPEG image that has been transformed using the parameters in
1490 * <tt>transforms[i]</tt>. TurboJPEG has the ability to reallocate the JPEG
1491 * buffer to accommodate the size of the JPEG image. Thus, you can choose to:
1492 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
1493 * let TurboJPEG grow the buffer as needed,
1494 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the buffer
1495 * for you, or
1496 * -# pre-allocate the buffer to a "worst case" size determined by calling
Chris Blumecca8c4d2019-03-01 01:09:50 -08001497 * #tjBufSize() with the transformed or cropped width and height. Under normal
1498 * circumstances, this should ensure that the buffer never has to be
1499 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.) Note,
1500 * however, that there are some rare cases (such as transforming images with a
1501 * large amount of embedded EXIF or ICC profile data) in which the output image
1502 * will be larger than the worst-case size, and #TJFLAG_NOREALLOC cannot be
1503 * used in those cases.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001504 * .
1505 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to the size of
1506 * your pre-allocated buffer. In any case, unless you have set
1507 * #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> upon return
1508 * from this function, as it may have changed.
1509 *
hbono@chromium.org0ec930e2012-01-18 07:01:04 +00001510 * @param dstSizes pointer to an array of n unsigned long variables that will
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001511 * receive the actual sizes (in bytes) of each transformed JPEG image. If
1512 * <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
1513 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon return,
1514 * <tt>dstSizes[i]</tt> will contain the size of the JPEG image (in bytes.)
1515 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00001516 * @param transforms pointer to an array of n #tjtransform structures, each of
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001517 * which specifies the transform parameters and/or cropping region for the
1518 * corresponding transformed output image.
1519 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001520 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001521 * "flags"
hbono@chromium.org98626972011-08-03 03:13:08 +00001522 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001523 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1524 * and #tjGetErrorCode().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001525 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001526DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
1527 unsigned long jpegSize, int n,
1528 unsigned char **dstBufs, unsigned long *dstSizes,
1529 tjtransform *transforms, int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001530
1531
1532/**
1533 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
1534 *
1535 * @param handle a handle to a TurboJPEG compressor, decompressor or
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001536 * transformer instance
hbono@chromium.org98626972011-08-03 03:13:08 +00001537 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001538 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
hbono@chromium.org98626972011-08-03 03:13:08 +00001539 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001540DLLEXPORT int tjDestroy(tjhandle handle);
hbono@chromium.org98626972011-08-03 03:13:08 +00001541
1542
1543/**
1544 * Allocate an image buffer for use with TurboJPEG. You should always use
Chris Blumecca8c4d2019-03-01 01:09:50 -08001545 * this function to allocate the JPEG destination buffer(s) for the compression
1546 * and transform functions unless you are disabling automatic buffer
hbono@chromium.org98626972011-08-03 03:13:08 +00001547 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
1548 *
1549 * @param bytes the number of bytes to allocate
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001550 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001551 * @return a pointer to a newly-allocated buffer with the specified number of
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001552 * bytes.
hbono@chromium.org98626972011-08-03 03:13:08 +00001553 *
1554 * @sa tjFree()
1555 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001556DLLEXPORT unsigned char *tjAlloc(int bytes);
1557
1558
1559/**
1560 * Load an uncompressed image from disk into memory.
1561 *
1562 * @param filename name of a file containing an uncompressed image in Windows
1563 * BMP or PBMPLUS (PPM/PGM) format
1564 *
1565 * @param width pointer to an integer variable that will receive the width (in
1566 * pixels) of the uncompressed image
1567 *
1568 * @param align row alignment of the image buffer to be returned (must be a
1569 * power of 2.) For instance, setting this parameter to 4 will cause all rows
1570 * in the image buffer to be padded to the nearest 32-bit boundary, and setting
1571 * this parameter to 1 will cause all rows in the image buffer to be unpadded.
1572 *
1573 * @param height pointer to an integer variable that will receive the height
1574 * (in pixels) of the uncompressed image
1575 *
1576 * @param pixelFormat pointer to an integer variable that specifies or will
1577 * receive the pixel format of the uncompressed image buffer. The behavior of
1578 * #tjLoadImage() will vary depending on the value of <tt>*pixelFormat</tt>
1579 * passed to the function:
1580 * - @ref TJPF_UNKNOWN : The uncompressed image buffer returned by the function
1581 * will use the most optimal pixel format for the file type, and
1582 * <tt>*pixelFormat</tt> will contain the ID of this pixel format upon
1583 * successful return from the function.
1584 * - @ref TJPF_GRAY : Only PGM files and 8-bit BMP files with a grayscale
1585 * colormap can be loaded.
1586 * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1587 * converted using a quick & dirty algorithm that is suitable only for testing
1588 * purposes (proper conversion between CMYK and other formats requires a color
1589 * management system.)
1590 * - Other @ref TJPF "pixel formats" : The uncompressed image buffer will use
1591 * the specified pixel format, and pixel format conversion will be performed if
1592 * necessary.
1593 *
1594 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1595 * "flags".
1596 *
1597 * @return a pointer to a newly-allocated buffer containing the uncompressed
1598 * image, converted to the chosen pixel format and with the chosen row
1599 * alignment, or NULL if an error occurred (see #tjGetErrorStr2().) This
1600 * buffer should be freed using #tjFree().
1601 */
1602DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
1603 int align, int *height, int *pixelFormat,
1604 int flags);
1605
1606
1607/**
1608 * Save an uncompressed image from memory to disk.
1609 *
1610 * @param filename name of a file to which to save the uncompressed image.
1611 * The image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format,
1612 * depending on the file extension.
1613 *
1614 * @param buffer pointer to an image buffer containing RGB, grayscale, or
1615 * CMYK pixels to be saved
1616 *
1617 * @param width width (in pixels) of the uncompressed image
1618 *
1619 * @param pitch bytes per line in the image buffer. Setting this parameter to
1620 * 0 is the equivalent of setting it to
1621 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1622 *
1623 * @param height height (in pixels) of the uncompressed image
1624 *
1625 * @param pixelFormat pixel format of the image buffer (see @ref TJPF
1626 * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
1627 * image will be stored in PGM or 8-bit (indexed color) BMP format. Otherwise,
1628 * the image will be stored in PPM or 24-bit BMP format. If this parameter
1629 * is set to @ref TJPF_CMYK, then the CMYK pixels will be converted to RGB
1630 * using a quick & dirty algorithm that is suitable only for testing (proper
1631 * conversion between CMYK and other formats requires a color management
1632 * system.)
1633 *
1634 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1635 * "flags".
1636 *
1637 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1638 */
1639DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
1640 int width, int pitch, int height, int pixelFormat,
1641 int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001642
1643
1644/**
1645 * Free an image buffer previously allocated by TurboJPEG. You should always
1646 * use this function to free JPEG destination buffer(s) that were automatically
Chris Blumecca8c4d2019-03-01 01:09:50 -08001647 * (re)allocated by the compression and transform functions or that were
1648 * manually allocated using #tjAlloc().
hbono@chromium.org98626972011-08-03 03:13:08 +00001649 *
Jonathan Wrightdb870df2020-08-05 11:42:22 +01001650 * @param buffer address of the buffer to free. If the address is NULL, then
1651 * this function has no effect.
hbono@chromium.org98626972011-08-03 03:13:08 +00001652 *
1653 * @sa tjAlloc()
1654 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001655DLLEXPORT void tjFree(unsigned char *buffer);
hbono@chromium.org98626972011-08-03 03:13:08 +00001656
1657
1658/**
1659 * Returns a descriptive error message explaining why the last command failed.
1660 *
Chris Blumecca8c4d2019-03-01 01:09:50 -08001661 * @param handle a handle to a TurboJPEG compressor, decompressor, or
1662 * transformer instance, or NULL if the error was generated by a global
1663 * function (but note that retrieving the error message for a global function
Jonathan Wrightdb870df2020-08-05 11:42:22 +01001664 * is thread-safe only on platforms that support thread-local storage.)
Chris Blumecca8c4d2019-03-01 01:09:50 -08001665 *
hbono@chromium.org98626972011-08-03 03:13:08 +00001666 * @return a descriptive error message explaining why the last command failed.
1667 */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001668DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
1669
1670
1671/**
1672 * Returns a code indicating the severity of the last error. See
1673 * @ref TJERR "Error codes".
1674 *
1675 * @param handle a handle to a TurboJPEG compressor, decompressor or
1676 * transformer instance
1677 *
1678 * @return a code indicating the severity of the last error. See
1679 * @ref TJERR "Error codes".
1680 */
1681DLLEXPORT int tjGetErrorCode(tjhandle handle);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001682
hbono@chromium.org98626972011-08-03 03:13:08 +00001683
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001684/* Deprecated functions and macros */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001685#define TJFLAG_FORCEMMX 8
1686#define TJFLAG_FORCESSE 16
1687#define TJFLAG_FORCESSE2 32
1688#define TJFLAG_FORCESSE3 128
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001689
1690
hbono@chromium.org98626972011-08-03 03:13:08 +00001691/* Backward compatibility functions and macros (nothing to see here) */
Chris Blumecca8c4d2019-03-01 01:09:50 -08001692#define NUMSUBOPT TJ_NUMSAMP
1693#define TJ_444 TJSAMP_444
1694#define TJ_422 TJSAMP_422
1695#define TJ_420 TJSAMP_420
1696#define TJ_411 TJSAMP_420
1697#define TJ_GRAYSCALE TJSAMP_GRAY
hbono@chromium.org98626972011-08-03 03:13:08 +00001698
Chris Blumecca8c4d2019-03-01 01:09:50 -08001699#define TJ_BGR 1
1700#define TJ_BOTTOMUP TJFLAG_BOTTOMUP
1701#define TJ_FORCEMMX TJFLAG_FORCEMMX
1702#define TJ_FORCESSE TJFLAG_FORCESSE
1703#define TJ_FORCESSE2 TJFLAG_FORCESSE2
1704#define TJ_ALPHAFIRST 64
1705#define TJ_FORCESSE3 TJFLAG_FORCESSE3
1706#define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
1707#define TJ_YUV 512
hbono@chromium.org98626972011-08-03 03:13:08 +00001708
Chris Blumecca8c4d2019-03-01 01:09:50 -08001709DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
hbono@chromium.org98626972011-08-03 03:13:08 +00001710
Chris Blumecca8c4d2019-03-01 01:09:50 -08001711DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
hbono@chromium.org98626972011-08-03 03:13:08 +00001712
Chris Blumecca8c4d2019-03-01 01:09:50 -08001713DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001714
Chris Blumecca8c4d2019-03-01 01:09:50 -08001715DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
1716 int pitch, int height, int pixelSize,
1717 unsigned char *dstBuf, unsigned long *compressedSize,
1718 int jpegSubsamp, int jpegQual, int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001719
Chris Blumecca8c4d2019-03-01 01:09:50 -08001720DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
1721 int pitch, int height, int pixelSize,
1722 unsigned char *dstBuf, int subsamp, int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001723
Chris Blumecca8c4d2019-03-01 01:09:50 -08001724DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
1725 int pitch, int height, int pixelFormat,
1726 unsigned char *dstBuf, int subsamp, int flags);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001727
Chris Blumecca8c4d2019-03-01 01:09:50 -08001728DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
1729 unsigned long jpegSize, int *width,
1730 int *height);
hbono@chromium.org98626972011-08-03 03:13:08 +00001731
Chris Blumecca8c4d2019-03-01 01:09:50 -08001732DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
1733 unsigned long jpegSize, int *width,
1734 int *height, int *jpegSubsamp);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001735
Chris Blumecca8c4d2019-03-01 01:09:50 -08001736DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
1737 unsigned long jpegSize, unsigned char *dstBuf,
1738 int width, int pitch, int height, int pixelSize,
1739 int flags);
hbono@chromium.org98626972011-08-03 03:13:08 +00001740
Chris Blumecca8c4d2019-03-01 01:09:50 -08001741DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
1742 unsigned long jpegSize, unsigned char *dstBuf,
1743 int flags);
1744
1745DLLEXPORT char *tjGetErrorStr(void);
Tom Hudson0d47d2d2016-05-04 13:22:56 -04001746
hbono@chromium.org98626972011-08-03 03:13:08 +00001747
1748/**
1749 * @}
1750 */
1751
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001752#ifdef __cplusplus
1753}
1754#endif
hbono@chromium.org98626972011-08-03 03:13:08 +00001755
1756#endif