2 * Copyright (C)2009-2013 D. R. Commander. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __TURBOJPEG_H__
30 #define __TURBOJPEG_H__
32 #if defined(_WIN32) && defined(DLLDEFINE)
33 #define DLLEXPORT __declspec(dllexport)
41 * @addtogroup TurboJPEG
42 * TurboJPEG API. This API provides an interface for generating, decoding, and
43 * transforming planar YUV and JPEG images in memory.
50 * The number of chrominance subsampling options
55 * Chrominance subsampling options.
56 * When an image is converted from the RGB to the YCbCr colorspace as part of
57 * the JPEG compression process, some of the Cb and Cr (chrominance) components
58 * can be discarded or averaged together to produce a smaller image with little
59 * perceptible loss of image clarity (the human eye is more sensitive to small
60 * changes in brightness than small changes in color.) This is called
61 * "chrominance subsampling".
63 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
64 * convention of the digital video community, the TurboJPEG API uses "YUV" to
65 * refer to an image format consisting of Y, Cb, and Cr image planes.
70 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
71 * YUV image will contain one chrominance component for every pixel in the
76 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
77 * chrominance component for every 2x1 block of pixels in the source image.
81 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
82 * chrominance component for every 2x2 block of pixels in the source image.
86 * Grayscale. The JPEG or YUV image will contain no chrominance components.
90 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
91 * chrominance component for every 1x2 block of pixels in the source image.
92 * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
98 * MCU block width (in pixels) for a given level of chrominance subsampling.
100 * - 8x8 for no subsampling or grayscale
105 static const int tjMCUWidth[TJ_NUMSAMP] = {8, 16, 16, 8, 8};
108 * MCU block height (in pixels) for a given level of chrominance subsampling.
110 * - 8x8 for no subsampling or grayscale
115 static const int tjMCUHeight[TJ_NUMSAMP] = {8, 8, 16, 8, 16};
119 * The number of pixel formats
129 * RGB pixel format. The red, green, and blue components in the image are
130 * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
131 * address within each pixel.
135 * BGR pixel format. The red, green, and blue components in the image are
136 * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
137 * address within each pixel.
141 * RGBX pixel format. The red, green, and blue components in the image are
142 * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
143 * address within each pixel. The X component is ignored when compressing
144 * and undefined when decompressing.
148 * BGRX pixel format. The red, green, and blue components in the image are
149 * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
150 * address within each pixel. The X component is ignored when compressing
151 * and undefined when decompressing.
155 * XBGR pixel format. The red, green, and blue components in the image are
156 * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
157 * address within each pixel. The X component is ignored when compressing
158 * and undefined when decompressing.
162 * XRGB pixel format. The red, green, and blue components in the image are
163 * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
164 * address within each pixel. The X component is ignored when compressing
165 * and undefined when decompressing.
169 * Grayscale pixel format. Each 1-byte pixel represents a luminance
170 * (brightness) level from 0 to 255.
174 * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
175 * decompressing, the X component is guaranteed to be 0xFF, which can be
176 * interpreted as an opaque alpha channel.
180 * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
181 * decompressing, the X component is guaranteed to be 0xFF, which can be
182 * interpreted as an opaque alpha channel.
186 * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
187 * decompressing, the X component is guaranteed to be 0xFF, which can be
188 * interpreted as an opaque alpha channel.
192 * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
193 * decompressing, the X component is guaranteed to be 0xFF, which can be
194 * interpreted as an opaque alpha channel.
200 * Red offset (in bytes) for a given pixel format. This specifies the number
201 * of bytes that the red component is offset from the start of the pixel. For
202 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
203 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>.
205 static const int tjRedOffset[TJ_NUMPF] = {0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1};
207 * Green offset (in bytes) for a given pixel format. This specifies the number
208 * of bytes that the green component is offset from the start of the pixel.
209 * For instance, if a pixel of format TJ_BGRX is stored in
210 * <tt>char pixel[]</tt>, then the green component will be
211 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>.
213 static const int tjGreenOffset[TJ_NUMPF] = {1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2};
215 * Blue offset (in bytes) for a given pixel format. This specifies the number
216 * of bytes that the Blue component is offset from the start of the pixel. For
217 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
218 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>.
220 static const int tjBlueOffset[TJ_NUMPF] = {2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3};
223 * Pixel size (in bytes) for a given pixel format.
225 static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4};
229 * The uncompressed source/destination image is stored in bottom-up (Windows,
230 * OpenGL) order, not top-down (X11) order.
232 #define TJFLAG_BOTTOMUP 2
234 * Turn off CPU auto-detection and force TurboJPEG to use MMX code (if the
235 * underlying codec supports it.)
237 #define TJFLAG_FORCEMMX 8
239 * Turn off CPU auto-detection and force TurboJPEG to use SSE code (if the
240 * underlying codec supports it.)
242 #define TJFLAG_FORCESSE 16
244 * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (if the
245 * underlying codec supports it.)
247 #define TJFLAG_FORCESSE2 32
249 * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (if the
250 * underlying codec supports it.)
252 #define TJFLAG_FORCESSE3 128
254 * When decompressing an image that was compressed using chrominance
255 * subsampling, use the fastest chrominance upsampling algorithm available in
256 * the underlying codec. The default is to use smooth upsampling, which
257 * creates a smooth transition between neighboring chrominance components in
258 * order to reduce upsampling artifacts in the decompressed image.
260 #define TJFLAG_FASTUPSAMPLE 256
262 * Disable buffer (re)allocation. If passed to #tjCompress2() or
263 * #tjTransform(), this flag will cause those functions to generate an error if
264 * the JPEG image buffer is invalid or too small rather than attempting to
265 * allocate or reallocate that buffer. This reproduces the behavior of earlier
266 * versions of TurboJPEG.
268 #define TJFLAG_NOREALLOC 1024
270 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
271 * default if this flag is not specified is implementation-specific. For
272 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
273 * algorithm by default when compressing, because this has been shown to have
274 * only a very slight effect on accuracy, but it uses the accurate algorithm
275 * when decompressing, because this has been shown to have a larger effect.
277 #define TJFLAG_FASTDCT 2048
279 * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
280 * The default if this flag is not specified is implementation-specific. For
281 * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
282 * algorithm by default when compressing, because this has been shown to have
283 * only a very slight effect on accuracy, but it uses the accurate algorithm
284 * when decompressing, because this has been shown to have a larger effect.
286 #define TJFLAG_ACCURATEDCT 4096
290 * The number of transform operations
295 * Transform operations for #tjTransform()
300 * Do not transform the position of the image pixels
304 * Flip (mirror) image horizontally. This transform is imperfect if there
305 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
309 * Flip (mirror) image vertically. This transform is imperfect if there are
310 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
314 * Transpose image (flip/mirror along upper left to lower right axis.) This
315 * transform is always perfect.
319 * Transverse transpose image (flip/mirror along upper right to lower left
320 * axis.) This transform is imperfect if there are any partial MCU blocks in
321 * the image (see #TJXOPT_PERFECT.)
325 * Rotate image clockwise by 90 degrees. This transform is imperfect if
326 * there are any partial MCU blocks on the bottom edge (see
331 * Rotate image 180 degrees. This transform is imperfect if there are any
332 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
336 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
337 * if there are any partial MCU blocks on the right edge (see
345 * This option will cause #tjTransform() to return an error if the transform is
346 * not perfect. Lossless transforms operate on MCU blocks, whose size depends
347 * on the level of chrominance subsampling used (see #tjMCUWidth
348 * and #tjMCUHeight.) If the image's width or height is not evenly divisible
349 * by the MCU block size, then there will be partial MCU blocks on the right
350 * and/or bottom edges. It is not possible to move these partial MCU blocks to
351 * the top or left of the image, so any transform that would require that is
352 * "imperfect." If this option is not specified, then any partial MCU blocks
353 * that cannot be transformed will be left in place, which will create
354 * odd-looking strips on the right or bottom edge of the image.
356 #define TJXOPT_PERFECT 1
358 * This option will cause #tjTransform() to discard any partial MCU blocks that
359 * cannot be transformed.
361 #define TJXOPT_TRIM 2
363 * This option will enable lossless cropping. See #tjTransform() for more
366 #define TJXOPT_CROP 4
368 * This option will discard the color data in the input image and produce
369 * a grayscale output image.
371 #define TJXOPT_GRAY 8
373 * This option will prevent #tjTransform() from outputting a JPEG image for
374 * this particular transform (this can be used in conjunction with a custom
375 * filter to capture the transformed DCT coefficients without transcoding
378 #define TJXOPT_NOOUTPUT 16
402 * The left boundary of the cropping region. This must be evenly divisible
403 * by the MCU block width (see #tjMCUWidth.)
407 * The upper boundary of the cropping region. This must be evenly divisible
408 * by the MCU block height (see #tjMCUHeight.)
412 * The width of the cropping region. Setting this to 0 is the equivalent of
413 * setting it to the width of the source JPEG image - x.
417 * The height of the cropping region. Setting this to 0 is the equivalent of
418 * setting it to the height of the source JPEG image - y.
426 typedef struct tjtransform
433 * One of the @ref TJXOP "transform operations"
437 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
441 * Arbitrary data that can be accessed within the body of the callback
446 * A callback function that can be used to modify the DCT coefficients
447 * after they are losslessly transformed but before they are transcoded to a
448 * new JPEG image. This allows for custom filters or other transformations
449 * to be applied in the frequency domain.
451 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
452 * this pointer is not guaranteed to be valid once the callback
453 * returns, so applications wishing to hand off the DCT coefficients
454 * to another function or library should make a copy of them within
455 * the body of the callback.)
456 * @param arrayRegion #tjregion structure containing the width and height of
457 * the array pointed to by <tt>coeffs</tt> as well as its offset
458 * relative to the component plane. TurboJPEG implementations may
459 * choose to split each component plane into multiple DCT coefficient
460 * arrays and call the callback function once for each array.
461 * @param planeRegion #tjregion structure containing the width and height of
462 * the component plane to which <tt>coeffs</tt> belongs
463 * @param componentID ID number of the component plane to which
464 * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of
465 * 0, 1, and 2 in typical JPEG images.)
466 * @param transformID ID number of the transformed image to which
467 * <tt>coeffs</tt> belongs. This is the same as the index of the
468 * transform in the <tt>transforms</tt> array that was passed to
470 * @param transform a pointer to a #tjtransform structure that specifies the
471 * parameters and/or cropping region for this transform
473 * @return 0 if the callback was successful, or -1 if an error occurred.
475 int (*customFilter)(short *coeffs, tjregion arrayRegion,
476 tjregion planeRegion, int componentIndex, int transformIndex,
477 struct tjtransform *transform);
481 * TurboJPEG instance handle
483 typedef void* tjhandle;
487 * Pad the given width to the nearest 32-bit boundary
489 #define TJPAD(width) (((width)+3)&(~3))
492 * Compute the scaled value of <tt>dimension</tt> using the given scaling
493 * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
494 * scalingFactor)</tt>.
496 #define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \
497 + scalingFactor.denom - 1) / scalingFactor.denom)
506 * Create a TurboJPEG compressor instance.
508 * @return a handle to the newly-created instance, or NULL if an error
509 * occurred (see #tjGetErrorStr().)
511 DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
515 * Compress an RGB or grayscale image into a JPEG image.
517 * @param handle a handle to a TurboJPEG compressor or transformer instance
518 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
520 * @param width width (in pixels) of the source image
521 * @param pitch bytes per line of the source image. Normally, this should be
522 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
523 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
524 * the image is padded to the nearest 32-bit boundary, as is the case
525 * for Windows bitmaps. You can also be clever and use this parameter
526 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
527 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
528 * @param height height (in pixels) of the source image
529 * @param pixelFormat pixel format of the source image (see @ref TJPF
531 * @param jpegBuf address of a pointer to an image buffer that will receive the
532 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
533 * to accommodate the size of the JPEG image. Thus, you can choose to:
534 * -# pre-allocate the JPEG buffer with an arbitrary size using
535 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
536 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the
538 * -# pre-allocate the buffer to a "worst case" size determined by
539 * calling #tjBufSize(). This should ensure that the buffer never has
540 * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
542 * If you choose option 1, <tt>*jpegSize</tt> should be set to the
543 * size of your pre-allocated buffer. In any case, unless you have
544 * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon
545 * return from this function, as it may have changed.
546 * @param jpegSize pointer to an unsigned long variable that holds the size of
547 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a
548 * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the
549 * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the
550 * size of the JPEG image (in bytes.)
551 * @param jpegSubsamp the level of chrominance subsampling to be used when
552 * generating the JPEG image (see @ref TJSAMP
553 * "Chrominance subsampling options".)
554 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
556 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
559 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
561 DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
562 int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
563 unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags);
567 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
568 * the given parameters. The number of bytes returned by this function is
569 * larger than the size of the uncompressed source image. The reason for this
570 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
571 * for a very high-quality JPEG image with very high-frequency content to
572 * expand rather than compress when converted to the JPEG format. Such images
573 * represent a very rare corner case, but since there is no way to predict the
574 * size of a JPEG image prior to compression, the corner case has to be
577 * @param width width of the image (in pixels)
578 * @param height height of the image (in pixels)
579 * @param jpegSubsamp the level of chrominance subsampling to be used when
580 * generating the JPEG image (see @ref TJSAMP
581 * "Chrominance subsampling options".)
583 * @return the maximum size of the buffer (in bytes) required to hold the
584 * image, or -1 if the arguments are out of bounds.
586 DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
591 * The size of the buffer (in bytes) required to hold a YUV planar image with
592 * the given parameters.
594 * @param width width of the image (in pixels)
595 * @param height height of the image (in pixels)
596 * @param subsamp level of chrominance subsampling in the image (see
597 * @ref TJSAMP "Chrominance subsampling options".)
599 * @return the size of the buffer (in bytes) required to hold the image, or
600 * -1 if the arguments are out of bounds.
602 DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
607 * Encode an RGB or grayscale image into a YUV planar image. This function
608 * uses the accelerated color conversion routines in TurboJPEG's underlying
609 * codec to produce a planar YUV image that is suitable for X Video.
610 * Specifically, if the chrominance components are subsampled along the
611 * horizontal dimension, then the width of the luminance plane is padded to the
612 * nearest multiple of 2 in the output image (same goes for the height of the
613 * luminance plane, if the chrominance components are subsampled along the
614 * vertical dimension.) Also, each line of each plane in the output image is
615 * padded to 4 bytes. Although this will work with any subsampling option, it
616 * is really only useful in combination with TJ_420, which produces an image
617 * compatible with the I420 (AKA "YUV420P") format.
619 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
620 * convention of the digital video community, the TurboJPEG API uses "YUV" to
621 * refer to an image format consisting of Y, Cb, and Cr image planes.
623 * @param handle a handle to a TurboJPEG compressor or transformer instance
624 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
626 * @param width width (in pixels) of the source image
627 * @param pitch bytes per line of the source image. Normally, this should be
628 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
629 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
630 * the image is padded to the nearest 32-bit boundary, as is the case
631 * for Windows bitmaps. You can also be clever and use this parameter
632 * to skip lines, etc. Setting this parameter to 0 is the equivalent of
633 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
634 * @param height height (in pixels) of the source image
635 * @param pixelFormat pixel format of the source image (see @ref TJPF
637 * @param dstBuf pointer to an image buffer that will receive the YUV image.
638 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
639 * based on the image width, height, and level of chrominance
641 * @param subsamp the level of chrominance subsampling to be used when
642 * generating the YUV image (see @ref TJSAMP
643 * "Chrominance subsampling options".)
644 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
647 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
649 DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle,
650 unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat,
651 unsigned char *dstBuf, int subsamp, int flags);
655 * Create a TurboJPEG decompressor instance.
657 * @return a handle to the newly-created instance, or NULL if an error
658 * occurred (see #tjGetErrorStr().)
660 DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
664 * Retrieve information about a JPEG image without decompressing it.
666 * @param handle a handle to a TurboJPEG decompressor or transformer instance
667 * @param jpegBuf pointer to a buffer containing a JPEG image
668 * @param jpegSize size of the JPEG image (in bytes)
669 * @param width pointer to an integer variable that will receive the width (in
670 * pixels) of the JPEG image
671 * @param height pointer to an integer variable that will receive the height
672 * (in pixels) of the JPEG image
673 * @param jpegSubsamp pointer to an integer variable that will receive the
674 * level of chrominance subsampling used when compressing the JPEG image
675 * (see @ref TJSAMP "Chrominance subsampling options".)
677 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
679 DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
680 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
685 * Returns a list of fractional scaling factors that the JPEG decompressor in
686 * this implementation of TurboJPEG supports.
688 * @param numscalingfactors pointer to an integer variable that will receive
689 * the number of elements in the list
691 * @return a pointer to a list of fractional scaling factors, or NULL if an
692 * error is encountered (see #tjGetErrorStr().)
694 DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors);
698 * Decompress a JPEG image to an RGB or grayscale image.
700 * @param handle a handle to a TurboJPEG decompressor or transformer instance
701 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
702 * @param jpegSize size of the JPEG image (in bytes)
703 * @param dstBuf pointer to an image buffer that will receive the decompressed
704 * image. This buffer should normally be <tt>pitch * scaledHeight</tt>
705 * bytes in size, where <tt>scaledHeight</tt> can be determined by
706 * calling #TJSCALED() with the JPEG image height and one of the scaling
707 * factors returned by #tjGetScalingFactors(). The <tt>dstBuf</tt>
708 * pointer may also be used to decompress into a specific region of a
710 * @param width desired width (in pixels) of the destination image. If this is
711 * different than the width of the JPEG image being decompressed, then
712 * TurboJPEG will use scaling in the JPEG decompressor to generate the
713 * largest possible image that will fit within the desired width. If
714 * <tt>width</tt> is set to 0, then only the height will be considered
715 * when determining the scaled image size.
716 * @param pitch bytes per line of the destination image. Normally, this is
717 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed
718 * image is unpadded, else <tt>#TJPAD(scaledWidth *
719 * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed
720 * image is padded to the nearest 32-bit boundary, as is the case for
721 * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by
722 * calling #TJSCALED() with the JPEG image width and one of the scaling
723 * factors returned by #tjGetScalingFactors().) You can also be clever
724 * and use the pitch parameter to skip lines, etc. Setting this
725 * parameter to 0 is the equivalent of setting it to <tt>scaledWidth
726 * * #tjPixelSize[pixelFormat]</tt>.
727 * @param height desired height (in pixels) of the destination image. If this
728 * is different than the height of the JPEG image being decompressed,
729 * then TurboJPEG will use scaling in the JPEG decompressor to generate
730 * the largest possible image that will fit within the desired height.
731 * If <tt>height</tt> is set to 0, then only the width will be
732 * considered when determining the scaled image size.
733 * @param pixelFormat pixel format of the destination image (see @ref
734 * TJPF "Pixel formats".)
735 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
738 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
740 DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
741 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
742 int width, int pitch, int height, int pixelFormat, int flags);
746 * Decompress a JPEG image to a YUV planar image. This function performs JPEG
747 * decompression but leaves out the color conversion step, so a planar YUV
748 * image is generated instead of an RGB image. The padding of the planes in
749 * this image is the same as in the images generated by #tjEncodeYUV2(). Note
750 * that, if the width or height of the image is not an even multiple of the MCU
751 * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer
752 * copy will be performed within TurboJPEG.
754 * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
755 * convention of the digital video community, the TurboJPEG API uses "YUV" to
756 * refer to an image format consisting of Y, Cb, and Cr image planes.
758 * @param handle a handle to a TurboJPEG decompressor or transformer instance
759 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
760 * @param jpegSize size of the JPEG image (in bytes)
761 * @param dstBuf pointer to an image buffer that will receive the YUV image.
762 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
763 * based on the image width, height, and level of subsampling.
764 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
767 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
769 DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
770 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
775 * Create a new TurboJPEG transformer instance.
777 * @return a handle to the newly-created instance, or NULL if an error
778 * occurred (see #tjGetErrorStr().)
780 DLLEXPORT tjhandle DLLCALL tjInitTransform(void);
784 * Losslessly transform a JPEG image into another JPEG image. Lossless
785 * transforms work by moving the raw coefficients from one JPEG image structure
786 * to another without altering the values of the coefficients. While this is
787 * typically faster than decompressing the image, transforming it, and
788 * re-compressing it, lossless transforms are not free. Each lossless
789 * transform requires reading and performing Huffman decoding on all of the
790 * coefficients in the source image, regardless of the size of the destination
791 * image. Thus, this function provides a means of generating multiple
792 * transformed images from the same source or applying multiple
793 * transformations simultaneously, in order to eliminate the need to read the
794 * source coefficients multiple times.
796 * @param handle a handle to a TurboJPEG transformer instance
797 * @param jpegBuf pointer to a buffer containing the JPEG image to transform
798 * @param jpegSize size of the JPEG image (in bytes)
799 * @param n the number of transformed JPEG images to generate
800 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
801 * will receive a JPEG image that has been transformed using the
802 * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to
803 * reallocate the JPEG buffer to accommodate the size of the JPEG image.
804 * Thus, you can choose to:
805 * -# pre-allocate the JPEG buffer with an arbitrary size using
806 * #tjAlloc() and let TurboJPEG grow the buffer as needed,
807 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the
809 * -# pre-allocate the buffer to a "worst case" size determined by
810 * calling #tjBufSize() with the transformed or cropped width and
811 * height. This should ensure that the buffer never has to be
812 * re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
814 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to
815 * the size of your pre-allocated buffer. In any case, unless you have
816 * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt>
817 * upon return from this function, as it may have changed.
818 * @param dstSizes pointer to an array of n unsigned long variables that will
819 * receive the actual sizes (in bytes) of each transformed JPEG image.
820 * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
821 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon
822 * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image
824 * @param transforms pointer to an array of n #tjtransform structures, each of
825 * which specifies the transform parameters and/or cropping region for
826 * the corresponding transformed output image.
827 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
830 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
832 DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
833 unsigned long jpegSize, int n, unsigned char **dstBufs,
834 unsigned long *dstSizes, tjtransform *transforms, int flags);
838 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
840 * @param handle a handle to a TurboJPEG compressor, decompressor or
841 * transformer instance
843 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
845 DLLEXPORT int DLLCALL tjDestroy(tjhandle handle);
849 * Allocate an image buffer for use with TurboJPEG. You should always use
850 * this function to allocate the JPEG destination buffer(s) for #tjCompress2()
851 * and #tjTransform() unless you are disabling automatic buffer
852 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
854 * @param bytes the number of bytes to allocate
856 * @return a pointer to a newly-allocated buffer with the specified number of
861 DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes);
865 * Free an image buffer previously allocated by TurboJPEG. You should always
866 * use this function to free JPEG destination buffer(s) that were automatically
867 * (re)allocated by #tjCompress2() or #tjTransform() or that were manually
868 * allocated using #tjAlloc().
870 * @param buffer address of the buffer to free
874 DLLEXPORT void DLLCALL tjFree(unsigned char *buffer);
878 * Returns a descriptive error message explaining why the last command failed.
880 * @return a descriptive error message explaining why the last command failed.
882 DLLEXPORT char* DLLCALL tjGetErrorStr(void);
885 /* Backward compatibility functions and macros (nothing to see here) */
886 #define NUMSUBOPT TJ_NUMSAMP
887 #define TJ_444 TJSAMP_444
888 #define TJ_422 TJSAMP_422
889 #define TJ_420 TJSAMP_420
890 #define TJ_411 TJSAMP_420
891 #define TJ_GRAYSCALE TJSAMP_GRAY
894 #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
895 #define TJ_FORCEMMX TJFLAG_FORCEMMX
896 #define TJ_FORCESSE TJFLAG_FORCESSE
897 #define TJ_FORCESSE2 TJFLAG_FORCESSE2
898 #define TJ_ALPHAFIRST 64
899 #define TJ_FORCESSE3 TJFLAG_FORCESSE3
900 #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
903 DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
905 DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
908 DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
909 int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
910 unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags);
912 DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle,
913 unsigned char *srcBuf, int width, int pitch, int height, int pixelSize,
914 unsigned char *dstBuf, int subsamp, int flags);
916 DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
917 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height);
919 DLLEXPORT int DLLCALL tjDecompress(tjhandle handle,
920 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
921 int width, int pitch, int height, int pixelSize, int flags);