blob: e4c57799edde304805e087da9956c18be86c3c00 [file] [log] [blame]
DRC9b28def2011-05-21 14:37:15 +00001/*
2 * Copyright (C)2009-2011 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.
DRC9b28def2011-05-21 14:37:15 +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
58 * 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".
62 */
DRC25b995a2011-05-21 15:34:54 +000063enum TJSAMP
DRC109a5782011-03-01 09:53:07 +000064{
DRC9b28def2011-05-21 14:37:15 +000065 /**
66 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
67 * YUV image will contain one chrominance component for every pixel in the
68 * source image.
69 */
DRC25b995a2011-05-21 15:34:54 +000070 TJSAMP_444=0,
DRC9b28def2011-05-21 14:37:15 +000071 /**
72 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
73 * chrominance component for every 2x1 block of pixels in the source image.
74 */
DRC25b995a2011-05-21 15:34:54 +000075 TJSAMP_422,
DRC9b28def2011-05-21 14:37:15 +000076 /**
77 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
78 * chrominance component for every 2x2 block of pixels in the source image.
79 */
DRC25b995a2011-05-21 15:34:54 +000080 TJSAMP_420,
DRC9b28def2011-05-21 14:37:15 +000081 /**
82 * Grayscale. The JPEG or YUV image will contain no chrominance components.
83 */
DRC25b995a2011-05-21 15:34:54 +000084 TJSAMP_GRAY,
DRC9b28def2011-05-21 14:37:15 +000085 /**
86 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
87 * chrominance component for every 1x2 block of pixels in the source image.
88 */
DRC25b995a2011-05-21 15:34:54 +000089 TJSAMP_440
DRC890f1e02011-02-26 22:02:37 +000090};
91
DRC9b28def2011-05-21 14:37:15 +000092/**
93 * MCU block width (in pixels) for a given level of chrominance subsampling.
94 * MCU block sizes:
95 * - 8x8 for no subsampling or grayscale
96 * - 16x8 for 4:2:2
97 * - 8x16 for 4:4:0
98 * - 16x16 for 4:2:0
99 */
100static const int tjMCUWidth[TJ_NUMSAMP] = {8, 16, 16, 8, 8};
DRC890f1e02011-02-26 22:02:37 +0000101
DRC9b28def2011-05-21 14:37:15 +0000102/**
103 * MCU block height (in pixels) for a given level of chrominance subsampling.
104 * MCU block sizes:
105 * - 8x8 for no subsampling or grayscale
106 * - 16x8 for 4:2:2
107 * - 8x16 for 4:4:0
108 * - 16x16 for 4:2:0
109 */
110static const int tjMCUHeight[TJ_NUMSAMP] = {8, 8, 16, 8, 16};
111
112
113/**
114 * The number of pixel formats
115 */
116#define TJ_NUMPF 7
117
118/**
119 * Pixel formats
DRC9b28def2011-05-21 14:37:15 +0000120 */
DRC25b995a2011-05-21 15:34:54 +0000121enum TJPF
DRC9b28def2011-05-21 14:37:15 +0000122{
123 /**
124 * RGB pixel format. The red, green, and blue components in the image are
125 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
126 * address within each pixel.
127 */
DRC25b995a2011-05-21 15:34:54 +0000128 TJPF_RGB=0,
DRC9b28def2011-05-21 14:37:15 +0000129 /**
130 * BGR pixel format. The red, green, and blue components in the image are
131 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
132 * address within each pixel.
133 */
DRC25b995a2011-05-21 15:34:54 +0000134 TJPF_BGR,
DRC9b28def2011-05-21 14:37:15 +0000135 /**
136 * RGBX pixel format. The red, green, and blue components in the image are
137 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
138 * address within each pixel.
139 */
DRC25b995a2011-05-21 15:34:54 +0000140 TJPF_RGBX,
DRC9b28def2011-05-21 14:37:15 +0000141 /**
142 * BGRX pixel format. The red, green, and blue components in the image are
143 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
144 * address within each pixel.
145 */
DRC25b995a2011-05-21 15:34:54 +0000146 TJPF_BGRX,
DRC9b28def2011-05-21 14:37:15 +0000147 /**
148 * XBGR pixel format. The red, green, and blue components in the image are
149 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
150 * address within each pixel.
151 */
DRC25b995a2011-05-21 15:34:54 +0000152 TJPF_XBGR,
DRC9b28def2011-05-21 14:37:15 +0000153 /**
154 * XRGB pixel format. The red, green, and blue components in the image are
155 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
156 * address within each pixel.
157 */
DRC25b995a2011-05-21 15:34:54 +0000158 TJPF_XRGB,
DRC9b28def2011-05-21 14:37:15 +0000159 /**
160 * Grayscale pixel format. Each 1-byte pixel represents a luminance
161 * (brightness) level from 0 to 255.
162 */
DRC25b995a2011-05-21 15:34:54 +0000163 TJPF_GRAY
DRC9b28def2011-05-21 14:37:15 +0000164};
165
166/**
167 * Red offset (in bytes) for a given pixel format. This specifies the number
168 * of bytes that the red component is offset from the start of the pixel. For
169 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
170 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>.
171 */
172static const int tjRedOffset[TJ_NUMPF] = {0, 2, 0, 2, 3, 1, 0};
173/**
174 * Green offset (in bytes) for a given pixel format. This specifies the number
175 * of bytes that the green component is offset from the start of the pixel.
176 * For instance, if a pixel of format TJ_BGRX is stored in
177 * <tt>char pixel[]</tt>, then the green component will be
178 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>.
179 */
180static const int tjGreenOffset[TJ_NUMPF] = {1, 1, 1, 1, 2, 2, 0};
181/**
182 * Blue offset (in bytes) for a given pixel format. This specifies the number
183 * of bytes that the Blue component is offset from the start of the pixel. For
184 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
185 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>.
186 */
187static const int tjBlueOffset[TJ_NUMPF] = {2, 0, 2, 0, 1, 3, 0};
188
189/**
190 * Pixel size (in bytes) for a given pixel format.
191 */
192static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1};
193
194
195/**
DRC9b28def2011-05-21 14:37:15 +0000196 * The uncompressed source/destination image is stored in bottom-up (Windows,
197 * OpenGL) order, not top-down (X11) order.
198 */
DRC25b995a2011-05-21 15:34:54 +0000199#define TJFLAG_BOTTOMUP 2
DRC9b28def2011-05-21 14:37:15 +0000200/**
DRC25b995a2011-05-21 15:34:54 +0000201 * Turn off CPU auto-detection and force TurboJPEG to use MMX code (IPP and
202 * 32-bit libjpeg-turbo versions only.)
DRC9b28def2011-05-21 14:37:15 +0000203 */
DRC25b995a2011-05-21 15:34:54 +0000204#define TJFLAG_FORCEMMX 8
DRC9b28def2011-05-21 14:37:15 +0000205/**
DRC25b995a2011-05-21 15:34:54 +0000206 * Turn off CPU auto-detection and force TurboJPEG to use SSE code (32-bit IPP
207 * and 32-bit libjpeg-turbo versions only)
DRC9b28def2011-05-21 14:37:15 +0000208 */
DRC25b995a2011-05-21 15:34:54 +0000209#define TJFLAG_FORCESSE 16
DRC9b28def2011-05-21 14:37:15 +0000210/**
DRC25b995a2011-05-21 15:34:54 +0000211 * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (32-bit IPP
212 * and 32-bit libjpeg-turbo versions only)
DRC9b28def2011-05-21 14:37:15 +0000213 */
DRC25b995a2011-05-21 15:34:54 +0000214#define TJFLAG_FORCESSE2 32
DRC9b28def2011-05-21 14:37:15 +0000215/**
DRC25b995a2011-05-21 15:34:54 +0000216 * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (64-bit IPP
217 * version only)
DRC9b28def2011-05-21 14:37:15 +0000218 */
DRC25b995a2011-05-21 15:34:54 +0000219#define TJFLAG_FORCESSE3 128
DRC9b28def2011-05-21 14:37:15 +0000220/**
DRC25b995a2011-05-21 15:34:54 +0000221 * Use fast, inaccurate chrominance upsampling routines in the JPEG
222 * decompressor (libjpeg and libjpeg-turbo versions only)
DRC9b28def2011-05-21 14:37:15 +0000223 */
DRC25b995a2011-05-21 15:34:54 +0000224#define TJFLAG_FASTUPSAMPLE 256
DRC9b28def2011-05-21 14:37:15 +0000225/**
DRC25b995a2011-05-21 15:34:54 +0000226 * Disable buffer (re)allocation. If passed to #tjCompress2() or
227 * #tjTransform(), this flag will cause those functions to generate an error if
228 * the JPEG image buffer is invalid or too small rather than attempting to
229 * allocate or reallocate that buffer. This reproduces the behavior of earlier
230 * versions of TurboJPEG.
DRC9b28def2011-05-21 14:37:15 +0000231 */
DRC25b995a2011-05-21 15:34:54 +0000232#define TJFLAG_NOREALLOC 1024
DRC9b28def2011-05-21 14:37:15 +0000233
234
235/**
236 * Number of transform operations
237 */
DRC25b995a2011-05-21 15:34:54 +0000238#define TJ_NUMXOP 8
DRC9b28def2011-05-21 14:37:15 +0000239
240/**
DRC25b995a2011-05-21 15:34:54 +0000241 * Transform operations for #tjTransform()
DRC9b28def2011-05-21 14:37:15 +0000242 */
DRC25b995a2011-05-21 15:34:54 +0000243enum TJXOP
DRC9b28def2011-05-21 14:37:15 +0000244{
245 /**
246 * Do not transform the position of the image pixels
247 */
DRC25b995a2011-05-21 15:34:54 +0000248 TJXOP_NONE=0,
DRC9b28def2011-05-21 14:37:15 +0000249 /**
250 * Flip (mirror) image horizontally. This transform is imperfect if there
DRC25b995a2011-05-21 15:34:54 +0000251 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000252 */
DRC25b995a2011-05-21 15:34:54 +0000253 TJXOP_HFLIP,
DRC9b28def2011-05-21 14:37:15 +0000254 /**
255 * Flip (mirror) image vertically. This transform is imperfect if there are
DRC25b995a2011-05-21 15:34:54 +0000256 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000257 */
DRC25b995a2011-05-21 15:34:54 +0000258 TJXOP_VFLIP,
DRC9b28def2011-05-21 14:37:15 +0000259 /**
260 * Transpose image (flip/mirror along upper left to lower right axis.) This
261 * transform is always perfect.
262 */
DRC25b995a2011-05-21 15:34:54 +0000263 TJXOP_TRANSPOSE,
DRC9b28def2011-05-21 14:37:15 +0000264 /**
265 * Transverse transpose image (flip/mirror along upper right to lower left
266 * axis.) This transform is imperfect if there are any partial MCU blocks in
DRC25b995a2011-05-21 15:34:54 +0000267 * the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000268 */
DRC25b995a2011-05-21 15:34:54 +0000269 TJXOP_TRANSVERSE,
DRC9b28def2011-05-21 14:37:15 +0000270 /**
271 * Rotate image clockwise by 90 degrees. This transform is imperfect if
272 * there are any partial MCU blocks on the bottom edge (see
DRC25b995a2011-05-21 15:34:54 +0000273 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000274 */
DRC25b995a2011-05-21 15:34:54 +0000275 TJXOP_ROT90,
DRC9b28def2011-05-21 14:37:15 +0000276 /**
277 * Rotate image 180 degrees. This transform is imperfect if there are any
DRC25b995a2011-05-21 15:34:54 +0000278 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000279 */
DRC25b995a2011-05-21 15:34:54 +0000280 TJXOP_ROT180,
DRC9b28def2011-05-21 14:37:15 +0000281 /**
282 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
283 * if there are any partial MCU blocks on the right edge (see
DRC25b995a2011-05-21 15:34:54 +0000284 * #TJXOPT_PERFECT.)
DRC9b28def2011-05-21 14:37:15 +0000285 */
DRC25b995a2011-05-21 15:34:54 +0000286 TJXOP_ROT270
DRC9b28def2011-05-21 14:37:15 +0000287};
288
289
290/**
DRC25b995a2011-05-21 15:34:54 +0000291 * This option will cause #tjTransform() to return an error if the transform is
DRC9b28def2011-05-21 14:37:15 +0000292 * not perfect. Lossless transforms operate on MCU blocks, whose size depends
293 * on the level of chrominance subsampling used (see #tjMCUWidth
294 * and #tjMCUHeight.) If the image's width or height is not evenly divisible
295 * by the MCU block size, then there will be partial MCU blocks on the right
296 * and/or bottom edges. It is not possible to move these partial MCU blocks to
297 * the top or left of the image, so any transform that would require that is
298 * "imperfect." If this option is not specified, then any partial MCU blocks
299 * that cannot be transformed will be left in place, which will create
300 * odd-looking strips on the right or bottom edge of the image.
301 */
DRC25b995a2011-05-21 15:34:54 +0000302#define TJXOPT_PERFECT 1
DRC9b28def2011-05-21 14:37:15 +0000303/**
DRC25b995a2011-05-21 15:34:54 +0000304 * This option will cause #tjTransform() to discard any partial MCU blocks that
DRC9b28def2011-05-21 14:37:15 +0000305 * cannot be transformed.
306 */
DRC25b995a2011-05-21 15:34:54 +0000307#define TJXOPT_TRIM 2
DRC9b28def2011-05-21 14:37:15 +0000308/**
DRC25b995a2011-05-21 15:34:54 +0000309 * This option will enable lossless cropping. See #tjTransform() for more
DRC9b28def2011-05-21 14:37:15 +0000310 * information.
311 */
DRC25b995a2011-05-21 15:34:54 +0000312#define TJXOPT_CROP 4
DRC9b28def2011-05-21 14:37:15 +0000313/**
314 * This option will discard the color data in the input image and produce
315 * a grayscale output image.
DRC9b28def2011-05-21 14:37:15 +0000316 */
DRC25b995a2011-05-21 15:34:54 +0000317#define TJXOPT_GRAY 8
DRC9b28def2011-05-21 14:37:15 +0000318
319
320/**
321 * Scaling factor
322 */
DRC0a079692011-03-02 09:27:49 +0000323typedef struct
324{
DRC9b28def2011-05-21 14:37:15 +0000325 /**
326 * Numerator
327 */
328 int num;
329 /**
330 * Denominator
331 */
332 int denom;
333} tjscalingfactor;
334
335/**
336 * Cropping region
337 */
338typedef struct
339{
340 /**
341 * The left boundary of the cropping region. This must be evenly divisible
342 * by the MCU block width (see #tjMCUWidth.)
343 */
344 int x;
345 /**
346 * The upper boundary of the cropping region. This must be evenly divisible
347 * by the MCU block height (see #tjMCUHeight.)
348 */
349 int y;
350 /**
351 * The width of the cropping region. Setting this to 0 is the equivalent of
352 * setting it to the width of the source JPEG image - x.
353 */
354 int w;
355 /**
356 * The height of the cropping region. Setting this to 0 is the equivalent of
357 * setting it to the height of the source JPEG image - y.
358 */
359 int h;
DRC0a079692011-03-02 09:27:49 +0000360} tjregion;
361
DRC9b28def2011-05-21 14:37:15 +0000362/**
363 * Lossless transform
364 */
DRC0a079692011-03-02 09:27:49 +0000365typedef struct
366{
DRC9b28def2011-05-21 14:37:15 +0000367 /**
368 * Cropping region
369 */
370 tjregion r;
371 /**
DRC25b995a2011-05-21 15:34:54 +0000372 * One of the @ref TJXOP "transform operations"
DRC9b28def2011-05-21 14:37:15 +0000373 */
374 int op;
375 /**
DRC25b995a2011-05-21 15:34:54 +0000376 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
DRC9b28def2011-05-21 14:37:15 +0000377 */
378 int options;
DRC0a079692011-03-02 09:27:49 +0000379} tjtransform;
380
DRC9b28def2011-05-21 14:37:15 +0000381/**
382 * TurboJPEG instance handle
383 */
DRC2e7b76b2009-04-03 12:04:24 +0000384typedef void* tjhandle;
385
DRC9b28def2011-05-21 14:37:15 +0000386
387/**
388 * Pad the given width to the nearest 32-bit boundary
389 */
390#define TJPAD(width) (((width)+3)&(~3))
391
392/**
DRC25b995a2011-05-21 15:34:54 +0000393 * Compute the scaled value of <tt>dimension</tt> using the given scaling
394 * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
DRC9b28def2011-05-21 14:37:15 +0000395 * scalingFactor)</tt>.
396 */
397#define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \
398 + scalingFactor.denom - 1) / scalingFactor.denom)
399
DRC2e7b76b2009-04-03 12:04:24 +0000400
401#ifdef __cplusplus
402extern "C" {
403#endif
404
DRC2e7b76b2009-04-03 12:04:24 +0000405
DRC9b28def2011-05-21 14:37:15 +0000406/**
407 * Create a TurboJPEG compressor instance.
408 *
409 * @return a handle to the newly-created instance, or NULL if an error
410 * occurred (see #tjGetErrorStr().)
411 */
DRC2e7b76b2009-04-03 12:04:24 +0000412DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
413
414
DRC9b28def2011-05-21 14:37:15 +0000415/**
416 * Compress an RGB or grayscale image into a JPEG image.
417 *
418 * @param handle a handle to a TurboJPEG compressor or transformer instance
419 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
420 * to be compressed
421 * @param width width (in pixels) of the source image
422 * @param pitch bytes per line of the source image. Normally, this should be
423 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
424 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
425 * the image is padded to the nearest 32-bit boundary, as is the case
426 * for Windows bitmaps. You can also be clever and use this parameter
427 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
428 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
429 * @param height height (in pixels) of the source image
DRC25b995a2011-05-21 15:34:54 +0000430 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9b28def2011-05-21 14:37:15 +0000431 * "Pixel formats".)
432 * @param jpegBuf address of a pointer to an image buffer that will receive the
433 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
434 * to accommodate the size of the JPEG image. Thus, you can choose to:
DRC6b76f752011-05-24 16:52:47 +0000435 * -# pre-allocate the JPEG buffer with an arbitrary size using
436 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
DRC9b28def2011-05-21 14:37:15 +0000437 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the
438 * buffer for you, or
439 * -# pre-allocate the buffer to a "worst case" size determined by
DRC9b49f0e2011-07-12 03:17:23 +0000440 * calling #tjBufSize(). This should ensure that the buffer never has
DRC25b995a2011-05-21 15:34:54 +0000441 * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
DRC9b28def2011-05-21 14:37:15 +0000442 * .
DRCff78e372011-05-24 10:17:32 +0000443 * If you choose option 1, <tt>*jpegSize</tt> should be set to the
DRC9b28def2011-05-21 14:37:15 +0000444 * size of your pre-allocated buffer. In any case, unless you have
DRC25b995a2011-05-21 15:34:54 +0000445 * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon
446 * return from this function, as it may have changed.
DRC9b28def2011-05-21 14:37:15 +0000447 * @param jpegSize pointer to an unsigned long variable which holds the size of
448 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a
449 * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the
450 * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the
451 * size of the JPEG image (in bytes.)
452 * @param jpegSubsamp the level of chrominance subsampling to be used when
DRC25b995a2011-05-21 15:34:54 +0000453 * generating the JPEG image (see @ref TJSAMP
DRC9b28def2011-05-21 14:37:15 +0000454 * "Chrominance subsampling options".)
455 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
456 100 = best)
DRC25b995a2011-05-21 15:34:54 +0000457 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
458 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000459 *
460 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000461*/
DRC9b28def2011-05-21 14:37:15 +0000462DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
463 int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
464 unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags);
DRC2e7b76b2009-04-03 12:04:24 +0000465
DRCb28fc572011-02-22 06:41:29 +0000466
DRC9b28def2011-05-21 14:37:15 +0000467/**
468 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
DRC9b49f0e2011-07-12 03:17:23 +0000469 * the given parameters. The number of bytes returned by this function is
470 * larger than the size of the uncompressed source image. The reason for this
471 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
472 * for a very high-quality JPEG image with very high frequency content to
473 * expand rather than compress when converted to the JPEG format. Such images
474 * represent a very rare corner case, but since there is no way to predict the
475 * size of a JPEG image prior to compression, the corner case has to be
476 * handled.
DRC9b28def2011-05-21 14:37:15 +0000477 *
478 * @param width width of the image (in pixels)
479 * @param height height of the image (in pixels)
DRC9b49f0e2011-07-12 03:17:23 +0000480 * @param jpegSubsamp the level of chrominance subsampling to be used when
481 * generating the JPEG image (see @ref TJSAMP
482 * "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000483 *
484 * @return the maximum size of the buffer (in bytes) required to hold the
485 * image, or -1 if the arguments are out of bounds.
486 */
DRC9b49f0e2011-07-12 03:17:23 +0000487DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
488 int jpegSubsamp);
DRC2e7b76b2009-04-03 12:04:24 +0000489
DRCb28fc572011-02-22 06:41:29 +0000490
DRC9b28def2011-05-21 14:37:15 +0000491/**
492 * The size of the buffer (in bytes) required to hold a YUV planar image with
493 * the given parameters.
494 *
495 * @param width width of the image (in pixels)
496 * @param height height of the image (in pixels)
DRC9b49f0e2011-07-12 03:17:23 +0000497 * @param subsamp level of chrominance subsampling in the image (see
DRC25b995a2011-05-21 15:34:54 +0000498 * @ref TJSAMP "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000499 *
500 * @return the size of the buffer (in bytes) required to hold the image, or
501 * -1 if the arguments are out of bounds.
502 */
DRC9b49f0e2011-07-12 03:17:23 +0000503DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
504 int subsamp);
DRCf3cf9732011-02-22 00:16:14 +0000505
DRCb28fc572011-02-22 06:41:29 +0000506
DRC9b28def2011-05-21 14:37:15 +0000507/**
508 * Encode an RGB or grayscale image into a YUV planar image. This function
509 * uses the accelerated color conversion routines in TurboJPEG's underlying
510 * codec to produce a planar YUV image that is suitable for X Video.
511 * Specifically, if the chrominance components are subsampled along the
512 * horizontal dimension, then the width of the luminance plane is padded to 2
513 * in the output image (same goes for the height of the luminance plane, if the
514 * chrominance components are subsampled along the vertical dimension.) Also,
515 * each line of each plane in the output image is padded to 4 bytes. Although
516 * this will work with any subsampling option, it is really only useful in
517 * combination with TJ_420, which produces an image compatible with the I420
518 * (AKA "YUV420P") format.
519 *
520 * @param handle a handle to a TurboJPEG compressor or transformer instance
521 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
522 * to be encoded
523 * @param width width (in pixels) of the source image
524 * @param pitch bytes per line of the source image. Normally, this should be
525 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
526 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
527 * the image is padded to the nearest 32-bit boundary, as is the case
528 * for Windows bitmaps. You can also be clever and use this parameter
529 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
530 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
531 * @param height height (in pixels) of the source image
DRC25b995a2011-05-21 15:34:54 +0000532 * @param pixelFormat pixel format of the source image (see @ref TJPF
DRC9b28def2011-05-21 14:37:15 +0000533 * "Pixel formats".)
534 * @param dstBuf pointer to an image buffer which will receive the YUV image.
DRC9b49f0e2011-07-12 03:17:23 +0000535 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
DRC9b28def2011-05-21 14:37:15 +0000536 * based on the image width, height, and level of chrominance
537 * subsampling.
538 * @param subsamp the level of chrominance subsampling to be used when
DRC25b995a2011-05-21 15:34:54 +0000539 * generating the YUV image (see @ref TJSAMP
DRC9b28def2011-05-21 14:37:15 +0000540 * "Chrominance subsampling options".)
DRC25b995a2011-05-21 15:34:54 +0000541 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
542 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000543 *
544 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC84241602011-02-25 02:08:23 +0000545*/
DRC9b28def2011-05-21 14:37:15 +0000546DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle,
547 unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat,
548 unsigned char *dstBuf, int subsamp, int flags);
DRC84241602011-02-25 02:08:23 +0000549
550
DRC9b28def2011-05-21 14:37:15 +0000551/**
552 * Create a TurboJPEG decompressor instance.
553 *
554 * @return a handle to the newly-created instance, or NULL if an error
555 * occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000556*/
557DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
558
559
DRC9b28def2011-05-21 14:37:15 +0000560/**
561 * Retrieve information about a JPEG image without decompressing it.
562 *
563 * @param handle a handle to a TurboJPEG decompressor or transformer instance
564 * @param jpegBuf pointer to a buffer containing a JPEG image
565 * @param jpegSize size of the JPEG image (in bytes)
566 * @param width pointer to an integer variable which will receive the width (in
567 * pixels) of the JPEG image
568 * @param height pointer to an integer variable which will receive the height
569 * (in pixels) of the JPEG image
570 * @param jpegSubsamp pointer to an integer variable which will receive the
571 * level of chrominance subsampling used when compressing the JPEG image
DRC25b995a2011-05-21 15:34:54 +0000572 * (see @ref TJSAMP "Chrominance subsampling options".)
DRC9b28def2011-05-21 14:37:15 +0000573 *
574 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
DRC2e7b76b2009-04-03 12:04:24 +0000575*/
DRC9b28def2011-05-21 14:37:15 +0000576DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
577 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
578 int *jpegSubsamp);
DRC2e7b76b2009-04-03 12:04:24 +0000579
580
DRC9b28def2011-05-21 14:37:15 +0000581/**
582 * Returns a list of fractional scaling factors that the JPEG decompressor in
583 * this implementation of TurboJPEG supports.
584 *
585 * @param numscalingfactors pointer to an integer variable that will receive
586 * the number of elements in the list
587 *
588 * @return a pointer to a list of fractional scaling factors, or NULL if an
589 * error is encountered (see #tjGetErrorStr().)
DRCb28fc572011-02-22 06:41:29 +0000590*/
DRC109a5782011-03-01 09:53:07 +0000591DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors);
DRCb28fc572011-02-22 06:41:29 +0000592
593
DRC9b28def2011-05-21 14:37:15 +0000594/**
595 * Decompress a JPEG image to an RGB or grayscale image.
596 *
597 * @param handle a handle to a TurboJPEG decompressor or transformer instance
598 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
599 * @param jpegSize size of the JPEG image (in bytes)
600 * @param dstBuf pointer to an image buffer which will receive the decompressed
601 * image. This buffer should normally be <tt>pitch * scaledHeight</tt>
602 * bytes in size, where <tt>scaledHeight</tt> can be determined by
603 * calling #TJSCALED() with the JPEG image height and one of the scaling
604 * factors returned by #tjGetScalingFactors(). The dstBuf pointer may
605 * also be used to decompress into a specific region of a larger buffer.
606 * @param width desired width (in pixels) of the destination image. If this is
607 * smaller than the width of the JPEG image being decompressed, then
608 * TurboJPEG will use scaling in the JPEG decompressor to generate the
609 * largest possible image that will fit within the desired width. If
610 * width is set to 0, then only the height will be considered when
611 * determining the scaled image size.
612 * @param pitch bytes per line of the destination image. Normally, this is
613 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed
614 * image is unpadded, else <tt>#TJPAD(scaledWidth *
615 * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed
616 * image is padded to the nearest 32-bit boundary, as is the case for
617 * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by
618 * calling #TJSCALED() with the JPEG image width and one of the scaling
619 * factors returned by #tjGetScalingFactors().) You can also be clever
620 * and use the pitch parameter to skip lines, etc. Setting this
621 * parameter to 0 is the equivalent of setting it to <tt>scaledWidth
622 * * #tjPixelSize[pixelFormat]</tt>.
623 * @param height desired height (in pixels) of the destination image. If this
624 * is smaller than the height of the JPEG image being decompressed, then
625 * TurboJPEG will use scaling in the JPEG decompressor to generate the
626 * largest possible image that will fit within the desired height. If
627 * height is set to 0, then only the width will be considered when
628 * determining the scaled image size.
629 * @param pixelFormat pixel format of the destination image (see @ref
DRC25b995a2011-05-21 15:34:54 +0000630 * TJPF "Pixel formats".)
631 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
632 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000633 *
634 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
635 */
636DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
637 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
638 int width, int pitch, int height, int pixelFormat, int flags);
DRC2e7b76b2009-04-03 12:04:24 +0000639
640
DRC9b28def2011-05-21 14:37:15 +0000641/**
642 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
643 * decompression but leaves out the color conversion step, so a planar YUV
644 * image is generated instead of an RGB image. The padding of the planes in
645 * this image is the same as the images generated by #tjEncodeYUV2(). Note
646 * that, if the width or height of the image is not an even multiple of the MCU
647 * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer
648 * copy will be performed within TurboJPEG.
649 *
650 * @param handle a handle to a TurboJPEG decompressor or transformer instance
651 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
652 * @param jpegSize size of the JPEG image (in bytes)
653 * @param dstBuf pointer to an image buffer which will receive the YUV image.
DRC9b49f0e2011-07-12 03:17:23 +0000654 * Use #tjBufSizeYUV to determine the appropriate size for this buffer
DRC9b28def2011-05-21 14:37:15 +0000655 * based on the image width, height, and level of subsampling.
DRC25b995a2011-05-21 15:34:54 +0000656 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
657 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000658 *
659 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
660 */
661DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
662 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
663 int flags);
DRC84241602011-02-25 02:08:23 +0000664
665
DRC9b28def2011-05-21 14:37:15 +0000666/**
667 * Create a new TurboJPEG transformer instance.
668 *
669 * @return a handle to the newly-created instance, or NULL if an error
670 * occurred (see #tjGetErrorStr().)
671 */
DRC890f1e02011-02-26 22:02:37 +0000672DLLEXPORT tjhandle DLLCALL tjInitTransform(void);
673
674
DRC9b28def2011-05-21 14:37:15 +0000675/**
676 * Losslessly transform a JPEG image into another JPEG image. Lossless
677 * transforms work by moving the raw coefficients from one JPEG image structure
678 * to another without altering the values of the coefficients. While this is
679 * typically faster than decompressing the image, transforming it, and
680 * re-compressing it, lossless transforms are not free. Each lossless
681 * transform requires reading and Huffman decoding all of the coefficients in
682 * the source image, regardless of the size of the destination image. Thus,
683 * this function provides a means of generating multiple transformed images
684 * from the same source or of applying multiple transformations simultaneously,
685 * in order to eliminate the need to read the source coefficients multiple
686 * times.
687 *
688 * @param handle a handle to a TurboJPEG transformer instance
689 * @param jpegBuf pointer to a buffer containing the JPEG image to transform
690 * @param jpegSize size of the JPEG image (in bytes)
691 * @param n the number of transformed JPEG images to generate
692 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
693 * will receive a JPEG image that has been transformed using the
694 * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to
695 * reallocate the JPEG buffer to accommodate the size of the JPEG image.
696 * Thus, you can choose to:
DRC6b76f752011-05-24 16:52:47 +0000697 * -# pre-allocate the JPEG buffer with an arbitrary size using
698 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
DRC9b28def2011-05-21 14:37:15 +0000699 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the
700 * buffer for you, or
701 * -# pre-allocate the buffer to a "worst case" size determined by
DRC9b49f0e2011-07-12 03:17:23 +0000702 * calling #tjBufSize() with the cropped width and height. This should
DRC25b995a2011-05-21 15:34:54 +0000703 * ensure that the buffer never has to be re-allocated (setting
704 * #TJFLAG_NOREALLOC guarantees this.)
DRC9b28def2011-05-21 14:37:15 +0000705 * .
DRCff78e372011-05-24 10:17:32 +0000706 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to
DRC9b28def2011-05-21 14:37:15 +0000707 * the size of your pre-allocated buffer. In any case, unless you have
DRC25b995a2011-05-21 15:34:54 +0000708 * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt>
709 * upon return from this function, as it may have changed.
DRC9b28def2011-05-21 14:37:15 +0000710 * @param dstSizes pointer to an array of n unsigned long variables which will
711 * receive the actual sizes (in bytes) of each transformed JPEG image.
712 * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
713 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon
714 * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image
715 * (in bytes.)
716 * @param transforms pointer to an array of n tjtransform structures, each of
717 * which specifies the transform parameters and/or cropping region for
718 * the corresponding transformed output image.
DRC25b995a2011-05-21 15:34:54 +0000719 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
720 * "flags".
DRC9b28def2011-05-21 14:37:15 +0000721 *
722 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
723 */
724DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
725 unsigned long jpegSize, int n, unsigned char **dstBufs,
726 unsigned long *dstSizes, tjtransform *transforms, int flags);
DRC890f1e02011-02-26 22:02:37 +0000727
728
DRC9b28def2011-05-21 14:37:15 +0000729/**
730 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
731 *
732 * @param handle a handle to a TurboJPEG compressor, decompressor or
733 * transformer instance
734 *
735 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
736 */
737DLLEXPORT int DLLCALL tjDestroy(tjhandle handle);
DRC2e7b76b2009-04-03 12:04:24 +0000738
739
DRC9b28def2011-05-21 14:37:15 +0000740/**
DRC6b76f752011-05-24 16:52:47 +0000741 * Allocate an image buffer for use with TurboJPEG. You should always use
742 * this function to allocate the JPEG destination buffer(s) for #tjCompress2()
743 * and #tjTransform() unless you are disabling automatic buffer
744 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
745 *
746 * @param bytes the number of bytes to allocate
747 *
748 * @return a pointer to a newly-allocated buffer with the specified number of
749 * bytes
DRCd4411072011-05-24 17:00:15 +0000750 *
751 * @sa tjFree()
DRC6b76f752011-05-24 16:52:47 +0000752 */
753DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes);
754
755
756/**
757 * Free an image buffer previously allocated by TurboJPEG. You should always
758 * use this function to free JPEG destination buffer(s) that were automatically
759 * (re)allocated by #tjCompress2() or #tjTransform() or that were manually
760 * allocated using #tjAlloc().
761 *
762 * @param buffer address of the buffer to free
DRCd4411072011-05-24 17:00:15 +0000763 *
764 * @sa tjAlloc()
DRC6b76f752011-05-24 16:52:47 +0000765 */
766DLLEXPORT void DLLCALL tjFree(unsigned char *buffer);
767
768
769/**
DRC9b28def2011-05-21 14:37:15 +0000770 * Returns a descriptive error message explaining why the last command failed.
771 *
772 * @return a descriptive error message explaining why the last command failed.
773 */
DRC2e7b76b2009-04-03 12:04:24 +0000774DLLEXPORT char* DLLCALL tjGetErrorStr(void);
775
DRC9b28def2011-05-21 14:37:15 +0000776
777/* Backward compatibility functions and macros (nothing to see here) */
778#define NUMSUBOPT TJ_NUMSAMP
DRC25b995a2011-05-21 15:34:54 +0000779#define TJ_444 TJSAMP_444
780#define TJ_422 TJSAMP_422
781#define TJ_420 TJSAMP_420
782#define TJ_411 TJSAMP_420
783#define TJ_GRAYSCALE TJSAMP_GRAY
DRC9b28def2011-05-21 14:37:15 +0000784
DRC25b995a2011-05-21 15:34:54 +0000785#define TJ_BGR 1
786#define TJ_BOTTOMUP TJFLAG_BOTTOMUP
787#define TJ_FORCEMMX TJFLAG_FORCEMMX
788#define TJ_FORCESSE TJFLAG_FORCESSE
789#define TJ_FORCESSE2 TJFLAG_FORCESSE2
DRC9b28def2011-05-21 14:37:15 +0000790#define TJ_ALPHAFIRST 64
DRC25b995a2011-05-21 15:34:54 +0000791#define TJ_FORCESSE3 TJFLAG_FORCESSE3
792#define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
DRC9b28def2011-05-21 14:37:15 +0000793#define TJ_YUV 512
794
DRC9b49f0e2011-07-12 03:17:23 +0000795DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
796
797DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
798 int jpegSubsamp);
799
DRC9b28def2011-05-21 14:37:15 +0000800DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
801 int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
802 unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags);
803
804DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle,
805 unsigned char *srcBuf, int width, int pitch, int height, int pixelSize,
806 unsigned char *dstBuf, int subsamp, int flags);
807
808DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
809 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height);
810
811DLLEXPORT int DLLCALL tjDecompress(tjhandle handle,
812 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
813 int width, int pitch, int height, int pixelSize, int flags);
814
815
816/**
817 * @}
818 */
819
DRC2e7b76b2009-04-03 12:04:24 +0000820#ifdef __cplusplus
821}
822#endif
DRC3a1bb352011-05-24 09:15:44 +0000823
824#endif