• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * transupp.h
3  *
4  * Copyright (C) 1997-2019, Thomas G. Lane, Guido Vollbeding.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains declarations for image transformation routines and
9  * other utility code used by the jpegtran sample application.  These are
10  * NOT part of the core JPEG library.  But we keep these routines separate
11  * from jpegtran.c to ease the task of maintaining jpegtran-like programs
12  * that have other user interfaces.
13  *
14  * NOTE: all the routines declared here have very specific requirements
15  * about when they are to be executed during the reading and writing of the
16  * source and destination files.  See the comments in transupp.c, or see
17  * jpegtran.c for an example of correct usage.
18  */
19 
20 /* If you happen not to want the image transform support, disable it here */
21 #ifndef TRANSFORMS_SUPPORTED
22 #define TRANSFORMS_SUPPORTED 1		/* 0 disables transform code */
23 #endif
24 
25 /*
26  * Although rotating and flipping data expressed as DCT coefficients is not
27  * hard, there is an asymmetry in the JPEG format specification for images
28  * whose dimensions aren't multiples of the iMCU size.  The right and bottom
29  * image edges are padded out to the next iMCU boundary with junk data; but
30  * no padding is possible at the top and left edges.  If we were to flip
31  * the whole image including the pad data, then pad garbage would become
32  * visible at the top and/or left, and real pixels would disappear into the
33  * pad margins --- perhaps permanently, since encoders & decoders may not
34  * bother to preserve DCT blocks that appear to be completely outside the
35  * nominal image area.  So, we have to exclude any partial iMCUs from the
36  * basic transformation.
37  *
38  * Transpose is the only transformation that can handle partial iMCUs at the
39  * right and bottom edges completely cleanly.  flip_h can flip partial iMCUs
40  * at the bottom, but leaves any partial iMCUs at the right edge untouched.
41  * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
42  * The other transforms are defined as combinations of these basic transforms
43  * and process edge blocks in a way that preserves the equivalence.
44  *
45  * The "trim" option causes untransformable partial iMCUs to be dropped;
46  * this is not strictly lossless, but it usually gives the best-looking
47  * result for odd-size images.  Note that when this option is active,
48  * the expected mathematical equivalences between the transforms may not hold.
49  * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
50  * followed by -rot 180 -trim trims both edges.)
51  *
52  * We also offer a lossless-crop option, which discards data outside a given
53  * image region but losslessly preserves what is inside.  Like the rotate and
54  * flip transforms, lossless crop is restricted by the current JPEG format: the
55  * upper left corner of the selected region must fall on an iMCU boundary.  If
56  * this does not hold for the given crop parameters, we silently move the upper
57  * left corner up and/or left to make it so, simultaneously increasing the
58  * region dimensions to keep the lower right crop corner unchanged.  (Thus, the
59  * output image covers at least the requested region, but may cover more.)
60  * The adjustment of the region dimensions may be optionally disabled.
61  *
62  * A complementary lossless-wipe option is provided to discard (gray out) data
63  * inside a given image region while losslessly preserving what is outside.
64  * Another option is lossless-drop, which replaces data at a given image
65  * position by another image.  Both source images must have the same
66  * subsampling values.  It is best if they also have the same quantization,
67  * otherwise quantization adaption occurs.  The trim option can be used with
68  * the drop option to requantize the drop file to the source file.
69  *
70  * We also provide a lossless-resize option, which is kind of a lossless-crop
71  * operation in the DCT coefficient block domain - it discards higher-order
72  * coefficients and losslessly preserves lower-order coefficients of a
73  * sub-block.
74  *
75  * Rotate/flip transform, resize, and crop can be requested together in a
76  * single invocation.  The crop is applied last --- that is, the crop region
77  * is specified in terms of the destination image after transform/resize.
78  *
79  * We also offer a "force to grayscale" option, which simply discards the
80  * chrominance channels of a YCbCr image.  This is lossless in the sense that
81  * the luminance channel is preserved exactly.  It's not the same kind of
82  * thing as the rotate/flip transformations, but it's convenient to handle it
83  * as part of this package, mainly because the transformation routines have to
84  * be aware of the option to know how many components to work on.
85  */
86 
87 
88 /* Short forms of external names for systems with brain-damaged linkers. */
89 
90 #ifdef NEED_SHORT_EXTERNAL_NAMES
91 #define jtransform_parse_crop_spec	jTrParCrop
92 #define jtransform_request_workspace	jTrRequest
93 #define jtransform_adjust_parameters	jTrAdjust
94 #define jtransform_execute_transform	jTrExec
95 #define jtransform_perfect_transform	jTrPerfect
96 #define jcopy_markers_setup		jCMrkSetup
97 #define jcopy_markers_execute		jCMrkExec
98 #endif /* NEED_SHORT_EXTERNAL_NAMES */
99 
100 
101 /*
102  * Codes for supported types of image transformations.
103  */
104 
105 typedef enum {
106 	JXFORM_NONE,		/* no transformation */
107 	JXFORM_FLIP_H,		/* horizontal flip */
108 	JXFORM_FLIP_V,		/* vertical flip */
109 	JXFORM_TRANSPOSE,	/* transpose across UL-to-LR axis */
110 	JXFORM_TRANSVERSE,	/* transpose across UR-to-LL axis */
111 	JXFORM_ROT_90,		/* 90-degree clockwise rotation */
112 	JXFORM_ROT_180,		/* 180-degree rotation */
113 	JXFORM_ROT_270,		/* 270-degree clockwise (or 90 ccw) */
114 	JXFORM_WIPE,		/* wipe */
115 	JXFORM_DROP		/* drop */
116 } JXFORM_CODE;
117 
118 /*
119  * Codes for crop parameters, which can individually be unspecified,
120  * positive or negative for xoffset or yoffset,
121  * positive or force or reflect for width or height.
122  */
123 
124 typedef enum {
125 	JCROP_UNSET,
126 	JCROP_POS,
127 	JCROP_NEG,
128 	JCROP_FORCE,
129 	JCROP_REFLECT
130 } JCROP_CODE;
131 
132 /*
133  * Transform parameters struct.
134  * NB: application must not change any elements of this struct after
135  * calling jtransform_request_workspace.
136  */
137 
138 typedef struct {
139   /* Options: set by caller */
140   JXFORM_CODE transform;	/* image transform operator */
141   boolean perfect;		/* if TRUE, fail if partial MCUs are requested */
142   boolean trim;			/* if TRUE, trim partial MCUs as needed */
143   boolean force_grayscale;	/* if TRUE, convert color image to grayscale */
144   boolean crop;			/* if TRUE, crop or wipe source image, or drop */
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    */
149   JDIMENSION crop_width;	/* Width of selected region */
150   JCROP_CODE crop_width_set;	/* (force disables adjustment) */
151   JDIMENSION crop_height;	/* Height of selected region */
152   JCROP_CODE crop_height_set;	/* (force disables adjustment) */
153   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) */
157 
158   /* Drop parameters: set by caller for drop request */
159   j_decompress_ptr drop_ptr;
160   jvirt_barray_ptr * drop_coef_arrays;
161 
162   /* Internal workspace: caller should not touch these */
163   int num_components;		/* # of components in workspace */
164   jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
165   JDIMENSION output_width;	/* cropped destination dimensions */
166   JDIMENSION output_height;
167   JDIMENSION x_crop_offset;	/* destination crop offsets measured in iMCUs */
168   JDIMENSION y_crop_offset;
169   JDIMENSION drop_width;	/* drop/wipe dimensions measured in iMCUs */
170   JDIMENSION drop_height;
171   int iMCU_sample_width;	/* destination iMCU size */
172   int iMCU_sample_height;
173 } jpeg_transform_info;
174 
175 
176 #if TRANSFORMS_SUPPORTED
177 
178 /* Parse a crop specification (written in X11 geometry style) */
179 EXTERN(boolean) jtransform_parse_crop_spec
180 	JPP((jpeg_transform_info *info, const char *spec));
181 /* Request any required workspace */
182 EXTERN(boolean) jtransform_request_workspace
183 	JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
184 /* Adjust output image parameters */
185 EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
186 	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
187 	     jvirt_barray_ptr *src_coef_arrays,
188 	     jpeg_transform_info *info));
189 /* Execute the actual transformation, if any */
190 EXTERN(void) jtransform_execute_transform
191 	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
192 	     jvirt_barray_ptr *src_coef_arrays,
193 	     jpeg_transform_info *info));
194 /* Determine whether lossless transformation is perfectly
195  * possible for a specified image and transformation.
196  */
197 EXTERN(boolean) jtransform_perfect_transform
198 	JPP((JDIMENSION image_width, JDIMENSION image_height,
199 	     int MCU_width, int MCU_height,
200 	     JXFORM_CODE transform));
201 
202 /* jtransform_execute_transform used to be called
203  * jtransform_execute_transformation, but some compilers complain about
204  * routine names that long.  This macro is here to avoid breaking any
205  * old source code that uses the original name...
206  */
207 #define jtransform_execute_transformation	jtransform_execute_transform
208 
209 #endif /* TRANSFORMS_SUPPORTED */
210 
211 
212 /*
213  * Support for copying optional markers from source to destination file.
214  */
215 
216 typedef enum {
217 	JCOPYOPT_NONE,		/* copy no optional markers */
218 	JCOPYOPT_COMMENTS,	/* copy only comment (COM) markers */
219 	JCOPYOPT_ALL		/* copy all optional markers */
220 } JCOPY_OPTION;
221 
222 #define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS	/* recommended default */
223 
224 /* Setup decompression object to save desired markers in memory */
225 EXTERN(void) jcopy_markers_setup
226 	JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
227 /* Copy markers saved in the given source object to the destination object */
228 EXTERN(void) jcopy_markers_execute
229 	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
230 	     JCOPY_OPTION option));
231