2 * Copyright (C)2009-2015, 2017, 2020-2021 D. R. Commander.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #ifndef __TURBOJPEG_H__
31 #define __TURBOJPEG_H__
33 #if defined(_WIN32) && defined(DLLDEFINE)
34 #define DLLEXPORT __declspec(dllexport)
42 * @addtogroup TurboJPEG
43 * TurboJPEG API. This API provides an interface for generating, decoding, and
44 * transforming planar YUV and JPEG images in memory.
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.
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.
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.
80 * The number of chrominance subsampling options
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".
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
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.
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.
111 * Grayscale. The JPEG or YUV image will contain no chrominance components.
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.
118 * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
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.
130 * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
136 * MCU block width (in pixels) for a given level of chrominance subsampling.
138 * - 8x8 for no subsampling or grayscale
144 static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32 };
147 * MCU block height (in pixels) for a given level of chrominance subsampling.
149 * - 8x8 for no subsampling or grayscale
155 static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8 };
159 * The number of pixel formats
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.
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.
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.
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.
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.
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.
208 * Grayscale pixel format. Each 1-byte pixel represents a luminance
209 * (brightness) level from 0 to 255.
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.
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.
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.
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.
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.
253 * Unknown pixel format. Currently this is only used by #tjLoadImage().
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.
265 static const int tjRedOffset[TJ_NUMPF] = {
266 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
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.
276 static const int tjGreenOffset[TJ_NUMPF] = {
277 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
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.
286 static const int tjBlueOffset[TJ_NUMPF] = {
287 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
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.
297 static const int tjAlphaOffset[TJ_NUMPF] = {
298 -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
301 * Pixel size (in bytes) for a given pixel format
303 static const int tjPixelSize[TJ_NUMPF] = {
304 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
309 * The number of JPEG colorspaces
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.
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.
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.
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.
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.
370 * The uncompressed source/destination image is stored in bottom-up (Windows,
371 * OpenGL) order, not top-down (X11) order.
373 #define TJFLAG_BOTTOMUP 2
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.
381 #define TJFLAG_FASTUPSAMPLE 256
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.
389 #define TJFLAG_NOREALLOC 1024
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.
398 #define TJFLAG_FASTDCT 2048
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.
407 #define TJFLAG_ACCURATEDCT 4096
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
414 #define TJFLAG_STOPONWARNING 8192
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.
421 #define TJFLAG_PROGRESSIVE 16384
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>.
431 #define TJFLAG_LIMITSCANS 32768
435 * The number of error codes
444 * The error was non-fatal and recoverable, but the image may still be
449 * The error was fatal and non-recoverable.
456 * The number of transform operations
461 * Transform operations for #tjTransform()
465 * Do not transform the position of the image pixels
469 * Flip (mirror) image horizontally. This transform is imperfect if there
470 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
474 * Flip (mirror) image vertically. This transform is imperfect if there are
475 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
479 * Transpose image (flip/mirror along upper left to lower right axis.) This
480 * transform is always perfect.
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.)
490 * Rotate image clockwise by 90 degrees. This transform is imperfect if
491 * there are any partial MCU blocks on the bottom edge (see
496 * Rotate image 180 degrees. This transform is imperfect if there are any
497 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
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
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.
521 #define TJXOPT_PERFECT 1
523 * This option will cause #tjTransform() to discard any partial MCU blocks that
524 * cannot be transformed.
526 #define TJXOPT_TRIM 2
528 * This option will enable lossless cropping. See #tjTransform() for more
531 #define TJXOPT_CROP 4
533 * This option will discard the color data in the input image and produce
534 * a grayscale output image.
536 #define TJXOPT_GRAY 8
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
543 #define TJXOPT_NOOUTPUT 16
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
551 #define TJXOPT_PROGRESSIVE 32
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
557 #define TJXOPT_COPYNONE 64
579 * The left boundary of the cropping region. This must be evenly divisible
580 * by the MCU block width (see #tjMCUWidth.)
584 * The upper boundary of the cropping region. This must be evenly divisible
585 * by the MCU block height (see #tjMCUHeight.)
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.
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.
603 typedef struct tjtransform {
609 * One of the @ref TJXOP "transform operations"
613 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
617 * Arbitrary data that can be accessed within the body of the callback
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.
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.)
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.
638 * @param planeRegion #tjregion structure containing the width and height of
639 * the component plane to which <tt>coeffs</tt> belongs
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.)
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().
649 * @param transform a pointer to a #tjtransform structure that specifies the
650 * parameters and/or cropping region for this transform
652 * @return 0 if the callback was successful, or -1 if an error occurred.
654 int (*customFilter) (short *coeffs, tjregion arrayRegion,
655 tjregion planeRegion, int componentIndex,
656 int transformIndex, struct tjtransform *transform);
660 * TurboJPEG instance handle
662 typedef void *tjhandle;
666 * Pad the given width to the nearest 32-bit boundary
668 #define TJPAD(width) (((width) + 3) & (~3))
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>.
675 #define TJSCALED(dimension, scalingFactor) \
676 (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
686 * Create a TurboJPEG compressor instance.
688 * @return a handle to the newly-created instance, or NULL if an error
689 * occurred (see #tjGetErrorStr2().)
691 DLLEXPORT tjhandle tjInitCompress(void);
695 * Compress an RGB, grayscale, or CMYK image into a JPEG image.
697 * @param handle a handle to a TurboJPEG compressor or transformer instance
699 * @param srcBuf pointer to an image buffer containing RGB, grayscale, or
700 * CMYK pixels to be compressed
702 * @param width width (in pixels) of the source image
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>.
712 * @param height height (in pixels) of the source image
714 * @param pixelFormat pixel format of the source image (see @ref TJPF
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
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.)
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.
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.
741 * @param jpegSubsamp the level of chrominance subsampling to be used when
742 * generating the JPEG image (see @ref TJSAMP
743 * "Chrominance subsampling options".)
745 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
748 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
751 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
752 * and #tjGetErrorCode().)
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);
761 * Compress a YUV planar image into a JPEG image.
763 * @param handle a handle to a TurboJPEG compressor or transformer instance
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".)
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.
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.
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.
784 * @param subsamp the level of chrominance subsampling used in the source
785 * image (see @ref TJSAMP "Chrominance subsampling options".)
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
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.)
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.
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.
811 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
814 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
817 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
818 * and #tjGetErrorCode().)
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,
828 * Compress a set of Y, U (Cb), and V (Cr) image planes into a JPEG image.
830 * @param handle a handle to a TurboJPEG compressor or transformer instance
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"
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.
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
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.
857 * @param subsamp the level of chrominance subsampling used in the source
858 * image (see @ref TJSAMP "Chrominance subsampling options".)
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
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.)
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.
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.
884 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
887 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
890 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
891 * and #tjGetErrorCode().)
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,
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
913 * @param width width (in pixels) of the image
915 * @param height height (in pixels) of the image
917 * @param jpegSubsamp the level of chrominance subsampling to be used when
918 * generating the JPEG image (see @ref TJSAMP
919 * "Chrominance subsampling options".)
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.
924 DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
928 * The size of the buffer (in bytes) required to hold a YUV planar image with
929 * the given parameters.
931 * @param width width (in pixels) of the image
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.)
936 * @param height height (in pixels) of the image
938 * @param subsamp level of chrominance subsampling in the image (see
939 * @ref TJSAMP "Chrominance subsampling options".)
941 * @return the size of the buffer (in bytes) required to hold the image, or
942 * -1 if the arguments are out of bounds.
944 DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height,
949 * The size of the buffer (in bytes) required to hold a YUV image plane with
950 * the given parameters.
952 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
954 * @param width width (in pixels) of the YUV image. NOTE: this is the width of
955 * the whole image, not the plane width.
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.
960 * @param height height (in pixels) of the YUV image. NOTE: this is the height
961 * of the whole image, not the plane height.
963 * @param subsamp level of chrominance subsampling in the image (see
964 * @ref TJSAMP "Chrominance subsampling options".)
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.
969 DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
970 int height, int subsamp);
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.
977 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
979 * @param width width (in pixels) of the YUV image
981 * @param subsamp level of chrominance subsampling in the image (see
982 * @ref TJSAMP "Chrominance subsampling options".)
984 * @return the plane width of a YUV image plane with the given parameters, or
985 * -1 if the arguments are out of bounds.
987 DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
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.
994 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
996 * @param height height (in pixels) of the YUV image
998 * @param subsamp level of chrominance subsampling in the image (see
999 * @ref TJSAMP "Chrominance subsampling options".)
1001 * @return the plane height of a YUV image plane with the given parameters, or
1002 * -1 if the arguments are out of bounds.
1004 DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
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
1013 * @param handle a handle to a TurboJPEG compressor or transformer instance
1015 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
1018 * @param width width (in pixels) of the source image
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>.
1028 * @param height height (in pixels) of the source image
1030 * @param pixelFormat pixel format of the source image (see @ref TJPF
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".)
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
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.
1050 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1053 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1054 * and #tjGetErrorCode().)
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,
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.
1068 * @param handle a handle to a TurboJPEG compressor or transformer instance
1070 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
1073 * @param width width (in pixels) of the source image
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>.
1083 * @param height height (in pixels) of the source image
1085 * @param pixelFormat pixel format of the source image (see @ref TJPF
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.
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.
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.
1110 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1113 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1114 * and #tjGetErrorCode().)
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);
1123 * Create a TurboJPEG decompressor instance.
1125 * @return a handle to the newly-created instance, or NULL if an error
1126 * occurred (see #tjGetErrorStr2().)
1128 DLLEXPORT tjhandle tjInitDecompress(void);
1132 * Retrieve information about a JPEG image without decompressing it, or prime
1133 * the decompressor with quantization and Huffman tables.
1135 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1137 * @param jpegBuf pointer to a buffer containing a JPEG image or an
1138 * "abbreviated table specification" (AKA "tables-only") datastream. Passing a
1139 * tables-only datastream to this function primes the decompressor with
1140 * quantization and Huffman tables that can be used when decompressing
1141 * subsequent "abbreviated image" datastreams. This is useful, for instance,
1142 * when decompressing video streams in which all frames share the same
1143 * quantization and Huffman tables.
1145 * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1147 * @param width pointer to an integer variable that will receive the width (in
1148 * pixels) of the JPEG image. If <tt>jpegBuf</tt> points to a tables-only
1149 * datastream, then <tt>width</tt> is ignored.
1151 * @param height pointer to an integer variable that will receive the height
1152 * (in pixels) of the JPEG image. If <tt>jpegBuf</tt> points to a tables-only
1153 * datastream, then <tt>height</tt> is ignored.
1155 * @param jpegSubsamp pointer to an integer variable that will receive the
1156 * level of chrominance subsampling used when the JPEG image was compressed
1157 * (see @ref TJSAMP "Chrominance subsampling options".) If <tt>jpegBuf</tt>
1158 * points to a tables-only datastream, then <tt>jpegSubsamp</tt> is ignored.
1160 * @param jpegColorspace pointer to an integer variable that will receive one
1161 * of the JPEG colorspace constants, indicating the colorspace of the JPEG
1162 * image (see @ref TJCS "JPEG colorspaces".) If <tt>jpegBuf</tt>
1163 * points to a tables-only datastream, then <tt>jpegColorspace</tt> is ignored.
1165 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1166 * and #tjGetErrorCode().)
1168 DLLEXPORT int tjDecompressHeader3(tjhandle handle,
1169 const unsigned char *jpegBuf,
1170 unsigned long jpegSize, int *width,
1171 int *height, int *jpegSubsamp,
1172 int *jpegColorspace);
1176 * Returns a list of fractional scaling factors that the JPEG decompressor in
1177 * this implementation of TurboJPEG supports.
1179 * @param numscalingfactors pointer to an integer variable that will receive
1180 * the number of elements in the list
1182 * @return a pointer to a list of fractional scaling factors, or NULL if an
1183 * error is encountered (see #tjGetErrorStr2().)
1185 DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
1189 * Decompress a JPEG image to an RGB, grayscale, or CMYK image.
1191 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1193 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
1195 * @param jpegSize size of the JPEG image (in bytes)
1197 * @param dstBuf pointer to an image buffer that will receive the decompressed
1198 * image. This buffer should normally be <tt>pitch * scaledHeight</tt> bytes
1199 * in size, where <tt>scaledHeight</tt> can be determined by calling
1200 * #TJSCALED() with the JPEG image height and one of the scaling factors
1201 * returned by #tjGetScalingFactors(). The <tt>dstBuf</tt> pointer may also be
1202 * used to decompress into a specific region of a larger buffer.
1204 * @param width desired width (in pixels) of the destination image. If this is
1205 * different than the width of the JPEG image being decompressed, then
1206 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1207 * possible image that will fit within the desired width. If <tt>width</tt> is
1208 * set to 0, then only the height will be considered when determining the
1209 * scaled image size.
1211 * @param pitch bytes per line in the destination image. Normally, this is
1212 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed image
1213 * is unpadded, else <tt>#TJPAD(scaledWidth * #tjPixelSize[pixelFormat])</tt>
1214 * if each line of the decompressed image is padded to the nearest 32-bit
1215 * boundary, as is the case for Windows bitmaps. (NOTE: <tt>scaledWidth</tt>
1216 * can be determined by calling #TJSCALED() with the JPEG image width and one
1217 * of the scaling factors returned by #tjGetScalingFactors().) You can also be
1218 * clever and use the pitch parameter to skip lines, etc. Setting this
1219 * parameter to 0 is the equivalent of setting it to
1220 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>.
1222 * @param height desired height (in pixels) of the destination image. If this
1223 * is different than the height of the JPEG image being decompressed, then
1224 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1225 * possible image that will fit within the desired height. If <tt>height</tt>
1226 * is set to 0, then only the width will be considered when determining the
1227 * scaled image size.
1229 * @param pixelFormat pixel format of the destination image (see @ref
1230 * TJPF "Pixel formats".)
1232 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1235 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1236 * and #tjGetErrorCode().)
1238 DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
1239 unsigned long jpegSize, unsigned char *dstBuf,
1240 int width, int pitch, int height, int pixelFormat,
1245 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
1246 * decompression but leaves out the color conversion step, so a planar YUV
1247 * image is generated instead of an RGB image.
1249 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1251 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
1253 * @param jpegSize size of the JPEG image (in bytes)
1255 * @param dstBuf pointer to an image buffer that will receive the YUV image.
1256 * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
1257 * on the image width, height, padding, and level of subsampling. The Y,
1258 * U (Cb), and V (Cr) image planes will be stored sequentially in the buffer
1259 * (refer to @ref YUVnotes "YUV Image Format Notes".)
1261 * @param width desired width (in pixels) of the YUV image. If this is
1262 * different than the width of the JPEG image being decompressed, then
1263 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1264 * possible image that will fit within the desired width. If <tt>width</tt> is
1265 * set to 0, then only the height will be considered when determining the
1266 * scaled image size. If the scaled width is not an even multiple of the MCU
1267 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1268 * performed within TurboJPEG.
1270 * @param pad the width of each line in each plane of the YUV image will be
1271 * padded to the nearest multiple of this number of bytes (must be a power of
1272 * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
1275 * @param height desired height (in pixels) of the YUV image. If this is
1276 * different than the height of the JPEG image being decompressed, then
1277 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1278 * possible image that will fit within the desired height. If <tt>height</tt>
1279 * is set to 0, then only the width will be considered when determining the
1280 * scaled image size. If the scaled height is not an even multiple of the MCU
1281 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1282 * performed within TurboJPEG.
1284 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1287 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1288 * and #tjGetErrorCode().)
1290 DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
1291 unsigned long jpegSize, unsigned char *dstBuf,
1292 int width, int pad, int height, int flags);
1296 * Decompress a JPEG image into separate Y, U (Cb), and V (Cr) image
1297 * planes. This function performs JPEG decompression but leaves out the color
1298 * conversion step, so a planar YUV image is generated instead of an RGB image.
1300 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1302 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
1304 * @param jpegSize size of the JPEG image (in bytes)
1306 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1307 * (or just a Y plane, if decompressing a grayscale image) that will receive
1308 * the YUV image. These planes can be contiguous or non-contiguous in memory.
1309 * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
1310 * on the scaled image width, scaled image height, strides, and level of
1311 * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
1314 * @param width desired width (in pixels) of the YUV image. If this is
1315 * different than the width of the JPEG image being decompressed, then
1316 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1317 * possible image that will fit within the desired width. If <tt>width</tt> is
1318 * set to 0, then only the height will be considered when determining the
1319 * scaled image size. If the scaled width is not an even multiple of the MCU
1320 * block width (see #tjMCUWidth), then an intermediate buffer copy will be
1321 * performed within TurboJPEG.
1323 * @param strides an array of integers, each specifying the number of bytes per
1324 * line in the corresponding plane of the output image. Setting the stride for
1325 * any plane to 0 is the same as setting it to the scaled plane width (see
1326 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1327 * the strides for all planes will be set to their respective scaled plane
1328 * widths. You can adjust the strides in order to add an arbitrary amount of
1329 * line padding to each plane or to decompress the JPEG image into a subregion
1330 * of a larger YUV planar image.
1332 * @param height desired height (in pixels) of the YUV image. If this is
1333 * different than the height of the JPEG image being decompressed, then
1334 * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
1335 * possible image that will fit within the desired height. If <tt>height</tt>
1336 * is set to 0, then only the width will be considered when determining the
1337 * scaled image size. If the scaled height is not an even multiple of the MCU
1338 * block height (see #tjMCUHeight), then an intermediate buffer copy will be
1339 * performed within TurboJPEG.
1341 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1344 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1345 * and #tjGetErrorCode().)
1347 DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
1348 const unsigned char *jpegBuf,
1349 unsigned long jpegSize,
1350 unsigned char **dstPlanes, int width,
1351 int *strides, int height, int flags);
1355 * Decode a YUV planar image into an RGB or grayscale image. This function
1356 * uses the accelerated color conversion routines in the underlying
1357 * codec but does not execute any of the other steps in the JPEG decompression
1360 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1362 * @param srcBuf pointer to an image buffer containing a YUV planar image to be
1363 * decoded. The size of this buffer should match the value returned by
1364 * #tjBufSizeYUV2() for the given image width, height, padding, and level of
1365 * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
1366 * stored sequentially in the source buffer (refer to @ref YUVnotes
1367 * "YUV Image Format Notes".)
1369 * @param pad Use this parameter to specify that the width of each line in each
1370 * plane of the YUV source image is padded to the nearest multiple of this
1371 * number of bytes (must be a power of 2.)
1373 * @param subsamp the level of chrominance subsampling used in the YUV source
1374 * image (see @ref TJSAMP "Chrominance subsampling options".)
1376 * @param dstBuf pointer to an image buffer that will receive the decoded
1377 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1378 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1379 * specific region of a larger buffer.
1381 * @param width width (in pixels) of the source and destination images
1383 * @param pitch bytes per line in the destination image. Normally, this should
1384 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1385 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1386 * of the destination image should be padded to the nearest 32-bit boundary, as
1387 * is the case for Windows bitmaps. You can also be clever and use the pitch
1388 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1389 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1391 * @param height height (in pixels) of the source and destination images
1393 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1396 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1399 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1400 * and #tjGetErrorCode().)
1402 DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
1403 int pad, int subsamp, unsigned char *dstBuf,
1404 int width, int pitch, int height, int pixelFormat,
1409 * Decode a set of Y, U (Cb), and V (Cr) image planes into an RGB or grayscale
1410 * image. This function uses the accelerated color conversion routines in the
1411 * underlying codec but does not execute any of the other steps in the JPEG
1412 * decompression process.
1414 * @param handle a handle to a TurboJPEG decompressor or transformer instance
1416 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1417 * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1418 * to be decoded. These planes can be contiguous or non-contiguous in memory.
1419 * The size of each plane should match the value returned by #tjPlaneSizeYUV()
1420 * for the given image width, height, strides, and level of chrominance
1421 * subsampling. Refer to @ref YUVnotes "YUV Image Format Notes" for more
1424 * @param strides an array of integers, each specifying the number of bytes per
1425 * line in the corresponding plane of the YUV source image. Setting the stride
1426 * for any plane to 0 is the same as setting it to the plane width (see
1427 * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
1428 * the strides for all planes will be set to their respective plane widths.
1429 * You can adjust the strides in order to specify an arbitrary amount of line
1430 * padding in each plane or to decode a subregion of a larger YUV planar image.
1432 * @param subsamp the level of chrominance subsampling used in the YUV source
1433 * image (see @ref TJSAMP "Chrominance subsampling options".)
1435 * @param dstBuf pointer to an image buffer that will receive the decoded
1436 * image. This buffer should normally be <tt>pitch * height</tt> bytes in
1437 * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
1438 * specific region of a larger buffer.
1440 * @param width width (in pixels) of the source and destination images
1442 * @param pitch bytes per line in the destination image. Normally, this should
1443 * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
1444 * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
1445 * of the destination image should be padded to the nearest 32-bit boundary, as
1446 * is the case for Windows bitmaps. You can also be clever and use the pitch
1447 * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
1448 * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
1450 * @param height height (in pixels) of the source and destination images
1452 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1455 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1458 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1459 * and #tjGetErrorCode().)
1461 DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
1462 const unsigned char **srcPlanes,
1463 const int *strides, int subsamp,
1464 unsigned char *dstBuf, int width, int pitch,
1465 int height, int pixelFormat, int flags);
1469 * Create a new TurboJPEG transformer instance.
1471 * @return a handle to the newly-created instance, or NULL if an error
1472 * occurred (see #tjGetErrorStr2().)
1474 DLLEXPORT tjhandle tjInitTransform(void);
1478 * Losslessly transform a JPEG image into another JPEG image. Lossless
1479 * transforms work by moving the raw DCT coefficients from one JPEG image
1480 * structure to another without altering the values of the coefficients. While
1481 * this is typically faster than decompressing the image, transforming it, and
1482 * re-compressing it, lossless transforms are not free. Each lossless
1483 * transform requires reading and performing Huffman decoding on all of the
1484 * coefficients in the source image, regardless of the size of the destination
1485 * image. Thus, this function provides a means of generating multiple
1486 * transformed images from the same source or applying multiple
1487 * transformations simultaneously, in order to eliminate the need to read the
1488 * source coefficients multiple times.
1490 * @param handle a handle to a TurboJPEG transformer instance
1492 * @param jpegBuf pointer to a buffer containing the JPEG source image to
1495 * @param jpegSize size of the JPEG source image (in bytes)
1497 * @param n the number of transformed JPEG images to generate
1499 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
1500 * will receive a JPEG image that has been transformed using the parameters in
1501 * <tt>transforms[i]</tt>. TurboJPEG has the ability to reallocate the JPEG
1502 * buffer to accommodate the size of the JPEG image. Thus, you can choose to:
1503 * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
1504 * let TurboJPEG grow the buffer as needed,
1505 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the buffer
1507 * -# pre-allocate the buffer to a "worst case" size determined by calling
1508 * #tjBufSize() with the transformed or cropped width and height. Under normal
1509 * circumstances, this should ensure that the buffer never has to be
1510 * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.) Note,
1511 * however, that there are some rare cases (such as transforming images with a
1512 * large amount of embedded EXIF or ICC profile data) in which the output image
1513 * will be larger than the worst-case size, and #TJFLAG_NOREALLOC cannot be
1514 * used in those cases.
1516 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to the size of
1517 * your pre-allocated buffer. In any case, unless you have set
1518 * #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> upon return
1519 * from this function, as it may have changed.
1521 * @param dstSizes pointer to an array of n unsigned long variables that will
1522 * receive the actual sizes (in bytes) of each transformed JPEG image. If
1523 * <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
1524 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon return,
1525 * <tt>dstSizes[i]</tt> will contain the size of the JPEG image (in bytes.)
1527 * @param transforms pointer to an array of n #tjtransform structures, each of
1528 * which specifies the transform parameters and/or cropping region for the
1529 * corresponding transformed output image.
1531 * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
1534 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
1535 * and #tjGetErrorCode().)
1537 DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
1538 unsigned long jpegSize, int n,
1539 unsigned char **dstBufs, unsigned long *dstSizes,
1540 tjtransform *transforms, int flags);
1544 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
1546 * @param handle a handle to a TurboJPEG compressor, decompressor or
1547 * transformer instance
1549 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1551 DLLEXPORT int tjDestroy(tjhandle handle);
1555 * Allocate an image buffer for use with TurboJPEG. You should always use
1556 * this function to allocate the JPEG destination buffer(s) for the compression
1557 * and transform functions unless you are disabling automatic buffer
1558 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
1560 * @param bytes the number of bytes to allocate
1562 * @return a pointer to a newly-allocated buffer with the specified number of
1567 DLLEXPORT unsigned char *tjAlloc(int bytes);
1571 * Load an uncompressed image from disk into memory.
1573 * @param filename name of a file containing an uncompressed image in Windows
1574 * BMP or PBMPLUS (PPM/PGM) format
1576 * @param width pointer to an integer variable that will receive the width (in
1577 * pixels) of the uncompressed image
1579 * @param align row alignment of the image buffer to be returned (must be a
1580 * power of 2.) For instance, setting this parameter to 4 will cause all rows
1581 * in the image buffer to be padded to the nearest 32-bit boundary, and setting
1582 * this parameter to 1 will cause all rows in the image buffer to be unpadded.
1584 * @param height pointer to an integer variable that will receive the height
1585 * (in pixels) of the uncompressed image
1587 * @param pixelFormat pointer to an integer variable that specifies or will
1588 * receive the pixel format of the uncompressed image buffer. The behavior of
1589 * #tjLoadImage() will vary depending on the value of <tt>*pixelFormat</tt>
1590 * passed to the function:
1591 * - @ref TJPF_UNKNOWN : The uncompressed image buffer returned by the function
1592 * will use the most optimal pixel format for the file type, and
1593 * <tt>*pixelFormat</tt> will contain the ID of this pixel format upon
1594 * successful return from the function.
1595 * - @ref TJPF_GRAY : Only PGM files and 8-bit BMP files with a grayscale
1596 * colormap can be loaded.
1597 * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1598 * converted using a quick & dirty algorithm that is suitable only for testing
1599 * purposes (proper conversion between CMYK and other formats requires a color
1600 * management system.)
1601 * - Other @ref TJPF "pixel formats" : The uncompressed image buffer will use
1602 * the specified pixel format, and pixel format conversion will be performed if
1605 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1608 * @return a pointer to a newly-allocated buffer containing the uncompressed
1609 * image, converted to the chosen pixel format and with the chosen row
1610 * alignment, or NULL if an error occurred (see #tjGetErrorStr2().) This
1611 * buffer should be freed using #tjFree().
1613 DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
1614 int align, int *height, int *pixelFormat,
1619 * Save an uncompressed image from memory to disk.
1621 * @param filename name of a file to which to save the uncompressed image.
1622 * The image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format,
1623 * depending on the file extension.
1625 * @param buffer pointer to an image buffer containing RGB, grayscale, or
1626 * CMYK pixels to be saved
1628 * @param width width (in pixels) of the uncompressed image
1630 * @param pitch bytes per line in the image buffer. Setting this parameter to
1631 * 0 is the equivalent of setting it to
1632 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
1634 * @param height height (in pixels) of the uncompressed image
1636 * @param pixelFormat pixel format of the image buffer (see @ref TJPF
1637 * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
1638 * image will be stored in PGM or 8-bit (indexed color) BMP format. Otherwise,
1639 * the image will be stored in PPM or 24-bit BMP format. If this parameter
1640 * is set to @ref TJPF_CMYK, then the CMYK pixels will be converted to RGB
1641 * using a quick & dirty algorithm that is suitable only for testing (proper
1642 * conversion between CMYK and other formats requires a color management
1645 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
1648 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
1650 DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
1651 int width, int pitch, int height, int pixelFormat,
1656 * Free an image buffer previously allocated by TurboJPEG. You should always
1657 * use this function to free JPEG destination buffer(s) that were automatically
1658 * (re)allocated by the compression and transform functions or that were
1659 * manually allocated using #tjAlloc().
1661 * @param buffer address of the buffer to free. If the address is NULL, then
1662 * this function has no effect.
1666 DLLEXPORT void tjFree(unsigned char *buffer);
1670 * Returns a descriptive error message explaining why the last command failed.
1672 * @param handle a handle to a TurboJPEG compressor, decompressor, or
1673 * transformer instance, or NULL if the error was generated by a global
1674 * function (but note that retrieving the error message for a global function
1675 * is thread-safe only on platforms that support thread-local storage.)
1677 * @return a descriptive error message explaining why the last command failed.
1679 DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
1683 * Returns a code indicating the severity of the last error. See
1684 * @ref TJERR "Error codes".
1686 * @param handle a handle to a TurboJPEG compressor, decompressor or
1687 * transformer instance
1689 * @return a code indicating the severity of the last error. See
1690 * @ref TJERR "Error codes".
1692 DLLEXPORT int tjGetErrorCode(tjhandle handle);
1695 /* Deprecated functions and macros */
1696 #define TJFLAG_FORCEMMX 8
1697 #define TJFLAG_FORCESSE 16
1698 #define TJFLAG_FORCESSE2 32
1699 #define TJFLAG_FORCESSE3 128
1702 /* Backward compatibility functions and macros (nothing to see here) */
1703 #define NUMSUBOPT TJ_NUMSAMP
1704 #define TJ_444 TJSAMP_444
1705 #define TJ_422 TJSAMP_422
1706 #define TJ_420 TJSAMP_420
1707 #define TJ_411 TJSAMP_420
1708 #define TJ_GRAYSCALE TJSAMP_GRAY
1711 #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
1712 #define TJ_FORCEMMX TJFLAG_FORCEMMX
1713 #define TJ_FORCESSE TJFLAG_FORCESSE
1714 #define TJ_FORCESSE2 TJFLAG_FORCESSE2
1715 #define TJ_ALPHAFIRST 64
1716 #define TJ_FORCESSE3 TJFLAG_FORCESSE3
1717 #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
1720 DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
1722 DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
1724 DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
1726 DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
1727 int pitch, int height, int pixelSize,
1728 unsigned char *dstBuf, unsigned long *compressedSize,
1729 int jpegSubsamp, int jpegQual, int flags);
1731 DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
1732 int pitch, int height, int pixelSize,
1733 unsigned char *dstBuf, int subsamp, int flags);
1735 DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
1736 int pitch, int height, int pixelFormat,
1737 unsigned char *dstBuf, int subsamp, int flags);
1739 DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
1740 unsigned long jpegSize, int *width,
1743 DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
1744 unsigned long jpegSize, int *width,
1745 int *height, int *jpegSubsamp);
1747 DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
1748 unsigned long jpegSize, unsigned char *dstBuf,
1749 int width, int pitch, int height, int pixelSize,
1752 DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
1753 unsigned long jpegSize, unsigned char *dstBuf,
1756 DLLEXPORT char *tjGetErrorStr(void);