blob: ea6be1fc3058eaa6884f3cd27893a80d42345c0d [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * transupp.h
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
Jonathan Wrightbbb82822020-11-25 13:36:43 +00005 * Copyright (C) 1997-2019, Thomas G. Lane, Guido Vollbeding.
Chris Blumecca8c4d2019-03-01 01:09:50 -08006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2017, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000010 *
11 * This file contains declarations for image transformation routines and
12 * other utility code used by the jpegtran sample application. These are
13 * NOT part of the core JPEG library. But we keep these routines separate
14 * from jpegtran.c to ease the task of maintaining jpegtran-like programs
15 * that have other user interfaces.
16 *
17 * NOTE: all the routines declared here have very specific requirements
18 * about when they are to be executed during the reading and writing of the
19 * source and destination files. See the comments in transupp.c, or see
20 * jpegtran.c for an example of correct usage.
21 */
22
23/* If you happen not to want the image transform support, disable it here */
24#ifndef TRANSFORMS_SUPPORTED
Chris Blumecca8c4d2019-03-01 01:09:50 -080025#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000026#endif
27
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000028/*
29 * Although rotating and flipping data expressed as DCT coefficients is not
30 * hard, there is an asymmetry in the JPEG format specification for images
31 * whose dimensions aren't multiples of the iMCU size. The right and bottom
32 * image edges are padded out to the next iMCU boundary with junk data; but
33 * no padding is possible at the top and left edges. If we were to flip
34 * the whole image including the pad data, then pad garbage would become
35 * visible at the top and/or left, and real pixels would disappear into the
36 * pad margins --- perhaps permanently, since encoders & decoders may not
37 * bother to preserve DCT blocks that appear to be completely outside the
38 * nominal image area. So, we have to exclude any partial iMCUs from the
39 * basic transformation.
40 *
41 * Transpose is the only transformation that can handle partial iMCUs at the
42 * right and bottom edges completely cleanly. flip_h can flip partial iMCUs
43 * at the bottom, but leaves any partial iMCUs at the right edge untouched.
44 * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
45 * The other transforms are defined as combinations of these basic transforms
46 * and process edge blocks in a way that preserves the equivalence.
47 *
48 * The "trim" option causes untransformable partial iMCUs to be dropped;
49 * this is not strictly lossless, but it usually gives the best-looking
50 * result for odd-size images. Note that when this option is active,
51 * the expected mathematical equivalences between the transforms may not hold.
52 * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
53 * followed by -rot 180 -trim trims both edges.)
54 *
hbono@chromium.org98626972011-08-03 03:13:08 +000055 * We also offer a lossless-crop option, which discards data outside a given
56 * image region but losslessly preserves what is inside. Like the rotate and
57 * flip transforms, lossless crop is restricted by the JPEG format: the upper
58 * left corner of the selected region must fall on an iMCU boundary. If this
59 * does not hold for the given crop parameters, we silently move the upper left
60 * corner up and/or left to make it so, simultaneously increasing the region
61 * dimensions to keep the lower right crop corner unchanged. (Thus, the
62 * output image covers at least the requested region, but may cover more.)
noel@chromium.org3395bcc2014-04-14 06:56:00 +000063 * The adjustment of the region dimensions may be optionally disabled.
hbono@chromium.org98626972011-08-03 03:13:08 +000064 *
Jonathan Wrightbbb82822020-11-25 13:36:43 +000065 * A complementary lossless wipe option is provided to discard (gray out) data
66 * inside a given image region while losslessly preserving what is outside.
67 * A lossless drop option is also provided, which allows another JPEG image to
68 * be inserted ("dropped") into the source image data at a given position,
69 * replacing the existing image data at that position. Both the source image
70 * and the drop image must have the same subsampling level. It is best if they
71 * also have the same quantization (quality.) Otherwise, the quantization of
72 * the output image will be adapted to accommodate the higher of the source
73 * image quality and the drop image quality. The trim option can be used with
74 * the drop option to requantize the drop image to match the source image.
75 *
hbono@chromium.org98626972011-08-03 03:13:08 +000076 * We also provide a lossless-resize option, which is kind of a lossless-crop
77 * operation in the DCT coefficient block domain - it discards higher-order
78 * coefficients and losslessly preserves lower-order coefficients of a
79 * sub-block.
80 *
81 * Rotate/flip transform, resize, and crop can be requested together in a
82 * single invocation. The crop is applied last --- that is, the crop region
83 * is specified in terms of the destination image after transform/resize.
84 *
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000085 * We also offer a "force to grayscale" option, which simply discards the
86 * chrominance channels of a YCbCr image. This is lossless in the sense that
87 * the luminance channel is preserved exactly. It's not the same kind of
88 * thing as the rotate/flip transformations, but it's convenient to handle it
89 * as part of this package, mainly because the transformation routines have to
90 * be aware of the option to know how many components to work on.
91 */
92
hbono@chromium.org98626972011-08-03 03:13:08 +000093
hbono@chromium.org98626972011-08-03 03:13:08 +000094/*
95 * Codes for supported types of image transformations.
96 */
97
98typedef enum {
Tom Hudson0d47d2d2016-05-04 13:22:56 -040099 JXFORM_NONE, /* no transformation */
100 JXFORM_FLIP_H, /* horizontal flip */
101 JXFORM_FLIP_V, /* vertical flip */
102 JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */
103 JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */
104 JXFORM_ROT_90, /* 90-degree clockwise rotation */
105 JXFORM_ROT_180, /* 180-degree rotation */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000106 JXFORM_ROT_270, /* 270-degree clockwise (or 90 ccw) */
107 JXFORM_WIPE, /* wipe */
108 JXFORM_DROP /* drop */
hbono@chromium.org98626972011-08-03 03:13:08 +0000109} JXFORM_CODE;
110
111/*
112 * Codes for crop parameters, which can individually be unspecified,
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000113 * positive or negative for xoffset or yoffset,
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000114 * positive or force or reflect for width or height.
hbono@chromium.org98626972011-08-03 03:13:08 +0000115 */
116
117typedef enum {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400118 JCROP_UNSET,
119 JCROP_POS,
120 JCROP_NEG,
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000121 JCROP_FORCE,
122 JCROP_REFLECT
hbono@chromium.org98626972011-08-03 03:13:08 +0000123} JCROP_CODE;
124
125/*
126 * Transform parameters struct.
127 * NB: application must not change any elements of this struct after
128 * calling jtransform_request_workspace.
129 */
130
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000131typedef struct {
132 /* Options: set by caller */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400133 JXFORM_CODE transform; /* image transform operator */
134 boolean perfect; /* if TRUE, fail if partial MCUs are requested */
135 boolean trim; /* if TRUE, trim partial MCUs as needed */
136 boolean force_grayscale; /* if TRUE, convert color image to grayscale */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000137 boolean crop; /* if TRUE, crop or wipe source image, or drop */
hbono@chromium.org98626972011-08-03 03:13:08 +0000138 boolean slow_hflip; /* For best performance, the JXFORM_FLIP_H transform
139 normally modifies the source coefficients in place.
140 Setting this to TRUE will instead use a slower,
141 double-buffered algorithm, which leaves the source
142 coefficients in tact (necessary if other transformed
143 images must be generated from the same set of
144 coefficients. */
145
146 /* Crop parameters: application need not set these unless crop is TRUE.
147 * These can be filled in by jtransform_parse_crop_spec().
148 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400149 JDIMENSION crop_width; /* Width of selected region */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000150 JCROP_CODE crop_width_set; /* (force-disables adjustment) */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400151 JDIMENSION crop_height; /* Height of selected region */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000152 JCROP_CODE crop_height_set; /* (force-disables adjustment) */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400153 JDIMENSION crop_xoffset; /* X offset of selected region */
154 JCROP_CODE crop_xoffset_set; /* (negative measures from right edge) */
155 JDIMENSION crop_yoffset; /* Y offset of selected region */
156 JCROP_CODE crop_yoffset_set; /* (negative measures from bottom edge) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000157
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000158 /* Drop parameters: set by caller for drop request */
159 j_decompress_ptr drop_ptr;
160 jvirt_barray_ptr *drop_coef_arrays;
161
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000162 /* Internal workspace: caller should not touch these */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400163 int num_components; /* # of components in workspace */
164 jvirt_barray_ptr *workspace_coef_arrays; /* workspace for transformations */
165 JDIMENSION output_width; /* cropped destination dimensions */
hbono@chromium.org98626972011-08-03 03:13:08 +0000166 JDIMENSION output_height;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400167 JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */
hbono@chromium.org98626972011-08-03 03:13:08 +0000168 JDIMENSION y_crop_offset;
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000169 JDIMENSION drop_width; /* drop/wipe dimensions measured in iMCUs */
170 JDIMENSION drop_height;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400171 int iMCU_sample_width; /* destination iMCU size */
hbono@chromium.org98626972011-08-03 03:13:08 +0000172 int iMCU_sample_height;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000173} jpeg_transform_info;
174
175
176#if TRANSFORMS_SUPPORTED
177
hbono@chromium.org98626972011-08-03 03:13:08 +0000178/* Parse a crop specification (written in X11 geometry style) */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800179EXTERN(boolean) jtransform_parse_crop_spec(jpeg_transform_info *info,
180 const char *spec);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000181/* Request any required workspace */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800182EXTERN(boolean) jtransform_request_workspace(j_decompress_ptr srcinfo,
183 jpeg_transform_info *info);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000184/* Adjust output image parameters */
185EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
Chris Blumecca8c4d2019-03-01 01:09:50 -0800186 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
187 jvirt_barray_ptr *src_coef_arrays, jpeg_transform_info *info);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000188/* Execute the actual transformation, if any */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800189EXTERN(void) jtransform_execute_transform(j_decompress_ptr srcinfo,
190 j_compress_ptr dstinfo,
191 jvirt_barray_ptr *src_coef_arrays,
192 jpeg_transform_info *info);
hbono@chromium.org98626972011-08-03 03:13:08 +0000193/* Determine whether lossless transformation is perfectly
194 * possible for a specified image and transformation.
195 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800196EXTERN(boolean) jtransform_perfect_transform(JDIMENSION image_width,
197 JDIMENSION image_height,
198 int MCU_width, int MCU_height,
199 JXFORM_CODE transform);
hbono@chromium.org98626972011-08-03 03:13:08 +0000200
201/* jtransform_execute_transform used to be called
202 * jtransform_execute_transformation, but some compilers complain about
203 * routine names that long. This macro is here to avoid breaking any
204 * old source code that uses the original name...
205 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400206#define jtransform_execute_transformation jtransform_execute_transform
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000207
208#endif /* TRANSFORMS_SUPPORTED */
209
210
211/*
212 * Support for copying optional markers from source to destination file.
213 */
214
215typedef enum {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400216 JCOPYOPT_NONE, /* copy no optional markers */
217 JCOPYOPT_COMMENTS, /* copy only comment (COM) markers */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800218 JCOPYOPT_ALL, /* copy all optional markers */
219 JCOPYOPT_ALL_EXCEPT_ICC /* copy all optional markers except APP2 */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000220} JCOPY_OPTION;
221
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400222#define JCOPYOPT_DEFAULT JCOPYOPT_COMMENTS /* recommended default */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000223
224/* Setup decompression object to save desired markers in memory */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800225EXTERN(void) jcopy_markers_setup(j_decompress_ptr srcinfo,
226 JCOPY_OPTION option);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000227/* Copy markers saved in the given source object to the destination object */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800228EXTERN(void) jcopy_markers_execute(j_decompress_ptr srcinfo,
229 j_compress_ptr dstinfo,
230 JCOPY_OPTION option);