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 | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 3 | * Copyright (C)2009 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 |
| 55 | (libjpeg version only) */ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 56 | #define TJ_YUV 512 |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 57 | /* If passed to tjCompress(), this causes TurboJPEG/OSS to use the |
| 58 | accelerated color conversion routines in libjpeg-turbo to produce a planar |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 59 | YUV image that is suitable for X Video. Specifically, if the chrominance |
| 60 | components are subsampled along the horizontal dimension, then the width |
| 61 | of the luminance plane is padded to 2 in the output image (same goes for |
| 62 | the height of the luminance plane, if the chrominance components are |
| 63 | subsampled along the vertical dimension.) Also, each line of each plane |
| 64 | in the output image is padded to 4 bytes. Although this will work with |
| 65 | any subsampling option, it is really only useful in combination with |
| 66 | TJ_420, which produces an image compatible with the I420 (AKA "YUV420P") |
| 67 | format. |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 68 | |
| 69 | If passed to tjDecompress(), this tells TurboJPEG/OSS to perform JPEG |
| 70 | decompression but to leave out the color conversion step, so a planar YUV |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 71 | image is generated instead of an RGB image. The padding of the planes in |
| 72 | this image is the same as in the above case. Note that, if the width or |
| 73 | height of the output image is not a multiple of 8 (or a multiple of 16 |
| 74 | along any dimension in which chrominance subsampling is used), then an |
| 75 | intermediate buffer copy will be performed within TurboJPEG/OSS. |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 76 | */ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 77 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 78 | typedef void* tjhandle; |
| 79 | |
| 80 | #define TJPAD(p) (((p)+3)&(~3)) |
| 81 | #ifndef max |
| 82 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 83 | #endif |
| 84 | |
| 85 | #ifdef __cplusplus |
| 86 | extern "C" { |
| 87 | #endif |
| 88 | |
| 89 | /* API follows */ |
| 90 | |
| 91 | |
| 92 | /* |
| 93 | tjhandle tjInitCompress(void) |
| 94 | |
| 95 | Creates a new JPEG compressor instance, allocates memory for the structures, |
| 96 | and returns a handle to the instance. Most applications will only |
| 97 | need to call this once at the beginning of the program or once for each |
| 98 | concurrent thread. Don't try to create a new instance every time you |
| 99 | compress an image, because this will cause performance to suffer. |
| 100 | |
| 101 | RETURNS: NULL on error |
| 102 | */ |
| 103 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void); |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | int tjCompress(tjhandle j, |
| 108 | unsigned char *srcbuf, int width, int pitch, int height, int pixelsize, |
| 109 | unsigned char *dstbuf, unsigned long *size, |
| 110 | int jpegsubsamp, int jpegqual, int flags) |
| 111 | |
| 112 | [INPUT] j = instance handle previously returned from a call to |
| 113 | tjInitCompress() |
| 114 | [INPUT] srcbuf = pointer to user-allocated image buffer containing pixels in |
| 115 | RGB(A) or BGR(A) form |
| 116 | [INPUT] width = width (in pixels) of the source image |
| 117 | [INPUT] pitch = bytes per line of the source image (width*pixelsize if the |
| 118 | bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap |
| 119 | is padded to the nearest 32-bit boundary, such as is the case for Windows |
| 120 | bitmaps. You can also be clever and use this parameter to skip lines, etc., |
| 121 | as long as the pitch is greater than 0.) |
| 122 | [INPUT] height = height (in pixels) of the source image |
| 123 | [INPUT] pixelsize = size (in bytes) of each pixel in the source image |
DRC | 7ab9d7c | 2010-11-04 22:42:18 +0000 | [diff] [blame] | 124 | RGBA and BGRA: 4, RGB and BGR: 3, Grayscale: 1 |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 125 | [INPUT] dstbuf = pointer to user-allocated image buffer which will receive |
| 126 | the JPEG image. Use the macro TJBUFSIZE(width, height) to determine |
| 127 | the appropriate size for this buffer based on the image width and height. |
| 128 | [OUTPUT] size = pointer to unsigned long which receives the size (in bytes) |
| 129 | of the compressed image |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 130 | [INPUT] jpegsubsamp = Specifies either 4:2:0, 4:2:2, or 4:4:4 subsampling. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 131 | When the image is converted from the RGB to YCbCr colorspace as part of the |
| 132 | JPEG compression process, every other Cb and Cr (chrominance) pixel can be |
| 133 | discarded to produce a smaller image with little perceptible loss of |
| 134 | image clarity (the human eye is more sensitive to small changes in |
| 135 | brightness than small changes in color.) |
| 136 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 137 | TJ_420: 4:2:0 subsampling. Discards every other Cb, Cr pixel in both |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 138 | horizontal and vertical directions. |
| 139 | TJ_422: 4:2:2 subsampling. Discards every other Cb, Cr pixel only in |
| 140 | the horizontal direction. |
| 141 | TJ_444: no subsampling. |
| 142 | TJ_GRAYSCALE: Generate grayscale JPEG image |
| 143 | |
| 144 | [INPUT] jpegqual = JPEG quality (an integer between 0 and 100 inclusive.) |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 145 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
| 146 | "Flags" section above. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 147 | |
| 148 | RETURNS: 0 on success, -1 on error |
| 149 | */ |
| 150 | DLLEXPORT int DLLCALL tjCompress(tjhandle j, |
| 151 | unsigned char *srcbuf, int width, int pitch, int height, int pixelsize, |
| 152 | unsigned char *dstbuf, unsigned long *size, |
| 153 | int jpegsubsamp, int jpegqual, int flags); |
| 154 | |
| 155 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height); |
| 156 | |
| 157 | /* |
| 158 | tjhandle tjInitDecompress(void) |
| 159 | |
| 160 | Creates a new JPEG decompressor instance, allocates memory for the |
| 161 | structures, and returns a handle to the instance. Most applications will |
| 162 | only need to call this once at the beginning of the program or once for each |
| 163 | concurrent thread. Don't try to create a new instance every time you |
| 164 | decompress an image, because this will cause performance to suffer. |
| 165 | |
| 166 | RETURNS: NULL on error |
| 167 | */ |
| 168 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void); |
| 169 | |
| 170 | |
| 171 | /* |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 172 | int tjDecompressHeader2(tjhandle j, |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 173 | unsigned char *srcbuf, unsigned long size, |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 174 | int *width, int *height, int *jpegsub) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 175 | |
| 176 | [INPUT] j = instance handle previously returned from a call to |
| 177 | tjInitDecompress() |
| 178 | [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image |
| 179 | to decompress |
| 180 | [INPUT] size = size of the JPEG image buffer (in bytes) |
| 181 | [OUTPUT] width = width (in pixels) of the JPEG image |
| 182 | [OUTPUT] height = height (in pixels) of the JPEG image |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 183 | [OUTPUT] jpegsub = type of chrominance subsampling used when compressing the |
| 184 | JPEG image |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 185 | |
| 186 | RETURNS: 0 on success, -1 on error |
| 187 | */ |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 188 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle j, |
| 189 | unsigned char *srcbuf, unsigned long size, |
| 190 | int *width, int *height, int *jpegsub); |
| 191 | |
| 192 | /* |
| 193 | Deprecated version of the above function |
| 194 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 195 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle j, |
| 196 | unsigned char *srcbuf, unsigned long size, |
| 197 | int *width, int *height); |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | int tjDecompress(tjhandle j, |
| 202 | unsigned char *srcbuf, unsigned long size, |
| 203 | unsigned char *dstbuf, int width, int pitch, int height, int pixelsize, |
| 204 | int flags) |
| 205 | |
| 206 | [INPUT] j = instance handle previously returned from a call to |
| 207 | tjInitDecompress() |
| 208 | [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image |
| 209 | to decompress |
| 210 | [INPUT] size = size of the JPEG image buffer (in bytes) |
| 211 | [INPUT] dstbuf = pointer to user-allocated image buffer which will receive |
| 212 | the bitmap image. This buffer should normally be pitch*height |
| 213 | bytes in size, although this pointer may also be used to decompress into |
| 214 | a specific region of a larger buffer. |
| 215 | [INPUT] width = width (in pixels) of the destination image |
| 216 | [INPUT] pitch = bytes per line of the destination image (width*pixelsize if the |
| 217 | bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap |
| 218 | is padded to the nearest 32-bit boundary, such as is the case for Windows |
| 219 | bitmaps. You can also be clever and use this parameter to skip lines, etc., |
| 220 | as long as the pitch is greater than 0.) |
| 221 | [INPUT] height = height (in pixels) of the destination image |
| 222 | [INPUT] pixelsize = size (in bytes) of each pixel in the destination image |
DRC | 7ab9d7c | 2010-11-04 22:42:18 +0000 | [diff] [blame] | 223 | RGBA/RGBx and BGRA/BGRx: 4, RGB and BGR: 3, Grayscale: 1 |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 224 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
| 225 | "Flags" section above. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 226 | |
| 227 | RETURNS: 0 on success, -1 on error |
| 228 | */ |
| 229 | DLLEXPORT int DLLCALL tjDecompress(tjhandle j, |
| 230 | unsigned char *srcbuf, unsigned long size, |
| 231 | unsigned char *dstbuf, int width, int pitch, int height, int pixelsize, |
| 232 | int flags); |
| 233 | |
| 234 | |
| 235 | /* |
| 236 | int tjDestroy(tjhandle h) |
| 237 | |
| 238 | Frees structures associated with a compression or decompression instance |
| 239 | |
| 240 | [INPUT] h = instance handle (returned from a previous call to |
| 241 | tjInitCompress() or tjInitDecompress() |
| 242 | |
| 243 | RETURNS: 0 on success, -1 on error |
| 244 | */ |
| 245 | DLLEXPORT int DLLCALL tjDestroy(tjhandle h); |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | char *tjGetErrorStr(void) |
| 250 | |
| 251 | Returns a descriptive error message explaining why the last command failed |
| 252 | */ |
| 253 | DLLEXPORT char* DLLCALL tjGetErrorStr(void); |
| 254 | |
| 255 | #ifdef __cplusplus |
| 256 | } |
| 257 | #endif |