Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-17 09:56:13

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