transupp.h 9.9 KB

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