2 * Copyright (C)2009-2015, 2017, 2020-2023 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__
35 #if defined(_WIN32) && defined(DLLDEFINE)
36 #define DLLEXPORT __declspec(dllexport)
44 * @addtogroup TurboJPEG
45 * TurboJPEG API. This API provides an interface for generating, decoding, and
46 * transforming planar YUV and JPEG images in memory.
49 * YUV Image Format Notes
50 * ----------------------
51 * Technically, the JPEG format uses the YCbCr colorspace (which is technically
52 * not a colorspace but a color transform), but per the convention of the
53 * digital video community, the TurboJPEG API uses "YUV" to refer to an image
54 * format consisting of Y, Cb, and Cr image planes.
56 * Each plane is simply a 2D array of bytes, each byte representing the value
57 * of one of the components (Y, Cb, or Cr) at a particular location in the
58 * image. The width and height of each plane are determined by the image
59 * width, height, and level of chrominance subsampling. The luminance plane
60 * width is the image width padded to the nearest multiple of the horizontal
61 * subsampling factor (1 in the case of 4:4:4, grayscale, 4:4:0, or 4:4:1; 2 in
62 * the case of 4:2:2 or 4:2:0; 4 in the case of 4:1:1.) Similarly, the
63 * luminance plane height is the image height padded to the nearest multiple of
64 * the vertical subsampling factor (1 in the case of 4:4:4, 4:2:2, grayscale,
65 * or 4:1:1; 2 in the case of 4:2:0 or 4:4:0; 4 in the case of 4:4:1.) This is
66 * irrespective of any additional padding that may be specified as an argument
67 * to the various YUV functions. The chrominance plane width is equal to the
68 * luminance plane width divided by the horizontal subsampling factor, and the
69 * chrominance plane height is equal to the luminance plane height divided by
70 * the vertical subsampling factor.
72 * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
73 * used, then the luminance plane would be 36 x 35 bytes, and each of the
74 * chrominance planes would be 18 x 35 bytes. If you specify a row alignment
75 * of 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes,
76 * and each of the chrominance planes would be 20 x 35 bytes.
83 * The number of initialization options
88 * Initialization options.
92 * Initialize the TurboJPEG instance for compression.
96 * Initialize the TurboJPEG instance for decompression.
100 * Initialize the TurboJPEG instance for lossless transformation (both
101 * compression and decompression.)
108 * The number of chrominance subsampling options
113 * Chrominance subsampling options.
114 * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
115 * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
116 * the Cb and Cr (chrominance) components can be discarded or averaged together
117 * to produce a smaller image with little perceptible loss of image clarity.
118 * (The human eye is more sensitive to small changes in brightness than to
119 * small changes in color.) This is called "chrominance subsampling".
123 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
124 * YUV image will contain one chrominance component for every pixel in the
129 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
130 * chrominance component for every 2x1 block of pixels in the source image.
134 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
135 * chrominance component for every 2x2 block of pixels in the source image.
139 * Grayscale. The JPEG or YUV image will contain no chrominance components.
143 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
144 * chrominance component for every 1x2 block of pixels in the source image.
146 * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
150 * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
151 * chrominance component for every 4x1 block of pixels in the source image.
152 * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
153 * same size as those compressed with 4:2:0 subsampling, and in the
154 * aggregate, both subsampling methods produce approximately the same
155 * perceptual quality. However, 4:1:1 is better able to reproduce sharp
156 * horizontal features.
158 * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
162 * 4:4:1 chrominance subsampling. The JPEG or YUV image will contain one
163 * chrominance component for every 1x4 block of pixels in the source image.
164 * JPEG images compressed with 4:4:1 subsampling will be almost exactly the
165 * same size as those compressed with 4:2:0 subsampling, and in the
166 * aggregate, both subsampling methods produce approximately the same
167 * perceptual quality. However, 4:4:1 is better able to reproduce sharp
170 * @note 4:4:1 subsampling is not fully accelerated in libjpeg-turbo.
174 * Unknown subsampling. The JPEG image uses an unusual type of chrominance
175 * subsampling. Such images can be decompressed into packed-pixel images,
177 * - decompressed into planar YUV images,
178 * - losslessly transformed if #TJXOPT_CROP is specified, or
179 * - partially decompressed using a cropping region.
185 * MCU block width (in pixels) for a given level of chrominance subsampling.
187 * - 8x8 for no subsampling or grayscale
194 static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32, 8 };
197 * MCU block height (in pixels) for a given level of chrominance subsampling.
199 * - 8x8 for no subsampling or grayscale
206 static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8, 32 };
210 * The number of pixel formats
219 * RGB pixel format. The red, green, and blue components in the image are
220 * stored in 3-sample pixels in the order R, G, B from lowest to highest
221 * memory address within each pixel.
225 * BGR pixel format. The red, green, and blue components in the image are
226 * stored in 3-sample pixels in the order B, G, R from lowest to highest
227 * memory address within each pixel.
231 * RGBX pixel format. The red, green, and blue components in the image are
232 * stored in 4-sample pixels in the order R, G, B from lowest to highest
233 * memory address within each pixel. The X component is ignored when
234 * compressing and undefined when decompressing.
238 * BGRX pixel format. The red, green, and blue components in the image are
239 * stored in 4-sample pixels in the order B, G, R from lowest to highest
240 * memory address within each pixel. The X component is ignored when
241 * compressing and undefined when decompressing.
245 * XBGR pixel format. The red, green, and blue components in the image are
246 * stored in 4-sample pixels in the order R, G, B from highest to lowest
247 * memory address within each pixel. The X component is ignored when
248 * compressing and undefined when decompressing.
252 * XRGB pixel format. The red, green, and blue components in the image are
253 * stored in 4-sample pixels in the order B, G, R from highest to lowest
254 * memory address within each pixel. The X component is ignored when
255 * compressing and undefined when decompressing.
259 * Grayscale pixel format. Each 1-sample pixel represents a luminance
260 * (brightness) level from 0 to the maximum sample value (255 for 8-bit
261 * samples, 4095 for 12-bit samples, and 65535 for 16-bit samples.)
265 * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
266 * decompressing, the X component is guaranteed to be equal to the maximum
267 * sample value, which can be interpreted as an opaque alpha channel.
271 * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
272 * decompressing, the X component is guaranteed to be equal to the maximum
273 * sample value, which can be interpreted as an opaque alpha channel.
277 * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
278 * decompressing, the X component is guaranteed to be equal to the maximum
279 * sample value, which can be interpreted as an opaque alpha channel.
283 * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
284 * decompressing, the X component is guaranteed to be equal to the maximum
285 * sample value, which can be interpreted as an opaque alpha channel.
289 * CMYK pixel format. Unlike RGB, which is an additive color model used
290 * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
291 * color model used primarily for printing. In the CMYK color model, the
292 * value of each color component typically corresponds to an amount of cyan,
293 * magenta, yellow, or black ink that is applied to a white background. In
294 * order to convert between CMYK and RGB, it is necessary to use a color
295 * management system (CMS.) A CMS will attempt to map colors within the
296 * printer's gamut to perceptually similar colors in the display's gamut and
297 * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
298 * be defined with a simple formula. Thus, such a conversion is out of scope
299 * for a codec library. However, the TurboJPEG API allows for compressing
300 * packed-pixel CMYK images into YCCK JPEG images (see #TJCS_YCCK) and
301 * decompressing YCCK JPEG images into packed-pixel CMYK images.
305 * Unknown pixel format. Currently this is only used by #tj3LoadImage8(),
306 * #tj3LoadImage12(), and #tj3LoadImage16().
312 * Red offset (in samples) for a given pixel format. This specifies the number
313 * of samples that the red component is offset from the start of the pixel.
314 * For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is stored
315 * in `unsigned char pixel[]`, then the red component will be
316 * `pixel[tjRedOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
317 * not have a red component.
319 static const int tjRedOffset[TJ_NUMPF] = {
320 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
323 * Green offset (in samples) for a given pixel format. This specifies the
324 * number of samples that the green component is offset from the start of the
325 * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is
326 * stored in `unsigned char pixel[]`, then the green component will be
327 * `pixel[tjGreenOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
328 * not have a green component.
330 static const int tjGreenOffset[TJ_NUMPF] = {
331 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
334 * Blue offset (in samples) for a given pixel format. This specifies the
335 * number of samples that the blue component is offset from the start of the
336 * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is
337 * stored in `unsigned char pixel[]`, then the blue component will be
338 * `pixel[tjBlueOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
339 * not have a blue component.
341 static const int tjBlueOffset[TJ_NUMPF] = {
342 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
345 * Alpha offset (in samples) for a given pixel format. This specifies the
346 * number of samples that the alpha component is offset from the start of the
347 * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRA is
348 * stored in `unsigned char pixel[]`, then the alpha component will be
349 * `pixel[tjAlphaOffset[TJPF_BGRA]]`. This will be -1 if the pixel format does
350 * not have an alpha component.
352 static const int tjAlphaOffset[TJ_NUMPF] = {
353 -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
356 * Pixel size (in samples) for a given pixel format
358 static const int tjPixelSize[TJ_NUMPF] = {
359 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
364 * The number of JPEG colorspaces
373 * RGB colorspace. When compressing the JPEG image, the R, G, and B
374 * components in the source image are reordered into image planes, but no
375 * colorspace conversion or subsampling is performed. RGB JPEG images can be
376 * compressed from and decompressed to packed-pixel images with any of the
377 * extended RGB or grayscale pixel formats, but they cannot be compressed
378 * from or decompressed to planar YUV images.
382 * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
383 * mathematical transformation of RGB designed solely for storage and
384 * transmission. YCbCr images must be converted to RGB before they can
385 * actually be displayed. In the YCbCr colorspace, the Y (luminance)
386 * component represents the black & white portion of the original image, and
387 * the Cb and Cr (chrominance) components represent the color portion of the
388 * original image. Originally, the analog equivalent of this transformation
389 * allowed the same signal to drive both black & white and color televisions,
390 * but JPEG images use YCbCr primarily because it allows the color data to be
391 * optionally subsampled for the purposes of reducing network or disk usage.
392 * YCbCr is the most common JPEG colorspace, and YCbCr JPEG images can be
393 * compressed from and decompressed to packed-pixel images with any of the
394 * extended RGB or grayscale pixel formats. YCbCr JPEG images can also be
395 * compressed from and decompressed to planar YUV images.
399 * Grayscale colorspace. The JPEG image retains only the luminance data (Y
400 * component), and any color data from the source image is discarded.
401 * Grayscale JPEG images can be compressed from and decompressed to
402 * packed-pixel images with any of the extended RGB or grayscale pixel
403 * formats, or they can be compressed from and decompressed to planar YUV
408 * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
409 * components in the source image are reordered into image planes, but no
410 * colorspace conversion or subsampling is performed. CMYK JPEG images can
411 * only be compressed from and decompressed to packed-pixel images with the
416 * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
417 * rather a mathematical transformation of CMYK designed solely for storage
418 * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
419 * reversibly transformed into YCCK, and as with YCbCr, the chrominance
420 * components in the YCCK pixels can be subsampled without incurring major
421 * perceptual loss. YCCK JPEG images can only be compressed from and
422 * decompressed to packed-pixel images with the CMYK pixel format.
429 * The number of parameters
437 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
438 TJPARAM_MAXPIXELS = -1,
441 * Error handling behavior
444 * - `0` *[default]* Allow the current compression/decompression/transform
445 * operation to complete unless a fatal error is encountered.
446 * - `1` Immediately discontinue the current
447 * compression/decompression/transform operation if a warning (non-fatal
450 TJPARAM_STOPONWARNING,
452 * Row order in packed-pixel source/destination images
455 * - `0` *[default]* top-down (X11) order
456 * - `1` bottom-up (Windows, OpenGL) order
460 * JPEG destination buffer (re)allocation [compression, lossless
464 * - `0` *[default]* Attempt to allocate or reallocate the JPEG destination
466 * - `1` Generate an error if the JPEG destination buffer is invalid or too
471 * Perceptual quality of lossy JPEG images [compression only]
474 * - `1`-`100` (`1` = worst quality but best compression, `100` = best
475 * quality but worst compression) *[no default; must be explicitly
480 * Chrominance subsampling level
482 * The JPEG or YUV image uses (decompression, decoding) or will use (lossy
483 * compression, encoding) the specified level of chrominance subsampling.
486 * - One of the @ref TJSAMP "chrominance subsampling options" *[no default;
487 * must be explicitly specified for lossy compression, encoding, and
492 * JPEG width (in pixels) [decompression only, read-only]
496 * JPEG height (in pixels) [decompression only, read-only]
500 * JPEG data precision (bits per sample) [decompression only, read-only]
502 * The JPEG image uses the specified number of bits per sample.
505 * - `8`, `12`, or `16`
507 * 12-bit data precision implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC
514 * The JPEG image uses (decompression) or will use (lossy compression) the
515 * specified colorspace.
518 * - One of the @ref TJCS "JPEG colorspaces" *[default for lossy compression:
519 * automatically selected based on the subsampling level and pixel format]*
523 * Chrominance upsampling algorithm [lossy decompression only]
526 * - `0` *[default]* Use smooth upsampling when decompressing a JPEG image
527 * that was compressed using chrominance subsampling. This creates a smooth
528 * transition between neighboring chrominance components in order to reduce
529 * upsampling artifacts in the decompressed image.
530 * - `1` Use the fastest chrominance upsampling algorithm available, which
531 * may combine upsampling with color conversion.
533 TJPARAM_FASTUPSAMPLE,
535 * DCT/IDCT algorithm [lossy compression and decompression]
538 * - `0` *[default]* Use the most accurate DCT/IDCT algorithm available.
539 * - `1` Use the fastest DCT/IDCT algorithm available.
541 * This parameter is provided mainly for backward compatibility with libjpeg,
542 * which historically implemented several different DCT/IDCT algorithms
543 * because of performance limitations with 1990s CPUs. In the libjpeg-turbo
544 * implementation of the TurboJPEG API:
545 * - The "fast" and "accurate" DCT/IDCT algorithms perform similarly on
546 * modern x86/x86-64 CPUs that support AVX2 instructions.
547 * - The "fast" algorithm is generally only about 5-15% faster than the
548 * "accurate" algorithm on other types of CPUs.
549 * - The difference in accuracy between the "fast" and "accurate" algorithms
550 * is the most pronounced at JPEG quality levels above 90 and tends to be
551 * more pronounced with decompression than with compression.
552 * - The "fast" algorithm degrades and is not fully accelerated for JPEG
553 * quality levels above 97, so it will be slower than the "accurate"
558 * Optimized baseline entropy coding [lossy compression only]
561 * - `0` *[default]* The JPEG image will use the default Huffman tables.
562 * - `1` Optimal Huffman tables will be computed for the JPEG image. For
563 * lossless transformation, this can also be specified using
566 * Optimized baseline entropy coding will improve compression slightly
567 * (generally 5% or less), but it will reduce compression performance
572 * Progressive entropy coding
575 * - `0` *[default for compression, lossless transformation]* The lossy JPEG
576 * image uses (decompression) or will use (compression, lossless
577 * transformation) baseline entropy coding.
578 * - `1` The lossy JPEG image uses (decompression) or will use (compression,
579 * lossless transformation) progressive entropy coding. For lossless
580 * transformation, this can also be specified using #TJXOPT_PROGRESSIVE.
582 * Progressive entropy coding will generally improve compression relative to
583 * baseline entropy coding, but it will reduce compression and decompression
584 * performance considerably. Can be combined with #TJPARAM_ARITHMETIC.
585 * Implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC is also set.
589 * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless
592 * Setting this parameter will cause the decompression and transform
593 * functions to return an error if the number of scans in a progressive JPEG
594 * image exceeds the specified limit. The primary purpose of this is to
595 * allow security-critical applications to guard against an exploit of the
596 * progressive JPEG format described in
597 * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
600 * - maximum number of progressive JPEG scans that the decompression and
601 * transform functions will process *[default: `0` (no limit)]*
603 * @see #TJPARAM_PROGRESSIVE
607 * Arithmetic entropy coding
610 * - `0` *[default for compression, lossless transformation]* The lossy JPEG
611 * image uses (decompression) or will use (compression, lossless
612 * transformation) Huffman entropy coding.
613 * - `1` The lossy JPEG image uses (decompression) or will use (compression,
614 * lossless transformation) arithmetic entropy coding. For lossless
615 * transformation, this can also be specified using #TJXOPT_ARITHMETIC.
617 * Arithmetic entropy coding will generally improve compression relative to
618 * Huffman entropy coding, but it will reduce compression and decompression
619 * performance considerably. Can be combined with #TJPARAM_PROGRESSIVE.
626 * - `0` *[default for compression]* The JPEG image is (decompression) or
627 * will be (compression) lossy/DCT-based.
628 * - `1` The JPEG image is (decompression) or will be (compression)
629 * lossless/predictive.
631 * In most cases, compressing and decompressing lossless JPEG images is
632 * considerably slower than compressing and decompressing lossy JPEG images.
633 * Also note that the following features are not available with lossless JPEG
635 * - Colorspace conversion (lossless JPEG images always use #TJCS_RGB,
636 * #TJCS_GRAY, or #TJCS_CMYK, depending on the pixel format of the source
638 * - Chrominance subsampling (lossless JPEG images always use #TJSAMP_444)
639 * - JPEG quality selection
640 * - DCT/IDCT algorithm selection
641 * - Progressive entropy coding
642 * - Arithmetic entropy coding
643 * - Compression from/decompression to planar YUV images
644 * - Decompression scaling
645 * - Lossless transformation
647 * @see #TJPARAM_LOSSLESSPSV, #TJPARAM_LOSSLESSPT
651 * Lossless JPEG predictor selection value (PSV)
654 * - `1`-`7` *[default for compression: `1`]*
656 * @see #TJPARAM_LOSSLESS
660 * Lossless JPEG point transform (Pt)
663 * - `0` through ***precision*** *- 1*, where ***precision*** is the JPEG
664 * data precision in bits *[default for compression: `0`]*
666 * A point transform value of `0` is necessary in order to generate a fully
667 * lossless JPEG image. (A non-zero point transform value right-shifts the
668 * input samples by the specified number of bits, which is effectively a form
669 * of lossy color quantization.)
671 * @see #TJPARAM_LOSSLESS, #TJPARAM_PRECISION
675 * JPEG restart marker interval in MCU blocks (lossy) or samples (lossless)
678 * The nature of entropy coding is such that a corrupt JPEG image cannot
679 * be decompressed beyond the point of corruption unless it contains restart
680 * markers. A restart marker stops and restarts the entropy coding algorithm
681 * so that, if a JPEG image is corrupted, decompression can resume at the
682 * next marker. Thus, adding more restart markers improves the fault
683 * tolerance of the JPEG image, but adding too many restart markers can
684 * adversely affect the compression ratio and performance.
687 * - the number of MCU blocks or samples between each restart marker
688 * *[default: `0` (no restart markers)]*
690 * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTROWS to 0.
692 TJPARAM_RESTARTBLOCKS,
694 * JPEG restart marker interval in MCU rows (lossy) or sample rows (lossless)
697 * See #TJPARAM_RESTARTBLOCKS for a description of restart markers.
700 * - the number of MCU rows or sample rows between each restart marker
701 * *[default: `0` (no restart markers)]*
703 * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTBLOCKS to
708 * JPEG horizontal pixel density
711 * - The JPEG image has (decompression) or will have (compression) the
712 * specified horizontal pixel density *[default for compression: `1`]*.
714 * This value is stored in or read from the JPEG header. It does not affect
715 * the contents of the JPEG image. Note that this parameter is set by
716 * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
717 * density information, and the value of this parameter is stored to a
718 * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNIT
721 * @see TJPARAM_DENSITYUNIT
725 * JPEG vertical pixel density
728 * - The JPEG image has (decompression) or will have (compression) the
729 * specified vertical pixel density *[default for compression: `1`]*.
731 * This value is stored in or read from the JPEG header. It does not affect
732 * the contents of the JPEG image. Note that this parameter is set by
733 * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
734 * density information, and the value of this parameter is stored to a
735 * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNIT
738 * @see TJPARAM_DENSITYUNIT
742 * JPEG pixel density units
745 * - `0` *[default for compression]* The pixel density of the JPEG image is
746 * expressed (decompression) or will be expressed (compression) in unknown
748 * - `1` The pixel density of the JPEG image is expressed (decompression) or
749 * will be expressed (compression) in units of pixels/inch.
750 * - `2` The pixel density of the JPEG image is expressed (decompression) or
751 * will be expressed (compression) in units of pixels/cm.
753 * This value is stored in or read from the JPEG header. It does not affect
754 * the contents of the JPEG image. Note that this parameter is set by
755 * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
756 * density information, and the value of this parameter is stored to a
757 * Windows BMP file by #tj3SaveImage8() if the value is `2`.
759 * @see TJPARAM_XDENSITY, TJPARAM_YDENSITY
766 * The number of error codes
775 * The error was non-fatal and recoverable, but the destination image may
780 * The error was fatal and non-recoverable.
787 * The number of transform operations
792 * Transform operations for #tj3Transform()
796 * Do not transform the position of the image pixels
800 * Flip (mirror) image horizontally. This transform is imperfect if there
801 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
805 * Flip (mirror) image vertically. This transform is imperfect if there are
806 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
810 * Transpose image (flip/mirror along upper left to lower right axis.) This
811 * transform is always perfect.
815 * Transverse transpose image (flip/mirror along upper right to lower left
816 * axis.) This transform is imperfect if there are any partial MCU blocks in
817 * the image (see #TJXOPT_PERFECT.)
821 * Rotate image clockwise by 90 degrees. This transform is imperfect if
822 * there are any partial MCU blocks on the bottom edge (see
827 * Rotate image 180 degrees. This transform is imperfect if there are any
828 * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
832 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
833 * if there are any partial MCU blocks on the right edge (see
841 * This option will cause #tj3Transform() to return an error if the transform
842 * is not perfect. Lossless transforms operate on MCU blocks, whose size
843 * depends on the level of chrominance subsampling used (see #tjMCUWidth and
844 * #tjMCUHeight.) If the image's width or height is not evenly divisible by
845 * the MCU block size, then there will be partial MCU blocks on the right
846 * and/or bottom edges. It is not possible to move these partial MCU blocks to
847 * the top or left of the image, so any transform that would require that is
848 * "imperfect." If this option is not specified, then any partial MCU blocks
849 * that cannot be transformed will be left in place, which will create
850 * odd-looking strips on the right or bottom edge of the image.
852 #define TJXOPT_PERFECT (1 << 0)
854 * This option will cause #tj3Transform() to discard any partial MCU blocks
855 * that cannot be transformed.
857 #define TJXOPT_TRIM (1 << 1)
859 * This option will enable lossless cropping. See #tj3Transform() for more
862 #define TJXOPT_CROP (1 << 2)
864 * This option will discard the color data in the source image and produce a
865 * grayscale destination image.
867 #define TJXOPT_GRAY (1 << 3)
869 * This option will prevent #tj3Transform() from outputting a JPEG image for
870 * this particular transform. (This can be used in conjunction with a custom
871 * filter to capture the transformed DCT coefficients without transcoding
874 #define TJXOPT_NOOUTPUT (1 << 4)
876 * This option will enable progressive entropy coding in the JPEG image
877 * generated by this particular transform. Progressive entropy coding will
878 * generally improve compression relative to baseline entropy coding (the
879 * default), but it will reduce decompression performance considerably.
880 * Can be combined with #TJXOPT_ARITHMETIC. Implies #TJXOPT_OPTIMIZE unless
881 * #TJXOPT_ARITHMETIC is also specified.
883 #define TJXOPT_PROGRESSIVE (1 << 5)
885 * This option will prevent #tj3Transform() from copying any extra markers
886 * (including EXIF and ICC profile data) from the source image to the
889 #define TJXOPT_COPYNONE (1 << 6)
891 * This option will enable arithmetic entropy coding in the JPEG image
892 * generated by this particular transform. Arithmetic entropy coding will
893 * generally improve compression relative to Huffman entropy coding (the
894 * default), but it will reduce decompression performance considerably. Can be
895 * combined with #TJXOPT_PROGRESSIVE.
897 #define TJXOPT_ARITHMETIC (1 << 7)
899 * This option will enable optimized baseline entropy coding in the JPEG image
900 * generated by this particular transform. Optimized baseline entropy coding
901 * will improve compression slightly (generally 5% or less.)
903 #define TJXOPT_OPTIMIZE (1 << 8)
925 * The left boundary of the cropping region. This must be evenly divisible
926 * by the MCU block width (see #tjMCUWidth.)
930 * The upper boundary of the cropping region. For lossless transformation,
931 * this must be evenly divisible by the MCU block height (see #tjMCUHeight.)
935 * The width of the cropping region. Setting this to 0 is the equivalent of
936 * setting it to the width of the source JPEG image - x.
940 * The height of the cropping region. Setting this to 0 is the equivalent of
941 * setting it to the height of the source JPEG image - y.
947 * A #tjregion structure that specifies no cropping
949 static const tjregion TJUNCROPPED = { 0, 0, 0, 0 };
954 typedef struct tjtransform {
960 * One of the @ref TJXOP "transform operations"
964 * The bitwise OR of one of more of the @ref TJXOPT_ARITHMETIC
965 * "transform options"
969 * Arbitrary data that can be accessed within the body of the callback
974 * A callback function that can be used to modify the DCT coefficients after
975 * they are losslessly transformed but before they are transcoded to a new
976 * JPEG image. This allows for custom filters or other transformations to be
977 * applied in the frequency domain.
979 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
980 * this pointer is not guaranteed to be valid once the callback returns, so
981 * applications wishing to hand off the DCT coefficients to another function
982 * or library should make a copy of them within the body of the callback.)
984 * @param arrayRegion #tjregion structure containing the width and height of
985 * the array pointed to by `coeffs` as well as its offset relative to the
986 * component plane. TurboJPEG implementations may choose to split each
987 * component plane into multiple DCT coefficient arrays and call the callback
988 * function once for each array.
990 * @param planeRegion #tjregion structure containing the width and height of
991 * the component plane to which `coeffs` belongs
993 * @param componentID ID number of the component plane to which `coeffs`
994 * belongs. (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
995 * typical JPEG images.)
997 * @param transformID ID number of the transformed image to which `coeffs`
998 * belongs. This is the same as the index of the transform in the
999 * `transforms` array that was passed to #tj3Transform().
1001 * @param transform a pointer to a #tjtransform structure that specifies the
1002 * parameters and/or cropping region for this transform
1004 * @return 0 if the callback was successful, or -1 if an error occurred.
1006 int (*customFilter) (short *coeffs, tjregion arrayRegion,
1007 tjregion planeRegion, int componentID, int transformID,
1008 struct tjtransform *transform);
1012 * TurboJPEG instance handle
1014 typedef void *tjhandle;
1018 * Compute the scaled value of `dimension` using the given scaling factor.
1019 * This macro performs the integer equivalent of `ceil(dimension *
1022 #define TJSCALED(dimension, scalingFactor) \
1023 (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
1024 scalingFactor.denom)
1027 * A #tjscalingfactor structure that specifies a scaling factor of 1/1 (no
1030 static const tjscalingfactor TJUNSCALED = { 1, 1 };
1039 * Create a new TurboJPEG instance.
1041 * @param initType one of the @ref TJINIT "initialization options"
1043 * @return a handle to the newly-created instance, or NULL if an error occurred
1044 * (see #tj3GetErrorStr().)
1046 DLLEXPORT tjhandle tj3Init(int initType);
1050 * Set the value of a parameter.
1052 * @param handle handle to a TurboJPEG instance
1054 * @param param one of the @ref TJPARAM "parameters"
1056 * @param value value of the parameter (refer to @ref TJPARAM
1057 * "parameter documentation")
1059 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1061 DLLEXPORT int tj3Set(tjhandle handle, int param, int value);
1065 * Get the value of a parameter.
1067 * @param handle handle to a TurboJPEG instance
1069 * @param param one of the @ref TJPARAM "parameters"
1071 * @return the value of the specified parameter, or -1 if the value is unknown.
1073 DLLEXPORT int tj3Get(tjhandle handle, int param);
1077 * Compress an 8-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1078 * an 8-bit-per-sample JPEG image.
1080 * @param handle handle to a TurboJPEG instance that has been initialized for
1083 * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1084 * or CMYK source image to be compressed. This buffer should normally be
1085 * `pitch * height` samples in size. However, you can also use this parameter
1086 * to compress from a specific region of a larger buffer.
1088 * @param width width (in pixels) of the source image
1090 * @param pitch samples per row in the source image. Normally this should be
1091 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1092 * (Setting this parameter to 0 is the equivalent of setting it to
1093 * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
1094 * parameter to specify the row alignment/padding of the source image, to skip
1095 * rows, or to compress from a specific region of a larger buffer.
1097 * @param height height (in pixels) of the source image
1099 * @param pixelFormat pixel format of the source image (see @ref TJPF
1102 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1103 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
1104 * accommodate the size of the JPEG image. Thus, you can choose to:
1105 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1106 * let TurboJPEG grow the buffer as needed,
1107 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1109 * -# pre-allocate the buffer to a "worst case" size determined by calling
1110 * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
1111 * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1113 * If you choose option 1, then `*jpegSize` should be set to the size of your
1114 * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
1115 * you should always check `*jpegBuf` upon return from this function, as it may
1118 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1119 * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1120 * should be set to the size of the buffer. Upon return, `*jpegSize` will
1121 * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
1122 * JPEG buffer that is being reused from a previous call to one of the JPEG
1123 * compression functions, then `*jpegSize` is ignored.
1125 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1126 * and #tj3GetErrorCode().)
1128 DLLEXPORT int tj3Compress8(tjhandle handle, const unsigned char *srcBuf,
1129 int width, int pitch, int height, int pixelFormat,
1130 unsigned char **jpegBuf, size_t *jpegSize);
1133 * Compress a 12-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1134 * a 12-bit-per-sample JPEG image.
1136 * \details \copydetails tj3Compress8()
1138 DLLEXPORT int tj3Compress12(tjhandle handle, const short *srcBuf, int width,
1139 int pitch, int height, int pixelFormat,
1140 unsigned char **jpegBuf, size_t *jpegSize);
1143 * Compress a 16-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1144 * a 16-bit-per-sample lossless JPEG image.
1146 * \details \copydetails tj3Compress8()
1148 DLLEXPORT int tj3Compress16(tjhandle handle, const unsigned short *srcBuf,
1149 int width, int pitch, int height, int pixelFormat,
1150 unsigned char **jpegBuf, size_t *jpegSize);
1154 * Compress an 8-bit-per-sample unified planar YUV image into an
1155 * 8-bit-per-sample JPEG image.
1157 * @param handle handle to a TurboJPEG instance that has been initialized for
1160 * @param srcBuf pointer to a buffer containing a unified planar YUV source
1161 * image to be compressed. The size of this buffer should match the value
1162 * returned by #tj3YUVBufSize() for the given image width, height, row
1163 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
1164 * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1165 * buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
1167 * @param width width (in pixels) of the source image. If the width is not an
1168 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
1169 * buffer copy will be performed.
1171 * @param align row alignment (in bytes) of the source image (must be a power
1172 * of 2.) Setting this parameter to n indicates that each row in each plane of
1173 * the source image is padded to the nearest multiple of n bytes
1176 * @param height height (in pixels) of the source image. If the height is not
1177 * an even multiple of the MCU block height (see #tjMCUHeight), then an
1178 * intermediate buffer copy will be performed.
1180 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1181 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
1182 * accommodate the size of the JPEG image. Thus, you can choose to:
1183 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1184 * let TurboJPEG grow the buffer as needed,
1185 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1187 * -# pre-allocate the buffer to a "worst case" size determined by calling
1188 * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
1189 * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1191 * If you choose option 1, then `*jpegSize` should be set to the size of your
1192 * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
1193 * you should always check `*jpegBuf` upon return from this function, as it may
1196 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1197 * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1198 * should be set to the size of the buffer. Upon return, `*jpegSize` will
1199 * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
1200 * JPEG buffer that is being reused from a previous call to one of the JPEG
1201 * compression functions, then `*jpegSize` is ignored.
1203 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1204 * and #tj3GetErrorCode().)
1206 DLLEXPORT int tj3CompressFromYUV8(tjhandle handle,
1207 const unsigned char *srcBuf, int width,
1208 int align, int height,
1209 unsigned char **jpegBuf, size_t *jpegSize);
1213 * Compress a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into
1214 * an 8-bit-per-sample JPEG image.
1216 * @param handle handle to a TurboJPEG instance that has been initialized for
1219 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1220 * (or just a Y plane, if compressing a grayscale image) that contain a YUV
1221 * source image to be compressed. These planes can be contiguous or
1222 * non-contiguous in memory. The size of each plane should match the value
1223 * returned by #tj3YUVPlaneSize() for the given image width, height, strides,
1224 * and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) Refer to
1225 * @ref YUVnotes "YUV Image Format Notes" for more details.
1227 * @param width width (in pixels) of the source image. If the width is not an
1228 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
1229 * buffer copy will be performed.
1231 * @param strides an array of integers, each specifying the number of bytes per
1232 * row in the corresponding plane of the YUV source image. Setting the stride
1233 * for any plane to 0 is the same as setting it to the plane width (see
1234 * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
1235 * strides for all planes will be set to their respective plane widths. You
1236 * can adjust the strides in order to specify an arbitrary amount of row
1237 * padding in each plane or to create a JPEG image from a subregion of a larger
1240 * @param height height (in pixels) of the source image. If the height is not
1241 * an even multiple of the MCU block height (see #tjMCUHeight), then an
1242 * intermediate buffer copy will be performed.
1244 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1245 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
1246 * accommodate the size of the JPEG image. Thus, you can choose to:
1247 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1248 * let TurboJPEG grow the buffer as needed,
1249 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1251 * -# pre-allocate the buffer to a "worst case" size determined by calling
1252 * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
1253 * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1255 * If you choose option 1, then `*jpegSize` should be set to the size of your
1256 * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
1257 * you should always check `*jpegBuf` upon return from this function, as it may
1260 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1261 * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1262 * should be set to the size of the buffer. Upon return, `*jpegSize` will
1263 * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
1264 * JPEG buffer that is being reused from a previous call to one of the JPEG
1265 * compression functions, then `*jpegSize` is ignored.
1267 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1268 * and #tj3GetErrorCode().)
1270 DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle,
1271 const unsigned char * const *srcPlanes,
1272 int width, const int *strides,
1273 int height, unsigned char **jpegBuf,
1278 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
1279 * the given parameters. The number of bytes returned by this function is
1280 * larger than the size of the uncompressed source image. The reason for this
1281 * is that the JPEG format uses 16-bit coefficients, so it is possible for a
1282 * very high-quality source image with very high-frequency content to expand
1283 * rather than compress when converted to the JPEG format. Such images
1284 * represent very rare corner cases, but since there is no way to predict the
1285 * size of a JPEG image prior to compression, the corner cases have to be
1288 * @param width width (in pixels) of the image
1290 * @param height height (in pixels) of the image
1292 * @param jpegSubsamp the level of chrominance subsampling to be used when
1293 * generating the JPEG image (see @ref TJSAMP
1294 * "Chrominance subsampling options".) #TJSAMP_UNKNOWN is treated like
1295 * #TJSAMP_444, since a buffer large enough to hold a JPEG image with no
1296 * subsampling should also be large enough to hold a JPEG image with an
1297 * arbitrary level of subsampling. Note that lossless JPEG images always
1300 * @return the maximum size of the buffer (in bytes) required to hold the
1301 * image, or 0 if the arguments are out of bounds.
1303 DLLEXPORT size_t tj3JPEGBufSize(int width, int height, int jpegSubsamp);
1307 * The size of the buffer (in bytes) required to hold a unified planar YUV
1308 * image with the given parameters.
1310 * @param width width (in pixels) of the image
1312 * @param align row alignment (in bytes) of the image (must be a power of 2.)
1313 * Setting this parameter to n specifies that each row in each plane of the
1314 * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
1316 * @param height height (in pixels) of the image
1318 * @param subsamp level of chrominance subsampling in the image (see
1319 * @ref TJSAMP "Chrominance subsampling options".)
1321 * @return the size of the buffer (in bytes) required to hold the image, or 0
1322 * if the arguments are out of bounds.
1324 DLLEXPORT size_t tj3YUVBufSize(int width, int align, int height, int subsamp);
1328 * The size of the buffer (in bytes) required to hold a YUV image plane with
1329 * the given parameters.
1331 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1333 * @param width width (in pixels) of the YUV image. NOTE: this is the width of
1334 * the whole image, not the plane width.
1336 * @param stride bytes per row in the image plane. Setting this to 0 is the
1337 * equivalent of setting it to the plane width.
1339 * @param height height (in pixels) of the YUV image. NOTE: this is the height
1340 * of the whole image, not the plane height.
1342 * @param subsamp level of chrominance subsampling in the image (see
1343 * @ref TJSAMP "Chrominance subsampling options".)
1345 * @return the size of the buffer (in bytes) required to hold the YUV image
1346 * plane, or 0 if the arguments are out of bounds.
1348 DLLEXPORT size_t tj3YUVPlaneSize(int componentID, int width, int stride,
1349 int height, int subsamp);
1353 * The plane width of a YUV image plane with the given parameters. Refer to
1354 * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
1356 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1358 * @param width width (in pixels) of the YUV image
1360 * @param subsamp level of chrominance subsampling in the image (see
1361 * @ref TJSAMP "Chrominance subsampling options".)
1363 * @return the plane width of a YUV image plane with the given parameters, or 0
1364 * if the arguments are out of bounds.
1366 DLLEXPORT int tj3YUVPlaneWidth(int componentID, int width, int subsamp);
1370 * The plane height of a YUV image plane with the given parameters. Refer to
1371 * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
1373 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1375 * @param height height (in pixels) of the YUV image
1377 * @param subsamp level of chrominance subsampling in the image (see
1378 * @ref TJSAMP "Chrominance subsampling options".)
1380 * @return the plane height of a YUV image plane with the given parameters, or
1381 * 0 if the arguments are out of bounds.
1383 DLLEXPORT int tj3YUVPlaneHeight(int componentID, int height, int subsamp);
1387 * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into an
1388 * 8-bit-per-sample unified planar YUV image. This function performs color
1389 * conversion (which is accelerated in the libjpeg-turbo implementation) but
1390 * does not execute any of the other steps in the JPEG compression process.
1392 * @param handle handle to a TurboJPEG instance that has been initialized for
1395 * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1396 * source image to be encoded. This buffer should normally be `pitch * height`
1397 * bytes in size. However, you can also use this parameter to encode from a
1398 * specific region of a larger buffer.
1400 * @param width width (in pixels) of the source image
1402 * @param pitch bytes per row in the source image. Normally this should be
1403 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1404 * (Setting this parameter to 0 is the equivalent of setting it to
1405 * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
1406 * parameter to specify the row alignment/padding of the source image, to skip
1407 * rows, or to encode from a specific region of a larger packed-pixel image.
1409 * @param height height (in pixels) of the source image
1411 * @param pixelFormat pixel format of the source image (see @ref TJPF
1414 * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1415 * image. Use #tj3YUVBufSize() to determine the appropriate size for this
1416 * buffer based on the image width, height, row alignment, and level of
1417 * chrominance subsampling (see #TJPARAM_SUBSAMP.) The Y, U (Cb), and V (Cr)
1418 * image planes will be stored sequentially in the buffer. (Refer to
1419 * @ref YUVnotes "YUV Image Format Notes".)
1421 * @param align row alignment (in bytes) of the YUV image (must be a power of
1422 * 2.) Setting this parameter to n will cause each row in each plane of the
1423 * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1424 * To generate images suitable for X Video, `align` should be set to 4.
1426 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1427 * and #tj3GetErrorCode().)
1429 DLLEXPORT int tj3EncodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1430 int width, int pitch, int height, int pixelFormat,
1431 unsigned char *dstBuf, int align);
1435 * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into separate
1436 * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes. This function performs
1437 * color conversion (which is accelerated in the libjpeg-turbo implementation)
1438 * but does not execute any of the other steps in the JPEG compression process.
1440 * @param handle handle to a TurboJPEG instance that has been initialized for
1443 * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1444 * source image to be encoded. This buffer should normally be `pitch * height`
1445 * bytes in size. However, you can also use this parameter to encode from a
1446 * specific region of a larger buffer.
1449 * @param width width (in pixels) of the source image
1451 * @param pitch bytes per row in the source image. Normally this should be
1452 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1453 * (Setting this parameter to 0 is the equivalent of setting it to
1454 * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
1455 * parameter to specify the row alignment/padding of the source image, to skip
1456 * rows, or to encode from a specific region of a larger packed-pixel image.
1458 * @param height height (in pixels) of the source image
1460 * @param pixelFormat pixel format of the source image (see @ref TJPF
1463 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1464 * (or just a Y plane, if generating a grayscale image) that will receive the
1465 * encoded image. These planes can be contiguous or non-contiguous in memory.
1466 * Use #tj3YUVPlaneSize() to determine the appropriate size for each plane
1467 * based on the image width, height, strides, and level of chrominance
1468 * subsampling (see #TJPARAM_SUBSAMP.) Refer to @ref YUVnotes
1469 * "YUV Image Format Notes" for more details.
1471 * @param strides an array of integers, each specifying the number of bytes per
1472 * row in the corresponding plane of the YUV image. Setting the stride for any
1473 * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
1474 * "YUV Image Format Notes".) If `strides` is NULL, then the strides for all
1475 * planes will be set to their respective plane widths. You can adjust the
1476 * strides in order to add an arbitrary amount of row padding to each plane or
1477 * to encode an RGB or grayscale image into a subregion of a larger planar YUV
1480 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1481 * and #tj3GetErrorCode().)
1483 DLLEXPORT int tj3EncodeYUVPlanes8(tjhandle handle, const unsigned char *srcBuf,
1484 int width, int pitch, int height,
1485 int pixelFormat, unsigned char **dstPlanes,
1490 * Retrieve information about a JPEG image without decompressing it, or prime
1491 * the decompressor with quantization and Huffman tables. If a JPEG image is
1492 * passed to this function, then the @ref TJPARAM "parameters" that describe
1493 * the JPEG image will be set when the function returns.
1495 * @param handle handle to a TurboJPEG instance that has been initialized for
1498 * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
1499 * "abbreviated table specification" (AKA "tables-only") datastream. Passing a
1500 * tables-only datastream to this function primes the decompressor with
1501 * quantization and Huffman tables that can be used when decompressing
1502 * subsequent "abbreviated image" datastreams. This is useful, for instance,
1503 * when decompressing video streams in which all frames share the same
1504 * quantization and Huffman tables.
1506 * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1508 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1509 * and #tj3GetErrorCode().)
1511 DLLEXPORT int tj3DecompressHeader(tjhandle handle,
1512 const unsigned char *jpegBuf,
1517 * Returns a list of fractional scaling factors that the JPEG decompressor
1520 * @param numScalingFactors pointer to an integer variable that will receive
1521 * the number of elements in the list
1523 * @return a pointer to a list of fractional scaling factors, or NULL if an
1524 * error is encountered (see #tj3GetErrorStr().)
1526 DLLEXPORT tjscalingfactor *tj3GetScalingFactors(int *numScalingFactors);
1530 * Set the scaling factor for subsequent lossy decompression operations.
1532 * @param handle handle to a TurboJPEG instance that has been initialized for
1535 * @param scalingFactor #tjscalingfactor structure that specifies a fractional
1536 * scaling factor that the decompressor supports (see #tj3GetScalingFactors()),
1537 * or <tt>#TJUNSCALED</tt> for no scaling. Decompression scaling is a function
1538 * of the IDCT algorithm, so scaling factors are generally limited to multiples
1539 * of 1/8. If the entire JPEG image will be decompressed, then the width and
1540 * height of the scaled destination image can be determined by calling
1541 * #TJSCALED() with the JPEG width and height (see #TJPARAM_JPEGWIDTH and
1542 * #TJPARAM_JPEGHEIGHT) and the specified scaling factor. When decompressing
1543 * into a planar YUV image, an intermediate buffer copy will be performed if
1544 * the width or height of the scaled destination image is not an even multiple
1545 * of the MCU block size (see #tjMCUWidth and #tjMCUHeight.) Note that
1546 * decompression scaling is not available (and the specified scaling factor is
1547 * ignored) when decompressing lossless JPEG images (see #TJPARAM_LOSSLESS),
1548 * since the IDCT algorithm is not used with those images. Note also that
1549 * #TJPARAM_FASTDCT is ignored when decompression scaling is enabled.
1551 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1553 DLLEXPORT int tj3SetScalingFactor(tjhandle handle,
1554 tjscalingfactor scalingFactor);
1558 * Set the cropping region for partially decompressing a lossy JPEG image into
1559 * a packed-pixel image
1561 * @param handle handle to a TurboJPEG instance that has been initialized for
1564 * @param croppingRegion #tjregion structure that specifies a subregion of the
1565 * JPEG image to decompress, or <tt>#TJUNCROPPED</tt> for no cropping. The
1566 * left boundary of the cropping region must be evenly divisible by the scaled
1567 * MCU block width (<tt>#TJSCALED(#tjMCUWidth[subsamp], scalingFactor)</tt>,
1568 * where `subsamp` is the level of chrominance subsampling in the JPEG image
1569 * (see #TJPARAM_SUBSAMP) and `scalingFactor` is the decompression scaling
1570 * factor (see #tj3SetScalingFactor().) The cropping region should be
1571 * specified relative to the scaled image dimensions. Unless `croppingRegion`
1572 * is <tt>#TJUNCROPPED</tt>, the JPEG header must be read (see
1573 * #tj3DecompressHeader()) prior to calling this function.
1575 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1577 DLLEXPORT int tj3SetCroppingRegion(tjhandle handle, tjregion croppingRegion);
1581 * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample
1582 * packed-pixel RGB, grayscale, or CMYK image. The @ref TJPARAM "parameters"
1583 * that describe the JPEG image will be set when this function returns.
1585 * @param handle handle to a TurboJPEG instance that has been initialized for
1588 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1591 * @param jpegSize size of the JPEG image (in bytes)
1593 * @param dstBuf pointer to a buffer that will receive the packed-pixel
1594 * decompressed image. This buffer should normally be
1595 * `pitch * destinationHeight` samples in size. However, you can also use this
1596 * parameter to decompress into a specific region of a larger buffer. NOTE:
1597 * If the JPEG image is lossy, then `destinationHeight` is either the scaled
1598 * JPEG height (see #TJSCALED(), #TJPARAM_JPEGHEIGHT, and
1599 * #tj3SetScalingFactor()) or the height of the cropping region (see
1600 * #tj3SetCroppingRegion().) If the JPEG image is lossless, then
1601 * `destinationHeight` is the JPEG height.
1603 * @param pitch samples per row in the destination image. Normally this should
1604 * be set to <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>, if the
1605 * destination image should be unpadded. (Setting this parameter to 0 is the
1606 * equivalent of setting it to
1607 * <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>.) However, you can
1608 * also use this parameter to specify the row alignment/padding of the
1609 * destination image, to skip rows, or to decompress into a specific region of
1610 * a larger buffer. NOTE: If the JPEG image is lossy, then `destinationWidth`
1611 * is either the scaled JPEG width (see #TJSCALED(), #TJPARAM_JPEGWIDTH, and
1612 * #tj3SetScalingFactor()) or the width of the cropping region (see
1613 * #tj3SetCroppingRegion().) If the JPEG image is lossless, then
1614 * `destinationWidth` is the JPEG width.
1616 * @param pixelFormat pixel format of the destination image (see @ref
1617 * TJPF "Pixel formats".)
1619 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1620 * and #tj3GetErrorCode().)
1622 DLLEXPORT int tj3Decompress8(tjhandle handle, const unsigned char *jpegBuf,
1623 size_t jpegSize, unsigned char *dstBuf, int pitch,
1627 * Decompress a 12-bit-per-sample JPEG image into a 12-bit-per-sample
1628 * packed-pixel RGB, grayscale, or CMYK image.
1630 * \details \copydetails tj3Decompress8()
1632 DLLEXPORT int tj3Decompress12(tjhandle handle, const unsigned char *jpegBuf,
1633 size_t jpegSize, short *dstBuf, int pitch,
1637 * Decompress a 16-bit-per-sample lossless JPEG image into a 16-bit-per-sample
1638 * packed-pixel RGB, grayscale, or CMYK image.
1640 * \details \copydetails tj3Decompress8()
1642 DLLEXPORT int tj3Decompress16(tjhandle handle, const unsigned char *jpegBuf,
1643 size_t jpegSize, unsigned short *dstBuf,
1644 int pitch, int pixelFormat);
1648 * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample unified
1649 * planar YUV image. This function performs JPEG decompression but leaves out
1650 * the color conversion step, so a planar YUV image is generated instead of a
1651 * packed-pixel image. The @ref TJPARAM "parameters" that describe the JPEG
1652 * image will be set when this function returns.
1654 * @param handle handle to a TurboJPEG instance that has been initialized for
1657 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1660 * @param jpegSize size of the JPEG image (in bytes)
1662 * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1663 * decompressed image. Use #tj3YUVBufSize() to determine the appropriate size
1664 * for this buffer based on the scaled JPEG width and height (see #TJSCALED(),
1665 * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()), row
1666 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
1667 * Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
1668 * buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
1670 * @param align row alignment (in bytes) of the YUV image (must be a power of
1671 * 2.) Setting this parameter to n will cause each row in each plane of the
1672 * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1673 * To generate images suitable for X Video, `align` should be set to 4.
1675 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1676 * and #tj3GetErrorCode().)
1678 DLLEXPORT int tj3DecompressToYUV8(tjhandle handle,
1679 const unsigned char *jpegBuf,
1681 unsigned char *dstBuf, int align);
1685 * Decompress an 8-bit-per-sample JPEG image into separate 8-bit-per-sample Y,
1686 * U (Cb), and V (Cr) image planes. This function performs JPEG decompression
1687 * but leaves out the color conversion step, so a planar YUV image is generated
1688 * instead of a packed-pixel image. The @ref TJPARAM "parameters" that
1689 * describe the JPEG image will be set when this function returns.
1691 * @param handle handle to a TurboJPEG instance that has been initialized for
1694 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1697 * @param jpegSize size of the JPEG image (in bytes)
1699 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1700 * (or just a Y plane, if decompressing a grayscale image) that will receive
1701 * the decompressed image. These planes can be contiguous or non-contiguous in
1702 * memory. Use #tj3YUVPlaneSize() to determine the appropriate size for each
1703 * plane based on the scaled JPEG width and height (see #TJSCALED(),
1704 * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()),
1705 * strides, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) Refer
1706 * to @ref YUVnotes "YUV Image Format Notes" for more details.
1708 * @param strides an array of integers, each specifying the number of bytes per
1709 * row in the corresponding plane of the YUV image. Setting the stride for any
1710 * plane to 0 is the same as setting it to the scaled plane width (see
1711 * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
1712 * strides for all planes will be set to their respective scaled plane widths.
1713 * You can adjust the strides in order to add an arbitrary amount of row
1714 * padding to each plane or to decompress the JPEG image into a subregion of a
1715 * larger planar YUV image.
1717 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1718 * and #tj3GetErrorCode().)
1720 DLLEXPORT int tj3DecompressToYUVPlanes8(tjhandle handle,
1721 const unsigned char *jpegBuf,
1723 unsigned char **dstPlanes,
1728 * Decode an 8-bit-per-sample unified planar YUV image into an 8-bit-per-sample
1729 * packed-pixel RGB or grayscale image. This function performs color
1730 * conversion (which is accelerated in the libjpeg-turbo implementation) but
1731 * does not execute any of the other steps in the JPEG decompression process.
1733 * @param handle handle to a TurboJPEG instance that has been initialized for
1736 * @param srcBuf pointer to a buffer containing a unified planar YUV source
1737 * image to be decoded. The size of this buffer should match the value
1738 * returned by #tj3YUVBufSize() for the given image width, height, row
1739 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
1740 * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1741 * source buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
1743 * @param align row alignment (in bytes) of the YUV source image (must be a
1744 * power of 2.) Setting this parameter to n indicates that each row in each
1745 * plane of the YUV source image is padded to the nearest multiple of n bytes
1748 * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1749 * image. This buffer should normally be `pitch * height` bytes in size.
1750 * However, you can also use this parameter to decode into a specific region of
1753 * @param width width (in pixels) of the source and destination images
1755 * @param pitch bytes per row in the destination image. Normally this should
1756 * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1757 * image should be unpadded. (Setting this parameter to 0 is the equivalent of
1758 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can
1759 * also use this parameter to specify the row alignment/padding of the
1760 * destination image, to skip rows, or to decode into a specific region of a
1763 * @param height height (in pixels) of the source and destination images
1765 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1768 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1769 * and #tj3GetErrorCode().)
1771 DLLEXPORT int tj3DecodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1772 int align, unsigned char *dstBuf, int width,
1773 int pitch, int height, int pixelFormat);
1777 * Decode a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into an
1778 * 8-bit-per-sample packed-pixel RGB or grayscale image. This function
1779 * performs color conversion (which is accelerated in the libjpeg-turbo
1780 * implementation) but does not execute any of the other steps in the JPEG
1781 * decompression process.
1783 * @param handle handle to a TurboJPEG instance that has been initialized for
1786 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1787 * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1788 * to be decoded. These planes can be contiguous or non-contiguous in memory.
1789 * The size of each plane should match the value returned by #tj3YUVPlaneSize()
1790 * for the given image width, height, strides, and level of chrominance
1791 * subsampling (see #TJPARAM_SUBSAMP.) Refer to @ref YUVnotes
1792 * "YUV Image Format Notes" for more details.
1794 * @param strides an array of integers, each specifying the number of bytes per
1795 * row in the corresponding plane of the YUV source image. Setting the stride
1796 * for any plane to 0 is the same as setting it to the plane width (see
1797 * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
1798 * strides for all planes will be set to their respective plane widths. You
1799 * can adjust the strides in order to specify an arbitrary amount of row
1800 * padding in each plane or to decode a subregion of a larger planar YUV image.
1802 * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1803 * image. This buffer should normally be `pitch * height` bytes in size.
1804 * However, you can also use this parameter to decode into a specific region of
1807 * @param width width (in pixels) of the source and destination images
1809 * @param pitch bytes per row in the destination image. Normally this should
1810 * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1811 * image should be unpadded. (Setting this parameter to 0 is the equivalent of
1812 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can
1813 * also use this parameter to specify the row alignment/padding of the
1814 * destination image, to skip rows, or to decode into a specific region of a
1817 * @param height height (in pixels) of the source and destination images
1819 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1822 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1823 * and #tj3GetErrorCode().)
1825 DLLEXPORT int tj3DecodeYUVPlanes8(tjhandle handle,
1826 const unsigned char * const *srcPlanes,
1827 const int *strides, unsigned char *dstBuf,
1828 int width, int pitch, int height,
1833 * Losslessly transform a JPEG image into another JPEG image. Lossless
1834 * transforms work by moving the raw DCT coefficients from one JPEG image
1835 * structure to another without altering the values of the coefficients. While
1836 * this is typically faster than decompressing the image, transforming it, and
1837 * re-compressing it, lossless transforms are not free. Each lossless
1838 * transform requires reading and performing entropy decoding on all of the
1839 * coefficients in the source image, regardless of the size of the destination
1840 * image. Thus, this function provides a means of generating multiple
1841 * transformed images from the same source or applying multiple transformations
1842 * simultaneously, in order to eliminate the need to read the source
1843 * coefficients multiple times.
1845 * @param handle handle to a TurboJPEG instance that has been initialized for
1846 * lossless transformation
1848 * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
1851 * @param jpegSize size of the JPEG source image (in bytes)
1853 * @param n the number of transformed JPEG images to generate
1855 * @param dstBufs pointer to an array of n byte buffers. `dstBufs[i]` will
1856 * receive a JPEG image that has been transformed using the parameters in
1857 * `transforms[i]`. TurboJPEG has the ability to reallocate the JPEG
1858 * destination buffer to accommodate the size of the transformed JPEG image.
1859 * Thus, you can choose to:
1860 * -# pre-allocate the JPEG destination buffer with an arbitrary size using
1861 * #tj3Alloc() and let TurboJPEG grow the buffer as needed,
1862 * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
1864 * -# pre-allocate the buffer to a "worst case" size determined by calling
1865 * #tj3JPEGBufSize() with the transformed or cropped width and height and the
1866 * level of subsampling used in the source image. Under normal circumstances,
1867 * this should ensure that the buffer never has to be re-allocated. (Setting
1868 * #TJPARAM_NOREALLOC guarantees that it won't be.) Note, however, that there
1869 * are some rare cases (such as transforming images with a large amount of
1870 * embedded EXIF or ICC profile data) in which the transformed JPEG image will
1871 * be larger than the worst-case size, and #TJPARAM_NOREALLOC cannot be used in
1874 * If you choose option 1, then `dstSizes[i]` should be set to the size of your
1875 * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
1876 * you should always check `dstBufs[i]` upon return from this function, as it
1879 * @param dstSizes pointer to an array of n size_t variables that will receive
1880 * the actual sizes (in bytes) of each transformed JPEG image. If `dstBufs[i]`
1881 * points to a pre-allocated buffer, then `dstSizes[i]` should be set to the
1882 * size of the buffer. Upon return, `dstSizes[i]` will contain the size of the
1883 * transformed JPEG image (in bytes.)
1885 * @param transforms pointer to an array of n #tjtransform structures, each of
1886 * which specifies the transform parameters and/or cropping region for the
1887 * corresponding transformed JPEG image.
1889 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1890 * and #tj3GetErrorCode().)
1892 DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
1893 size_t jpegSize, int n, unsigned char **dstBufs,
1894 size_t *dstSizes, const tjtransform *transforms);
1898 * Destroy a TurboJPEG instance.
1900 * @param handle handle to a TurboJPEG instance. If the handle is NULL, then
1901 * this function has no effect.
1903 DLLEXPORT void tj3Destroy(tjhandle handle);
1907 * Allocate a byte buffer for use with TurboJPEG. You should always use this
1908 * function to allocate the JPEG destination buffer(s) for the compression and
1909 * transform functions unless you are disabling automatic buffer (re)allocation
1910 * (by setting #TJPARAM_NOREALLOC.)
1912 * @param bytes the number of bytes to allocate
1914 * @return a pointer to a newly-allocated buffer with the specified number of
1919 DLLEXPORT void *tj3Alloc(size_t bytes);
1923 * Load an 8-bit-per-sample packed-pixel image from disk into memory.
1925 * @param handle handle to a TurboJPEG instance
1927 * @param filename name of a file containing a packed-pixel image in Windows
1928 * BMP or PBMPLUS (PPM/PGM) format. Windows BMP files require 8-bit-per-sample
1929 * data precision. If the data precision of the PBMPLUS file does not match
1930 * the target data precision, then upconverting or downconverting will be
1933 * @param width pointer to an integer variable that will receive the width (in
1934 * pixels) of the packed-pixel image
1936 * @param align row alignment (in samples) of the packed-pixel buffer to be
1937 * returned (must be a power of 2.) Setting this parameter to n will cause all
1938 * rows in the buffer to be padded to the nearest multiple of n samples
1941 * @param height pointer to an integer variable that will receive the height
1942 * (in pixels) of the packed-pixel image
1944 * @param pixelFormat pointer to an integer variable that specifies or will
1945 * receive the pixel format of the packed-pixel buffer. The behavior of this
1946 * function will vary depending on the value of `*pixelFormat` passed to the
1948 * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
1949 * use the most optimal pixel format for the file type, and `*pixelFormat` will
1950 * contain the ID of that pixel format upon successful return from this
1952 * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
1953 * grayscale colormap can be loaded.
1954 * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1955 * converted using a quick & dirty algorithm that is suitable only for testing
1956 * purposes. (Proper conversion between CMYK and other formats requires a
1957 * color management system.)
1958 * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
1959 * specified pixel format, and pixel format conversion will be performed if
1962 * @return a pointer to a newly-allocated buffer containing the packed-pixel
1963 * image, converted to the chosen pixel format and with the chosen row
1964 * alignment, or NULL if an error occurred (see #tj3GetErrorStr().) This
1965 * buffer should be freed using #tj3Free().
1967 DLLEXPORT unsigned char *tj3LoadImage8(tjhandle handle, const char *filename,
1968 int *width, int align, int *height,
1972 * Load a 12-bit-per-sample packed-pixel image from disk into memory.
1974 * \details \copydetails tj3LoadImage8()
1976 DLLEXPORT short *tj3LoadImage12(tjhandle handle, const char *filename,
1977 int *width, int align, int *height,
1981 * Load a 16-bit-per-sample packed-pixel image from disk into memory.
1983 * \details \copydetails tj3LoadImage8()
1985 DLLEXPORT unsigned short *tj3LoadImage16(tjhandle handle, const char *filename,
1986 int *width, int align, int *height,
1991 * Save an 8-bit-per-sample packed-pixel image from memory to disk.
1993 * @param handle handle to a TurboJPEG instance
1995 * @param filename name of a file to which to save the packed-pixel image. The
1996 * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
1997 * on the file extension. Windows BMP files require 8-bit-per-sample data
2000 * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2001 * or CMYK image to be saved
2003 * @param width width (in pixels) of the packed-pixel image
2005 * @param pitch samples per row in the packed-pixel image. Setting this
2006 * parameter to 0 is the equivalent of setting it to
2007 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2009 * @param height height (in pixels) of the packed-pixel image
2011 * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2012 * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
2013 * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
2014 * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
2015 * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
2016 * converted to RGB using a quick & dirty algorithm that is suitable only for
2017 * testing purposes. (Proper conversion between CMYK and other formats
2018 * requires a color management system.)
2020 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2022 DLLEXPORT int tj3SaveImage8(tjhandle handle, const char *filename,
2023 const unsigned char *buffer, int width, int pitch,
2024 int height, int pixelFormat);
2027 * Save a 12-bit-per-sample packed-pixel image from memory to disk.
2029 * \details \copydetails tj3SaveImage8()
2031 DLLEXPORT int tj3SaveImage12(tjhandle handle, const char *filename,
2032 const short *buffer, int width, int pitch,
2033 int height, int pixelFormat);
2036 * Save a 16-bit-per-sample packed-pixel image from memory to disk.
2038 * \details \copydetails tj3SaveImage8()
2040 DLLEXPORT int tj3SaveImage16(tjhandle handle, const char *filename,
2041 const unsigned short *buffer, int width,
2042 int pitch, int height, int pixelFormat);
2046 * Free a byte buffer previously allocated by TurboJPEG. You should always use
2047 * this function to free JPEG destination buffer(s) that were automatically
2048 * (re)allocated by the compression and transform functions or that were
2049 * manually allocated using #tj3Alloc().
2051 * @param buffer address of the buffer to free. If the address is NULL, then
2052 * this function has no effect.
2056 DLLEXPORT void tj3Free(void *buffer);
2060 * Returns a descriptive error message explaining why the last command failed.
2062 * @param handle handle to a TurboJPEG instance, or NULL if the error was
2063 * generated by a global function (but note that retrieving the error message
2064 * for a global function is thread-safe only on platforms that support
2065 * thread-local storage.)
2067 * @return a descriptive error message explaining why the last command failed.
2069 DLLEXPORT char *tj3GetErrorStr(tjhandle handle);
2073 * Returns a code indicating the severity of the last error. See
2074 * @ref TJERR "Error codes".
2076 * @param handle handle to a TurboJPEG instance
2078 * @return a code indicating the severity of the last error. See
2079 * @ref TJERR "Error codes".
2081 DLLEXPORT int tj3GetErrorCode(tjhandle handle);
2084 /* Backward compatibility functions and macros (nothing to see here) */
2086 /* TurboJPEG 1.0+ */
2088 #define NUMSUBOPT TJ_NUMSAMP
2089 #define TJ_444 TJSAMP_444
2090 #define TJ_422 TJSAMP_422
2091 #define TJ_420 TJSAMP_420
2092 #define TJ_411 TJSAMP_420
2093 #define TJ_GRAYSCALE TJSAMP_GRAY
2096 #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
2097 #define TJ_FORCEMMX TJFLAG_FORCEMMX
2098 #define TJ_FORCESSE TJFLAG_FORCESSE
2099 #define TJ_FORCESSE2 TJFLAG_FORCESSE2
2100 #define TJ_ALPHAFIRST 64
2101 #define TJ_FORCESSE3 TJFLAG_FORCESSE3
2102 #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
2104 #define TJPAD(width) (((width) + 3) & (~3))
2106 DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
2108 DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
2109 int pitch, int height, int pixelSize,
2110 unsigned char *dstBuf, unsigned long *compressedSize,
2111 int jpegSubsamp, int jpegQual, int flags);
2113 DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
2114 unsigned long jpegSize, unsigned char *dstBuf,
2115 int width, int pitch, int height, int pixelSize,
2118 DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
2119 unsigned long jpegSize, int *width,
2122 DLLEXPORT int tjDestroy(tjhandle handle);
2124 DLLEXPORT char *tjGetErrorStr(void);
2126 DLLEXPORT tjhandle tjInitCompress(void);
2128 DLLEXPORT tjhandle tjInitDecompress(void);
2130 /* TurboJPEG 1.1+ */
2134 DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
2136 DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
2137 unsigned long jpegSize, int *width,
2138 int *height, int *jpegSubsamp);
2140 DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
2141 unsigned long jpegSize, unsigned char *dstBuf,
2144 DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
2145 int pitch, int height, int pixelSize,
2146 unsigned char *dstBuf, int subsamp, int flags);
2148 /* TurboJPEG 1.2+ */
2150 #define TJFLAG_BOTTOMUP 2
2151 #define TJFLAG_FORCEMMX 8
2152 #define TJFLAG_FORCESSE 16
2153 #define TJFLAG_FORCESSE2 32
2154 #define TJFLAG_FORCESSE3 128
2155 #define TJFLAG_FASTUPSAMPLE 256
2156 #define TJFLAG_NOREALLOC 1024
2158 DLLEXPORT unsigned char *tjAlloc(int bytes);
2160 DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
2162 DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
2164 DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
2165 int width, int pitch, int height, int pixelFormat,
2166 unsigned char **jpegBuf, unsigned long *jpegSize,
2167 int jpegSubsamp, int jpegQual, int flags);
2169 DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
2170 unsigned long jpegSize, unsigned char *dstBuf,
2171 int width, int pitch, int height, int pixelFormat,
2174 DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
2175 int pitch, int height, int pixelFormat,
2176 unsigned char *dstBuf, int subsamp, int flags);
2178 DLLEXPORT void tjFree(unsigned char *buffer);
2180 DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
2182 DLLEXPORT tjhandle tjInitTransform(void);
2184 DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
2185 unsigned long jpegSize, int n,
2186 unsigned char **dstBufs, unsigned long *dstSizes,
2187 tjtransform *transforms, int flags);
2189 /* TurboJPEG 1.2.1+ */
2191 #define TJFLAG_FASTDCT 2048
2192 #define TJFLAG_ACCURATEDCT 4096
2194 /* TurboJPEG 1.4+ */
2196 DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
2199 DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
2200 int width, int align, int height, int subsamp,
2201 unsigned char **jpegBuf,
2202 unsigned long *jpegSize, int jpegQual,
2205 DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
2206 const unsigned char **srcPlanes,
2207 int width, const int *strides,
2208 int height, int subsamp,
2209 unsigned char **jpegBuf,
2210 unsigned long *jpegSize, int jpegQual,
2213 DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
2214 int align, int subsamp, unsigned char *dstBuf,
2215 int width, int pitch, int height, int pixelFormat,
2218 DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
2219 const unsigned char **srcPlanes,
2220 const int *strides, int subsamp,
2221 unsigned char *dstBuf, int width, int pitch,
2222 int height, int pixelFormat, int flags);
2224 DLLEXPORT int tjDecompressHeader3(tjhandle handle,
2225 const unsigned char *jpegBuf,
2226 unsigned long jpegSize, int *width,
2227 int *height, int *jpegSubsamp,
2228 int *jpegColorspace);
2230 DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
2231 unsigned long jpegSize, unsigned char *dstBuf,
2232 int width, int align, int height, int flags);
2234 DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
2235 const unsigned char *jpegBuf,
2236 unsigned long jpegSize,
2237 unsigned char **dstPlanes, int width,
2238 int *strides, int height, int flags);
2240 DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
2241 int width, int pitch, int height, int pixelFormat,
2242 unsigned char *dstBuf, int align, int subsamp,
2245 DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
2246 int width, int pitch, int height,
2247 int pixelFormat, unsigned char **dstPlanes,
2248 int *strides, int subsamp, int flags);
2250 DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
2252 DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
2253 int height, int subsamp);
2255 DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
2257 /* TurboJPEG 2.0+ */
2259 #define TJFLAG_STOPONWARNING 8192
2260 #define TJFLAG_PROGRESSIVE 16384
2262 DLLEXPORT int tjGetErrorCode(tjhandle handle);
2264 DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
2266 DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
2267 int align, int *height, int *pixelFormat,
2270 DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
2271 int width, int pitch, int height, int pixelFormat,
2274 /* TurboJPEG 2.1+ */
2276 #define TJFLAG_LIMITSCANS 32768