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 | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 318 | |
| 319 | |
| 320 | /** |
| 321 | * Scaling factor |
| 322 | */ |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 323 | typedef struct |
| 324 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 325 | /** |
| 326 | * Numerator |
| 327 | */ |
| 328 | int num; |
| 329 | /** |
| 330 | * Denominator |
| 331 | */ |
| 332 | int denom; |
| 333 | } tjscalingfactor; |
| 334 | |
| 335 | /** |
| 336 | * Cropping region |
| 337 | */ |
| 338 | typedef struct |
| 339 | { |
| 340 | /** |
| 341 | * The left boundary of the cropping region. This must be evenly divisible |
| 342 | * by the MCU block width (see #tjMCUWidth.) |
| 343 | */ |
| 344 | int x; |
| 345 | /** |
| 346 | * The upper boundary of the cropping region. This must be evenly divisible |
| 347 | * by the MCU block height (see #tjMCUHeight.) |
| 348 | */ |
| 349 | int y; |
| 350 | /** |
| 351 | * The width of the cropping region. Setting this to 0 is the equivalent of |
| 352 | * setting it to the width of the source JPEG image - x. |
| 353 | */ |
| 354 | int w; |
| 355 | /** |
| 356 | * The height of the cropping region. Setting this to 0 is the equivalent of |
| 357 | * setting it to the height of the source JPEG image - y. |
| 358 | */ |
| 359 | int h; |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 360 | } tjregion; |
| 361 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 362 | /** |
| 363 | * Lossless transform |
| 364 | */ |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 365 | typedef struct |
| 366 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 367 | /** |
| 368 | * Cropping region |
| 369 | */ |
| 370 | tjregion r; |
| 371 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 372 | * One of the @ref TJXOP "transform operations" |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 373 | */ |
| 374 | int op; |
| 375 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 376 | * 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] | 377 | */ |
| 378 | int options; |
DRC | 0a07969 | 2011-03-02 09:27:49 +0000 | [diff] [blame] | 379 | } tjtransform; |
| 380 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 381 | /** |
| 382 | * TurboJPEG instance handle |
| 383 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 384 | typedef void* tjhandle; |
| 385 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 386 | |
| 387 | /** |
| 388 | * Pad the given width to the nearest 32-bit boundary |
| 389 | */ |
| 390 | #define TJPAD(width) (((width)+3)&(~3)) |
| 391 | |
| 392 | /** |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 393 | * Compute the scaled value of <tt>dimension</tt> using the given scaling |
| 394 | * factor. This macro performs the integer equivalent of <tt>ceil(dimension * |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 395 | * scalingFactor)</tt>. |
| 396 | */ |
| 397 | #define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \ |
| 398 | + scalingFactor.denom - 1) / scalingFactor.denom) |
| 399 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 400 | |
| 401 | #ifdef __cplusplus |
| 402 | extern "C" { |
| 403 | #endif |
| 404 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 405 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 406 | /** |
| 407 | * Create a TurboJPEG compressor instance. |
| 408 | * |
| 409 | * @return a handle to the newly-created instance, or NULL if an error |
| 410 | * occurred (see #tjGetErrorStr().) |
| 411 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 412 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void); |
| 413 | |
| 414 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 415 | /** |
| 416 | * Compress an RGB or grayscale image into a JPEG image. |
| 417 | * |
| 418 | * @param handle a handle to a TurboJPEG compressor or transformer instance |
| 419 | * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels |
| 420 | * to be compressed |
| 421 | * @param width width (in pixels) of the source image |
| 422 | * @param pitch bytes per line of the source image. Normally, this should be |
| 423 | * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, |
| 424 | * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of |
| 425 | * the image is padded to the nearest 32-bit boundary, as is the case |
| 426 | * for Windows bitmaps. You can also be clever and use this parameter |
| 427 | * to skip lines, etc. Setting this parameter to 0 is the equivalent of |
| 428 | * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. |
| 429 | * @param height height (in pixels) of the source image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 430 | * @param pixelFormat pixel format of the source image (see @ref TJPF |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 431 | * "Pixel formats".) |
| 432 | * @param jpegBuf address of a pointer to an image buffer that will receive the |
| 433 | * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer |
| 434 | * to accommodate the size of the JPEG image. Thus, you can choose to: |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 435 | * -# pre-allocate the JPEG buffer with an arbitrary size using |
| 436 | * #tjAlloc() and let TurboJPEG grow the buffer as needed, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 437 | * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the |
| 438 | * buffer for you, or |
| 439 | * -# pre-allocate the buffer to a "worst case" size determined by |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 440 | * calling #tjBufSize(). This should ensure that the buffer never has |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 441 | * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 442 | * . |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 443 | * If you choose option 1, <tt>*jpegSize</tt> should be set to the |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 444 | * size of your pre-allocated buffer. In any case, unless you have |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 445 | * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon |
| 446 | * return from this function, as it may have changed. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 447 | * @param jpegSize pointer to an unsigned long variable which holds the size of |
| 448 | * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a |
| 449 | * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the |
| 450 | * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the |
| 451 | * size of the JPEG image (in bytes.) |
| 452 | * @param jpegSubsamp the level of chrominance subsampling to be used when |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 453 | * generating the JPEG image (see @ref TJSAMP |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 454 | * "Chrominance subsampling options".) |
| 455 | * @param jpegQual the image quality of the generated JPEG image (1 = worst, |
| 456 | 100 = best) |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 457 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 458 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 459 | * |
| 460 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 461 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 462 | DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, |
| 463 | int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf, |
| 464 | unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 465 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 466 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 467 | /** |
| 468 | * 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] | 469 | * the given parameters. The number of bytes returned by this function is |
| 470 | * larger than the size of the uncompressed source image. The reason for this |
| 471 | * is that the JPEG format uses 16-bit coefficients, and it is thus possible |
| 472 | * for a very high-quality JPEG image with very high frequency content to |
| 473 | * expand rather than compress when converted to the JPEG format. Such images |
| 474 | * represent a very rare corner case, but since there is no way to predict the |
| 475 | * size of a JPEG image prior to compression, the corner case has to be |
| 476 | * handled. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 477 | * |
| 478 | * @param width width of the image (in pixels) |
| 479 | * @param height height of the image (in pixels) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 480 | * @param jpegSubsamp the level of chrominance subsampling to be used when |
| 481 | * generating the JPEG image (see @ref TJSAMP |
| 482 | * "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 483 | * |
| 484 | * @return the maximum size of the buffer (in bytes) required to hold the |
| 485 | * image, or -1 if the arguments are out of bounds. |
| 486 | */ |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 487 | DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height, |
| 488 | int jpegSubsamp); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 489 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 490 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 491 | /** |
| 492 | * The size of the buffer (in bytes) required to hold a YUV planar image with |
| 493 | * the given parameters. |
| 494 | * |
| 495 | * @param width width of the image (in pixels) |
| 496 | * @param height height of the image (in pixels) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 497 | * @param subsamp level of chrominance subsampling in the image (see |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 498 | * @ref TJSAMP "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 499 | * |
| 500 | * @return the size of the buffer (in bytes) required to hold the image, or |
| 501 | * -1 if the arguments are out of bounds. |
| 502 | */ |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 503 | DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height, |
| 504 | int subsamp); |
DRC | f3cf973 | 2011-02-22 00:16:14 +0000 | [diff] [blame] | 505 | |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 506 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 507 | /** |
| 508 | * Encode an RGB or grayscale image into a YUV planar image. This function |
| 509 | * uses the accelerated color conversion routines in TurboJPEG's underlying |
| 510 | * codec to produce a planar YUV image that is suitable for X Video. |
| 511 | * Specifically, if the chrominance components are subsampled along the |
| 512 | * horizontal dimension, then the width of the luminance plane is padded to 2 |
| 513 | * in the output image (same goes for the height of the luminance plane, if the |
| 514 | * chrominance components are subsampled along the vertical dimension.) Also, |
| 515 | * each line of each plane in the output image is padded to 4 bytes. Although |
| 516 | * this will work with any subsampling option, it is really only useful in |
| 517 | * combination with TJ_420, which produces an image compatible with the I420 |
| 518 | * (AKA "YUV420P") format. |
| 519 | * |
| 520 | * @param handle a handle to a TurboJPEG compressor or transformer instance |
| 521 | * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels |
| 522 | * to be encoded |
| 523 | * @param width width (in pixels) of the source image |
| 524 | * @param pitch bytes per line of the source image. Normally, this should be |
| 525 | * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, |
| 526 | * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of |
| 527 | * the image is padded to the nearest 32-bit boundary, as is the case |
| 528 | * for Windows bitmaps. You can also be clever and use this parameter |
| 529 | * to skip lines, etc. Setting this parameter to 0 is the equivalent of |
| 530 | * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. |
| 531 | * @param height height (in pixels) of the source image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 532 | * @param pixelFormat pixel format of the source image (see @ref TJPF |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 533 | * "Pixel formats".) |
| 534 | * @param dstBuf pointer to an image buffer which will receive the YUV image. |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 535 | * Use #tjBufSizeYUV() to determine the appropriate size for this buffer |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 536 | * based on the image width, height, and level of chrominance |
| 537 | * subsampling. |
| 538 | * @param subsamp the level of chrominance subsampling to be used when |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 539 | * generating the YUV image (see @ref TJSAMP |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 540 | * "Chrominance subsampling options".) |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 541 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 542 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 543 | * |
| 544 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 545 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 546 | DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, |
| 547 | unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat, |
| 548 | unsigned char *dstBuf, int subsamp, int flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 549 | |
| 550 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 551 | /** |
| 552 | * Create a TurboJPEG decompressor instance. |
| 553 | * |
| 554 | * @return a handle to the newly-created instance, or NULL if an error |
| 555 | * occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 556 | */ |
| 557 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void); |
| 558 | |
| 559 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 560 | /** |
| 561 | * Retrieve information about a JPEG image without decompressing it. |
| 562 | * |
| 563 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 564 | * @param jpegBuf pointer to a buffer containing a JPEG image |
| 565 | * @param jpegSize size of the JPEG image (in bytes) |
| 566 | * @param width pointer to an integer variable which will receive the width (in |
| 567 | * pixels) of the JPEG image |
| 568 | * @param height pointer to an integer variable which will receive the height |
| 569 | * (in pixels) of the JPEG image |
| 570 | * @param jpegSubsamp pointer to an integer variable which will receive the |
| 571 | * level of chrominance subsampling used when compressing the JPEG image |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 572 | * (see @ref TJSAMP "Chrominance subsampling options".) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 573 | * |
| 574 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 575 | */ |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 576 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle, |
| 577 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height, |
| 578 | int *jpegSubsamp); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 579 | |
| 580 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 581 | /** |
| 582 | * Returns a list of fractional scaling factors that the JPEG decompressor in |
| 583 | * this implementation of TurboJPEG supports. |
| 584 | * |
| 585 | * @param numscalingfactors pointer to an integer variable that will receive |
| 586 | * the number of elements in the list |
| 587 | * |
| 588 | * @return a pointer to a list of fractional scaling factors, or NULL if an |
| 589 | * error is encountered (see #tjGetErrorStr().) |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 590 | */ |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 591 | DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors); |
DRC | b28fc57 | 2011-02-22 06:41:29 +0000 | [diff] [blame] | 592 | |
| 593 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 594 | /** |
| 595 | * Decompress a JPEG image to an RGB or grayscale image. |
| 596 | * |
| 597 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 598 | * @param jpegBuf pointer to a buffer containing the JPEG image to decompress |
| 599 | * @param jpegSize size of the JPEG image (in bytes) |
| 600 | * @param dstBuf pointer to an image buffer which will receive the decompressed |
| 601 | * image. This buffer should normally be <tt>pitch * scaledHeight</tt> |
| 602 | * bytes in size, where <tt>scaledHeight</tt> can be determined by |
| 603 | * calling #TJSCALED() with the JPEG image height and one of the scaling |
| 604 | * factors returned by #tjGetScalingFactors(). The dstBuf pointer may |
| 605 | * also be used to decompress into a specific region of a larger buffer. |
| 606 | * @param width desired width (in pixels) of the destination image. If this is |
| 607 | * smaller than the width of the JPEG image being decompressed, then |
| 608 | * TurboJPEG will use scaling in the JPEG decompressor to generate the |
| 609 | * largest possible image that will fit within the desired width. If |
| 610 | * width is set to 0, then only the height will be considered when |
| 611 | * determining the scaled image size. |
| 612 | * @param pitch bytes per line of the destination image. Normally, this is |
| 613 | * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed |
| 614 | * image is unpadded, else <tt>#TJPAD(scaledWidth * |
| 615 | * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed |
| 616 | * image is padded to the nearest 32-bit boundary, as is the case for |
| 617 | * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by |
| 618 | * calling #TJSCALED() with the JPEG image width and one of the scaling |
| 619 | * factors returned by #tjGetScalingFactors().) You can also be clever |
| 620 | * and use the pitch parameter to skip lines, etc. Setting this |
| 621 | * parameter to 0 is the equivalent of setting it to <tt>scaledWidth |
| 622 | * * #tjPixelSize[pixelFormat]</tt>. |
| 623 | * @param height desired height (in pixels) of the destination image. If this |
| 624 | * is smaller than the height of the JPEG image being decompressed, then |
| 625 | * TurboJPEG will use scaling in the JPEG decompressor to generate the |
| 626 | * largest possible image that will fit within the desired height. If |
| 627 | * height is set to 0, then only the width will be considered when |
| 628 | * determining the scaled image size. |
| 629 | * @param pixelFormat pixel format of the destination image (see @ref |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 630 | * TJPF "Pixel formats".) |
| 631 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 632 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 633 | * |
| 634 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 635 | */ |
| 636 | DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, |
| 637 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 638 | int width, int pitch, int height, int pixelFormat, int flags); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 639 | |
| 640 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 641 | /** |
| 642 | * Decompress a JPEG image to a YUV planar image. This function performs JPEG |
| 643 | * decompression but leaves out the color conversion step, so a planar YUV |
| 644 | * image is generated instead of an RGB image. The padding of the planes in |
| 645 | * this image is the same as the images generated by #tjEncodeYUV2(). Note |
| 646 | * that, if the width or height of the image is not an even multiple of the MCU |
| 647 | * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer |
| 648 | * copy will be performed within TurboJPEG. |
| 649 | * |
| 650 | * @param handle a handle to a TurboJPEG decompressor or transformer instance |
| 651 | * @param jpegBuf pointer to a buffer containing the JPEG image to decompress |
| 652 | * @param jpegSize size of the JPEG image (in bytes) |
| 653 | * @param dstBuf pointer to an image buffer which will receive the YUV image. |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 654 | * Use #tjBufSizeYUV to determine the appropriate size for this buffer |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 655 | * based on the image width, height, and level of subsampling. |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 656 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 657 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 658 | * |
| 659 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 660 | */ |
| 661 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle, |
| 662 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 663 | int flags); |
DRC | 8424160 | 2011-02-25 02:08:23 +0000 | [diff] [blame] | 664 | |
| 665 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 666 | /** |
| 667 | * Create a new TurboJPEG transformer instance. |
| 668 | * |
| 669 | * @return a handle to the newly-created instance, or NULL if an error |
| 670 | * occurred (see #tjGetErrorStr().) |
| 671 | */ |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 672 | DLLEXPORT tjhandle DLLCALL tjInitTransform(void); |
| 673 | |
| 674 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 675 | /** |
| 676 | * Losslessly transform a JPEG image into another JPEG image. Lossless |
| 677 | * transforms work by moving the raw coefficients from one JPEG image structure |
| 678 | * to another without altering the values of the coefficients. While this is |
| 679 | * typically faster than decompressing the image, transforming it, and |
| 680 | * re-compressing it, lossless transforms are not free. Each lossless |
| 681 | * transform requires reading and Huffman decoding all of the coefficients in |
| 682 | * the source image, regardless of the size of the destination image. Thus, |
| 683 | * this function provides a means of generating multiple transformed images |
| 684 | * from the same source or of applying multiple transformations simultaneously, |
| 685 | * in order to eliminate the need to read the source coefficients multiple |
| 686 | * times. |
| 687 | * |
| 688 | * @param handle a handle to a TurboJPEG transformer instance |
| 689 | * @param jpegBuf pointer to a buffer containing the JPEG image to transform |
| 690 | * @param jpegSize size of the JPEG image (in bytes) |
| 691 | * @param n the number of transformed JPEG images to generate |
| 692 | * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt> |
| 693 | * will receive a JPEG image that has been transformed using the |
| 694 | * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to |
| 695 | * reallocate the JPEG buffer to accommodate the size of the JPEG image. |
| 696 | * Thus, you can choose to: |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 697 | * -# pre-allocate the JPEG buffer with an arbitrary size using |
| 698 | * #tjAlloc() and let TurboJPEG grow the buffer as needed, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 699 | * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the |
| 700 | * buffer for you, or |
| 701 | * -# pre-allocate the buffer to a "worst case" size determined by |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 702 | * calling #tjBufSize() with the cropped width and height. This should |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 703 | * ensure that the buffer never has to be re-allocated (setting |
| 704 | * #TJFLAG_NOREALLOC guarantees this.) |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 705 | * . |
DRC | ff78e37 | 2011-05-24 10:17:32 +0000 | [diff] [blame] | 706 | * If you choose option 1, <tt>dstSizes[i]</tt> should be set to |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 707 | * the size of your pre-allocated buffer. In any case, unless you have |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 708 | * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> |
| 709 | * upon return from this function, as it may have changed. |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 710 | * @param dstSizes pointer to an array of n unsigned long variables which will |
| 711 | * receive the actual sizes (in bytes) of each transformed JPEG image. |
| 712 | * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then |
| 713 | * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon |
| 714 | * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image |
| 715 | * (in bytes.) |
| 716 | * @param transforms pointer to an array of n tjtransform structures, each of |
| 717 | * which specifies the transform parameters and/or cropping region for |
| 718 | * the corresponding transformed output image. |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 719 | * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP |
| 720 | * "flags". |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 721 | * |
| 722 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 723 | */ |
| 724 | DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf, |
| 725 | unsigned long jpegSize, int n, unsigned char **dstBufs, |
| 726 | unsigned long *dstSizes, tjtransform *transforms, int flags); |
DRC | 890f1e0 | 2011-02-26 22:02:37 +0000 | [diff] [blame] | 727 | |
| 728 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 729 | /** |
| 730 | * Destroy a TurboJPEG compressor, decompressor, or transformer instance. |
| 731 | * |
| 732 | * @param handle a handle to a TurboJPEG compressor, decompressor or |
| 733 | * transformer instance |
| 734 | * |
| 735 | * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) |
| 736 | */ |
| 737 | DLLEXPORT int DLLCALL tjDestroy(tjhandle handle); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 738 | |
| 739 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 740 | /** |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 741 | * Allocate an image buffer for use with TurboJPEG. You should always use |
| 742 | * this function to allocate the JPEG destination buffer(s) for #tjCompress2() |
| 743 | * and #tjTransform() unless you are disabling automatic buffer |
| 744 | * (re)allocation (by setting #TJFLAG_NOREALLOC.) |
| 745 | * |
| 746 | * @param bytes the number of bytes to allocate |
| 747 | * |
| 748 | * @return a pointer to a newly-allocated buffer with the specified number of |
| 749 | * bytes |
DRC | d441107 | 2011-05-24 17:00:15 +0000 | [diff] [blame] | 750 | * |
| 751 | * @sa tjFree() |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 752 | */ |
| 753 | DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes); |
| 754 | |
| 755 | |
| 756 | /** |
| 757 | * Free an image buffer previously allocated by TurboJPEG. You should always |
| 758 | * use this function to free JPEG destination buffer(s) that were automatically |
| 759 | * (re)allocated by #tjCompress2() or #tjTransform() or that were manually |
| 760 | * allocated using #tjAlloc(). |
| 761 | * |
| 762 | * @param buffer address of the buffer to free |
DRC | d441107 | 2011-05-24 17:00:15 +0000 | [diff] [blame] | 763 | * |
| 764 | * @sa tjAlloc() |
DRC | 6b76f75 | 2011-05-24 16:52:47 +0000 | [diff] [blame] | 765 | */ |
| 766 | DLLEXPORT void DLLCALL tjFree(unsigned char *buffer); |
| 767 | |
| 768 | |
| 769 | /** |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 770 | * Returns a descriptive error message explaining why the last command failed. |
| 771 | * |
| 772 | * @return a descriptive error message explaining why the last command failed. |
| 773 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 774 | DLLEXPORT char* DLLCALL tjGetErrorStr(void); |
| 775 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 776 | |
| 777 | /* Backward compatibility functions and macros (nothing to see here) */ |
| 778 | #define NUMSUBOPT TJ_NUMSAMP |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 779 | #define TJ_444 TJSAMP_444 |
| 780 | #define TJ_422 TJSAMP_422 |
| 781 | #define TJ_420 TJSAMP_420 |
| 782 | #define TJ_411 TJSAMP_420 |
| 783 | #define TJ_GRAYSCALE TJSAMP_GRAY |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 784 | |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 785 | #define TJ_BGR 1 |
| 786 | #define TJ_BOTTOMUP TJFLAG_BOTTOMUP |
| 787 | #define TJ_FORCEMMX TJFLAG_FORCEMMX |
| 788 | #define TJ_FORCESSE TJFLAG_FORCESSE |
| 789 | #define TJ_FORCESSE2 TJFLAG_FORCESSE2 |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 790 | #define TJ_ALPHAFIRST 64 |
DRC | 25b995a | 2011-05-21 15:34:54 +0000 | [diff] [blame] | 791 | #define TJ_FORCESSE3 TJFLAG_FORCESSE3 |
| 792 | #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 793 | #define TJ_YUV 512 |
| 794 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 795 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height); |
| 796 | |
| 797 | DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height, |
| 798 | int jpegSubsamp); |
| 799 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 800 | DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf, |
| 801 | int width, int pitch, int height, int pixelSize, unsigned char *dstBuf, |
| 802 | unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags); |
| 803 | |
| 804 | DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, |
| 805 | unsigned char *srcBuf, int width, int pitch, int height, int pixelSize, |
| 806 | unsigned char *dstBuf, int subsamp, int flags); |
| 807 | |
| 808 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle, |
| 809 | unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height); |
| 810 | |
| 811 | DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, |
| 812 | unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf, |
| 813 | int width, int pitch, int height, int pixelSize, int flags); |
| 814 | |
| 815 | |
| 816 | /** |
| 817 | * @} |
| 818 | */ |
| 819 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 820 | #ifdef __cplusplus |
| 821 | } |
| 822 | #endif |
DRC | 3a1bb35 | 2011-05-24 09:15:44 +0000 | [diff] [blame] | 823 | |
| 824 | #endif |