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