Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-07 08:41:44

0001 /*
0002  * Copyright (C)2009-2015, 2017, 2020-2025 D. R. Commander.
0003  *                                         All Rights Reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions are met:
0007  *
0008  * - Redistributions of source code must retain the above copyright notice,
0009  *   this list of conditions and the following disclaimer.
0010  * - Redistributions in binary form must reproduce the above copyright notice,
0011  *   this list of conditions and the following disclaimer in the documentation
0012  *   and/or other materials provided with the distribution.
0013  * - Neither the name of the libjpeg-turbo Project nor the names of its
0014  *   contributors may be used to endorse or promote products derived from this
0015  *   software without specific prior written permission.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
0018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
0021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0027  * POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 
0030 #ifndef __TURBOJPEG_H__
0031 #define __TURBOJPEG_H__
0032 
0033 #include <stddef.h>
0034 
0035 #if defined(_WIN32) && defined(DLLDEFINE)
0036 #define DLLEXPORT  __declspec(dllexport)
0037 #else
0038 #define DLLEXPORT
0039 #endif
0040 #define DLLCALL
0041 
0042 
0043 /**
0044  * @addtogroup TurboJPEG
0045  * TurboJPEG API.  This API provides an interface for generating, decoding, and
0046  * transforming planar YUV and JPEG images in memory.
0047  *
0048  * @anchor YUVnotes
0049  * YUV Image Format Notes
0050  * ----------------------
0051  * Technically, the JPEG format uses the YCbCr colorspace (which is technically
0052  * not a colorspace but a color transform), but per the convention of the
0053  * digital video community, the TurboJPEG API uses "YUV" to refer to an image
0054  * format consisting of Y, Cb, and Cr image planes.
0055  *
0056  * Each plane is simply a 2D array of bytes, each byte representing the value
0057  * of one of the components (Y, Cb, or Cr) at a particular location in the
0058  * image.  The width and height of each plane are determined by the image
0059  * width, height, and level of chrominance subsampling.  The luminance plane
0060  * width is the image width padded to the nearest multiple of the horizontal
0061  * subsampling factor (1 in the case of 4:4:4, grayscale, 4:4:0, or 4:4:1; 2 in
0062  * the case of 4:2:2 or 4:2:0; 4 in the case of 4:1:1.)  Similarly, the
0063  * luminance plane height is the image height padded to the nearest multiple of
0064  * the vertical subsampling factor (1 in the case of 4:4:4, 4:2:2, grayscale,
0065  * or 4:1:1; 2 in the case of 4:2:0 or 4:4:0; 4 in the case of 4:4:1.)  This is
0066  * irrespective of any additional padding that may be specified as an argument
0067  * to the various YUV functions.  The chrominance plane width is equal to the
0068  * luminance plane width divided by the horizontal subsampling factor, and the
0069  * chrominance plane height is equal to the luminance plane height divided by
0070  * the vertical subsampling factor.
0071  *
0072  * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
0073  * used, then the luminance plane would be 36 x 35 bytes, and each of the
0074  * chrominance planes would be 18 x 35 bytes.  If you specify a row alignment
0075  * of 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes,
0076  * and each of the chrominance planes would be 20 x 35 bytes.
0077  *
0078  * @{
0079  */
0080 
0081 
0082 /**
0083  * The number of initialization options
0084  */
0085 #define TJ_NUMINIT  3
0086 
0087 /**
0088  * Initialization options
0089  */
0090 enum TJINIT {
0091   /**
0092    * Initialize the TurboJPEG instance for compression.
0093    */
0094   TJINIT_COMPRESS,
0095   /**
0096    * Initialize the TurboJPEG instance for decompression.
0097    */
0098   TJINIT_DECOMPRESS,
0099   /**
0100    * Initialize the TurboJPEG instance for lossless transformation (both
0101    * compression and decompression.)
0102    */
0103   TJINIT_TRANSFORM
0104 };
0105 
0106 
0107 /**
0108  * The number of chrominance subsampling options
0109  */
0110 #define TJ_NUMSAMP  7
0111 
0112 /**
0113  * Chrominance subsampling options
0114  *
0115  * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
0116  * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
0117  * the Cb and Cr (chrominance) components can be discarded or averaged together
0118  * to produce a smaller image with little perceptible loss of image quality.
0119  * (The human eye is more sensitive to small changes in brightness than to
0120  * small changes in color.)  This is called "chrominance subsampling".
0121  */
0122 enum TJSAMP {
0123   /**
0124    * 4:4:4 chrominance subsampling (no chrominance subsampling)
0125    *
0126    * The JPEG or YUV image will contain one chrominance component for every
0127    * pixel in the source image.
0128    */
0129   TJSAMP_444,
0130   /**
0131    * 4:2:2 chrominance subsampling
0132    *
0133    * The JPEG or YUV image will contain one chrominance component for every 2x1
0134    * block of pixels in the source image.
0135    */
0136   TJSAMP_422,
0137   /**
0138    * 4:2:0 chrominance subsampling
0139    *
0140    * The JPEG or YUV image will contain one chrominance component for every 2x2
0141    * block of pixels in the source image.
0142    */
0143   TJSAMP_420,
0144   /**
0145    * Grayscale
0146    *
0147    * The JPEG or YUV image will contain no chrominance components.
0148    */
0149   TJSAMP_GRAY,
0150   /**
0151    * 4:4:0 chrominance subsampling
0152    *
0153    * The JPEG or YUV image will contain one chrominance component for every 1x2
0154    * block of pixels in the source image.
0155    *
0156    * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
0157    */
0158   TJSAMP_440,
0159   /**
0160    * 4:1:1 chrominance subsampling
0161    *
0162    * The JPEG or YUV image will contain one chrominance component for every 4x1
0163    * block of pixels in the source image.  All else being equal, a JPEG image
0164    * with 4:1:1 subsampling is almost exactly the same size as a JPEG image
0165    * with 4:2:0 subsampling, and in the aggregate, both subsampling methods
0166    * produce approximately the same perceptual quality.  However, 4:1:1 is
0167    * better able to reproduce sharp horizontal features.
0168    *
0169    * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
0170    */
0171   TJSAMP_411,
0172   /**
0173    * 4:4:1 chrominance subsampling
0174    *
0175    * The JPEG or YUV image will contain one chrominance component for every 1x4
0176    * block of pixels in the source image.  All else being equal, a JPEG image
0177    * with 4:4:1 subsampling is almost exactly the same size as a JPEG image
0178    * with 4:2:0 subsampling, and in the aggregate, both subsampling methods
0179    * produce approximately the same perceptual quality.  However, 4:4:1 is
0180    * better able to reproduce sharp vertical features.
0181    *
0182    * @note 4:4:1 subsampling is not fully accelerated in libjpeg-turbo.
0183    */
0184   TJSAMP_441,
0185   /**
0186    * Unknown subsampling
0187    *
0188    * The JPEG image uses an unusual type of chrominance subsampling.  Such
0189    * images can be decompressed into packed-pixel images, but they cannot be
0190    * - decompressed into planar YUV images,
0191    * - losslessly transformed if #TJXOPT_CROP is specified and #TJXOPT_GRAY is
0192    * not specified, or
0193    * - partially decompressed using a cropping region.
0194    */
0195   TJSAMP_UNKNOWN = -1
0196 };
0197 
0198 /**
0199  * iMCU width (in pixels) for a given level of chrominance subsampling
0200  *
0201  * In a typical lossy JPEG image, 8x8 blocks of DCT coefficients for each
0202  * component are interleaved in a single scan.  If the image uses chrominance
0203  * subsampling, then multiple luminance blocks are stored together, followed by
0204  * a single block for each chrominance component.  The minimum set of
0205  * full-resolution luminance block(s) and corresponding (possibly subsampled)
0206  * chrominance blocks necessary to represent at least one DCT block per
0207  * component is called a "Minimum Coded Unit" or "MCU".  (For example, an MCU
0208  * in an interleaved lossy JPEG image that uses 4:2:2 subsampling consists of
0209  * two luminance blocks followed by one block for each chrominance component.)
0210  * In a non-interleaved lossy JPEG image, each component is stored in a
0211  * separate scan, and an MCU is a single DCT block, so we use the term "iMCU"
0212  * (interleaved MCU) to refer to the equivalent of an MCU in an interleaved
0213  * JPEG image.  For the common case of interleaved JPEG images, an iMCU is the
0214  * same as an MCU.
0215  *
0216  * iMCU sizes:
0217  * - 8x8 for no subsampling or grayscale
0218  * - 16x8 for 4:2:2
0219  * - 8x16 for 4:4:0
0220  * - 16x16 for 4:2:0
0221  * - 32x8 for 4:1:1
0222  * - 8x32 for 4:4:1
0223  */
0224 static const int tjMCUWidth[TJ_NUMSAMP]  = { 8, 16, 16, 8, 8, 32, 8 };
0225 
0226 /**
0227  * iMCU height (in pixels) for a given level of chrominance subsampling
0228  *
0229  * In a typical lossy JPEG image, 8x8 blocks of DCT coefficients for each
0230  * component are interleaved in a single scan.  If the image uses chrominance
0231  * subsampling, then multiple luminance blocks are stored together, followed by
0232  * a single block for each chrominance component.  The minimum set of
0233  * full-resolution luminance block(s) and corresponding (possibly subsampled)
0234  * chrominance blocks necessary to represent at least one DCT block per
0235  * component is called a "Minimum Coded Unit" or "MCU".  (For example, an MCU
0236  * in an interleaved lossy JPEG image that uses 4:2:2 subsampling consists of
0237  * two luminance blocks followed by one block for each chrominance component.)
0238  * In a non-interleaved lossy JPEG image, each component is stored in a
0239  * separate scan, and an MCU is a single DCT block, so we use the term "iMCU"
0240  * (interleaved MCU) to refer to the equivalent of an MCU in an interleaved
0241  * JPEG image.  For the common case of interleaved JPEG images, an iMCU is the
0242  * same as an MCU.
0243  *
0244  * iMCU sizes:
0245  * - 8x8 for no subsampling or grayscale
0246  * - 16x8 for 4:2:2
0247  * - 8x16 for 4:4:0
0248  * - 16x16 for 4:2:0
0249  * - 32x8 for 4:1:1
0250  * - 8x32 for 4:4:1
0251  */
0252 static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8, 32 };
0253 
0254 
0255 /**
0256  * The number of pixel formats
0257  */
0258 #define TJ_NUMPF  12
0259 
0260 /**
0261  * Pixel formats
0262  */
0263 enum TJPF {
0264   /**
0265    * RGB pixel format
0266    *
0267    * The red, green, and blue components in the image are stored in 3-sample
0268    * pixels in the order R, G, B from lowest to highest memory address within
0269    * each pixel.
0270    */
0271   TJPF_RGB,
0272   /**
0273    * BGR pixel format
0274    *
0275    * The red, green, and blue components in the image are stored in 3-sample
0276    * pixels in the order B, G, R from lowest to highest memory address within
0277    * each pixel.
0278    */
0279   TJPF_BGR,
0280   /**
0281    * RGBX pixel format
0282    *
0283    * The red, green, and blue components in the image are stored in 4-sample
0284    * pixels in the order R, G, B from lowest to highest memory address within
0285    * each pixel.  The X component is ignored when compressing/encoding and
0286    * undefined when decompressing/decoding.
0287    */
0288   TJPF_RGBX,
0289   /**
0290    * BGRX pixel format
0291    *
0292    * The red, green, and blue components in the image are stored in 4-sample
0293    * pixels in the order B, G, R from lowest to highest memory address within
0294    * each pixel.  The X component is ignored when compressing/encoding and
0295    * undefined when decompressing/decoding.
0296    */
0297   TJPF_BGRX,
0298   /**
0299    * XBGR pixel format
0300    *
0301    * The red, green, and blue components in the image are stored in 4-sample
0302    * pixels in the order R, G, B from highest to lowest memory address within
0303    * each pixel.  The X component is ignored when compressing/encoding and
0304    * undefined when decompressing/decoding.
0305    */
0306   TJPF_XBGR,
0307   /**
0308    * XRGB pixel format
0309    *
0310    * The red, green, and blue components in the image are stored in 4-sample
0311    * pixels in the order B, G, R from highest to lowest memory address within
0312    * each pixel.  The X component is ignored when compressing/encoding and
0313    * undefined when decompressing/decoding.
0314    */
0315   TJPF_XRGB,
0316   /**
0317    * Grayscale pixel format
0318    *
0319    * Each 1-sample pixel represents a luminance (brightness) level from 0 to
0320    * the maximum sample value (which is, for instance, 255 for 8-bit samples or
0321    * 4095 for 12-bit samples or 65535 for 16-bit samples.)
0322    */
0323   TJPF_GRAY,
0324   /**
0325    * RGBA pixel format
0326    *
0327    * This is the same as @ref TJPF_RGBX, except that when
0328    * decompressing/decoding, the X component is guaranteed to be equal to the
0329    * maximum sample value, which can be interpreted as an opaque alpha channel.
0330    */
0331   TJPF_RGBA,
0332   /**
0333    * BGRA pixel format
0334    *
0335    * This is the same as @ref TJPF_BGRX, except that when
0336    * decompressing/decoding, the X component is guaranteed to be equal to the
0337    * maximum sample value, which can be interpreted as an opaque alpha channel.
0338    */
0339   TJPF_BGRA,
0340   /**
0341    * ABGR pixel format
0342    *
0343    * This is the same as @ref TJPF_XBGR, except that when
0344    * decompressing/decoding, the X component is guaranteed to be equal to the
0345    * maximum sample value, which can be interpreted as an opaque alpha channel.
0346    */
0347   TJPF_ABGR,
0348   /**
0349    * ARGB pixel format
0350    *
0351    * This is the same as @ref TJPF_XRGB, except that when
0352    * decompressing/decoding, the X component is guaranteed to be equal to the
0353    * maximum sample value, which can be interpreted as an opaque alpha channel.
0354    */
0355   TJPF_ARGB,
0356   /**
0357    * CMYK pixel format
0358    *
0359    * Unlike RGB, which is an additive color model used primarily for display,
0360    * CMYK (Cyan/Magenta/Yellow/Key) is a subtractive color model used primarily
0361    * for printing.  In the CMYK color model, the value of each color component
0362    * typically corresponds to an amount of cyan, magenta, yellow, or black ink
0363    * that is applied to a white background.  In order to convert between CMYK
0364    * and RGB, it is necessary to use a color management system (CMS.)  A CMS
0365    * will attempt to map colors within the printer's gamut to perceptually
0366    * similar colors in the display's gamut and vice versa, but the mapping is
0367    * typically not 1:1 or reversible, nor can it be defined with a simple
0368    * formula.  Thus, such a conversion is out of scope for a codec library.
0369    * However, the TurboJPEG API allows for compressing packed-pixel CMYK images
0370    * into YCCK JPEG images (see #TJCS_YCCK) and decompressing YCCK JPEG images
0371    * into packed-pixel CMYK images.
0372    */
0373   TJPF_CMYK,
0374   /**
0375    * Unknown pixel format
0376    *
0377    * Currently this is only used by #tj3LoadImage8(), #tj3LoadImage12(), and
0378    * #tj3LoadImage16().
0379    */
0380   TJPF_UNKNOWN = -1
0381 };
0382 
0383 /**
0384  * Red offset (in samples) for a given pixel format
0385  *
0386  * This specifies the number of samples that the red component is offset from
0387  * the start of the pixel.  For instance, if an 8-bit-per-component pixel of
0388  * format TJPF_BGRX is stored in `unsigned char pixel[]`, then the red
0389  * component is `pixel[tjRedOffset[TJPF_BGRX]]`.  The offset is -1 if the pixel
0390  * format does not have a red component.
0391  */
0392 static const int tjRedOffset[TJ_NUMPF] = {
0393   0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
0394 };
0395 /**
0396  * Green offset (in samples) for a given pixel format
0397  *
0398  * This specifies the number of samples that the green component is offset from
0399  * the start of the pixel.  For instance, if an 8-bit-per-component pixel of
0400  * format TJPF_BGRX is stored in `unsigned char pixel[]`, then the green
0401  * component is `pixel[tjGreenOffset[TJPF_BGRX]]`.  The offset is -1 if the
0402  * pixel format does not have a green component.
0403  */
0404 static const int tjGreenOffset[TJ_NUMPF] = {
0405   1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
0406 };
0407 /**
0408  * Blue offset (in samples) for a given pixel format
0409  *
0410  * This specifies the number of samples that the blue component is offset from
0411  * the start of the pixel.  For instance, if an 8-bit-per-component pixel of
0412  * format TJPF_BGRX is stored in `unsigned char pixel[]`, then the blue
0413  * component is `pixel[tjBlueOffset[TJPF_BGRX]]`.  The offset is -1 if the
0414  * pixel format does not have a blue component.
0415  */
0416 static const int tjBlueOffset[TJ_NUMPF] = {
0417   2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
0418 };
0419 /**
0420  * Alpha offset (in samples) for a given pixel format
0421  *
0422  * This specifies the number of samples that the alpha component is offset from
0423  * the start of the pixel.  For instance, if an 8-bit-per-component pixel of
0424  * format TJPF_BGRA is stored in `unsigned char pixel[]`, then the alpha
0425  * component is `pixel[tjAlphaOffset[TJPF_BGRA]]`.  The offset is -1 if the
0426  * pixel format does not have an alpha component.
0427  */
0428 static const int tjAlphaOffset[TJ_NUMPF] = {
0429   -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
0430 };
0431 /**
0432  * Pixel size (in samples) for a given pixel format
0433  */
0434 static const int tjPixelSize[TJ_NUMPF] = {
0435   3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
0436 };
0437 
0438 
0439 /**
0440  * The number of JPEG colorspaces
0441  */
0442 #define TJ_NUMCS  5
0443 
0444 /**
0445  * JPEG colorspaces
0446  */
0447 enum TJCS {
0448   /**
0449    * RGB colorspace
0450    *
0451    * When generating the JPEG image, the R, G, and B components in the source
0452    * image are reordered into image planes, but no colorspace conversion or
0453    * subsampling is performed.  RGB JPEG images can be generated from and
0454    * decompressed to packed-pixel images with any of the extended RGB or
0455    * grayscale pixel formats, but they cannot be generated from or
0456    * decompressed to planar YUV images.
0457    */
0458   TJCS_RGB,
0459   /**
0460    * YCbCr colorspace
0461    *
0462    * YCbCr is not an absolute colorspace but rather a mathematical
0463    * transformation of RGB designed solely for storage and transmission.  YCbCr
0464    * images must be converted to RGB before they can be displayed.  In the
0465    * YCbCr colorspace, the Y (luminance) component represents the black & white
0466    * portion of the original image, and the Cb and Cr (chrominance) components
0467    * represent the color portion of the original image.  Historically, the
0468    * analog equivalent of this transformation allowed the same signal to be
0469    * displayed to both black & white and color televisions, but JPEG images use
0470    * YCbCr primarily because it allows the color data to be optionally
0471    * subsampled in order to reduce network and disk usage.  YCbCr is the most
0472    * common JPEG colorspace, and YCbCr JPEG images can be generated from and
0473    * decompressed to packed-pixel images with any of the extended RGB or
0474    * grayscale pixel formats.  YCbCr JPEG images can also be generated from
0475    * and decompressed to planar YUV images.
0476    */
0477   TJCS_YCbCr,
0478   /**
0479    * Grayscale colorspace
0480    *
0481    * The JPEG image retains only the luminance data (Y component), and any
0482    * color data from the source image is discarded.  Grayscale JPEG images can
0483    * be generated from and decompressed to packed-pixel images with any of the
0484    * extended RGB or grayscale pixel formats, or they can be generated from
0485    * and decompressed to planar YUV images.
0486    */
0487   TJCS_GRAY,
0488   /**
0489    * CMYK colorspace
0490    *
0491    * When generating the JPEG image, the C, M, Y, and K components in the
0492    * source image are reordered into image planes, but no colorspace conversion
0493    * or subsampling is performed.  CMYK JPEG images can only be generated from
0494    * and decompressed to packed-pixel images with the CMYK pixel format.
0495    */
0496   TJCS_CMYK,
0497   /**
0498    * YCCK colorspace
0499    *
0500    * YCCK (AKA "YCbCrK") is not an absolute colorspace but rather a
0501    * mathematical transformation of CMYK designed solely for storage and
0502    * transmission.  It is to CMYK as YCbCr is to RGB.  CMYK pixels can be
0503    * reversibly transformed into YCCK, and as with YCbCr, the chrominance
0504    * components in the YCCK pixels can be subsampled without incurring major
0505    * perceptual loss.  YCCK JPEG images can only be generated from and
0506    * decompressed to packed-pixel images with the CMYK pixel format.
0507    */
0508   TJCS_YCCK
0509 };
0510 
0511 
0512 /**
0513  * Parameters
0514  */
0515 enum TJPARAM {
0516   /**
0517    * Error handling behavior
0518    *
0519    * **Value**
0520    * - `0` *[default]* Allow the current compression/decompression/transform
0521    * operation to complete unless a fatal error is encountered.
0522    * - `1` Immediately discontinue the current
0523    * compression/decompression/transform operation if a warning (non-fatal
0524    * error) occurs.
0525    */
0526   TJPARAM_STOPONWARNING,
0527   /**
0528    * Row order in packed-pixel source/destination images
0529    *
0530    * **Value**
0531    * - `0` *[default]* top-down (X11) order
0532    * - `1` bottom-up (Windows, OpenGL) order
0533    */
0534   TJPARAM_BOTTOMUP,
0535   /**
0536    * JPEG destination buffer (re)allocation [compression, lossless
0537    * transformation]
0538    *
0539    * **Value**
0540    * - `0` *[default]* Attempt to allocate or reallocate the JPEG destination
0541    * buffer as needed.
0542    * - `1` Generate an error if the JPEG destination buffer is invalid or too
0543    * small.
0544    */
0545   TJPARAM_NOREALLOC,
0546   /**
0547    * Perceptual quality of lossy JPEG images [compression only]
0548    *
0549    * **Value**
0550    * - `1`-`100` (`1` = worst quality but best compression, `100` = best
0551    * quality but worst compression) *[no default; must be explicitly
0552    * specified]*
0553    */
0554   TJPARAM_QUALITY,
0555   /**
0556    * Chrominance subsampling level
0557    *
0558    * The JPEG or YUV image uses (decompression, decoding) or will use (lossy
0559    * compression, encoding) the specified level of chrominance subsampling.
0560    *
0561    * **Value**
0562    * - One of the @ref TJSAMP "chrominance subsampling options" *[no default;
0563    * must be explicitly specified for lossy compression, encoding, and
0564    * decoding]*
0565    */
0566   TJPARAM_SUBSAMP,
0567   /**
0568    * JPEG width (in pixels) [decompression only, read-only]
0569    */
0570   TJPARAM_JPEGWIDTH,
0571   /**
0572    * JPEG height (in pixels) [decompression only, read-only]
0573    */
0574   TJPARAM_JPEGHEIGHT,
0575   /**
0576    * Data precision (bits per sample)
0577    *
0578    * The JPEG image uses (decompression) or will use (lossless compression) the
0579    * specified number of bits per sample.  This parameter also specifies the
0580    * target data precision when loading a PBMPLUS file with #tj3LoadImage8(),
0581    * #tj3LoadImage12(), or #tj3LoadImage16() and the source data precision when
0582    * saving a PBMPLUS file with #tj3SaveImage8(), #tj3SaveImage12(), or
0583    * #tj3SaveImage16().
0584    *
0585    * The data precision is the number of bits in the maximum sample value,
0586    * which may not be the same as the width of the data type used to store the
0587    * sample.
0588    *
0589    * **Value**
0590    * - `8` or `12` for lossy JPEG images; `2` to `16` for lossless JPEG and
0591    * PBMPLUS images
0592    *
0593    * 12-bit JPEG data precision implies #TJPARAM_OPTIMIZE unless
0594    * #TJPARAM_ARITHMETIC is set.
0595    */
0596   TJPARAM_PRECISION,
0597   /**
0598    * JPEG colorspace
0599    *
0600    * The JPEG image uses (decompression) or will use (lossy compression) the
0601    * specified colorspace.
0602    *
0603    * **Value**
0604    * - One of the @ref TJCS "JPEG colorspaces" *[default for lossy compression:
0605    * automatically selected based on the subsampling level and pixel format]*
0606    */
0607   TJPARAM_COLORSPACE,
0608   /**
0609    * Chrominance upsampling algorithm [lossy decompression only]
0610    *
0611    * **Value**
0612    * - `0` *[default]* Use smooth upsampling when decompressing a JPEG image
0613    * that was generated using chrominance subsampling.  This creates a smooth
0614    * transition between neighboring chrominance components in order to reduce
0615    * upsampling artifacts in the decompressed image.
0616    * - `1` Use the fastest chrominance upsampling algorithm available, which
0617    * may combine upsampling with color conversion.
0618    */
0619   TJPARAM_FASTUPSAMPLE,
0620   /**
0621    * DCT/IDCT algorithm [lossy compression and decompression]
0622    *
0623    * **Value**
0624    * - `0` *[default]* Use the most accurate DCT/IDCT algorithm available.
0625    * - `1` Use the fastest DCT/IDCT algorithm available.
0626    *
0627    * This parameter is provided mainly for backward compatibility with libjpeg,
0628    * which historically implemented several different DCT/IDCT algorithms
0629    * because of performance limitations with 1990s CPUs.  In the libjpeg-turbo
0630    * implementation of the TurboJPEG API:
0631    * - The "fast" and "accurate" DCT/IDCT algorithms perform similarly on
0632    * modern x86/x86-64 CPUs that support AVX2 instructions.
0633    * - The "fast" algorithm is generally only about 5-15% faster than the
0634    * "accurate" algorithm on other types of CPUs.
0635    * - The difference in accuracy between the "fast" and "accurate" algorithms
0636    * is the most pronounced at JPEG quality levels above 90 and tends to be
0637    * more pronounced with decompression than with compression.
0638    * - For JPEG quality levels above 97, the "fast" algorithm degrades and is
0639    * not fully accelerated, so it is slower than the "accurate" algorithm.
0640    */
0641   TJPARAM_FASTDCT,
0642   /**
0643    * Huffman table optimization [lossy compression, lossless transformation]
0644    *
0645    * **Value**
0646    * - `0` *[default]* The JPEG image will use the default Huffman tables.
0647    * - `1` Optimal Huffman tables will be computed for the JPEG image.  For
0648    * lossless transformation, this can also be specified using
0649    * #TJXOPT_OPTIMIZE.
0650    *
0651    * Huffman table optimization improves compression slightly (generally 5% or
0652    * less), but it reduces compression performance considerably.
0653    */
0654   TJPARAM_OPTIMIZE,
0655   /**
0656    * Progressive JPEG
0657    *
0658    * In a progressive JPEG image, the DCT coefficients are split across
0659    * multiple "scans" of increasing quality.  Thus, a low-quality scan
0660    * containing the lowest-frequency DCT coefficients can be transmitted first
0661    * and refined with subsequent higher-quality scans containing
0662    * higher-frequency DCT coefficients.  When using Huffman entropy coding, the
0663    * progressive JPEG format also provides an "end-of-bands (EOB) run" feature
0664    * that allows large groups of zeroes, potentially spanning multiple MCUs,
0665    * to be represented using only a few bytes.
0666    *
0667    * **Value**
0668    * - `0` *[default for compression, lossless transformation]* The lossy JPEG
0669    * image is (decompression) or will be (compression, lossless transformation)
0670    * single-scan.
0671    * - `1` The lossy JPEG image is (decompression) or will be (compression,
0672    * lossless transformation) progressive.  For lossless transformation, this
0673    * can also be specified using #TJXOPT_PROGRESSIVE.
0674    *
0675    * Progressive JPEG images generally have better compression ratios than
0676    * single-scan JPEG images (much better if the image has large areas of solid
0677    * color), but progressive JPEG compression and decompression is considerably
0678    * slower than single-scan JPEG compression and decompression.  Can be
0679    * combined with #TJPARAM_ARITHMETIC.  Implies #TJPARAM_OPTIMIZE unless
0680    * #TJPARAM_ARITHMETIC is also set.
0681    */
0682   TJPARAM_PROGRESSIVE,
0683   /**
0684    * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless
0685    * transformation]
0686    *
0687    * Setting this parameter causes the decompression and transform functions to
0688    * return an error if the number of scans in a progressive JPEG image exceeds
0689    * the specified limit.  The primary purpose of this is to allow
0690    * security-critical applications to guard against an exploit of the
0691    * progressive JPEG format described in
0692    * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
0693    *
0694    * **Value**
0695    * - maximum number of progressive JPEG scans that the decompression and
0696    * transform functions will process *[default: `0` (no limit)]*
0697    *
0698    * @see #TJPARAM_PROGRESSIVE
0699    */
0700   TJPARAM_SCANLIMIT,
0701   /**
0702    * Arithmetic entropy coding
0703    *
0704    * **Value**
0705    * - `0` *[default for compression, lossless transformation]* The lossy JPEG
0706    * image uses (decompression) or will use (compression, lossless
0707    * transformation) Huffman entropy coding.
0708    * - `1` The lossy JPEG image uses (decompression) or will use (compression,
0709    * lossless transformation) arithmetic entropy coding.  For lossless
0710    * transformation, this can also be specified using #TJXOPT_ARITHMETIC.
0711    *
0712    * Arithmetic entropy coding generally improves compression relative to
0713    * Huffman entropy coding, but it reduces compression and decompression
0714    * performance considerably.  Can be combined with #TJPARAM_PROGRESSIVE.
0715    */
0716   TJPARAM_ARITHMETIC,
0717   /**
0718    * Lossless JPEG
0719    *
0720    * **Value**
0721    * - `0` *[default for compression]* The JPEG image is (decompression) or
0722    * will be (compression) lossy/DCT-based.
0723    * - `1` The JPEG image is (decompression) or will be (compression)
0724    * lossless/predictive.
0725    *
0726    * In most cases, lossless JPEG compression and decompression is considerably
0727    * slower than lossy JPEG compression and decompression, and lossless JPEG
0728    * images are much larger than lossy JPEG images.  Thus, lossless JPEG images
0729    * are typically used only for applications that require mathematically
0730    * lossless compression.  Also note that the following features are not
0731    * available with lossless JPEG images:
0732    * - Colorspace conversion (lossless JPEG images always use #TJCS_RGB,
0733    * #TJCS_GRAY, or #TJCS_CMYK, depending on the pixel format of the source
0734    * image)
0735    * - Chrominance subsampling (lossless JPEG images always use #TJSAMP_444)
0736    * - JPEG quality selection
0737    * - DCT/IDCT algorithm selection
0738    * - Progressive JPEG
0739    * - Arithmetic entropy coding
0740    * - Compression from/decompression to planar YUV images (this parameter is
0741    * ignored by #tj3CompressFromYUV8() and #tj3CompressFromYUVPlanes8())
0742    * - Decompression scaling
0743    * - Lossless transformation
0744    *
0745    * @see #TJPARAM_LOSSLESSPSV, #TJPARAM_LOSSLESSPT
0746    */
0747   TJPARAM_LOSSLESS,
0748   /**
0749    * Lossless JPEG predictor selection value (PSV)
0750    *
0751    * **Value**
0752    * - `1`-`7` *[default for compression: `1`]*
0753    *
0754    * Lossless JPEG compression shares no algorithms with lossy JPEG
0755    * compression.  Instead, it uses differential pulse-code modulation (DPCM),
0756    * an algorithm whereby each sample is encoded as the difference between the
0757    * sample's value and a "predictor", which is based on the values of
0758    * neighboring samples.  If Ra is the sample immediately to the left of the
0759    * current sample, Rb is the sample immediately above the current sample, and
0760    * Rc is the sample diagonally to the left and above the current sample, then
0761    * the relationship between the predictor selection value and the predictor
0762    * is as follows:
0763    *
0764    * PSV | Predictor
0765    * ----|----------
0766    * 1   | Ra
0767    * 2   | Rb
0768    * 3   | Rc
0769    * 4   | Ra + Rb – Rc
0770    * 5   | Ra + (Rb – Rc) / 2
0771    * 6   | Rb + (Ra – Rc) / 2
0772    * 7   | (Ra + Rb) / 2
0773    *
0774    * Predictors 1-3 are 1-dimensional predictors, whereas Predictors 4-7 are
0775    * 2-dimensional predictors.  The best predictor for a particular image
0776    * depends on the image.
0777    *
0778    * @see #TJPARAM_LOSSLESS
0779    */
0780   TJPARAM_LOSSLESSPSV,
0781   /**
0782    * Lossless JPEG point transform (Pt)
0783    *
0784    * **Value**
0785    * - `0` through ***precision*** *- 1*, where ***precision*** is the JPEG
0786    * data precision in bits *[default for compression: `0`]*
0787    *
0788    * A point transform value of `0` is necessary in order to generate a fully
0789    * lossless JPEG image.  (A non-zero point transform value right-shifts the
0790    * input samples by the specified number of bits, which is effectively a form
0791    * of lossy color quantization.)
0792    *
0793    * @see #TJPARAM_LOSSLESS, #TJPARAM_PRECISION
0794    */
0795   TJPARAM_LOSSLESSPT,
0796   /**
0797    * JPEG restart marker interval in MCUs [lossy compression,
0798    * lossless transformation]
0799    *
0800    * The nature of entropy coding is such that a corrupt JPEG image cannot
0801    * be decompressed beyond the point of corruption unless it contains restart
0802    * markers.  A restart marker stops and restarts the entropy coding algorithm
0803    * so that, if a JPEG image is corrupted, decompression can resume at the
0804    * next marker.  Thus, adding more restart markers improves the fault
0805    * tolerance of the JPEG image, but adding too many restart markers can
0806    * adversely affect the compression ratio and performance.
0807    *
0808    * In typical JPEG images, an MCU (Minimum Coded Unit) is the minimum set of
0809    * interleaved "data units" (8x8 DCT blocks if the image is lossy or samples
0810    * if the image is lossless) necessary to represent at least one data unit
0811    * per component.  (For example, an MCU in an interleaved lossy JPEG image
0812    * that uses 4:2:2 subsampling consists of two luminance blocks followed by
0813    * one block for each chrominance component.)  In single-component or
0814    * non-interleaved JPEG images, an MCU is the same as a data unit.
0815    *
0816    * **Value**
0817    * - the number of MCUs between each restart marker *[default: `0` (no
0818    * restart markers)]*
0819    *
0820    * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTROWS to 0.
0821    */
0822   TJPARAM_RESTARTBLOCKS,
0823   /**
0824    * JPEG restart marker interval in MCU rows [compression,
0825    * lossless transformation]
0826    *
0827    * See #TJPARAM_RESTARTBLOCKS for a description of restart markers and MCUs.
0828    * An MCU row is a row of MCUs spanning the entire width of the image.
0829    *
0830    * **Value**
0831    * - the number of MCU rows between each restart marker *[default: `0` (no
0832    * restart markers)]*
0833    *
0834    * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTBLOCKS to
0835    * 0.
0836    */
0837   TJPARAM_RESTARTROWS,
0838   /**
0839    * JPEG horizontal pixel density
0840    *
0841    * **Value**
0842    * - The JPEG image has (decompression) or will have (compression) the
0843    * specified horizontal pixel density *[default for compression: `1`]*.
0844    *
0845    * This value is stored in or read from the JPEG header.  It does not affect
0846    * the contents of the JPEG image.  Note that this parameter is set by
0847    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0848    * density information, and the value of this parameter is stored to a
0849    * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
0850    * is `2`.
0851    *
0852    * This parameter has no effect unless the JPEG colorspace (see
0853    * #TJPARAM_COLORSPACE) is #TJCS_YCbCr or #TJCS_GRAY.
0854    *
0855    * @see TJPARAM_DENSITYUNITS
0856    */
0857   TJPARAM_XDENSITY,
0858   /**
0859    * JPEG vertical pixel density
0860    *
0861    * **Value**
0862    * - The JPEG image has (decompression) or will have (compression) the
0863    * specified vertical pixel density *[default for compression: `1`]*.
0864    *
0865    * This value is stored in or read from the JPEG header.  It does not affect
0866    * the contents of the JPEG image.  Note that this parameter is set by
0867    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0868    * density information, and the value of this parameter is stored to a
0869    * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
0870    * is `2`.
0871    *
0872    * This parameter has no effect unless the JPEG colorspace (see
0873    * #TJPARAM_COLORSPACE) is #TJCS_YCbCr or #TJCS_GRAY.
0874    *
0875    * @see TJPARAM_DENSITYUNITS
0876    */
0877   TJPARAM_YDENSITY,
0878   /**
0879    * JPEG pixel density units
0880    *
0881    * **Value**
0882    * - `0` *[default for compression]* The pixel density of the JPEG image is
0883    * expressed (decompression) or will be expressed (compression) in unknown
0884    * units.
0885    * - `1` The pixel density of the JPEG image is expressed (decompression) or
0886    * will be expressed (compression) in units of pixels/inch.
0887    * - `2` The pixel density of the JPEG image is expressed (decompression) or
0888    * will be expressed (compression) in units of pixels/cm.
0889    *
0890    * This value is stored in or read from the JPEG header.  It does not affect
0891    * the contents of the JPEG image.  Note that this parameter is set by
0892    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0893    * density information, and the value of this parameter is stored to a
0894    * Windows BMP file by #tj3SaveImage8() if the value is `2`.
0895    *
0896    * This parameter has no effect unless the JPEG colorspace (see
0897    * #TJPARAM_COLORSPACE) is #TJCS_YCbCr or #TJCS_GRAY.
0898    *
0899    * @see TJPARAM_XDENSITY, TJPARAM_YDENSITY
0900    */
0901   TJPARAM_DENSITYUNITS,
0902   /**
0903    * Memory limit for intermediate buffers
0904    *
0905    * **Value**
0906    * - the maximum amount of memory (in megabytes) that will be allocated for
0907    * intermediate buffers, which are used with progressive JPEG compression and
0908    * decompression, Huffman table optimization, lossless JPEG compression, and
0909    * lossless transformation *[default: `0` (no limit)]*
0910    */
0911   TJPARAM_MAXMEMORY,
0912   /**
0913    * Image size limit [decompression, lossless transformation, packed-pixel
0914    * image loading]
0915    *
0916    * Setting this parameter causes the decompression, transform, and image
0917    * loading functions to return an error if the number of pixels in the source
0918    * image exceeds the specified limit.  This allows security-critical
0919    * applications to guard against excessive memory consumption.
0920    *
0921    * **Value**
0922    * - maximum number of pixels that the decompression, transform, and image
0923    * loading functions will process *[default: `0` (no limit)]*
0924    */
0925   TJPARAM_MAXPIXELS,
0926   /**
0927    * Marker copying behavior [decompression, lossless transformation]
0928    *
0929    * **Value [lossless transformation]**
0930    * - `0` Do not copy any extra markers (including comments, JFIF thumbnails,
0931    * Exif data, and ICC profile data) from the source image to the destination
0932    * image.
0933    * - `1` Do not copy any extra markers, except comment (COM) markers, from
0934    * the source image to the destination image.
0935    * - `2` *[default]* Copy all extra markers from the source image to the
0936    * destination image.
0937    * - `3` Copy all extra markers, except ICC profile data (APP2 markers), from
0938    * the source image to the destination image.
0939    * - `4` Do not copy any extra markers, except ICC profile data (APP2
0940    * markers), from the source image to the destination image.
0941    *
0942    * #TJXOPT_COPYNONE overrides this parameter for a particular transform.
0943    * This parameter overrides any ICC profile that was previously associated
0944    * with the TurboJPEG instance using #tj3SetICCProfile().
0945    *
0946    * When decompressing, #tj3DecompressHeader() extracts the ICC profile from a
0947    * JPEG image if this parameter is set to `2` or `4`.  #tj3GetICCProfile()
0948    * can then be used to retrieve the profile.
0949    */
0950   TJPARAM_SAVEMARKERS
0951 };
0952 
0953 
0954 /**
0955  * The number of error codes
0956  */
0957 #define TJ_NUMERR  2
0958 
0959 /**
0960  * Error codes
0961  */
0962 enum TJERR {
0963   /**
0964    * The error was non-fatal and recoverable, but the destination image may
0965    * still be corrupt.
0966    */
0967   TJERR_WARNING,
0968   /**
0969    * The error was fatal and non-recoverable.
0970    */
0971   TJERR_FATAL
0972 };
0973 
0974 
0975 /**
0976  * The number of transform operations
0977  */
0978 #define TJ_NUMXOP  8
0979 
0980 /**
0981  * Transform operations for #tj3Transform()
0982  */
0983 enum TJXOP {
0984   /**
0985    * Do not transform the position of the image pixels.
0986    */
0987   TJXOP_NONE,
0988   /**
0989    * Flip (mirror) image horizontally.  This transform is imperfect if there
0990    * are any partial iMCUs on the right edge (see #TJXOPT_PERFECT.)
0991    */
0992   TJXOP_HFLIP,
0993   /**
0994    * Flip (mirror) image vertically.  This transform is imperfect if there are
0995    * any partial iMCUs on the bottom edge (see #TJXOPT_PERFECT.)
0996    */
0997   TJXOP_VFLIP,
0998   /**
0999    * Transpose image (flip/mirror along upper left to lower right axis.)  This
1000    * transform is always perfect.
1001    */
1002   TJXOP_TRANSPOSE,
1003   /**
1004    * Transverse transpose image (flip/mirror along upper right to lower left
1005    * axis.)  This transform is imperfect if there are any partial iMCUs in the
1006    * image (see #TJXOPT_PERFECT.)
1007    */
1008   TJXOP_TRANSVERSE,
1009   /**
1010    * Rotate image clockwise by 90 degrees.  This transform is imperfect if
1011    * there are any partial iMCUs on the bottom edge (see #TJXOPT_PERFECT.)
1012    */
1013   TJXOP_ROT90,
1014   /**
1015    * Rotate image 180 degrees.  This transform is imperfect if there are any
1016    * partial iMCUs in the image (see #TJXOPT_PERFECT.)
1017    */
1018   TJXOP_ROT180,
1019   /**
1020    * Rotate image counter-clockwise by 90 degrees.  This transform is imperfect
1021    * if there are any partial iMCUs on the right edge (see #TJXOPT_PERFECT.)
1022    */
1023   TJXOP_ROT270
1024 };
1025 
1026 
1027 /**
1028  * This option causes #tj3Transform() to return an error if the transform is
1029  * not perfect.  Lossless transforms operate on iMCUs, the size of which
1030  * depends on the level of chrominance subsampling used (see #tjMCUWidth and
1031  * #tjMCUHeight.)  If the image's width or height is not evenly divisible by
1032  * the iMCU size, then there will be partial iMCUs on the right and/or bottom
1033  * edges.  It is not possible to move these partial iMCUs to the top or left of
1034  * the image, so any transform that would require that is "imperfect."  If this
1035  * option is not specified, then any partial iMCUs that cannot be transformed
1036  * will be left in place, which will create odd-looking strips on the right or
1037  * bottom edge of the image.
1038  */
1039 #define TJXOPT_PERFECT  (1 << 0)
1040 /**
1041  * Discard any partial iMCUs that cannot be transformed.
1042  */
1043 #define TJXOPT_TRIM  (1 << 1)
1044 /**
1045  * Enable lossless cropping.  See #tj3Transform() for more information.
1046  */
1047 #define TJXOPT_CROP  (1 << 2)
1048 /**
1049  * Discard the color data in the source image, and generate a grayscale
1050  * destination image.
1051  */
1052 #define TJXOPT_GRAY  (1 << 3)
1053 /**
1054  * Do not generate a destination image.  (This can be used in conjunction with
1055  * a custom filter to capture the transformed DCT coefficients without
1056  * transcoding them.)
1057  */
1058 #define TJXOPT_NOOUTPUT  (1 << 4)
1059 /**
1060  * Generate a progressive destination image instead of a single-scan
1061  * destination image.  Progressive JPEG images generally have better
1062  * compression ratios than single-scan JPEG images (much better if the image
1063  * has large areas of solid color), but progressive JPEG decompression is
1064  * considerably slower than single-scan JPEG decompression.  Can be combined
1065  * with #TJXOPT_ARITHMETIC.  Implies #TJXOPT_OPTIMIZE unless #TJXOPT_ARITHMETIC
1066  * is also specified.
1067  */
1068 #define TJXOPT_PROGRESSIVE  (1 << 5)
1069 /**
1070  * Do not copy any extra markers (including Exif and ICC profile data) from the
1071  * source image to the destination image.
1072  */
1073 #define TJXOPT_COPYNONE  (1 << 6)
1074 /**
1075  * Enable arithmetic entropy coding in the destination image.  Arithmetic
1076  * entropy coding generally improves compression relative to Huffman entropy
1077  * coding (the default), but it reduces decompression performance considerably.
1078  * Can be combined with #TJXOPT_PROGRESSIVE.
1079  */
1080 #define TJXOPT_ARITHMETIC  (1 << 7)
1081 /**
1082  * Enable Huffman table optimization for the destination image.  Huffman table
1083  * optimization improves compression slightly (generally 5% or less.)
1084  */
1085 #define TJXOPT_OPTIMIZE  (1 << 8)
1086 
1087 
1088 /**
1089  * Scaling factor
1090  */
1091 typedef struct {
1092   /**
1093    * Numerator
1094    */
1095   int num;
1096   /**
1097    * Denominator
1098    */
1099   int denom;
1100 } tjscalingfactor;
1101 
1102 /**
1103  * Cropping region
1104  */
1105 typedef struct {
1106   /**
1107    * The left boundary of the cropping region.  For lossless transformation,
1108    * this must be evenly divisible by the iMCU width (see #tjMCUWidth) of the
1109    * destination image.  For decompression, this must be evenly divisible by
1110    * the scaled iMCU width of the source image.
1111    */
1112   int x;
1113   /**
1114    * The upper boundary of the cropping region.  For lossless transformation,
1115    * this must be evenly divisible by the iMCU height (see #tjMCUHeight) of the
1116    * destination image.
1117    */
1118   int y;
1119   /**
1120    * The width of the cropping region.  Setting this to 0 is the equivalent of
1121    * setting it to the width of the source JPEG image - x.
1122    */
1123   int w;
1124   /**
1125    * The height of the cropping region.  Setting this to 0 is the equivalent of
1126    * setting it to the height of the source JPEG image - y.
1127    */
1128   int h;
1129 } tjregion;
1130 
1131 /**
1132  * A #tjregion structure that specifies no cropping
1133  */
1134 static const tjregion TJUNCROPPED = { 0, 0, 0, 0 };
1135 
1136 /**
1137  * Lossless transform
1138  */
1139 typedef struct tjtransform {
1140   /**
1141    * Cropping region
1142    */
1143   tjregion r;
1144   /**
1145    * One of the @ref TJXOP "transform operations"
1146    */
1147   int op;
1148   /**
1149    * The bitwise OR of one of more of the @ref TJXOPT_ARITHMETIC
1150    * "transform options"
1151    */
1152   int options;
1153   /**
1154    * Arbitrary data that can be accessed within the body of the callback
1155    * function
1156    */
1157   void *data;
1158   /**
1159    * A callback function that can be used to modify the DCT coefficients after
1160    * they are losslessly transformed but before they are transcoded to a new
1161    * JPEG image.  This allows for custom filters or other transformations to be
1162    * applied in the frequency domain.
1163    *
1164    * @param coeffs pointer to an array of transformed DCT coefficients.  (NOTE:
1165    * This pointer is not guaranteed to be valid once the callback returns, so
1166    * applications wishing to hand off the DCT coefficients to another function
1167    * or library should make a copy of them within the body of the callback.)
1168    *
1169    * @param arrayRegion #tjregion structure containing the width and height of
1170    * the array pointed to by `coeffs` as well as its offset relative to the
1171    * component plane.  TurboJPEG implementations may choose to split each
1172    * component plane into multiple DCT coefficient arrays and call the callback
1173    * function once for each array.
1174    *
1175    * @param planeRegion #tjregion structure containing the width and height of
1176    * the component plane to which `coeffs` belongs
1177    *
1178    * @param componentID ID number of the component plane to which `coeffs`
1179    * belongs.  (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
1180    * typical JPEG images.)
1181    *
1182    * @param transformID ID number of the transformed image to which `coeffs`
1183    * belongs.  This is the same as the index of the transform in the
1184    * `transforms` array that was passed to #tj3Transform().
1185    *
1186    * @param transform a pointer to a #tjtransform structure that specifies the
1187    * parameters and/or cropping region for this transform
1188    *
1189    * @return 0 if the callback was successful, or -1 if an error occurred.
1190    */
1191   int (*customFilter) (short *coeffs, tjregion arrayRegion,
1192                        tjregion planeRegion, int componentID, int transformID,
1193                        struct tjtransform *transform);
1194 } tjtransform;
1195 
1196 /**
1197  * TurboJPEG instance handle
1198  */
1199 typedef void *tjhandle;
1200 
1201 
1202 /**
1203  * Compute the scaled value of `dimension` using the given scaling factor.
1204  * This macro performs the integer equivalent of `ceil(dimension *
1205  * scalingFactor)`.
1206  */
1207 #define TJSCALED(dimension, scalingFactor) \
1208   (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
1209    scalingFactor.denom)
1210 
1211 /**
1212  * A #tjscalingfactor structure that specifies a scaling factor of 1/1 (no
1213  * scaling)
1214  */
1215 static const tjscalingfactor TJUNSCALED = { 1, 1 };
1216 
1217 
1218 #ifdef __cplusplus
1219 extern "C" {
1220 #endif
1221 
1222 
1223 /**
1224  * Create a new TurboJPEG instance.
1225  *
1226  * @param initType one of the @ref TJINIT "initialization options"
1227  *
1228  * @return a handle to the newly-created instance, or NULL if an error occurred
1229  * (see #tj3GetErrorStr().)
1230  */
1231 DLLEXPORT tjhandle tj3Init(int initType);
1232 
1233 
1234 /**
1235  * Destroy a TurboJPEG instance.
1236  *
1237  * @param handle handle to a TurboJPEG instance.  If the handle is NULL, then
1238  * this function has no effect.
1239  */
1240 DLLEXPORT void tj3Destroy(tjhandle handle);
1241 
1242 
1243 /**
1244  * Returns a descriptive error message explaining why the last command failed.
1245  *
1246  * @param handle handle to a TurboJPEG instance, or NULL if the error was
1247  * generated by a global function (but note that retrieving the error message
1248  * for a global function is thread-safe only on platforms that support
1249  * thread-local storage.)
1250  *
1251  * @return a descriptive error message explaining why the last command failed.
1252  */
1253 DLLEXPORT char *tj3GetErrorStr(tjhandle handle);
1254 
1255 
1256 /**
1257  * Returns a code indicating the severity of the last error.  See
1258  * @ref TJERR "Error codes".
1259  *
1260  * @param handle handle to a TurboJPEG instance
1261  *
1262  * @return a code indicating the severity of the last error.  See
1263  * @ref TJERR "Error codes".
1264  */
1265 DLLEXPORT int tj3GetErrorCode(tjhandle handle);
1266 
1267 
1268 /**
1269  * Set the value of a parameter.
1270  *
1271  * @param handle handle to a TurboJPEG instance
1272  *
1273  * @param param one of the @ref TJPARAM "parameters"
1274  *
1275  * @param value value of the parameter (refer to @ref TJPARAM
1276  * "parameter documentation")
1277  *
1278  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1279  */
1280 DLLEXPORT int tj3Set(tjhandle handle, int param, int value);
1281 
1282 
1283 /**
1284  * Get the value of a parameter.
1285  *
1286  * @param handle handle to a TurboJPEG instance
1287  *
1288  * @param param one of the @ref TJPARAM "parameters"
1289  *
1290  * @return the value of the specified parameter, or -1 if the value is unknown.
1291  */
1292 DLLEXPORT int tj3Get(tjhandle handle, int param);
1293 
1294 
1295 /**
1296  * Allocate a byte buffer for use with TurboJPEG.  You should always use this
1297  * function to allocate the JPEG destination buffer(s) for the compression and
1298  * transform functions unless you are disabling automatic buffer (re)allocation
1299  * (by setting #TJPARAM_NOREALLOC.)
1300  *
1301  * @param bytes the number of bytes to allocate
1302  *
1303  * @return a pointer to a newly-allocated buffer with the specified number of
1304  * bytes.
1305  *
1306  * @see tj3Free()
1307  */
1308 DLLEXPORT void *tj3Alloc(size_t bytes);
1309 
1310 
1311 /**
1312  * Free a byte buffer previously allocated by TurboJPEG.  You should always use
1313  * this function to free JPEG destination buffer(s) that were automatically
1314  * (re)allocated by the compression and transform functions or that were
1315  * manually allocated using #tj3Alloc().
1316  *
1317  * @param buffer address of the buffer to free.  If the address is NULL, then
1318  * this function has no effect.
1319  *
1320  * @see tj3Alloc()
1321  */
1322 DLLEXPORT void tj3Free(void *buffer);
1323 
1324 
1325 /**
1326  * The maximum size of the buffer (in bytes) required to hold a JPEG image with
1327  * the given parameters.  The number of bytes returned by this function is
1328  * larger than the size of the uncompressed source image.  The reason for this
1329  * is that the JPEG format uses 16-bit coefficients, so it is possible for a
1330  * very high-quality source image with very high-frequency content to expand
1331  * rather than compress when converted to the JPEG format.  Such images
1332  * represent very rare corner cases, but since there is no way to predict the
1333  * size of a JPEG image prior to compression, the corner cases have to be
1334  * handled.
1335  *
1336  * @param width width (in pixels) of the image
1337  *
1338  * @param height height (in pixels) of the image
1339  *
1340  * @param jpegSubsamp the level of chrominance subsampling to be used when
1341  * generating the JPEG image (see @ref TJSAMP
1342  * "Chrominance subsampling options".)  #TJSAMP_UNKNOWN is treated like
1343  * #TJSAMP_444, since a buffer large enough to hold a JPEG image with no
1344  * subsampling should also be large enough to hold a JPEG image with an
1345  * arbitrary level of subsampling.  Note that lossless JPEG images always
1346  * use #TJSAMP_444.
1347  *
1348  * @return the maximum size of the buffer (in bytes) required to hold the
1349  * image, or 0 if the arguments are out of bounds.
1350  */
1351 DLLEXPORT size_t tj3JPEGBufSize(int width, int height, int jpegSubsamp);
1352 
1353 
1354 /**
1355  * The size of the buffer (in bytes) required to hold a unified planar YUV
1356  * image with the given parameters.
1357  *
1358  * @param width width (in pixels) of the image
1359  *
1360  * @param align row alignment (in bytes) of the image (must be a power of 2.)
1361  * Setting this parameter to n specifies that each row in each plane of the
1362  * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
1363  *
1364  * @param height height (in pixels) of the image
1365  *
1366  * @param subsamp level of chrominance subsampling in the image (see
1367  * @ref TJSAMP "Chrominance subsampling options".)
1368  *
1369  * @return the size of the buffer (in bytes) required to hold the image, or 0
1370  * if the arguments are out of bounds.
1371  */
1372 DLLEXPORT size_t tj3YUVBufSize(int width, int align, int height, int subsamp);
1373 
1374 
1375 /**
1376  * The size of the buffer (in bytes) required to hold a YUV image plane with
1377  * the given parameters.
1378  *
1379  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1380  *
1381  * @param width width (in pixels) of the YUV image.  NOTE: This is the width of
1382  * the whole image, not the plane width.
1383  *
1384  * @param stride bytes per row in the image plane.  Setting this to 0 is the
1385  * equivalent of setting it to the plane width.
1386  *
1387  * @param height height (in pixels) of the YUV image.  NOTE: This is the height
1388  * of the whole image, not the plane height.
1389  *
1390  * @param subsamp level of chrominance subsampling in the image (see
1391  * @ref TJSAMP "Chrominance subsampling options".)
1392  *
1393  * @return the size of the buffer (in bytes) required to hold the YUV image
1394  * plane, or 0 if the arguments are out of bounds.
1395  */
1396 DLLEXPORT size_t tj3YUVPlaneSize(int componentID, int width, int stride,
1397                                  int height, int subsamp);
1398 
1399 
1400 /**
1401  * The plane width of a YUV image plane with the given parameters.  Refer to
1402  * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
1403  *
1404  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1405  *
1406  * @param width width (in pixels) of the YUV image
1407  *
1408  * @param subsamp level of chrominance subsampling in the image (see
1409  * @ref TJSAMP "Chrominance subsampling options".)
1410  *
1411  * @return the plane width of a YUV image plane with the given parameters, or 0
1412  * if the arguments are out of bounds.
1413  */
1414 DLLEXPORT int tj3YUVPlaneWidth(int componentID, int width, int subsamp);
1415 
1416 
1417 /**
1418  * The plane height of a YUV image plane with the given parameters.  Refer to
1419  * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
1420  *
1421  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1422  *
1423  * @param height height (in pixels) of the YUV image
1424  *
1425  * @param subsamp level of chrominance subsampling in the image (see
1426  * @ref TJSAMP "Chrominance subsampling options".)
1427  *
1428  * @return the plane height of a YUV image plane with the given parameters, or
1429  * 0 if the arguments are out of bounds.
1430  */
1431 DLLEXPORT int tj3YUVPlaneHeight(int componentID, int height, int subsamp);
1432 
1433 
1434 /**
1435  * Embed an ICC (International Color Consortium) color management profile in
1436  * JPEG images generated by subsequent compression and lossless transformation
1437  * operations.
1438  *
1439  * @param handle handle to a TurboJPEG instance that has been initialized for
1440  * compression
1441  *
1442  * @param iccBuf pointer to a byte buffer containing an ICC profile.  A copy is
1443  * made of the ICC profile, so this buffer can be freed or reused as soon as
1444  * this function returns.  Setting this parameter to NULL or setting `iccSize`
1445  * to 0 removes any ICC profile that was previously associated with the
1446  * TurboJPEG instance.
1447  *
1448  * @param iccSize size of the ICC profile (in bytes.)  Setting this parameter
1449  * to 0 or setting `iccBuf` to NULL removes any ICC profile that was previously
1450  * associated with the TurboJPEG instance.
1451  *
1452  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1453  */
1454 DLLEXPORT int tj3SetICCProfile(tjhandle handle, unsigned char *iccBuf,
1455                                size_t iccSize);
1456 
1457 
1458 /**
1459  * Compress a packed-pixel RGB, grayscale, or CMYK image with 2 to 8 bits of
1460  * data precision per sample into a JPEG image with the same data precision.
1461  *
1462  * @param handle handle to a TurboJPEG instance that has been initialized for
1463  * compression
1464  *
1465  * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1466  * or CMYK source image to be compressed.  This buffer should normally be
1467  * `pitch * height` samples in size.  However, you can also use this parameter
1468  * to compress from a specific region of a larger buffer.  The data precision
1469  * of the source image (from 2 to 8 bits per sample) can be specified using
1470  * #TJPARAM_PRECISION and defaults to 8 if #TJPARAM_PRECISION is unset or out
1471  * of range.
1472  *
1473  * @param width width (in pixels) of the source image
1474  *
1475  * @param pitch samples per row in the source image.  Normally this should be
1476  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1477  * (Setting this parameter to 0 is the equivalent of setting it to
1478  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1479  * parameter to specify the row alignment/padding of the source image, to skip
1480  * rows, or to compress from a specific region of a larger buffer.
1481  *
1482  * @param height height (in pixels) of the source image
1483  *
1484  * @param pixelFormat pixel format of the source image (see @ref TJPF
1485  * "Pixel formats".)
1486  *
1487  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1488  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1489  * accommodate the size of the JPEG image.  Thus, you can choose to:
1490  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1491  * let TurboJPEG grow the buffer as needed,
1492  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1493  * or
1494  * -# pre-allocate the buffer to a "worst case" size determined by calling
1495  * #tj3JPEGBufSize() and adding the return value to the size of the ICC profile
1496  * (if any) that was previously associated with the TurboJPEG instance (see
1497  * #tj3SetICCProfile().)  This should ensure that the buffer never has to be
1498  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1499  * .
1500  * Unless you have set #TJPARAM_NOREALLOC, you should always check `*jpegBuf`
1501  * upon return from this function, as it may have changed.
1502  *
1503  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1504  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1505  * should be set to the size of the buffer.  Otherwise, `*jpegSize` is
1506  * ignored.  If `*jpegBuf` points to a JPEG buffer that is being reused from a
1507  * previous call to one of the JPEG compression functions, then `*jpegSize` is
1508  * also ignored.  Upon return, `*jpegSize` will contain the size of the JPEG
1509  * image (in bytes.)
1510  *
1511  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1512  * and #tj3GetErrorCode().)
1513  */
1514 DLLEXPORT int tj3Compress8(tjhandle handle, const unsigned char *srcBuf,
1515                            int width, int pitch, int height, int pixelFormat,
1516                            unsigned char **jpegBuf, size_t *jpegSize);
1517 
1518 /**
1519  * Compress a packed-pixel RGB, grayscale, or CMYK image with 9 to 12 bits of
1520  * data precision per sample into a JPEG image with the same data precision.
1521  *
1522  * @param handle handle to a TurboJPEG instance that has been initialized for
1523  * compression
1524  *
1525  * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1526  * or CMYK source image to be compressed.  This buffer should normally be
1527  * `pitch * height` samples in size.  However, you can also use this parameter
1528  * to compress from a specific region of a larger buffer.  The data precision
1529  * of the source image (from 9 to 12 bits per sample) can be specified using
1530  * #TJPARAM_PRECISION and defaults to 12 if #TJPARAM_PRECISION is unset or out
1531  * of range.
1532  *
1533  * @param width width (in pixels) of the source image
1534  *
1535  * @param pitch samples per row in the source image.  Normally this should be
1536  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1537  * (Setting this parameter to 0 is the equivalent of setting it to
1538  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1539  * parameter to specify the row alignment/padding of the source image, to skip
1540  * rows, or to compress from a specific region of a larger buffer.
1541  *
1542  * @param height height (in pixels) of the source image
1543  *
1544  * @param pixelFormat pixel format of the source image (see @ref TJPF
1545  * "Pixel formats".)
1546  *
1547  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1548  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1549  * accommodate the size of the JPEG image.  Thus, you can choose to:
1550  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1551  * let TurboJPEG grow the buffer as needed,
1552  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1553  * or
1554  * -# pre-allocate the buffer to a "worst case" size determined by calling
1555  * #tj3JPEGBufSize() and adding the return value to the size of the ICC profile
1556  * (if any) that was previously associated with the TurboJPEG instance (see
1557  * #tj3SetICCProfile().)  This should ensure that the buffer never has to be
1558  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1559  * .
1560  * Unless you have set #TJPARAM_NOREALLOC, you should always check `*jpegBuf`
1561  * upon return from this function, as it may have changed.
1562  *
1563  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1564  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1565  * should be set to the size of the buffer.  Otherwise, `*jpegSize` is
1566  * ignored.  If `*jpegBuf` points to a JPEG buffer that is being reused from a
1567  * previous call to one of the JPEG compression functions, then `*jpegSize` is
1568  * also ignored.  Upon return, `*jpegSize` will contain the size of the JPEG
1569  * image (in bytes.)
1570  *
1571  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1572  * and #tj3GetErrorCode().)
1573  */
1574 DLLEXPORT int tj3Compress12(tjhandle handle, const short *srcBuf, int width,
1575                             int pitch, int height, int pixelFormat,
1576                             unsigned char **jpegBuf, size_t *jpegSize);
1577 
1578 /**
1579  * Compress a packed-pixel RGB, grayscale, or CMYK image with 13 to 16 bits of
1580  * data precision per sample into a lossless JPEG image with the same data
1581  * precision.
1582  *
1583  * @param handle handle to a TurboJPEG instance that has been initialized for
1584  * compression
1585  *
1586  * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1587  * or CMYK source image to be compressed.  This buffer should normally be
1588  * `pitch * height` samples in size.  However, you can also use this parameter
1589  * to compress from a specific region of a larger buffer.  The data precision
1590  * of the source image (from 13 to 16 bits per sample) can be specified using
1591  * #TJPARAM_PRECISION and defaults to 16 if #TJPARAM_PRECISION is unset or out
1592  * of range.
1593  *
1594  * @param width width (in pixels) of the source image
1595  *
1596  * @param pitch samples per row in the source image.  Normally this should be
1597  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1598  * (Setting this parameter to 0 is the equivalent of setting it to
1599  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1600  * parameter to specify the row alignment/padding of the source image, to skip
1601  * rows, or to compress from a specific region of a larger buffer.
1602  *
1603  * @param height height (in pixels) of the source image
1604  *
1605  * @param pixelFormat pixel format of the source image (see @ref TJPF
1606  * "Pixel formats".)
1607  *
1608  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1609  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1610  * accommodate the size of the JPEG image.  Thus, you can choose to:
1611  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1612  * let TurboJPEG grow the buffer as needed,
1613  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1614  * or
1615  * -# pre-allocate the buffer to a "worst case" size determined by calling
1616  * #tj3JPEGBufSize() and adding the return value to the size of the ICC profile
1617  * (if any) that was previously associated with the TurboJPEG instance (see
1618  * #tj3SetICCProfile().)  This should ensure that the buffer never has to be
1619  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1620  * .
1621  * Unless you have set #TJPARAM_NOREALLOC, you should always check `*jpegBuf`
1622  * upon return from this function, as it may have changed.
1623  *
1624  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1625  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1626  * should be set to the size of the buffer.  Otherwise, `*jpegSize` is
1627  * ignored.  If `*jpegBuf` points to a JPEG buffer that is being reused from a
1628  * previous call to one of the JPEG compression functions, then `*jpegSize` is
1629  * also ignored.  Upon return, `*jpegSize` will contain the size of the JPEG
1630  * image (in bytes.)
1631  *
1632  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1633  * and #tj3GetErrorCode().)
1634  */
1635 DLLEXPORT int tj3Compress16(tjhandle handle, const unsigned short *srcBuf,
1636                             int width, int pitch, int height, int pixelFormat,
1637                             unsigned char **jpegBuf, size_t *jpegSize);
1638 
1639 
1640 /**
1641  * Compress a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into
1642  * an 8-bit-per-sample lossy @ref TJCS_YCbCr "YCbCr" or
1643  * @ref TJCS_GRAY "grayscale" JPEG image.
1644  *
1645  * @param handle handle to a TurboJPEG instance that has been initialized for
1646  * compression
1647  *
1648  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1649  * (or just a Y plane, if compressing a grayscale image) that contain a YUV
1650  * source image to be compressed.  These planes can be contiguous or
1651  * non-contiguous in memory.  The size of each plane should match the value
1652  * returned by #tj3YUVPlaneSize() for the given image width, height, strides,
1653  * and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer to
1654  * @ref YUVnotes "YUV Image Format Notes" for more details.
1655  *
1656  * @param width width (in pixels) of the source image.  If the width is not an
1657  * even multiple of the iMCU width (see #tjMCUWidth), then an intermediate
1658  * buffer copy will be performed.
1659  *
1660  * @param strides an array of integers, each specifying the number of bytes per
1661  * row in the corresponding plane of the YUV source image.  Setting the stride
1662  * for any plane to 0 is the same as setting it to the plane width (see
1663  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1664  * strides for all planes will be set to their respective plane widths.  You
1665  * can adjust the strides in order to specify an arbitrary amount of row
1666  * padding in each plane or to create a JPEG image from a subregion of a larger
1667  * planar YUV image.
1668  *
1669  * @param height height (in pixels) of the source image.  If the height is not
1670  * an even multiple of the iMCU height (see #tjMCUHeight), then an intermediate
1671  * buffer copy will be performed.
1672  *
1673  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1674  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1675  * accommodate the size of the JPEG image.  Thus, you can choose to:
1676  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1677  * let TurboJPEG grow the buffer as needed,
1678  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1679  * or
1680  * -# pre-allocate the buffer to a "worst case" size determined by calling
1681  * #tj3JPEGBufSize() and adding the return value to the size of the ICC profile
1682  * (if any) that was previously associated with the TurboJPEG instance (see
1683  * #tj3SetICCProfile().)  This should ensure that the buffer never has to be
1684  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1685  * .
1686  * Unless you have set #TJPARAM_NOREALLOC, you should always check `*jpegBuf`
1687  * upon return from this function, as it may have changed.
1688  *
1689  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1690  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1691  * should be set to the size of the buffer.  Otherwise, `*jpegSize` is
1692  * ignored.  If `*jpegBuf` points to a JPEG buffer that is being reused from a
1693  * previous call to one of the JPEG compression functions, then `*jpegSize` is
1694  * also ignored.  Upon return, `*jpegSize` will contain the size of the JPEG
1695  * image (in bytes.)
1696  *
1697  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1698  * and #tj3GetErrorCode().)
1699  */
1700 DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle,
1701                                         const unsigned char * const *srcPlanes,
1702                                         int width, const int *strides,
1703                                         int height, unsigned char **jpegBuf,
1704                                         size_t *jpegSize);
1705 
1706 
1707 /**
1708  * Compress an 8-bit-per-sample unified planar YUV image into an
1709  * 8-bit-per-sample lossy @ref TJCS_YCbCr "YCbCr" or @ref TJCS_GRAY "grayscale"
1710  * JPEG image.
1711  *
1712  * @param handle handle to a TurboJPEG instance that has been initialized for
1713  * compression
1714  *
1715  * @param srcBuf pointer to a buffer containing a unified planar YUV source
1716  * image to be compressed.  The size of this buffer should match the value
1717  * returned by #tj3YUVBufSize() for the given image width, height, row
1718  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1719  * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1720  * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1721  *
1722  * @param width width (in pixels) of the source image.  If the width is not an
1723  * even multiple of the iMCU width (see #tjMCUWidth), then an intermediate
1724  * buffer copy will be performed.
1725  *
1726  * @param align row alignment (in bytes) of the source image (must be a power
1727  * of 2.)  Setting this parameter to n indicates that each row in each plane of
1728  * the source image is padded to the nearest multiple of n bytes
1729  * (1 = unpadded.)
1730  *
1731  * @param height height (in pixels) of the source image.  If the height is not
1732  * an even multiple of the iMCU height (see #tjMCUHeight), then an intermediate
1733  * buffer copy will be performed.
1734  *
1735  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1736  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1737  * accommodate the size of the JPEG image.  Thus, you can choose to:
1738  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1739  * let TurboJPEG grow the buffer as needed,
1740  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1741  * or
1742  * -# pre-allocate the buffer to a "worst case" size determined by calling
1743  * #tj3JPEGBufSize() and adding the return value to the size of the ICC profile
1744  * (if any) that was previously associated with the TurboJPEG instance (see
1745  * #tj3SetICCProfile().)  This should ensure that the buffer never has to be
1746  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1747  * .
1748  * Unless you have set #TJPARAM_NOREALLOC, you should always check `*jpegBuf`
1749  * upon return from this function, as it may have changed.
1750  *
1751  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1752  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1753  * should be set to the size of the buffer.  Otherwise, `*jpegSize` is
1754  * ignored.  If `*jpegBuf` points to a JPEG buffer that is being reused from a
1755  * previous call to one of the JPEG compression functions, then `*jpegSize` is
1756  * also ignored.  Upon return, `*jpegSize` will contain the size of the JPEG
1757  * image (in bytes.)
1758  *
1759  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1760  * and #tj3GetErrorCode().)
1761  */
1762 DLLEXPORT int tj3CompressFromYUV8(tjhandle handle,
1763                                   const unsigned char *srcBuf, int width,
1764                                   int align, int height,
1765                                   unsigned char **jpegBuf, size_t *jpegSize);
1766 
1767 
1768 /**
1769  * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into separate
1770  * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes.  This function performs
1771  * color conversion and downsampling (which are accelerated in the
1772  * libjpeg-turbo implementation) but does not execute any of the other steps in
1773  * the JPEG compression process.
1774  *
1775  * @param handle handle to a TurboJPEG instance that has been initialized for
1776  * compression
1777  *
1778  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1779  * source image to be encoded.  This buffer should normally be `pitch * height`
1780  * bytes in size.  However, you can also use this parameter to encode from a
1781  * specific region of a larger buffer.
1782  *
1783  *
1784  * @param width width (in pixels) of the source image
1785  *
1786  * @param pitch bytes per row in the source image.  Normally this should be
1787  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1788  * (Setting this parameter to 0 is the equivalent of setting it to
1789  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1790  * parameter to specify the row alignment/padding of the source image, to skip
1791  * rows, or to encode from a specific region of a larger packed-pixel image.
1792  *
1793  * @param height height (in pixels) of the source image
1794  *
1795  * @param pixelFormat pixel format of the source image (see @ref TJPF
1796  * "Pixel formats".)
1797  *
1798  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1799  * (or just a Y plane, if generating a grayscale image) that will receive the
1800  * encoded image.  These planes can be contiguous or non-contiguous in memory.
1801  * Use #tj3YUVPlaneSize() to determine the appropriate size for each plane
1802  * based on the image width, height, strides, and level of chrominance
1803  * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
1804  * "YUV Image Format Notes" for more details.
1805  *
1806  * @param strides an array of integers, each specifying the number of bytes per
1807  * row in the corresponding plane of the YUV image.  Setting the stride for any
1808  * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
1809  * "YUV Image Format Notes".)  If `strides` is NULL, then the strides for all
1810  * planes will be set to their respective plane widths.  You can adjust the
1811  * strides in order to add an arbitrary amount of row padding to each plane or
1812  * to encode an RGB or grayscale image into a subregion of a larger planar YUV
1813  * image.
1814  *
1815  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1816  * and #tj3GetErrorCode().)
1817  */
1818 DLLEXPORT int tj3EncodeYUVPlanes8(tjhandle handle, const unsigned char *srcBuf,
1819                                   int width, int pitch, int height,
1820                                   int pixelFormat, unsigned char **dstPlanes,
1821                                   int *strides);
1822 
1823 
1824 /**
1825  * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into an
1826  * 8-bit-per-sample unified planar YUV image.  This function performs color
1827  * conversion and downsampling (which are accelerated in the libjpeg-turbo
1828  * implementation) but does not execute any of the other steps in the JPEG
1829  * compression process.
1830  *
1831  * @param handle handle to a TurboJPEG instance that has been initialized for
1832  * compression
1833  *
1834  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1835  * source image to be encoded.  This buffer should normally be `pitch * height`
1836  * bytes in size.  However, you can also use this parameter to encode from a
1837  * specific region of a larger buffer.
1838  *
1839  * @param width width (in pixels) of the source image
1840  *
1841  * @param pitch bytes per row in the source image.  Normally this should be
1842  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1843  * (Setting this parameter to 0 is the equivalent of setting it to
1844  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1845  * parameter to specify the row alignment/padding of the source image, to skip
1846  * rows, or to encode from a specific region of a larger packed-pixel image.
1847  *
1848  * @param height height (in pixels) of the source image
1849  *
1850  * @param pixelFormat pixel format of the source image (see @ref TJPF
1851  * "Pixel formats".)
1852  *
1853  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1854  * image.  Use #tj3YUVBufSize() to determine the appropriate size for this
1855  * buffer based on the image width, height, row alignment, and level of
1856  * chrominance subsampling (see #TJPARAM_SUBSAMP.)  The Y, U (Cb), and V (Cr)
1857  * image planes will be stored sequentially in the buffer.  (Refer to
1858  * @ref YUVnotes "YUV Image Format Notes".)
1859  *
1860  * @param align row alignment (in bytes) of the YUV image (must be a power of
1861  * 2.)  Setting this parameter to n will cause each row in each plane of the
1862  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1863  * To generate images suitable for X Video, `align` should be set to 4.
1864  *
1865  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1866  * and #tj3GetErrorCode().)
1867  */
1868 DLLEXPORT int tj3EncodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1869                             int width, int pitch, int height, int pixelFormat,
1870                             unsigned char *dstBuf, int align);
1871 
1872 
1873 /**
1874  * Retrieve information about a JPEG image without decompressing it, or prime
1875  * the decompressor with quantization and Huffman tables.  If a JPEG image is
1876  * passed to this function, then the @ref TJPARAM "parameters" that describe
1877  * the JPEG image will be set when the function returns.  If a JPEG image is
1878  * passed to this function and #TJPARAM_SAVEMARKERS is set to `2` or `4`, then
1879  * the ICC profile (if any) will be extracted from the JPEG image.
1880  * (#tj3GetICCProfile() can then be used to retrieve the profile.)
1881  *
1882  * @param handle handle to a TurboJPEG instance that has been initialized for
1883  * decompression
1884  *
1885  * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
1886  * "abbreviated table specification" (AKA "tables-only") datastream.  Passing a
1887  * tables-only datastream to this function primes the decompressor with
1888  * quantization and Huffman tables that can be used when decompressing
1889  * subsequent "abbreviated image" datastreams.  This is useful, for instance,
1890  * when decompressing video streams in which all frames share the same
1891  * quantization and Huffman tables.
1892  *
1893  * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1894  *
1895  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1896  * and #tj3GetErrorCode().)
1897  */
1898 DLLEXPORT int tj3DecompressHeader(tjhandle handle,
1899                                   const unsigned char *jpegBuf,
1900                                   size_t jpegSize);
1901 
1902 
1903 /**
1904  * Retrieve the ICC (International Color Consortium) color management profile
1905  * (if any) that was previously extracted from a JPEG image.
1906  *
1907  * @note To extract the ICC profile from a JPEG image, call
1908  * #tj3DecompressHeader() with #TJPARAM_SAVEMARKERS set to `2` or `4`.  Once
1909  * the ICC profile is retrieved, it must be re-extracted before it can be
1910  * retrieved again.
1911  *
1912  * @param handle handle to a TurboJPEG instance that has been initialized for
1913  * decompression
1914  *
1915  * @param iccBuf address of a pointer to a byte buffer.  Upon return:
1916  * - If `iccBuf` is not NULL and there is an ICC profile to retrieve, then
1917  * `*iccBuf` will point to a byte buffer containing the ICC profile.  This
1918  * buffer should be freed using #tj3Free().
1919  * - If `iccBuf` is not NULL and there is no ICC profile to retrieve, then
1920  * `*iccBuf` will be NULL.
1921  * - If `iccBuf` is NULL, then only the ICC profile size will be retrieved, and
1922  * the ICC profile can be retrieved later.
1923  *
1924  * @param iccSize address of a size_t variable.  Upon return, the variable will
1925  * contain the ICC profile size (or 0 if there is no ICC profile to retrieve.)
1926  *
1927  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1928  * and #tj3GetErrorCode().)
1929  */
1930 DLLEXPORT int tj3GetICCProfile(tjhandle handle, unsigned char **iccBuf,
1931                                size_t *iccSize);
1932 
1933 
1934 /**
1935  * Returns a list of fractional scaling factors that the JPEG decompressor
1936  * supports.
1937  *
1938  * @param numScalingFactors pointer to an integer variable that will receive
1939  * the number of elements in the list
1940  *
1941  * @return a pointer to a list of fractional scaling factors, or NULL if an
1942  * error is encountered (see #tj3GetErrorStr().)
1943  */
1944 DLLEXPORT tjscalingfactor *tj3GetScalingFactors(int *numScalingFactors);
1945 
1946 
1947 /**
1948  * Set the scaling factor for subsequent lossy decompression operations.
1949  *
1950  * @param handle handle to a TurboJPEG instance that has been initialized for
1951  * decompression
1952  *
1953  * @param scalingFactor #tjscalingfactor structure that specifies a fractional
1954  * scaling factor that the decompressor supports (see #tj3GetScalingFactors()),
1955  * or <tt>#TJUNSCALED</tt> for no scaling.  Decompression scaling is a function
1956  * of the IDCT algorithm, so scaling factors are generally limited to multiples
1957  * of 1/8.  If the entire JPEG image will be decompressed, then the width and
1958  * height of the scaled destination image can be determined by calling
1959  * #TJSCALED() with the JPEG width and height (see #TJPARAM_JPEGWIDTH and
1960  * #TJPARAM_JPEGHEIGHT) and the specified scaling factor.  When decompressing
1961  * into a planar YUV image, an intermediate buffer copy will be performed if
1962  * the width or height of the scaled destination image is not an even multiple
1963  * of the iMCU size (see #tjMCUWidth and #tjMCUHeight.)  Note that
1964  * decompression scaling is not available (and the specified scaling factor is
1965  * ignored) when decompressing lossless JPEG images (see #TJPARAM_LOSSLESS),
1966  * since the IDCT algorithm is not used with those images.  Note also that
1967  * #TJPARAM_FASTDCT is ignored when decompression scaling is enabled.
1968  *
1969  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1970  */
1971 DLLEXPORT int tj3SetScalingFactor(tjhandle handle,
1972                                   tjscalingfactor scalingFactor);
1973 
1974 
1975 /**
1976  * Set the cropping region for partially decompressing a lossy JPEG image into
1977  * a packed-pixel image
1978  *
1979  * @param handle handle to a TurboJPEG instance that has been initialized for
1980  * decompression
1981  *
1982  * @param croppingRegion #tjregion structure that specifies a subregion of the
1983  * JPEG image to decompress, or <tt>#TJUNCROPPED</tt> for no cropping.  The
1984  * left boundary of the cropping region must be evenly divisible by the scaled
1985  * iMCU width-- <tt>#TJSCALED(#tjMCUWidth[subsamp], scalingFactor)</tt>, where
1986  * `subsamp` is the level of chrominance subsampling in the JPEG image (see
1987  * #TJPARAM_SUBSAMP) and `scalingFactor` is the decompression scaling factor
1988  * (see #tj3SetScalingFactor().)  The cropping region should be specified
1989  * relative to the scaled image dimensions.  Unless `croppingRegion` is
1990  * <tt>#TJUNCROPPED</tt>, the JPEG header must be read (see
1991  * #tj3DecompressHeader()) prior to calling this function.
1992  *
1993  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1994  */
1995 DLLEXPORT int tj3SetCroppingRegion(tjhandle handle, tjregion croppingRegion);
1996 
1997 
1998 /**
1999  * Decompress a JPEG image with 2 to 8 bits of data precision per sample into a
2000  * packed-pixel RGB, grayscale, or CMYK image with the same data precision.
2001  * The @ref TJPARAM "parameters" that describe the JPEG image will be set when
2002  * this function returns.
2003  *
2004  * @param handle handle to a TurboJPEG instance that has been initialized for
2005  * decompression
2006  *
2007  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
2008  * decompress
2009  *
2010  * @param jpegSize size of the JPEG image (in bytes)
2011  *
2012  * @param dstBuf pointer to a buffer that will receive the packed-pixel
2013  * decompressed image.  This buffer should normally be
2014  * `pitch * destinationHeight` samples in size.  However, you can also use this
2015  * parameter to decompress into a specific region of a larger buffer.  NOTE:
2016  * If the JPEG image is lossy, then `destinationHeight` is either the scaled
2017  * JPEG height (see #TJSCALED(), #TJPARAM_JPEGHEIGHT, and
2018  * #tj3SetScalingFactor()) or the height of the cropping region (see
2019  * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
2020  * `destinationHeight` is the JPEG height.
2021  *
2022  * @param pitch samples per row in the destination image.  Normally this should
2023  * be set to <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>, if the
2024  * destination image should be unpadded.  (Setting this parameter to 0 is the
2025  * equivalent of setting it to
2026  * <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>.)  However, you can
2027  * also use this parameter to specify the row alignment/padding of the
2028  * destination image, to skip rows, or to decompress into a specific region of
2029  * a larger buffer.  NOTE: If the JPEG image is lossy, then `destinationWidth`
2030  * is either the scaled JPEG width (see #TJSCALED(), #TJPARAM_JPEGWIDTH, and
2031  * #tj3SetScalingFactor()) or the width of the cropping region (see
2032  * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
2033  * `destinationWidth` is the JPEG width.
2034  *
2035  * @param pixelFormat pixel format of the destination image (see @ref
2036  * TJPF "Pixel formats".)
2037  *
2038  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2039  * and #tj3GetErrorCode().)
2040  */
2041 DLLEXPORT int tj3Decompress8(tjhandle handle, const unsigned char *jpegBuf,
2042                              size_t jpegSize, unsigned char *dstBuf, int pitch,
2043                              int pixelFormat);
2044 
2045 /**
2046  * Decompress a JPEG image with 9 to 12 bits of data precision per sample into
2047  * a packed-pixel RGB, grayscale, or CMYK image with the same data precision.
2048  *
2049  * \details \copydetails tj3Decompress8()
2050  */
2051 DLLEXPORT int tj3Decompress12(tjhandle handle, const unsigned char *jpegBuf,
2052                               size_t jpegSize, short *dstBuf, int pitch,
2053                               int pixelFormat);
2054 
2055 /**
2056  * Decompress a lossless JPEG image with 13 to 16 bits of data precision per
2057  * sample into a packed-pixel RGB, grayscale, or CMYK image with the same
2058  * data precision.
2059  *
2060  * \details \copydetails tj3Decompress8()
2061  */
2062 DLLEXPORT int tj3Decompress16(tjhandle handle, const unsigned char *jpegBuf,
2063                               size_t jpegSize, unsigned short *dstBuf,
2064                               int pitch, int pixelFormat);
2065 
2066 
2067 /**
2068  * Decompress an 8-bit-per-sample lossy JPEG image into separate
2069  * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes.  This function performs
2070  * JPEG decompression but leaves out the color conversion step, so a planar YUV
2071  * image is generated instead of a packed-pixel image.  The
2072  * @ref TJPARAM "parameters" that describe the JPEG image will be set when this
2073  * function returns.
2074  *
2075  * @param handle handle to a TurboJPEG instance that has been initialized for
2076  * decompression
2077  *
2078  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
2079  * decompress
2080  *
2081  * @param jpegSize size of the JPEG image (in bytes)
2082  *
2083  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
2084  * (or just a Y plane, if decompressing a grayscale image) that will receive
2085  * the decompressed image.  These planes can be contiguous or non-contiguous in
2086  * memory.  Use #tj3YUVPlaneSize() to determine the appropriate size for each
2087  * plane based on the scaled JPEG width and height (see #TJSCALED(),
2088  * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()),
2089  * strides, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer
2090  * to @ref YUVnotes "YUV Image Format Notes" for more details.
2091  *
2092  * @param strides an array of integers, each specifying the number of bytes per
2093  * row in the corresponding plane of the YUV image.  Setting the stride for any
2094  * plane to 0 is the same as setting it to the scaled plane width (see
2095  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
2096  * strides for all planes will be set to their respective scaled plane widths.
2097  * You can adjust the strides in order to add an arbitrary amount of row
2098  * padding to each plane or to decompress the JPEG image into a subregion of a
2099  * larger planar YUV image.
2100  *
2101  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2102  * and #tj3GetErrorCode().)
2103  */
2104 DLLEXPORT int tj3DecompressToYUVPlanes8(tjhandle handle,
2105                                         const unsigned char *jpegBuf,
2106                                         size_t jpegSize,
2107                                         unsigned char **dstPlanes,
2108                                         int *strides);
2109 
2110 
2111 /**
2112  * Decompress an 8-bit-per-sample lossy JPEG image into an 8-bit-per-sample
2113  * unified planar YUV image.  This function performs JPEG decompression but
2114  * leaves out the color conversion step, so a planar YUV image is generated
2115  * instead of a packed-pixel image.  The @ref TJPARAM "parameters" that
2116  * describe the JPEG image will be set when this function returns.
2117  *
2118  * @param handle handle to a TurboJPEG instance that has been initialized for
2119  * decompression
2120  *
2121  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
2122  * decompress
2123  *
2124  * @param jpegSize size of the JPEG image (in bytes)
2125  *
2126  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
2127  * decompressed image.  Use #tj3YUVBufSize() to determine the appropriate size
2128  * for this buffer based on the scaled JPEG width and height (see #TJSCALED(),
2129  * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()), row
2130  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
2131  * Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
2132  * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
2133  *
2134  * @param align row alignment (in bytes) of the YUV image (must be a power of
2135  * 2.)  Setting this parameter to n will cause each row in each plane of the
2136  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
2137  * To generate images suitable for X Video, `align` should be set to 4.
2138  *
2139  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2140  * and #tj3GetErrorCode().)
2141  */
2142 DLLEXPORT int tj3DecompressToYUV8(tjhandle handle,
2143                                   const unsigned char *jpegBuf,
2144                                   size_t jpegSize,
2145                                   unsigned char *dstBuf, int align);
2146 
2147 
2148 /**
2149  * Decode a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into an
2150  * 8-bit-per-sample packed-pixel RGB or grayscale image.  This function
2151  * performs color conversion (which is accelerated in the libjpeg-turbo
2152  * implementation) but does not execute any of the other steps in the JPEG
2153  * decompression process.
2154  *
2155  * @param handle handle to a TurboJPEG instance that has been initialized for
2156  * decompression
2157  *
2158  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
2159  * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
2160  * to be decoded.  These planes can be contiguous or non-contiguous in memory.
2161  * The size of each plane should match the value returned by #tj3YUVPlaneSize()
2162  * for the given image width, height, strides, and level of chrominance
2163  * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
2164  * "YUV Image Format Notes" for more details.
2165  *
2166  * @param strides an array of integers, each specifying the number of bytes per
2167  * row in the corresponding plane of the YUV source image.  Setting the stride
2168  * for any plane to 0 is the same as setting it to the plane width (see
2169  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
2170  * strides for all planes will be set to their respective plane widths.  You
2171  * can adjust the strides in order to specify an arbitrary amount of row
2172  * padding in each plane or to decode a subregion of a larger planar YUV image.
2173  *
2174  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
2175  * image.  This buffer should normally be `pitch * height` bytes in size.
2176  * However, you can also use this parameter to decode into a specific region of
2177  * a larger buffer.
2178  *
2179  * @param width width (in pixels) of the source and destination images
2180  *
2181  * @param pitch bytes per row in the destination image.  Normally this should
2182  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
2183  * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
2184  * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
2185  * also use this parameter to specify the row alignment/padding of the
2186  * destination image, to skip rows, or to decode into a specific region of a
2187  * larger buffer.
2188  *
2189  * @param height height (in pixels) of the source and destination images
2190  *
2191  * @param pixelFormat pixel format of the destination image (see @ref TJPF
2192  * "Pixel formats".)
2193  *
2194  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2195  * and #tj3GetErrorCode().)
2196  */
2197 DLLEXPORT int tj3DecodeYUVPlanes8(tjhandle handle,
2198                                   const unsigned char * const *srcPlanes,
2199                                   const int *strides, unsigned char *dstBuf,
2200                                   int width, int pitch, int height,
2201                                   int pixelFormat);
2202 
2203 
2204 /**
2205  * Decode an 8-bit-per-sample unified planar YUV image into an 8-bit-per-sample
2206  * packed-pixel RGB or grayscale image.  This function performs color
2207  * conversion (which is accelerated in the libjpeg-turbo implementation) but
2208  * does not execute any of the other steps in the JPEG decompression process.
2209  *
2210  * @param handle handle to a TurboJPEG instance that has been initialized for
2211  * decompression
2212  *
2213  * @param srcBuf pointer to a buffer containing a unified planar YUV source
2214  * image to be decoded.  The size of this buffer should match the value
2215  * returned by #tj3YUVBufSize() for the given image width, height, row
2216  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
2217  * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
2218  * source buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
2219  *
2220  * @param align row alignment (in bytes) of the YUV source image (must be a
2221  * power of 2.)  Setting this parameter to n indicates that each row in each
2222  * plane of the YUV source image is padded to the nearest multiple of n bytes
2223  * (1 = unpadded.)
2224  *
2225  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
2226  * image.  This buffer should normally be `pitch * height` bytes in size.
2227  * However, you can also use this parameter to decode into a specific region of
2228  * a larger buffer.
2229  *
2230  * @param width width (in pixels) of the source and destination images
2231  *
2232  * @param pitch bytes per row in the destination image.  Normally this should
2233  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
2234  * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
2235  * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
2236  * also use this parameter to specify the row alignment/padding of the
2237  * destination image, to skip rows, or to decode into a specific region of a
2238  * larger buffer.
2239  *
2240  * @param height height (in pixels) of the source and destination images
2241  *
2242  * @param pixelFormat pixel format of the destination image (see @ref TJPF
2243  * "Pixel formats".)
2244  *
2245  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2246  * and #tj3GetErrorCode().)
2247  */
2248 DLLEXPORT int tj3DecodeYUV8(tjhandle handle, const unsigned char *srcBuf,
2249                             int align, unsigned char *dstBuf, int width,
2250                             int pitch, int height, int pixelFormat);
2251 
2252 
2253 /**
2254  * The maximum size of the buffer (in bytes) required to hold a JPEG image
2255  * transformed with the given transform parameters and/or cropping region.
2256  * This function is a wrapper for #tj3JPEGBufSize() that takes into account
2257  * cropping, transposition of the width and height (which affects the
2258  * destination image dimensions and level of chrominance subsampling),
2259  * grayscale conversion, and the ICC profile (if any) that was previously
2260  * associated with the TurboJPEG instance (see #tj3SetICCProfile()) or
2261  * extracted from the source image (see #tj3GetICCProfile() and
2262  * #TJPARAM_SAVEMARKERS.)  The JPEG header must be read (see
2263  * tj3DecompressHeader()) prior to calling this function.
2264  *
2265  * @param handle handle to a TurboJPEG instance that has been initialized for
2266  * lossless transformation
2267  *
2268  * @param transform pointer to a #tjtransform structure that specifies the
2269  * transform parameters and/or cropping region for the JPEG image.
2270  *
2271  * @return the maximum size of the buffer (in bytes) required to hold the
2272  * transformed image, or 0 if an error occurred (see #tj3GetErrorStr() and
2273  * #tj3GetErrorCode().)
2274  */
2275 DLLEXPORT size_t tj3TransformBufSize(tjhandle handle,
2276                                      const tjtransform *transform);
2277 
2278 
2279 /**
2280  * Losslessly transform a JPEG image into another JPEG image.  Lossless
2281  * transforms work by moving the raw DCT coefficients from one JPEG image
2282  * structure to another without altering the values of the coefficients.  While
2283  * this is typically faster than decompressing the image, transforming it, and
2284  * re-compressing it, lossless transforms are not free.  Each lossless
2285  * transform requires reading and performing entropy decoding on all of the
2286  * coefficients in the source image, regardless of the size of the destination
2287  * image.  Thus, this function provides a means of generating multiple
2288  * transformed images from the same source or applying multiple transformations
2289  * simultaneously, in order to eliminate the need to read the source
2290  * coefficients multiple times.
2291  *
2292  * @param handle handle to a TurboJPEG instance that has been initialized for
2293  * lossless transformation
2294  *
2295  * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
2296  * transform
2297  *
2298  * @param jpegSize size of the JPEG source image (in bytes)
2299  *
2300  * @param n the number of transformed JPEG images to generate
2301  *
2302  * @param dstBufs pointer to an array of n byte buffers.  `dstBufs[i]` will
2303  * receive a JPEG image that has been transformed using the parameters in
2304  * `transforms[i]`.  TurboJPEG has the ability to reallocate the JPEG
2305  * destination buffer to accommodate the size of the transformed JPEG image.
2306  * Thus, you can choose to:
2307  * -# pre-allocate the JPEG destination buffer with an arbitrary size using
2308  * #tj3Alloc() and let TurboJPEG grow the buffer as needed,
2309  * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
2310  * you, or
2311  * -# pre-allocate the buffer to a "worst case" size determined by calling
2312  * #tj3TransformBufSize().  Under normal circumstances, this should ensure that
2313  * the buffer never has to be re-allocated.  (Setting #TJPARAM_NOREALLOC
2314  * guarantees that it won't be.  However, if the source image has a large
2315  * amount of embedded Exif data, then the transformed JPEG image may be larger
2316  * than the worst-case size.  #TJPARAM_NOREALLOC cannot be used in that case
2317  * unless the embedded data is discarded using #TJXOPT_COPYNONE or
2318  * #TJPARAM_SAVEMARKERS.)
2319  * .
2320  * Unless you have set #TJPARAM_NOREALLOC, you should always check `dstBufs[i]`
2321  * upon return from this function, as it may have changed.
2322  *
2323  * @param dstSizes pointer to an array of n size_t variables that will receive
2324  * the actual sizes (in bytes) of each transformed JPEG image.  If `dstBufs[i]`
2325  * points to a pre-allocated buffer, then `dstSizes[i]` should be set to the
2326  * size of the buffer.  Otherwise, `dstSizes[i]` is ignored.  Upon return,
2327  * `dstSizes[i]` will contain the size of the transformed JPEG image (in
2328  * bytes.)
2329  *
2330  * @param transforms pointer to an array of n #tjtransform structures, each of
2331  * which specifies the transform parameters and/or cropping region for the
2332  * corresponding transformed JPEG image.
2333  *
2334  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2335  * and #tj3GetErrorCode().)
2336  */
2337 DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
2338                            size_t jpegSize, int n, unsigned char **dstBufs,
2339                            size_t *dstSizes, const tjtransform *transforms);
2340 
2341 
2342 /**
2343  * Load a packed-pixel image with 2 to 8 bits of data precision per sample from
2344  * disk into memory.
2345  *
2346  * @param handle handle to a TurboJPEG instance
2347  *
2348  * @param filename name of a file containing a packed-pixel image in Windows
2349  * BMP or PBMPLUS (PPM/PGM) format.  Windows BMP files require 8-bit-per-sample
2350  * data precision.  When loading a PBMPLUS file, the target data precision
2351  * (from 2 to 8 bits per sample) can be specified using #TJPARAM_PRECISION and
2352  * defaults to 8 if #TJPARAM_PRECISION is unset or out of range.  If the data
2353  * precision of the PBMPLUS file does not match the target data precision, then
2354  * upconverting or downconverting will be performed.
2355  *
2356  * @param width pointer to an integer variable that will receive the width (in
2357  * pixels) of the packed-pixel image
2358  *
2359  * @param align row alignment (in samples) of the packed-pixel buffer to be
2360  * returned (must be a power of 2.)  Setting this parameter to n will cause all
2361  * rows in the buffer to be padded to the nearest multiple of n samples
2362  * (1 = unpadded.)
2363  *
2364  * @param height pointer to an integer variable that will receive the height
2365  * (in pixels) of the packed-pixel image
2366  *
2367  * @param pixelFormat pointer to an integer variable that specifies or will
2368  * receive the pixel format of the packed-pixel buffer.  The behavior of this
2369  * function varies depending on the value of `*pixelFormat` passed to the
2370  * function:
2371  * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
2372  * use the most optimal pixel format for the file type, and `*pixelFormat` will
2373  * contain the ID of that pixel format upon successful return from this
2374  * function.
2375  * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
2376  * grayscale colormap can be loaded.
2377  * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
2378  * converted using a quick & dirty algorithm that is suitable only for testing
2379  * purposes.  (Proper conversion between CMYK and other formats requires a
2380  * color management system.)
2381  * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
2382  * specified pixel format, and pixel format conversion will be performed if
2383  * necessary.
2384  *
2385  * @return a pointer to a newly-allocated buffer containing the packed-pixel
2386  * image, converted to the chosen pixel format and with the chosen row
2387  * alignment, or NULL if an error occurred (see #tj3GetErrorStr().)  This
2388  * buffer should be freed using #tj3Free().
2389  */
2390 DLLEXPORT unsigned char *tj3LoadImage8(tjhandle handle, const char *filename,
2391                                        int *width, int align, int *height,
2392                                        int *pixelFormat);
2393 
2394 /**
2395  * Load a packed-pixel image with 9 to 12 bits of data precision per sample
2396  * from disk into memory.
2397  *
2398  * @param handle handle to a TurboJPEG instance
2399  *
2400  * @param filename name of a file containing a packed-pixel image in PBMPLUS
2401  * (PPM/PGM) format.  The target data precision (from 9 to 12 bits per sample)
2402  * can be specified using #TJPARAM_PRECISION and defaults to 12 if
2403  * #TJPARAM_PRECISION is unset or out of range.  If the data precision of the
2404  * PBMPLUS file does not match the target data precision, then upconverting or
2405  * downconverting will be performed.
2406  *
2407  * @param width pointer to an integer variable that will receive the width (in
2408  * pixels) of the packed-pixel image
2409  *
2410  * @param align row alignment (in samples) of the packed-pixel buffer to be
2411  * returned (must be a power of 2.)  Setting this parameter to n will cause all
2412  * rows in the buffer to be padded to the nearest multiple of n samples
2413  * (1 = unpadded.)
2414  *
2415  * @param height pointer to an integer variable that will receive the height
2416  * (in pixels) of the packed-pixel image
2417  *
2418  * @param pixelFormat pointer to an integer variable that specifies or will
2419  * receive the pixel format of the packed-pixel buffer.  The behavior of this
2420  * function will vary depending on the value of `*pixelFormat` passed to the
2421  * function:
2422  * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
2423  * use the most optimal pixel format for the file type, and `*pixelFormat` will
2424  * contain the ID of that pixel format upon successful return from this
2425  * function.
2426  * - @ref TJPF_GRAY : Only PGM files can be loaded.
2427  * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
2428  * converted using a quick & dirty algorithm that is suitable only for testing
2429  * purposes.  (Proper conversion between CMYK and other formats requires a
2430  * color management system.)
2431  * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
2432  * specified pixel format, and pixel format conversion will be performed if
2433  * necessary.
2434  *
2435  * @return a pointer to a newly-allocated buffer containing the packed-pixel
2436  * image, converted to the chosen pixel format and with the chosen row
2437  * alignment, or NULL if an error occurred (see #tj3GetErrorStr().)  This
2438  * buffer should be freed using #tj3Free().
2439  */
2440 DLLEXPORT short *tj3LoadImage12(tjhandle handle, const char *filename,
2441                                 int *width, int align, int *height,
2442                                 int *pixelFormat);
2443 
2444 /**
2445  * Load a packed-pixel image with 13 to 16 bits of data precision per sample
2446  * from disk into memory.
2447  *
2448  * @param handle handle to a TurboJPEG instance
2449  *
2450  * @param filename name of a file containing a packed-pixel image in PBMPLUS
2451  * (PPM/PGM) format.  The target data precision (from 13 to 16 bits per sample)
2452  * can be specified using #TJPARAM_PRECISION and defaults to 16 if
2453  * #TJPARAM_PRECISION is unset or out of range.  If the data precision of the
2454  * PBMPLUS file does not match the target data precision, then upconverting or
2455  * downconverting will be performed.
2456  *
2457  * @param width pointer to an integer variable that will receive the width (in
2458  * pixels) of the packed-pixel image
2459  *
2460  * @param align row alignment (in samples) of the packed-pixel buffer to be
2461  * returned (must be a power of 2.)  Setting this parameter to n will cause all
2462  * rows in the buffer to be padded to the nearest multiple of n samples
2463  * (1 = unpadded.)
2464  *
2465  * @param height pointer to an integer variable that will receive the height
2466  * (in pixels) of the packed-pixel image
2467  *
2468  * @param pixelFormat pointer to an integer variable that specifies or will
2469  * receive the pixel format of the packed-pixel buffer.  The behavior of this
2470  * function will vary depending on the value of `*pixelFormat` passed to the
2471  * function:
2472  * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
2473  * use the most optimal pixel format for the file type, and `*pixelFormat` will
2474  * contain the ID of that pixel format upon successful return from this
2475  * function.
2476  * - @ref TJPF_GRAY : Only PGM files can be loaded.
2477  * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
2478  * converted using a quick & dirty algorithm that is suitable only for testing
2479  * purposes.  (Proper conversion between CMYK and other formats requires a
2480  * color management system.)
2481  * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
2482  * specified pixel format, and pixel format conversion will be performed if
2483  * necessary.
2484  *
2485  * @return a pointer to a newly-allocated buffer containing the packed-pixel
2486  * image, converted to the chosen pixel format and with the chosen row
2487  * alignment, or NULL if an error occurred (see #tj3GetErrorStr().)  This
2488  * buffer should be freed using #tj3Free().
2489  */
2490 DLLEXPORT unsigned short *tj3LoadImage16(tjhandle handle, const char *filename,
2491                                          int *width, int align, int *height,
2492                                          int *pixelFormat);
2493 
2494 
2495 /**
2496  * Save a packed-pixel image with 2 to 8 bits of data precision per sample from
2497  * memory to disk.
2498  *
2499  * @param handle handle to a TurboJPEG instance
2500  *
2501  * @param filename name of a file to which to save the packed-pixel image.  The
2502  * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
2503  * on the file extension.  Windows BMP files require 8-bit-per-sample data
2504  * precision.  When saving a PBMPLUS file, the source data precision (from 2 to
2505  * 8 bits per sample) can be specified using #TJPARAM_PRECISION and defaults to
2506  * 8 if #TJPARAM_PRECISION is unset or out of range.
2507  *
2508  * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2509  * or CMYK image to be saved
2510  *
2511  * @param width width (in pixels) of the packed-pixel image
2512  *
2513  * @param pitch samples per row in the packed-pixel image.  Setting this
2514  * parameter to 0 is the equivalent of setting it to
2515  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2516  *
2517  * @param height height (in pixels) of the packed-pixel image
2518  *
2519  * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2520  * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
2521  * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
2522  * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
2523  * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
2524  * converted to RGB using a quick & dirty algorithm that is suitable only for
2525  * testing purposes.  (Proper conversion between CMYK and other formats
2526  * requires a color management system.)
2527  *
2528  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2529  */
2530 DLLEXPORT int tj3SaveImage8(tjhandle handle, const char *filename,
2531                             const unsigned char *buffer, int width, int pitch,
2532                             int height, int pixelFormat);
2533 
2534 /**
2535  * Save a packed-pixel image with 9 to 12 bits of data precision per sample
2536  * from memory to disk.
2537  *
2538  * @param handle handle to a TurboJPEG instance
2539  *
2540  * @param filename name of a file to which to save the packed-pixel image,
2541  * which will be stored in PBMPLUS (PPM/PGM) format.  The source data precision
2542  * (from 9 to 12 bits per sample) can be specified using #TJPARAM_PRECISION and
2543  * defaults to 12 if #TJPARAM_PRECISION is unset or out of range.
2544  *
2545  * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2546  * or CMYK image to be saved
2547  *
2548  * @param width width (in pixels) of the packed-pixel image
2549  *
2550  * @param pitch samples per row in the packed-pixel image.  Setting this
2551  * parameter to 0 is the equivalent of setting it to
2552  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2553  *
2554  * @param height height (in pixels) of the packed-pixel image
2555  *
2556  * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2557  * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
2558  * image will be stored in PGM format.  Otherwise, the image will be stored in
2559  * PPM format.  If this parameter is set to @ref TJPF_CMYK, then the CMYK
2560  * pixels will be converted to RGB using a quick & dirty algorithm that is
2561  * suitable only for testing purposes.  (Proper conversion between CMYK and
2562  * other formats requires a color management system.)
2563  *
2564  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2565  */
2566 DLLEXPORT int tj3SaveImage12(tjhandle handle, const char *filename,
2567                              const short *buffer, int width, int pitch,
2568                              int height, int pixelFormat);
2569 
2570 /**
2571  * Save a packed-pixel image with 13 to 16 bits of data precision per sample
2572  * from memory to disk.
2573  *
2574  * @param handle handle to a TurboJPEG instance
2575  *
2576  * @param filename name of a file to which to save the packed-pixel image,
2577  * which will be stored in PBMPLUS (PPM/PGM) format.  The source data precision
2578  * (from 13 to 16 bits per sample) can be specified using #TJPARAM_PRECISION
2579  * and defaults to 16 if #TJPARAM_PRECISION is unset or out of range.
2580  *
2581  * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2582  * or CMYK image to be saved
2583  *
2584  * @param width width (in pixels) of the packed-pixel image
2585  *
2586  * @param pitch samples per row in the packed-pixel image.  Setting this
2587  * parameter to 0 is the equivalent of setting it to
2588  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2589  *
2590  * @param height height (in pixels) of the packed-pixel image
2591  *
2592  * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2593  * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
2594  * image will be stored in PGM format.  Otherwise, the image will be stored in
2595  * PPM format.  If this parameter is set to @ref TJPF_CMYK, then the CMYK
2596  * pixels will be converted to RGB using a quick & dirty algorithm that is
2597  * suitable only for testing purposes.  (Proper conversion between CMYK and
2598  * other formats requires a color management system.)
2599  *
2600  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2601  */
2602 DLLEXPORT int tj3SaveImage16(tjhandle handle, const char *filename,
2603                              const unsigned short *buffer, int width,
2604                              int pitch, int height, int pixelFormat);
2605 
2606 
2607 /* Backward compatibility functions and macros (nothing to see here) */
2608 
2609 /* TurboJPEG 1.0+ */
2610 
2611 #define NUMSUBOPT  TJ_NUMSAMP
2612 #define TJ_444  TJSAMP_444
2613 #define TJ_422  TJSAMP_422
2614 #define TJ_420  TJSAMP_420
2615 #define TJ_411  TJSAMP_420
2616 #define TJ_GRAYSCALE  TJSAMP_GRAY
2617 
2618 #define TJ_BGR  1
2619 #define TJ_BOTTOMUP  TJFLAG_BOTTOMUP
2620 #define TJ_FORCEMMX  TJFLAG_FORCEMMX
2621 #define TJ_FORCESSE  TJFLAG_FORCESSE
2622 #define TJ_FORCESSE2  TJFLAG_FORCESSE2
2623 #define TJ_ALPHAFIRST  64
2624 #define TJ_FORCESSE3  TJFLAG_FORCESSE3
2625 #define TJ_FASTUPSAMPLE  TJFLAG_FASTUPSAMPLE
2626 
2627 #define TJPAD(width)  (((width) + 3) & (~3))
2628 
2629 DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
2630 
2631 DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
2632                          int pitch, int height, int pixelSize,
2633                          unsigned char *dstBuf, unsigned long *compressedSize,
2634                          int jpegSubsamp, int jpegQual, int flags);
2635 
2636 DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
2637                            unsigned long jpegSize, unsigned char *dstBuf,
2638                            int width, int pitch, int height, int pixelSize,
2639                            int flags);
2640 
2641 DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
2642                                  unsigned long jpegSize, int *width,
2643                                  int *height);
2644 
2645 DLLEXPORT int tjDestroy(tjhandle handle);
2646 
2647 DLLEXPORT char *tjGetErrorStr(void);
2648 
2649 DLLEXPORT tjhandle tjInitCompress(void);
2650 
2651 DLLEXPORT tjhandle tjInitDecompress(void);
2652 
2653 /* TurboJPEG 1.1+ */
2654 
2655 #define TJ_YUV  512
2656 
2657 DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
2658 
2659 DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
2660                                   unsigned long jpegSize, int *width,
2661                                   int *height, int *jpegSubsamp);
2662 
2663 DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
2664                                 unsigned long jpegSize, unsigned char *dstBuf,
2665                                 int flags);
2666 
2667 DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
2668                           int pitch, int height, int pixelSize,
2669                           unsigned char *dstBuf, int subsamp, int flags);
2670 
2671 /* TurboJPEG 1.2+ */
2672 
2673 #define TJFLAG_BOTTOMUP  2
2674 #define TJFLAG_FORCEMMX  8
2675 #define TJFLAG_FORCESSE  16
2676 #define TJFLAG_FORCESSE2  32
2677 #define TJFLAG_FORCESSE3  128
2678 #define TJFLAG_FASTUPSAMPLE  256
2679 #define TJFLAG_NOREALLOC  1024
2680 
2681 DLLEXPORT unsigned char *tjAlloc(int bytes);
2682 
2683 DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
2684 
2685 DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
2686 
2687 DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
2688                           int width, int pitch, int height, int pixelFormat,
2689                           unsigned char **jpegBuf, unsigned long *jpegSize,
2690                           int jpegSubsamp, int jpegQual, int flags);
2691 
2692 DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
2693                             unsigned long jpegSize, unsigned char *dstBuf,
2694                             int width, int pitch, int height, int pixelFormat,
2695                             int flags);
2696 
2697 DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
2698                            int pitch, int height, int pixelFormat,
2699                            unsigned char *dstBuf, int subsamp, int flags);
2700 
2701 DLLEXPORT void tjFree(unsigned char *buffer);
2702 
2703 DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
2704 
2705 DLLEXPORT tjhandle tjInitTransform(void);
2706 
2707 DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
2708                             unsigned long jpegSize, int n,
2709                             unsigned char **dstBufs, unsigned long *dstSizes,
2710                             tjtransform *transforms, int flags);
2711 
2712 /* TurboJPEG 1.2.1+ */
2713 
2714 #define TJFLAG_FASTDCT  2048
2715 #define TJFLAG_ACCURATEDCT  4096
2716 
2717 /* TurboJPEG 1.4+ */
2718 
2719 DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
2720                                       int subsamp);
2721 
2722 DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
2723                                 int width, int align, int height, int subsamp,
2724                                 unsigned char **jpegBuf,
2725                                 unsigned long *jpegSize, int jpegQual,
2726                                 int flags);
2727 
2728 DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
2729                                       const unsigned char **srcPlanes,
2730                                       int width, const int *strides,
2731                                       int height, int subsamp,
2732                                       unsigned char **jpegBuf,
2733                                       unsigned long *jpegSize, int jpegQual,
2734                                       int flags);
2735 
2736 DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
2737                           int align, int subsamp, unsigned char *dstBuf,
2738                           int width, int pitch, int height, int pixelFormat,
2739                           int flags);
2740 
2741 DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
2742                                 const unsigned char **srcPlanes,
2743                                 const int *strides, int subsamp,
2744                                 unsigned char *dstBuf, int width, int pitch,
2745                                 int height, int pixelFormat, int flags);
2746 
2747 DLLEXPORT int tjDecompressHeader3(tjhandle handle,
2748                                   const unsigned char *jpegBuf,
2749                                   unsigned long jpegSize, int *width,
2750                                   int *height, int *jpegSubsamp,
2751                                   int *jpegColorspace);
2752 
2753 DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
2754                                  unsigned long jpegSize, unsigned char *dstBuf,
2755                                  int width, int align, int height, int flags);
2756 
2757 DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
2758                                       const unsigned char *jpegBuf,
2759                                       unsigned long jpegSize,
2760                                       unsigned char **dstPlanes, int width,
2761                                       int *strides, int height, int flags);
2762 
2763 DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
2764                            int width, int pitch, int height, int pixelFormat,
2765                            unsigned char *dstBuf, int align, int subsamp,
2766                            int flags);
2767 
2768 DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
2769                                 int width, int pitch, int height,
2770                                 int pixelFormat, unsigned char **dstPlanes,
2771                                 int *strides, int subsamp, int flags);
2772 
2773 DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
2774 
2775 DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
2776                                        int height, int subsamp);
2777 
2778 DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
2779 
2780 /* TurboJPEG 2.0+ */
2781 
2782 #define TJFLAG_STOPONWARNING  8192
2783 #define TJFLAG_PROGRESSIVE  16384
2784 
2785 DLLEXPORT int tjGetErrorCode(tjhandle handle);
2786 
2787 DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
2788 
2789 DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
2790                                      int align, int *height, int *pixelFormat,
2791                                      int flags);
2792 
2793 DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
2794                           int width, int pitch, int height, int pixelFormat,
2795                           int flags);
2796 
2797 /* TurboJPEG 2.1+ */
2798 
2799 #define TJFLAG_LIMITSCANS  32768
2800 
2801 /**
2802  * @}
2803  */
2804 
2805 #ifdef __cplusplus
2806 }
2807 #endif
2808 
2809 #endif