blob: 8fe9071019e7cec7d1def8d3628ecc3532f2be01 [file] [log] [blame]
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00001/*
2 * transupp.h
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Guido Vollbeding5829cb22012-01-15 00:00:00 +00005 * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains declarations for image transformation routines and
11 * other utility code used by the jpegtran sample application. These are
12 * NOT part of the core JPEG library. But we keep these routines separate
13 * from jpegtran.c to ease the task of maintaining jpegtran-like programs
14 * that have other user interfaces.
15 *
16 * NOTE: all the routines declared here have very specific requirements
17 * about when they are to be executed during the reading and writing of the
18 * source and destination files. See the comments in transupp.c, or see
19 * jpegtran.c for an example of correct usage.
20 */
21
22/* If you happen not to want the image transform support, disable it here */
23#ifndef TRANSFORMS_SUPPORTED
DRCb7753512014-05-11 09:36:25 +000024#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000025#endif
26
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000027/*
28 * Although rotating and flipping data expressed as DCT coefficients is not
29 * hard, there is an asymmetry in the JPEG format specification for images
30 * whose dimensions aren't multiples of the iMCU size. The right and bottom
31 * image edges are padded out to the next iMCU boundary with junk data; but
32 * no padding is possible at the top and left edges. If we were to flip
33 * the whole image including the pad data, then pad garbage would become
34 * visible at the top and/or left, and real pixels would disappear into the
35 * pad margins --- perhaps permanently, since encoders & decoders may not
36 * bother to preserve DCT blocks that appear to be completely outside the
37 * nominal image area. So, we have to exclude any partial iMCUs from the
38 * basic transformation.
39 *
40 * Transpose is the only transformation that can handle partial iMCUs at the
41 * right and bottom edges completely cleanly. flip_h can flip partial iMCUs
42 * at the bottom, but leaves any partial iMCUs at the right edge untouched.
43 * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
44 * The other transforms are defined as combinations of these basic transforms
45 * and process edge blocks in a way that preserves the equivalence.
46 *
47 * The "trim" option causes untransformable partial iMCUs to be dropped;
48 * this is not strictly lossless, but it usually gives the best-looking
49 * result for odd-size images. Note that when this option is active,
50 * the expected mathematical equivalences between the transforms may not hold.
51 * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
52 * followed by -rot 180 -trim trims both edges.)
53 *
Guido Vollbeding5996a252009-06-27 00:00:00 +000054 * We also offer a lossless-crop option, which discards data outside a given
55 * image region but losslessly preserves what is inside. Like the rotate and
56 * flip transforms, lossless crop is restricted by the JPEG format: the upper
57 * left corner of the selected region must fall on an iMCU boundary. If this
58 * does not hold for the given crop parameters, we silently move the upper left
59 * corner up and/or left to make it so, simultaneously increasing the region
60 * dimensions to keep the lower right crop corner unchanged. (Thus, the
61 * output image covers at least the requested region, but may cover more.)
Guido Vollbeding5829cb22012-01-15 00:00:00 +000062 * The adjustment of the region dimensions may be optionally disabled.
Guido Vollbeding5996a252009-06-27 00:00:00 +000063 *
Guido Vollbeding989630f2010-01-10 00:00:00 +000064 * We also provide a lossless-resize option, which is kind of a lossless-crop
65 * operation in the DCT coefficient block domain - it discards higher-order
66 * coefficients and losslessly preserves lower-order coefficients of a
67 * sub-block.
68 *
69 * Rotate/flip transform, resize, and crop can be requested together in a
70 * single invocation. The crop is applied last --- that is, the crop region
71 * is specified in terms of the destination image after transform/resize.
Guido Vollbeding5996a252009-06-27 00:00:00 +000072 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000073 * We also offer a "force to grayscale" option, which simply discards the
74 * chrominance channels of a YCbCr image. This is lossless in the sense that
75 * the luminance channel is preserved exactly. It's not the same kind of
76 * thing as the rotate/flip transformations, but it's convenient to handle it
77 * as part of this package, mainly because the transformation routines have to
78 * be aware of the option to know how many components to work on.
79 */
80
Guido Vollbeding5996a252009-06-27 00:00:00 +000081
Guido Vollbeding5996a252009-06-27 00:00:00 +000082/*
83 * Codes for supported types of image transformations.
84 */
85
86typedef enum {
DRC333e9182014-05-12 00:34:08 +000087 JXFORM_NONE, /* no transformation */
88 JXFORM_FLIP_H, /* horizontal flip */
89 JXFORM_FLIP_V, /* vertical flip */
90 JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */
91 JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */
92 JXFORM_ROT_90, /* 90-degree clockwise rotation */
93 JXFORM_ROT_180, /* 180-degree rotation */
94 JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */
Guido Vollbeding5996a252009-06-27 00:00:00 +000095} JXFORM_CODE;
96
97/*
98 * Codes for crop parameters, which can individually be unspecified,
Guido Vollbeding5829cb22012-01-15 00:00:00 +000099 * positive or negative for xoffset or yoffset,
100 * positive or forced for width or height.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000101 */
102
103typedef enum {
DRC333e9182014-05-12 00:34:08 +0000104 JCROP_UNSET,
105 JCROP_POS,
106 JCROP_NEG,
107 JCROP_FORCE
Guido Vollbeding5996a252009-06-27 00:00:00 +0000108} JCROP_CODE;
109
110/*
111 * Transform parameters struct.
112 * NB: application must not change any elements of this struct after
113 * calling jtransform_request_workspace.
114 */
115
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000116typedef struct {
117 /* Options: set by caller */
DRCb7753512014-05-11 09:36:25 +0000118 JXFORM_CODE transform; /* image transform operator */
119 boolean perfect; /* if TRUE, fail if partial MCUs are requested */
120 boolean trim; /* if TRUE, trim partial MCUs as needed */
121 boolean force_grayscale; /* if TRUE, convert color image to grayscale */
122 boolean crop; /* if TRUE, crop source image */
DRCba5ea512011-03-04 03:20:34 +0000123 boolean slow_hflip; /* For best performance, the JXFORM_FLIP_H transform
124 normally modifies the source coefficients in place.
125 Setting this to TRUE will instead use a slower,
126 double-buffered algorithm, which leaves the source
127 coefficients in tact (necessary if other transformed
128 images must be generated from the same set of
129 coefficients. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000130
131 /* Crop parameters: application need not set these unless crop is TRUE.
132 * These can be filled in by jtransform_parse_crop_spec().
133 */
DRCb7753512014-05-11 09:36:25 +0000134 JDIMENSION crop_width; /* Width of selected region */
135 JCROP_CODE crop_width_set; /* (forced disables adjustment) */
136 JDIMENSION crop_height; /* Height of selected region */
137 JCROP_CODE crop_height_set; /* (forced disables adjustment) */
138 JDIMENSION crop_xoffset; /* X offset of selected region */
139 JCROP_CODE crop_xoffset_set; /* (negative measures from right edge) */
140 JDIMENSION crop_yoffset; /* Y offset of selected region */
141 JCROP_CODE crop_yoffset_set; /* (negative measures from bottom edge) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000142
143 /* Internal workspace: caller should not touch these */
DRCb7753512014-05-11 09:36:25 +0000144 int num_components; /* # of components in workspace */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000145 jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
DRCb7753512014-05-11 09:36:25 +0000146 JDIMENSION output_width; /* cropped destination dimensions */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000147 JDIMENSION output_height;
DRCb7753512014-05-11 09:36:25 +0000148 JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000149 JDIMENSION y_crop_offset;
DRCb7753512014-05-11 09:36:25 +0000150 int iMCU_sample_width; /* destination iMCU size */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000151 int iMCU_sample_height;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000152} jpeg_transform_info;
153
154
155#if TRANSFORMS_SUPPORTED
156
Guido Vollbeding5996a252009-06-27 00:00:00 +0000157/* Parse a crop specification (written in X11 geometry style) */
158EXTERN(boolean) jtransform_parse_crop_spec
DRCbc56b752014-05-16 10:43:44 +0000159 (jpeg_transform_info *info, const char *spec);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000160/* Request any required workspace */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000161EXTERN(boolean) jtransform_request_workspace
DRCbc56b752014-05-16 10:43:44 +0000162 (j_decompress_ptr srcinfo, jpeg_transform_info *info);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000163/* Adjust output image parameters */
164EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
DRCbc56b752014-05-16 10:43:44 +0000165 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
166 jvirt_barray_ptr *src_coef_arrays, jpeg_transform_info *info);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000167/* Execute the actual transformation, if any */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000168EXTERN(void) jtransform_execute_transform
DRCbc56b752014-05-16 10:43:44 +0000169 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
170 jvirt_barray_ptr *src_coef_arrays, jpeg_transform_info *info);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000171/* Determine whether lossless transformation is perfectly
172 * possible for a specified image and transformation.
173 */
174EXTERN(boolean) jtransform_perfect_transform
DRCbc56b752014-05-16 10:43:44 +0000175 (JDIMENSION image_width, JDIMENSION image_height, int MCU_width,
176 int MCU_height, JXFORM_CODE transform);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000177
178/* jtransform_execute_transform used to be called
179 * jtransform_execute_transformation, but some compilers complain about
180 * routine names that long. This macro is here to avoid breaking any
181 * old source code that uses the original name...
182 */
DRCb7753512014-05-11 09:36:25 +0000183#define jtransform_execute_transformation jtransform_execute_transform
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000184
185#endif /* TRANSFORMS_SUPPORTED */
186
187
188/*
189 * Support for copying optional markers from source to destination file.
190 */
191
192typedef enum {
DRC333e9182014-05-12 00:34:08 +0000193 JCOPYOPT_NONE, /* copy no optional markers */
194 JCOPYOPT_COMMENTS, /* copy only comment (COM) markers */
195 JCOPYOPT_ALL /* copy all optional markers */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000196} JCOPY_OPTION;
197
DRCb7753512014-05-11 09:36:25 +0000198#define JCOPYOPT_DEFAULT JCOPYOPT_COMMENTS /* recommended default */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000199
200/* Setup decompression object to save desired markers in memory */
201EXTERN(void) jcopy_markers_setup
DRCbc56b752014-05-16 10:43:44 +0000202 (j_decompress_ptr srcinfo, JCOPY_OPTION option);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000203/* Copy markers saved in the given source object to the destination object */
204EXTERN(void) jcopy_markers_execute
DRCbc56b752014-05-16 10:43:44 +0000205 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
206 JCOPY_OPTION option);