DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1 | /* Copyright (C)2004 Landmark Graphics Corporation |
| 2 | * Copyright (C)2005, 2006 Sun Microsystems, Inc. |
DRC | 8254df1 | 2011-02-15 07:57:48 +0000 | [diff] [blame] | 3 | * Copyright (C)2009-2011 D. R. Commander |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 4 | * |
| 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 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 27 | enum {TJ_444=0, TJ_422, TJ_420, TJ_GRAYSCALE}; |
DRC | 4ea16c0 | 2010-05-17 16:42:14 +0000 | [diff] [blame] | 28 | #define TJ_411 TJ_420 /* for backward compatibility with VirtualGL <= 2.1.x, |
| 29 | TurboVNC <= 0.6, and TurboJPEG/IPP */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 30 | |
| 31 | /* Flags */ |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 32 | #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 |
DRC | 7d9d434 | 2011-02-22 00:31:27 +0000 | [diff] [blame] | 55 | (libjpeg and libjpeg-turbo versions only) */ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 56 | #define TJ_YUV 512 |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 57 | /* Nothing to see here. Pay no attention to the man behind the curtain. */ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 58 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 59 | typedef 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 |
| 67 | extern "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 |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 80 | compress an image, because this may cause performance to suffer in some |
| 81 | TurboJPEG implementations. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 82 | |
| 83 | RETURNS: NULL on error |
| 84 | */ |
| 85 | DLLEXPORT 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() |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 96 | [INPUT] srcbuf = pointer to user-allocated image buffer containing RGB or |
| 97 | grayscale pixels to be compressed |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 98 | [INPUT] width = width (in pixels) of the source image |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 99 | [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 |
DRC | 8254df1 | 2011-02-15 07:57:48 +0000 | [diff] [blame] | 102 | 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 |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 104 | width*pixelsize. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 105 | [INPUT] height = height (in pixels) of the source image |
| 106 | [INPUT] pixelsize = size (in bytes) of each pixel in the source image |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 107 | RGBX/BGRX/XRGB/XBGR: 4, RGB/BGR: 3, Grayscale: 1 |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 108 | [INPUT] dstbuf = pointer to user-allocated image buffer which will receive |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 109 | the JPEG image. Use the TJBUFSIZE(width, height) function to determine |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 110 | 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 |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 113 | [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.) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 119 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 120 | TJ_420: 4:2:0 subsampling. Discards every other Cb, Cr pixel in both |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 121 | horizontal and vertical directions |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 122 | TJ_422: 4:2:2 subsampling. Discards every other Cb, Cr pixel only in |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 123 | the horizontal direction |
| 124 | TJ_444: no subsampling |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 125 | TJ_GRAYSCALE: Generate grayscale JPEG image |
| 126 | |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 127 | [INPUT] jpegqual = JPEG quality (an integer between 0 and 100 inclusive) |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 128 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 129 | "Flags" section above |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 130 | |
| 131 | RETURNS: 0 on success, -1 on error |
| 132 | */ |
| 133 | DLLEXPORT 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 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 138 | |
| 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 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 147 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height); |
| 148 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 149 | |
| 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 | */ |
| 159 | DLLEXPORT 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 | */ |
| 204 | DLLEXPORT 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 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 209 | /* |
| 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 |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 216 | decompress an image, because this may cause performance to suffer in some |
| 217 | TurboJPEG implementations. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 218 | |
| 219 | RETURNS: NULL on error |
| 220 | */ |
| 221 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void); |
| 222 | |
| 223 | |
| 224 | /* |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 225 | int tjDecompressHeader2(tjhandle j, |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 226 | unsigned char *srcbuf, unsigned long size, |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 227 | int *width, int *height, int *jpegsubsamp) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 228 | |
| 229 | [INPUT] j = instance handle previously returned from a call to |
| 230 | tjInitDecompress() |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 231 | [INPUT] srcbuf = pointer to a user-allocated buffer containing a JPEG image |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 232 | [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 |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 235 | [OUTPUT] jpegsubsamp = type of chrominance subsampling used when compressing |
| 236 | the JPEG image |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 237 | |
| 238 | RETURNS: 0 on success, -1 on error |
| 239 | */ |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 240 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle j, |
| 241 | unsigned char *srcbuf, unsigned long size, |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 242 | int *width, int *height, int *jpegsubsamp); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 243 | |
| 244 | /* |
DRC | e27124a | 2011-02-08 07:14:33 +0000 | [diff] [blame] | 245 | Legacy version of the above function |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 246 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 247 | DLLEXPORT 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. |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 267 | [INPUT] width = width (in pixels) of the destination image |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 268 | [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. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 274 | [INPUT] height = height (in pixels) of the destination image |
| 275 | [INPUT] pixelsize = size (in bytes) of each pixel in the destination image |
DRC | 3e87c8b | 2011-02-19 16:42:14 +0000 | [diff] [blame] | 276 | RGBX/BGRX/XRGB/XBGR: 4, RGB/BGR: 3, Grayscale: 1 |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 277 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
| 278 | "Flags" section above. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 279 | |
| 280 | RETURNS: 0 on success, -1 on error |
| 281 | */ |
| 282 | DLLEXPORT 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 | /* |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 289 | 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 | */ |
| 314 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle j, |
| 315 | unsigned char *srcbuf, unsigned long size, |
| 316 | unsigned char *dstbuf, int flags); |
| 317 | |
| 318 | |
| 319 | /* |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 320 | 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 | */ |
| 329 | DLLEXPORT 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 | */ |
| 337 | DLLEXPORT char* DLLCALL tjGetErrorStr(void); |
| 338 | |
| 339 | #ifdef __cplusplus |
| 340 | } |
| 341 | #endif |