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 | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 56 | typedef void* tjhandle; |
| 57 | |
| 58 | #define TJPAD(p) (((p)+3)&(~3)) |
| 59 | #ifndef max |
| 60 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 61 | #endif |
| 62 | |
| 63 | #ifdef __cplusplus |
| 64 | extern "C" { |
| 65 | #endif |
| 66 | |
| 67 | /* API follows */ |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | tjhandle tjInitCompress(void) |
| 72 | |
| 73 | Creates a new JPEG compressor instance, allocates memory for the structures, |
| 74 | and returns a handle to the instance. Most applications will only |
| 75 | need to call this once at the beginning of the program or once for each |
| 76 | concurrent thread. Don't try to create a new instance every time you |
| 77 | compress an image, because this will cause performance to suffer. |
| 78 | |
| 79 | RETURNS: NULL on error |
| 80 | */ |
| 81 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void); |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | int tjCompress(tjhandle j, |
| 86 | unsigned char *srcbuf, int width, int pitch, int height, int pixelsize, |
| 87 | unsigned char *dstbuf, unsigned long *size, |
| 88 | int jpegsubsamp, int jpegqual, int flags) |
| 89 | |
| 90 | [INPUT] j = instance handle previously returned from a call to |
| 91 | tjInitCompress() |
| 92 | [INPUT] srcbuf = pointer to user-allocated image buffer containing pixels in |
| 93 | RGB(A) or BGR(A) form |
| 94 | [INPUT] width = width (in pixels) of the source image |
| 95 | [INPUT] pitch = bytes per line of the source image (width*pixelsize if the |
| 96 | bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap |
| 97 | is padded to the nearest 32-bit boundary, such as is the case for Windows |
| 98 | bitmaps. You can also be clever and use this parameter to skip lines, etc., |
| 99 | as long as the pitch is greater than 0.) |
| 100 | [INPUT] height = height (in pixels) of the source image |
| 101 | [INPUT] pixelsize = size (in bytes) of each pixel in the source image |
| 102 | RGBA and BGRA: 4, RGB and BGR: 3 |
| 103 | [INPUT] dstbuf = pointer to user-allocated image buffer which will receive |
| 104 | the JPEG image. Use the macro TJBUFSIZE(width, height) to determine |
| 105 | the appropriate size for this buffer based on the image width and height. |
| 106 | [OUTPUT] size = pointer to unsigned long which receives the size (in bytes) |
| 107 | of the compressed image |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 108 | [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] | 109 | When the image is converted from the RGB to YCbCr colorspace as part of the |
| 110 | JPEG compression process, every other Cb and Cr (chrominance) pixel can be |
| 111 | discarded to produce a smaller image with little perceptible loss of |
| 112 | image clarity (the human eye is more sensitive to small changes in |
| 113 | brightness than small changes in color.) |
| 114 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 115 | 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] | 116 | horizontal and vertical directions. |
| 117 | TJ_422: 4:2:2 subsampling. Discards every other Cb, Cr pixel only in |
| 118 | the horizontal direction. |
| 119 | TJ_444: no subsampling. |
| 120 | TJ_GRAYSCALE: Generate grayscale JPEG image |
| 121 | |
| 122 | [INPUT] jpegqual = JPEG quality (an integer between 0 and 100 inclusive.) |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 123 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
| 124 | "Flags" section above. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 125 | |
| 126 | RETURNS: 0 on success, -1 on error |
| 127 | */ |
| 128 | DLLEXPORT int DLLCALL tjCompress(tjhandle j, |
| 129 | unsigned char *srcbuf, int width, int pitch, int height, int pixelsize, |
| 130 | unsigned char *dstbuf, unsigned long *size, |
| 131 | int jpegsubsamp, int jpegqual, int flags); |
| 132 | |
| 133 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height); |
| 134 | |
| 135 | /* |
| 136 | tjhandle tjInitDecompress(void) |
| 137 | |
| 138 | Creates a new JPEG decompressor instance, allocates memory for the |
| 139 | structures, and returns a handle to the instance. Most applications will |
| 140 | only need to call this once at the beginning of the program or once for each |
| 141 | concurrent thread. Don't try to create a new instance every time you |
| 142 | decompress an image, because this will cause performance to suffer. |
| 143 | |
| 144 | RETURNS: NULL on error |
| 145 | */ |
| 146 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void); |
| 147 | |
| 148 | |
| 149 | /* |
| 150 | int tjDecompressHeader(tjhandle j, |
| 151 | unsigned char *srcbuf, unsigned long size, |
| 152 | int *width, int *height) |
| 153 | |
| 154 | [INPUT] j = instance handle previously returned from a call to |
| 155 | tjInitDecompress() |
| 156 | [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image |
| 157 | to decompress |
| 158 | [INPUT] size = size of the JPEG image buffer (in bytes) |
| 159 | [OUTPUT] width = width (in pixels) of the JPEG image |
| 160 | [OUTPUT] height = height (in pixels) of the JPEG image |
| 161 | |
| 162 | RETURNS: 0 on success, -1 on error |
| 163 | */ |
| 164 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle j, |
| 165 | unsigned char *srcbuf, unsigned long size, |
| 166 | int *width, int *height); |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | int tjDecompress(tjhandle j, |
| 171 | unsigned char *srcbuf, unsigned long size, |
| 172 | unsigned char *dstbuf, int width, int pitch, int height, int pixelsize, |
| 173 | int flags) |
| 174 | |
| 175 | [INPUT] j = instance handle previously returned from a call to |
| 176 | tjInitDecompress() |
| 177 | [INPUT] srcbuf = pointer to a user-allocated buffer containing the JPEG image |
| 178 | to decompress |
| 179 | [INPUT] size = size of the JPEG image buffer (in bytes) |
| 180 | [INPUT] dstbuf = pointer to user-allocated image buffer which will receive |
| 181 | the bitmap image. This buffer should normally be pitch*height |
| 182 | bytes in size, although this pointer may also be used to decompress into |
| 183 | a specific region of a larger buffer. |
| 184 | [INPUT] width = width (in pixels) of the destination image |
| 185 | [INPUT] pitch = bytes per line of the destination image (width*pixelsize if the |
| 186 | bitmap is unpadded, else TJPAD(width*pixelsize) if each line of the bitmap |
| 187 | is padded to the nearest 32-bit boundary, such as is the case for Windows |
| 188 | bitmaps. You can also be clever and use this parameter to skip lines, etc., |
| 189 | as long as the pitch is greater than 0.) |
| 190 | [INPUT] height = height (in pixels) of the destination image |
| 191 | [INPUT] pixelsize = size (in bytes) of each pixel in the destination image |
| 192 | RGBA/RGBx and BGRA/BGRx: 4, RGB and BGR: 3 |
DRC | 8f0d62c | 2010-02-22 08:35:19 +0000 | [diff] [blame] | 193 | [INPUT] flags = the bitwise OR of one or more of the flags described in the |
| 194 | "Flags" section above. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 195 | |
| 196 | RETURNS: 0 on success, -1 on error |
| 197 | */ |
| 198 | DLLEXPORT int DLLCALL tjDecompress(tjhandle j, |
| 199 | unsigned char *srcbuf, unsigned long size, |
| 200 | unsigned char *dstbuf, int width, int pitch, int height, int pixelsize, |
| 201 | int flags); |
| 202 | |
| 203 | |
| 204 | /* |
| 205 | int tjDestroy(tjhandle h) |
| 206 | |
| 207 | Frees structures associated with a compression or decompression instance |
| 208 | |
| 209 | [INPUT] h = instance handle (returned from a previous call to |
| 210 | tjInitCompress() or tjInitDecompress() |
| 211 | |
| 212 | RETURNS: 0 on success, -1 on error |
| 213 | */ |
| 214 | DLLEXPORT int DLLCALL tjDestroy(tjhandle h); |
| 215 | |
| 216 | |
| 217 | /* |
| 218 | char *tjGetErrorStr(void) |
| 219 | |
| 220 | Returns a descriptive error message explaining why the last command failed |
| 221 | */ |
| 222 | DLLEXPORT char* DLLCALL tjGetErrorStr(void); |
| 223 | |
| 224 | #ifdef __cplusplus |
| 225 | } |
| 226 | #endif |