transupp.h 11 KB

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