Coverage Report

Created: 2024-05-28 04:24

/src/libjpeg-turbo.main/turbojpeg.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C)2009-2015, 2017, 2020-2023 D. R. Commander.
3
 *                                         All Rights Reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * - Redistributions of source code must retain the above copyright notice,
9
 *   this list of conditions and the following disclaimer.
10
 * - Redistributions in binary form must reproduce the above copyright notice,
11
 *   this list of conditions and the following disclaimer in the documentation
12
 *   and/or other materials provided with the distribution.
13
 * - Neither the name of the libjpeg-turbo Project nor the names of its
14
 *   contributors may be used to endorse or promote products derived from this
15
 *   software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#ifndef __TURBOJPEG_H__
31
#define __TURBOJPEG_H__
32
33
#include <stddef.h>
34
35
#if defined(_WIN32) && defined(DLLDEFINE)
36
#define DLLEXPORT  __declspec(dllexport)
37
#else
38
#define DLLEXPORT
39
#endif
40
#define DLLCALL
41
42
43
/**
44
 * @addtogroup TurboJPEG
45
 * TurboJPEG API.  This API provides an interface for generating, decoding, and
46
 * transforming planar YUV and JPEG images in memory.
47
 *
48
 * @anchor YUVnotes
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.
55
 *
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.
71
 *
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.
77
 *
78
 * @{
79
 */
80
81
82
/**
83
 * The number of initialization options
84
 */
85
592k
#define TJ_NUMINIT  3
86
87
/**
88
 * Initialization options.
89
 */
90
enum TJINIT {
91
  /**
92
   * Initialize the TurboJPEG instance for compression.
93
   */
94
  TJINIT_COMPRESS,
95
  /**
96
   * Initialize the TurboJPEG instance for decompression.
97
   */
98
  TJINIT_DECOMPRESS,
99
  /**
100
   * Initialize the TurboJPEG instance for lossless transformation (both
101
   * compression and decompression.)
102
   */
103
  TJINIT_TRANSFORM
104
};
105
106
107
/**
108
 * The number of chrominance subsampling options
109
 */
110
1.56M
#define TJ_NUMSAMP  7
111
112
/**
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".
120
 */
121
enum TJSAMP {
122
  /**
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
125
   * source image.
126
   */
127
  TJSAMP_444,
128
  /**
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.
131
   */
132
  TJSAMP_422,
133
  /**
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.
136
   */
137
  TJSAMP_420,
138
  /**
139
   * Grayscale.  The JPEG or YUV image will contain no chrominance components.
140
   */
141
  TJSAMP_GRAY,
142
  /**
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.
145
   *
146
   * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
147
   */
148
  TJSAMP_440,
149
  /**
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.
157
   *
158
   * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
159
   */
160
  TJSAMP_411,
161
  /**
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
168
   * vertical features.
169
   *
170
   * @note 4:4:1 subsampling is not fully accelerated in libjpeg-turbo.
171
   */
172
  TJSAMP_441,
173
  /**
174
   * Unknown subsampling.  The JPEG image uses an unusual type of chrominance
175
   * subsampling.  Such images can be decompressed into packed-pixel images,
176
   * but they cannot be
177
   * - decompressed into planar YUV images,
178
   * - losslessly transformed if #TJXOPT_CROP is specified, or
179
   * - partially decompressed using a cropping region.
180
   */
181
  TJSAMP_UNKNOWN = -1
182
};
183
184
/**
185
 * MCU block width (in pixels) for a given level of chrominance subsampling.
186
 * MCU block sizes:
187
 * - 8x8 for no subsampling or grayscale
188
 * - 16x8 for 4:2:2
189
 * - 8x16 for 4:4:0
190
 * - 16x16 for 4:2:0
191
 * - 32x8 for 4:1:1
192
 * - 8x32 for 4:4:1
193
 */
194
static const int tjMCUWidth[TJ_NUMSAMP]  = { 8, 16, 16, 8, 8, 32, 8 };
195
196
/**
197
 * MCU block height (in pixels) for a given level of chrominance subsampling.
198
 * MCU block sizes:
199
 * - 8x8 for no subsampling or grayscale
200
 * - 16x8 for 4:2:2
201
 * - 8x16 for 4:4:0
202
 * - 16x16 for 4:2:0
203
 * - 32x8 for 4:1:1
204
 * - 8x32 for 4:4:1
205
 */
206
static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8, 32 };
207
208
209
/**
210
 * The number of pixel formats
211
 */
212
151k
#define TJ_NUMPF  12
213
214
/**
215
 * Pixel formats
216
 */
217
enum TJPF {
218
  /**
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.
222
   */
223
  TJPF_RGB,
224
  /**
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.
228
   */
229
  TJPF_BGR,
230
  /**
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.
235
   */
236
  TJPF_RGBX,
237
  /**
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.
242
   */
243
  TJPF_BGRX,
244
  /**
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.
249
   */
250
  TJPF_XBGR,
251
  /**
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.
256
   */
257
  TJPF_XRGB,
258
  /**
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.)
262
   */
263
  TJPF_GRAY,
264
  /**
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.
268
   */
269
  TJPF_RGBA,
270
  /**
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.
274
   */
275
  TJPF_BGRA,
276
  /**
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.
280
   */
281
  TJPF_ABGR,
282
  /**
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.
286
   */
287
  TJPF_ARGB,
288
  /**
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.
302
   */
303
  TJPF_CMYK,
304
  /**
305
   * Unknown pixel format.  Currently this is only used by #tj3LoadImage8(),
306
   * #tj3LoadImage12(), and #tj3LoadImage16().
307
   */
308
  TJPF_UNKNOWN = -1
309
};
310
311
/**
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.
318
 */
319
static const int tjRedOffset[TJ_NUMPF] = {
320
  0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
321
};
322
/**
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.
329
 */
330
static const int tjGreenOffset[TJ_NUMPF] = {
331
  1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
332
};
333
/**
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.
340
 */
341
static const int tjBlueOffset[TJ_NUMPF] = {
342
  2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
343
};
344
/**
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.
351
 */
352
static const int tjAlphaOffset[TJ_NUMPF] = {
353
  -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
354
};
355
/**
356
 * Pixel size (in samples) for a given pixel format
357
 */
358
static const int tjPixelSize[TJ_NUMPF] = {
359
  3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
360
};
361
362
363
/**
364
 * The number of JPEG colorspaces
365
 */
366
#define TJ_NUMCS  5
367
368
/**
369
 * JPEG colorspaces
370
 */
371
enum TJCS {
372
  /**
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.
379
   */
380
  TJCS_RGB,
381
  /**
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.
396
   */
397
  TJCS_YCbCr,
398
  /**
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
404
   * images.
405
   */
406
  TJCS_GRAY,
407
  /**
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
412
   * CMYK pixel format.
413
   */
414
  TJCS_CMYK,
415
  /**
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.
423
   */
424
  TJCS_YCCK
425
};
426
427
428
/**
429
 * Parameters
430
 */
431
enum TJPARAM {
432
  /**
433
   * Error handling behavior
434
   *
435
   * **Value**
436
   * - `0` *[default]* Allow the current compression/decompression/transform
437
   * operation to complete unless a fatal error is encountered.
438
   * - `1` Immediately discontinue the current
439
   * compression/decompression/transform operation if a warning (non-fatal
440
   * error) occurs.
441
   */
442
  TJPARAM_STOPONWARNING,
443
  /**
444
   * Row order in packed-pixel source/destination images
445
   *
446
   * **Value**
447
   * - `0` *[default]* top-down (X11) order
448
   * - `1` bottom-up (Windows, OpenGL) order
449
   */
450
  TJPARAM_BOTTOMUP,
451
  /**
452
   * JPEG destination buffer (re)allocation [compression, lossless
453
   * transformation]
454
   *
455
   * **Value**
456
   * - `0` *[default]* Attempt to allocate or reallocate the JPEG destination
457
   * buffer as needed.
458
   * - `1` Generate an error if the JPEG destination buffer is invalid or too
459
   * small.
460
   */
461
  TJPARAM_NOREALLOC,
462
  /**
463
   * Perceptual quality of lossy JPEG images [compression only]
464
   *
465
   * **Value**
466
   * - `1`-`100` (`1` = worst quality but best compression, `100` = best
467
   * quality but worst compression) *[no default; must be explicitly
468
   * specified]*
469
   */
470
  TJPARAM_QUALITY,
471
  /**
472
   * Chrominance subsampling level
473
   *
474
   * The JPEG or YUV image uses (decompression, decoding) or will use (lossy
475
   * compression, encoding) the specified level of chrominance subsampling.
476
   *
477
   * **Value**
478
   * - One of the @ref TJSAMP "chrominance subsampling options" *[no default;
479
   * must be explicitly specified for lossy compression, encoding, and
480
   * decoding]*
481
   */
482
  TJPARAM_SUBSAMP,
483
  /**
484
   * JPEG width (in pixels) [decompression only, read-only]
485
   */
486
  TJPARAM_JPEGWIDTH,
487
  /**
488
   * JPEG height (in pixels) [decompression only, read-only]
489
   */
490
  TJPARAM_JPEGHEIGHT,
491
  /**
492
   * JPEG data precision (bits per sample) [decompression only, read-only]
493
   *
494
   * The JPEG image uses the specified number of bits per sample.
495
   *
496
   * **Value**
497
   * - `8`, `12`, or `16`
498
   *
499
   * 12-bit data precision implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC
500
   * is set.
501
   */
502
  TJPARAM_PRECISION,
503
  /**
504
   * JPEG colorspace
505
   *
506
   * The JPEG image uses (decompression) or will use (lossy compression) the
507
   * specified colorspace.
508
   *
509
   * **Value**
510
   * - One of the @ref TJCS "JPEG colorspaces" *[default for lossy compression:
511
   * automatically selected based on the subsampling level and pixel format]*
512
   */
513
  TJPARAM_COLORSPACE,
514
  /**
515
   * Chrominance upsampling algorithm [lossy decompression only]
516
   *
517
   * **Value**
518
   * - `0` *[default]* Use smooth upsampling when decompressing a JPEG image
519
   * that was compressed using chrominance subsampling.  This creates a smooth
520
   * transition between neighboring chrominance components in order to reduce
521
   * upsampling artifacts in the decompressed image.
522
   * - `1` Use the fastest chrominance upsampling algorithm available, which
523
   * may combine upsampling with color conversion.
524
   */
525
  TJPARAM_FASTUPSAMPLE,
526
  /**
527
   * DCT/IDCT algorithm [lossy compression and decompression]
528
   *
529
   * **Value**
530
   * - `0` *[default]* Use the most accurate DCT/IDCT algorithm available.
531
   * - `1` Use the fastest DCT/IDCT algorithm available.
532
   *
533
   * This parameter is provided mainly for backward compatibility with libjpeg,
534
   * which historically implemented several different DCT/IDCT algorithms
535
   * because of performance limitations with 1990s CPUs.  In the libjpeg-turbo
536
   * implementation of the TurboJPEG API:
537
   * - The "fast" and "accurate" DCT/IDCT algorithms perform similarly on
538
   * modern x86/x86-64 CPUs that support AVX2 instructions.
539
   * - The "fast" algorithm is generally only about 5-15% faster than the
540
   * "accurate" algorithm on other types of CPUs.
541
   * - The difference in accuracy between the "fast" and "accurate" algorithms
542
   * is the most pronounced at JPEG quality levels above 90 and tends to be
543
   * more pronounced with decompression than with compression.
544
   * - The "fast" algorithm degrades and is not fully accelerated for JPEG
545
   * quality levels above 97, so it will be slower than the "accurate"
546
   * algorithm.
547
   */
548
  TJPARAM_FASTDCT,
549
  /**
550
   * Optimized baseline entropy coding [lossy compression only]
551
   *
552
   * **Value**
553
   * - `0` *[default]* The JPEG image will use the default Huffman tables.
554
   * - `1` Optimal Huffman tables will be computed for the JPEG image.  For
555
   * lossless transformation, this can also be specified using
556
   * #TJXOPT_OPTIMIZE.
557
   *
558
   * Optimized baseline entropy coding will improve compression slightly
559
   * (generally 5% or less), but it will reduce compression performance
560
   * considerably.
561
   */
562
  TJPARAM_OPTIMIZE,
563
  /**
564
   * Progressive entropy coding
565
   *
566
   * **Value**
567
   * - `0` *[default for compression, lossless transformation]* The lossy JPEG
568
   * image uses (decompression) or will use (compression, lossless
569
   * transformation) baseline entropy coding.
570
   * - `1` The lossy JPEG image uses (decompression) or will use (compression,
571
   * lossless transformation) progressive entropy coding.  For lossless
572
   * transformation, this can also be specified using #TJXOPT_PROGRESSIVE.
573
   *
574
   * Progressive entropy coding will generally improve compression relative to
575
   * baseline entropy coding, but it will reduce compression and decompression
576
   * performance considerably.  Can be combined with #TJPARAM_ARITHMETIC.
577
   * Implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC is also set.
578
   */
579
  TJPARAM_PROGRESSIVE,
580
  /**
581
   * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless
582
   * transformation]
583
   *
584
   * Setting this parameter will cause the decompression and transform
585
   * functions to return an error if the number of scans in a progressive JPEG
586
   * image exceeds the specified limit.  The primary purpose of this is to
587
   * allow security-critical applications to guard against an exploit of the
588
   * progressive JPEG format described in
589
   * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
590
   *
591
   * **Value**
592
   * - maximum number of progressive JPEG scans that the decompression and
593
   * transform functions will process *[default: `0` (no limit)]*
594
   *
595
   * @see #TJPARAM_PROGRESSIVE
596
   */
597
  TJPARAM_SCANLIMIT,
598
  /**
599
   * Arithmetic entropy coding
600
   *
601
   * **Value**
602
   * - `0` *[default for compression, lossless transformation]* The lossy JPEG
603
   * image uses (decompression) or will use (compression, lossless
604
   * transformation) Huffman entropy coding.
605
   * - `1` The lossy JPEG image uses (decompression) or will use (compression,
606
   * lossless transformation) arithmetic entropy coding.  For lossless
607
   * transformation, this can also be specified using #TJXOPT_ARITHMETIC.
608
   *
609
   * Arithmetic entropy coding will generally improve compression relative to
610
   * Huffman entropy coding, but it will reduce compression and decompression
611
   * performance considerably.  Can be combined with #TJPARAM_PROGRESSIVE.
612
   */
613
  TJPARAM_ARITHMETIC,
614
  /**
615
   * Lossless JPEG
616
   *
617
   * **Value**
618
   * - `0` *[default for compression]* The JPEG image is (decompression) or
619
   * will be (compression) lossy/DCT-based.
620
   * - `1` The JPEG image is (decompression) or will be (compression)
621
   * lossless/predictive.
622
   *
623
   * In most cases, compressing and decompressing lossless JPEG images is
624
   * considerably slower than compressing and decompressing lossy JPEG images,
625
   * and lossless JPEG images are much larger than lossy JPEG images.  Thus,
626
   * lossless JPEG images are typically used only for applications that require
627
   * mathematically lossless compression.  Also note that the following
628
   * features are not available with lossless JPEG images:
629
   * - Colorspace conversion (lossless JPEG images always use #TJCS_RGB,
630
   * #TJCS_GRAY, or #TJCS_CMYK, depending on the pixel format of the source
631
   * image)
632
   * - Chrominance subsampling (lossless JPEG images always use #TJSAMP_444)
633
   * - JPEG quality selection
634
   * - DCT/IDCT algorithm selection
635
   * - Progressive entropy coding
636
   * - Arithmetic entropy coding
637
   * - Compression from/decompression to planar YUV images
638
   * - Decompression scaling
639
   * - Lossless transformation
640
   *
641
   * @see #TJPARAM_LOSSLESSPSV, #TJPARAM_LOSSLESSPT
642
   */
643
  TJPARAM_LOSSLESS,
644
  /**
645
   * Lossless JPEG predictor selection value (PSV)
646
   *
647
   * **Value**
648
   * - `1`-`7` *[default for compression: `1`]*
649
   *
650
   * Lossless JPEG compression shares no algorithms with lossy JPEG
651
   * compression.  Instead, it uses differential pulse-code modulation (DPCM),
652
   * an algorithm whereby each sample is encoded as the difference between the
653
   * sample's value and a "predictor", which is based on the values of
654
   * neighboring samples.  If Ra is the sample immediately to the left of the
655
   * current sample, Rb is the sample immediately above the current sample, and
656
   * Rc is the sample diagonally to the left and above the current sample, then
657
   * the relationship between the predictor selection value and the predictor
658
   * is as follows:
659
   *
660
   * PSV | Predictor
661
   * ----|----------
662
   * 1   | Ra
663
   * 2   | Rb
664
   * 3   | Rc
665
   * 4   | Ra + Rb – Rc
666
   * 5   | Ra + (Rb – Rc) / 2
667
   * 6   | Rb + (Ra – Rc) / 2
668
   * 7   | (Ra + Rb) / 2
669
   *
670
   * Predictors 1-3 are 1-dimensional predictors, whereas Predictors 4-7 are
671
   * 2-dimensional predictors.  The best predictor for a particular image
672
   * depends on the image.
673
   *
674
   * @see #TJPARAM_LOSSLESS
675
   */
676
  TJPARAM_LOSSLESSPSV,
677
  /**
678
   * Lossless JPEG point transform (Pt)
679
   *
680
   * **Value**
681
   * - `0` through ***precision*** *- 1*, where ***precision*** is the JPEG
682
   * data precision in bits *[default for compression: `0`]*
683
   *
684
   * A point transform value of `0` is necessary in order to generate a fully
685
   * lossless JPEG image.  (A non-zero point transform value right-shifts the
686
   * input samples by the specified number of bits, which is effectively a form
687
   * of lossy color quantization.)
688
   *
689
   * @see #TJPARAM_LOSSLESS, #TJPARAM_PRECISION
690
   */
691
  TJPARAM_LOSSLESSPT,
692
  /**
693
   * JPEG restart marker interval in MCU blocks (lossy) or samples (lossless)
694
   * [compression only]
695
   *
696
   * The nature of entropy coding is such that a corrupt JPEG image cannot
697
   * be decompressed beyond the point of corruption unless it contains restart
698
   * markers.  A restart marker stops and restarts the entropy coding algorithm
699
   * so that, if a JPEG image is corrupted, decompression can resume at the
700
   * next marker.  Thus, adding more restart markers improves the fault
701
   * tolerance of the JPEG image, but adding too many restart markers can
702
   * adversely affect the compression ratio and performance.
703
   *
704
   * **Value**
705
   * - the number of MCU blocks or samples between each restart marker
706
   * *[default: `0` (no restart markers)]*
707
   *
708
   * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTROWS to 0.
709
   */
710
  TJPARAM_RESTARTBLOCKS,
711
  /**
712
   * JPEG restart marker interval in MCU rows (lossy) or sample rows (lossless)
713
   * [compression only]
714
   *
715
   * See #TJPARAM_RESTARTBLOCKS for a description of restart markers.
716
   *
717
   * **Value**
718
   * - the number of MCU rows or sample rows between each restart marker
719
   * *[default: `0` (no restart markers)]*
720
   *
721
   * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTBLOCKS to
722
   * 0.
723
   */
724
  TJPARAM_RESTARTROWS,
725
  /**
726
   * JPEG horizontal pixel density
727
   *
728
   * **Value**
729
   * - The JPEG image has (decompression) or will have (compression) the
730
   * specified horizontal pixel density *[default for compression: `1`]*.
731
   *
732
   * This value is stored in or read from the JPEG header.  It does not affect
733
   * the contents of the JPEG image.  Note that this parameter is set by
734
   * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
735
   * density information, and the value of this parameter is stored to a
736
   * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
737
   * is `2`.
738
   *
739
   * @see TJPARAM_DENSITYUNITS
740
   */
741
  TJPARAM_XDENSITY,
742
  /**
743
   * JPEG vertical pixel density
744
   *
745
   * **Value**
746
   * - The JPEG image has (decompression) or will have (compression) the
747
   * specified vertical pixel density *[default for compression: `1`]*.
748
   *
749
   * This value is stored in or read from the JPEG header.  It does not affect
750
   * the contents of the JPEG image.  Note that this parameter is set by
751
   * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
752
   * density information, and the value of this parameter is stored to a
753
   * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
754
   * is `2`.
755
   *
756
   * @see TJPARAM_DENSITYUNITS
757
   */
758
  TJPARAM_YDENSITY,
759
  /**
760
   * JPEG pixel density units
761
   *
762
   * **Value**
763
   * - `0` *[default for compression]* The pixel density of the JPEG image is
764
   * expressed (decompression) or will be expressed (compression) in unknown
765
   * units.
766
   * - `1` The pixel density of the JPEG image is expressed (decompression) or
767
   * will be expressed (compression) in units of pixels/inch.
768
   * - `2` The pixel density of the JPEG image is expressed (decompression) or
769
   * will be expressed (compression) in units of pixels/cm.
770
   *
771
   * This value is stored in or read from the JPEG header.  It does not affect
772
   * the contents of the JPEG image.  Note that this parameter is set by
773
   * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
774
   * density information, and the value of this parameter is stored to a
775
   * Windows BMP file by #tj3SaveImage8() if the value is `2`.
776
   *
777
   * @see TJPARAM_XDENSITY, TJPARAM_YDENSITY
778
   */
779
  TJPARAM_DENSITYUNITS,
780
  /**
781
   * Memory limit for intermediate buffers
782
   *
783
   * **Value**
784
   * - the maximum amount of memory (in megabytes) that will be allocated for
785
   * intermediate buffers, which are used with progressive JPEG compression and
786
   * decompression, optimized baseline entropy coding, lossless JPEG
787
   * compression, and lossless transformation *[default: `0` (no limit)]*
788
   */
789
  TJPARAM_MAXMEMORY,
790
  /**
791
   * Image size limit [decompression, lossless transformation, packed-pixel
792
   * image loading]
793
   *
794
   * Setting this parameter will cause the decompression, transform, and image
795
   * loading functions to return an error if the number of pixels in the source
796
   * image exceeds the specified limit.  This allows security-critical
797
   * applications to guard against excessive memory consumption.
798
   *
799
   * **Value**
800
   * - maximum number of pixels that the decompression, transform, and image
801
   * loading functions will process *[default: `0` (no limit)]*
802
   */
803
  TJPARAM_MAXPIXELS
804
};
805
806
807
/**
808
 * The number of error codes
809
 */
810
#define TJ_NUMERR  2
811
812
/**
813
 * Error codes
814
 */
815
enum TJERR {
816
  /**
817
   * The error was non-fatal and recoverable, but the destination image may
818
   * still be corrupt.
819
   */
820
  TJERR_WARNING,
821
  /**
822
   * The error was fatal and non-recoverable.
823
   */
824
  TJERR_FATAL
825
};
826
827
828
/**
829
 * The number of transform operations
830
 */
831
0
#define TJ_NUMXOP  8
832
833
/**
834
 * Transform operations for #tj3Transform()
835
 */
836
enum TJXOP {
837
  /**
838
   * Do not transform the position of the image pixels
839
   */
840
  TJXOP_NONE,
841
  /**
842
   * Flip (mirror) image horizontally.  This transform is imperfect if there
843
   * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
844
   */
845
  TJXOP_HFLIP,
846
  /**
847
   * Flip (mirror) image vertically.  This transform is imperfect if there are
848
   * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
849
   */
850
  TJXOP_VFLIP,
851
  /**
852
   * Transpose image (flip/mirror along upper left to lower right axis.)  This
853
   * transform is always perfect.
854
   */
855
  TJXOP_TRANSPOSE,
856
  /**
857
   * Transverse transpose image (flip/mirror along upper right to lower left
858
   * axis.)  This transform is imperfect if there are any partial MCU blocks in
859
   * the image (see #TJXOPT_PERFECT.)
860
   */
861
  TJXOP_TRANSVERSE,
862
  /**
863
   * Rotate image clockwise by 90 degrees.  This transform is imperfect if
864
   * there are any partial MCU blocks on the bottom edge (see
865
   * #TJXOPT_PERFECT.)
866
   */
867
  TJXOP_ROT90,
868
  /**
869
   * Rotate image 180 degrees.  This transform is imperfect if there are any
870
   * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
871
   */
872
  TJXOP_ROT180,
873
  /**
874
   * Rotate image counter-clockwise by 90 degrees.  This transform is imperfect
875
   * if there are any partial MCU blocks on the right edge (see
876
   * #TJXOPT_PERFECT.)
877
   */
878
  TJXOP_ROT270
879
};
880
881
882
/**
883
 * This option will cause #tj3Transform() to return an error if the transform
884
 * is not perfect.  Lossless transforms operate on MCU blocks, whose size
885
 * depends on the level of chrominance subsampling used (see #tjMCUWidth and
886
 * #tjMCUHeight.)  If the image's width or height is not evenly divisible by
887
 * the MCU block size, then there will be partial MCU blocks on the right
888
 * and/or bottom edges.  It is not possible to move these partial MCU blocks to
889
 * the top or left of the image, so any transform that would require that is
890
 * "imperfect."  If this option is not specified, then any partial MCU blocks
891
 * that cannot be transformed will be left in place, which will create
892
 * odd-looking strips on the right or bottom edge of the image.
893
 */
894
0
#define TJXOPT_PERFECT  (1 << 0)
895
/**
896
 * This option will cause #tj3Transform() to discard any partial MCU blocks
897
 * that cannot be transformed.
898
 */
899
0
#define TJXOPT_TRIM  (1 << 1)
900
/**
901
 * This option will enable lossless cropping.  See #tj3Transform() for more
902
 * information.
903
 */
904
0
#define TJXOPT_CROP  (1 << 2)
905
/**
906
 * This option will discard the color data in the source image and produce a
907
 * grayscale destination image.
908
 */
909
0
#define TJXOPT_GRAY  (1 << 3)
910
/**
911
 * This option will prevent #tj3Transform() from outputting a JPEG image for
912
 * this particular transform.  (This can be used in conjunction with a custom
913
 * filter to capture the transformed DCT coefficients without transcoding
914
 * them.)
915
 */
916
0
#define TJXOPT_NOOUTPUT  (1 << 4)
917
/**
918
 * This option will enable progressive entropy coding in the JPEG image
919
 * generated by this particular transform.  Progressive entropy coding will
920
 * generally improve compression relative to baseline entropy coding (the
921
 * default), but it will reduce decompression performance considerably.
922
 * Can be combined with #TJXOPT_ARITHMETIC.  Implies #TJXOPT_OPTIMIZE unless
923
 * #TJXOPT_ARITHMETIC is also specified.
924
 */
925
0
#define TJXOPT_PROGRESSIVE  (1 << 5)
926
/**
927
 * This option will prevent #tj3Transform() from copying any extra markers
928
 * (including EXIF and ICC profile data) from the source image to the
929
 * destination image.
930
 */
931
0
#define TJXOPT_COPYNONE  (1 << 6)
932
/**
933
 * This option will enable arithmetic entropy coding in the JPEG image
934
 * generated by this particular transform.  Arithmetic entropy coding will
935
 * generally improve compression relative to Huffman entropy coding (the
936
 * default), but it will reduce decompression performance considerably.  Can be
937
 * combined with #TJXOPT_PROGRESSIVE.
938
 */
939
0
#define TJXOPT_ARITHMETIC  (1 << 7)
940
/**
941
 * This option will enable optimized baseline entropy coding in the JPEG image
942
 * generated by this particular transform.  Optimized baseline entropy coding
943
 * will improve compression slightly (generally 5% or less.)
944
 */
945
0
#define TJXOPT_OPTIMIZE  (1 << 8)
946
947
948
/**
949
 * Scaling factor
950
 */
951
typedef struct {
952
  /**
953
   * Numerator
954
   */
955
  int num;
956
  /**
957
   * Denominator
958
   */
959
  int denom;
960
} tjscalingfactor;
961
962
/**
963
 * Cropping region
964
 */
965
typedef struct {
966
  /**
967
   * The left boundary of the cropping region.  This must be evenly divisible
968
   * by the MCU block width (see #tjMCUWidth.)
969
   */
970
  int x;
971
  /**
972
   * The upper boundary of the cropping region.  For lossless transformation,
973
   * this must be evenly divisible by the MCU block height (see #tjMCUHeight.)
974
   */
975
  int y;
976
  /**
977
   * The width of the cropping region.  Setting this to 0 is the equivalent of
978
   * setting it to the width of the source JPEG image - x.
979
   */
980
  int w;
981
  /**
982
   * The height of the cropping region.  Setting this to 0 is the equivalent of
983
   * setting it to the height of the source JPEG image - y.
984
   */
985
  int h;
986
} tjregion;
987
988
/**
989
 * A #tjregion structure that specifies no cropping
990
 */
991
static const tjregion TJUNCROPPED = { 0, 0, 0, 0 };
992
993
/**
994
 * Lossless transform
995
 */
996
typedef struct tjtransform {
997
  /**
998
   * Cropping region
999
   */
1000
  tjregion r;
1001
  /**
1002
   * One of the @ref TJXOP "transform operations"
1003
   */
1004
  int op;
1005
  /**
1006
   * The bitwise OR of one of more of the @ref TJXOPT_ARITHMETIC
1007
   * "transform options"
1008
   */
1009
  int options;
1010
  /**
1011
   * Arbitrary data that can be accessed within the body of the callback
1012
   * function
1013
   */
1014
  void *data;
1015
  /**
1016
   * A callback function that can be used to modify the DCT coefficients after
1017
   * they are losslessly transformed but before they are transcoded to a new
1018
   * JPEG image.  This allows for custom filters or other transformations to be
1019
   * applied in the frequency domain.
1020
   *
1021
   * @param coeffs pointer to an array of transformed DCT coefficients.  (NOTE:
1022
   * this pointer is not guaranteed to be valid once the callback returns, so
1023
   * applications wishing to hand off the DCT coefficients to another function
1024
   * or library should make a copy of them within the body of the callback.)
1025
   *
1026
   * @param arrayRegion #tjregion structure containing the width and height of
1027
   * the array pointed to by `coeffs` as well as its offset relative to the
1028
   * component plane.  TurboJPEG implementations may choose to split each
1029
   * component plane into multiple DCT coefficient arrays and call the callback
1030
   * function once for each array.
1031
   *
1032
   * @param planeRegion #tjregion structure containing the width and height of
1033
   * the component plane to which `coeffs` belongs
1034
   *
1035
   * @param componentID ID number of the component plane to which `coeffs`
1036
   * belongs.  (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
1037
   * typical JPEG images.)
1038
   *
1039
   * @param transformID ID number of the transformed image to which `coeffs`
1040
   * belongs.  This is the same as the index of the transform in the
1041
   * `transforms` array that was passed to #tj3Transform().
1042
   *
1043
   * @param transform a pointer to a #tjtransform structure that specifies the
1044
   * parameters and/or cropping region for this transform
1045
   *
1046
   * @return 0 if the callback was successful, or -1 if an error occurred.
1047
   */
1048
  int (*customFilter) (short *coeffs, tjregion arrayRegion,
1049
                       tjregion planeRegion, int componentID, int transformID,
1050
                       struct tjtransform *transform);
1051
} tjtransform;
1052
1053
/**
1054
 * TurboJPEG instance handle
1055
 */
1056
typedef void *tjhandle;
1057
1058
1059
/**
1060
 * Compute the scaled value of `dimension` using the given scaling factor.
1061
 * This macro performs the integer equivalent of `ceil(dimension *
1062
 * scalingFactor)`.
1063
 */
1064
#define TJSCALED(dimension, scalingFactor) \
1065
134k
  (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
1066
134k
   scalingFactor.denom)
1067
1068
/**
1069
 * A #tjscalingfactor structure that specifies a scaling factor of 1/1 (no
1070
 * scaling)
1071
 */
1072
static const tjscalingfactor TJUNSCALED = { 1, 1 };
1073
1074
1075
#ifdef __cplusplus
1076
extern "C" {
1077
#endif
1078
1079
1080
/**
1081
 * Create a new TurboJPEG instance.
1082
 *
1083
 * @param initType one of the @ref TJINIT "initialization options"
1084
 *
1085
 * @return a handle to the newly-created instance, or NULL if an error occurred
1086
 * (see #tj3GetErrorStr().)
1087
 */
1088
DLLEXPORT tjhandle tj3Init(int initType);
1089
1090
1091
/**
1092
 * Set the value of a parameter.
1093
 *
1094
 * @param handle handle to a TurboJPEG instance
1095
 *
1096
 * @param param one of the @ref TJPARAM "parameters"
1097
 *
1098
 * @param value value of the parameter (refer to @ref TJPARAM
1099
 * "parameter documentation")
1100
 *
1101
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1102
 */
1103
DLLEXPORT int tj3Set(tjhandle handle, int param, int value);
1104
1105
1106
/**
1107
 * Get the value of a parameter.
1108
 *
1109
 * @param handle handle to a TurboJPEG instance
1110
 *
1111
 * @param param one of the @ref TJPARAM "parameters"
1112
 *
1113
 * @return the value of the specified parameter, or -1 if the value is unknown.
1114
 */
1115
DLLEXPORT int tj3Get(tjhandle handle, int param);
1116
1117
1118
/**
1119
 * Compress an 8-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1120
 * an 8-bit-per-sample JPEG image.
1121
 *
1122
 * @param handle handle to a TurboJPEG instance that has been initialized for
1123
 * compression
1124
 *
1125
 * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
1126
 * or CMYK source image to be compressed.  This buffer should normally be
1127
 * `pitch * height` samples in size.  However, you can also use this parameter
1128
 * to compress from a specific region of a larger buffer.
1129
 *
1130
 * @param width width (in pixels) of the source image
1131
 *
1132
 * @param pitch samples per row in the source image.  Normally this should be
1133
 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1134
 * (Setting this parameter to 0 is the equivalent of setting it to
1135
 * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1136
 * parameter to specify the row alignment/padding of the source image, to skip
1137
 * rows, or to compress from a specific region of a larger buffer.
1138
 *
1139
 * @param height height (in pixels) of the source image
1140
 *
1141
 * @param pixelFormat pixel format of the source image (see @ref TJPF
1142
 * "Pixel formats".)
1143
 *
1144
 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1145
 * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1146
 * accommodate the size of the JPEG image.  Thus, you can choose to:
1147
 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1148
 * let TurboJPEG grow the buffer as needed,
1149
 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1150
 * or
1151
 * -# pre-allocate the buffer to a "worst case" size determined by calling
1152
 * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1153
 * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1154
 * .
1155
 * If you choose option 1, then `*jpegSize` should be set to the size of your
1156
 * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1157
 * you should always check `*jpegBuf` upon return from this function, as it may
1158
 * have changed.
1159
 *
1160
 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1161
 * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1162
 * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1163
 * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1164
 * JPEG buffer that is being reused from a previous call to one of the JPEG
1165
 * compression functions, then `*jpegSize` is ignored.
1166
 *
1167
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1168
 * and #tj3GetErrorCode().)
1169
 */
1170
DLLEXPORT int tj3Compress8(tjhandle handle, const unsigned char *srcBuf,
1171
                           int width, int pitch, int height, int pixelFormat,
1172
                           unsigned char **jpegBuf, size_t *jpegSize);
1173
1174
/**
1175
 * Compress a 12-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1176
 * a 12-bit-per-sample JPEG image.
1177
 *
1178
 * \details \copydetails tj3Compress8()
1179
 */
1180
DLLEXPORT int tj3Compress12(tjhandle handle, const short *srcBuf, int width,
1181
                            int pitch, int height, int pixelFormat,
1182
                            unsigned char **jpegBuf, size_t *jpegSize);
1183
1184
/**
1185
 * Compress a 16-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
1186
 * a 16-bit-per-sample lossless JPEG image.
1187
 *
1188
 * \details \copydetails tj3Compress8()
1189
 */
1190
DLLEXPORT int tj3Compress16(tjhandle handle, const unsigned short *srcBuf,
1191
                            int width, int pitch, int height, int pixelFormat,
1192
                            unsigned char **jpegBuf, size_t *jpegSize);
1193
1194
1195
/**
1196
 * Compress an 8-bit-per-sample unified planar YUV image into an
1197
 * 8-bit-per-sample JPEG image.
1198
 *
1199
 * @param handle handle to a TurboJPEG instance that has been initialized for
1200
 * compression
1201
 *
1202
 * @param srcBuf pointer to a buffer containing a unified planar YUV source
1203
 * image to be compressed.  The size of this buffer should match the value
1204
 * returned by #tj3YUVBufSize() for the given image width, height, row
1205
 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1206
 * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1207
 * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1208
 *
1209
 * @param width width (in pixels) of the source image.  If the width is not an
1210
 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
1211
 * buffer copy will be performed.
1212
 *
1213
 * @param align row alignment (in bytes) of the source image (must be a power
1214
 * of 2.)  Setting this parameter to n indicates that each row in each plane of
1215
 * the source image is padded to the nearest multiple of n bytes
1216
 * (1 = unpadded.)
1217
 *
1218
 * @param height height (in pixels) of the source image.  If the height is not
1219
 * an even multiple of the MCU block height (see #tjMCUHeight), then an
1220
 * intermediate buffer copy will be performed.
1221
 *
1222
 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1223
 * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1224
 * accommodate the size of the JPEG image.  Thus, you can choose to:
1225
 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1226
 * let TurboJPEG grow the buffer as needed,
1227
 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1228
 * or
1229
 * -# pre-allocate the buffer to a "worst case" size determined by calling
1230
 * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1231
 * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1232
 * .
1233
 * If you choose option 1, then `*jpegSize` should be set to the size of your
1234
 * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1235
 * you should always check `*jpegBuf` upon return from this function, as it may
1236
 * have changed.
1237
 *
1238
 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1239
 * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1240
 * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1241
 * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1242
 * JPEG buffer that is being reused from a previous call to one of the JPEG
1243
 * compression functions, then `*jpegSize` is ignored.
1244
 *
1245
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1246
 * and #tj3GetErrorCode().)
1247
 */
1248
DLLEXPORT int tj3CompressFromYUV8(tjhandle handle,
1249
                                  const unsigned char *srcBuf, int width,
1250
                                  int align, int height,
1251
                                  unsigned char **jpegBuf, size_t *jpegSize);
1252
1253
1254
/**
1255
 * Compress a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into
1256
 * an 8-bit-per-sample JPEG image.
1257
 *
1258
 * @param handle handle to a TurboJPEG instance that has been initialized for
1259
 * compression
1260
 *
1261
 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1262
 * (or just a Y plane, if compressing a grayscale image) that contain a YUV
1263
 * source image to be compressed.  These planes can be contiguous or
1264
 * non-contiguous in memory.  The size of each plane should match the value
1265
 * returned by #tj3YUVPlaneSize() for the given image width, height, strides,
1266
 * and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer to
1267
 * @ref YUVnotes "YUV Image Format Notes" for more details.
1268
 *
1269
 * @param width width (in pixels) of the source image.  If the width is not an
1270
 * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
1271
 * buffer copy will be performed.
1272
 *
1273
 * @param strides an array of integers, each specifying the number of bytes per
1274
 * row in the corresponding plane of the YUV source image.  Setting the stride
1275
 * for any plane to 0 is the same as setting it to the plane width (see
1276
 * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1277
 * strides for all planes will be set to their respective plane widths.  You
1278
 * can adjust the strides in order to specify an arbitrary amount of row
1279
 * padding in each plane or to create a JPEG image from a subregion of a larger
1280
 * planar YUV image.
1281
 *
1282
 * @param height height (in pixels) of the source image.  If the height is not
1283
 * an even multiple of the MCU block height (see #tjMCUHeight), then an
1284
 * intermediate buffer copy will be performed.
1285
 *
1286
 * @param jpegBuf address of a pointer to a byte buffer that will receive the
1287
 * JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer to
1288
 * accommodate the size of the JPEG image.  Thus, you can choose to:
1289
 * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
1290
 * let TurboJPEG grow the buffer as needed,
1291
 * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
1292
 * or
1293
 * -# pre-allocate the buffer to a "worst case" size determined by calling
1294
 * #tj3JPEGBufSize().  This should ensure that the buffer never has to be
1295
 * re-allocated.  (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
1296
 * .
1297
 * If you choose option 1, then `*jpegSize` should be set to the size of your
1298
 * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1299
 * you should always check `*jpegBuf` upon return from this function, as it may
1300
 * have changed.
1301
 *
1302
 * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
1303
 * buffer.  If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
1304
 * should be set to the size of the buffer.  Upon return, `*jpegSize` will
1305
 * contain the size of the JPEG image (in bytes.)  If `*jpegBuf` points to a
1306
 * JPEG buffer that is being reused from a previous call to one of the JPEG
1307
 * compression functions, then `*jpegSize` is ignored.
1308
 *
1309
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1310
 * and #tj3GetErrorCode().)
1311
 */
1312
DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle,
1313
                                        const unsigned char * const *srcPlanes,
1314
                                        int width, const int *strides,
1315
                                        int height, unsigned char **jpegBuf,
1316
                                        size_t *jpegSize);
1317
1318
1319
/**
1320
 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
1321
 * the given parameters.  The number of bytes returned by this function is
1322
 * larger than the size of the uncompressed source image.  The reason for this
1323
 * is that the JPEG format uses 16-bit coefficients, so it is possible for a
1324
 * very high-quality source image with very high-frequency content to expand
1325
 * rather than compress when converted to the JPEG format.  Such images
1326
 * represent very rare corner cases, but since there is no way to predict the
1327
 * size of a JPEG image prior to compression, the corner cases have to be
1328
 * handled.
1329
 *
1330
 * @param width width (in pixels) of the image
1331
 *
1332
 * @param height height (in pixels) of the image
1333
 *
1334
 * @param jpegSubsamp the level of chrominance subsampling to be used when
1335
 * generating the JPEG image (see @ref TJSAMP
1336
 * "Chrominance subsampling options".)  #TJSAMP_UNKNOWN is treated like
1337
 * #TJSAMP_444, since a buffer large enough to hold a JPEG image with no
1338
 * subsampling should also be large enough to hold a JPEG image with an
1339
 * arbitrary level of subsampling.  Note that lossless JPEG images always
1340
 * use #TJSAMP_444.
1341
 *
1342
 * @return the maximum size of the buffer (in bytes) required to hold the
1343
 * image, or 0 if the arguments are out of bounds.
1344
 */
1345
DLLEXPORT size_t tj3JPEGBufSize(int width, int height, int jpegSubsamp);
1346
1347
1348
/**
1349
 * The size of the buffer (in bytes) required to hold a unified planar YUV
1350
 * image with the given parameters.
1351
 *
1352
 * @param width width (in pixels) of the image
1353
 *
1354
 * @param align row alignment (in bytes) of the image (must be a power of 2.)
1355
 * Setting this parameter to n specifies that each row in each plane of the
1356
 * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
1357
 *
1358
 * @param height height (in pixels) of the image
1359
 *
1360
 * @param subsamp level of chrominance subsampling in the image (see
1361
 * @ref TJSAMP "Chrominance subsampling options".)
1362
 *
1363
 * @return the size of the buffer (in bytes) required to hold the image, or 0
1364
 * if the arguments are out of bounds.
1365
 */
1366
DLLEXPORT size_t tj3YUVBufSize(int width, int align, int height, int subsamp);
1367
1368
1369
/**
1370
 * The size of the buffer (in bytes) required to hold a YUV image plane with
1371
 * the given parameters.
1372
 *
1373
 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1374
 *
1375
 * @param width width (in pixels) of the YUV image.  NOTE: this is the width of
1376
 * the whole image, not the plane width.
1377
 *
1378
 * @param stride bytes per row in the image plane.  Setting this to 0 is the
1379
 * equivalent of setting it to the plane width.
1380
 *
1381
 * @param height height (in pixels) of the YUV image.  NOTE: this is the height
1382
 * of the whole image, not the plane height.
1383
 *
1384
 * @param subsamp level of chrominance subsampling in the image (see
1385
 * @ref TJSAMP "Chrominance subsampling options".)
1386
 *
1387
 * @return the size of the buffer (in bytes) required to hold the YUV image
1388
 * plane, or 0 if the arguments are out of bounds.
1389
 */
1390
DLLEXPORT size_t tj3YUVPlaneSize(int componentID, int width, int stride,
1391
                                 int height, int subsamp);
1392
1393
1394
/**
1395
 * The plane width of a YUV image plane with the given parameters.  Refer to
1396
 * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
1397
 *
1398
 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1399
 *
1400
 * @param width width (in pixels) of the YUV image
1401
 *
1402
 * @param subsamp level of chrominance subsampling in the image (see
1403
 * @ref TJSAMP "Chrominance subsampling options".)
1404
 *
1405
 * @return the plane width of a YUV image plane with the given parameters, or 0
1406
 * if the arguments are out of bounds.
1407
 */
1408
DLLEXPORT int tj3YUVPlaneWidth(int componentID, int width, int subsamp);
1409
1410
1411
/**
1412
 * The plane height of a YUV image plane with the given parameters.  Refer to
1413
 * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
1414
 *
1415
 * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
1416
 *
1417
 * @param height height (in pixels) of the YUV image
1418
 *
1419
 * @param subsamp level of chrominance subsampling in the image (see
1420
 * @ref TJSAMP "Chrominance subsampling options".)
1421
 *
1422
 * @return the plane height of a YUV image plane with the given parameters, or
1423
 * 0 if the arguments are out of bounds.
1424
 */
1425
DLLEXPORT int tj3YUVPlaneHeight(int componentID, int height, int subsamp);
1426
1427
1428
/**
1429
 * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into an
1430
 * 8-bit-per-sample unified planar YUV image.  This function performs color
1431
 * conversion (which is accelerated in the libjpeg-turbo implementation) but
1432
 * does not execute any of the other steps in the JPEG compression process.
1433
 *
1434
 * @param handle handle to a TurboJPEG instance that has been initialized for
1435
 * compression
1436
 *
1437
 * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1438
 * source image to be encoded.  This buffer should normally be `pitch * height`
1439
 * bytes in size.  However, you can also use this parameter to encode from a
1440
 * specific region of a larger buffer.
1441
 *
1442
 * @param width width (in pixels) of the source image
1443
 *
1444
 * @param pitch bytes per row in the source image.  Normally this should be
1445
 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1446
 * (Setting this parameter to 0 is the equivalent of setting it to
1447
 * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1448
 * parameter to specify the row alignment/padding of the source image, to skip
1449
 * rows, or to encode from a specific region of a larger packed-pixel image.
1450
 *
1451
 * @param height height (in pixels) of the source image
1452
 *
1453
 * @param pixelFormat pixel format of the source image (see @ref TJPF
1454
 * "Pixel formats".)
1455
 *
1456
 * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1457
 * image.  Use #tj3YUVBufSize() to determine the appropriate size for this
1458
 * buffer based on the image width, height, row alignment, and level of
1459
 * chrominance subsampling (see #TJPARAM_SUBSAMP.)  The Y, U (Cb), and V (Cr)
1460
 * image planes will be stored sequentially in the buffer.  (Refer to
1461
 * @ref YUVnotes "YUV Image Format Notes".)
1462
 *
1463
 * @param align row alignment (in bytes) of the YUV image (must be a power of
1464
 * 2.)  Setting this parameter to n will cause each row in each plane of the
1465
 * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1466
 * To generate images suitable for X Video, `align` should be set to 4.
1467
 *
1468
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1469
 * and #tj3GetErrorCode().)
1470
 */
1471
DLLEXPORT int tj3EncodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1472
                            int width, int pitch, int height, int pixelFormat,
1473
                            unsigned char *dstBuf, int align);
1474
1475
1476
/**
1477
 * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into separate
1478
 * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes.  This function performs
1479
 * color conversion (which is accelerated in the libjpeg-turbo implementation)
1480
 * but does not execute any of the other steps in the JPEG compression process.
1481
 *
1482
 * @param handle handle to a TurboJPEG instance that has been initialized for
1483
 * compression
1484
 *
1485
 * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
1486
 * source image to be encoded.  This buffer should normally be `pitch * height`
1487
 * bytes in size.  However, you can also use this parameter to encode from a
1488
 * specific region of a larger buffer.
1489
 *
1490
 *
1491
 * @param width width (in pixels) of the source image
1492
 *
1493
 * @param pitch bytes per row in the source image.  Normally this should be
1494
 * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
1495
 * (Setting this parameter to 0 is the equivalent of setting it to
1496
 * <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can also use this
1497
 * parameter to specify the row alignment/padding of the source image, to skip
1498
 * rows, or to encode from a specific region of a larger packed-pixel image.
1499
 *
1500
 * @param height height (in pixels) of the source image
1501
 *
1502
 * @param pixelFormat pixel format of the source image (see @ref TJPF
1503
 * "Pixel formats".)
1504
 *
1505
 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1506
 * (or just a Y plane, if generating a grayscale image) that will receive the
1507
 * encoded image.  These planes can be contiguous or non-contiguous in memory.
1508
 * Use #tj3YUVPlaneSize() to determine the appropriate size for each plane
1509
 * based on the image width, height, strides, and level of chrominance
1510
 * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
1511
 * "YUV Image Format Notes" for more details.
1512
 *
1513
 * @param strides an array of integers, each specifying the number of bytes per
1514
 * row in the corresponding plane of the YUV image.  Setting the stride for any
1515
 * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
1516
 * "YUV Image Format Notes".)  If `strides` is NULL, then the strides for all
1517
 * planes will be set to their respective plane widths.  You can adjust the
1518
 * strides in order to add an arbitrary amount of row padding to each plane or
1519
 * to encode an RGB or grayscale image into a subregion of a larger planar YUV
1520
 * image.
1521
 *
1522
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1523
 * and #tj3GetErrorCode().)
1524
 */
1525
DLLEXPORT int tj3EncodeYUVPlanes8(tjhandle handle, const unsigned char *srcBuf,
1526
                                  int width, int pitch, int height,
1527
                                  int pixelFormat, unsigned char **dstPlanes,
1528
                                  int *strides);
1529
1530
1531
/**
1532
 * Retrieve information about a JPEG image without decompressing it, or prime
1533
 * the decompressor with quantization and Huffman tables.  If a JPEG image is
1534
 * passed to this function, then the @ref TJPARAM "parameters" that describe
1535
 * the JPEG image will be set when the function returns.
1536
 *
1537
 * @param handle handle to a TurboJPEG instance that has been initialized for
1538
 * decompression
1539
 *
1540
 * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
1541
 * "abbreviated table specification" (AKA "tables-only") datastream.  Passing a
1542
 * tables-only datastream to this function primes the decompressor with
1543
 * quantization and Huffman tables that can be used when decompressing
1544
 * subsequent "abbreviated image" datastreams.  This is useful, for instance,
1545
 * when decompressing video streams in which all frames share the same
1546
 * quantization and Huffman tables.
1547
 *
1548
 * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
1549
 *
1550
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1551
 * and #tj3GetErrorCode().)
1552
 */
1553
DLLEXPORT int tj3DecompressHeader(tjhandle handle,
1554
                                  const unsigned char *jpegBuf,
1555
                                  size_t jpegSize);
1556
1557
1558
/**
1559
 * Returns a list of fractional scaling factors that the JPEG decompressor
1560
 * supports.
1561
 *
1562
 * @param numScalingFactors pointer to an integer variable that will receive
1563
 * the number of elements in the list
1564
 *
1565
 * @return a pointer to a list of fractional scaling factors, or NULL if an
1566
 * error is encountered (see #tj3GetErrorStr().)
1567
 */
1568
DLLEXPORT tjscalingfactor *tj3GetScalingFactors(int *numScalingFactors);
1569
1570
1571
/**
1572
 * Set the scaling factor for subsequent lossy decompression operations.
1573
 *
1574
 * @param handle handle to a TurboJPEG instance that has been initialized for
1575
 * decompression
1576
 *
1577
 * @param scalingFactor #tjscalingfactor structure that specifies a fractional
1578
 * scaling factor that the decompressor supports (see #tj3GetScalingFactors()),
1579
 * or <tt>#TJUNSCALED</tt> for no scaling.  Decompression scaling is a function
1580
 * of the IDCT algorithm, so scaling factors are generally limited to multiples
1581
 * of 1/8.  If the entire JPEG image will be decompressed, then the width and
1582
 * height of the scaled destination image can be determined by calling
1583
 * #TJSCALED() with the JPEG width and height (see #TJPARAM_JPEGWIDTH and
1584
 * #TJPARAM_JPEGHEIGHT) and the specified scaling factor.  When decompressing
1585
 * into a planar YUV image, an intermediate buffer copy will be performed if
1586
 * the width or height of the scaled destination image is not an even multiple
1587
 * of the MCU block size (see #tjMCUWidth and #tjMCUHeight.)  Note that
1588
 * decompression scaling is not available (and the specified scaling factor is
1589
 * ignored) when decompressing lossless JPEG images (see #TJPARAM_LOSSLESS),
1590
 * since the IDCT algorithm is not used with those images.  Note also that
1591
 * #TJPARAM_FASTDCT is ignored when decompression scaling is enabled.
1592
 *
1593
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1594
 */
1595
DLLEXPORT int tj3SetScalingFactor(tjhandle handle,
1596
                                  tjscalingfactor scalingFactor);
1597
1598
1599
/**
1600
 * Set the cropping region for partially decompressing a lossy JPEG image into
1601
 * a packed-pixel image
1602
 *
1603
 * @param handle handle to a TurboJPEG instance that has been initialized for
1604
 * decompression
1605
 *
1606
 * @param croppingRegion #tjregion structure that specifies a subregion of the
1607
 * JPEG image to decompress, or <tt>#TJUNCROPPED</tt> for no cropping.  The
1608
 * left boundary of the cropping region must be evenly divisible by the scaled
1609
 * MCU block width (<tt>#TJSCALED(#tjMCUWidth[subsamp], scalingFactor)</tt>,
1610
 * where `subsamp` is the level of chrominance subsampling in the JPEG image
1611
 * (see #TJPARAM_SUBSAMP) and `scalingFactor` is the decompression scaling
1612
 * factor (see #tj3SetScalingFactor().)  The cropping region should be
1613
 * specified relative to the scaled image dimensions.  Unless `croppingRegion`
1614
 * is <tt>#TJUNCROPPED</tt>, the JPEG header must be read (see
1615
 * #tj3DecompressHeader()) prior to calling this function.
1616
 *
1617
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
1618
 */
1619
DLLEXPORT int tj3SetCroppingRegion(tjhandle handle, tjregion croppingRegion);
1620
1621
1622
/**
1623
 * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample
1624
 * packed-pixel RGB, grayscale, or CMYK image.  The @ref TJPARAM "parameters"
1625
 * that describe the JPEG image will be set when this function returns.
1626
 *
1627
 * @param handle handle to a TurboJPEG instance that has been initialized for
1628
 * decompression
1629
 *
1630
 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1631
 * decompress
1632
 *
1633
 * @param jpegSize size of the JPEG image (in bytes)
1634
 *
1635
 * @param dstBuf pointer to a buffer that will receive the packed-pixel
1636
 * decompressed image.  This buffer should normally be
1637
 * `pitch * destinationHeight` samples in size.  However, you can also use this
1638
 * parameter to decompress into a specific region of a larger buffer.  NOTE:
1639
 * If the JPEG image is lossy, then `destinationHeight` is either the scaled
1640
 * JPEG height (see #TJSCALED(), #TJPARAM_JPEGHEIGHT, and
1641
 * #tj3SetScalingFactor()) or the height of the cropping region (see
1642
 * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
1643
 * `destinationHeight` is the JPEG height.
1644
 *
1645
 * @param pitch samples per row in the destination image.  Normally this should
1646
 * be set to <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>, if the
1647
 * destination image should be unpadded.  (Setting this parameter to 0 is the
1648
 * equivalent of setting it to
1649
 * <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>.)  However, you can
1650
 * also use this parameter to specify the row alignment/padding of the
1651
 * destination image, to skip rows, or to decompress into a specific region of
1652
 * a larger buffer.  NOTE: If the JPEG image is lossy, then `destinationWidth`
1653
 * is either the scaled JPEG width (see #TJSCALED(), #TJPARAM_JPEGWIDTH, and
1654
 * #tj3SetScalingFactor()) or the width of the cropping region (see
1655
 * #tj3SetCroppingRegion().)  If the JPEG image is lossless, then
1656
 * `destinationWidth` is the JPEG width.
1657
 *
1658
 * @param pixelFormat pixel format of the destination image (see @ref
1659
 * TJPF "Pixel formats".)
1660
 *
1661
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1662
 * and #tj3GetErrorCode().)
1663
 */
1664
DLLEXPORT int tj3Decompress8(tjhandle handle, const unsigned char *jpegBuf,
1665
                             size_t jpegSize, unsigned char *dstBuf, int pitch,
1666
                             int pixelFormat);
1667
1668
/**
1669
 * Decompress a 12-bit-per-sample JPEG image into a 12-bit-per-sample
1670
 * packed-pixel RGB, grayscale, or CMYK image.
1671
 *
1672
 * \details \copydetails tj3Decompress8()
1673
 */
1674
DLLEXPORT int tj3Decompress12(tjhandle handle, const unsigned char *jpegBuf,
1675
                              size_t jpegSize, short *dstBuf, int pitch,
1676
                              int pixelFormat);
1677
1678
/**
1679
 * Decompress a 16-bit-per-sample lossless JPEG image into a 16-bit-per-sample
1680
 * packed-pixel RGB, grayscale, or CMYK image.
1681
 *
1682
 * \details \copydetails tj3Decompress8()
1683
 */
1684
DLLEXPORT int tj3Decompress16(tjhandle handle, const unsigned char *jpegBuf,
1685
                              size_t jpegSize, unsigned short *dstBuf,
1686
                              int pitch, int pixelFormat);
1687
1688
1689
/**
1690
 * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample unified
1691
 * planar YUV image.  This function performs JPEG decompression but leaves out
1692
 * the color conversion step, so a planar YUV image is generated instead of a
1693
 * packed-pixel image.  The @ref TJPARAM "parameters" that describe the JPEG
1694
 * image will be set when this function returns.
1695
 *
1696
 * @param handle handle to a TurboJPEG instance that has been initialized for
1697
 * decompression
1698
 *
1699
 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1700
 * decompress
1701
 *
1702
 * @param jpegSize size of the JPEG image (in bytes)
1703
 *
1704
 * @param dstBuf pointer to a buffer that will receive the unified planar YUV
1705
 * decompressed image.  Use #tj3YUVBufSize() to determine the appropriate size
1706
 * for this buffer based on the scaled JPEG width and height (see #TJSCALED(),
1707
 * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()), row
1708
 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1709
 * Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
1710
 * buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1711
 *
1712
 * @param align row alignment (in bytes) of the YUV image (must be a power of
1713
 * 2.)  Setting this parameter to n will cause each row in each plane of the
1714
 * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
1715
 * To generate images suitable for X Video, `align` should be set to 4.
1716
 *
1717
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1718
 * and #tj3GetErrorCode().)
1719
 */
1720
DLLEXPORT int tj3DecompressToYUV8(tjhandle handle,
1721
                                  const unsigned char *jpegBuf,
1722
                                  size_t jpegSize,
1723
                                  unsigned char *dstBuf, int align);
1724
1725
1726
/**
1727
 * Decompress an 8-bit-per-sample JPEG image into separate 8-bit-per-sample Y,
1728
 * U (Cb), and V (Cr) image planes.  This function performs JPEG decompression
1729
 * but leaves out the color conversion step, so a planar YUV image is generated
1730
 * instead of a packed-pixel image.  The @ref TJPARAM "parameters" that
1731
 * describe the JPEG image will be set when this function returns.
1732
 *
1733
 * @param handle handle to a TurboJPEG instance that has been initialized for
1734
 * decompression
1735
 *
1736
 * @param jpegBuf pointer to a byte buffer containing the JPEG image to
1737
 * decompress
1738
 *
1739
 * @param jpegSize size of the JPEG image (in bytes)
1740
 *
1741
 * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1742
 * (or just a Y plane, if decompressing a grayscale image) that will receive
1743
 * the decompressed image.  These planes can be contiguous or non-contiguous in
1744
 * memory.  Use #tj3YUVPlaneSize() to determine the appropriate size for each
1745
 * plane based on the scaled JPEG width and height (see #TJSCALED(),
1746
 * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()),
1747
 * strides, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  Refer
1748
 * to @ref YUVnotes "YUV Image Format Notes" for more details.
1749
 *
1750
 * @param strides an array of integers, each specifying the number of bytes per
1751
 * row in the corresponding plane of the YUV image.  Setting the stride for any
1752
 * plane to 0 is the same as setting it to the scaled plane width (see
1753
 * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1754
 * strides for all planes will be set to their respective scaled plane widths.
1755
 * You can adjust the strides in order to add an arbitrary amount of row
1756
 * padding to each plane or to decompress the JPEG image into a subregion of a
1757
 * larger planar YUV image.
1758
 *
1759
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1760
 * and #tj3GetErrorCode().)
1761
 */
1762
DLLEXPORT int tj3DecompressToYUVPlanes8(tjhandle handle,
1763
                                        const unsigned char *jpegBuf,
1764
                                        size_t jpegSize,
1765
                                        unsigned char **dstPlanes,
1766
                                        int *strides);
1767
1768
1769
/**
1770
 * Decode an 8-bit-per-sample unified planar YUV image into an 8-bit-per-sample
1771
 * packed-pixel RGB or grayscale image.  This function performs color
1772
 * conversion (which is accelerated in the libjpeg-turbo implementation) but
1773
 * does not execute any of the other steps in the JPEG decompression process.
1774
 *
1775
 * @param handle handle to a TurboJPEG instance that has been initialized for
1776
 * decompression
1777
 *
1778
 * @param srcBuf pointer to a buffer containing a unified planar YUV source
1779
 * image to be decoded.  The size of this buffer should match the value
1780
 * returned by #tj3YUVBufSize() for the given image width, height, row
1781
 * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.)  The
1782
 * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
1783
 * source buffer.  (Refer to @ref YUVnotes "YUV Image Format Notes".)
1784
 *
1785
 * @param align row alignment (in bytes) of the YUV source image (must be a
1786
 * power of 2.)  Setting this parameter to n indicates that each row in each
1787
 * plane of the YUV source image is padded to the nearest multiple of n bytes
1788
 * (1 = unpadded.)
1789
 *
1790
 * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1791
 * image.  This buffer should normally be `pitch * height` bytes in size.
1792
 * However, you can also use this parameter to decode into a specific region of
1793
 * a larger buffer.
1794
 *
1795
 * @param width width (in pixels) of the source and destination images
1796
 *
1797
 * @param pitch bytes per row in the destination image.  Normally this should
1798
 * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1799
 * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
1800
 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
1801
 * also use this parameter to specify the row alignment/padding of the
1802
 * destination image, to skip rows, or to decode into a specific region of a
1803
 * larger buffer.
1804
 *
1805
 * @param height height (in pixels) of the source and destination images
1806
 *
1807
 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1808
 * "Pixel formats".)
1809
 *
1810
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1811
 * and #tj3GetErrorCode().)
1812
 */
1813
DLLEXPORT int tj3DecodeYUV8(tjhandle handle, const unsigned char *srcBuf,
1814
                            int align, unsigned char *dstBuf, int width,
1815
                            int pitch, int height, int pixelFormat);
1816
1817
1818
/**
1819
 * Decode a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into an
1820
 * 8-bit-per-sample packed-pixel RGB or grayscale image.  This function
1821
 * performs color conversion (which is accelerated in the libjpeg-turbo
1822
 * implementation) but does not execute any of the other steps in the JPEG
1823
 * decompression process.
1824
 *
1825
 * @param handle handle to a TurboJPEG instance that has been initialized for
1826
 * decompression
1827
 *
1828
 * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
1829
 * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
1830
 * to be decoded.  These planes can be contiguous or non-contiguous in memory.
1831
 * The size of each plane should match the value returned by #tj3YUVPlaneSize()
1832
 * for the given image width, height, strides, and level of chrominance
1833
 * subsampling (see #TJPARAM_SUBSAMP.)  Refer to @ref YUVnotes
1834
 * "YUV Image Format Notes" for more details.
1835
 *
1836
 * @param strides an array of integers, each specifying the number of bytes per
1837
 * row in the corresponding plane of the YUV source image.  Setting the stride
1838
 * for any plane to 0 is the same as setting it to the plane width (see
1839
 * @ref YUVnotes "YUV Image Format Notes".)  If `strides` is NULL, then the
1840
 * strides for all planes will be set to their respective plane widths.  You
1841
 * can adjust the strides in order to specify an arbitrary amount of row
1842
 * padding in each plane or to decode a subregion of a larger planar YUV image.
1843
 *
1844
 * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
1845
 * image.  This buffer should normally be `pitch * height` bytes in size.
1846
 * However, you can also use this parameter to decode into a specific region of
1847
 * a larger buffer.
1848
 *
1849
 * @param width width (in pixels) of the source and destination images
1850
 *
1851
 * @param pitch bytes per row in the destination image.  Normally this should
1852
 * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
1853
 * image should be unpadded.  (Setting this parameter to 0 is the equivalent of
1854
 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.)  However, you can
1855
 * also use this parameter to specify the row alignment/padding of the
1856
 * destination image, to skip rows, or to decode into a specific region of a
1857
 * larger buffer.
1858
 *
1859
 * @param height height (in pixels) of the source and destination images
1860
 *
1861
 * @param pixelFormat pixel format of the destination image (see @ref TJPF
1862
 * "Pixel formats".)
1863
 *
1864
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1865
 * and #tj3GetErrorCode().)
1866
 */
1867
DLLEXPORT int tj3DecodeYUVPlanes8(tjhandle handle,
1868
                                  const unsigned char * const *srcPlanes,
1869
                                  const int *strides, unsigned char *dstBuf,
1870
                                  int width, int pitch, int height,
1871
                                  int pixelFormat);
1872
1873
1874
/**
1875
 * Losslessly transform a JPEG image into another JPEG image.  Lossless
1876
 * transforms work by moving the raw DCT coefficients from one JPEG image
1877
 * structure to another without altering the values of the coefficients.  While
1878
 * this is typically faster than decompressing the image, transforming it, and
1879
 * re-compressing it, lossless transforms are not free.  Each lossless
1880
 * transform requires reading and performing entropy decoding on all of the
1881
 * coefficients in the source image, regardless of the size of the destination
1882
 * image.  Thus, this function provides a means of generating multiple
1883
 * transformed images from the same source or applying multiple transformations
1884
 * simultaneously, in order to eliminate the need to read the source
1885
 * coefficients multiple times.
1886
 *
1887
 * @param handle handle to a TurboJPEG instance that has been initialized for
1888
 * lossless transformation
1889
 *
1890
 * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
1891
 * transform
1892
 *
1893
 * @param jpegSize size of the JPEG source image (in bytes)
1894
 *
1895
 * @param n the number of transformed JPEG images to generate
1896
 *
1897
 * @param dstBufs pointer to an array of n byte buffers.  `dstBufs[i]` will
1898
 * receive a JPEG image that has been transformed using the parameters in
1899
 * `transforms[i]`.  TurboJPEG has the ability to reallocate the JPEG
1900
 * destination buffer to accommodate the size of the transformed JPEG image.
1901
 * Thus, you can choose to:
1902
 * -# pre-allocate the JPEG destination buffer with an arbitrary size using
1903
 * #tj3Alloc() and let TurboJPEG grow the buffer as needed,
1904
 * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
1905
 * you, or
1906
 * -# pre-allocate the buffer to a "worst case" size determined by calling
1907
 * #tj3JPEGBufSize() with the transformed or cropped width and height and the
1908
 * level of subsampling used in the source image.  Under normal circumstances,
1909
 * this should ensure that the buffer never has to be re-allocated.  (Setting
1910
 * #TJPARAM_NOREALLOC guarantees that it won't be.)  Note, however, that there
1911
 * are some rare cases (such as transforming images with a large amount of
1912
 * embedded EXIF or ICC profile data) in which the transformed JPEG image will
1913
 * be larger than the worst-case size, and #TJPARAM_NOREALLOC cannot be used in
1914
 * those cases.
1915
 * .
1916
 * If you choose option 1, then `dstSizes[i]` should be set to the size of your
1917
 * pre-allocated buffer.  In any case, unless you have set #TJPARAM_NOREALLOC,
1918
 * you should always check `dstBufs[i]` upon return from this function, as it
1919
 * may have changed.
1920
 *
1921
 * @param dstSizes pointer to an array of n size_t variables that will receive
1922
 * the actual sizes (in bytes) of each transformed JPEG image.  If `dstBufs[i]`
1923
 * points to a pre-allocated buffer, then `dstSizes[i]` should be set to the
1924
 * size of the buffer.  Upon return, `dstSizes[i]` will contain the size of the
1925
 * transformed JPEG image (in bytes.)
1926
 *
1927
 * @param transforms pointer to an array of n #tjtransform structures, each of
1928
 * which specifies the transform parameters and/or cropping region for the
1929
 * corresponding transformed JPEG image.
1930
 *
1931
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
1932
 * and #tj3GetErrorCode().)
1933
 */
1934
DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
1935
                           size_t jpegSize, int n, unsigned char **dstBufs,
1936
                           size_t *dstSizes, const tjtransform *transforms);
1937
1938
1939
/**
1940
 * Destroy a TurboJPEG instance.
1941
 *
1942
 * @param handle handle to a TurboJPEG instance.  If the handle is NULL, then
1943
 * this function has no effect.
1944
 */
1945
DLLEXPORT void tj3Destroy(tjhandle handle);
1946
1947
1948
/**
1949
 * Allocate a byte buffer for use with TurboJPEG.  You should always use this
1950
 * function to allocate the JPEG destination buffer(s) for the compression and
1951
 * transform functions unless you are disabling automatic buffer (re)allocation
1952
 * (by setting #TJPARAM_NOREALLOC.)
1953
 *
1954
 * @param bytes the number of bytes to allocate
1955
 *
1956
 * @return a pointer to a newly-allocated buffer with the specified number of
1957
 * bytes.
1958
 *
1959
 * @see tj3Free()
1960
 */
1961
DLLEXPORT void *tj3Alloc(size_t bytes);
1962
1963
1964
/**
1965
 * Load an 8-bit-per-sample packed-pixel image from disk into memory.
1966
 *
1967
 * @param handle handle to a TurboJPEG instance
1968
 *
1969
 * @param filename name of a file containing a packed-pixel image in Windows
1970
 * BMP or PBMPLUS (PPM/PGM) format.  Windows BMP files require 8-bit-per-sample
1971
 * data precision.  If the data precision of the PBMPLUS file does not match
1972
 * the target data precision, then upconverting or downconverting will be
1973
 * performed.
1974
 *
1975
 * @param width pointer to an integer variable that will receive the width (in
1976
 * pixels) of the packed-pixel image
1977
 *
1978
 * @param align row alignment (in samples) of the packed-pixel buffer to be
1979
 * returned (must be a power of 2.)  Setting this parameter to n will cause all
1980
 * rows in the buffer to be padded to the nearest multiple of n samples
1981
 * (1 = unpadded.)
1982
 *
1983
 * @param height pointer to an integer variable that will receive the height
1984
 * (in pixels) of the packed-pixel image
1985
 *
1986
 * @param pixelFormat pointer to an integer variable that specifies or will
1987
 * receive the pixel format of the packed-pixel buffer.  The behavior of this
1988
 * function will vary depending on the value of `*pixelFormat` passed to the
1989
 * function:
1990
 * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
1991
 * use the most optimal pixel format for the file type, and `*pixelFormat` will
1992
 * contain the ID of that pixel format upon successful return from this
1993
 * function.
1994
 * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
1995
 * grayscale colormap can be loaded.
1996
 * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
1997
 * converted using a quick & dirty algorithm that is suitable only for testing
1998
 * purposes.  (Proper conversion between CMYK and other formats requires a
1999
 * color management system.)
2000
 * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
2001
 * specified pixel format, and pixel format conversion will be performed if
2002
 * necessary.
2003
 *
2004
 * @return a pointer to a newly-allocated buffer containing the packed-pixel
2005
 * image, converted to the chosen pixel format and with the chosen row
2006
 * alignment, or NULL if an error occurred (see #tj3GetErrorStr().)  This
2007
 * buffer should be freed using #tj3Free().
2008
 */
2009
DLLEXPORT unsigned char *tj3LoadImage8(tjhandle handle, const char *filename,
2010
                                       int *width, int align, int *height,
2011
                                       int *pixelFormat);
2012
2013
/**
2014
 * Load a 12-bit-per-sample packed-pixel image from disk into memory.
2015
 *
2016
 * \details \copydetails tj3LoadImage8()
2017
 */
2018
DLLEXPORT short *tj3LoadImage12(tjhandle handle, const char *filename,
2019
                                int *width, int align, int *height,
2020
                                int *pixelFormat);
2021
2022
/**
2023
 * Load a 16-bit-per-sample packed-pixel image from disk into memory.
2024
 *
2025
 * \details \copydetails tj3LoadImage8()
2026
 */
2027
DLLEXPORT unsigned short *tj3LoadImage16(tjhandle handle, const char *filename,
2028
                                         int *width, int align, int *height,
2029
                                         int *pixelFormat);
2030
2031
2032
/**
2033
 * Save an 8-bit-per-sample packed-pixel image from memory to disk.
2034
 *
2035
 * @param handle handle to a TurboJPEG instance
2036
 *
2037
 * @param filename name of a file to which to save the packed-pixel image.  The
2038
 * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
2039
 * on the file extension.  Windows BMP files require 8-bit-per-sample data
2040
 * precision.
2041
 *
2042
 * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
2043
 * or CMYK image to be saved
2044
 *
2045
 * @param width width (in pixels) of the packed-pixel image
2046
 *
2047
 * @param pitch samples per row in the packed-pixel image.  Setting this
2048
 * parameter to 0 is the equivalent of setting it to
2049
 * <tt>width * #tjPixelSize[pixelFormat]</tt>.
2050
 *
2051
 * @param height height (in pixels) of the packed-pixel image
2052
 *
2053
 * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
2054
 * "Pixel formats".)  If this parameter is set to @ref TJPF_GRAY, then the
2055
 * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
2056
 * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
2057
 * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
2058
 * converted to RGB using a quick & dirty algorithm that is suitable only for
2059
 * testing purposes.  (Proper conversion between CMYK and other formats
2060
 * requires a color management system.)
2061
 *
2062
 * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
2063
 */
2064
DLLEXPORT int tj3SaveImage8(tjhandle handle, const char *filename,
2065
                            const unsigned char *buffer, int width, int pitch,
2066
                            int height, int pixelFormat);
2067
2068
/**
2069
 * Save a 12-bit-per-sample packed-pixel image from memory to disk.
2070
 *
2071
 * \details \copydetails tj3SaveImage8()
2072
 */
2073
DLLEXPORT int tj3SaveImage12(tjhandle handle, const char *filename,
2074
                             const short *buffer, int width, int pitch,
2075
                             int height, int pixelFormat);
2076
2077
/**
2078
 * Save a 16-bit-per-sample packed-pixel image from memory to disk.
2079
 *
2080
 * \details \copydetails tj3SaveImage8()
2081
 */
2082
DLLEXPORT int tj3SaveImage16(tjhandle handle, const char *filename,
2083
                             const unsigned short *buffer, int width,
2084
                             int pitch, int height, int pixelFormat);
2085
2086
2087
/**
2088
 * Free a byte buffer previously allocated by TurboJPEG.  You should always use
2089
 * this function to free JPEG destination buffer(s) that were automatically
2090
 * (re)allocated by the compression and transform functions or that were
2091
 * manually allocated using #tj3Alloc().
2092
 *
2093
 * @param buffer address of the buffer to free.  If the address is NULL, then
2094
 * this function has no effect.
2095
 *
2096
 * @see tj3Alloc()
2097
 */
2098
DLLEXPORT void tj3Free(void *buffer);
2099
2100
2101
/**
2102
 * Returns a descriptive error message explaining why the last command failed.
2103
 *
2104
 * @param handle handle to a TurboJPEG instance, or NULL if the error was
2105
 * generated by a global function (but note that retrieving the error message
2106
 * for a global function is thread-safe only on platforms that support
2107
 * thread-local storage.)
2108
 *
2109
 * @return a descriptive error message explaining why the last command failed.
2110
 */
2111
DLLEXPORT char *tj3GetErrorStr(tjhandle handle);
2112
2113
2114
/**
2115
 * Returns a code indicating the severity of the last error.  See
2116
 * @ref TJERR "Error codes".
2117
 *
2118
 * @param handle handle to a TurboJPEG instance
2119
 *
2120
 * @return a code indicating the severity of the last error.  See
2121
 * @ref TJERR "Error codes".
2122
 */
2123
DLLEXPORT int tj3GetErrorCode(tjhandle handle);
2124
2125
2126
/* Backward compatibility functions and macros (nothing to see here) */
2127
2128
/* TurboJPEG 1.0+ */
2129
2130
#define NUMSUBOPT  TJ_NUMSAMP
2131
#define TJ_444  TJSAMP_444
2132
#define TJ_422  TJSAMP_422
2133
#define TJ_420  TJSAMP_420
2134
#define TJ_411  TJSAMP_420
2135
#define TJ_GRAYSCALE  TJSAMP_GRAY
2136
2137
0
#define TJ_BGR  1
2138
#define TJ_BOTTOMUP  TJFLAG_BOTTOMUP
2139
#define TJ_FORCEMMX  TJFLAG_FORCEMMX
2140
#define TJ_FORCESSE  TJFLAG_FORCESSE
2141
#define TJ_FORCESSE2  TJFLAG_FORCESSE2
2142
0
#define TJ_ALPHAFIRST  64
2143
#define TJ_FORCESSE3  TJFLAG_FORCESSE3
2144
#define TJ_FASTUPSAMPLE  TJFLAG_FASTUPSAMPLE
2145
2146
#define TJPAD(width)  (((width) + 3) & (~3))
2147
2148
DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
2149
2150
DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
2151
                         int pitch, int height, int pixelSize,
2152
                         unsigned char *dstBuf, unsigned long *compressedSize,
2153
                         int jpegSubsamp, int jpegQual, int flags);
2154
2155
DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
2156
                           unsigned long jpegSize, unsigned char *dstBuf,
2157
                           int width, int pitch, int height, int pixelSize,
2158
                           int flags);
2159
2160
DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
2161
                                 unsigned long jpegSize, int *width,
2162
                                 int *height);
2163
2164
DLLEXPORT int tjDestroy(tjhandle handle);
2165
2166
DLLEXPORT char *tjGetErrorStr(void);
2167
2168
DLLEXPORT tjhandle tjInitCompress(void);
2169
2170
DLLEXPORT tjhandle tjInitDecompress(void);
2171
2172
/* TurboJPEG 1.1+ */
2173
2174
0
#define TJ_YUV  512
2175
2176
DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
2177
2178
DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
2179
                                  unsigned long jpegSize, int *width,
2180
                                  int *height, int *jpegSubsamp);
2181
2182
DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
2183
                                unsigned long jpegSize, unsigned char *dstBuf,
2184
                                int flags);
2185
2186
DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
2187
                          int pitch, int height, int pixelSize,
2188
                          unsigned char *dstBuf, int subsamp, int flags);
2189
2190
/* TurboJPEG 1.2+ */
2191
2192
0
#define TJFLAG_BOTTOMUP  2
2193
0
#define TJFLAG_FORCEMMX  8
2194
0
#define TJFLAG_FORCESSE  16
2195
0
#define TJFLAG_FORCESSE2  32
2196
#define TJFLAG_FORCESSE3  128
2197
0
#define TJFLAG_FASTUPSAMPLE  256
2198
0
#define TJFLAG_NOREALLOC  1024
2199
2200
DLLEXPORT unsigned char *tjAlloc(int bytes);
2201
2202
DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
2203
2204
DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
2205
2206
DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
2207
                          int width, int pitch, int height, int pixelFormat,
2208
                          unsigned char **jpegBuf, unsigned long *jpegSize,
2209
                          int jpegSubsamp, int jpegQual, int flags);
2210
2211
DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
2212
                            unsigned long jpegSize, unsigned char *dstBuf,
2213
                            int width, int pitch, int height, int pixelFormat,
2214
                            int flags);
2215
2216
DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
2217
                           int pitch, int height, int pixelFormat,
2218
                           unsigned char *dstBuf, int subsamp, int flags);
2219
2220
DLLEXPORT void tjFree(unsigned char *buffer);
2221
2222
DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
2223
2224
DLLEXPORT tjhandle tjInitTransform(void);
2225
2226
DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
2227
                            unsigned long jpegSize, int n,
2228
                            unsigned char **dstBufs, unsigned long *dstSizes,
2229
                            tjtransform *transforms, int flags);
2230
2231
/* TurboJPEG 1.2.1+ */
2232
2233
0
#define TJFLAG_FASTDCT  2048
2234
0
#define TJFLAG_ACCURATEDCT  4096
2235
2236
/* TurboJPEG 1.4+ */
2237
2238
DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
2239
                                      int subsamp);
2240
2241
DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
2242
                                int width, int align, int height, int subsamp,
2243
                                unsigned char **jpegBuf,
2244
                                unsigned long *jpegSize, int jpegQual,
2245
                                int flags);
2246
2247
DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
2248
                                      const unsigned char **srcPlanes,
2249
                                      int width, const int *strides,
2250
                                      int height, int subsamp,
2251
                                      unsigned char **jpegBuf,
2252
                                      unsigned long *jpegSize, int jpegQual,
2253
                                      int flags);
2254
2255
DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
2256
                          int align, int subsamp, unsigned char *dstBuf,
2257
                          int width, int pitch, int height, int pixelFormat,
2258
                          int flags);
2259
2260
DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
2261
                                const unsigned char **srcPlanes,
2262
                                const int *strides, int subsamp,
2263
                                unsigned char *dstBuf, int width, int pitch,
2264
                                int height, int pixelFormat, int flags);
2265
2266
DLLEXPORT int tjDecompressHeader3(tjhandle handle,
2267
                                  const unsigned char *jpegBuf,
2268
                                  unsigned long jpegSize, int *width,
2269
                                  int *height, int *jpegSubsamp,
2270
                                  int *jpegColorspace);
2271
2272
DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
2273
                                 unsigned long jpegSize, unsigned char *dstBuf,
2274
                                 int width, int align, int height, int flags);
2275
2276
DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
2277
                                      const unsigned char *jpegBuf,
2278
                                      unsigned long jpegSize,
2279
                                      unsigned char **dstPlanes, int width,
2280
                                      int *strides, int height, int flags);
2281
2282
DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
2283
                           int width, int pitch, int height, int pixelFormat,
2284
                           unsigned char *dstBuf, int align, int subsamp,
2285
                           int flags);
2286
2287
DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
2288
                                int width, int pitch, int height,
2289
                                int pixelFormat, unsigned char **dstPlanes,
2290
                                int *strides, int subsamp, int flags);
2291
2292
DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
2293
2294
DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
2295
                                       int height, int subsamp);
2296
2297
DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
2298
2299
/* TurboJPEG 2.0+ */
2300
2301
0
#define TJFLAG_STOPONWARNING  8192
2302
0
#define TJFLAG_PROGRESSIVE  16384
2303
2304
DLLEXPORT int tjGetErrorCode(tjhandle handle);
2305
2306
DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
2307
2308
DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
2309
                                     int align, int *height, int *pixelFormat,
2310
                                     int flags);
2311
2312
DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
2313
                          int width, int pitch, int height, int pixelFormat,
2314
                          int flags);
2315
2316
/* TurboJPEG 2.1+ */
2317
2318
0
#define TJFLAG_LIMITSCANS  32768
2319
2320
/**
2321
 * @}
2322
 */
2323
2324
#ifdef __cplusplus
2325
}
2326
#endif
2327
2328
#endif