blob: a563c8188125f681edf8353a64079803a4521f14 [file] [log] [blame]
DRC9b28def2011-05-21 14:37:15 +00001/*
DRC9ce1a212013-04-26 05:32:32 +00002 * Copyright (C)2009-2013 D. R. Commander. All Rights Reserved.
DRC2e7b76b2009-04-03 12:04:24 +00003 *
DRC9b28def2011-05-21 14:37:15 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
DRC2e7b76b2009-04-03 12:04:24 +00006 *
DRC9b28def2011-05-21 14:37:15 +00007 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
DRC2e7b76b2009-04-03 12:04:24 +000027 */
28
DRC3a1bb352011-05-24 09:15:44 +000029#ifndef __TURBOJPEG_H__
30#define __TURBOJPEG_H__
31
DRC9b28def2011-05-21 14:37:15 +000032#if defined(_WIN32) && defined(DLLDEFINE)
DRC2e7b76b2009-04-03 12:04:24 +000033#define DLLEXPORT __declspec(dllexport)
34#else
35#define DLLEXPORT
36#endif
DRC2e7b76b2009-04-03 12:04:24 +000037#define DLLCALL
38
DRC2e7b76b2009-04-03 12:04:24 +000039
DRC9b28def2011-05-21 14:37:15 +000040/**
41 * @addtogroup TurboJPEG
42 * TurboJPEG API. This API provides an interface for generating, decoding, and
43 * transforming planar YUV and JPEG images in memory.
44 *
45 * @{
46 */
DRC2e7b76b2009-04-03 12:04:24 +000047
DRC0a325192011-03-02 09:22:41 +000048
DRC9b28def2011-05-21 14:37:15 +000049/**
50 * The number of chrominance subsampling options
51 */
52#define TJ_NUMSAMP 5
DRCfbb67472010-11-24 04:02:37 +000053
DRC9b28def2011-05-21 14:37:15 +000054/**
55 * Chrominance subsampling options.
DRC5a7e9e52013-11-25 20:30:12 +000056 * When an image is converted from the RGB to the YCbCr colorspace as part of
57 * the JPEG compression process, some of the Cb and Cr (chrominance) components
DRC9b28def2011-05-21 14:37:15 +000058 * can be discarded or averaged together to produce a smaller image with little
59 * perceptible loss of image clarity (the human eye is more sensitive to small
60 * changes in brightness than small changes in color.) This is called
61 * "chrominance subsampling".
DRC5a7e9e52013-11-25 20:30:12 +000062 * <p>
63 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
64 * convention of the digital video community, the TurboJPEG API uses "YUV" to
65 * refer to an image format consisting of Y, Cb, and Cr image planes.
DRC9b28def2011-05-21 14:37:15 +000066 */
DRC25b995a2011-05-21 15:34:54 +000067enum TJSAMP
DRC109a5782011-03-01 09:53:07 +000068{
DRC9b28def2011-05-21 14:37:15 +000069 /**
70 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
71 * YUV image will contain one chrominance component for every pixel in the
72 * source image.
73 */
DRC25b995a2011-05-21 15:34:54 +000074 TJSAMP_444=0,
DRC9b28def2011-05-21 14:37:15 +000075 /**
76 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
77 * chrominance component for every 2x1 block of pixels in the source image.
78 */
DRC25b995a2011-05-21 15:34:54 +000079 TJSAMP_422,
DRC9b28def2011-05-21 14:37:15 +000080 /**
81 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
82 * chrominance component for every 2x2 block of pixels in the source image.
83 */
DRC25b995a2011-05-21 15:34:54 +000084 TJSAMP_420,
DRC9b28def2011-05-21 14:37:15 +000085 /**
86 * Grayscale. The JPEG or YUV image will contain no chrominance components.
87 */
DRC25b995a2011-05-21 15:34:54 +000088 TJSAMP_GRAY,
DRC9b28def2011-05-21 14:37:15 +000089 /**
90 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
91 * chrominance component for every 1x2 block of pixels in the source image.
DRC76577262013-08-18 09:28:09 +000092 * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
DRC9b28def2011-05-21 14:37:15 +000093 */
DRC25b995a2011-05-21 15:34:54 +000094 TJSAMP_440
DRC890f1e02011-02-26 22:02:37 +000095};
96
DRC9b28def2011-05-21 14:37:15 +000097/**
98 * MCU block width (in pixels) for a given level of chrominance subsampling.
99 * MCU block sizes:
100 * - 8x8 for no subsampling or grayscale
101 * - 16x8 for 4:2:2
102 * - 8x16 for 4:4:0
103 * - 16x16 for 4:2:0
104 */
105static const int tjMCUWidth[TJ_NUMSAMP] = {8, 16, 16, 8, 8};
DRC890f1e02011-02-26 22:02:37 +0000106
DRC9b28def2011-05-21 14:37:15 +0000107/**
108 * MCU block height (in pixels) for a given level of chrominance subsampling.
109 * MCU block sizes:
110 * - 8x8 for no subsampling or grayscale
111 * - 16x8 for 4:2:2
112 * - 8x16 for 4:4:0
113 * - 16x16 for 4:2:0
114 */
115static const int tjMCUHeight[TJ_NUMSAMP] = {8, 8, 16, 8, 16};
116
117
118/**
119 * The number of pixel formats
120 */
DRC67ce3b22011-12-19 02:21:03 +0000121#define TJ_NUMPF 11
DRC9b28def2011-05-21 14:37:15 +0000122
123/**
124 * Pixel formats
DRC9b28def2011-05-21 14:37:15 +0000125 */
DRC25b995a2011-05-21 15:34:54 +0000126enum TJPF
DRC9b28def2011-05-21 14:37:15 +0000127{
128 /**
129 * RGB pixel format. The red, green, and blue components in the image are
130 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
131 * address within each pixel.
132 */
DRC25b995a2011-05-21 15:34:54 +0000133 TJPF_RGB=0,
DRC9b28def2011-05-21 14:37:15 +0000134 /**
135 * BGR pixel format. The red, green, and blue components in the image are
136 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
137 * address within each pixel.
138 */
DRC25b995a2011-05-21 15:34:54 +0000139 TJPF_BGR,
DRC9b28def2011-05-21 14:37:15 +0000140 /**
141 * RGBX pixel format. The red, green, and blue components in the image are
142 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
DRC67ce3b22011-12-19 02:21:03 +0000143 * address within each pixel. The X component is ignored when compressing
144 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000145 */
DRC25b995a2011-05-21 15:34:54 +0000146 TJPF_RGBX,
DRC9b28def2011-05-21 14:37:15 +0000147 /**
148 * BGRX pixel format. The red, green, and blue components in the image are
149 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
DRC67ce3b22011-12-19 02:21:03 +0000150 * address within each pixel. The X component is ignored when compressing
151 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000152 */
DRC25b995a2011-05-21 15:34:54 +0000153 TJPF_BGRX,
DRC9b28def2011-05-21 14:37:15 +0000154 /**
155 * XBGR pixel format. The red, green, and blue components in the image are
156 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
DRC67ce3b22011-12-19 02:21:03 +0000157 * address within each pixel. The X component is ignored when compressing
158 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000159 */
DRC25b995a2011-05-21 15:34:54 +0000160 TJPF_XBGR,
DRC9b28def2011-05-21 14:37:15 +0000161 /**
162 * XRGB pixel format. The red, green, and blue components in the image are
163 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
DRC67ce3b22011-12-19 02:21:03 +0000164 * address within each pixel. The X component is ignored when compressing
165 * and undefined when decompressing.
DRC9b28def2011-05-21 14:37:15 +0000166 */
DRC25b995a2011-05-21 15:34:54 +0000167 TJPF_XRGB,
DRC9b28def2011-05-21 14:37:15 +0000168 /**
169 * Grayscale pixel format. Each 1-byte pixel represents a luminance
170 * (brightness) level from 0 to 255.
171 */
DRC67ce3b22011-12-19 02:21:03 +0000172 TJPF_GRAY,
173 /**
174 * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
175 * decompressing, the X component is guaranteed to be 0xFF, which can be
176 * interpreted as an opaque alpha channel.
177 */
178 TJPF_RGBA,
179 /**
180 * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
181 * decompressing, the X component is guaranteed to be 0xFF, which can be
182 * interpreted as an opaque alpha channel.
183 */
184 TJPF_BGRA,
185 /**
186 * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
187 * decompressing, the X component is guaranteed to be 0xFF, which can be
188 * interpreted as an opaque alpha channel.
189 */
190 TJPF_ABGR,
191 /**
192 * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
193 * decompressing, the X component is guaranteed to be 0xFF, which can be
194 * interpreted as an opaque alpha channel.
195 */
196 TJPF_ARGB
DRC9b28def2011-05-21 14:37:15 +0000197};
198
199/**
200 * Red offset (in bytes) for a given pixel format. This specifies the number
201 * of bytes that the red component is offset from the start of the pixel. For
202 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
203 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>.
204 */
DRC67ce3b22011-12-19 02:21:03 +0000205static const int tjRedOffset[TJ_NUMPF] = {0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1};
DRC9b28def2011-05-21 14:37:15 +0000206/**
207 * Green offset (in bytes) for a given pixel format. This specifies the number
208 * of bytes that the green component is offset from the start of the pixel.
209 * For instance, if a pixel of format TJ_BGRX is stored in
210 * <tt>char pixel[]</tt>, then the green component will be
211 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>.
212 */
DRC67ce3b22011-12-19 02:21:03 +0000213static const int tjGreenOffset[TJ_NUMPF] = {1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2};
DRC9b28def2011-05-21 14:37:15 +0000214/**
215 * Blue offset (in bytes) for a given pixel format. This specifies the number
216 * of bytes that the Blue component is offset from the start of the pixel. For
217 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
218 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>.
219 */
DRC67ce3b22011-12-19 02:21:03 +0000220static const int tjBlueOffset[TJ_NUMPF] = {2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3};
DRC9b28def2011-05-21 14:37:15 +0000221
222/**
223 * Pixel size (in bytes) for a given pixel format.
224 */
DRC67ce3b22011-12-19 02:21:03 +0000225static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4};
DRC9b28def2011-05-21 14:37:15 +0000226
227
228/**
DRC9b28def2011-05-21 14:37:15 +0000229 * The uncompressed source/destination image is stored in bottom-up (Windows,
230 * OpenGL) order, not top-down (X11) order.
231 */
DRC25b995a2011-05-21 15:34:54 +0000232#define TJFLAG_BOTTOMUP 2
DRC9b28def2011-05-21 14:37:15 +0000233/**
DRC73d74c12012-06-29 23:46:38 +0000234 * Turn off CPU auto-detection and force TurboJPEG to use MMX code (if the
235 * underlying codec supports it.)
DRC9b28def2011-05-21 14:37:15 +0000236 */
DRC25b995a2011-05-21 15:34:54 +0000237#define TJFLAG_FORCEMMX 8
DRC9b28def2011-05-21 14:37:15 +0000238/**
DRC73d74c12012-06-29 23:46:38 +0000239 * Turn off CPU auto-detection and force TurboJPEG to use SSE code (if the
240 * underlying codec supports it.)
DRC9b28def2011-05-21 14:37:15 +0000241 */
DRC25b995a2011-05-21 15:34:54 +0000242#define TJFLAG_FORCESSE 16
DRC9b28def2011-05-21 14:37:15 +0000243/**
DRC73d74c12012-06-29 23:46:38 +0000244 * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (if the
245 * underlying codec supports it.)
DRC9b28def2011-05-21 14:37:15 +0000246 */
DRC25b995a2011-05-21 15:34:54 +0000247#define TJFLAG_FORCESSE2 32
DRC9b28def2011-05-21 14:37:15 +0000248/**
DRC73d74c12012-06-29 23:46:38 +0000249 * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (if the
250 * underlying codec supports it.)
DRC9b28def2011-05-21 14:37:15 +0000251 */
DRC25b995a2011-05-21 15:34:54 +0000252#define TJFLAG_FORCESSE3 128
DRC9b28def2011-05-21 14:37:15 +0000253/**
DRC01fdcc32013-04-26 08:41:25 +0000254 * When decompressing an image that was compressed using chrominance
255 * subsampling, use the fastest chrominance upsampling algorithm available in
256 * the underlying codec. The default is to use smooth upsampling, which
257 * creates a smooth transition between neighboring chrominance components in
258 * order to reduce upsampling artifacts in the decompressed image.
DRC9b28def2011-05-21 14:37:15 +0000259 */
DRC25b995a2011-05-21 15:34:54 +0000260#define TJFLAG_FASTUPSAMPLE 256
DRC9b28def2011-05-21 14:37:15 +0000261/**
DRC25b995a2011-05-21 15:34:54 +0000262 * Disable buffer (re)allocation. If passed to #tjCompress2() or
263 * #tjTransform(), this flag will cause those functions to generate an error if
264 * the JPEG image buffer is invalid or too small rather than attempting to
265 * allocate or reallocate that buffer. This reproduces the behavior of earlier
266 * versions of TurboJPEG.
DRC9b28def2011-05-21 14:37:15 +0000267 */
DRC25b995a2011-05-21 15:34:54 +0000268#define TJFLAG_NOREALLOC 1024
DRC73d74c12012-06-29 23:46:38 +0000269/**
270 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
DRC76577262013-08-18 09:28:09 +0000271 * default if this flag is not specified is implementation-specific. For
272 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
273 * algorithm by default when compressing, because this has been shown to have
274 * only a very slight effect on accuracy, but it uses the accurate algorithm
275 * when decompressing, because this has been shown to have a larger effect.
DRC73d74c12012-06-29 23:46:38 +0000276 */
277#define TJFLAG_FASTDCT 2048
278/**
279 * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
DRC76577262013-08-18 09:28:09 +0000280 * The default if this flag is not specified is implementation-specific. For
281 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
282 * algorithm by default when compressing, because this has been shown to have
283 * only a very slight effect on accuracy, but it uses the accurate algorithm
284 * when decompressing, because this has been shown to have a larger effect.
DRC73d74c12012-06-29 23:46:38 +0000285 */
286#define TJFLAG_ACCURATEDCT 4096
DRC9b28def2011-05-21 14:37:15 +0000287
288
289/**
DRC01fdcc32013-04-26 08:41:25 +0000290 * The number of transform operations
DRC9b28def2011-05-21 14:37:15 +0000291 */
DRC25b995a2011-05-21 15:34:54 +0000292#define TJ_NUMXOP 8
DRC9b28def2011-05-21 14:37:15 +0000293
294/**
DRC25b995a2011-05-21 15:34:54 +0000295 * Transform operations for #tjTransform()
DRC9b28def2011-05-21 14:37:15 +0000296 */
DRC25b995a2011-05-21 15:34:54 +0000297enum TJXOP
DRC9b28def2011-05-21 14:37:15 +0000298{
299 /**
300 * Do not transform the position of the image pixels
301 */
DRC25b995a2011-05-21 15:34:54 +0000302 TJXOP_NONE=0,
DRC9b28def2011-05-21 14:37:15 +0000303 /**
304 * Flip (mirror) image horizontally. This transform is imperfect if there
DRC25b995a2011-05-21 15:34:54 +0000305 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000306 */
DRC25b995a2011-05-21 15:34:54 +0000307 TJXOP_HFLIP,
DRC9b28def2011-05-21 14:37:15 +0000308 /**
309 * Flip (mirror) image vertically. This transform is imperfect if there are
DRC25b995a2011-05-21 15:34:54 +0000310 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000311 */
DRC25b995a2011-05-21 15:34:54 +0000312 TJXOP_VFLIP,
DRC9b28def2011-05-21 14:37:15 +0000313 /**
314 * Transpose image (flip/mirror along upper left to lower right axis.) This
315 * transform is always perfect.
316 */
DRC25b995a2011-05-21 15:34:54 +0000317 TJXOP_TRANSPOSE,
DRC9b28def2011-05-21 14:37:15 +0000318 /**
319 * Transverse transpose image (flip/mirror along upper right to lower left
320 * axis.) This transform is imperfect if there are any partial MCU blocks in
DRC25b995a2011-05-21 15:34:54 +0000321 * the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000322 */
DRC25b995a2011-05-21 15:34:54 +0000323 TJXOP_TRANSVERSE,
DRC9b28def2011-05-21 14:37:15 +0000324 /**
325 * Rotate image clockwise by 90 degrees. This transform is imperfect if
326 * there are any partial MCU blocks on the bottom edge (see
DRC25b995a2011-05-21 15:34:54 +0000327 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000328 */
DRC25b995a2011-05-21 15:34:54 +0000329 TJXOP_ROT90,
DRC9b28def2011-05-21 14:37:15 +0000330 /**
331 * Rotate image 180 degrees. This transform is imperfect if there are any
DRC25b995a2011-05-21 15:34:54 +0000332 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000333 */
DRC25b995a2011-05-21 15:34:54 +0000334 TJXOP_ROT180,
DRC9b28def2011-05-21 14:37:15 +0000335 /**
336 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
337 * if there are any partial MCU blocks on the right edge (see
DRC25b995a2011-05-21 15:34:54 +0000338 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000339 */
DRC25b995a2011-05-21 15:34:54 +0000340 TJXOP_ROT270
DRC9b28def2011-05-21 14:37:15 +0000341};
342
343
344/**
DRC25b995a2011-05-21 15:34:54 +0000345 * This option will cause #tjTransform() to return an error if the transform is
DRC9b28def2011-05-21 14:37:15 +0000346 * not perfect. Lossless transforms operate on MCU blocks, whose size depends
347 * on the level of chrominance subsampling used (see #tjMCUWidth
348 * and #tjMCUHeight.) If the image's width or height is not evenly divisible
349 * by the MCU block size, then there will be partial MCU blocks on the right
350 * and/or bottom edges. It is not possible to move these partial MCU blocks to
351 * the top or left of the image, so any transform that would require that is
352 * "imperfect." If this option is not specified, then any partial MCU blocks
353 * that cannot be transformed will be left in place, which will create
354 * odd-looking strips on the right or bottom edge of the image.
355 */
DRC25b995a2011-05-21 15:34:54 +0000356#define TJXOPT_PERFECT 1
DRC9b28def2011-05-21 14:37:15 +0000357/**
DRC25b995a2011-05-21 15:34:54 +0000358 * This option will cause #tjTransform() to discard any partial MCU blocks that
DRC9b28def2011-05-21 14:37:15 +0000359 * cannot be transformed.
360 */
DRC25b995a2011-05-21 15:34:54 +0000361#define TJXOPT_TRIM 2
DRC9b28def2011-05-21 14:37:15 +0000362/**
DRC25b995a2011-05-21 15:34:54 +0000363 * This option will enable lossless cropping. See #tjTransform() for more
DRC9b28def2011-05-21 14:37:15 +0000364 * information.
365 */
DRC25b995a2011-05-21 15:34:54 +0000366#define TJXOPT_CROP 4
DRC9b28def2011-05-21 14:37:15 +0000367/**
368 * This option will discard the color data in the input image and produce
369 * a grayscale output image.
DRC9b28def2011-05-21 14:37:15 +0000370 */
DRC25b995a2011-05-21 15:34:54 +0000371#define TJXOPT_GRAY 8
DRC7bf04d32011-09-17 00:18:31 +0000372/**
373 * This option will prevent #tjTransform() from outputting a JPEG image for
374 * this particular transform (this can be used in conjunction with a custom
375 * filter to capture the transformed DCT coefficients without transcoding
376 * them.)
377 */
378#define TJXOPT_NOOUTPUT 16
DRC9b28def2011-05-21 14:37:15 +0000379
380
381/**
382 * Scaling factor
383 */
DRC0a079692011-03-02 09:27:49 +0000384typedef struct
385{
DRC9b28def2011-05-21 14:37:15 +0000386 /**
387 * Numerator
388 */
389 int num;
390 /**
391 * Denominator
392 */
393 int denom;
394} tjscalingfactor;
395
396/**
397 * Cropping region
398 */
399typedef struct
400{
401 /**
402 * The left boundary of the cropping region. This must be evenly divisible
403 * by the MCU block width (see #tjMCUWidth.)
404 */
405 int x;
406 /**
407 * The upper boundary of the cropping region. This must be evenly divisible
408 * by the MCU block height (see #tjMCUHeight.)
409 */
410 int y;
411 /**
412 * The width of the cropping region. Setting this to 0 is the equivalent of
413 * setting it to the width of the source JPEG image - x.
414 */
415 int w;
416 /**
417 * The height of the cropping region. Setting this to 0 is the equivalent of
418 * setting it to the height of the source JPEG image - y.
419 */
420 int h;
DRC0a079692011-03-02 09:27:49 +0000421} tjregion;
422
DRC9b28def2011-05-21 14:37:15 +0000423/**
424 * Lossless transform
425 */
DRCf5467112011-09-20 05:02:19 +0000426typedef struct tjtransform
DRC0a079692011-03-02 09:27:49 +0000427{
DRC9b28def2011-05-21 14:37:15 +0000428 /**
429 * Cropping region
430 */
431 tjregion r;
432 /**
DRC25b995a2011-05-21 15:34:54 +0000433 * One of the @ref TJXOP "transform operations"
DRC9b28def2011-05-21 14:37:15 +0000434 */
435 int op;
436 /**
DRC25b995a2011-05-21 15:34:54 +0000437 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
DRC9b28def2011-05-21 14:37:15 +0000438 */
439 int options;
DRC7bf04d32011-09-17 00:18:31 +0000440 /**
DRCf5467112011-09-20 05:02:19 +0000441 * Arbitrary data that can be accessed within the body of the callback
442 * function
443 */
444 void *data;
445 /**
DRC7bf04d32011-09-17 00:18:31 +0000446 * A callback function that can be used to modify the DCT coefficients
447 * after they are losslessly transformed but before they are transcoded to a
DRCa15f19f2014-03-11 09:46:50 +0000448 * new JPEG image. This allows for custom filters or other transformations
449 * to be applied in the frequency domain.
DRC7bf04d32011-09-17 00:18:31 +0000450 *
DRCf5467112011-09-20 05:02:19 +0000451 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
452 * this pointer is not guaranteed to be valid once the callback
453 * returns, so applications wishing to hand off the DCT coefficients
454 * to another function or library should make a copy of them within
455 * the body of the callback.)
456 * @param arrayRegion #tjregion structure containing the width and height of
DRCf69dc282011-09-20 18:20:43 +0000457 * the array pointed to by <tt>coeffs</tt> as well as its offset
458 * relative to the component plane. TurboJPEG implementations may
459 * choose to split each component plane into multiple DCT coefficient
460 * arrays and call the callback function once for each array.
DRCf5467112011-09-20 05:02:19 +0000461 * @param planeRegion #tjregion structure containing the width and height of
462 * the component plane to which <tt>coeffs</tt> belongs
463 * @param componentID ID number of the component plane to which
DRCb3a028e2013-11-25 21:08:47 +0000464 * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of
DRCf5467112011-09-20 05:02:19 +0000465 * 0, 1, and 2 in typical JPEG images.)
466 * @param transformID ID number of the transformed image to which
467 * <tt>coeffs</tt> belongs. This is the same as the index of the
DRC01fdcc32013-04-26 08:41:25 +0000468 * transform in the <tt>transforms</tt> array that was passed to
DRCf5467112011-09-20 05:02:19 +0000469 * #tjTransform().
470 * @param transform a pointer to a #tjtransform structure that specifies the
471 * parameters and/or cropping region for this transform
DRC7bf04d32011-09-17 00:18:31 +0000472 *
473 * @return 0 if the callback was successful, or -1 if an error occurred.
474 */
475 int (*customFilter)(short *coeffs, tjregion arrayRegion,
DRCf5467112011-09-20 05:02:19 +0000476 tjregion planeRegion, int componentIndex, int transformIndex,
477 struct tjtransform *transform);
DRC0a079692011-03-02 09:27:49 +0000478} tjtransform;
479
DRC9b28def2011-05-21 14:37:15 +0000480/**
481 * TurboJPEG instance handle
482 */
DRC2e7b76b2009-04-03 12:04:24 +0000483typedef void* tjhandle;
484
DRC9b28def2011-05-21 14:37:15 +0000485
486/**
487 * Pad the given width to the nearest 32-bit boundary
488 */
489#define TJPAD(width) (((width)+3)&(~3))
490
491/**
DRC25b995a2011-05-21 15:34:54 +0000492 * Compute the scaled value of <tt>dimension</tt> using the given scaling
493 * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
DRC9b28def2011-05-21 14:37:15 +0000494 * scalingFactor)</tt>.
495 */
496#define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \
497 + scalingFactor.denom - 1) / scalingFactor.denom)
498
DRC2e7b76b2009-04-03 12:04:24 +0000499
500#ifdef __cplusplus
501extern "C" {
502#endif
503
DRC2e7b76b2009-04-03 12:04:24 +0000504
DRC9b28def2011-05-21 14:37:15 +0000505/**
506 * Create a TurboJPEG compressor instance.
507 *
508 * @return a handle to the newly-created instance, or NULL if an error
509 * occurred (see #tjGetErrorStr().)
510 */
DRC2e7b76b2009-04-03 12:04:24 +0000511DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
512
513
DRC9b28def2011-05-21 14:37:15 +0000514/**
515 * Compress an RGB or grayscale image into a JPEG image.
516 *
517 * @param handle a handle to a TurboJPEG compressor or transformer instance
518 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
519 * to be compressed
520 * @param width width (in pixels) of the source image
521 * @param pitch bytes per line of the source image. Normally, this should be
522 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
523 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
524 * the image is padded to the nearest 32-bit boundary, as is the case
525 * for Windows bitmaps. You can also be clever and use this parameter
526 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
527 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
528 * @param height height (in pixels) of the source image
DRC25b995a2011-05-21 15:34:54 +0000529 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9b28def2011-05-21 14:37:15 +0000530 * "Pixel formats".)
531 * @param jpegBuf address of a pointer to an image buffer that will receive the
532 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
533 * to accommodate the size of the JPEG image. Thus, you can choose to:
DRC6b76f752011-05-24 16:52:47 +0000534 * -# pre-allocate the JPEG buffer with an arbitrary size using
535 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
DRC9b28def2011-05-21 14:37:15 +0000536 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the
537 * buffer for you, or
538 * -# pre-allocate the buffer to a "worst case" size determined by
DRC9b49f0e2011-07-12 03:17:23 +0000539 * calling #tjBufSize(). This should ensure that the buffer never has
DRC25b995a2011-05-21 15:34:54 +0000540 * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
DRC9b28def2011-05-21 14:37:15 +0000541 * .
DRCff78e372011-05-24 10:17:32 +0000542 * If you choose option 1, <tt>*jpegSize</tt> should be set to the
DRC9b28def2011-05-21 14:37:15 +0000543 * size of your pre-allocated buffer. In any case, unless you have
DRC25b995a2011-05-21 15:34:54 +0000544 * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon
545 * return from this function, as it may have changed.
DRC80803ae2011-12-15 13:12:59 +0000546 * @param jpegSize pointer to an unsigned long variable that holds the size of
DRC9b28def2011-05-21 14:37:15 +0000547 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a
548 * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the
549 * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the
550 * size of the JPEG image (in bytes.)
551 * @param jpegSubsamp the level of chrominance subsampling to be used when
DRC25b995a2011-05-21 15:34:54 +0000552 * generating the JPEG image (see @ref TJSAMP
DRC9b28def2011-05-21 14:37:15 +0000553 * "Chrominance subsampling options".)
554 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
555 100 = best)
DRC25b995a2011-05-21 15:34:54 +0000556 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
557 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000558 *
559 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000560*/
DRC9b28def2011-05-21 14:37:15 +0000561DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
562 int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
563 unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags);
DRC2e7b76b2009-04-03 12:04:24 +0000564
DRCb28fc572011-02-22 06:41:29 +0000565
DRC9b28def2011-05-21 14:37:15 +0000566/**
567 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
DRC9b49f0e2011-07-12 03:17:23 +0000568 * the given parameters. The number of bytes returned by this function is
569 * larger than the size of the uncompressed source image. The reason for this
570 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
DRC01fdcc32013-04-26 08:41:25 +0000571 * for a very high-quality JPEG image with very high-frequency content to
DRC9b49f0e2011-07-12 03:17:23 +0000572 * expand rather than compress when converted to the JPEG format. Such images
573 * represent a very rare corner case, but since there is no way to predict the
574 * size of a JPEG image prior to compression, the corner case has to be
575 * handled.
DRC9b28def2011-05-21 14:37:15 +0000576 *
577 * @param width width of the image (in pixels)
578 * @param height height of the image (in pixels)
DRC9b49f0e2011-07-12 03:17:23 +0000579 * @param jpegSubsamp the level of chrominance subsampling to be used when
580 * generating the JPEG image (see @ref TJSAMP
581 * "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000582 *
583 * @return the maximum size of the buffer (in bytes) required to hold the
584 * image, or -1 if the arguments are out of bounds.
585 */
DRC9b49f0e2011-07-12 03:17:23 +0000586DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
587 int jpegSubsamp);
DRC2e7b76b2009-04-03 12:04:24 +0000588
DRCb28fc572011-02-22 06:41:29 +0000589
DRC9b28def2011-05-21 14:37:15 +0000590/**
591 * The size of the buffer (in bytes) required to hold a YUV planar image with
592 * the given parameters.
593 *
594 * @param width width of the image (in pixels)
595 * @param height height of the image (in pixels)
DRC9b49f0e2011-07-12 03:17:23 +0000596 * @param subsamp level of chrominance subsampling in the image (see
DRC25b995a2011-05-21 15:34:54 +0000597 * @ref TJSAMP "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000598 *
599 * @return the size of the buffer (in bytes) required to hold the image, or
600 * -1 if the arguments are out of bounds.
601 */
DRC9b49f0e2011-07-12 03:17:23 +0000602DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
603 int subsamp);
DRCf3cf9732011-02-22 00:16:14 +0000604
DRCb28fc572011-02-22 06:41:29 +0000605
DRC9b28def2011-05-21 14:37:15 +0000606/**
607 * Encode an RGB or grayscale image into a YUV planar image. This function
608 * uses the accelerated color conversion routines in TurboJPEG's underlying
609 * codec to produce a planar YUV image that is suitable for X Video.
610 * Specifically, if the chrominance components are subsampled along the
DRCd8522a72013-04-26 08:54:10 +0000611 * horizontal dimension, then the width of the luminance plane is padded to the
612 * nearest multiple of 2 in the output image (same goes for the height of the
613 * luminance plane, if the chrominance components are subsampled along the
614 * vertical dimension.) Also, each line of each plane in the output image is
615 * padded to 4 bytes. Although this will work with any subsampling option, it
616 * is really only useful in combination with TJ_420, which produces an image
617 * compatible with the I420 (AKA "YUV420P") format.
DRC5a7e9e52013-11-25 20:30:12 +0000618 * <p>
619 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
620 * convention of the digital video community, the TurboJPEG API uses "YUV" to
621 * refer to an image format consisting of Y, Cb, and Cr image planes.
DRC9b28def2011-05-21 14:37:15 +0000622 *
623 * @param handle a handle to a TurboJPEG compressor or transformer instance
624 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
625 * to be encoded
626 * @param width width (in pixels) of the source image
627 * @param pitch bytes per line of the source image. Normally, this should be
628 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
629 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
630 * the image is padded to the nearest 32-bit boundary, as is the case
631 * for Windows bitmaps. You can also be clever and use this parameter
632 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
633 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
634 * @param height height (in pixels) of the source image
DRC25b995a2011-05-21 15:34:54 +0000635 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9b28def2011-05-21 14:37:15 +0000636 * "Pixel formats".)
DRC80803ae2011-12-15 13:12:59 +0000637 * @param dstBuf pointer to an image buffer that will receive the YUV image.
DRC9b49f0e2011-07-12 03:17:23 +0000638 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
DRC9b28def2011-05-21 14:37:15 +0000639 * based on the image width, height, and level of chrominance
640 * subsampling.
641 * @param subsamp the level of chrominance subsampling to be used when
DRC25b995a2011-05-21 15:34:54 +0000642 * generating the YUV image (see @ref TJSAMP
DRC9b28def2011-05-21 14:37:15 +0000643 * "Chrominance subsampling options".)
DRC25b995a2011-05-21 15:34:54 +0000644 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
645 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000646 *
647 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC84241602011-02-25 02:08:23 +0000648*/
DRC9b28def2011-05-21 14:37:15 +0000649DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle,
650 unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat,
651 unsigned char *dstBuf, int subsamp, int flags);
DRC84241602011-02-25 02:08:23 +0000652
653
DRC9b28def2011-05-21 14:37:15 +0000654/**
655 * Create a TurboJPEG decompressor instance.
656 *
657 * @return a handle to the newly-created instance, or NULL if an error
658 * occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000659*/
660DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
661
662
DRC9b28def2011-05-21 14:37:15 +0000663/**
664 * Retrieve information about a JPEG image without decompressing it.
665 *
666 * @param handle a handle to a TurboJPEG decompressor or transformer instance
667 * @param jpegBuf pointer to a buffer containing a JPEG image
668 * @param jpegSize size of the JPEG image (in bytes)
DRC80803ae2011-12-15 13:12:59 +0000669 * @param width pointer to an integer variable that will receive the width (in
DRC9b28def2011-05-21 14:37:15 +0000670 * pixels) of the JPEG image
DRC80803ae2011-12-15 13:12:59 +0000671 * @param height pointer to an integer variable that will receive the height
DRC9b28def2011-05-21 14:37:15 +0000672 * (in pixels) of the JPEG image
DRC80803ae2011-12-15 13:12:59 +0000673 * @param jpegSubsamp pointer to an integer variable that will receive the
DRC9b28def2011-05-21 14:37:15 +0000674 * level of chrominance subsampling used when compressing the JPEG image
DRC25b995a2011-05-21 15:34:54 +0000675 * (see @ref TJSAMP "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000676 *
677 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000678*/
DRC9b28def2011-05-21 14:37:15 +0000679DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
680 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
681 int *jpegSubsamp);
DRC2e7b76b2009-04-03 12:04:24 +0000682
683
DRC9b28def2011-05-21 14:37:15 +0000684/**
685 * Returns a list of fractional scaling factors that the JPEG decompressor in
686 * this implementation of TurboJPEG supports.
687 *
688 * @param numscalingfactors pointer to an integer variable that will receive
689 * the number of elements in the list
690 *
691 * @return a pointer to a list of fractional scaling factors, or NULL if an
692 * error is encountered (see #tjGetErrorStr().)
DRCb28fc572011-02-22 06:41:29 +0000693*/
DRC109a5782011-03-01 09:53:07 +0000694DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors);
DRCb28fc572011-02-22 06:41:29 +0000695
696
DRC9b28def2011-05-21 14:37:15 +0000697/**
698 * Decompress a JPEG image to an RGB or grayscale image.
699 *
700 * @param handle a handle to a TurboJPEG decompressor or transformer instance
701 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
702 * @param jpegSize size of the JPEG image (in bytes)
DRC80803ae2011-12-15 13:12:59 +0000703 * @param dstBuf pointer to an image buffer that will receive the decompressed
DRC9b28def2011-05-21 14:37:15 +0000704 * image. This buffer should normally be <tt>pitch * scaledHeight</tt>
705 * bytes in size, where <tt>scaledHeight</tt> can be determined by
706 * calling #TJSCALED() with the JPEG image height and one of the scaling
DRC01fdcc32013-04-26 08:41:25 +0000707 * factors returned by #tjGetScalingFactors(). The <tt>dstBuf</tt>
708 * pointer may also be used to decompress into a specific region of a
709 * larger buffer.
DRC9b28def2011-05-21 14:37:15 +0000710 * @param width desired width (in pixels) of the destination image. If this is
DRC9ce1a212013-04-26 05:32:32 +0000711 * different than the width of the JPEG image being decompressed, then
DRC9b28def2011-05-21 14:37:15 +0000712 * TurboJPEG will use scaling in the JPEG decompressor to generate the
713 * largest possible image that will fit within the desired width. If
DRC01fdcc32013-04-26 08:41:25 +0000714 * <tt>width</tt> is set to 0, then only the height will be considered
715 * when determining the scaled image size.
DRC9b28def2011-05-21 14:37:15 +0000716 * @param pitch bytes per line of the destination image. Normally, this is
717 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed
718 * image is unpadded, else <tt>#TJPAD(scaledWidth *
719 * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed
720 * image is padded to the nearest 32-bit boundary, as is the case for
721 * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by
722 * calling #TJSCALED() with the JPEG image width and one of the scaling
723 * factors returned by #tjGetScalingFactors().) You can also be clever
724 * and use the pitch parameter to skip lines, etc. Setting this
725 * parameter to 0 is the equivalent of setting it to <tt>scaledWidth
726 * * #tjPixelSize[pixelFormat]</tt>.
727 * @param height desired height (in pixels) of the destination image. If this
DRC9ce1a212013-04-26 05:32:32 +0000728 * is different than the height of the JPEG image being decompressed,
729 * then TurboJPEG will use scaling in the JPEG decompressor to generate
730 * the largest possible image that will fit within the desired height.
DRC01fdcc32013-04-26 08:41:25 +0000731 * If <tt>height</tt> is set to 0, then only the width will be
732 * considered when determining the scaled image size.
DRC9b28def2011-05-21 14:37:15 +0000733 * @param pixelFormat pixel format of the destination image (see @ref
DRC25b995a2011-05-21 15:34:54 +0000734 * TJPF "Pixel formats".)
735 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
736 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000737 *
738 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
739 */
740DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
741 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
742 int width, int pitch, int height, int pixelFormat, int flags);
DRC2e7b76b2009-04-03 12:04:24 +0000743
744
DRC9b28def2011-05-21 14:37:15 +0000745/**
746 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
747 * decompression but leaves out the color conversion step, so a planar YUV
748 * image is generated instead of an RGB image. The padding of the planes in
DRC01fdcc32013-04-26 08:41:25 +0000749 * this image is the same as in the images generated by #tjEncodeYUV2(). Note
DRC9b28def2011-05-21 14:37:15 +0000750 * that, if the width or height of the image is not an even multiple of the MCU
751 * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer
752 * copy will be performed within TurboJPEG.
DRC5a7e9e52013-11-25 20:30:12 +0000753 * <p>
754 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
755 * convention of the digital video community, the TurboJPEG API uses "YUV" to
756 * refer to an image format consisting of Y, Cb, and Cr image planes.
DRC9b28def2011-05-21 14:37:15 +0000757 *
758 * @param handle a handle to a TurboJPEG decompressor or transformer instance
759 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
760 * @param jpegSize size of the JPEG image (in bytes)
DRC80803ae2011-12-15 13:12:59 +0000761 * @param dstBuf pointer to an image buffer that will receive the YUV image.
DRC01fdcc32013-04-26 08:41:25 +0000762 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
DRC9b28def2011-05-21 14:37:15 +0000763 * based on the image width, height, and level of subsampling.
DRC25b995a2011-05-21 15:34:54 +0000764 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
765 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000766 *
767 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
768 */
769DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
770 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
771 int flags);
DRC84241602011-02-25 02:08:23 +0000772
773
DRC9b28def2011-05-21 14:37:15 +0000774/**
775 * Create a new TurboJPEG transformer instance.
776 *
777 * @return a handle to the newly-created instance, or NULL if an error
778 * occurred (see #tjGetErrorStr().)
779 */
DRC890f1e02011-02-26 22:02:37 +0000780DLLEXPORT tjhandle DLLCALL tjInitTransform(void);
781
782
DRC9b28def2011-05-21 14:37:15 +0000783/**
784 * Losslessly transform a JPEG image into another JPEG image. Lossless
785 * transforms work by moving the raw coefficients from one JPEG image structure
786 * to another without altering the values of the coefficients. While this is
787 * typically faster than decompressing the image, transforming it, and
788 * re-compressing it, lossless transforms are not free. Each lossless
DRC01fdcc32013-04-26 08:41:25 +0000789 * transform requires reading and performing Huffman decoding on all of the
790 * coefficients in the source image, regardless of the size of the destination
791 * image. Thus, this function provides a means of generating multiple
792 * transformed images from the same source or applying multiple
793 * transformations simultaneously, in order to eliminate the need to read the
794 * source coefficients multiple times.
DRC9b28def2011-05-21 14:37:15 +0000795 *
796 * @param handle a handle to a TurboJPEG transformer instance
797 * @param jpegBuf pointer to a buffer containing the JPEG image to transform
798 * @param jpegSize size of the JPEG image (in bytes)
799 * @param n the number of transformed JPEG images to generate
800 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
801 * will receive a JPEG image that has been transformed using the
802 * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to
803 * reallocate the JPEG buffer to accommodate the size of the JPEG image.
804 * Thus, you can choose to:
DRC6b76f752011-05-24 16:52:47 +0000805 * -# pre-allocate the JPEG buffer with an arbitrary size using
806 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
DRC9b28def2011-05-21 14:37:15 +0000807 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the
808 * buffer for you, or
809 * -# pre-allocate the buffer to a "worst case" size determined by
DRC01fdcc32013-04-26 08:41:25 +0000810 * calling #tjBufSize() with the transformed or cropped width and
811 * height. This should ensure that the buffer never has to be
812 * re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
DRC9b28def2011-05-21 14:37:15 +0000813 * .
DRCff78e372011-05-24 10:17:32 +0000814 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to
DRC9b28def2011-05-21 14:37:15 +0000815 * the size of your pre-allocated buffer. In any case, unless you have
DRC25b995a2011-05-21 15:34:54 +0000816 * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt>
817 * upon return from this function, as it may have changed.
DRC80803ae2011-12-15 13:12:59 +0000818 * @param dstSizes pointer to an array of n unsigned long variables that will
DRC9b28def2011-05-21 14:37:15 +0000819 * receive the actual sizes (in bytes) of each transformed JPEG image.
820 * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
821 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon
822 * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image
823 * (in bytes.)
DRC01fdcc32013-04-26 08:41:25 +0000824 * @param transforms pointer to an array of n #tjtransform structures, each of
DRC9b28def2011-05-21 14:37:15 +0000825 * which specifies the transform parameters and/or cropping region for
826 * the corresponding transformed output image.
DRC25b995a2011-05-21 15:34:54 +0000827 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
828 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000829 *
830 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
831 */
832DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
833 unsigned long jpegSize, int n, unsigned char **dstBufs,
834 unsigned long *dstSizes, tjtransform *transforms, int flags);
DRC890f1e02011-02-26 22:02:37 +0000835
836
DRC9b28def2011-05-21 14:37:15 +0000837/**
838 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
839 *
840 * @param handle a handle to a TurboJPEG compressor, decompressor or
841 * transformer instance
842 *
843 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
844 */
845DLLEXPORT int DLLCALL tjDestroy(tjhandle handle);
DRC2e7b76b2009-04-03 12:04:24 +0000846
847
DRC9b28def2011-05-21 14:37:15 +0000848/**
DRC6b76f752011-05-24 16:52:47 +0000849 * Allocate an image buffer for use with TurboJPEG. You should always use
850 * this function to allocate the JPEG destination buffer(s) for #tjCompress2()
851 * and #tjTransform() unless you are disabling automatic buffer
852 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
853 *
854 * @param bytes the number of bytes to allocate
855 *
856 * @return a pointer to a newly-allocated buffer with the specified number of
857 * bytes
DRCd4411072011-05-24 17:00:15 +0000858 *
859 * @sa tjFree()
DRC6b76f752011-05-24 16:52:47 +0000860 */
861DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes);
862
863
864/**
865 * Free an image buffer previously allocated by TurboJPEG. You should always
866 * use this function to free JPEG destination buffer(s) that were automatically
867 * (re)allocated by #tjCompress2() or #tjTransform() or that were manually
868 * allocated using #tjAlloc().
869 *
870 * @param buffer address of the buffer to free
DRCd4411072011-05-24 17:00:15 +0000871 *
872 * @sa tjAlloc()
DRC6b76f752011-05-24 16:52:47 +0000873 */
874DLLEXPORT void DLLCALL tjFree(unsigned char *buffer);
875
876
877/**
DRC9b28def2011-05-21 14:37:15 +0000878 * Returns a descriptive error message explaining why the last command failed.
879 *
880 * @return a descriptive error message explaining why the last command failed.
881 */
DRC2e7b76b2009-04-03 12:04:24 +0000882DLLEXPORT char* DLLCALL tjGetErrorStr(void);
883
DRC9b28def2011-05-21 14:37:15 +0000884
885/* Backward compatibility functions and macros (nothing to see here) */
886#define NUMSUBOPT TJ_NUMSAMP
DRC25b995a2011-05-21 15:34:54 +0000887#define TJ_444 TJSAMP_444
888#define TJ_422 TJSAMP_422
889#define TJ_420 TJSAMP_420
890#define TJ_411 TJSAMP_420
891#define TJ_GRAYSCALE TJSAMP_GRAY
DRC9b28def2011-05-21 14:37:15 +0000892
DRC25b995a2011-05-21 15:34:54 +0000893#define TJ_BGR 1
894#define TJ_BOTTOMUP TJFLAG_BOTTOMUP
895#define TJ_FORCEMMX TJFLAG_FORCEMMX
896#define TJ_FORCESSE TJFLAG_FORCESSE
897#define TJ_FORCESSE2 TJFLAG_FORCESSE2
DRC9b28def2011-05-21 14:37:15 +0000898#define TJ_ALPHAFIRST 64
DRC25b995a2011-05-21 15:34:54 +0000899#define TJ_FORCESSE3 TJFLAG_FORCESSE3
900#define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
DRC9b28def2011-05-21 14:37:15 +0000901#define TJ_YUV 512
902
DRC9b49f0e2011-07-12 03:17:23 +0000903DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
904
905DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
906 int jpegSubsamp);
907
DRC9b28def2011-05-21 14:37:15 +0000908DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
909 int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
910 unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags);
911
912DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle,
913 unsigned char *srcBuf, int width, int pitch, int height, int pixelSize,
914 unsigned char *dstBuf, int subsamp, int flags);
915
916DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
917 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height);
918
919DLLEXPORT int DLLCALL tjDecompress(tjhandle handle,
920 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
921 int width, int pitch, int height, int pixelSize, int flags);
922
923
924/**
925 * @}
926 */
927
DRC2e7b76b2009-04-03 12:04:24 +0000928#ifdef __cplusplus
929}
930#endif
DRC3a1bb352011-05-24 09:15:44 +0000931
932#endif