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