blob: 24816d7ecd1c24cb0714da6492bdfa5b1168585e [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
DRC8254df12011-02-15 07:57:48 +00003 * Copyright (C)2009-2011 D. R. Commander
DRC2e7b76b2009-04-03 12:04:24 +00004 *
5 * This library is free software and may be redistributed and/or modified under
6 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
7 * any later version. The full license is in the LICENSE.txt file included
8 * with this distribution.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * wxWindows Library License for more details.
14 */
15
16#if (defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)) && defined(_WIN32) && defined(DLLDEFINE)
17#define DLLEXPORT __declspec(dllexport)
18#else
19#define DLLEXPORT
20#endif
21
22#define DLLCALL
23
24/* Subsampling */
25#define NUMSUBOPT 4
26
DRC61e51f92009-04-05 21:53:20 +000027enum {TJ_444=0, TJ_422, TJ_420, TJ_GRAYSCALE};
DRC4ea16c02010-05-17 16:42:14 +000028#define TJ_411 TJ_420 /* for backward compatibility with VirtualGL <= 2.1.x,
29 TurboVNC <= 0.6, and TurboJPEG/IPP */
DRC2e7b76b2009-04-03 12:04:24 +000030
31/* Flags */
DRC8f0d62c2010-02-22 08:35:19 +000032#define TJ_BGR 1
33 /* The components of each pixel in the source/destination bitmap are stored
34 in B,G,R order, not R,G,B */
35#define TJ_BOTTOMUP 2
36 /* The source/destination bitmap is stored in bottom-up (Windows, OpenGL)
37 order, not top-down (X11) order */
38#define TJ_FORCEMMX 8
39 /* Turn off CPU auto-detection and force TurboJPEG to use MMX code
40 (IPP and 32-bit libjpeg-turbo versions only) */
41#define TJ_FORCESSE 16
42 /* Turn off CPU auto-detection and force TurboJPEG to use SSE code
43 (32-bit IPP and 32-bit libjpeg-turbo versions only) */
44#define TJ_FORCESSE2 32
45 /* Turn off CPU auto-detection and force TurboJPEG to use SSE2 code
46 (32-bit IPP and 32-bit libjpeg-turbo versions only) */
47#define TJ_ALPHAFIRST 64
48 /* If the source/destination bitmap is 32 bpp, assume that each pixel is
49 ARGB/XRGB (or ABGR/XBGR if TJ_BGR is also specified) */
50#define TJ_FORCESSE3 128
51 /* Turn off CPU auto-detection and force TurboJPEG to use SSE3 code
52 (64-bit IPP version only) */
53#define TJ_FASTUPSAMPLE 256
54 /* Use fast, inaccurate 4:2:2 and 4:2:0 YUV upsampling routines
DRC7d9d4342011-02-22 00:31:27 +000055 (libjpeg and libjpeg-turbo versions only) */
DRCfbb67472010-11-24 04:02:37 +000056#define TJ_YUV 512
DRCeeab6952011-02-25 00:02:04 +000057 /* Nothing to see here. Pay no attention to the man behind the curtain. */
DRCfbb67472010-11-24 04:02:37 +000058
DRC2e7b76b2009-04-03 12:04:24 +000059typedef void* tjhandle;
60
61#define TJPAD(p) (((p)+3)&(~3))
62#ifndef max
63 #define max(a,b) ((a)>(b)?(a):(b))
64#endif
65
66#ifdef __cplusplus
67extern "C" {
68#endif
69
70/* API follows */
71
72
73/*
74 tjhandle tjInitCompress(void)
75
76 Creates a new JPEG compressor instance, allocates memory for the structures,
77 and returns a handle to the instance. Most applications will only
78 need to call this once at the beginning of the program or once for each
79 concurrent thread. Don't try to create a new instance every time you
DRCeeab6952011-02-25 00:02:04 +000080 compress an image, because this may cause performance to suffer in some
81 TurboJPEG implementations.
DRC2e7b76b2009-04-03 12:04:24 +000082
83 RETURNS: NULL on error
84*/
85DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
86
87
88/*
89 int tjCompress(tjhandle j,
90 unsigned char *srcbuf, int width, int pitch, int height, int pixelsize,
91 unsigned char *dstbuf, unsigned long *size,
92 int jpegsubsamp, int jpegqual, int flags)
93
94 [INPUT] j = instance handle previously returned from a call to
95 tjInitCompress()
DRC3e87c8b2011-02-19 16:42:14 +000096 [INPUT] srcbuf = pointer to user-allocated image buffer containing RGB or
97 grayscale pixels to be compressed
DRCeeab6952011-02-25 00:02:04 +000098 [INPUT] width = width (in pixels) of the source image
DRC2e7b76b2009-04-03 12:04:24 +000099 [INPUT] pitch = bytes per line of the source image (width*pixelsize if the
100 bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap
101 is padded to the nearest 32-bit boundary, such as is the case for Windows
DRC8254df12011-02-15 07:57:48 +0000102 bitmaps. You can also be clever and use this parameter to skip lines,
103 etc. Setting this parameter to 0 is the equivalent of setting it to
DRC3e87c8b2011-02-19 16:42:14 +0000104 width*pixelsize.
DRC2e7b76b2009-04-03 12:04:24 +0000105 [INPUT] height = height (in pixels) of the source image
106 [INPUT] pixelsize = size (in bytes) of each pixel in the source image
DRC3e87c8b2011-02-19 16:42:14 +0000107 RGBX/BGRX/XRGB/XBGR: 4, RGB/BGR: 3, Grayscale: 1
DRC2e7b76b2009-04-03 12:04:24 +0000108 [INPUT] dstbuf = pointer to user-allocated image buffer which will receive
DRC3e87c8b2011-02-19 16:42:14 +0000109 the JPEG image. Use the TJBUFSIZE(width, height) function to determine
DRC2e7b76b2009-04-03 12:04:24 +0000110 the appropriate size for this buffer based on the image width and height.
111 [OUTPUT] size = pointer to unsigned long which receives the size (in bytes)
112 of the compressed image
DRCeeab6952011-02-25 00:02:04 +0000113 [INPUT] jpegsubsamp = Specifies either 4:2:0, 4:2:2, 4:4:4, or grayscale
114 subsampling. When the image is converted from the RGB to YCbCr colorspace
115 as part of the JPEG compression process, every other Cb and Cr
116 (chrominance) pixel can be discarded to produce a smaller image with
117 little perceptible loss of image clarity (the human eye is more sensitive
118 to small changes in brightness than small changes in color.)
DRC2e7b76b2009-04-03 12:04:24 +0000119
DRC61e51f92009-04-05 21:53:20 +0000120 TJ_420: 4:2:0 subsampling. Discards every other Cb, Cr pixel in both
DRC3e87c8b2011-02-19 16:42:14 +0000121 horizontal and vertical directions
DRC2e7b76b2009-04-03 12:04:24 +0000122 TJ_422: 4:2:2 subsampling. Discards every other Cb, Cr pixel only in
DRC3e87c8b2011-02-19 16:42:14 +0000123 the horizontal direction
124 TJ_444: no subsampling
DRC2e7b76b2009-04-03 12:04:24 +0000125 TJ_GRAYSCALE: Generate grayscale JPEG image
126
DRC3e87c8b2011-02-19 16:42:14 +0000127 [INPUT] jpegqual = JPEG quality (an integer between 0 and 100 inclusive)
DRC8f0d62c2010-02-22 08:35:19 +0000128 [INPUT] flags = the bitwise OR of one or more of the flags described in the
DRC3e87c8b2011-02-19 16:42:14 +0000129 "Flags" section above
DRC2e7b76b2009-04-03 12:04:24 +0000130
131 RETURNS: 0 on success, -1 on error
132*/
133DLLEXPORT int DLLCALL tjCompress(tjhandle j,
134 unsigned char *srcbuf, int width, int pitch, int height, int pixelsize,
135 unsigned char *dstbuf, unsigned long *size,
136 int jpegsubsamp, int jpegqual, int flags);
137
DRCeeab6952011-02-25 00:02:04 +0000138
139/*
140 unsigned long TJBUFSIZE(int width, int height)
141
142 Convenience function which returns the maximum size of the buffer required to
143 hold a JPEG image with the given width and height
144
145 RETURNS: -1 if arguments are out of bounds
146*/
DRC2e7b76b2009-04-03 12:04:24 +0000147DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
148
DRCeeab6952011-02-25 00:02:04 +0000149
150/*
151 unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
152
153 Convenience function which returns the size of the buffer required to
154 hold a YUV planar image with the given width, height, and level of
155 chrominance subsampling
156
157 RETURNS: -1 if arguments are out of bounds
158*/
159DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
160 int subsamp);
161
162
163/*
164 int tjEncodeYUV(tjhandle j,
165 unsigned char *srcbuf, int width, int pitch, int height, int pixelsize,
166 unsigned char *dstbuf, int subsamp, int flags)
167
168 This function uses the accelerated color conversion routines in TurboJPEG's
169 underlying codec to produce a planar YUV image that is suitable for X Video.
170 Specifically, if the chrominance components are subsampled along the
171 horizontal dimension, then the width of the luminance plane is padded to 2 in
172 the output image (same goes for the height of the luminance plane, if the
173 chrominance components are subsampled along the vertical dimension.) Also,
174 each line of each plane in the output image is padded to 4 bytes. Although
175 this will work with any subsampling option, it is really only useful in
176 combination with TJ_420, which produces an image compatible with the I420
177 (AKA "YUV420P") format.
178
179 [INPUT] j = instance handle previously returned from a call to
180 tjInitCompress()
181 [INPUT] srcbuf = pointer to user-allocated image buffer containing RGB or
182 grayscale pixels to be encoded
183 [INPUT] width = width (in pixels) of the source image
184 [INPUT] pitch = bytes per line of the source image (width*pixelsize if the
185 bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap
186 is padded to the nearest 32-bit boundary, such as is the case for Windows
187 bitmaps. You can also be clever and use this parameter to skip lines,
188 etc. Setting this parameter to 0 is the equivalent of setting it to
189 width*pixelsize.
190 [INPUT] height = height (in pixels) of the source image
191 [INPUT] pixelsize = size (in bytes) of each pixel in the source image
192 RGBX/BGRX/XRGB/XBGR: 4, RGB/BGR: 3, Grayscale: 1
193 [INPUT] dstbuf = pointer to user-allocated image buffer which will receive
194 the YUV image. Use the TJBUFSIZEYUV(width, height, subsamp) function to
195 determine the appropriate size for this buffer based on the image width,
196 height, and level of subsampling.
197 [INPUT] subsamp = Specifies either 4:2:0, 4:2:2, 4:4:4, or grayscale
198 subsampling (see description under tjCompress())
199 [INPUT] flags = the bitwise OR of one or more of the flags described in the
200 "Flags" section above
201
202 RETURNS: 0 on success, -1 on error
203*/
204DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle j,
205 unsigned char *srcbuf, int width, int pitch, int height, int pixelsize,
206 unsigned char *dstbuf, int subsamp, int flags);
207
208
DRC2e7b76b2009-04-03 12:04:24 +0000209/*
210 tjhandle tjInitDecompress(void)
211
212 Creates a new JPEG decompressor instance, allocates memory for the
213 structures, and returns a handle to the instance. Most applications will
214 only need to call this once at the beginning of the program or once for each
215 concurrent thread. Don't try to create a new instance every time you
DRCeeab6952011-02-25 00:02:04 +0000216 decompress an image, because this may cause performance to suffer in some
217 TurboJPEG implementations.
DRC2e7b76b2009-04-03 12:04:24 +0000218
219 RETURNS: NULL on error
220*/
221DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
222
223
224/*
DRC1fe80f82010-12-14 01:21:29 +0000225 int tjDecompressHeader2(tjhandle j,
DRC2e7b76b2009-04-03 12:04:24 +0000226 unsigned char *srcbuf, unsigned long size,
DRC3e87c8b2011-02-19 16:42:14 +0000227 int *width, int *height, int *jpegsubsamp)
DRC2e7b76b2009-04-03 12:04:24 +0000228
229 [INPUT] j = instance handle previously returned from a call to
230 tjInitDecompress()
DRCeeab6952011-02-25 00:02:04 +0000231 [INPUT] srcbuf = pointer to a user-allocated buffer containing a JPEG image
DRC2e7b76b2009-04-03 12:04:24 +0000232 [INPUT] size = size of the JPEG image buffer (in bytes)
233 [OUTPUT] width = width (in pixels) of the JPEG image
234 [OUTPUT] height = height (in pixels) of the JPEG image
DRC3e87c8b2011-02-19 16:42:14 +0000235 [OUTPUT] jpegsubsamp = type of chrominance subsampling used when compressing
236 the JPEG image
DRC2e7b76b2009-04-03 12:04:24 +0000237
238 RETURNS: 0 on success, -1 on error
239*/
DRC1fe80f82010-12-14 01:21:29 +0000240DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle j,
241 unsigned char *srcbuf, unsigned long size,
DRC3e87c8b2011-02-19 16:42:14 +0000242 int *width, int *height, int *jpegsubsamp);
DRC1fe80f82010-12-14 01:21:29 +0000243
244/*
DRCe27124a2011-02-08 07:14:33 +0000245 Legacy version of the above function
DRC1fe80f82010-12-14 01:21:29 +0000246*/
DRC2e7b76b2009-04-03 12:04:24 +0000247DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle j,
248 unsigned char *srcbuf, unsigned long size,
249 int *width, int *height);
250
251
252/*
253 int tjDecompress(tjhandle j,
254 unsigned char *srcbuf, unsigned long size,
255 unsigned char *dstbuf, int width, int pitch, int height, int pixelsize,
256 int flags)
257
258 [INPUT] j = instance handle previously returned from a call to
259 tjInitDecompress()
260 [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image
261 to decompress
262 [INPUT] size = size of the JPEG image buffer (in bytes)
263 [INPUT] dstbuf = pointer to user-allocated image buffer which will receive
264 the bitmap image. This buffer should normally be pitch*height
265 bytes in size, although this pointer may also be used to decompress into
266 a specific region of a larger buffer.
DRCeeab6952011-02-25 00:02:04 +0000267 [INPUT] width = width (in pixels) of the destination image
DRC3e87c8b2011-02-19 16:42:14 +0000268 [INPUT] pitch = bytes per line of the destination image (width*pixelsize if
269 the bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the
270 bitmap is padded to the nearest 32-bit boundary, such as is the case for
271 Windows bitmaps. You can also be clever and use this parameter to skip
272 lines, etc. Setting this parameter to 0 is the equivalent of setting it
273 to width*pixelsize.
DRC2e7b76b2009-04-03 12:04:24 +0000274 [INPUT] height = height (in pixels) of the destination image
275 [INPUT] pixelsize = size (in bytes) of each pixel in the destination image
DRC3e87c8b2011-02-19 16:42:14 +0000276 RGBX/BGRX/XRGB/XBGR: 4, RGB/BGR: 3, Grayscale: 1
DRC8f0d62c2010-02-22 08:35:19 +0000277 [INPUT] flags = the bitwise OR of one or more of the flags described in the
278 "Flags" section above.
DRC2e7b76b2009-04-03 12:04:24 +0000279
280 RETURNS: 0 on success, -1 on error
281*/
282DLLEXPORT int DLLCALL tjDecompress(tjhandle j,
283 unsigned char *srcbuf, unsigned long size,
284 unsigned char *dstbuf, int width, int pitch, int height, int pixelsize,
285 int flags);
286
287
288/*
DRCeeab6952011-02-25 00:02:04 +0000289 int tjDecompressToYUV(tjhandle j,
290 unsigned char *srcbuf, unsigned long size,
291 unsigned char *dstbuf, int flags)
292
293 This function performs JPEG decompression but leaves out the color conversion
294 step, so a planar YUV image is generated instead of an RGB image. The
295 padding of the planes in this image is the same as in tjEncodeYUV().
296 Note that, if the width or height of the output image is not a multiple of 8
297 (or a multiple of 16 along any dimension in which chrominance subsampling is
298 used), then an intermediate buffer copy will be performed within TurboJPEG.
299
300 [INPUT] j = instance handle previously returned from a call to
301 tjInitDecompress()
302 [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image
303 to decompress
304 [INPUT] size = size of the JPEG image buffer (in bytes)
305 [INPUT] dstbuf = pointer to user-allocated image buffer which will receive
306 the YUV image. Use the TJBUFSIZEYUV(width, height, subsamp) function to
307 determine the appropriate size for this buffer based on the image width,
308 height, and level of subsampling.
309 [INPUT] flags = the bitwise OR of one or more of the flags described in the
310 "Flags" section above.
311
312 RETURNS: 0 on success, -1 on error
313*/
314DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle j,
315 unsigned char *srcbuf, unsigned long size,
316 unsigned char *dstbuf, int flags);
317
318
319/*
DRC2e7b76b2009-04-03 12:04:24 +0000320 int tjDestroy(tjhandle h)
321
322 Frees structures associated with a compression or decompression instance
323
324 [INPUT] h = instance handle (returned from a previous call to
325 tjInitCompress() or tjInitDecompress()
326
327 RETURNS: 0 on success, -1 on error
328*/
329DLLEXPORT int DLLCALL tjDestroy(tjhandle h);
330
331
332/*
333 char *tjGetErrorStr(void)
334
335 Returns a descriptive error message explaining why the last command failed
336*/
337DLLEXPORT char* DLLCALL tjGetErrorStr(void);
338
339#ifdef __cplusplus
340}
341#endif