Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-24 09:25:22

0001 /*
0002  * Copyright (C)2009-2015, 2017, 2020-2024 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 (255 for 8-bit samples, 4095 for 12-bit samples,
0321    * and 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    * JPEG data precision (bits per sample) [decompression only, read-only]
0577    *
0578    * The JPEG image uses the specified number of bits per sample.
0579    *
0580    * **Value**
0581    * - `8`, `12`, or `16`
0582    *
0583    * 12-bit data precision implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC
0584    * is set.
0585    */
0586   TJPARAM_PRECISION,
0587   /**
0588    * JPEG colorspace
0589    *
0590    * The JPEG image uses (decompression) or will use (lossy compression) the
0591    * specified colorspace.
0592    *
0593    * **Value**
0594    * - One of the @ref TJCS "JPEG colorspaces" *[default for lossy compression:
0595    * automatically selected based on the subsampling level and pixel format]*
0596    */
0597   TJPARAM_COLORSPACE,
0598   /**
0599    * Chrominance upsampling algorithm [lossy decompression only]
0600    *
0601    * **Value**
0602    * - `0` *[default]* Use smooth upsampling when decompressing a JPEG image
0603    * that was generated using chrominance subsampling.  This creates a smooth
0604    * transition between neighboring chrominance components in order to reduce
0605    * upsampling artifacts in the decompressed image.
0606    * - `1` Use the fastest chrominance upsampling algorithm available, which
0607    * may combine upsampling with color conversion.
0608    */
0609   TJPARAM_FASTUPSAMPLE,
0610   /**
0611    * DCT/IDCT algorithm [lossy compression and decompression]
0612    *
0613    * **Value**
0614    * - `0` *[default]* Use the most accurate DCT/IDCT algorithm available.
0615    * - `1` Use the fastest DCT/IDCT algorithm available.
0616    *
0617    * This parameter is provided mainly for backward compatibility with libjpeg,
0618    * which historically implemented several different DCT/IDCT algorithms
0619    * because of performance limitations with 1990s CPUs.  In the libjpeg-turbo
0620    * implementation of the TurboJPEG API:
0621    * - The "fast" and "accurate" DCT/IDCT algorithms perform similarly on
0622    * modern x86/x86-64 CPUs that support AVX2 instructions.
0623    * - The "fast" algorithm is generally only about 5-15% faster than the
0624    * "accurate" algorithm on other types of CPUs.
0625    * - The difference in accuracy between the "fast" and "accurate" algorithms
0626    * is the most pronounced at JPEG quality levels above 90 and tends to be
0627    * more pronounced with decompression than with compression.
0628    * - For JPEG quality levels above 97, the "fast" algorithm degrades and is
0629    * not fully accelerated, so it is slower than the "accurate" algorithm.
0630    */
0631   TJPARAM_FASTDCT,
0632   /**
0633    * Huffman table optimization [lossy compression, lossless transformation]
0634    *
0635    * **Value**
0636    * - `0` *[default]* The JPEG image will use the default Huffman tables.
0637    * - `1` Optimal Huffman tables will be computed for the JPEG image.  For
0638    * lossless transformation, this can also be specified using
0639    * #TJXOPT_OPTIMIZE.
0640    *
0641    * Huffman table optimization improves compression slightly (generally 5% or
0642    * less), but it reduces compression performance considerably.
0643    */
0644   TJPARAM_OPTIMIZE,
0645   /**
0646    * Progressive JPEG
0647    *
0648    * In a progressive JPEG image, the DCT coefficients are split across
0649    * multiple "scans" of increasing quality.  Thus, a low-quality scan
0650    * containing the lowest-frequency DCT coefficients can be transmitted first
0651    * and refined with subsequent higher-quality scans containing
0652    * higher-frequency DCT coefficients.  When using Huffman entropy coding, the
0653    * progressive JPEG format also provides an "end-of-bands (EOB) run" feature
0654    * that allows large groups of zeroes, potentially spanning multiple MCUs,
0655    * to be represented using only a few bytes.
0656    *
0657    * **Value**
0658    * - `0` *[default for compression, lossless transformation]* The lossy JPEG
0659    * image is (decompression) or will be (compression, lossless transformation)
0660    * single-scan.
0661    * - `1` The lossy JPEG image is (decompression) or will be (compression,
0662    * lossless transformation) progressive.  For lossless transformation, this
0663    * can also be specified using #TJXOPT_PROGRESSIVE.
0664    *
0665    * Progressive JPEG images generally have better compression ratios than
0666    * single-scan JPEG images (much better if the image has large areas of solid
0667    * color), but progressive JPEG compression and decompression is considerably
0668    * slower than single-scan JPEG compression and decompression.  Can be
0669    * combined with #TJPARAM_ARITHMETIC.  Implies #TJPARAM_OPTIMIZE unless
0670    * #TJPARAM_ARITHMETIC is also set.
0671    */
0672   TJPARAM_PROGRESSIVE,
0673   /**
0674    * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless
0675    * transformation]
0676    *
0677    * Setting this parameter causes the decompression and transform functions to
0678    * return an error if the number of scans in a progressive JPEG image exceeds
0679    * the specified limit.  The primary purpose of this is to allow
0680    * security-critical applications to guard against an exploit of the
0681    * progressive JPEG format described in
0682    * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
0683    *
0684    * **Value**
0685    * - maximum number of progressive JPEG scans that the decompression and
0686    * transform functions will process *[default: `0` (no limit)]*
0687    *
0688    * @see #TJPARAM_PROGRESSIVE
0689    */
0690   TJPARAM_SCANLIMIT,
0691   /**
0692    * Arithmetic entropy coding
0693    *
0694    * **Value**
0695    * - `0` *[default for compression, lossless transformation]* The lossy JPEG
0696    * image uses (decompression) or will use (compression, lossless
0697    * transformation) Huffman entropy coding.
0698    * - `1` The lossy JPEG image uses (decompression) or will use (compression,
0699    * lossless transformation) arithmetic entropy coding.  For lossless
0700    * transformation, this can also be specified using #TJXOPT_ARITHMETIC.
0701    *
0702    * Arithmetic entropy coding generally improves compression relative to
0703    * Huffman entropy coding, but it reduces compression and decompression
0704    * performance considerably.  Can be combined with #TJPARAM_PROGRESSIVE.
0705    */
0706   TJPARAM_ARITHMETIC,
0707   /**
0708    * Lossless JPEG
0709    *
0710    * **Value**
0711    * - `0` *[default for compression]* The JPEG image is (decompression) or
0712    * will be (compression) lossy/DCT-based.
0713    * - `1` The JPEG image is (decompression) or will be (compression)
0714    * lossless/predictive.
0715    *
0716    * In most cases, lossless JPEG compression and decompression is considerably
0717    * slower than lossy JPEG compression and decompression, and lossless JPEG
0718    * images are much larger than lossy JPEG images.  Thus, lossless JPEG images
0719    * are typically used only for applications that require mathematically
0720    * lossless compression.  Also note that the following features are not
0721    * available with lossless JPEG images:
0722    * - Colorspace conversion (lossless JPEG images always use #TJCS_RGB,
0723    * #TJCS_GRAY, or #TJCS_CMYK, depending on the pixel format of the source
0724    * image)
0725    * - Chrominance subsampling (lossless JPEG images always use #TJSAMP_444)
0726    * - JPEG quality selection
0727    * - DCT/IDCT algorithm selection
0728    * - Progressive JPEG
0729    * - Arithmetic entropy coding
0730    * - Compression from/decompression to planar YUV images
0731    * - Decompression scaling
0732    * - Lossless transformation
0733    *
0734    * @see #TJPARAM_LOSSLESSPSV, #TJPARAM_LOSSLESSPT
0735    */
0736   TJPARAM_LOSSLESS,
0737   /**
0738    * Lossless JPEG predictor selection value (PSV)
0739    *
0740    * **Value**
0741    * - `1`-`7` *[default for compression: `1`]*
0742    *
0743    * Lossless JPEG compression shares no algorithms with lossy JPEG
0744    * compression.  Instead, it uses differential pulse-code modulation (DPCM),
0745    * an algorithm whereby each sample is encoded as the difference between the
0746    * sample's value and a "predictor", which is based on the values of
0747    * neighboring samples.  If Ra is the sample immediately to the left of the
0748    * current sample, Rb is the sample immediately above the current sample, and
0749    * Rc is the sample diagonally to the left and above the current sample, then
0750    * the relationship between the predictor selection value and the predictor
0751    * is as follows:
0752    *
0753    * PSV | Predictor
0754    * ----|----------
0755    * 1   | Ra
0756    * 2   | Rb
0757    * 3   | Rc
0758    * 4   | Ra + Rb – Rc
0759    * 5   | Ra + (Rb – Rc) / 2
0760    * 6   | Rb + (Ra – Rc) / 2
0761    * 7   | (Ra + Rb) / 2
0762    *
0763    * Predictors 1-3 are 1-dimensional predictors, whereas Predictors 4-7 are
0764    * 2-dimensional predictors.  The best predictor for a particular image
0765    * depends on the image.
0766    *
0767    * @see #TJPARAM_LOSSLESS
0768    */
0769   TJPARAM_LOSSLESSPSV,
0770   /**
0771    * Lossless JPEG point transform (Pt)
0772    *
0773    * **Value**
0774    * - `0` through ***precision*** *- 1*, where ***precision*** is the JPEG
0775    * data precision in bits *[default for compression: `0`]*
0776    *
0777    * A point transform value of `0` is necessary in order to generate a fully
0778    * lossless JPEG image.  (A non-zero point transform value right-shifts the
0779    * input samples by the specified number of bits, which is effectively a form
0780    * of lossy color quantization.)
0781    *
0782    * @see #TJPARAM_LOSSLESS, #TJPARAM_PRECISION
0783    */
0784   TJPARAM_LOSSLESSPT,
0785   /**
0786    * JPEG restart marker interval in MCUs [lossy compression only]
0787    *
0788    * The nature of entropy coding is such that a corrupt JPEG image cannot
0789    * be decompressed beyond the point of corruption unless it contains restart
0790    * markers.  A restart marker stops and restarts the entropy coding algorithm
0791    * so that, if a JPEG image is corrupted, decompression can resume at the
0792    * next marker.  Thus, adding more restart markers improves the fault
0793    * tolerance of the JPEG image, but adding too many restart markers can
0794    * adversely affect the compression ratio and performance.
0795    *
0796    * In typical JPEG images, an MCU (Minimum Coded Unit) is the minimum set of
0797    * interleaved "data units" (8x8 DCT blocks if the image is lossy or samples
0798    * if the image is lossless) necessary to represent at least one data unit
0799    * per component.  (For example, an MCU in an interleaved lossy JPEG image
0800    * that uses 4:2:2 subsampling consists of two luminance blocks followed by
0801    * one block for each chrominance component.)  In single-component or
0802    * non-interleaved JPEG images, an MCU is the same as a data unit.
0803    *
0804    * **Value**
0805    * - the number of MCUs between each restart marker *[default: `0` (no
0806    * restart markers)]*
0807    *
0808    * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTROWS to 0.
0809    */
0810   TJPARAM_RESTARTBLOCKS,
0811   /**
0812    * JPEG restart marker interval in MCU rows [compression only]
0813    *
0814    * See #TJPARAM_RESTARTBLOCKS for a description of restart markers and MCUs.
0815    * An MCU row is a row of MCUs spanning the entire width of the image.
0816    *
0817    * **Value**
0818    * - the number of MCU rows between each restart marker *[default: `0` (no
0819    * restart markers)]*
0820    *
0821    * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTBLOCKS to
0822    * 0.
0823    */
0824   TJPARAM_RESTARTROWS,
0825   /**
0826    * JPEG horizontal pixel density
0827    *
0828    * **Value**
0829    * - The JPEG image has (decompression) or will have (compression) the
0830    * specified horizontal pixel density *[default for compression: `1`]*.
0831    *
0832    * This value is stored in or read from the JPEG header.  It does not affect
0833    * the contents of the JPEG image.  Note that this parameter is set by
0834    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0835    * density information, and the value of this parameter is stored to a
0836    * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
0837    * is `2`.
0838    *
0839    * @see TJPARAM_DENSITYUNITS
0840    */
0841   TJPARAM_XDENSITY,
0842   /**
0843    * JPEG vertical pixel density
0844    *
0845    * **Value**
0846    * - The JPEG image has (decompression) or will have (compression) the
0847    * specified vertical pixel density *[default for compression: `1`]*.
0848    *
0849    * This value is stored in or read from the JPEG header.  It does not affect
0850    * the contents of the JPEG image.  Note that this parameter is set by
0851    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0852    * density information, and the value of this parameter is stored to a
0853    * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
0854    * is `2`.
0855    *
0856    * @see TJPARAM_DENSITYUNITS
0857    */
0858   TJPARAM_YDENSITY,
0859   /**
0860    * JPEG pixel density units
0861    *
0862    * **Value**
0863    * - `0` *[default for compression]* The pixel density of the JPEG image is
0864    * expressed (decompression) or will be expressed (compression) in unknown
0865    * units.
0866    * - `1` The pixel density of the JPEG image is expressed (decompression) or
0867    * will be expressed (compression) in units of pixels/inch.
0868    * - `2` The pixel density of the JPEG image is expressed (decompression) or
0869    * will be expressed (compression) in units of pixels/cm.
0870    *
0871    * This value is stored in or read from the JPEG header.  It does not affect
0872    * the contents of the JPEG image.  Note that this parameter is set by
0873    * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
0874    * density information, and the value of this parameter is stored to a
0875    * Windows BMP file by #tj3SaveImage8() if the value is `2`.
0876    *
0877    * @see TJPARAM_XDENSITY, TJPARAM_YDENSITY
0878    */
0879   TJPARAM_DENSITYUNITS,
0880   /**
0881    * Memory limit for intermediate buffers
0882    *
0883    * **Value**
0884    * - the maximum amount of memory (in megabytes) that will be allocated for
0885    * intermediate buffers, which are used with progressive JPEG compression and
0886    * decompression, Huffman table optimization, lossless JPEG compression, and
0887    * lossless transformation *[default: `0` (no limit)]*
0888    */
0889   TJPARAM_MAXMEMORY,
0890   /**
0891    * Image size limit [decompression, lossless transformation, packed-pixel
0892    * image loading]
0893    *
0894    * Setting this parameter causes the decompression, transform, and image
0895    * loading functions to return an error if the number of pixels in the source
0896    * image exceeds the specified limit.  This allows security-critical
0897    * applications to guard against excessive memory consumption.
0898    *
0899    * **Value**
0900    * - maximum number of pixels that the decompression, transform, and image
0901    * loading functions will process *[default: `0` (no limit)]*
0902    */
0903   TJPARAM_MAXPIXELS
0904 };
0905 
0906 
0907 /**
0908  * The number of error codes
0909  */
0910 #define TJ_NUMERR  2
0911 
0912 /**
0913  * Error codes
0914  */
0915 enum TJERR {
0916   /**
0917    * The error was non-fatal and recoverable, but the destination image may
0918    * still be corrupt.
0919    */
0920   TJERR_WARNING,
0921   /**
0922    * The error was fatal and non-recoverable.
0923    */
0924   TJERR_FATAL
0925 };
0926 
0927 
0928 /**
0929  * The number of transform operations
0930  */
0931 #define TJ_NUMXOP  8
0932 
0933 /**
0934  * Transform operations for #tj3Transform()
0935  */
0936 enum TJXOP {
0937   /**
0938    * Do not transform the position of the image pixels.
0939    */
0940   TJXOP_NONE,
0941   /**
0942    * Flip (mirror) image horizontally.  This transform is imperfect if there
0943    * are any partial iMCUs on the right edge (see #TJXOPT_PERFECT.)
0944    */
0945   TJXOP_HFLIP,
0946   /**
0947    * Flip (mirror) image vertically.  This transform is imperfect if there are
0948    * any partial iMCUs on the bottom edge (see #TJXOPT_PERFECT.)
0949    */
0950   TJXOP_VFLIP,
0951   /**
0952    * Transpose image (flip/mirror along upper left to lower right axis.)  This
0953    * transform is always perfect.
0954    */
0955   TJXOP_TRANSPOSE,
0956   /**
0957    * Transverse transpose image (flip/mirror along upper right to lower left
0958    * axis.)  This transform is imperfect if there are any partial iMCUs in the
0959    * image (see #TJXOPT_PERFECT.)
0960    */
0961   TJXOP_TRANSVERSE,
0962   /**
0963    * Rotate image clockwise by 90 degrees.  This transform is imperfect if
0964    * there are any partial iMCUs on the bottom edge (see #TJXOPT_PERFECT.)
0965    */
0966   TJXOP_ROT90,
0967   /**
0968    * Rotate image 180 degrees.  This transform is imperfect if there are any
0969    * partial iMCUs in the image (see #TJXOPT_PERFECT.)
0970    */
0971   TJXOP_ROT180,
0972   /**
0973    * Rotate image counter-clockwise by 90 degrees.  This transform is imperfect
0974    * if there are any partial iMCUs on the right edge (see #TJXOPT_PERFECT.)
0975    */
0976   TJXOP_ROT270
0977 };
0978 
0979 
0980 /**
0981  * This option causes #tj3Transform() to return an error if the transform is
0982  * not perfect.  Lossless transforms operate on iMCUs, the size of which
0983  * depends on the level of chrominance subsampling used (see #tjMCUWidth and
0984  * #tjMCUHeight.)  If the image's width or height is not evenly divisible by
0985  * the iMCU size, then there will be partial iMCUs on the right and/or bottom
0986  * edges.  It is not possible to move these partial iMCUs to the top or left of
0987  * the image, so any transform that would require that is "imperfect."  If this
0988  * option is not specified, then any partial iMCUs that cannot be transformed
0989  * will be left in place, which will create odd-looking strips on the right or
0990  * bottom edge of the image.
0991  */
0992 #define TJXOPT_PERFECT  (1 << 0)
0993 /**
0994  * Discard any partial iMCUs that cannot be transformed.
0995  */
0996 #define TJXOPT_TRIM  (1 << 1)
0997 /**
0998  * Enable lossless cropping.  See #tj3Transform() for more information.
0999  */
1000 #define TJXOPT_CROP  (1 << 2)
1001 /**
1002  * Discard the color data in the source image, and generate a grayscale
1003  * destination image.
1004  */
1005 #define TJXOPT_GRAY  (1 << 3)
1006 /**
1007  * Do not generate a destination image.  (This can be used in conjunction with
1008  * a custom filter to capture the transformed DCT coefficients without
1009  * transcoding them.)
1010  */
1011 #define TJXOPT_NOOUTPUT  (1 << 4)
1012 /**
1013  * Generate a progressive destination image instead of a single-scan
1014  * destination image.  Progressive JPEG images generally have better
1015  * compression ratios than single-scan JPEG images (much better if the image
1016  * has large areas of solid color), but progressive JPEG decompression is
1017  * considerably slower than single-scan JPEG decompression.  Can be combined
1018  * with #TJXOPT_ARITHMETIC.  Implies #TJXOPT_OPTIMIZE unless #TJXOPT_ARITHMETIC
1019  * is also specified.
1020  */
1021 #define TJXOPT_PROGRESSIVE  (1 << 5)
1022 /**
1023  * Do not copy any extra markers (including Exif and ICC profile data) from the
1024  * source image to the destination image.
1025  */
1026 #define TJXOPT_COPYNONE  (1 << 6)
1027 /**
1028  * Enable arithmetic entropy coding in the destination image.  Arithmetic
1029  * entropy coding generally improves compression relative to Huffman entropy
1030  * coding (the default), but it reduces decompression performance considerably.
1031  * Can be combined with #TJXOPT_PROGRESSIVE.
1032  */
1033 #define TJXOPT_ARITHMETIC  (1 << 7)
1034 /**
1035  * Enable Huffman table optimization for the destination image.  Huffman table
1036  * optimization improves compression slightly (generally 5% or less.)
1037  */
1038 #define TJXOPT_OPTIMIZE  (1 << 8)
1039 
1040 
1041 /**
1042  * Scaling factor
1043  */
1044 typedef struct {
1045   /**
1046    * Numerator
1047    */
1048   int num;
1049   /**
1050    * Denominator
1051    */
1052   int denom;
1053 } tjscalingfactor;
1054 
1055 /**
1056  * Cropping region
1057  */
1058 typedef struct {
1059   /**
1060    * The left boundary of the cropping region.  For lossless transformation,
1061    * this must be evenly divisible by the iMCU width (see #tjMCUWidth) of the
1062    * destination image.  For decompression, this must be evenly divisible by
1063    * the scaled iMCU width of the source image.
1064    */
1065   int x;
1066   /**
1067    * The upper boundary of the cropping region.  For lossless transformation,
1068    * this must be evenly divisible by the iMCU height (see #tjMCUHeight) of the
1069    * destination image.
1070    */
1071   int y;
1072   /**
1073    * The width of the cropping region.  Setting this to 0 is the equivalent of
1074    * setting it to the width of the source JPEG image - x.
1075    */
1076   int w;
1077   /**
1078    * The height of the cropping region.  Setting this to 0 is the equivalent of
1079    * setting it to the height of the source JPEG image - y.
1080    */
1081   int h;
1082 } tjregion;
1083 
1084 /**
1085  * A #tjregion structure that specifies no cropping
1086  */
1087 static const tjregion TJUNCROPPED = { 0, 0, 0, 0 };
1088 
1089 /**
1090  * Lossless transform
1091  */
1092 typedef struct tjtransform {
1093   /**
1094    * Cropping region
1095    */
1096   tjregion r;
1097   /**
1098    * One of the @ref TJXOP "transform operations"
1099    */
1100   int op;
1101   /**
1102    * The bitwise OR of one of more of the @ref TJXOPT_ARITHMETIC
1103    * "transform options"
1104    */
1105   int options;
1106   /**
1107    * Arbitrary data that can be accessed within the body of the callback
1108    * function
1109    */
1110   void *data;
1111   /**
1112    * A callback function that can be used to modify the DCT coefficients after
1113    * they are losslessly transformed but before they are transcoded to a new
1114    * JPEG image.  This allows for custom filters or other transformations to be
1115    * applied in the frequency domain.
1116    *
1117    * @param coeffs pointer to an array of transformed DCT coefficients.  (NOTE:
1118    * This pointer is not guaranteed to be valid once the callback returns, so
1119    * applications wishing to hand off the DCT coefficients to another function
1120    * or library should make a copy of them within the body of the callback.)
1121    *
1122    * @param arrayRegion #tjregion structure containing the width and height of
1123    * the array pointed to by `coeffs` as well as its offset relative to the
1124    * component plane.  TurboJPEG implementations may choose to split each
1125    * component plane into multiple DCT coefficient arrays and call the callback
1126    * function once for each array.
1127    *
1128    * @param planeRegion #tjregion structure containing the width and height of
1129    * the component plane to which `coeffs` belongs
1130    *
1131    * @param componentID ID number of the component plane to which `coeffs`
1132    * belongs.  (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
1133    * typical JPEG images.)
1134    *
1135    * @param transformID ID number of the transformed image to which `coeffs`
1136    * belongs.  This is the same as the index of the transform in the
1137    * `transforms` array that was passed to #tj3Transform().
1138    *
1139    * @param transform a pointer to a #tjtransform structure that specifies the
1140    * parameters and/or cropping region for this transform
1141    *
1142    * @return 0 if the callback was successful, or -1 if an error occurred.
1143    */
1144   int (*customFilter) (short *coeffs, tjregion arrayRegion,
1145                        tjregion planeRegion, int componentID, int transformID,
1146                        struct tjtransform *transform);
1147 } tjtransform;
1148 
1149 /**
1150  * TurboJPEG instance handle
1151  */
1152 typedef void *tjhandle;
1153 
1154 
1155 /**
1156  * Compute the scaled value of `dimension` using the given scaling factor.
1157  * This macro performs the integer equivalent of `ceil(dimension *
1158  * scalingFactor)`.
1159  */
1160 #define TJSCALED(dimension, scalingFactor) \
1161   (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
1162    scalingFactor.denom)
1163 
1164 /**
1165  * A #tjscalingfactor structure that specifies a scaling factor of 1/1 (no
1166  * scaling)
1167  */
1168 static const tjscalingfactor TJUNSCALED = { 1, 1 };
1169 
1170 
1171 #ifdef __cplusplus
1172 extern "C" {
1173 #endif
1174 
1175 
1176 /**
1177  * Create a new TurboJPEG instance.
1178  *
1179  * @param initType one of the @ref TJINIT "initialization options"
1180  *
1181  * @return a handle to the newly-created instance, or NULL if an error occurred
1182  * (see #tj3GetErrorStr().)
1183  */
1184 DLLEXPORT tjhandle tj3Init(int initType);
1185 
1186 
1187 /**
1188  * Destroy a TurboJPEG instance.
1189  *
1190  * @param handle handle to a TurboJPEG instance.  If the handle is NULL, then
1191  * this function has no effect.
1192  */
1193 DLLEXPORT void tj3Destroy(tjhandle handle);
1194 
1195 
1196 /**
1197  * Returns a descriptive error message explaining why the last command failed.
1198  *
1199  * @param handle handle to a TurboJPEG instance, or NULL if the error was
1200  * generated by a global function (but note that retrieving the error message
1201  * for a global function is thread-safe only on platforms that support
1202  * thread-local storage.)
1203  *
1204  * @return a descriptive error message explaining why the last command failed.
1205  */
1206 DLLEXPORT char *tj3GetErrorStr(tjhandle handle);
1207 
1208 
1209 /**
1210  * Returns a code indicating the severity of the last error.  See
1211  * @ref TJERR "Error codes".
1212  *
1213  * @param handle handle to a TurboJPEG instance
1214  *
1215  * @return a code indicating the severity of the last error.  See
1216  * @ref TJERR "Error codes".
1217  */
1218 DLLEXPORT int tj3GetErrorCode(tjhandle handle);
1219 
1220 
1221 /**
1222  * Set the value of a parameter.
1223  *
1224  * @param handle handle to a TurboJPEG instance
1225  *
1226  * @param param one of the @ref TJPARAM "parameters"
1227  *
1228  * @param value value of the parameter (refer to @ref TJPARAM
1229  * "parameter documentation")
1230  *
1231  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1232  */
1233 DLLEXPORT int tj3Set(tjhandle handle, int param, int value);
1234 
1235 
1236 /**
1237  * Get the value of a parameter.
1238  *
1239  * @param handle handle to a TurboJPEG instance
1240  *
1241  * @param param one of the @ref TJPARAM "parameters"
1242  *
1243  * @return the value of the specified parameter, or -1 if the value is unknown.
1244  */
1245 DLLEXPORT int tj3Get(tjhandle handle, int param);
1246 
1247 
1248 /**
1249  * Allocate a byte buffer for use with TurboJPEG.  You should always use this
1250  * function to allocate the JPEG destination buffer(s) for the compression and
1251  * transform functions unless you are disabling automatic buffer (re)allocation
1252  * (by setting #TJPARAM_NOREALLOC.)
1253  *
1254  * @param bytes the number of bytes to allocate
1255  *
1256  * @return a pointer to a newly-allocated buffer with the specified number of
1257  * bytes.
1258  *
1259  * @see tj3Free()
1260  */
1261 DLLEXPORT void *tj3Alloc(size_t bytes);
1262 
1263 
1264 /**
1265  * Free a byte buffer previously allocated by TurboJPEG.  You should always use
1266  * this function to free JPEG destination buffer(s) that were automatically
1267  * (re)allocated by the compression and transform functions or that were
1268  * manually allocated using #tj3Alloc().
1269  *
1270  * @param buffer address of the buffer to free.  If the address is NULL, then
1271  * this function has no effect.
1272  *
1273  * @see tj3Alloc()
1274  */
1275 DLLEXPORT void tj3Free(void *buffer);
1276 
1277 
1278 /**
1279  * The maximum size of the buffer (in bytes) required to hold a JPEG image with
1280  * the given parameters.  The number of bytes returned by this function is
1281  * larger than the size of the uncompressed source image.  The reason for this
1282  * is that the JPEG format uses 16-bit coefficients, so it is possible for a
1283  * very high-quality source image with very high-frequency content to expand
1284  * rather than compress when converted to the JPEG format.  Such images
1285  * represent very rare corner cases, but since there is no way to predict the
1286  * size of a JPEG image prior to compression, the corner cases have to be
1287  * handled.
1288  *
1289  * @param width width (in pixels) of the image
1290  *
1291  * @param height height (in pixels) of the image
1292  *
1293  * @param jpegSubsamp the level of chrominance subsampling to be used when
1294  * generating the JPEG image (see @ref TJSAMP
1295  * "Chrominance subsampling options".)  #TJSAMP_UNKNOWN is treated like
1296  * #TJSAMP_444, since a buffer large enough to hold a JPEG image with no
1297  * subsampling should also be large enough to hold a JPEG image with an
1298  * arbitrary level of subsampling.  Note that lossless JPEG images always
1299  * use #TJSAMP_444.
1300  *
1301  * @return the maximum size of the buffer (in bytes) required to hold the
1302  * image, or 0 if the arguments are out of bounds.
1303  */
1304 DLLEXPORT size_t tj3JPEGBufSize(int width, int height, int jpegSubsamp);
1305 
1306 
1307 /**
1308  * The size of the buffer (in bytes) required to hold a unified planar YUV
1309  * image with the given parameters.
1310  *
1311  * @param width width (in pixels) of the image
1312  *
1313  * @param align row alignment (in bytes) of the image (must be a power of 2.)
1314  * Setting this parameter to n specifies that each row in each plane of the
1315  * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
1316  *
1317  * @param height height (in pixels) of the image
1318  *
1319  * @param subsamp level of chrominance subsampling in the image (see
1320  * @ref TJSAMP "Chrominance subsampling options".)
1321  *
1322  * @return the size of the buffer (in bytes) required to hold the image, or 0
1323  * if the arguments are out of bounds.
1324  */
1325 DLLEXPORT size_t tj3YUVBufSize(int width, int align, int height, int subsamp);
1326 
1327 
1328 /**
1329  * The size of the buffer (in bytes) required to hold a YUV image plane with
1330  * the given parameters.
1331  *
1332  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1333  *
1334  * @param width width (in pixels) of the YUV image.  NOTE: This is the width of
1335  * the whole image, not the plane width.
1336  *
1337  * @param stride bytes per row in the image plane.  Setting this to 0 is the
1338  * equivalent of setting it to the plane width.
1339  *
1340  * @param height height (in pixels) of the YUV image.  NOTE: This is the height
1341  * of the whole image, not the plane height.
1342  *
1343  * @param subsamp level of chrominance subsampling in the image (see
1344  * @ref TJSAMP "Chrominance subsampling options".)
1345  *
1346  * @return the size of the buffer (in bytes) required to hold the YUV image
1347  * plane, or 0 if the arguments are out of bounds.
1348  */
1349 DLLEXPORT size_t tj3YUVPlaneSize(int componentID, int width, int stride,
1350                                  int height, int subsamp);
1351 
1352 
1353 /**
1354  * The plane width of a YUV image plane with the given parameters.  Refer to
1355  * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
1356  *
1357  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1358  *
1359  * @param width width (in pixels) of the YUV image
1360  *
1361  * @param subsamp level of chrominance subsampling in the image (see
1362  * @ref TJSAMP "Chrominance subsampling options".)
1363  *
1364  * @return the plane width of a YUV image plane with the given parameters, or 0
1365  * if the arguments are out of bounds.
1366  */
1367 DLLEXPORT int tj3YUVPlaneWidth(int componentID, int width, int subsamp);
1368 
1369 
1370 /**
1371  * The plane height of a YUV image plane with the given parameters.  Refer to
1372  * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
1373  *
1374  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1375  *
1376  * @param height height (in pixels) of the YUV image
1377  *
1378  * @param subsamp level of chrominance subsampling in the image (see
1379  * @ref TJSAMP "Chrominance subsampling options".)
1380  *
1381  * @return the plane height of a YUV image plane with the given parameters, or
1382  * 0 if the arguments are out of bounds.
1383  */
1384 DLLEXPORT int tj3YUVPlaneHeight(int componentID, int height, int subsamp);
1385 
1386 
1387 /**
1388  * Compress an 8-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1389  * an 8-bit-per-sample JPEG image.
1390  *
1391  * @param handle handle to a TurboJPEG instance that has been initialized for
1392  * compression
1393  *
1394  * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1395  * or CMYK source image to be compressed.  This buffer should normally be
1396  * `pitch * height` samples in size.  However, you can also use this parameter
1397  * to compress from a specific region of a larger buffer.
1398  *
1399  * @param width width (in pixels) of the source image
1400  *
1401  * @param pitch samples per row in the source image.  Normally this should be
1402  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1403  * (Setting this parameter to 0 is the equivalent of setting it to
1404  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1405  * parameter to specify the row alignment/padding of the source image, to skip
1406  * rows, or to compress from a specific region of a larger buffer.
1407  *
1408  * @param height height (in pixels) of the source image
1409  *
1410  * @param pixelFormat pixel format of the source image (see @ref TJPF
1411  * "Pixel formats".)
1412  *
1413  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1414  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1415  * accommodate the size of the JPEG image.  Thus, you can choose to:
1416  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1417  * let TurboJPEG grow the buffer as needed,
1418  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1419  * or
1420  * -# pre-allocate the buffer to a "worst case" size determined by calling
1421  * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1422  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1423  * .
1424  * If you choose option 1, then `*jpegSize` should be set to the size of your
1425  * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1426  * you should always check `*jpegBuf` upon return from this function, as it may
1427  * have changed.
1428  *
1429  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1430  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1431  * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1432  * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1433  * JPEG buffer that is being reused from a previous call to one of the JPEG
1434  * compression functions, then `*jpegSize` is ignored.
1435  *
1436  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1437  * and #tj3GetErrorCode().)
1438  */
1439 DLLEXPORT int tj3Compress8(tjhandle handle, const unsigned char *srcBuf,
1440                            int width, int pitch, int height, int pixelFormat,
1441                            unsigned char **jpegBuf, size_t *jpegSize);
1442 
1443 /**
1444  * Compress a 12-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1445  * a 12-bit-per-sample JPEG image.
1446  *
1447  * \details \copydetails tj3Compress8()
1448  */
1449 DLLEXPORT int tj3Compress12(tjhandle handle, const short *srcBuf, int width,
1450                             int pitch, int height, int pixelFormat,
1451                             unsigned char **jpegBuf, size_t *jpegSize);
1452 
1453 /**
1454  * Compress a 16-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1455  * a 16-bit-per-sample lossless JPEG image.
1456  *
1457  * \details \copydetails tj3Compress8()
1458  */
1459 DLLEXPORT int tj3Compress16(tjhandle handle, const unsigned short *srcBuf,
1460                             int width, int pitch, int height, int pixelFormat,
1461                             unsigned char **jpegBuf, size_t *jpegSize);
1462 
1463 
1464 /**
1465  * Compress a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into
1466  * an 8-bit-per-sample JPEG image.
1467  *
1468  * @param handle handle to a TurboJPEG instance that has been initialized for
1469  * compression
1470  *
1471  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1472  * (or just a Y plane, if compressing a grayscale image) that contain a YUV
1473  * source image to be compressed.  These planes can be contiguous or
1474  * non-contiguous in memory.  The size of each plane should match the value
1475  * returned by #tj3YUVPlaneSize() for the given image width, height, strides,
1476  * and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer to
1477  * @ref YUVnotes "YUV Image Format Notes" for more details.
1478  *
1479  * @param width width (in pixels) of the source image.  If the width is not an
1480  * even multiple of the iMCU width (see #tjMCUWidth), then an intermediate
1481  * buffer copy will be performed.
1482  *
1483  * @param strides an array of integers, each specifying the number of bytes per
1484  * row in the corresponding plane of the YUV source image.  Setting the stride
1485  * for any plane to 0 is the same as setting it to the plane width (see
1486  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1487  * strides for all planes will be set to their respective plane widths.  You
1488  * can adjust the strides in order to specify an arbitrary amount of row
1489  * padding in each plane or to create a JPEG image from a subregion of a larger
1490  * planar YUV image.
1491  *
1492  * @param height height (in pixels) of the source image.  If the height is not
1493  * an even multiple of the iMCU height (see #tjMCUHeight), then an intermediate
1494  * buffer copy will be performed.
1495  *
1496  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1497  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1498  * accommodate the size of the JPEG image.  Thus, you can choose to:
1499  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1500  * let TurboJPEG grow the buffer as needed,
1501  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1502  * or
1503  * -# pre-allocate the buffer to a "worst case" size determined by calling
1504  * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1505  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1506  * .
1507  * If you choose option 1, then `*jpegSize` should be set to the size of your
1508  * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1509  * you should always check `*jpegBuf` upon return from this function, as it may
1510  * have changed.
1511  *
1512  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1513  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1514  * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1515  * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1516  * JPEG buffer that is being reused from a previous call to one of the JPEG
1517  * compression functions, then `*jpegSize` is ignored.
1518  *
1519  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1520  * and #tj3GetErrorCode().)
1521  */
1522 DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle,
1523                                         const unsigned char * const *srcPlanes,
1524                                         int width, const int *strides,
1525                                         int height, unsigned char **jpegBuf,
1526                                         size_t *jpegSize);
1527 
1528 
1529 /**
1530  * Compress an 8-bit-per-sample unified planar YUV image into an
1531  * 8-bit-per-sample JPEG image.
1532  *
1533  * @param handle handle to a TurboJPEG instance that has been initialized for
1534  * compression
1535  *
1536  * @param srcBuf pointer to a buffer containing a unified planar YUV source
1537  * image to be compressed.  The size of this buffer should match the value
1538  * returned by #tj3YUVBufSize() for the given image width, height, row
1539  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1540  * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1541  * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1542  *
1543  * @param width width (in pixels) of the source image.  If the width is not an
1544  * even multiple of the iMCU width (see #tjMCUWidth), then an intermediate
1545  * buffer copy will be performed.
1546  *
1547  * @param align row alignment (in bytes) of the source image (must be a power
1548  * of 2.)  Setting this parameter to n indicates that each row in each plane of
1549  * the source image is padded to the nearest multiple of n bytes
1550  * (1 = unpadded.)
1551  *
1552  * @param height height (in pixels) of the source image.  If the height is not
1553  * an even multiple of the iMCU height (see #tjMCUHeight), then an intermediate
1554  * buffer copy will be performed.
1555  *
1556  * @param jpegBuf address of a pointer to a byte buffer that will receive the
1557  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1558  * accommodate the size of the JPEG image.  Thus, you can choose to:
1559  * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1560  * let TurboJPEG grow the buffer as needed,
1561  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1562  * or
1563  * -# pre-allocate the buffer to a "worst case" size determined by calling
1564  * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1565  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1566  * .
1567  * If you choose option 1, then `*jpegSize` should be set to the size of your
1568  * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1569  * you should always check `*jpegBuf` upon return from this function, as it may
1570  * have changed.
1571  *
1572  * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1573  * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1574  * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1575  * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1576  * JPEG buffer that is being reused from a previous call to one of the JPEG
1577  * compression functions, then `*jpegSize` is ignored.
1578  *
1579  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1580  * and #tj3GetErrorCode().)
1581  */
1582 DLLEXPORT int tj3CompressFromYUV8(tjhandle handle,
1583                                   const unsigned char *srcBuf, int width,
1584                                   int align, int height,
1585                                   unsigned char **jpegBuf, size_t *jpegSize);
1586 
1587 
1588 /**
1589  * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into separate
1590  * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes.  This function performs
1591  * color conversion (which is accelerated in the libjpeg-turbo implementation)
1592  * but does not execute any of the other steps in the JPEG compression process.
1593  *
1594  * @param handle handle to a TurboJPEG instance that has been initialized for
1595  * compression
1596  *
1597  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1598  * source image to be encoded.  This buffer should normally be `pitch * height`
1599  * bytes in size.  However, you can also use this parameter to encode from a
1600  * specific region of a larger buffer.
1601  *
1602  *
1603  * @param width width (in pixels) of the source image
1604  *
1605  * @param pitch bytes per row in the source image.  Normally this should be
1606  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1607  * (Setting this parameter to 0 is the equivalent of setting it to
1608  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1609  * parameter to specify the row alignment/padding of the source image, to skip
1610  * rows, or to encode from a specific region of a larger packed-pixel image.
1611  *
1612  * @param height height (in pixels) of the source image
1613  *
1614  * @param pixelFormat pixel format of the source image (see @ref TJPF
1615  * "Pixel formats".)
1616  *
1617  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1618  * (or just a Y plane, if generating a grayscale image) that will receive the
1619  * encoded image.  These planes can be contiguous or non-contiguous in memory.
1620  * Use #tj3YUVPlaneSize() to determine the appropriate size for each plane
1621  * based on the image width, height, strides, and level of chrominance
1622  * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
1623  * "YUV Image Format Notes" for more details.
1624  *
1625  * @param strides an array of integers, each specifying the number of bytes per
1626  * row in the corresponding plane of the YUV image.  Setting the stride for any
1627  * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
1628  * "YUV Image Format Notes".)  If `strides` is NULL, then the strides for all
1629  * planes will be set to their respective plane widths.  You can adjust the
1630  * strides in order to add an arbitrary amount of row padding to each plane or
1631  * to encode an RGB or grayscale image into a subregion of a larger planar YUV
1632  * image.
1633  *
1634  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1635  * and #tj3GetErrorCode().)
1636  */
1637 DLLEXPORT int tj3EncodeYUVPlanes8(tjhandle handle, const unsigned char *srcBuf,
1638                                   int width, int pitch, int height,
1639                                   int pixelFormat, unsigned char **dstPlanes,
1640                                   int *strides);
1641 
1642 
1643 /**
1644  * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into an
1645  * 8-bit-per-sample unified planar YUV image.  This function performs color
1646  * conversion (which is accelerated in the libjpeg-turbo implementation) but
1647  * does not execute any of the other steps in the JPEG compression process.
1648  *
1649  * @param handle handle to a TurboJPEG instance that has been initialized for
1650  * compression
1651  *
1652  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1653  * source image to be encoded.  This buffer should normally be `pitch * height`
1654  * bytes in size.  However, you can also use this parameter to encode from a
1655  * specific region of a larger buffer.
1656  *
1657  * @param width width (in pixels) of the source image
1658  *
1659  * @param pitch bytes per row in the source image.  Normally this should be
1660  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1661  * (Setting this parameter to 0 is the equivalent of setting it to
1662  * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1663  * parameter to specify the row alignment/padding of the source image, to skip
1664  * rows, or to encode from a specific region of a larger packed-pixel image.
1665  *
1666  * @param height height (in pixels) of the source image
1667  *
1668  * @param pixelFormat pixel format of the source image (see @ref TJPF
1669  * "Pixel formats".)
1670  *
1671  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1672  * image.  Use #tj3YUVBufSize() to determine the appropriate size for this
1673  * buffer based on the image width, height, row alignment, and level of
1674  * chrominance subsampling (see #TJPARAM_SUBSAMP.)  The Y, U (Cb), and V (Cr)
1675  * image planes will be stored sequentially in the buffer.  (Refer to
1676  * @ref YUVnotes "YUV Image Format Notes".)
1677  *
1678  * @param align row alignment (in bytes) of the YUV image (must be a power of
1679  * 2.)  Setting this parameter to n will cause each row in each plane of the
1680  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1681  * To generate images suitable for X Video, `align` should be set to 4.
1682  *
1683  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1684  * and #tj3GetErrorCode().)
1685  */
1686 DLLEXPORT int tj3EncodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1687                             int width, int pitch, int height, int pixelFormat,
1688                             unsigned char *dstBuf, int align);
1689 
1690 
1691 /**
1692  * Retrieve information about a JPEG image without decompressing it, or prime
1693  * the decompressor with quantization and Huffman tables.  If a JPEG image is
1694  * passed to this function, then the @ref TJPARAM "parameters" that describe
1695  * the JPEG image will be set when the function returns.
1696  *
1697  * @param handle handle to a TurboJPEG instance that has been initialized for
1698  * decompression
1699  *
1700  * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
1701  * "abbreviated table specification" (AKA "tables-only") datastream.  Passing a
1702  * tables-only datastream to this function primes the decompressor with
1703  * quantization and Huffman tables that can be used when decompressing
1704  * subsequent "abbreviated image" datastreams.  This is useful, for instance,
1705  * when decompressing video streams in which all frames share the same
1706  * quantization and Huffman tables.
1707  *
1708  * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1709  *
1710  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1711  * and #tj3GetErrorCode().)
1712  */
1713 DLLEXPORT int tj3DecompressHeader(tjhandle handle,
1714                                   const unsigned char *jpegBuf,
1715                                   size_t jpegSize);
1716 
1717 
1718 /**
1719  * Returns a list of fractional scaling factors that the JPEG decompressor
1720  * supports.
1721  *
1722  * @param numScalingFactors pointer to an integer variable that will receive
1723  * the number of elements in the list
1724  *
1725  * @return a pointer to a list of fractional scaling factors, or NULL if an
1726  * error is encountered (see #tj3GetErrorStr().)
1727  */
1728 DLLEXPORT tjscalingfactor *tj3GetScalingFactors(int *numScalingFactors);
1729 
1730 
1731 /**
1732  * Set the scaling factor for subsequent lossy decompression operations.
1733  *
1734  * @param handle handle to a TurboJPEG instance that has been initialized for
1735  * decompression
1736  *
1737  * @param scalingFactor #tjscalingfactor structure that specifies a fractional
1738  * scaling factor that the decompressor supports (see #tj3GetScalingFactors()),
1739  * or <tt>#TJUNSCALED</tt> for no scaling.  Decompression scaling is a function
1740  * of the IDCT algorithm, so scaling factors are generally limited to multiples
1741  * of 1/8.  If the entire JPEG image will be decompressed, then the width and
1742  * height of the scaled destination image can be determined by calling
1743  * #TJSCALED() with the JPEG width and height (see #TJPARAM_JPEGWIDTH and
1744  * #TJPARAM_JPEGHEIGHT) and the specified scaling factor.  When decompressing
1745  * into a planar YUV image, an intermediate buffer copy will be performed if
1746  * the width or height of the scaled destination image is not an even multiple
1747  * of the iMCU size (see #tjMCUWidth and #tjMCUHeight.)  Note that
1748  * decompression scaling is not available (and the specified scaling factor is
1749  * ignored) when decompressing lossless JPEG images (see #TJPARAM_LOSSLESS),
1750  * since the IDCT algorithm is not used with those images.  Note also that
1751  * #TJPARAM_FASTDCT is ignored when decompression scaling is enabled.
1752  *
1753  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1754  */
1755 DLLEXPORT int tj3SetScalingFactor(tjhandle handle,
1756                                   tjscalingfactor scalingFactor);
1757 
1758 
1759 /**
1760  * Set the cropping region for partially decompressing a lossy JPEG image into
1761  * a packed-pixel image
1762  *
1763  * @param handle handle to a TurboJPEG instance that has been initialized for
1764  * decompression
1765  *
1766  * @param croppingRegion #tjregion structure that specifies a subregion of the
1767  * JPEG image to decompress, or <tt>#TJUNCROPPED</tt> for no cropping.  The
1768  * left boundary of the cropping region must be evenly divisible by the scaled
1769  * iMCU width-- <tt>#TJSCALED(#tjMCUWidth[subsamp], scalingFactor)</tt>, where
1770  * `subsamp` is the level of chrominance subsampling in the JPEG image (see
1771  * #TJPARAM_SUBSAMP) and `scalingFactor` is the decompression scaling factor
1772  * (see #tj3SetScalingFactor().)  The cropping region should be specified
1773  * relative to the scaled image dimensions.  Unless `croppingRegion` is
1774  * <tt>#TJUNCROPPED</tt>, the JPEG header must be read (see
1775  * #tj3DecompressHeader()) prior to calling this function.
1776  *
1777  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1778  */
1779 DLLEXPORT int tj3SetCroppingRegion(tjhandle handle, tjregion croppingRegion);
1780 
1781 
1782 /**
1783  * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample
1784  * packed-pixel RGB, grayscale, or CMYK image.  The @ref TJPARAM "parameters"
1785  * that describe the JPEG image will be set when this function returns.
1786  *
1787  * @param handle handle to a TurboJPEG instance that has been initialized for
1788  * decompression
1789  *
1790  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1791  * decompress
1792  *
1793  * @param jpegSize size of the JPEG image (in bytes)
1794  *
1795  * @param dstBuf pointer to a buffer that will receive the packed-pixel
1796  * decompressed image.  This buffer should normally be
1797  * `pitch * destinationHeight` samples in size.  However, you can also use this
1798  * parameter to decompress into a specific region of a larger buffer.  NOTE:
1799  * If the JPEG image is lossy, then `destinationHeight` is either the scaled
1800  * JPEG height (see #TJSCALED(), #TJPARAM_JPEGHEIGHT, and
1801  * #tj3SetScalingFactor()) or the height of the cropping region (see
1802  * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
1803  * `destinationHeight` is the JPEG height.
1804  *
1805  * @param pitch samples per row in the destination image.  Normally this should
1806  * be set to <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>, if the
1807  * destination image should be unpadded.  (Setting this parameter to 0 is the
1808  * equivalent of setting it to
1809  * <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>.)  However, you can
1810  * also use this parameter to specify the row alignment/padding of the
1811  * destination image, to skip rows, or to decompress into a specific region of
1812  * a larger buffer.  NOTE: If the JPEG image is lossy, then `destinationWidth`
1813  * is either the scaled JPEG width (see #TJSCALED(), #TJPARAM_JPEGWIDTH, and
1814  * #tj3SetScalingFactor()) or the width of the cropping region (see
1815  * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
1816  * `destinationWidth` is the JPEG width.
1817  *
1818  * @param pixelFormat pixel format of the destination image (see @ref
1819  * TJPF "Pixel formats".)
1820  *
1821  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1822  * and #tj3GetErrorCode().)
1823  */
1824 DLLEXPORT int tj3Decompress8(tjhandle handle, const unsigned char *jpegBuf,
1825                              size_t jpegSize, unsigned char *dstBuf, int pitch,
1826                              int pixelFormat);
1827 
1828 /**
1829  * Decompress a 12-bit-per-sample JPEG image into a 12-bit-per-sample
1830  * packed-pixel RGB, grayscale, or CMYK image.
1831  *
1832  * \details \copydetails tj3Decompress8()
1833  */
1834 DLLEXPORT int tj3Decompress12(tjhandle handle, const unsigned char *jpegBuf,
1835                               size_t jpegSize, short *dstBuf, int pitch,
1836                               int pixelFormat);
1837 
1838 /**
1839  * Decompress a 16-bit-per-sample lossless JPEG image into a 16-bit-per-sample
1840  * packed-pixel RGB, grayscale, or CMYK image.
1841  *
1842  * \details \copydetails tj3Decompress8()
1843  */
1844 DLLEXPORT int tj3Decompress16(tjhandle handle, const unsigned char *jpegBuf,
1845                               size_t jpegSize, unsigned short *dstBuf,
1846                               int pitch, int pixelFormat);
1847 
1848 
1849 /**
1850  * Decompress an 8-bit-per-sample JPEG image into separate 8-bit-per-sample Y,
1851  * U (Cb), and V (Cr) image planes.  This function performs JPEG decompression
1852  * but leaves out the color conversion step, so a planar YUV image is generated
1853  * instead of a packed-pixel image.  The @ref TJPARAM "parameters" that
1854  * describe the JPEG image will be set when this function returns.
1855  *
1856  * @param handle handle to a TurboJPEG instance that has been initialized for
1857  * decompression
1858  *
1859  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1860  * decompress
1861  *
1862  * @param jpegSize size of the JPEG image (in bytes)
1863  *
1864  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1865  * (or just a Y plane, if decompressing a grayscale image) that will receive
1866  * the decompressed image.  These planes can be contiguous or non-contiguous in
1867  * memory.  Use #tj3YUVPlaneSize() to determine the appropriate size for each
1868  * plane based on the scaled JPEG width and height (see #TJSCALED(),
1869  * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()),
1870  * strides, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer
1871  * to @ref YUVnotes "YUV Image Format Notes" for more details.
1872  *
1873  * @param strides an array of integers, each specifying the number of bytes per
1874  * row in the corresponding plane of the YUV image.  Setting the stride for any
1875  * plane to 0 is the same as setting it to the scaled plane width (see
1876  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1877  * strides for all planes will be set to their respective scaled plane widths.
1878  * You can adjust the strides in order to add an arbitrary amount of row
1879  * padding to each plane or to decompress the JPEG image into a subregion of a
1880  * larger planar YUV image.
1881  *
1882  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1883  * and #tj3GetErrorCode().)
1884  */
1885 DLLEXPORT int tj3DecompressToYUVPlanes8(tjhandle handle,
1886                                         const unsigned char *jpegBuf,
1887                                         size_t jpegSize,
1888                                         unsigned char **dstPlanes,
1889                                         int *strides);
1890 
1891 
1892 /**
1893  * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample unified
1894  * planar YUV image.  This function performs JPEG decompression but leaves out
1895  * the color conversion step, so a planar YUV image is generated instead of a
1896  * packed-pixel image.  The @ref TJPARAM "parameters" that describe the JPEG
1897  * image will be set when this function returns.
1898  *
1899  * @param handle handle to a TurboJPEG instance that has been initialized for
1900  * decompression
1901  *
1902  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1903  * decompress
1904  *
1905  * @param jpegSize size of the JPEG image (in bytes)
1906  *
1907  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1908  * decompressed image.  Use #tj3YUVBufSize() to determine the appropriate size
1909  * for this buffer based on the scaled JPEG width and height (see #TJSCALED(),
1910  * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()), row
1911  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1912  * Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
1913  * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1914  *
1915  * @param align row alignment (in bytes) of the YUV image (must be a power of
1916  * 2.)  Setting this parameter to n will cause each row in each plane of the
1917  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1918  * To generate images suitable for X Video, `align` should be set to 4.
1919  *
1920  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1921  * and #tj3GetErrorCode().)
1922  */
1923 DLLEXPORT int tj3DecompressToYUV8(tjhandle handle,
1924                                   const unsigned char *jpegBuf,
1925                                   size_t jpegSize,
1926                                   unsigned char *dstBuf, int align);
1927 
1928 
1929 /**
1930  * Decode a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into an
1931  * 8-bit-per-sample packed-pixel RGB or grayscale image.  This function
1932  * performs color conversion (which is accelerated in the libjpeg-turbo
1933  * implementation) but does not execute any of the other steps in the JPEG
1934  * decompression process.
1935  *
1936  * @param handle handle to a TurboJPEG instance that has been initialized for
1937  * decompression
1938  *
1939  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1940  * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1941  * to be decoded.  These planes can be contiguous or non-contiguous in memory.
1942  * The size of each plane should match the value returned by #tj3YUVPlaneSize()
1943  * for the given image width, height, strides, and level of chrominance
1944  * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
1945  * "YUV Image Format Notes" for more details.
1946  *
1947  * @param strides an array of integers, each specifying the number of bytes per
1948  * row in the corresponding plane of the YUV source image.  Setting the stride
1949  * for any plane to 0 is the same as setting it to the plane width (see
1950  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1951  * strides for all planes will be set to their respective plane widths.  You
1952  * can adjust the strides in order to specify an arbitrary amount of row
1953  * padding in each plane or to decode a subregion of a larger planar YUV image.
1954  *
1955  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1956  * image.  This buffer should normally be `pitch * height` bytes in size.
1957  * However, you can also use this parameter to decode into a specific region of
1958  * a larger buffer.
1959  *
1960  * @param width width (in pixels) of the source and destination images
1961  *
1962  * @param pitch bytes per row in the destination image.  Normally this should
1963  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1964  * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
1965  * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
1966  * also use this parameter to specify the row alignment/padding of the
1967  * destination image, to skip rows, or to decode into a specific region of a
1968  * larger buffer.
1969  *
1970  * @param height height (in pixels) of the source and destination images
1971  *
1972  * @param pixelFormat pixel format of the destination image (see @ref TJPF
1973  * "Pixel formats".)
1974  *
1975  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1976  * and #tj3GetErrorCode().)
1977  */
1978 DLLEXPORT int tj3DecodeYUVPlanes8(tjhandle handle,
1979                                   const unsigned char * const *srcPlanes,
1980                                   const int *strides, unsigned char *dstBuf,
1981                                   int width, int pitch, int height,
1982                                   int pixelFormat);
1983 
1984 
1985 /**
1986  * Decode an 8-bit-per-sample unified planar YUV image into an 8-bit-per-sample
1987  * packed-pixel RGB or grayscale image.  This function performs color
1988  * conversion (which is accelerated in the libjpeg-turbo implementation) but
1989  * does not execute any of the other steps in the JPEG decompression process.
1990  *
1991  * @param handle handle to a TurboJPEG instance that has been initialized for
1992  * decompression
1993  *
1994  * @param srcBuf pointer to a buffer containing a unified planar YUV source
1995  * image to be decoded.  The size of this buffer should match the value
1996  * returned by #tj3YUVBufSize() for the given image width, height, row
1997  * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1998  * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1999  * source buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
2000  *
2001  * @param align row alignment (in bytes) of the YUV source image (must be a
2002  * power of 2.)  Setting this parameter to n indicates that each row in each
2003  * plane of the YUV source image is padded to the nearest multiple of n bytes
2004  * (1 = unpadded.)
2005  *
2006  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
2007  * image.  This buffer should normally be `pitch * height` bytes in size.
2008  * However, you can also use this parameter to decode into a specific region of
2009  * a larger buffer.
2010  *
2011  * @param width width (in pixels) of the source and destination images
2012  *
2013  * @param pitch bytes per row in the destination image.  Normally this should
2014  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
2015  * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
2016  * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
2017  * also use this parameter to specify the row alignment/padding of the
2018  * destination image, to skip rows, or to decode into a specific region of a
2019  * larger buffer.
2020  *
2021  * @param height height (in pixels) of the source and destination images
2022  *
2023  * @param pixelFormat pixel format of the destination image (see @ref TJPF
2024  * "Pixel formats".)
2025  *
2026  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2027  * and #tj3GetErrorCode().)
2028  */
2029 DLLEXPORT int tj3DecodeYUV8(tjhandle handle, const unsigned char *srcBuf,
2030                             int align, unsigned char *dstBuf, int width,
2031                             int pitch, int height, int pixelFormat);
2032 
2033 
2034 /**
2035  * Losslessly transform a JPEG image into another JPEG image.  Lossless
2036  * transforms work by moving the raw DCT coefficients from one JPEG image
2037  * structure to another without altering the values of the coefficients.  While
2038  * this is typically faster than decompressing the image, transforming it, and
2039  * re-compressing it, lossless transforms are not free.  Each lossless
2040  * transform requires reading and performing entropy decoding on all of the
2041  * coefficients in the source image, regardless of the size of the destination
2042  * image.  Thus, this function provides a means of generating multiple
2043  * transformed images from the same source or applying multiple transformations
2044  * simultaneously, in order to eliminate the need to read the source
2045  * coefficients multiple times.
2046  *
2047  * @param handle handle to a TurboJPEG instance that has been initialized for
2048  * lossless transformation
2049  *
2050  * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
2051  * transform
2052  *
2053  * @param jpegSize size of the JPEG source image (in bytes)
2054  *
2055  * @param n the number of transformed JPEG images to generate
2056  *
2057  * @param dstBufs pointer to an array of n byte buffers.  `dstBufs[i]` will
2058  * receive a JPEG image that has been transformed using the parameters in
2059  * `transforms[i]`.  TurboJPEG has the ability to reallocate the JPEG
2060  * destination buffer to accommodate the size of the transformed JPEG image.
2061  * Thus, you can choose to:
2062  * -# pre-allocate the JPEG destination buffer with an arbitrary size using
2063  * #tj3Alloc() and let TurboJPEG grow the buffer as needed,
2064  * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
2065  * you, or
2066  * -# pre-allocate the buffer to a "worst case" size determined by calling
2067  * #tj3JPEGBufSize() with the transformed or cropped width and height and the
2068  * level of subsampling used in the destination image (taking into account
2069  * grayscale conversion and transposition of the width and height.)  Under
2070  * normal circumstances, this should ensure that the buffer never has to be
2071  * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
2072  * Note, however, that there are some rare cases (such as transforming images
2073  * with a large amount of embedded Exif or ICC profile data) in which the
2074  * transformed JPEG image will be larger than the worst-case size, and
2075  * #TJPARAM_NOREALLOC cannot be used in those cases unless the embedded data is
2076  * discarded using #TJXOPT_COPYNONE.
2077  * .
2078  * If you choose option 1, then `dstSizes[i]` should be set to the size of your
2079  * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
2080  * you should always check `dstBufs[i]` upon return from this function, as it
2081  * may have changed.
2082  *
2083  * @param dstSizes pointer to an array of n size_t variables that will receive
2084  * the actual sizes (in bytes) of each transformed JPEG image.  If `dstBufs[i]`
2085  * points to a pre-allocated buffer, then `dstSizes[i]` should be set to the
2086  * size of the buffer.  Upon return, `dstSizes[i]` will contain the size of the
2087  * transformed JPEG image (in bytes.)
2088  *
2089  * @param transforms pointer to an array of n #tjtransform structures, each of
2090  * which specifies the transform parameters and/or cropping region for the
2091  * corresponding transformed JPEG image.
2092  *
2093  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
2094  * and #tj3GetErrorCode().)
2095  */
2096 DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
2097                            size_t jpegSize, int n, unsigned char **dstBufs,
2098                            size_t *dstSizes, const tjtransform *transforms);
2099 
2100 
2101 /**
2102  * Load an 8-bit-per-sample packed-pixel image from disk into memory.
2103  *
2104  * @param handle handle to a TurboJPEG instance
2105  *
2106  * @param filename name of a file containing a packed-pixel image in Windows
2107  * BMP or PBMPLUS (PPM/PGM) format.  Windows BMP files require 8-bit-per-sample
2108  * data precision.  If the data precision of the PBMPLUS file does not match
2109  * the target data precision, then upconverting or downconverting will be
2110  * performed.
2111  *
2112  * @param width pointer to an integer variable that will receive the width (in
2113  * pixels) of the packed-pixel image
2114  *
2115  * @param align row alignment (in samples) of the packed-pixel buffer to be
2116  * returned (must be a power of 2.)  Setting this parameter to n will cause all
2117  * rows in the buffer to be padded to the nearest multiple of n samples
2118  * (1 = unpadded.)
2119  *
2120  * @param height pointer to an integer variable that will receive the height
2121  * (in pixels) of the packed-pixel image
2122  *
2123  * @param pixelFormat pointer to an integer variable that specifies or will
2124  * receive the pixel format of the packed-pixel buffer.  The behavior of this
2125  * function varies depending on the value of `*pixelFormat` passed to the
2126  * function:
2127  * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
2128  * use the most optimal pixel format for the file type, and `*pixelFormat` will
2129  * contain the ID of that pixel format upon successful return from this
2130  * function.
2131  * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
2132  * grayscale colormap can be loaded.
2133  * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
2134  * converted using a quick & dirty algorithm that is suitable only for testing
2135  * purposes.  (Proper conversion between CMYK and other formats requires a
2136  * color management system.)
2137  * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
2138  * specified pixel format, and pixel format conversion will be performed if
2139  * necessary.
2140  *
2141  * @return a pointer to a newly-allocated buffer containing the packed-pixel
2142  * image, converted to the chosen pixel format and with the chosen row
2143  * alignment, or NULL if an error occurred (see #tj3GetErrorStr().)  This
2144  * buffer should be freed using #tj3Free().
2145  */
2146 DLLEXPORT unsigned char *tj3LoadImage8(tjhandle handle, const char *filename,
2147                                        int *width, int align, int *height,
2148                                        int *pixelFormat);
2149 
2150 /**
2151  * Load a 12-bit-per-sample packed-pixel image from disk into memory.
2152  *
2153  * \details \copydetails tj3LoadImage8()
2154  */
2155 DLLEXPORT short *tj3LoadImage12(tjhandle handle, const char *filename,
2156                                 int *width, int align, int *height,
2157                                 int *pixelFormat);
2158 
2159 /**
2160  * Load a 16-bit-per-sample packed-pixel image from disk into memory.
2161  *
2162  * \details \copydetails tj3LoadImage8()
2163  */
2164 DLLEXPORT unsigned short *tj3LoadImage16(tjhandle handle, const char *filename,
2165                                          int *width, int align, int *height,
2166                                          int *pixelFormat);
2167 
2168 
2169 /**
2170  * Save an 8-bit-per-sample packed-pixel image from memory to disk.
2171  *
2172  * @param handle handle to a TurboJPEG instance
2173  *
2174  * @param filename name of a file to which to save the packed-pixel image.  The
2175  * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
2176  * on the file extension.  Windows BMP files require 8-bit-per-sample data
2177  * precision.
2178  *
2179  * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2180  * or CMYK image to be saved
2181  *
2182  * @param width width (in pixels) of the packed-pixel image
2183  *
2184  * @param pitch samples per row in the packed-pixel image.  Setting this
2185  * parameter to 0 is the equivalent of setting it to
2186  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2187  *
2188  * @param height height (in pixels) of the packed-pixel image
2189  *
2190  * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2191  * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
2192  * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
2193  * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
2194  * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
2195  * converted to RGB using a quick & dirty algorithm that is suitable only for
2196  * testing purposes.  (Proper conversion between CMYK and other formats
2197  * requires a color management system.)
2198  *
2199  * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2200  */
2201 DLLEXPORT int tj3SaveImage8(tjhandle handle, const char *filename,
2202                             const unsigned char *buffer, int width, int pitch,
2203                             int height, int pixelFormat);
2204 
2205 /**
2206  * Save a 12-bit-per-sample packed-pixel image from memory to disk.
2207  *
2208  * \details \copydetails tj3SaveImage8()
2209  */
2210 DLLEXPORT int tj3SaveImage12(tjhandle handle, const char *filename,
2211                              const short *buffer, int width, int pitch,
2212                              int height, int pixelFormat);
2213 
2214 /**
2215  * Save a 16-bit-per-sample packed-pixel image from memory to disk.
2216  *
2217  * \details \copydetails tj3SaveImage8()
2218  */
2219 DLLEXPORT int tj3SaveImage16(tjhandle handle, const char *filename,
2220                              const unsigned short *buffer, int width,
2221                              int pitch, int height, int pixelFormat);
2222 
2223 
2224 /* Backward compatibility functions and macros (nothing to see here) */
2225 
2226 /* TurboJPEG 1.0+ */
2227 
2228 #define NUMSUBOPT  TJ_NUMSAMP
2229 #define TJ_444  TJSAMP_444
2230 #define TJ_422  TJSAMP_422
2231 #define TJ_420  TJSAMP_420
2232 #define TJ_411  TJSAMP_420
2233 #define TJ_GRAYSCALE  TJSAMP_GRAY
2234 
2235 #define TJ_BGR  1
2236 #define TJ_BOTTOMUP  TJFLAG_BOTTOMUP
2237 #define TJ_FORCEMMX  TJFLAG_FORCEMMX
2238 #define TJ_FORCESSE  TJFLAG_FORCESSE
2239 #define TJ_FORCESSE2  TJFLAG_FORCESSE2
2240 #define TJ_ALPHAFIRST  64
2241 #define TJ_FORCESSE3  TJFLAG_FORCESSE3
2242 #define TJ_FASTUPSAMPLE  TJFLAG_FASTUPSAMPLE
2243 
2244 #define TJPAD(width)  (((width) + 3) & (~3))
2245 
2246 DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
2247 
2248 DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
2249                          int pitch, int height, int pixelSize,
2250                          unsigned char *dstBuf, unsigned long *compressedSize,
2251                          int jpegSubsamp, int jpegQual, int flags);
2252 
2253 DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
2254                            unsigned long jpegSize, unsigned char *dstBuf,
2255                            int width, int pitch, int height, int pixelSize,
2256                            int flags);
2257 
2258 DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
2259                                  unsigned long jpegSize, int *width,
2260                                  int *height);
2261 
2262 DLLEXPORT int tjDestroy(tjhandle handle);
2263 
2264 DLLEXPORT char *tjGetErrorStr(void);
2265 
2266 DLLEXPORT tjhandle tjInitCompress(void);
2267 
2268 DLLEXPORT tjhandle tjInitDecompress(void);
2269 
2270 /* TurboJPEG 1.1+ */
2271 
2272 #define TJ_YUV  512
2273 
2274 DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
2275 
2276 DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
2277                                   unsigned long jpegSize, int *width,
2278                                   int *height, int *jpegSubsamp);
2279 
2280 DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
2281                                 unsigned long jpegSize, unsigned char *dstBuf,
2282                                 int flags);
2283 
2284 DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
2285                           int pitch, int height, int pixelSize,
2286                           unsigned char *dstBuf, int subsamp, int flags);
2287 
2288 /* TurboJPEG 1.2+ */
2289 
2290 #define TJFLAG_BOTTOMUP  2
2291 #define TJFLAG_FORCEMMX  8
2292 #define TJFLAG_FORCESSE  16
2293 #define TJFLAG_FORCESSE2  32
2294 #define TJFLAG_FORCESSE3  128
2295 #define TJFLAG_FASTUPSAMPLE  256
2296 #define TJFLAG_NOREALLOC  1024
2297 
2298 DLLEXPORT unsigned char *tjAlloc(int bytes);
2299 
2300 DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
2301 
2302 DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
2303 
2304 DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
2305                           int width, int pitch, int height, int pixelFormat,
2306                           unsigned char **jpegBuf, unsigned long *jpegSize,
2307                           int jpegSubsamp, int jpegQual, int flags);
2308 
2309 DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
2310                             unsigned long jpegSize, unsigned char *dstBuf,
2311                             int width, int pitch, int height, int pixelFormat,
2312                             int flags);
2313 
2314 DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
2315                            int pitch, int height, int pixelFormat,
2316                            unsigned char *dstBuf, int subsamp, int flags);
2317 
2318 DLLEXPORT void tjFree(unsigned char *buffer);
2319 
2320 DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
2321 
2322 DLLEXPORT tjhandle tjInitTransform(void);
2323 
2324 DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
2325                             unsigned long jpegSize, int n,
2326                             unsigned char **dstBufs, unsigned long *dstSizes,
2327                             tjtransform *transforms, int flags);
2328 
2329 /* TurboJPEG 1.2.1+ */
2330 
2331 #define TJFLAG_FASTDCT  2048
2332 #define TJFLAG_ACCURATEDCT  4096
2333 
2334 /* TurboJPEG 1.4+ */
2335 
2336 DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
2337                                       int subsamp);
2338 
2339 DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
2340                                 int width, int align, int height, int subsamp,
2341                                 unsigned char **jpegBuf,
2342                                 unsigned long *jpegSize, int jpegQual,
2343                                 int flags);
2344 
2345 DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
2346                                       const unsigned char **srcPlanes,
2347                                       int width, const int *strides,
2348                                       int height, int subsamp,
2349                                       unsigned char **jpegBuf,
2350                                       unsigned long *jpegSize, int jpegQual,
2351                                       int flags);
2352 
2353 DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
2354                           int align, int subsamp, unsigned char *dstBuf,
2355                           int width, int pitch, int height, int pixelFormat,
2356                           int flags);
2357 
2358 DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
2359                                 const unsigned char **srcPlanes,
2360                                 const int *strides, int subsamp,
2361                                 unsigned char *dstBuf, int width, int pitch,
2362                                 int height, int pixelFormat, int flags);
2363 
2364 DLLEXPORT int tjDecompressHeader3(tjhandle handle,
2365                                   const unsigned char *jpegBuf,
2366                                   unsigned long jpegSize, int *width,
2367                                   int *height, int *jpegSubsamp,
2368                                   int *jpegColorspace);
2369 
2370 DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
2371                                  unsigned long jpegSize, unsigned char *dstBuf,
2372                                  int width, int align, int height, int flags);
2373 
2374 DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
2375                                       const unsigned char *jpegBuf,
2376                                       unsigned long jpegSize,
2377                                       unsigned char **dstPlanes, int width,
2378                                       int *strides, int height, int flags);
2379 
2380 DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
2381                            int width, int pitch, int height, int pixelFormat,
2382                            unsigned char *dstBuf, int align, int subsamp,
2383                            int flags);
2384 
2385 DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
2386                                 int width, int pitch, int height,
2387                                 int pixelFormat, unsigned char **dstPlanes,
2388                                 int *strides, int subsamp, int flags);
2389 
2390 DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
2391 
2392 DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
2393                                        int height, int subsamp);
2394 
2395 DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
2396 
2397 /* TurboJPEG 2.0+ */
2398 
2399 #define TJFLAG_STOPONWARNING  8192
2400 #define TJFLAG_PROGRESSIVE  16384
2401 
2402 DLLEXPORT int tjGetErrorCode(tjhandle handle);
2403 
2404 DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
2405 
2406 DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
2407                                      int align, int *height, int *pixelFormat,
2408                                      int flags);
2409 
2410 DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
2411                           int width, int pitch, int height, int pixelFormat,
2412                           int flags);
2413 
2414 /* TurboJPEG 2.1+ */
2415 
2416 #define TJFLAG_LIMITSCANS  32768
2417 
2418 /**
2419  * @}
2420  */
2421 
2422 #ifdef __cplusplus
2423 }
2424 #endif
2425 
2426 #endif