DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2009-2011 D. R. Commander. All Rights Reserved. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 3 | * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 6 | * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 7 | * - 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. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
DRC | 3a1bb35 | 2011-05-24 09:15:44 +0000 | [diff] [blame] | 29 | #ifndef __TURBOJPEG_H__ |
| 30 | #define __TURBOJPEG_H__ |
| 31 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 32 | #if defined(_WIN32) && defined(DLLDEFINE) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 33 | #define DLLEXPORT __declspec(dllexport) |
| 34 | #else |
| 35 | #define DLLEXPORT |
| 36 | #endif |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 37 | #define DLLCALL |
| 38 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 39 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 40 | /** |
| 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 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 47 | |
DRC | 0a32519 | 2011-03-02 09:22:41 +0000 | [diff] [blame] | 48 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 49 | /** |
| 50 | * The number of chrominance subsampling options |
| 51 | */ |
| 52 | #define TJ_NUMSAMP 5 |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 53 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Chrominance subsampling options. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 56 | * 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 63 | enum TJSAMP |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 64 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 65 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 70 | TJSAMP_444=0, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 71 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 75 | TJSAMP_422, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 76 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 80 | TJSAMP_420, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Grayscale. The JPEG or YUV image will contain no chrominance components. |
| 83 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 84 | TJSAMP_GRAY, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 85 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 89 | TJSAMP_440 |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 92 | /** |
| 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 | */ |
| 100 | static const int tjMCUWidth[TJ_NUMSAMP] = {8, 16, 16, 8, 8}; |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 101 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 102 | /** |
| 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 | */ |
| 110 | static 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 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 120 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 121 | enum TJPF |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 122 | { |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 128 | TJPF_RGB=0, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 129 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 134 | TJPF_BGR, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 135 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 140 | TJPF_RGBX, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 141 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 146 | TJPF_BGRX, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 147 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 152 | TJPF_XBGR, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 153 | /** |
| 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 158 | TJPF_XRGB, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 159 | /** |
| 160 | * Grayscale pixel format. Each 1-byte pixel represents a luminance |
| 161 | * (brightness) level from 0 to 255. |
| 162 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 163 | TJPF_GRAY |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 164 | }; |
| 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 | */ |
| 172 | static 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 | */ |
| 180 | static 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 | */ |
| 187 | static 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 | */ |
| 192 | static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1}; |
| 193 | |
| 194 | |
| 195 | /** |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 196 | * The uncompressed source/destination image is stored in bottom-up (Windows, |
| 197 | * OpenGL) order, not top-down (X11) order. |
| 198 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 199 | #define TJFLAG_BOTTOMUP 2 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 200 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 201 | * Turn off CPU auto-detection and force TurboJPEG to use MMX code (IPP and |
| 202 | * 32-bit libjpeg-turbo versions only.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 203 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 204 | #define TJFLAG_FORCEMMX 8 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 205 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 206 | * Turn off CPU auto-detection and force TurboJPEG to use SSE code (32-bit IPP |
| 207 | * and 32-bit libjpeg-turbo versions only) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 208 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 209 | #define TJFLAG_FORCESSE 16 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 210 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 211 | * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (32-bit IPP |
| 212 | * and 32-bit libjpeg-turbo versions only) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 213 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 214 | #define TJFLAG_FORCESSE2 32 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 215 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 216 | * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (64-bit IPP |
| 217 | * version only) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 218 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 219 | #define TJFLAG_FORCESSE3 128 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 220 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 221 | * Use fast, inaccurate chrominance upsampling routines in the JPEG |
| 222 | * decompressor (libjpeg and libjpeg-turbo versions only) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 223 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 224 | #define TJFLAG_FASTUPSAMPLE 256 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 225 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 226 | * 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. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 231 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 232 | #define TJFLAG_NOREALLOC 1024 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | /** |
| 236 | * Number of transform operations |
| 237 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 238 | #define TJ_NUMXOP 8 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 239 | |
| 240 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 241 | * Transform operations for #tjTransform() |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 242 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 243 | enum TJXOP |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 244 | { |
| 245 | /** |
| 246 | * Do not transform the position of the image pixels |
| 247 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 248 | TJXOP_NONE=0, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 249 | /** |
| 250 | * Flip (mirror) image horizontally. This transform is imperfect if there |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 251 | * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 252 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 253 | TJXOP_HFLIP, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 254 | /** |
| 255 | * Flip (mirror) image vertically. This transform is imperfect if there are |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 256 | * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 257 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 258 | TJXOP_VFLIP, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 259 | /** |
| 260 | * Transpose image (flip/mirror along upper left to lower right axis.) This |
| 261 | * transform is always perfect. |
| 262 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 263 | TJXOP_TRANSPOSE, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 264 | /** |
| 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 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 267 | * the image (see #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 268 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 269 | TJXOP_TRANSVERSE, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 270 | /** |
| 271 | * Rotate image clockwise by 90 degrees. This transform is imperfect if |
| 272 | * there are any partial MCU blocks on the bottom edge (see |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 273 | * #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 274 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 275 | TJXOP_ROT90, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 276 | /** |
| 277 | * Rotate image 180 degrees. This transform is imperfect if there are any |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 278 | * partial MCU blocks in the image (see #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 279 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 280 | TJXOP_ROT180, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 281 | /** |
| 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 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 284 | * #TJXOPT_PERFECT.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 285 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 286 | TJXOP_ROT270 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | |
| 290 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 291 | * This option will cause #tjTransform() to return an error if the transform is |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 292 | * 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 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 302 | #define TJXOPT_PERFECT 1 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 303 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 304 | * This option will cause #tjTransform() to discard any partial MCU blocks that |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 305 | * cannot be transformed. |
| 306 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 307 | #define TJXOPT_TRIM 2 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 308 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 309 | * This option will enable lossless cropping. See #tjTransform() for more |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 310 | * information. |
| 311 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 312 | #define TJXOPT_CROP 4 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 313 | /** |
| 314 | * This option will discard the color data in the input image and produce |
| 315 | * a grayscale output image. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 316 | */ |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 317 | #define TJXOPT_GRAY 8 |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 318 | /** |
| 319 | * This option will prevent #tjTransform() from outputting a JPEG image for |
| 320 | * this particular transform (this can be used in conjunction with a custom |
| 321 | * filter to capture the transformed DCT coefficients without transcoding |
| 322 | * them.) |
| 323 | */ |
| 324 | #define TJXOPT_NOOUTPUT 16 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | /** |
| 328 | * Scaling factor |
| 329 | */ |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 330 | typedef struct |
| 331 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 332 | /** |
| 333 | * Numerator |
| 334 | */ |
| 335 | int num; |
| 336 | /** |
| 337 | * Denominator |
| 338 | */ |
| 339 | int denom; |
| 340 | } tjscalingfactor; |
| 341 | |
| 342 | /** |
| 343 | * Cropping region |
| 344 | */ |
| 345 | typedef struct |
| 346 | { |
| 347 | /** |
| 348 | * The left boundary of the cropping region. This must be evenly divisible |
| 349 | * by the MCU block width (see #tjMCUWidth.) |
| 350 | */ |
| 351 | int x; |
| 352 | /** |
| 353 | * The upper boundary of the cropping region. This must be evenly divisible |
| 354 | * by the MCU block height (see #tjMCUHeight.) |
| 355 | */ |
| 356 | int y; |
| 357 | /** |
| 358 | * The width of the cropping region. Setting this to 0 is the equivalent of |
| 359 | * setting it to the width of the source JPEG image - x. |
| 360 | */ |
| 361 | int w; |
| 362 | /** |
| 363 | * The height of the cropping region. Setting this to 0 is the equivalent of |
| 364 | * setting it to the height of the source JPEG image - y. |
| 365 | */ |
| 366 | int h; |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 367 | } tjregion; |
| 368 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 369 | /** |
| 370 | * Lossless transform |
| 371 | */ |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 372 | typedef struct tjtransform |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 373 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 374 | /** |
| 375 | * Cropping region |
| 376 | */ |
| 377 | tjregion r; |
| 378 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 379 | * One of the @ref TJXOP "transform operations" |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 380 | */ |
| 381 | int op; |
| 382 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 383 | * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options" |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 384 | */ |
| 385 | int options; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 386 | /** |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 387 | * Arbitrary data that can be accessed within the body of the callback |
| 388 | * function |
| 389 | */ |
| 390 | void *data; |
| 391 | /** |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 392 | * A callback function that can be used to modify the DCT coefficients |
| 393 | * after they are losslessly transformed but before they are transcoded to a |
| 394 | * new JPEG file. This allows for custom filters or other transformations to |
| 395 | * be applied in the frequency domain. |
| 396 | * |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 397 | * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE: |
| 398 | * this pointer is not guaranteed to be valid once the callback |
| 399 | * returns, so applications wishing to hand off the DCT coefficients |
| 400 | * to another function or library should make a copy of them within |
| 401 | * the body of the callback.) |
| 402 | * @param arrayRegion #tjregion structure containing the width and height of |
DRC | f69dc28 | 2011-09-20 18:20:43 +0000 | [diff] [blame] | 403 | * the array pointed to by <tt>coeffs</tt> as well as its offset |
| 404 | * relative to the component plane. TurboJPEG implementations may |
| 405 | * choose to split each component plane into multiple DCT coefficient |
| 406 | * arrays and call the callback function once for each array. |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 407 | * @param planeRegion #tjregion structure containing the width and height of |
| 408 | * the component plane to which <tt>coeffs</tt> belongs |
| 409 | * @param componentID ID number of the component plane to which |
| 410 | * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of |
| 411 | * 0, 1, and 2 in typical JPEG images.) |
| 412 | * @param transformID ID number of the transformed image to which |
| 413 | * <tt>coeffs</tt> belongs. This is the same as the index of the |
| 414 | * transform in the transforms array that was passed to |
| 415 | * #tjTransform(). |
| 416 | * @param transform a pointer to a #tjtransform structure that specifies the |
| 417 | * parameters and/or cropping region for this transform |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 418 | * |
| 419 | * @return 0 if the callback was successful, or -1 if an error occurred. |
| 420 | */ |
| 421 | int (*customFilter)(short *coeffs, tjregion arrayRegion, |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 422 | tjregion planeRegion, int componentIndex, int transformIndex, |
| 423 | struct tjtransform *transform); |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 424 | } tjtransform; |
| 425 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 426 | /** |
| 427 | * TurboJPEG instance handle |
| 428 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 429 | typedef void* tjhandle; |
| 430 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 431 | |
| 432 | /** |
| 433 | * Pad the given width to the nearest 32-bit boundary |
| 434 | */ |
| 435 | #define TJPAD(width) (((width)+3)&(~3)) |
| 436 | |
| 437 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 438 | * Compute the scaled value of <tt>dimension</tt> using the given scaling |
| 439 | * factor. This macro performs the integer equivalent of <tt>ceil(dimension * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 440 | * scalingFactor)</tt>. |
| 441 | */ |
| 442 | #define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \ |
| 443 | + scalingFactor.denom - 1) / scalingFactor.denom) |
| 444 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 445 | |
| 446 | #ifdef __cplusplus |
| 447 | extern "C" { |
| 448 | #endif |
| 449 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 450 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 451 | /** |
| 452 | * Create a TurboJPEG compressor instance. |
| 453 | * |
| 454 | * @return a handle to the newly-created instance, or NULL if an error |
| 455 | * occurred (see #tjGetErrorStr().) |
| 456 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 457 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void); |
| 458 | |
| 459 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 460 | /** |
| 461 | * Compress an RGB or grayscale image into a JPEG image. |
| 462 | * |
| 463 | * @param handle a handle to a TurboJPEG compressor or transformer instance |
| 464 | * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels |
| 465 | * to be compressed |
| 466 | * @param width width (in pixels) of the source image |
| 467 | * @param pitch bytes per line of the source image. Normally, this should be |
| 468 | * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, |
| 469 | * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of |
| 470 | * the image is padded to the nearest 32-bit boundary, as is the case |
| 471 | * for Windows bitmaps. You can also be clever and use this parameter |
| 472 | * to skip lines, etc. Setting this parameter to 0 is the equivalent of |
| 473 | * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. |
| 474 | * @param height height (in pixels) of the source image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 475 | * @param pixelFormat pixel format of the source image (see @ref TJPF |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 476 | * "Pixel formats".) |
| 477 | * @param jpegBuf address of a pointer to an image buffer that will receive the |
| 478 | * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer |
| 479 | * to accommodate the size of the JPEG image. Thus, you can choose to: |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 480 | * -# pre-allocate the JPEG buffer with an arbitrary size using |
| 481 | * #tjAlloc() and let TurboJPEG grow the buffer as needed, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 482 | * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the |
| 483 | * buffer for you, or |
| 484 | * -# pre-allocate the buffer to a "worst case" size determined by |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 485 | * calling #tjBufSize(). This should ensure that the buffer never has |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 486 | * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 487 | * . |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 488 | * If you choose option 1, <tt>*jpegSize</tt> should be set to the |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 489 | * size of your pre-allocated buffer. In any case, unless you have |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 490 | * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon |
| 491 | * return from this function, as it may have changed. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 492 | * @param jpegSize pointer to an unsigned long variable which holds the size of |
| 493 | * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a |
| 494 | * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the |
| 495 | * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the |
| 496 | * size of the JPEG image (in bytes.) |
| 497 | * @param jpegSubsamp the level of chrominance subsampling to be used when |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 498 | * generating the JPEG image (see @ref TJSAMP |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 499 | * "Chrominance subsampling options".) |
| 500 | * @param jpegQual the image quality of the generated JPEG image (1 = worst, |
| 501 | 100 = best) |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 502 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 503 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 504 | * |
| 505 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 506 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 507 | DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, |
| 508 | int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf, |
| 509 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 510 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 511 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 512 | /** |
| 513 | * The maximum size of the buffer (in bytes) required to hold a JPEG image with |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 514 | * the given parameters. The number of bytes returned by this function is |
| 515 | * larger than the size of the uncompressed source image. The reason for this |
| 516 | * is that the JPEG format uses 16-bit coefficients, and it is thus possible |
| 517 | * for a very high-quality JPEG image with very high frequency content to |
| 518 | * expand rather than compress when converted to the JPEG format. Such images |
| 519 | * represent a very rare corner case, but since there is no way to predict the |
| 520 | * size of a JPEG image prior to compression, the corner case has to be |
| 521 | * handled. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 522 | * |
| 523 | * @param width width of the image (in pixels) |
| 524 | * @param height height of the image (in pixels) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 525 | * @param jpegSubsamp the level of chrominance subsampling to be used when |
| 526 | * generating the JPEG image (see @ref TJSAMP |
| 527 | * "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 528 | * |
| 529 | * @return the maximum size of the buffer (in bytes) required to hold the |
| 530 | * image, or -1 if the arguments are out of bounds. |
| 531 | */ |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 532 | DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height, |
| 533 | int jpegSubsamp); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 534 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 535 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 536 | /** |
| 537 | * The size of the buffer (in bytes) required to hold a YUV planar image with |
| 538 | * the given parameters. |
| 539 | * |
| 540 | * @param width width of the image (in pixels) |
| 541 | * @param height height of the image (in pixels) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 542 | * @param subsamp level of chrominance subsampling in the image (see |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 543 | * @ref TJSAMP "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 544 | * |
| 545 | * @return the size of the buffer (in bytes) required to hold the image, or |
| 546 | * -1 if the arguments are out of bounds. |
| 547 | */ |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 548 | DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height, |
| 549 | int subsamp); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 550 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 551 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 552 | /** |
| 553 | * Encode an RGB or grayscale image into a YUV planar image. This function |
| 554 | * uses the accelerated color conversion routines in TurboJPEG's underlying |
| 555 | * codec to produce a planar YUV image that is suitable for X Video. |
| 556 | * Specifically, if the chrominance components are subsampled along the |
| 557 | * horizontal dimension, then the width of the luminance plane is padded to 2 |
| 558 | * in the output image (same goes for the height of the luminance plane, if the |
| 559 | * chrominance components are subsampled along the vertical dimension.) Also, |
| 560 | * each line of each plane in the output image is padded to 4 bytes. Although |
| 561 | * this will work with any subsampling option, it is really only useful in |
| 562 | * combination with TJ_420, which produces an image compatible with the I420 |
| 563 | * (AKA "YUV420P") format. |
| 564 | * |
| 565 | * @param handle a handle to a TurboJPEG compressor or transformer instance |
| 566 | * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels |
| 567 | * to be encoded |
| 568 | * @param width width (in pixels) of the source image |
| 569 | * @param pitch bytes per line of the source image. Normally, this should be |
| 570 | * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, |
| 571 | * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of |
| 572 | * the image is padded to the nearest 32-bit boundary, as is the case |
| 573 | * for Windows bitmaps. You can also be clever and use this parameter |
| 574 | * to skip lines, etc. Setting this parameter to 0 is the equivalent of |
| 575 | * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. |
| 576 | * @param height height (in pixels) of the source image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 577 | * @param pixelFormat pixel format of the source image (see @ref TJPF |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 578 | * "Pixel formats".) |
| 579 | * @param dstBuf pointer to an image buffer which will receive the YUV image. |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 580 | * Use #tjBufSizeYUV() to determine the appropriate size for this buffer |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 581 | * based on the image width, height, and level of chrominance |
| 582 | * subsampling. |
| 583 | * @param subsamp the level of chrominance subsampling to be used when |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 584 | * generating the YUV image (see @ref TJSAMP |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 585 | * "Chrominance subsampling options".) |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 586 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 587 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 588 | * |
| 589 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 590 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 591 | DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, |
| 592 | unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat, |
| 593 | unsigned char *dstBuf, int subsamp, int flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 594 | |
| 595 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 596 | /** |
| 597 | * Create a TurboJPEG decompressor instance. |
| 598 | * |
| 599 | * @return a handle to the newly-created instance, or NULL if an error |
| 600 | * occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 601 | */ |
| 602 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void); |
| 603 | |
| 604 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 605 | /** |
| 606 | * Retrieve information about a JPEG image without decompressing it. |
| 607 | * |
| 608 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 609 | * @param jpegBuf pointer to a buffer containing a JPEG image |
| 610 | * @param jpegSize size of the JPEG image (in bytes) |
| 611 | * @param width pointer to an integer variable which will receive the width (in |
| 612 | * pixels) of the JPEG image |
| 613 | * @param height pointer to an integer variable which will receive the height |
| 614 | * (in pixels) of the JPEG image |
| 615 | * @param jpegSubsamp pointer to an integer variable which will receive the |
| 616 | * level of chrominance subsampling used when compressing the JPEG image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 617 | * (see @ref TJSAMP "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 618 | * |
| 619 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 620 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 621 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle, |
| 622 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height, |
| 623 | int *jpegSubsamp); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 624 | |
| 625 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 626 | /** |
| 627 | * Returns a list of fractional scaling factors that the JPEG decompressor in |
| 628 | * this implementation of TurboJPEG supports. |
| 629 | * |
| 630 | * @param numscalingfactors pointer to an integer variable that will receive |
| 631 | * the number of elements in the list |
| 632 | * |
| 633 | * @return a pointer to a list of fractional scaling factors, or NULL if an |
| 634 | * error is encountered (see #tjGetErrorStr().) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 635 | */ |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 636 | DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors); |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 637 | |
| 638 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 639 | /** |
| 640 | * Decompress a JPEG image to an RGB or grayscale image. |
| 641 | * |
| 642 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 643 | * @param jpegBuf pointer to a buffer containing the JPEG image to decompress |
| 644 | * @param jpegSize size of the JPEG image (in bytes) |
| 645 | * @param dstBuf pointer to an image buffer which will receive the decompressed |
| 646 | * image. This buffer should normally be <tt>pitch * scaledHeight</tt> |
| 647 | * bytes in size, where <tt>scaledHeight</tt> can be determined by |
| 648 | * calling #TJSCALED() with the JPEG image height and one of the scaling |
| 649 | * factors returned by #tjGetScalingFactors(). The dstBuf pointer may |
| 650 | * also be used to decompress into a specific region of a larger buffer. |
| 651 | * @param width desired width (in pixels) of the destination image. If this is |
| 652 | * smaller than the width of the JPEG image being decompressed, then |
| 653 | * TurboJPEG will use scaling in the JPEG decompressor to generate the |
| 654 | * largest possible image that will fit within the desired width. If |
| 655 | * width is set to 0, then only the height will be considered when |
| 656 | * determining the scaled image size. |
| 657 | * @param pitch bytes per line of the destination image. Normally, this is |
| 658 | * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed |
| 659 | * image is unpadded, else <tt>#TJPAD(scaledWidth * |
| 660 | * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed |
| 661 | * image is padded to the nearest 32-bit boundary, as is the case for |
| 662 | * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by |
| 663 | * calling #TJSCALED() with the JPEG image width and one of the scaling |
| 664 | * factors returned by #tjGetScalingFactors().) You can also be clever |
| 665 | * and use the pitch parameter to skip lines, etc. Setting this |
| 666 | * parameter to 0 is the equivalent of setting it to <tt>scaledWidth |
| 667 | * * #tjPixelSize[pixelFormat]</tt>. |
| 668 | * @param height desired height (in pixels) of the destination image. If this |
| 669 | * is smaller than the height of the JPEG image being decompressed, then |
| 670 | * TurboJPEG will use scaling in the JPEG decompressor to generate the |
| 671 | * largest possible image that will fit within the desired height. If |
| 672 | * height is set to 0, then only the width will be considered when |
| 673 | * determining the scaled image size. |
| 674 | * @param pixelFormat pixel format of the destination image (see @ref |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 675 | * TJPF "Pixel formats".) |
| 676 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 677 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 678 | * |
| 679 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 680 | */ |
| 681 | DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, |
| 682 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 683 | int width, int pitch, int height, int pixelFormat, int flags); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 684 | |
| 685 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 686 | /** |
| 687 | * Decompress a JPEG image to a YUV planar image. This function performs JPEG |
| 688 | * decompression but leaves out the color conversion step, so a planar YUV |
| 689 | * image is generated instead of an RGB image. The padding of the planes in |
| 690 | * this image is the same as the images generated by #tjEncodeYUV2(). Note |
| 691 | * that, if the width or height of the image is not an even multiple of the MCU |
| 692 | * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer |
| 693 | * copy will be performed within TurboJPEG. |
| 694 | * |
| 695 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 696 | * @param jpegBuf pointer to a buffer containing the JPEG image to decompress |
| 697 | * @param jpegSize size of the JPEG image (in bytes) |
| 698 | * @param dstBuf pointer to an image buffer which will receive the YUV image. |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 699 | * Use #tjBufSizeYUV to determine the appropriate size for this buffer |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 700 | * based on the image width, height, and level of subsampling. |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 701 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 702 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 703 | * |
| 704 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 705 | */ |
| 706 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle, |
| 707 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 708 | int flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 709 | |
| 710 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 711 | /** |
| 712 | * Create a new TurboJPEG transformer instance. |
| 713 | * |
| 714 | * @return a handle to the newly-created instance, or NULL if an error |
| 715 | * occurred (see #tjGetErrorStr().) |
| 716 | */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 717 | DLLEXPORT tjhandle DLLCALL tjInitTransform(void); |
| 718 | |
| 719 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 720 | /** |
| 721 | * Losslessly transform a JPEG image into another JPEG image. Lossless |
| 722 | * transforms work by moving the raw coefficients from one JPEG image structure |
| 723 | * to another without altering the values of the coefficients. While this is |
| 724 | * typically faster than decompressing the image, transforming it, and |
| 725 | * re-compressing it, lossless transforms are not free. Each lossless |
| 726 | * transform requires reading and Huffman decoding all of the coefficients in |
| 727 | * the source image, regardless of the size of the destination image. Thus, |
| 728 | * this function provides a means of generating multiple transformed images |
| 729 | * from the same source or of applying multiple transformations simultaneously, |
| 730 | * in order to eliminate the need to read the source coefficients multiple |
| 731 | * times. |
| 732 | * |
| 733 | * @param handle a handle to a TurboJPEG transformer instance |
| 734 | * @param jpegBuf pointer to a buffer containing the JPEG image to transform |
| 735 | * @param jpegSize size of the JPEG image (in bytes) |
| 736 | * @param n the number of transformed JPEG images to generate |
| 737 | * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt> |
| 738 | * will receive a JPEG image that has been transformed using the |
| 739 | * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to |
| 740 | * reallocate the JPEG buffer to accommodate the size of the JPEG image. |
| 741 | * Thus, you can choose to: |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 742 | * -# pre-allocate the JPEG buffer with an arbitrary size using |
| 743 | * #tjAlloc() and let TurboJPEG grow the buffer as needed, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 744 | * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the |
| 745 | * buffer for you, or |
| 746 | * -# pre-allocate the buffer to a "worst case" size determined by |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 747 | * calling #tjBufSize() with the cropped width and height. This should |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 748 | * ensure that the buffer never has to be re-allocated (setting |
| 749 | * #TJFLAG_NOREALLOC guarantees this.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 750 | * . |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 751 | * If you choose option 1, <tt>dstSizes[i]</tt> should be set to |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 752 | * the size of your pre-allocated buffer. In any case, unless you have |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 753 | * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> |
| 754 | * upon return from this function, as it may have changed. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 755 | * @param dstSizes pointer to an array of n unsigned long variables which will |
| 756 | * receive the actual sizes (in bytes) of each transformed JPEG image. |
| 757 | * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then |
| 758 | * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon |
| 759 | * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image |
| 760 | * (in bytes.) |
| 761 | * @param transforms pointer to an array of n tjtransform structures, each of |
| 762 | * which specifies the transform parameters and/or cropping region for |
| 763 | * the corresponding transformed output image. |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 764 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 765 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 766 | * |
| 767 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 768 | */ |
| 769 | DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf, |
| 770 | unsigned long jpegSize, int n, unsigned char **dstBufs, |
| 771 | unsigned long *dstSizes, tjtransform *transforms, int flags); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 772 | |
| 773 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 774 | /** |
| 775 | * Destroy a TurboJPEG compressor, decompressor, or transformer instance. |
| 776 | * |
| 777 | * @param handle a handle to a TurboJPEG compressor, decompressor or |
| 778 | * transformer instance |
| 779 | * |
| 780 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 781 | */ |
| 782 | DLLEXPORT int DLLCALL tjDestroy(tjhandle handle); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 783 | |
| 784 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 785 | /** |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 786 | * Allocate an image buffer for use with TurboJPEG. You should always use |
| 787 | * this function to allocate the JPEG destination buffer(s) for #tjCompress2() |
| 788 | * and #tjTransform() unless you are disabling automatic buffer |
| 789 | * (re)allocation (by setting #TJFLAG_NOREALLOC.) |
| 790 | * |
| 791 | * @param bytes the number of bytes to allocate |
| 792 | * |
| 793 | * @return a pointer to a newly-allocated buffer with the specified number of |
| 794 | * bytes |
DRC | d441107 | 2011-05-24 17:00:15 +0000 | [diff] [blame] | 795 | * |
| 796 | * @sa tjFree() |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 797 | */ |
| 798 | DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes); |
| 799 | |
| 800 | |
| 801 | /** |
| 802 | * Free an image buffer previously allocated by TurboJPEG. You should always |
| 803 | * use this function to free JPEG destination buffer(s) that were automatically |
| 804 | * (re)allocated by #tjCompress2() or #tjTransform() or that were manually |
| 805 | * allocated using #tjAlloc(). |
| 806 | * |
| 807 | * @param buffer address of the buffer to free |
DRC | d441107 | 2011-05-24 17:00:15 +0000 | [diff] [blame] | 808 | * |
| 809 | * @sa tjAlloc() |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 810 | */ |
| 811 | DLLEXPORT void DLLCALL tjFree(unsigned char *buffer); |
| 812 | |
| 813 | |
| 814 | /** |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 815 | * Returns a descriptive error message explaining why the last command failed. |
| 816 | * |
| 817 | * @return a descriptive error message explaining why the last command failed. |
| 818 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 819 | DLLEXPORT char* DLLCALL tjGetErrorStr(void); |
| 820 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 821 | |
| 822 | /* Backward compatibility functions and macros (nothing to see here) */ |
| 823 | #define NUMSUBOPT TJ_NUMSAMP |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 824 | #define TJ_444 TJSAMP_444 |
| 825 | #define TJ_422 TJSAMP_422 |
| 826 | #define TJ_420 TJSAMP_420 |
| 827 | #define TJ_411 TJSAMP_420 |
| 828 | #define TJ_GRAYSCALE TJSAMP_GRAY |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 829 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 830 | #define TJ_BGR 1 |
| 831 | #define TJ_BOTTOMUP TJFLAG_BOTTOMUP |
| 832 | #define TJ_FORCEMMX TJFLAG_FORCEMMX |
| 833 | #define TJ_FORCESSE TJFLAG_FORCESSE |
| 834 | #define TJ_FORCESSE2 TJFLAG_FORCESSE2 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 835 | #define TJ_ALPHAFIRST 64 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 836 | #define TJ_FORCESSE3 TJFLAG_FORCESSE3 |
| 837 | #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 838 | #define TJ_YUV 512 |
| 839 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 840 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height); |
| 841 | |
| 842 | DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height, |
| 843 | int jpegSubsamp); |
| 844 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 845 | DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf, |
| 846 | int width, int pitch, int height, int pixelSize, unsigned char *dstBuf, |
| 847 | unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags); |
| 848 | |
| 849 | DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, |
| 850 | unsigned char *srcBuf, int width, int pitch, int height, int pixelSize, |
| 851 | unsigned char *dstBuf, int subsamp, int flags); |
| 852 | |
| 853 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle, |
| 854 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height); |
| 855 | |
| 856 | DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, |
| 857 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 858 | int width, int pitch, int height, int pixelSize, int flags); |
| 859 | |
| 860 | |
| 861 | /** |
| 862 | * @} |
| 863 | */ |
| 864 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 865 | #ifdef __cplusplus |
| 866 | } |
| 867 | #endif |
DRC | 3a1bb35 | 2011-05-24 09:15:44 +0000 | [diff] [blame] | 868 | |
| 869 | #endif |