xref: /aosp_15_r20/external/libjpeg-turbo/turbojpeg.h (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1 /*
2  * Copyright (C)2009-2015, 2017, 2020-2021, 2023 D. R. Commander.
3  *                                               All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of the libjpeg-turbo Project nor the names of its
14  *   contributors may be used to endorse or promote products derived from this
15  *   software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __TURBOJPEG_H__
31 #define __TURBOJPEG_H__
32 
33 #if defined(_WIN32) && defined(DLLDEFINE)
34 #define DLLEXPORT  __declspec(dllexport)
35 #else
36 #define DLLEXPORT
37 #endif
38 #define DLLCALL
39 
40 
41 /**
42  * @addtogroup TurboJPEG
43  * TurboJPEG API.  This API provides an interface for generating, decoding, and
44  * transforming planar YUV and JPEG images in memory.
45  *
46  * @anchor YUVnotes
47  * YUV Image Format Notes
48  * ----------------------
49  * Technically, the JPEG format uses the YCbCr colorspace (which is technically
50  * not a colorspace but a color transform), but per the convention of the
51  * digital video community, the TurboJPEG API uses "YUV" to refer to an image
52  * format consisting of Y, Cb, and Cr image planes.
53  *
54  * Each plane is simply a 2D array of bytes, each byte representing the value
55  * of one of the components (Y, Cb, or Cr) at a particular location in the
56  * image.  The width and height of each plane are determined by the image
57  * width, height, and level of chrominance subsampling.  The luminance plane
58  * width is the image width padded to the nearest multiple of the horizontal
59  * subsampling factor (1 in the case of 4:4:4, grayscale, or 4:4:0; 2 in the
60  * case of 4:2:2 or 4:2:0; 4 in the case of 4:1:1.)  Similarly, the luminance
61  * plane height is the image height padded to the nearest multiple of the
62  * vertical subsampling factor (1 in the case of 4:4:4, 4:2:2, grayscale, or
63  * 4:1:1; 2 in the case of 4:2:0 or 4:4:0.)  This is irrespective of any
64  * additional padding that may be specified as an argument to the various YUV
65  * functions.  The chrominance plane width is equal to the luminance plane
66  * width divided by the horizontal subsampling factor, and the chrominance
67  * plane height is equal to the luminance plane height divided by the vertical
68  * subsampling factor.
69  *
70  * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
71  * used, then the luminance plane would be 36 x 35 bytes, and each of the
72  * chrominance planes would be 18 x 35 bytes.  If you specify a row alignment
73  * of 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes,
74  * and each of the chrominance planes would be 20 x 35 bytes.
75  *
76  * @{
77  */
78 
79 
80 /**
81  * The number of chrominance subsampling options
82  */
83 #define TJ_NUMSAMP  6
84 
85 /**
86  * Chrominance subsampling options.
87  * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
88  * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
89  * the Cb and Cr (chrominance) components can be discarded or averaged together
90  * to produce a smaller image with little perceptible loss of image clarity.
91  * (The human eye is more sensitive to small changes in brightness than to
92  * small changes in color.)  This is called "chrominance subsampling".
93  */
94 enum TJSAMP {
95   /**
96    * 4:4:4 chrominance subsampling (no chrominance subsampling).  The JPEG or
97    * YUV image will contain one chrominance component for every pixel in the
98    * source image.
99    */
100   TJSAMP_444 = 0,
101   /**
102    * 4:2:2 chrominance subsampling.  The JPEG or YUV image will contain one
103    * chrominance component for every 2x1 block of pixels in the source image.
104    */
105   TJSAMP_422,
106   /**
107    * 4:2:0 chrominance subsampling.  The JPEG or YUV image will contain one
108    * chrominance component for every 2x2 block of pixels in the source image.
109    */
110   TJSAMP_420,
111   /**
112    * Grayscale.  The JPEG or YUV image will contain no chrominance components.
113    */
114   TJSAMP_GRAY,
115   /**
116    * 4:4:0 chrominance subsampling.  The JPEG or YUV image will contain one
117    * chrominance component for every 1x2 block of pixels in the source image.
118    *
119    * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
120    */
121   TJSAMP_440,
122   /**
123    * 4:1:1 chrominance subsampling.  The JPEG or YUV image will contain one
124    * chrominance component for every 4x1 block of pixels in the source image.
125    * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
126    * same size as those compressed with 4:2:0 subsampling, and in the
127    * aggregate, both subsampling methods produce approximately the same
128    * perceptual quality.  However, 4:1:1 is better able to reproduce sharp
129    * horizontal features.
130    *
131    * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
132    */
133   TJSAMP_411
134 };
135 
136 /**
137  * MCU block width (in pixels) for a given level of chrominance subsampling.
138  * MCU block sizes:
139  * - 8x8 for no subsampling or grayscale
140  * - 16x8 for 4:2:2
141  * - 8x16 for 4:4:0
142  * - 16x16 for 4:2:0
143  * - 32x8 for 4:1:1
144  */
145 static const int tjMCUWidth[TJ_NUMSAMP]  = { 8, 16, 16, 8, 8, 32 };
146 
147 /**
148  * MCU block height (in pixels) for a given level of chrominance subsampling.
149  * MCU block sizes:
150  * - 8x8 for no subsampling or grayscale
151  * - 16x8 for 4:2:2
152  * - 8x16 for 4:4:0
153  * - 16x16 for 4:2:0
154  * - 32x8 for 4:1:1
155  */
156 static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8 };
157 
158 
159 /**
160  * The number of pixel formats
161  */
162 #define TJ_NUMPF  12
163 
164 /**
165  * Pixel formats
166  */
167 enum TJPF {
168   /**
169    * RGB pixel format.  The red, green, and blue components in the image are
170    * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
171    * address within each pixel.
172    */
173   TJPF_RGB = 0,
174   /**
175    * BGR pixel format.  The red, green, and blue components in the image are
176    * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
177    * address within each pixel.
178    */
179   TJPF_BGR,
180   /**
181    * RGBX pixel format.  The red, green, and blue components in the image are
182    * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
183    * address within each pixel.  The X component is ignored when compressing
184    * and undefined when decompressing.
185    */
186   TJPF_RGBX,
187   /**
188    * BGRX pixel format.  The red, green, and blue components in the image are
189    * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
190    * address within each pixel.  The X component is ignored when compressing
191    * and undefined when decompressing.
192    */
193   TJPF_BGRX,
194   /**
195    * XBGR pixel format.  The red, green, and blue components in the image are
196    * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
197    * address within each pixel.  The X component is ignored when compressing
198    * and undefined when decompressing.
199    */
200   TJPF_XBGR,
201   /**
202    * XRGB pixel format.  The red, green, and blue components in the image are
203    * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
204    * address within each pixel.  The X component is ignored when compressing
205    * and undefined when decompressing.
206    */
207   TJPF_XRGB,
208   /**
209    * Grayscale pixel format.  Each 1-byte pixel represents a luminance
210    * (brightness) level from 0 to 255.
211    */
212   TJPF_GRAY,
213   /**
214    * RGBA pixel format.  This is the same as @ref TJPF_RGBX, except that when
215    * decompressing, the X component is guaranteed to be 0xFF, which can be
216    * interpreted as an opaque alpha channel.
217    */
218   TJPF_RGBA,
219   /**
220    * BGRA pixel format.  This is the same as @ref TJPF_BGRX, except that when
221    * decompressing, the X component is guaranteed to be 0xFF, which can be
222    * interpreted as an opaque alpha channel.
223    */
224   TJPF_BGRA,
225   /**
226    * ABGR pixel format.  This is the same as @ref TJPF_XBGR, except that when
227    * decompressing, the X component is guaranteed to be 0xFF, which can be
228    * interpreted as an opaque alpha channel.
229    */
230   TJPF_ABGR,
231   /**
232    * ARGB pixel format.  This is the same as @ref TJPF_XRGB, except that when
233    * decompressing, the X component is guaranteed to be 0xFF, which can be
234    * interpreted as an opaque alpha channel.
235    */
236   TJPF_ARGB,
237   /**
238    * CMYK pixel format.  Unlike RGB, which is an additive color model used
239    * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
240    * color model used primarily for printing.  In the CMYK color model, the
241    * value of each color component typically corresponds to an amount of cyan,
242    * magenta, yellow, or black ink that is applied to a white background.  In
243    * order to convert between CMYK and RGB, it is necessary to use a color
244    * management system (CMS.)  A CMS will attempt to map colors within the
245    * printer's gamut to perceptually similar colors in the display's gamut and
246    * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
247    * be defined with a simple formula.  Thus, such a conversion is out of scope
248    * for a codec library.  However, the TurboJPEG API allows for compressing
249    * packed-pixel CMYK images into YCCK JPEG images (see #TJCS_YCCK) and
250    * decompressing YCCK JPEG images into packed-pixel CMYK images.
251    */
252   TJPF_CMYK,
253   /**
254    * Unknown pixel format.  Currently this is only used by #tjLoadImage().
255    */
256   TJPF_UNKNOWN = -1
257 };
258 
259 /**
260  * Red offset (in bytes) for a given pixel format.  This specifies the number
261  * of bytes that the red component is offset from the start of the pixel.  For
262  * instance, if a pixel of format TJPF_BGRX is stored in
263  * `unsigned char pixel[]`, then the red component will be
264  *`pixel[tjRedOffset[TJPF_BGRX]]`.  This will be -1 if the pixel format does
265  * not have a red component.
266  */
267 static const int tjRedOffset[TJ_NUMPF] = {
268   0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
269 };
270 /**
271  * Green offset (in bytes) for a given pixel format.  This specifies the number
272  * of bytes that the green component is offset from the start of the pixel.
273  * For instance, if a pixel of format TJPF_BGRX is stored in
274  * `unsigned char pixel[]`, then the green component will be
275  * `pixel[tjGreenOffset[TJPF_BGRX]]`.  This will be -1 if the pixel format does
276  * not have a green component.
277  */
278 static const int tjGreenOffset[TJ_NUMPF] = {
279   1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
280 };
281 /**
282  * Blue offset (in bytes) for a given pixel format.  This specifies the number
283  * of bytes that the blue component is offset from the start of the pixel.  For
284  * instance, if a pixel of format TJPF_BGRX is stored in
285  * `unsigned char pixel[]`, then the blue component will be
286  * `pixel[tjBlueOffset[TJPF_BGRX]]`.  This will be -1 if the pixel format does
287  * not have a blue component.
288  */
289 static const int tjBlueOffset[TJ_NUMPF] = {
290   2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
291 };
292 /**
293  * Alpha offset (in bytes) for a given pixel format.  This specifies the number
294  * of bytes that the alpha component is offset from the start of the pixel.
295  * For instance, if a pixel of format TJPF_BGRA is stored in
296  * `unsigned char pixel[]`, then the alpha component will be
297  * `pixel[tjAlphaOffset[TJPF_BGRA]]`.  This will be -1 if the pixel format does
298  * not have an alpha component.
299  */
300 static const int tjAlphaOffset[TJ_NUMPF] = {
301   -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
302 };
303 /**
304  * Pixel size (in bytes) for a given pixel format
305  */
306 static const int tjPixelSize[TJ_NUMPF] = {
307   3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
308 };
309 
310 
311 /**
312  * The number of JPEG colorspaces
313  */
314 #define TJ_NUMCS  5
315 
316 /**
317  * JPEG colorspaces
318  */
319 enum TJCS {
320   /**
321    * RGB colorspace.  When compressing the JPEG image, the R, G, and B
322    * components in the source image are reordered into image planes, but no
323    * colorspace conversion or subsampling is performed.  RGB JPEG images can be
324    * decompressed to packed-pixel images with any of the extended RGB or
325    * grayscale pixel formats, but they cannot be decompressed to planar YUV
326    * images.
327    */
328   TJCS_RGB = 0,
329   /**
330    * YCbCr colorspace.  YCbCr is not an absolute colorspace but rather a
331    * mathematical transformation of RGB designed solely for storage and
332    * transmission.  YCbCr images must be converted to RGB before they can
333    * actually be displayed.  In the YCbCr colorspace, the Y (luminance)
334    * component represents the black & white portion of the original image, and
335    * the Cb and Cr (chrominance) components represent the color portion of the
336    * original image.  Originally, the analog equivalent of this transformation
337    * allowed the same signal to drive both black & white and color televisions,
338    * but JPEG images use YCbCr primarily because it allows the color data to be
339    * optionally subsampled for the purposes of reducing network or disk usage.
340    * YCbCr is the most common JPEG colorspace, and YCbCr JPEG images can be
341    * compressed from and decompressed to packed-pixel images with any of the
342    * extended RGB or grayscale pixel formats.  YCbCr JPEG images can also be
343    * compressed from and decompressed to planar YUV images.
344    */
345   TJCS_YCbCr,
346   /**
347    * Grayscale colorspace.  The JPEG image retains only the luminance data (Y
348    * component), and any color data from the source image is discarded.
349    * Grayscale JPEG images can be compressed from and decompressed to
350    * packed-pixel images with any of the extended RGB or grayscale pixel
351    * formats, or they can be compressed from and decompressed to planar YUV
352    * images.
353    */
354   TJCS_GRAY,
355   /**
356    * CMYK colorspace.  When compressing the JPEG image, the C, M, Y, and K
357    * components in the source image are reordered into image planes, but no
358    * colorspace conversion or subsampling is performed.  CMYK JPEG images can
359    * only be decompressed to packed-pixel images with the CMYK pixel format.
360    */
361   TJCS_CMYK,
362   /**
363    * YCCK colorspace.  YCCK (AKA "YCbCrK") is not an absolute colorspace but
364    * rather a mathematical transformation of CMYK designed solely for storage
365    * and transmission.  It is to CMYK as YCbCr is to RGB.  CMYK pixels can be
366    * reversibly transformed into YCCK, and as with YCbCr, the chrominance
367    * components in the YCCK pixels can be subsampled without incurring major
368    * perceptual loss.  YCCK JPEG images can only be compressed from and
369    * decompressed to packed-pixel images with the CMYK pixel format.
370    */
371   TJCS_YCCK
372 };
373 
374 
375 /**
376  * Rows in the packed-pixel source/destination image are stored in bottom-up
377  * (Windows, OpenGL) order rather than in top-down (X11) order.
378  */
379 #define TJFLAG_BOTTOMUP  2
380 /**
381  * When decompressing an image that was compressed using chrominance
382  * subsampling, use the fastest chrominance upsampling algorithm available.
383  * The default is to use smooth upsampling, which creates a smooth transition
384  * between neighboring chrominance components in order to reduce upsampling
385  * artifacts in the decompressed image.
386  */
387 #define TJFLAG_FASTUPSAMPLE  256
388 /**
389  * Disable JPEG buffer (re)allocation.  If passed to one of the JPEG
390  * compression or transform functions, this flag will cause those functions to
391  * generate an error if the JPEG destination buffer is invalid or too small,
392  * rather than attempt to allocate or reallocate that buffer.
393  */
394 #define TJFLAG_NOREALLOC  1024
395 /**
396  * Use the fastest DCT/IDCT algorithm available.  The default if this flag is
397  * not specified is implementation-specific.  For example, the implementation
398  * of the TurboJPEG API in libjpeg-turbo uses the fast algorithm by default
399  * when compressing, because this has been shown to have only a very slight
400  * effect on accuracy, but it uses the accurate algorithm when decompressing,
401  * because this has been shown to have a larger effect.
402  */
403 #define TJFLAG_FASTDCT  2048
404 /**
405  * Use the most accurate DCT/IDCT algorithm available.  The default if this
406  * flag is not specified is implementation-specific.  For example, the
407  * implementation of the TurboJPEG API in libjpeg-turbo uses the fast algorithm
408  * by default when compressing, because this has been shown to have only a very
409  * slight effect on accuracy, but it uses the accurate algorithm when
410  * decompressing, because this has been shown to have a larger effect.
411  */
412 #define TJFLAG_ACCURATEDCT  4096
413 /**
414  * Immediately discontinue the current compression/decompression/transform
415  * operation if a warning (non-fatal error) occurs.  The default behavior is to
416  * allow the operation to complete unless a fatal error is encountered.
417  */
418 #define TJFLAG_STOPONWARNING  8192
419 /**
420  * Use progressive entropy coding in JPEG images generated by the compression
421  * and transform functions.  Progressive entropy coding will generally improve
422  * compression relative to baseline entropy coding (the default), but it will
423  * reduce compression and decompression performance considerably.
424  */
425 #define TJFLAG_PROGRESSIVE  16384
426 /**
427  * Limit the number of progressive JPEG scans that the decompression and
428  * transform functions will process.  If a progressive JPEG image contains an
429  * unreasonably large number of scans, then this flag will cause the
430  * decompression and transform functions to return an error.  The primary
431  * purpose of this is to allow security-critical applications to guard against
432  * an exploit of the progressive JPEG format described in
433  * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
434  */
435 #define TJFLAG_LIMITSCANS  32768
436 
437 
438 /**
439  * The number of error codes
440  */
441 #define TJ_NUMERR  2
442 
443 /**
444  * Error codes
445  */
446 enum TJERR {
447   /**
448    * The error was non-fatal and recoverable, but the destination image may
449    * still be corrupt.
450    */
451   TJERR_WARNING = 0,
452   /**
453    * The error was fatal and non-recoverable.
454    */
455   TJERR_FATAL
456 };
457 
458 
459 /**
460  * The number of transform operations
461  */
462 #define TJ_NUMXOP  8
463 
464 /**
465  * Transform operations for #tjTransform()
466  */
467 enum TJXOP {
468   /**
469    * Do not transform the position of the image pixels
470    */
471   TJXOP_NONE = 0,
472   /**
473    * Flip (mirror) image horizontally.  This transform is imperfect if there
474    * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
475    */
476   TJXOP_HFLIP,
477   /**
478    * Flip (mirror) image vertically.  This transform is imperfect if there are
479    * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
480    */
481   TJXOP_VFLIP,
482   /**
483    * Transpose image (flip/mirror along upper left to lower right axis.)  This
484    * transform is always perfect.
485    */
486   TJXOP_TRANSPOSE,
487   /**
488    * Transverse transpose image (flip/mirror along upper right to lower left
489    * axis.)  This transform is imperfect if there are any partial MCU blocks in
490    * the image (see #TJXOPT_PERFECT.)
491    */
492   TJXOP_TRANSVERSE,
493   /**
494    * Rotate image clockwise by 90 degrees.  This transform is imperfect if
495    * there are any partial MCU blocks on the bottom edge (see
496    * #TJXOPT_PERFECT.)
497    */
498   TJXOP_ROT90,
499   /**
500    * Rotate image 180 degrees.  This transform is imperfect if there are any
501    * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
502    */
503   TJXOP_ROT180,
504   /**
505    * Rotate image counter-clockwise by 90 degrees.  This transform is imperfect
506    * if there are any partial MCU blocks on the right edge (see
507    * #TJXOPT_PERFECT.)
508    */
509   TJXOP_ROT270
510 };
511 
512 
513 /**
514  * This option will cause #tjTransform() to return an error if the transform is
515  * not perfect.  Lossless transforms operate on MCU blocks, whose size depends
516  * on the level of chrominance subsampling used (see #tjMCUWidth and
517  * #tjMCUHeight.)  If the image's width or height is not evenly divisible by
518  * the MCU block size, then there will be partial MCU blocks on the right
519  * and/or bottom edges.  It is not possible to move these partial MCU blocks to
520  * the top or left of the image, so any transform that would require that is
521  * "imperfect."  If this option is not specified, then any partial MCU blocks
522  * that cannot be transformed will be left in place, which will create
523  * odd-looking strips on the right or bottom edge of the image.
524  */
525 #define TJXOPT_PERFECT  1
526 /**
527  * This option will cause #tjTransform() to discard any partial MCU blocks that
528  * cannot be transformed.
529  */
530 #define TJXOPT_TRIM  2
531 /**
532  * This option will enable lossless cropping.  See #tjTransform() for more
533  * information.
534  */
535 #define TJXOPT_CROP  4
536 /**
537  * This option will discard the color data in the source image and produce a
538  * grayscale destination image.
539  */
540 #define TJXOPT_GRAY  8
541 /**
542  * This option will prevent #tjTransform() from outputting a JPEG image for
543  * this particular transform.  (This can be used in conjunction with a custom
544  * filter to capture the transformed DCT coefficients without transcoding
545  * them.)
546  */
547 #define TJXOPT_NOOUTPUT  16
548 /**
549  * This option will enable progressive entropy coding in the JPEG image
550  * generated by this particular transform.  Progressive entropy coding will
551  * generally improve compression relative to baseline entropy coding (the
552  * default), but it will reduce decompression performance considerably.
553  */
554 #define TJXOPT_PROGRESSIVE  32
555 /**
556  * This option will prevent #tjTransform() from copying any extra markers
557  * (including EXIF and ICC profile data) from the source image to the
558  * destination image.
559  */
560 #define TJXOPT_COPYNONE  64
561 
562 
563 /**
564  * Scaling factor
565  */
566 typedef struct {
567   /**
568    * Numerator
569    */
570   int num;
571   /**
572    * Denominator
573    */
574   int denom;
575 } tjscalingfactor;
576 
577 /**
578  * Cropping region
579  */
580 typedef struct {
581   /**
582    * The left boundary of the cropping region.  This must be evenly divisible
583    * by the MCU block width (see #tjMCUWidth.)
584    */
585   int x;
586   /**
587    * The upper boundary of the cropping region.  This must be evenly divisible
588    * by the MCU block height (see #tjMCUHeight.)
589    */
590   int y;
591   /**
592    * The width of the cropping region.  Setting this to 0 is the equivalent of
593    * setting it to the width of the source JPEG image - x.
594    */
595   int w;
596   /**
597    * The height of the cropping region.  Setting this to 0 is the equivalent of
598    * setting it to the height of the source JPEG image - y.
599    */
600   int h;
601 } tjregion;
602 
603 /**
604  * Lossless transform
605  */
606 typedef struct tjtransform {
607   /**
608    * Cropping region
609    */
610   tjregion r;
611   /**
612    * One of the @ref TJXOP "transform operations"
613    */
614   int op;
615   /**
616    * The bitwise OR of one of more of the @ref TJXOPT_COPYNONE
617    * "transform options"
618    */
619   int options;
620   /**
621    * Arbitrary data that can be accessed within the body of the callback
622    * function
623    */
624   void *data;
625   /**
626    * A callback function that can be used to modify the DCT coefficients after
627    * they are losslessly transformed but before they are transcoded to a new
628    * JPEG image.  This allows for custom filters or other transformations to be
629    * applied in the frequency domain.
630    *
631    * @param coeffs pointer to an array of transformed DCT coefficients.  (NOTE:
632    * this pointer is not guaranteed to be valid once the callback returns, so
633    * applications wishing to hand off the DCT coefficients to another function
634    * or library should make a copy of them within the body of the callback.)
635    *
636    * @param arrayRegion #tjregion structure containing the width and height of
637    * the array pointed to by `coeffs` as well as its offset relative to the
638    * component plane.  TurboJPEG implementations may choose to split each
639    * component plane into multiple DCT coefficient arrays and call the callback
640    * function once for each array.
641    *
642    * @param planeRegion #tjregion structure containing the width and height of
643    * the component plane to which `coeffs` belongs
644    *
645    * @param componentID ID number of the component plane to which `coeffs`
646    * belongs.  (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
647    * typical JPEG images.)
648    *
649    * @param transformID ID number of the transformed image to which `coeffs`
650    * belongs.  This is the same as the index of the transform in the
651    * `transforms` array that was passed to #tjTransform().
652    *
653    * @param transform a pointer to a #tjtransform structure that specifies the
654    * parameters and/or cropping region for this transform
655    *
656    * @return 0 if the callback was successful, or -1 if an error occurred.
657    */
658   int (*customFilter) (short *coeffs, tjregion arrayRegion,
659                        tjregion planeRegion, int componentIndex,
660                        int transformIndex, struct tjtransform *transform);
661 } tjtransform;
662 
663 /**
664  * TurboJPEG instance handle
665  */
666 typedef void *tjhandle;
667 
668 
669 /**
670  * Pad the given width to the nearest multiple of 4
671  */
672 #define TJPAD(width)  (((width) + 3) & (~3))
673 
674 /**
675  * Compute the scaled value of `dimension` using the given scaling factor.
676  * This macro performs the integer equivalent of `ceil(dimension *
677  * scalingFactor)`.
678  */
679 #define TJSCALED(dimension, scalingFactor) \
680   (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
681    scalingFactor.denom)
682 
683 
684 #ifdef __cplusplus
685 extern "C" {
686 #endif
687 
688 
689 /**
690  * Create a TurboJPEG compressor instance.
691  *
692  * @return a handle to the newly-created instance, or NULL if an error occurred
693  * (see #tjGetErrorStr2().)
694  */
695 DLLEXPORT tjhandle tjInitCompress(void);
696 
697 
698 /**
699  * Compress a packed-pixel RGB, grayscale, or CMYK image into a JPEG image.
700  *
701  * @param handle a handle to a TurboJPEG compressor or transformer instance
702  *
703  * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
704  * or CMYK source image to be compressed
705  *
706  * @param width width (in pixels) of the source image
707  *
708  * @param pitch bytes per row in the source image.  Normally this should be
709  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded, or
710  * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each row of the image
711  * is padded to the nearest multiple of 4 bytes, as is the case for Windows
712  * bitmaps.  You can also be clever and use this parameter to skip rows, etc.
713  * Setting this parameter to 0 is the equivalent of setting it to
714  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
715  *
716  * @param height height (in pixels) of the source image
717  *
718  * @param pixelFormat pixel format of the source image (see @ref TJPF
719  * "Pixel formats".)
720  *
721  * @param jpegBuf address of a pointer to a byte buffer that will receive the
722  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
723  * accommodate the size of the JPEG image.  Thus, you can choose to:
724  * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
725  * let TurboJPEG grow the buffer as needed,
726  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
727  * or
728  * -# pre-allocate the buffer to a "worst case" size determined by calling
729  * #tjBufSize().  This should ensure that the buffer never has to be
730  * re-allocated.  (Setting #TJFLAG_NOREALLOC guarantees that it won't be.)
731  * .
732  * If you choose option 1, then `*jpegSize` should be set to the size of your
733  * pre-allocated buffer.  In any case, unless you have set #TJFLAG_NOREALLOC,
734  * you should always check `*jpegBuf` upon return from this function, as it may
735  * have changed.
736  *
737  * @param jpegSize pointer to an unsigned long variable that holds the size of
738  * the JPEG buffer.  If `*jpegBuf` points to a pre-allocated buffer, then
739  * `*jpegSize` should be set to the size of the buffer.  Upon return,
740  * `*jpegSize` will contain the size of the JPEG image (in bytes.)  If
741  * `*jpegBuf` points to a JPEG buffer that is being reused from a previous call
742  * to one of the JPEG compression functions, then `*jpegSize` is ignored.
743  *
744  * @param jpegSubsamp the level of chrominance subsampling to be used when
745  * generating the JPEG image (see @ref TJSAMP
746  * "Chrominance subsampling options".)
747  *
748  * @param jpegQual the image quality of the generated JPEG image (1 = worst,
749  * 100 = best)
750  *
751  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
752  * "flags"
753  *
754  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
755  * and #tjGetErrorCode().)
756  */
757 DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
758                           int width, int pitch, int height, int pixelFormat,
759                           unsigned char **jpegBuf, unsigned long *jpegSize,
760                           int jpegSubsamp, int jpegQual, int flags);
761 
762 
763 /**
764  * Compress a unified planar YUV image into a JPEG image.
765  *
766  * @param handle a handle to a TurboJPEG compressor or transformer instance
767  *
768  * @param srcBuf pointer to a buffer containing a unified planar YUV source
769  * image to be compressed.  The size of this buffer should match the value
770  * returned by #tjBufSizeYUV2() for the given image width, height, row
771  * alignment, and level of chrominance subsampling.  The Y, U (Cb), and V (Cr)
772  * image planes should be stored sequentially in the buffer.  (Refer to
773  * @ref YUVnotes "YUV Image Format Notes".)
774  *
775  * @param width width (in pixels) of the source image.  If the width is not an
776  * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
777  * buffer copy will be performed.
778  *
779  * @param align row alignment (in bytes) of the source image (must be a power
780  * of 2.)  Setting this parameter to n indicates that each row in each plane of
781  * the source image is padded to the nearest multiple of n bytes
782  * (1 = unpadded.)
783  *
784  * @param height height (in pixels) of the source image.  If the height is not
785  * an even multiple of the MCU block height (see #tjMCUHeight), then an
786  * intermediate buffer copy will be performed.
787  *
788  * @param subsamp the level of chrominance subsampling used in the source image
789  * (see @ref TJSAMP "Chrominance subsampling options".)
790  *
791  * @param jpegBuf address of a pointer to a byte buffer that will receive the
792  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
793  * accommodate the size of the JPEG image.  Thus, you can choose to:
794  * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
795  * let TurboJPEG grow the buffer as needed,
796  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
797  * or
798  * -# pre-allocate the buffer to a "worst case" size determined by calling
799  * #tjBufSize().  This should ensure that the buffer never has to be
800  * re-allocated.  (Setting #TJFLAG_NOREALLOC guarantees that it won't be.)
801  * .
802  * If you choose option 1, then `*jpegSize` should be set to the size of your
803  * pre-allocated buffer.  In any case, unless you have set #TJFLAG_NOREALLOC,
804  * you should always check `*jpegBuf` upon return from this function, as it may
805  * have changed.
806  *
807  * @param jpegSize pointer to an unsigned long variable that holds the size of
808  * the JPEG buffer.  If `*jpegBuf` points to a pre-allocated buffer, then
809  * `*jpegSize` should be set to the size of the buffer.  Upon return,
810  * `*jpegSize` will contain the size of the JPEG image (in bytes.)  If
811  * `*jpegBuf` points to a JPEG buffer that is being reused from a previous call
812  * to one of the JPEG compression functions, then `*jpegSize` is ignored.
813  *
814  * @param jpegQual the image quality of the generated JPEG image (1 = worst,
815  * 100 = best)
816  *
817  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
818  * "flags"
819  *
820  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
821  * and #tjGetErrorCode().)
822  */
823 DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
824                                 int width, int align, int height, int subsamp,
825                                 unsigned char **jpegBuf,
826                                 unsigned long *jpegSize, int jpegQual,
827                                 int flags);
828 
829 
830 /**
831  * Compress a set of Y, U (Cb), and V (Cr) image planes into a JPEG image.
832  *
833  * @param handle a handle to a TurboJPEG compressor or transformer instance
834  *
835  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
836  * (or just a Y plane, if compressing a grayscale image) that contain a YUV
837  * source image to be compressed.  These planes can be contiguous or
838  * non-contiguous in memory.  The size of each plane should match the value
839  * returned by #tjPlaneSizeYUV() for the given image width, height, strides,
840  * and level of chrominance subsampling.  Refer to @ref YUVnotes
841  * "YUV Image Format Notes" for more details.
842  *
843  * @param width width (in pixels) of the source image.  If the width is not an
844  * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
845  * buffer copy will be performed.
846  *
847  * @param strides an array of integers, each specifying the number of bytes per
848  * row in the corresponding plane of the YUV source image.  Setting the stride
849  * for any plane to 0 is the same as setting it to the plane width (see
850  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
851  * strides for all planes will be set to their respective plane widths.  You
852  * can adjust the strides in order to specify an arbitrary amount of row
853  * padding in each plane or to create a JPEG image from a subregion of a larger
854  * planar YUV image.
855  *
856  * @param height height (in pixels) of the source image.  If the height is not
857  * an even multiple of the MCU block height (see #tjMCUHeight), then an
858  * intermediate buffer copy will be performed.
859  *
860  * @param subsamp the level of chrominance subsampling used in the source image
861  * (see @ref TJSAMP "Chrominance subsampling options".)
862  *
863  * @param jpegBuf address of a pointer to a byte buffer that will receive the
864  * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
865  * accommodate the size of the JPEG image.  Thus, you can choose to:
866  * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
867  * let TurboJPEG grow the buffer as needed,
868  * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
869  * or
870  * -# pre-allocate the buffer to a "worst case" size determined by calling
871  * #tjBufSize().  This should ensure that the buffer never has to be
872  * re-allocated.  (Setting #TJFLAG_NOREALLOC guarantees that it won't be.)
873  * .
874  * If you choose option 1, then `*jpegSize` should be set to the size of your
875  * pre-allocated buffer.  In any case, unless you have set #TJFLAG_NOREALLOC,
876  * you should always check `*jpegBuf` upon return from this function, as it may
877  * have changed.
878  *
879  * @param jpegSize pointer to an unsigned long variable that holds the size of
880  * the JPEG buffer.  If `*jpegBuf` points to a pre-allocated buffer, then
881  * `*jpegSize` should be set to the size of the buffer.  Upon return,
882  * `*jpegSize` will contain the size of the JPEG image (in bytes.)  If
883  * `*jpegBuf` points to a JPEG buffer that is being reused from a previous call
884  * to one of the JPEG compression functions, then `*jpegSize` is ignored.
885  *
886  * @param jpegQual the image quality of the generated JPEG image (1 = worst,
887  * 100 = best)
888  *
889  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
890  * "flags"
891  *
892  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
893  * and #tjGetErrorCode().)
894  */
895 DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
896                                       const unsigned char **srcPlanes,
897                                       int width, const int *strides,
898                                       int height, int subsamp,
899                                       unsigned char **jpegBuf,
900                                       unsigned long *jpegSize, int jpegQual,
901                                       int flags);
902 
903 
904 /**
905  * The maximum size of the buffer (in bytes) required to hold a JPEG image with
906  * the given parameters.  The number of bytes returned by this function is
907  * larger than the size of the uncompressed source image.  The reason for this
908  * is that the JPEG format uses 16-bit coefficients, so it is possible for a
909  * very high-quality source image with very high-frequency content to expand
910  * rather than compress when converted to the JPEG format.  Such images
911  * represent very rare corner cases, but since there is no way to predict the
912  * size of a JPEG image prior to compression, the corner cases have to be
913  * handled.
914  *
915  * @param width width (in pixels) of the image
916  *
917  * @param height height (in pixels) of the image
918  *
919  * @param jpegSubsamp the level of chrominance subsampling to be used when
920  * generating the JPEG image (see @ref TJSAMP
921  * "Chrominance subsampling options".)
922  *
923  * @return the maximum size of the buffer (in bytes) required to hold the
924  * image, or -1 if the arguments are out of bounds.
925  */
926 DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
927 
928 
929 /**
930  * The size of the buffer (in bytes) required to hold a unified planar YUV
931  * image with the given parameters.
932  *
933  * @param width width (in pixels) of the image
934  *
935  * @param align row alignment (in bytes) of the image (must be a power of 2.)
936  * Setting this parameter to n specifies that each row in each plane of the
937  * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
938  *
939  * @param height height (in pixels) of the image
940  *
941  * @param subsamp level of chrominance subsampling in the image (see
942  * @ref TJSAMP "Chrominance subsampling options".)
943  *
944  * @return the size of the buffer (in bytes) required to hold the image, or -1
945  * if the arguments are out of bounds.
946  */
947 DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
948                                       int subsamp);
949 
950 
951 /**
952  * The size of the buffer (in bytes) required to hold a YUV image plane with
953  * the given parameters.
954  *
955  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
956  *
957  * @param width width (in pixels) of the YUV image.  NOTE: this is the width of
958  * the whole image, not the plane width.
959  *
960  * @param stride bytes per row in the image plane.  Setting this to 0 is the
961  * equivalent of setting it to the plane width.
962  *
963  * @param height height (in pixels) of the YUV image.  NOTE: this is the height
964  * of the whole image, not the plane height.
965  *
966  * @param subsamp level of chrominance subsampling in the image (see
967  * @ref TJSAMP "Chrominance subsampling options".)
968  *
969  * @return the size of the buffer (in bytes) required to hold the YUV image
970  * plane, or -1 if the arguments are out of bounds.
971  */
972 DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
973                                        int height, int subsamp);
974 
975 
976 /**
977  * The plane width of a YUV image plane with the given parameters.  Refer to
978  * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
979  *
980  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
981  *
982  * @param width width (in pixels) of the YUV image
983  *
984  * @param subsamp level of chrominance subsampling in the image (see
985  * @ref TJSAMP "Chrominance subsampling options".)
986  *
987  * @return the plane width of a YUV image plane with the given parameters, or
988  * -1 if the arguments are out of bounds.
989  */
990 DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
991 
992 
993 /**
994  * The plane height of a YUV image plane with the given parameters.  Refer to
995  * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
996  *
997  * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
998  *
999  * @param height height (in pixels) of the YUV image
1000  *
1001  * @param subsamp level of chrominance subsampling in the image (see
1002  * @ref TJSAMP "Chrominance subsampling options".)
1003  *
1004  * @return the plane height of a YUV image plane with the given parameters, or
1005  * -1 if the arguments are out of bounds.
1006  */
1007 DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
1008 
1009 
1010 /**
1011  * Encode a packed-pixel RGB or grayscale image into a unified planar YUV
1012  * image.  This function performs color conversion (which is accelerated in the
1013  * libjpeg-turbo implementation) but does not execute any of the other steps in
1014  * the JPEG compression process.
1015  *
1016  * @param handle a handle to a TurboJPEG compressor or transformer instance
1017  *
1018  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1019  * source image to be encoded
1020  *
1021  * @param width width (in pixels) of the source image
1022  *
1023  * @param pitch bytes per row in the source image.  Normally this should be
1024  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded, or
1025  * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each row of the image
1026  * is padded to the nearest multiple of 4 bytes, as is the case for Windows
1027  * bitmaps.  You can also be clever and use this parameter to skip rows, etc.
1028  * Setting this parameter to 0 is the equivalent of setting it to
1029  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1030  *
1031  * @param height height (in pixels) of the source image
1032  *
1033  * @param pixelFormat pixel format of the source image (see @ref TJPF
1034  * "Pixel formats".)
1035  *
1036  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1037  * image.  Use #tjBufSizeYUV2() to determine the appropriate size for this
1038  * buffer based on the image width, height, row alignment, and level of
1039  * chrominance subsampling.  The Y, U (Cb), and V (Cr) image planes will be
1040  * stored sequentially in the buffer.  (Refer to @ref YUVnotes
1041  * "YUV Image Format Notes".)
1042  *
1043  * @param align row alignment (in bytes) of the YUV image (must be a power of
1044  * 2.)  Setting this parameter to n will cause each row in each plane of the
1045  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1046  * To generate images suitable for X Video, `align` should be set to 4.
1047  *
1048  * @param subsamp the level of chrominance subsampling to be used when
1049  * generating the YUV image (see @ref TJSAMP
1050  * "Chrominance subsampling options".)  To generate images suitable for X
1051  * Video, `subsamp` should be set to @ref TJSAMP_420.  This produces an image
1052  * compatible with the I420 (AKA "YUV420P") format.
1053  *
1054  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1055  * "flags"
1056  *
1057  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1058  * and #tjGetErrorCode().)
1059  */
1060 DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
1061                            int width, int pitch, int height, int pixelFormat,
1062                            unsigned char *dstBuf, int align, int subsamp,
1063                            int flags);
1064 
1065 
1066 /**
1067  * Encode a packed-pixel RGB or grayscale image into separate Y, U (Cb), and
1068  * V (Cr) image planes.  This function performs color conversion (which is
1069  * accelerated in the libjpeg-turbo implementation) but does not execute any of
1070  * the other steps in the JPEG compression process.
1071  *
1072  * @param handle a handle to a TurboJPEG compressor or transformer instance
1073  *
1074  * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1075  * source image to be encoded
1076  *
1077  * @param width width (in pixels) of the source image
1078  *
1079  * @param pitch bytes per row in the source image.  Normally this should be
1080  * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded, or
1081  * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each row of the image
1082  * is padded to the nearest multiple of 4 bytes, as is the case for Windows
1083  * bitmaps.  You can also be clever and use this parameter to skip rows, etc.
1084  * Setting this parameter to 0 is the equivalent of setting it to
1085  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1086  *
1087  * @param height height (in pixels) of the source image
1088  *
1089  * @param pixelFormat pixel format of the source image (see @ref TJPF
1090  * "Pixel formats".)
1091  *
1092  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1093  * (or just a Y plane, if generating a grayscale image) that will receive the
1094  * encoded image.  These planes can be contiguous or non-contiguous in memory.
1095  * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1096  * on the image width, height, strides, and level of chrominance subsampling.
1097  * Refer to @ref YUVnotes "YUV Image Format Notes" for more details.
1098  *
1099  * @param strides an array of integers, each specifying the number of bytes per
1100  * row in the corresponding plane of the YUV image.  Setting the stride for any
1101  * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
1102  * "YUV Image Format Notes".)  If `strides` is NULL, then the strides for all
1103  * planes will be set to their respective plane widths.  You can adjust the
1104  * strides in order to add an arbitrary amount of row padding to each plane or
1105  * to encode an RGB or grayscale image into a subregion of a larger planar YUV
1106  * image.
1107  *
1108  * @param subsamp the level of chrominance subsampling to be used when
1109  * generating the YUV image (see @ref TJSAMP
1110  * "Chrominance subsampling options".)  To generate images suitable for X
1111  * Video, `subsamp` should be set to @ref TJSAMP_420.  This produces an image
1112  * compatible with the I420 (AKA "YUV420P") format.
1113  *
1114  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1115  * "flags"
1116  *
1117  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1118  * and #tjGetErrorCode().)
1119  */
1120 DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
1121                                 int width, int pitch, int height,
1122                                 int pixelFormat, unsigned char **dstPlanes,
1123                                 int *strides, int subsamp, int flags);
1124 
1125 
1126 /**
1127  * Create a TurboJPEG decompressor instance.
1128  *
1129  * @return a handle to the newly-created instance, or NULL if an error occurred
1130  * (see #tjGetErrorStr2().)
1131  */
1132 DLLEXPORT tjhandle tjInitDecompress(void);
1133 
1134 
1135 /**
1136  * Retrieve information about a JPEG image without decompressing it, or prime
1137  * the decompressor with quantization and Huffman tables.
1138  *
1139  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1140  *
1141  * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
1142  * "abbreviated table specification" (AKA "tables-only") datastream.  Passing a
1143  * tables-only datastream to this function primes the decompressor with
1144  * quantization and Huffman tables that can be used when decompressing
1145  * subsequent "abbreviated image" datastreams.  This is useful, for instance,
1146  * when decompressing video streams in which all frames share the same
1147  * quantization and Huffman tables.
1148  *
1149  * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1150  *
1151  * @param width pointer to an integer variable that will receive the width (in
1152  * pixels) of the JPEG image.  If `jpegBuf` points to a tables-only datastream,
1153  * then `width` is ignored.
1154  *
1155  * @param height pointer to an integer variable that will receive the height
1156  * (in pixels) of the JPEG image.  If `jpegBuf` points to a tables-only
1157  * datastream, then `height` is ignored.
1158  *
1159  * @param jpegSubsamp pointer to an integer variable that will receive the
1160  * level of chrominance subsampling used when the JPEG image was compressed
1161  * (see @ref TJSAMP "Chrominance subsampling options".)  If `jpegBuf` points to
1162  * a tables-only datastream, then `jpegSubsamp` is ignored.
1163  *
1164  * @param jpegColorspace pointer to an integer variable that will receive one
1165  * of the JPEG colorspace constants, indicating the colorspace of the JPEG
1166  * image (see @ref TJCS "JPEG colorspaces".)  If `jpegBuf` points to a
1167  * tables-only datastream, then `jpegColorspace` is ignored.
1168  *
1169  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1170  * and #tjGetErrorCode().)
1171  */
1172 DLLEXPORT int tjDecompressHeader3(tjhandle handle,
1173                                   const unsigned char *jpegBuf,
1174                                   unsigned long jpegSize, int *width,
1175                                   int *height, int *jpegSubsamp,
1176                                   int *jpegColorspace);
1177 
1178 
1179 /**
1180  * Returns a list of fractional scaling factors that the JPEG decompressor
1181  * supports.
1182  *
1183  * @param numScalingFactors pointer to an integer variable that will receive
1184  * the number of elements in the list
1185  *
1186  * @return a pointer to a list of fractional scaling factors, or NULL if an
1187  * error is encountered (see #tjGetErrorStr2().)
1188  */
1189 DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numScalingFactors);
1190 
1191 
1192 /**
1193  * Decompress a JPEG image into a packed-pixel RGB, grayscale, or CMYK image.
1194  *
1195  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1196  *
1197  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1198  * decompress
1199  *
1200  * @param jpegSize size of the JPEG image (in bytes)
1201  *
1202  * @param dstBuf pointer to a buffer that will receive the packed-pixel
1203  * decompressed image.  This buffer should normally be `pitch * scaledHeight`
1204  * bytes in size, where `scaledHeight` can be determined by calling #TJSCALED()
1205  * with the JPEG image height and one of the scaling factors returned by
1206  * #tjGetScalingFactors().  The `dstBuf` pointer may also be used to decompress
1207  * into a specific region of a larger buffer.
1208  *
1209  * @param width desired width (in pixels) of the destination image.  If this is
1210  * different than the width of the JPEG image being decompressed, then
1211  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1212  * possible image that will fit within the desired width.  If `width` is set to
1213  * 0, then only the height will be considered when determining the scaled image
1214  * size.
1215  *
1216  * @param pitch bytes per row in the destination image.  Normally this should
1217  * be set to <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>, if the
1218  * destination image should be unpadded, or
1219  * <tt>#TJPAD(scaledWidth * #tjPixelSize[pixelFormat])</tt> if each row of the
1220  * destination image should be padded to the nearest multiple of 4 bytes, as is
1221  * the case for Windows bitmaps.  (NOTE: `scaledWidth` can be determined by
1222  * calling #TJSCALED() with the JPEG image width and one of the scaling factors
1223  * returned by #tjGetScalingFactors().)  You can also be clever and use the
1224  * pitch parameter to skip rows, etc.  Setting this parameter to 0 is the
1225  * equivalent of setting it to
1226  * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>.
1227  *
1228  * @param height desired height (in pixels) of the destination image.  If this
1229  * is different than the height of the JPEG image being decompressed, then
1230  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1231  * possible image that will fit within the desired height.  If `height` is set
1232  * to 0, then only the width will be considered when determining the scaled
1233  * image size.
1234  *
1235  * @param pixelFormat pixel format of the destination image (see @ref
1236  * TJPF "Pixel formats".)
1237  *
1238  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1239  * "flags"
1240  *
1241  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1242  * and #tjGetErrorCode().)
1243  */
1244 DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
1245                             unsigned long jpegSize, unsigned char *dstBuf,
1246                             int width, int pitch, int height, int pixelFormat,
1247                             int flags);
1248 
1249 
1250 /**
1251  * Decompress a JPEG image into a unified planar YUV image.  This function
1252  * performs JPEG decompression but leaves out the color conversion step, so a
1253  * planar YUV image is generated instead of a packed-pixel image.
1254  *
1255  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1256  *
1257  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1258  * decompress
1259  *
1260  * @param jpegSize size of the JPEG image (in bytes)
1261  *
1262  * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1263  * decompressed image.  Use #tjBufSizeYUV2() to determine the appropriate size
1264  * for this buffer based on the scaled image width, scaled image height, row
1265  * alignment, and level of chrominance subsampling.  The Y, U (Cb), and V (Cr)
1266  * image planes will be stored sequentially in the buffer.  (Refer to
1267  * @ref YUVnotes "YUV Image Format Notes".)
1268  *
1269  * @param width desired width (in pixels) of the YUV image.  If this is
1270  * different than the width of the JPEG image being decompressed, then
1271  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1272  * possible image that will fit within the desired width.  If `width` is set to
1273  * 0, then only the height will be considered when determining the scaled image
1274  * size.  If the scaled width is not an even multiple of the MCU block width
1275  * (see #tjMCUWidth), then an intermediate buffer copy will be performed.
1276  *
1277  * @param align row alignment (in bytes) of the YUV image (must be a power of
1278  * 2.)  Setting this parameter to n will cause each row in each plane of the
1279  * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1280  * To generate images suitable for X Video, `align` should be set to 4.
1281  *
1282  * @param height desired height (in pixels) of the YUV image.  If this is
1283  * different than the height of the JPEG image being decompressed, then
1284  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1285  * possible image that will fit within the desired height.  If `height` is set
1286  * to 0, then only the width will be considered when determining the scaled
1287  * image size.  If the scaled height is not an even multiple of the MCU block
1288  * height (see #tjMCUHeight), then an intermediate buffer copy will be
1289  * performed.
1290  *
1291  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1292  * "flags"
1293  *
1294  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1295  * and #tjGetErrorCode().)
1296  */
1297 DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
1298                                  unsigned long jpegSize, unsigned char *dstBuf,
1299                                  int width, int align, int height, int flags);
1300 
1301 
1302 /**
1303  * Decompress a JPEG image into separate Y, U (Cb), and V (Cr) image
1304  * planes.  This function performs JPEG decompression but leaves out the color
1305  * conversion step, so a planar YUV image is generated instead of a
1306  * packed-pixel image.
1307  *
1308  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1309  *
1310  * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1311  * decompress
1312  *
1313  * @param jpegSize size of the JPEG image (in bytes)
1314  *
1315  * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1316  * (or just a Y plane, if decompressing a grayscale image) that will receive
1317  * the decompressed image.  These planes can be contiguous or non-contiguous in
1318  * memory.  Use #tjPlaneSizeYUV() to determine the appropriate size for each
1319  * plane based on the scaled image width, scaled image height, strides, and
1320  * level of chrominance subsampling.  Refer to @ref YUVnotes
1321  * "YUV Image Format Notes" for more details.
1322  *
1323  * @param width desired width (in pixels) of the YUV image.  If this is
1324  * different than the width of the JPEG image being decompressed, then
1325  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1326  * possible image that will fit within the desired width.  If `width` is set to
1327  * 0, then only the height will be considered when determining the scaled image
1328  * size.  If the scaled width is not an even multiple of the MCU block width
1329  * (see #tjMCUWidth), then an intermediate buffer copy will be performed.
1330  *
1331  * @param strides an array of integers, each specifying the number of bytes per
1332  * row in the corresponding plane of the YUV image.  Setting the stride for any
1333  * plane to 0 is the same as setting it to the scaled plane width (see
1334  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1335  * strides for all planes will be set to their respective scaled plane widths.
1336  * You can adjust the strides in order to add an arbitrary amount of row
1337  * padding to each plane or to decompress the JPEG image into a subregion of a
1338  * larger planar YUV image.
1339  *
1340  * @param height desired height (in pixels) of the YUV image.  If this is
1341  * different than the height of the JPEG image being decompressed, then
1342  * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1343  * possible image that will fit within the desired height.  If `height` is set
1344  * to 0, then only the width will be considered when determining the scaled
1345  * image size.  If the scaled height is not an even multiple of the MCU block
1346  * height (see #tjMCUHeight), then an intermediate buffer copy will be
1347  * performed.
1348  *
1349  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1350  * "flags"
1351  *
1352  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1353  * and #tjGetErrorCode().)
1354  */
1355 DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
1356                                       const unsigned char *jpegBuf,
1357                                       unsigned long jpegSize,
1358                                       unsigned char **dstPlanes, int width,
1359                                       int *strides, int height, int flags);
1360 
1361 
1362 /**
1363  * Decode a unified planar YUV image into a packed-pixel RGB or grayscale
1364  * image.  This function performs color conversion (which is accelerated in the
1365  * libjpeg-turbo implementation) but does not execute any of the other steps in
1366  * the JPEG decompression process.
1367  *
1368  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1369  *
1370  * @param srcBuf pointer to a buffer containing a unified planar YUV source
1371  * image to be decoded.  The size of this buffer should match the value
1372  * returned by #tjBufSizeYUV2() for the given image width, height, row
1373  * alignment, and level of chrominance subsampling.  The Y, U (Cb), and V (Cr)
1374  * image planes should be stored sequentially in the source buffer.  (Refer to
1375  * @ref YUVnotes "YUV Image Format Notes".)
1376  *
1377  * @param align row alignment (in bytes) of the YUV source image (must be a
1378  * power of 2.)  Setting this parameter to n indicates that each row in each
1379  * plane of the YUV source image is padded to the nearest multiple of n bytes
1380  * (1 = unpadded.)
1381  *
1382  * @param subsamp the level of chrominance subsampling used in the YUV source
1383  * image (see @ref TJSAMP "Chrominance subsampling options".)
1384  *
1385  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1386  * image.  This buffer should normally be `pitch * height` bytes in size, but
1387  * the `dstBuf` pointer can also be used to decode into a specific region of a
1388  * larger buffer.
1389  *
1390  * @param width width (in pixels) of the source and destination images
1391  *
1392  * @param pitch bytes per row in the destination image.  Normally this should
1393  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1394  * image should be unpadded, or
1395  * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each row of the
1396  * destination image should be padded to the nearest multiple of 4 bytes, as is
1397  * the case for Windows bitmaps.  You can also be clever and use the pitch
1398  * parameter to skip rows, etc.  Setting this parameter to 0 is the equivalent
1399  * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1400  *
1401  * @param height height (in pixels) of the source and destination images
1402  *
1403  * @param pixelFormat pixel format of the destination image (see @ref TJPF
1404  * "Pixel formats".)
1405  *
1406  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1407  * "flags"
1408  *
1409  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1410  * and #tjGetErrorCode().)
1411  */
1412 DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
1413                           int align, int subsamp, unsigned char *dstBuf,
1414                           int width, int pitch, int height, int pixelFormat,
1415                           int flags);
1416 
1417 
1418 /**
1419  * Decode a set of Y, U (Cb), and V (Cr) image planes into a packed-pixel RGB
1420  * or grayscale image.  This function performs color conversion (which is
1421  * accelerated in the libjpeg-turbo implementation) but does not execute any of
1422  * the other steps in the JPEG decompression process.
1423  *
1424  * @param handle a handle to a TurboJPEG decompressor or transformer instance
1425  *
1426  * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1427  * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1428  * to be decoded.  These planes can be contiguous or non-contiguous in memory.
1429  * The size of each plane should match the value returned by #tjPlaneSizeYUV()
1430  * for the given image width, height, strides, and level of chrominance
1431  * subsampling.  Refer to @ref YUVnotes "YUV Image Format Notes" for more
1432  * details.
1433  *
1434  * @param strides an array of integers, each specifying the number of bytes per
1435  * row in the corresponding plane of the YUV source image.  Setting the stride
1436  * for any plane to 0 is the same as setting it to the plane width (see
1437  * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1438  * strides for all planes will be set to their respective plane widths.  You
1439  * can adjust the strides in order to specify an arbitrary amount of row
1440  * padding in each plane or to decode a subregion of a larger planar YUV image.
1441  *
1442  * @param subsamp the level of chrominance subsampling used in the YUV source
1443  * image (see @ref TJSAMP "Chrominance subsampling options".)
1444  *
1445  * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1446  * image.  This buffer should normally be `pitch * height` bytes in size, but
1447  * the `dstBuf` pointer can also be used to decode into a specific region of a
1448  * larger buffer.
1449  *
1450  * @param width width (in pixels) of the source and destination images
1451  *
1452  * @param pitch bytes per row in the destination image.  Normally this should
1453  * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1454  * image should be unpadded, or
1455  * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each row of the
1456  * destination image should be padded to the nearest multiple of 4 bytes, as is
1457  * the case for Windows bitmaps.  You can also be clever and use the pitch
1458  * parameter to skip rows, etc.  Setting this parameter to 0 is the equivalent
1459  * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1460  *
1461  * @param height height (in pixels) of the source and destination images
1462  *
1463  * @param pixelFormat pixel format of the destination image (see @ref TJPF
1464  * "Pixel formats".)
1465  *
1466  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1467  * "flags"
1468  *
1469  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1470  * and #tjGetErrorCode().)
1471  */
1472 DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
1473                                 const unsigned char **srcPlanes,
1474                                 const int *strides, int subsamp,
1475                                 unsigned char *dstBuf, int width, int pitch,
1476                                 int height, int pixelFormat, int flags);
1477 
1478 
1479 /**
1480  * Create a new TurboJPEG transformer instance.
1481  *
1482  * @return a handle to the newly-created instance, or NULL if an error
1483  * occurred (see #tjGetErrorStr2().)
1484  */
1485 DLLEXPORT tjhandle tjInitTransform(void);
1486 
1487 
1488 /**
1489  * Losslessly transform a JPEG image into another JPEG image.  Lossless
1490  * transforms work by moving the raw DCT coefficients from one JPEG image
1491  * structure to another without altering the values of the coefficients.  While
1492  * this is typically faster than decompressing the image, transforming it, and
1493  * re-compressing it, lossless transforms are not free.  Each lossless
1494  * transform requires reading and performing Huffman decoding on all of the
1495  * coefficients in the source image, regardless of the size of the destination
1496  * image.  Thus, this function provides a means of generating multiple
1497  * transformed images from the same source or applying multiple transformations
1498  * simultaneously, in order to eliminate the need to read the source
1499  * coefficients multiple times.
1500  *
1501  * @param handle a handle to a TurboJPEG transformer instance
1502  *
1503  * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
1504  * transform
1505  *
1506  * @param jpegSize size of the JPEG source image (in bytes)
1507  *
1508  * @param n the number of transformed JPEG images to generate
1509  *
1510  * @param dstBufs pointer to an array of n byte buffers.  `dstBufs[i]` will
1511  * receive a JPEG image that has been transformed using the parameters in
1512  * `transforms[i]`.  TurboJPEG has the ability to reallocate the JPEG
1513  * destination buffer to accommodate the size of the transformed JPEG image.
1514  * Thus, you can choose to:
1515  * -# pre-allocate the JPEG destination buffer with an arbitrary size using
1516  * #tjAlloc() and let TurboJPEG grow the buffer as needed,
1517  * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
1518  * you, or
1519  * -# pre-allocate the buffer to a "worst case" size determined by calling
1520  * #tjBufSize() with the transformed or cropped width and height.  Under normal
1521  * circumstances, this should ensure that the buffer never has to be
1522  * re-allocated.  (Setting #TJFLAG_NOREALLOC guarantees that it won't be.)
1523  * Note, however, that there are some rare cases (such as transforming images
1524  * with a large amount of embedded EXIF or ICC profile data) in which the
1525  * transformed JPEG image will be larger than the worst-case size, and
1526  * #TJFLAG_NOREALLOC cannot be used in those cases.
1527  * .
1528  * If you choose option 1, then `dstSizes[i]` should be set to the size of your
1529  * pre-allocated buffer.  In any case, unless you have set #TJFLAG_NOREALLOC,
1530  * you should always check `dstBufs[i]` upon return from this function, as it
1531  * may have changed.
1532  *
1533  * @param dstSizes pointer to an array of n unsigned long variables that will
1534  * receive the actual sizes (in bytes) of each transformed JPEG image.  If
1535  * `dstBufs[i]` points to a pre-allocated buffer, then `dstSizes[i]` should be
1536  * set to the size of the buffer.  Upon return, `dstSizes[i]` will contain the
1537  * size of the transformed JPEG image (in bytes.)
1538  *
1539  * @param transforms pointer to an array of n #tjtransform structures, each of
1540  * which specifies the transform parameters and/or cropping region for the
1541  * corresponding transformed JPEG image.
1542  *
1543  * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1544  * "flags"
1545  *
1546  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1547  * and #tjGetErrorCode().)
1548  */
1549 DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
1550                           unsigned long jpegSize, int n,
1551                           unsigned char **dstBufs, unsigned long *dstSizes,
1552                           tjtransform *transforms, int flags);
1553 
1554 
1555 /**
1556  * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
1557  *
1558  * @param handle a handle to a TurboJPEG compressor, decompressor or
1559  * transformer instance
1560  *
1561  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1562  */
1563 DLLEXPORT int tjDestroy(tjhandle handle);
1564 
1565 
1566 /**
1567  * Allocate a byte buffer for use with TurboJPEG.  You should always use this
1568  * function to allocate the JPEG destination buffer(s) for the compression and
1569  * transform functions unless you are disabling automatic buffer (re)allocation
1570  * (by setting #TJFLAG_NOREALLOC.)
1571  *
1572  * @param bytes the number of bytes to allocate
1573  *
1574  * @return a pointer to a newly-allocated buffer with the specified number of
1575  * bytes.
1576  *
1577  * @sa tjFree()
1578  */
1579 DLLEXPORT unsigned char *tjAlloc(int bytes);
1580 
1581 
1582 /**
1583  * Load a packed-pixel image from disk into memory.
1584  *
1585  * @param filename name of a file containing a packed-pixel image in Windows
1586  * BMP or PBMPLUS (PPM/PGM) format
1587  *
1588  * @param width pointer to an integer variable that will receive the width (in
1589  * pixels) of the packed-pixel image
1590  *
1591  * @param align row alignment of the packed-pixel buffer to be returned (must
1592  * be a power of 2.)  Setting this parameter to n will cause all rows in the
1593  * buffer to be padded to the nearest multiple of n bytes (1 = unpadded.)
1594  *
1595  * @param height pointer to an integer variable that will receive the height
1596  * (in pixels) of the packed-pixel image
1597  *
1598  * @param pixelFormat pointer to an integer variable that specifies or will
1599  * receive the pixel format of the packed-pixel buffer.  The behavior of
1600  * #tjLoadImage() will vary depending on the value of `*pixelFormat` passed to
1601  * the function:
1602  * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
1603  * use the most optimal pixel format for the file type, and `*pixelFormat` will
1604  * contain the ID of that pixel format upon successful return from this
1605  * function.
1606  * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
1607  * grayscale colormap can be loaded.
1608  * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1609  * converted using a quick & dirty algorithm that is suitable only for testing
1610  * purposes.  (Proper conversion between CMYK and other formats requires a
1611  * color management system.)
1612  * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
1613  * specified pixel format, and pixel format conversion will be performed if
1614  * necessary.
1615  *
1616  * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1617  * "flags".
1618  *
1619  * @return a pointer to a newly-allocated buffer containing the packed-pixel
1620  * image, converted to the chosen pixel format and with the chosen row
1621  * alignment, or NULL if an error occurred (see #tjGetErrorStr2().)  This
1622  * buffer should be freed using #tjFree().
1623  */
1624 DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
1625                                      int align, int *height, int *pixelFormat,
1626                                      int flags);
1627 
1628 
1629 /**
1630  * Save a packed-pixel image from memory to disk.
1631  *
1632  * @param filename name of a file to which to save the packed-pixel image.  The
1633  * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
1634  * on the file extension.
1635  *
1636  * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
1637  * or CMYK image to be saved
1638  *
1639  * @param width width (in pixels) of the packed-pixel image
1640  *
1641  * @param pitch bytes per row in the packed-pixel image.  Setting this
1642  * parameter to 0 is the equivalent of setting it to
1643  * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1644  *
1645  * @param height height (in pixels) of the packed-pixel image
1646  *
1647  * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
1648  * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
1649  * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
1650  * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
1651  * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
1652  * converted to RGB using a quick & dirty algorithm that is suitable only for
1653  * testing purposes.  (Proper conversion between CMYK and other formats
1654  * requires a color management system.)
1655  *
1656  * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1657  * "flags".
1658  *
1659  * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1660  */
1661 DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
1662                           int width, int pitch, int height, int pixelFormat,
1663                           int flags);
1664 
1665 
1666 /**
1667  * Free a byte buffer previously allocated by TurboJPEG.  You should always use
1668  * this function to free JPEG destination buffer(s) that were automatically
1669  * (re)allocated by the compression and transform functions or that were
1670  * manually allocated using #tjAlloc().
1671  *
1672  * @param buffer address of the buffer to free.  If the address is NULL, then
1673  * this function has no effect.
1674  *
1675  * @sa tjAlloc()
1676  */
1677 DLLEXPORT void tjFree(unsigned char *buffer);
1678 
1679 
1680 /**
1681  * Returns a descriptive error message explaining why the last command failed.
1682  *
1683  * @param handle a handle to a TurboJPEG compressor, decompressor, or
1684  * transformer instance, or NULL if the error was generated by a global
1685  * function (but note that retrieving the error message for a global function
1686  * is thread-safe only on platforms that support thread-local storage.)
1687  *
1688  * @return a descriptive error message explaining why the last command failed.
1689  */
1690 DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
1691 
1692 
1693 /**
1694  * Returns a code indicating the severity of the last error.  See
1695  * @ref TJERR "Error codes".
1696  *
1697  * @param handle a handle to a TurboJPEG compressor, decompressor or
1698  * transformer instance
1699  *
1700  * @return a code indicating the severity of the last error.  See
1701  * @ref TJERR "Error codes".
1702  */
1703 DLLEXPORT int tjGetErrorCode(tjhandle handle);
1704 
1705 
1706 /* Backward compatibility functions and macros (nothing to see here) */
1707 
1708 /* TurboJPEG 1.0+ */
1709 
1710 #define NUMSUBOPT  TJ_NUMSAMP
1711 #define TJ_444  TJSAMP_444
1712 #define TJ_422  TJSAMP_422
1713 #define TJ_420  TJSAMP_420
1714 #define TJ_411  TJSAMP_420
1715 #define TJ_GRAYSCALE  TJSAMP_GRAY
1716 
1717 #define TJ_BGR  1
1718 #define TJ_BOTTOMUP  TJFLAG_BOTTOMUP
1719 #define TJ_FORCEMMX  TJFLAG_FORCEMMX
1720 #define TJ_FORCESSE  TJFLAG_FORCESSE
1721 #define TJ_FORCESSE2  TJFLAG_FORCESSE2
1722 #define TJ_ALPHAFIRST  64
1723 #define TJ_FORCESSE3  TJFLAG_FORCESSE3
1724 #define TJ_FASTUPSAMPLE  TJFLAG_FASTUPSAMPLE
1725 
1726 DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
1727 
1728 DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
1729                          int pitch, int height, int pixelSize,
1730                          unsigned char *dstBuf, unsigned long *compressedSize,
1731                          int jpegSubsamp, int jpegQual, int flags);
1732 
1733 DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
1734                            unsigned long jpegSize, unsigned char *dstBuf,
1735                            int width, int pitch, int height, int pixelSize,
1736                            int flags);
1737 
1738 DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
1739                                  unsigned long jpegSize, int *width,
1740                                  int *height);
1741 
1742 DLLEXPORT char *tjGetErrorStr(void);
1743 
1744 /* TurboJPEG 1.1+ */
1745 
1746 #define TJ_YUV  512
1747 
1748 DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
1749 
1750 DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
1751                                   unsigned long jpegSize, int *width,
1752                                   int *height, int *jpegSubsamp);
1753 
1754 DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
1755                                 unsigned long jpegSize, unsigned char *dstBuf,
1756                                 int flags);
1757 
1758 DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
1759                           int pitch, int height, int pixelSize,
1760                           unsigned char *dstBuf, int subsamp, int flags);
1761 
1762 /* TurboJPEG 1.2+ */
1763 
1764 #define TJFLAG_FORCEMMX  8
1765 #define TJFLAG_FORCESSE  16
1766 #define TJFLAG_FORCESSE2  32
1767 #define TJFLAG_FORCESSE3  128
1768 
1769 DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
1770 
1771 DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
1772                            int pitch, int height, int pixelFormat,
1773                            unsigned char *dstBuf, int subsamp, int flags);
1774 
1775 /**
1776  * @}
1777  */
1778 
1779 #ifdef __cplusplus
1780 }
1781 #endif
1782 
1783 #endif
1784