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