~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/libjpeg_turbo/src/turbojpeg.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C)2009-2012 D. R. Commander.  All Rights Reserved.
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions are met:
6
 
 *
7
 
 * - Redistributions of source code must retain the above copyright notice,
8
 
 *   this list of conditions and the following disclaimer.
9
 
 * - Redistributions in binary form must reproduce the above copyright notice,
10
 
 *   this list of conditions and the following disclaimer in the documentation
11
 
 *   and/or other materials provided with the distribution.
12
 
 * - Neither the name of the libjpeg-turbo Project nor the names of its
13
 
 *   contributors may be used to endorse or promote products derived from this
14
 
 *   software without specific prior written permission.
15
 
 *
16
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 
 * POSSIBILITY OF SUCH DAMAGE.
27
 
 */
28
 
 
29
 
#ifndef __TURBOJPEG_H__
30
 
#define __TURBOJPEG_H__
31
 
 
32
 
#if defined(_WIN32) && defined(DLLDEFINE)
33
 
#define DLLEXPORT __declspec(dllexport)
34
 
#else
35
 
#define DLLEXPORT
36
 
#endif
37
 
#define DLLCALL
38
 
 
39
 
 
40
 
/**
41
 
 * @addtogroup TurboJPEG
42
 
 * TurboJPEG API.  This API provides an interface for generating, decoding, and
43
 
 * transforming planar YUV and JPEG images in memory.
44
 
 *
45
 
 * @{
46
 
 */
47
 
 
48
 
 
49
 
/**
50
 
 * The number of chrominance subsampling options
51
 
 */
52
 
#define TJ_NUMSAMP 5
53
 
 
54
 
/**
55
 
 * Chrominance subsampling options.
56
 
 * When an image is converted from the RGB to the YCbCr colorspace as part of
57
 
 * the JPEG compression process, some of the Cb and Cr (chrominance) components
58
 
 * can be discarded or averaged together to produce a smaller image with little
59
 
 * perceptible loss of image clarity (the human eye is more sensitive to small
60
 
 * changes in brightness than small changes in color.)  This is called
61
 
 * "chrominance subsampling".
62
 
 */
63
 
enum TJSAMP
64
 
{
65
 
  /**
66
 
   * 4:4:4 chrominance subsampling (no chrominance subsampling).  The JPEG or
67
 
   * YUV image will contain one chrominance component for every pixel in the
68
 
   * source image.
69
 
   */
70
 
  TJSAMP_444=0,
71
 
  /**
72
 
   * 4:2:2 chrominance subsampling.  The JPEG or YUV image will contain one
73
 
   * chrominance component for every 2x1 block of pixels in the source image.
74
 
   */
75
 
  TJSAMP_422,
76
 
  /**
77
 
   * 4:2:0 chrominance subsampling.  The JPEG or YUV image will contain one
78
 
   * chrominance component for every 2x2 block of pixels in the source image.
79
 
   */
80
 
  TJSAMP_420,
81
 
  /**
82
 
   * Grayscale.  The JPEG or YUV image will contain no chrominance components.
83
 
   */
84
 
  TJSAMP_GRAY,
85
 
  /**
86
 
   * 4:4:0 chrominance subsampling.  The JPEG or YUV image will contain one
87
 
   * chrominance component for every 1x2 block of pixels in the source image.
88
 
   */
89
 
  TJSAMP_440
90
 
};
91
 
 
92
 
/**
93
 
 * MCU block width (in pixels) for a given level of chrominance subsampling.
94
 
 * MCU block sizes:
95
 
 * - 8x8 for no subsampling or grayscale
96
 
 * - 16x8 for 4:2:2
97
 
 * - 8x16 for 4:4:0
98
 
 * - 16x16 for 4:2:0 
99
 
 */
100
 
static const int tjMCUWidth[TJ_NUMSAMP]  = {8, 16, 16, 8, 8};
101
 
 
102
 
/**
103
 
 * MCU block height (in pixels) for a given level of chrominance subsampling.
104
 
 * MCU block sizes:
105
 
 * - 8x8 for no subsampling or grayscale
106
 
 * - 16x8 for 4:2:2
107
 
 * - 8x16 for 4:4:0
108
 
 * - 16x16 for 4:2:0 
109
 
 */
110
 
static const int tjMCUHeight[TJ_NUMSAMP] = {8, 8, 16, 8, 16};
111
 
 
112
 
 
113
 
/**
114
 
 * The number of pixel formats
115
 
 */
116
 
#define TJ_NUMPF 11
117
 
 
118
 
/**
119
 
 * Pixel formats
120
 
 */
121
 
enum TJPF
122
 
{
123
 
  /**
124
 
   * RGB pixel format.  The red, green, and blue components in the image are
125
 
   * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
126
 
   * address within each pixel.
127
 
   */
128
 
  TJPF_RGB=0,
129
 
  /**
130
 
   * BGR pixel format.  The red, green, and blue components in the image are
131
 
   * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
132
 
   * address within each pixel.
133
 
   */
134
 
  TJPF_BGR,
135
 
  /**
136
 
   * RGBX pixel format.  The red, green, and blue components in the image are
137
 
   * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
138
 
   * address within each pixel.  The X component is ignored when compressing
139
 
   * and undefined when decompressing.
140
 
   */
141
 
  TJPF_RGBX,
142
 
  /**
143
 
   * BGRX pixel format.  The red, green, and blue components in the image are
144
 
   * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
145
 
   * address within each pixel.  The X component is ignored when compressing
146
 
   * and undefined when decompressing.
147
 
   */
148
 
  TJPF_BGRX,
149
 
  /**
150
 
   * XBGR pixel format.  The red, green, and blue components in the image are
151
 
   * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
152
 
   * address within each pixel.  The X component is ignored when compressing
153
 
   * and undefined when decompressing.
154
 
   */
155
 
  TJPF_XBGR,
156
 
  /**
157
 
   * XRGB pixel format.  The red, green, and blue components in the image are
158
 
   * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
159
 
   * address within each pixel.  The X component is ignored when compressing
160
 
   * and undefined when decompressing.
161
 
   */
162
 
  TJPF_XRGB,
163
 
  /**
164
 
   * Grayscale pixel format.  Each 1-byte pixel represents a luminance
165
 
   * (brightness) level from 0 to 255.
166
 
   */
167
 
  TJPF_GRAY,
168
 
  /**
169
 
   * RGBA pixel format.  This is the same as @ref TJPF_RGBX, except that when
170
 
   * decompressing, the X component is guaranteed to be 0xFF, which can be
171
 
   * interpreted as an opaque alpha channel.
172
 
   */
173
 
  TJPF_RGBA,
174
 
  /**
175
 
   * BGRA pixel format.  This is the same as @ref TJPF_BGRX, except that when
176
 
   * decompressing, the X component is guaranteed to be 0xFF, which can be
177
 
   * interpreted as an opaque alpha channel.
178
 
   */
179
 
  TJPF_BGRA,
180
 
  /**
181
 
   * ABGR pixel format.  This is the same as @ref TJPF_XBGR, except that when
182
 
   * decompressing, the X component is guaranteed to be 0xFF, which can be
183
 
   * interpreted as an opaque alpha channel.
184
 
   */
185
 
  TJPF_ABGR,
186
 
  /**
187
 
   * ARGB pixel format.  This is the same as @ref TJPF_XRGB, except that when
188
 
   * decompressing, the X component is guaranteed to be 0xFF, which can be
189
 
   * interpreted as an opaque alpha channel.
190
 
   */
191
 
  TJPF_ARGB
192
 
};
193
 
 
194
 
/**
195
 
 * Red offset (in bytes) for a given pixel format.  This specifies the number
196
 
 * of bytes that the red component is offset from the start of the pixel.  For
197
 
 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
198
 
 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>.
199
 
 */
200
 
static const int tjRedOffset[TJ_NUMPF] = {0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1};
201
 
/**
202
 
 * Green offset (in bytes) for a given pixel format.  This specifies the number
203
 
 * of bytes that the green component is offset from the start of the pixel.
204
 
 * For instance, if a pixel of format TJ_BGRX is stored in
205
 
 * <tt>char pixel[]</tt>, then the green component will be
206
 
 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>.
207
 
 */
208
 
static const int tjGreenOffset[TJ_NUMPF] = {1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2};
209
 
/**
210
 
 * Blue offset (in bytes) for a given pixel format.  This specifies the number
211
 
 * of bytes that the Blue component is offset from the start of the pixel.  For
212
 
 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
213
 
 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>.
214
 
 */
215
 
static const int tjBlueOffset[TJ_NUMPF] = {2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3};
216
 
 
217
 
/**
218
 
 * Pixel size (in bytes) for a given pixel format.
219
 
 */
220
 
static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4};
221
 
 
222
 
 
223
 
/**
224
 
 * The uncompressed source/destination image is stored in bottom-up (Windows,
225
 
 * OpenGL) order, not top-down (X11) order.
226
 
 */
227
 
#define TJFLAG_BOTTOMUP        2
228
 
/**
229
 
 * Turn off CPU auto-detection and force TurboJPEG to use MMX code (if the
230
 
 * underlying codec supports it.)
231
 
 */
232
 
#define TJFLAG_FORCEMMX        8
233
 
/**
234
 
 * Turn off CPU auto-detection and force TurboJPEG to use SSE code (if the
235
 
 * underlying codec supports it.)
236
 
 */
237
 
#define TJFLAG_FORCESSE       16
238
 
/**
239
 
 * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (if the
240
 
 * underlying codec supports it.)
241
 
 */
242
 
#define TJFLAG_FORCESSE2      32
243
 
/**
244
 
 * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (if the
245
 
 * underlying codec supports it.)
246
 
 */
247
 
#define TJFLAG_FORCESSE3     128
248
 
/**
249
 
 * When decompressing, use the fastest chrominance upsampling algorithm
250
 
 * available in the underlying codec.  The default is to use smooth upsampling,
251
 
 * which creates a smooth transition between neighboring chrominance components
252
 
 * in order to reduce upsampling artifacts in the decompressed image.
253
 
 */
254
 
#define TJFLAG_FASTUPSAMPLE  256
255
 
/**
256
 
 * Disable buffer (re)allocation.  If passed to #tjCompress2() or
257
 
 * #tjTransform(), this flag will cause those functions to generate an error if
258
 
 * the JPEG image buffer is invalid or too small rather than attempting to
259
 
 * allocate or reallocate that buffer.  This reproduces the behavior of earlier
260
 
 * versions of TurboJPEG.
261
 
 */
262
 
#define TJFLAG_NOREALLOC     1024
263
 
/**
264
 
 * Use the fastest DCT/IDCT algorithm available in the underlying codec.  The
265
 
 * default if this flag is not specified is implementation-specific.  The
266
 
 * libjpeg implementation, for example, uses the fast algorithm by default when
267
 
 * compressing, because this has been shown to have only a very slight effect
268
 
 * on accuracy, but it uses the accurate algorithm when decompressing, because
269
 
 * this has been shown to have a larger effect.
270
 
 */
271
 
#define TJFLAG_FASTDCT       2048
272
 
/**
273
 
 * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
274
 
 * The default if this flag is not specified is implementation-specific.  The
275
 
 * libjpeg implementation, for example, uses the fast algorithm by default when
276
 
 * compressing, because this has been shown to have only a very slight effect
277
 
 * on accuracy, but it uses the accurate algorithm when decompressing, because
278
 
 * this has been shown to have a larger effect.
279
 
 */
280
 
#define TJFLAG_ACCURATEDCT   4096
281
 
 
282
 
 
283
 
/**
284
 
 * Number of transform operations
285
 
 */
286
 
#define TJ_NUMXOP 8
287
 
 
288
 
/**
289
 
 * Transform operations for #tjTransform()
290
 
 */
291
 
enum TJXOP
292
 
{
293
 
  /**
294
 
   * Do not transform the position of the image pixels
295
 
   */
296
 
  TJXOP_NONE=0,
297
 
  /**
298
 
   * Flip (mirror) image horizontally.  This transform is imperfect if there
299
 
   * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
300
 
   */
301
 
  TJXOP_HFLIP,
302
 
  /**
303
 
   * Flip (mirror) image vertically.  This transform is imperfect if there are
304
 
   * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
305
 
   */
306
 
  TJXOP_VFLIP,
307
 
  /**
308
 
   * Transpose image (flip/mirror along upper left to lower right axis.)  This
309
 
   * transform is always perfect.
310
 
   */
311
 
  TJXOP_TRANSPOSE,
312
 
  /**
313
 
   * Transverse transpose image (flip/mirror along upper right to lower left
314
 
   * axis.)  This transform is imperfect if there are any partial MCU blocks in
315
 
   * the image (see #TJXOPT_PERFECT.)
316
 
   */
317
 
  TJXOP_TRANSVERSE,
318
 
  /**
319
 
   * Rotate image clockwise by 90 degrees.  This transform is imperfect if
320
 
   * there are any partial MCU blocks on the bottom edge (see
321
 
   * #TJXOPT_PERFECT.)
322
 
   */
323
 
  TJXOP_ROT90,
324
 
  /**
325
 
   * Rotate image 180 degrees.  This transform is imperfect if there are any
326
 
   * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
327
 
   */
328
 
  TJXOP_ROT180,
329
 
  /**
330
 
   * Rotate image counter-clockwise by 90 degrees.  This transform is imperfect
331
 
   * if there are any partial MCU blocks on the right edge (see
332
 
   * #TJXOPT_PERFECT.)
333
 
   */
334
 
  TJXOP_ROT270
335
 
};
336
 
 
337
 
 
338
 
/**
339
 
 * This option will cause #tjTransform() to return an error if the transform is
340
 
 * not perfect.  Lossless transforms operate on MCU blocks, whose size depends
341
 
 * on the level of chrominance subsampling used (see #tjMCUWidth
342
 
 * and #tjMCUHeight.)  If the image's width or height is not evenly divisible
343
 
 * by the MCU block size, then there will be partial MCU blocks on the right
344
 
 * and/or bottom edges.  It is not possible to move these partial MCU blocks to
345
 
 * the top or left of the image, so any transform that would require that is
346
 
 * "imperfect."  If this option is not specified, then any partial MCU blocks
347
 
 * that cannot be transformed will be left in place, which will create
348
 
 * odd-looking strips on the right or bottom edge of the image.
349
 
 */
350
 
#define TJXOPT_PERFECT  1
351
 
/**
352
 
 * This option will cause #tjTransform() to discard any partial MCU blocks that
353
 
 * cannot be transformed.
354
 
 */
355
 
#define TJXOPT_TRIM     2
356
 
/**
357
 
 * This option will enable lossless cropping.  See #tjTransform() for more
358
 
 * information.
359
 
 */
360
 
#define TJXOPT_CROP     4
361
 
/**
362
 
 * This option will discard the color data in the input image and produce
363
 
 * a grayscale output image.
364
 
 */
365
 
#define TJXOPT_GRAY     8
366
 
/**
367
 
 * This option will prevent #tjTransform() from outputting a JPEG image for
368
 
 * this particular transform (this can be used in conjunction with a custom
369
 
 * filter to capture the transformed DCT coefficients without transcoding
370
 
 * them.)
371
 
 */
372
 
#define TJXOPT_NOOUTPUT 16
373
 
 
374
 
 
375
 
/**
376
 
 * Scaling factor
377
 
 */
378
 
typedef struct
379
 
{
380
 
  /**
381
 
   * Numerator
382
 
   */
383
 
  int num;
384
 
  /**
385
 
   * Denominator
386
 
   */
387
 
  int denom;
388
 
} tjscalingfactor;
389
 
 
390
 
/**
391
 
 * Cropping region
392
 
 */
393
 
typedef struct
394
 
{
395
 
  /**
396
 
   * The left boundary of the cropping region.  This must be evenly divisible
397
 
   * by the MCU block width (see #tjMCUWidth.)
398
 
   */
399
 
  int x;
400
 
  /**
401
 
   * The upper boundary of the cropping region.  This must be evenly divisible
402
 
   * by the MCU block height (see #tjMCUHeight.)
403
 
   */
404
 
  int y;
405
 
  /**
406
 
   * The width of the cropping region. Setting this to 0 is the equivalent of
407
 
   * setting it to the width of the source JPEG image - x.
408
 
   */
409
 
  int w;
410
 
  /**
411
 
   * The height of the cropping region. Setting this to 0 is the equivalent of
412
 
   * setting it to the height of the source JPEG image - y.
413
 
   */
414
 
  int h;
415
 
} tjregion;
416
 
 
417
 
/**
418
 
 * Lossless transform
419
 
 */
420
 
typedef struct tjtransform
421
 
{
422
 
  /**
423
 
   * Cropping region
424
 
   */
425
 
  tjregion r;
426
 
  /**
427
 
   * One of the @ref TJXOP "transform operations"
428
 
   */
429
 
  int op;
430
 
  /**
431
 
   * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
432
 
   */
433
 
  int options;
434
 
  /**
435
 
   * Arbitrary data that can be accessed within the body of the callback
436
 
   * function
437
 
   */
438
 
  void *data;
439
 
  /**
440
 
   * A callback function that can be used to modify the DCT coefficients
441
 
   * after they are losslessly transformed but before they are transcoded to a
442
 
   * new JPEG file.  This allows for custom filters or other transformations to
443
 
   * be applied in the frequency domain.
444
 
   *
445
 
   * @param coeffs pointer to an array of transformed DCT coefficients.  (NOTE:
446
 
   *        this pointer is not guaranteed to be valid once the callback
447
 
   *        returns, so applications wishing to hand off the DCT coefficients
448
 
   *        to another function or library should make a copy of them within
449
 
   *        the body of the callback.)
450
 
   * @param arrayRegion #tjregion structure containing the width and height of
451
 
   *        the array pointed to by <tt>coeffs</tt> as well as its offset
452
 
   *        relative to the component plane.  TurboJPEG implementations may
453
 
   *        choose to split each component plane into multiple DCT coefficient
454
 
   *        arrays and call the callback function once for each array.
455
 
   * @param planeRegion #tjregion structure containing the width and height of
456
 
   *        the component plane to which <tt>coeffs</tt> belongs
457
 
   * @param componentID ID number of the component plane to which
458
 
   *        <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of
459
 
   *        0, 1, and 2 in typical JPEG images.)
460
 
   * @param transformID ID number of the transformed image to which
461
 
   *        <tt>coeffs</tt> belongs.  This is the same as the index of the
462
 
   *        transform in the transforms array that was passed to
463
 
   *        #tjTransform().
464
 
   * @param transform a pointer to a #tjtransform structure that specifies the
465
 
   *        parameters and/or cropping region for this transform
466
 
   *
467
 
   * @return 0 if the callback was successful, or -1 if an error occurred.
468
 
   */
469
 
  int (*customFilter)(short *coeffs, tjregion arrayRegion,
470
 
    tjregion planeRegion, int componentIndex, int transformIndex,
471
 
    struct tjtransform *transform);
472
 
} tjtransform;
473
 
 
474
 
/**
475
 
 * TurboJPEG instance handle
476
 
 */
477
 
typedef void* tjhandle;
478
 
 
479
 
 
480
 
/**
481
 
 * Pad the given width to the nearest 32-bit boundary
482
 
 */
483
 
#define TJPAD(width) (((width)+3)&(~3))
484
 
 
485
 
/**
486
 
 * Compute the scaled value of <tt>dimension</tt> using the given scaling
487
 
 * factor.  This macro performs the integer equivalent of <tt>ceil(dimension *
488
 
 * scalingFactor)</tt>. 
489
 
 */
490
 
#define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \
491
 
  + scalingFactor.denom - 1) / scalingFactor.denom)
492
 
 
493
 
 
494
 
#ifdef __cplusplus
495
 
extern "C" {
496
 
#endif
497
 
 
498
 
 
499
 
/**
500
 
 * Create a TurboJPEG compressor instance.
501
 
 *
502
 
 * @return a handle to the newly-created instance, or NULL if an error
503
 
 * occurred (see #tjGetErrorStr().)
504
 
 */
505
 
DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
506
 
 
507
 
 
508
 
/**
509
 
 * Compress an RGB or grayscale image into a JPEG image.
510
 
 *
511
 
 * @param handle a handle to a TurboJPEG compressor or transformer instance
512
 
 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
513
 
 *        to be compressed
514
 
 * @param width width (in pixels) of the source image
515
 
 * @param pitch bytes per line of the source image.  Normally, this should be
516
 
 *        <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
517
 
 *        or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
518
 
 *        the image is padded to the nearest 32-bit boundary, as is the case
519
 
 *        for Windows bitmaps.  You can also be clever and use this parameter
520
 
 *        to skip lines, etc.  Setting this parameter to 0 is the equivalent of
521
 
 *        setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
522
 
 * @param height height (in pixels) of the source image
523
 
 * @param pixelFormat pixel format of the source image (see @ref TJPF
524
 
 *        "Pixel formats".)
525
 
 * @param jpegBuf address of a pointer to an image buffer that will receive the
526
 
 *        JPEG image.  TurboJPEG has the ability to reallocate the JPEG buffer
527
 
 *        to accommodate the size of the JPEG image.  Thus, you can choose to:
528
 
 *        -# pre-allocate the JPEG buffer with an arbitrary size using
529
 
 *        #tjAlloc() and let TurboJPEG grow the buffer as needed,
530
 
 *        -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the
531
 
 *        buffer for you, or
532
 
 *        -# pre-allocate the buffer to a "worst case" size determined by
533
 
 *        calling #tjBufSize().  This should ensure that the buffer never has
534
 
 *        to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
535
 
 *        .
536
 
 *        If you choose option 1, <tt>*jpegSize</tt> should be set to the
537
 
 *        size of your pre-allocated buffer.  In any case, unless you have
538
 
 *        set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon
539
 
 *        return from this function, as it may have changed.
540
 
 * @param jpegSize pointer to an unsigned long variable that holds the size of
541
 
 *        the JPEG image buffer.  If <tt>*jpegBuf</tt> points to a
542
 
 *        pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the
543
 
 *        size of the buffer.  Upon return, <tt>*jpegSize</tt> will contain the
544
 
 *        size of the JPEG image (in bytes.)
545
 
 * @param jpegSubsamp the level of chrominance subsampling to be used when
546
 
 *        generating the JPEG image (see @ref TJSAMP
547
 
 *        "Chrominance subsampling options".)
548
 
 * @param jpegQual the image quality of the generated JPEG image (1 = worst,
549
 
          100 = best)
550
 
 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
551
 
 *        "flags".
552
 
 *
553
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
554
 
*/
555
 
DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
556
 
  int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
557
 
  unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags);
558
 
 
559
 
 
560
 
/**
561
 
 * The maximum size of the buffer (in bytes) required to hold a JPEG image with
562
 
 * the given parameters.  The number of bytes returned by this function is
563
 
 * larger than the size of the uncompressed source image.  The reason for this
564
 
 * is that the JPEG format uses 16-bit coefficients, and it is thus possible
565
 
 * for a very high-quality JPEG image with very high frequency content to
566
 
 * expand rather than compress when converted to the JPEG format.  Such images
567
 
 * represent a very rare corner case, but since there is no way to predict the
568
 
 * size of a JPEG image prior to compression, the corner case has to be
569
 
 * handled.
570
 
 *
571
 
 * @param width width of the image (in pixels)
572
 
 * @param height height of the image (in pixels)
573
 
 * @param jpegSubsamp the level of chrominance subsampling to be used when
574
 
 *        generating the JPEG image (see @ref TJSAMP
575
 
 *        "Chrominance subsampling options".)
576
 
 *
577
 
 * @return the maximum size of the buffer (in bytes) required to hold the
578
 
 * image, or -1 if the arguments are out of bounds.
579
 
 */
580
 
DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
581
 
  int jpegSubsamp);
582
 
 
583
 
 
584
 
/**
585
 
 * The size of the buffer (in bytes) required to hold a YUV planar image with
586
 
 * the given parameters.
587
 
 *
588
 
 * @param width width of the image (in pixels)
589
 
 * @param height height of the image (in pixels)
590
 
 * @param subsamp level of chrominance subsampling in the image (see
591
 
 *        @ref TJSAMP "Chrominance subsampling options".)
592
 
 *
593
 
 * @return the size of the buffer (in bytes) required to hold the image, or
594
 
 * -1 if the arguments are out of bounds.
595
 
 */
596
 
DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
597
 
  int subsamp);
598
 
 
599
 
 
600
 
/**
601
 
 * Encode an RGB or grayscale image into a YUV planar image.  This function
602
 
 * uses the accelerated color conversion routines in TurboJPEG's underlying
603
 
 * codec to produce a planar YUV image that is suitable for X Video.
604
 
 * Specifically, if the chrominance components are subsampled along the
605
 
 * horizontal dimension, then the width of the luminance plane is padded to 2
606
 
 * in the output image (same goes for the height of the luminance plane, if the
607
 
 * chrominance components are subsampled along the vertical dimension.)  Also,
608
 
 * each line of each plane in the output image is padded to 4 bytes.  Although
609
 
 * this will work with any subsampling option, it is really only useful in
610
 
 * combination with TJ_420, which produces an image compatible with the I420
611
 
 * (AKA "YUV420P") format.
612
 
 *
613
 
 * @param handle a handle to a TurboJPEG compressor or transformer instance
614
 
 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
615
 
 *        to be encoded
616
 
 * @param width width (in pixels) of the source image
617
 
 * @param pitch bytes per line of the source image.  Normally, this should be
618
 
 *        <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
619
 
 *        or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
620
 
 *        the image is padded to the nearest 32-bit boundary, as is the case
621
 
 *        for Windows bitmaps.  You can also be clever and use this parameter
622
 
 *        to skip lines, etc.  Setting this parameter to 0 is the equivalent of
623
 
 *        setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
624
 
 * @param height height (in pixels) of the source image
625
 
 * @param pixelFormat pixel format of the source image (see @ref TJPF
626
 
 *        "Pixel formats".)
627
 
 * @param dstBuf pointer to an image buffer that will receive the YUV image.
628
 
 *        Use #tjBufSizeYUV() to determine the appropriate size for this buffer
629
 
 *        based on the image width, height, and level of chrominance
630
 
 *        subsampling.
631
 
 * @param subsamp the level of chrominance subsampling to be used when
632
 
 *        generating the YUV image (see @ref TJSAMP
633
 
 *        "Chrominance subsampling options".)
634
 
 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
635
 
 *        "flags".
636
 
 *
637
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
638
 
*/
639
 
DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle,
640
 
  unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat,
641
 
  unsigned char *dstBuf, int subsamp, int flags);
642
 
 
643
 
 
644
 
/**
645
 
 * Create a TurboJPEG decompressor instance.
646
 
 *
647
 
 * @return a handle to the newly-created instance, or NULL if an error
648
 
 * occurred (see #tjGetErrorStr().)
649
 
*/
650
 
DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
651
 
 
652
 
 
653
 
/**
654
 
 * Retrieve information about a JPEG image without decompressing it.
655
 
 *
656
 
 * @param handle a handle to a TurboJPEG decompressor or transformer instance
657
 
 * @param jpegBuf pointer to a buffer containing a JPEG image
658
 
 * @param jpegSize size of the JPEG image (in bytes)
659
 
 * @param width pointer to an integer variable that will receive the width (in
660
 
 *        pixels) of the JPEG image
661
 
 * @param height pointer to an integer variable that will receive the height
662
 
 *        (in pixels) of the JPEG image
663
 
 * @param jpegSubsamp pointer to an integer variable that will receive the
664
 
 *        level of chrominance subsampling used when compressing the JPEG image
665
 
 *        (see @ref TJSAMP "Chrominance subsampling options".)
666
 
 *
667
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
668
 
*/
669
 
DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
670
 
  unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
671
 
  int *jpegSubsamp);
672
 
 
673
 
 
674
 
/**
675
 
 * Returns a list of fractional scaling factors that the JPEG decompressor in
676
 
 * this implementation of TurboJPEG supports.
677
 
 *
678
 
 * @param numscalingfactors pointer to an integer variable that will receive
679
 
 *        the number of elements in the list
680
 
 *
681
 
 * @return a pointer to a list of fractional scaling factors, or NULL if an
682
 
 * error is encountered (see #tjGetErrorStr().)
683
 
*/
684
 
DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors);
685
 
 
686
 
 
687
 
/**
688
 
 * Decompress a JPEG image to an RGB or grayscale image.
689
 
 *
690
 
 * @param handle a handle to a TurboJPEG decompressor or transformer instance
691
 
 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
692
 
 * @param jpegSize size of the JPEG image (in bytes)
693
 
 * @param dstBuf pointer to an image buffer that will receive the decompressed
694
 
 *        image.  This buffer should normally be <tt>pitch * scaledHeight</tt>
695
 
 *        bytes in size, where <tt>scaledHeight</tt> can be determined by
696
 
 *        calling #TJSCALED() with the JPEG image height and one of the scaling
697
 
 *        factors returned by #tjGetScalingFactors().  The dstBuf pointer may
698
 
 *        also be used to decompress into a specific region of a larger buffer.
699
 
 * @param width desired width (in pixels) of the destination image.  If this is
700
 
 *        smaller than the width of the JPEG image being decompressed, then
701
 
 *        TurboJPEG will use scaling in the JPEG decompressor to generate the
702
 
 *        largest possible image that will fit within the desired width.  If
703
 
 *        width is set to 0, then only the height will be considered when
704
 
 *        determining the scaled image size.
705
 
 * @param pitch bytes per line of the destination image.  Normally, this is
706
 
 *        <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed
707
 
 *        image is unpadded, else <tt>#TJPAD(scaledWidth *
708
 
 *        #tjPixelSize[pixelFormat])</tt> if each line of the decompressed
709
 
 *        image is padded to the nearest 32-bit boundary, as is the case for
710
 
 *        Windows bitmaps.  (NOTE: <tt>scaledWidth</tt> can be determined by
711
 
 *        calling #TJSCALED() with the JPEG image width and one of the scaling
712
 
 *        factors returned by #tjGetScalingFactors().)  You can also be clever
713
 
 *        and use the pitch parameter to skip lines, etc.  Setting this
714
 
 *        parameter to 0 is the equivalent of setting it to <tt>scaledWidth
715
 
 *        * #tjPixelSize[pixelFormat]</tt>.
716
 
 * @param height desired height (in pixels) of the destination image.  If this
717
 
 *        is smaller than the height of the JPEG image being decompressed, then
718
 
 *        TurboJPEG will use scaling in the JPEG decompressor to generate the
719
 
 *        largest possible image that will fit within the desired height.  If
720
 
 *        height is set to 0, then only the width will be considered when
721
 
 *        determining the scaled image size.
722
 
 * @param pixelFormat pixel format of the destination image (see @ref
723
 
 *        TJPF "Pixel formats".)
724
 
 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
725
 
 *        "flags".
726
 
 *
727
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
728
 
 */
729
 
DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
730
 
  unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
731
 
  int width, int pitch, int height, int pixelFormat, int flags);
732
 
 
733
 
 
734
 
/**
735
 
 * Decompress a JPEG image to a YUV planar image.  This function performs JPEG
736
 
 * decompression but leaves out the color conversion step, so a planar YUV
737
 
 * image is generated instead of an RGB image.  The padding of the planes in
738
 
 * this image is the same as the images generated by #tjEncodeYUV2().  Note
739
 
 * that, if the width or height of the image is not an even multiple of the MCU
740
 
 * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer
741
 
 * copy will be performed within TurboJPEG.
742
 
 *
743
 
 * @param handle a handle to a TurboJPEG decompressor or transformer instance
744
 
 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
745
 
 * @param jpegSize size of the JPEG image (in bytes)
746
 
 * @param dstBuf pointer to an image buffer that will receive the YUV image.
747
 
 *        Use #tjBufSizeYUV to determine the appropriate size for this buffer
748
 
 *        based on the image width, height, and level of subsampling.
749
 
 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
750
 
 *        "flags".
751
 
 *
752
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
753
 
 */
754
 
DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
755
 
  unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
756
 
  int flags);
757
 
 
758
 
 
759
 
/**
760
 
 * Create a new TurboJPEG transformer instance.
761
 
 *
762
 
 * @return a handle to the newly-created instance, or NULL if an error
763
 
 * occurred (see #tjGetErrorStr().)
764
 
 */
765
 
DLLEXPORT tjhandle DLLCALL tjInitTransform(void);
766
 
 
767
 
 
768
 
/**
769
 
 * Losslessly transform a JPEG image into another JPEG image.  Lossless
770
 
 * transforms work by moving the raw coefficients from one JPEG image structure
771
 
 * to another without altering the values of the coefficients.  While this is
772
 
 * typically faster than decompressing the image, transforming it, and
773
 
 * re-compressing it, lossless transforms are not free.  Each lossless
774
 
 * transform requires reading and Huffman decoding all of the coefficients in
775
 
 * the source image, regardless of the size of the destination image.  Thus,
776
 
 * this function provides a means of generating multiple transformed images
777
 
 * from the same source or of applying multiple transformations simultaneously,
778
 
 * in order to eliminate the need to read the source coefficients multiple
779
 
 * times.
780
 
 *
781
 
 * @param handle a handle to a TurboJPEG transformer instance
782
 
 * @param jpegBuf pointer to a buffer containing the JPEG image to transform
783
 
 * @param jpegSize size of the JPEG image (in bytes)
784
 
 * @param n the number of transformed JPEG images to generate
785
 
 * @param dstBufs pointer to an array of n image buffers.  <tt>dstBufs[i]</tt>
786
 
 *        will receive a JPEG image that has been transformed using the
787
 
 *        parameters in <tt>transforms[i]</tt>.  TurboJPEG has the ability to
788
 
 *        reallocate the JPEG buffer to accommodate the size of the JPEG image.
789
 
 *        Thus, you can choose to:
790
 
 *        -# pre-allocate the JPEG buffer with an arbitrary size using
791
 
 *        #tjAlloc() and let TurboJPEG grow the buffer as needed,
792
 
 *        -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the
793
 
 *        buffer for you, or
794
 
 *        -# pre-allocate the buffer to a "worst case" size determined by
795
 
 *        calling #tjBufSize() with the cropped width and height.  This should
796
 
 *        ensure that the buffer never has to be re-allocated (setting
797
 
 *        #TJFLAG_NOREALLOC guarantees this.)
798
 
 *        .
799
 
 *        If you choose option 1, <tt>dstSizes[i]</tt> should be set to
800
 
 *        the size of your pre-allocated buffer.  In any case, unless you have
801
 
 *        set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt>
802
 
 *        upon return from this function, as it may have changed.
803
 
 * @param dstSizes pointer to an array of n unsigned long variables that will
804
 
 *        receive the actual sizes (in bytes) of each transformed JPEG image.
805
 
 *        If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
806
 
 *        <tt>dstSizes[i]</tt> should be set to the size of the buffer.  Upon
807
 
 *        return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image
808
 
 *        (in bytes.)
809
 
 * @param transforms pointer to an array of n tjtransform structures, each of
810
 
 *        which specifies the transform parameters and/or cropping region for
811
 
 *        the corresponding transformed output image.
812
 
 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
813
 
 *        "flags".
814
 
 *
815
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
816
 
 */
817
 
DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
818
 
  unsigned long jpegSize, int n, unsigned char **dstBufs,
819
 
  unsigned long *dstSizes, tjtransform *transforms, int flags);
820
 
 
821
 
 
822
 
/**
823
 
 * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
824
 
 *
825
 
 * @param handle a handle to a TurboJPEG compressor, decompressor or
826
 
 *        transformer instance
827
 
 *
828
 
 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
829
 
 */
830
 
DLLEXPORT int DLLCALL tjDestroy(tjhandle handle);
831
 
 
832
 
 
833
 
/**
834
 
 * Allocate an image buffer for use with TurboJPEG.  You should always use
835
 
 * this function to allocate the JPEG destination buffer(s) for #tjCompress2()
836
 
 * and #tjTransform() unless you are disabling automatic buffer
837
 
 * (re)allocation (by setting #TJFLAG_NOREALLOC.)
838
 
 *
839
 
 * @param bytes the number of bytes to allocate
840
 
 * 
841
 
 * @return a pointer to a newly-allocated buffer with the specified number of
842
 
 *         bytes
843
 
 *
844
 
 * @sa tjFree()
845
 
 */
846
 
DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes);
847
 
 
848
 
 
849
 
/**
850
 
 * Free an image buffer previously allocated by TurboJPEG.  You should always
851
 
 * use this function to free JPEG destination buffer(s) that were automatically
852
 
 * (re)allocated by #tjCompress2() or #tjTransform() or that were manually
853
 
 * allocated using #tjAlloc().
854
 
 *
855
 
 * @param buffer address of the buffer to free
856
 
 *
857
 
 * @sa tjAlloc()
858
 
 */
859
 
DLLEXPORT void DLLCALL tjFree(unsigned char *buffer);
860
 
 
861
 
 
862
 
/**
863
 
 * Returns a descriptive error message explaining why the last command failed.
864
 
 *
865
 
 * @return a descriptive error message explaining why the last command failed.
866
 
 */
867
 
DLLEXPORT char* DLLCALL tjGetErrorStr(void);
868
 
 
869
 
 
870
 
/* Backward compatibility functions and macros (nothing to see here) */
871
 
#define NUMSUBOPT TJ_NUMSAMP
872
 
#define TJ_444 TJSAMP_444
873
 
#define TJ_422 TJSAMP_422
874
 
#define TJ_420 TJSAMP_420
875
 
#define TJ_411 TJSAMP_420
876
 
#define TJ_GRAYSCALE TJSAMP_GRAY
877
 
 
878
 
#define TJ_BGR 1
879
 
#define TJ_BOTTOMUP TJFLAG_BOTTOMUP
880
 
#define TJ_FORCEMMX TJFLAG_FORCEMMX
881
 
#define TJ_FORCESSE TJFLAG_FORCESSE
882
 
#define TJ_FORCESSE2 TJFLAG_FORCESSE2
883
 
#define TJ_ALPHAFIRST 64
884
 
#define TJ_FORCESSE3 TJFLAG_FORCESSE3
885
 
#define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
886
 
#define TJ_YUV 512
887
 
 
888
 
DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
889
 
 
890
 
DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
891
 
  int jpegSubsamp);
892
 
 
893
 
DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
894
 
  int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
895
 
  unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags);
896
 
 
897
 
DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle,
898
 
  unsigned char *srcBuf, int width, int pitch, int height, int pixelSize,
899
 
  unsigned char *dstBuf, int subsamp, int flags);
900
 
 
901
 
DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
902
 
  unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height);
903
 
 
904
 
DLLEXPORT int DLLCALL tjDecompress(tjhandle handle,
905
 
  unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
906
 
  int width, int pitch, int height, int pixelSize, int flags);
907
 
 
908
 
 
909
 
/**
910
 
 * @}
911
 
 */
912
 
 
913
 
#ifdef __cplusplus
914
 
}
915
 
#endif
916
 
 
917
 
#endif