File indexing completed on 2025-03-09 03:55:08

0001 /*
0002  * transupp.h
0003  *
0004  * SPDX-FileCopyrightText: 1997-2011, Thomas G. Lane, Guido Vollbeding.
0005  * This file is part of the Independent JPEG Group's software.
0006  * For conditions of distribution and use, see the accompanying README file.
0007  *
0008  * This file contains declarations for image transformation routines and
0009  * other utility code used by the jpegtran sample application.  These are
0010  * NOT part of the core JPEG library.  But we keep these routines separate
0011  * from jpegtran.c to ease the task of maintaining jpegtran-like programs
0012  * that have other user interfaces.
0013  *
0014  * NOTE: all the routines declared here have very specific requirements
0015  * about when they are to be executed during the reading and writing of the
0016  * source and destination files.  See the comments in transupp.c, or see
0017  * jpegtran.c for an example of correct usage.
0018  */
0019 
0020 /* If you happen not to want the image transform support, disable it here */
0021 #ifndef TRANSFORMS_SUPPORTED
0022 #define TRANSFORMS_SUPPORTED 1      /* 0 disables transform code */
0023 #endif
0024 
0025 /*
0026  * Although rotating and flipping data expressed as DCT coefficients is not
0027  * hard, there is an asymmetry in the JPEG format specification for images
0028  * whose dimensions aren't multiples of the iMCU size.  The right and bottom
0029  * image edges are padded out to the next iMCU boundary with junk data; but
0030  * no padding is possible at the top and left edges.  If we were to flip
0031  * the whole image including the pad data, then pad garbage would become
0032  * visible at the top and/or left, and real pixels would disappear into the
0033  * pad margins --- perhaps permanently, since encoders & decoders may not
0034  * bother to preserve DCT blocks that appear to be completely outside the
0035  * nominal image area.  So, we have to exclude any partial iMCUs from the
0036  * basic transformation.
0037  *
0038  * Transpose is the only transformation that can handle partial iMCUs at the
0039  * right and bottom edges completely cleanly.  flip_h can flip partial iMCUs
0040  * at the bottom, but leaves any partial iMCUs at the right edge untouched.
0041  * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
0042  * The other transforms are defined as combinations of these basic transforms
0043  * and process edge blocks in a way that preserves the equivalence.
0044  *
0045  * The "trim" option causes untransformable partial iMCUs to be dropped;
0046  * this is not strictly lossless, but it usually gives the best-looking
0047  * result for odd-size images.  Note that when this option is active,
0048  * the expected mathematical equivalences between the transforms may not hold.
0049  * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
0050  * followed by -rot 180 -trim trims both edges.)
0051  *
0052  * We also offer a lossless-crop option, which discards data outside a given
0053  * image region but losslessly preserves what is inside.  Like the rotate and
0054  * flip transforms, lossless crop is restricted by the JPEG format: the upper
0055  * left corner of the selected region must fall on an iMCU boundary.  If this
0056  * does not hold for the given crop parameters, we silently move the upper left
0057  * corner up and/or left to make it so, simultaneously increasing the region
0058  * dimensions to keep the lower right crop corner unchanged.  (Thus, the
0059  * output image covers at least the requested region, but may cover more.)
0060  * The adjustment of the region dimensions may be optionally disabled.
0061  *
0062  * We also provide a lossless-resize option, which is kind of a lossless-crop
0063  * operation in the DCT coefficient block domain - it discards higher-order
0064  * coefficients and losslessly preserves lower-order coefficients of a
0065  * sub-block.
0066  *
0067  * Rotate/flip transform, resize, and crop can be requested together in a
0068  * single invocation.  The crop is applied last --- that is, the crop region
0069  * is specified in terms of the destination image after transform/resize.
0070  *
0071  * We also offer a "force to grayscale" option, which simply discards the
0072  * chrominance channels of a YCbCr image.  This is lossless in the sense that
0073  * the luminance channel is preserved exactly.  It's not the same kind of
0074  * thing as the rotate/flip transformations, but it's convenient to handle it
0075  * as part of this package, mainly because the transformation routines have to
0076  * be aware of the option to know how many components to work on.
0077  */
0078 
0079 
0080 /* Short forms of external names for systems with brain-damaged linkers. */
0081 
0082 #ifdef NEED_SHORT_EXTERNAL_NAMES
0083 #define jtransform_parse_crop_spec  jTrParCrop
0084 #define jtransform_request_workspace    jTrRequest
0085 #define jtransform_adjust_parameters    jTrAdjust
0086 #define jtransform_execute_transform    jTrExec
0087 #define jtransform_perfect_transform    jTrPerfect
0088 #define jcopy_markers_setup     jCMrkSetup
0089 #define jcopy_markers_execute       jCMrkExec
0090 #endif /* NEED_SHORT_EXTERNAL_NAMES */
0091 
0092 
0093 /*
0094  * Codes for supported types of image transformations.
0095  */
0096 
0097 typedef enum {
0098     JXFORM_NONE,        /* no transformation */
0099     JXFORM_FLIP_H,      /* horizontal flip */
0100     JXFORM_FLIP_V,      /* vertical flip */
0101     JXFORM_TRANSPOSE,   /* transpose across UL-to-LR axis */
0102     JXFORM_TRANSVERSE,  /* transpose across UR-to-LL axis */
0103     JXFORM_ROT_90,      /* 90-degree clockwise rotation */
0104     JXFORM_ROT_180,     /* 180-degree rotation */
0105     JXFORM_ROT_270      /* 270-degree clockwise (or 90 ccw) */
0106 } JXFORM_CODE;
0107 
0108 /*
0109  * Codes for crop parameters, which can individually be unspecified,
0110  * positive or negative for xoffset or yoffset,
0111  * positive or forced for width or height.
0112  */
0113 
0114 typedef enum {
0115         JCROP_UNSET,
0116         JCROP_POS,
0117         JCROP_NEG,
0118         JCROP_FORCE
0119 } JCROP_CODE;
0120 
0121 /*
0122  * Transform parameters struct.
0123  * NB: application must not change any elements of this struct after
0124  * calling jtransform_request_workspace.
0125  */
0126 
0127 typedef struct {
0128   /* Options: set by caller */
0129   JXFORM_CODE transform;    /* image transform operator */
0130   boolean perfect;      /* if TRUE, fail if partial MCUs are requested */
0131   boolean trim;         /* if TRUE, trim partial MCUs as needed */
0132   boolean force_grayscale;  /* if TRUE, convert color image to grayscale */
0133   boolean crop;         /* if TRUE, crop source image */
0134 
0135   /* Crop parameters: application need not set these unless crop is TRUE.
0136    * These can be filled in by jtransform_parse_crop_spec().
0137    */
0138   JDIMENSION crop_width;    /* Width of selected region */
0139   JCROP_CODE crop_width_set;    /* (forced disables adjustment) */
0140   JDIMENSION crop_height;   /* Height of selected region */
0141   JCROP_CODE crop_height_set;   /* (forced disables adjustment) */
0142   JDIMENSION crop_xoffset;  /* X offset of selected region */
0143   JCROP_CODE crop_xoffset_set;  /* (negative measures from right edge) */
0144   JDIMENSION crop_yoffset;  /* Y offset of selected region */
0145   JCROP_CODE crop_yoffset_set;  /* (negative measures from bottom edge) */
0146 
0147   /* Internal workspace: caller should not touch these */
0148   int num_components;       /* # of components in workspace */
0149   jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
0150   JDIMENSION output_width;  /* cropped destination dimensions */
0151   JDIMENSION output_height;
0152   JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */
0153   JDIMENSION y_crop_offset;
0154   int iMCU_sample_width;    /* destination iMCU size */
0155   int iMCU_sample_height;
0156 } jpeg_transform_info;
0157 
0158 
0159 #if TRANSFORMS_SUPPORTED
0160 
0161 /* Parse a crop specification (written in X11 geometry style) */
0162 EXTERN(boolean) jtransform_parse_crop_spec
0163     JPP((jpeg_transform_info *info, const char *spec));
0164 /* Request any required workspace */
0165 EXTERN(boolean) jtransform_request_workspace
0166     JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
0167 /* Adjust output image parameters */
0168 EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
0169     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0170          jvirt_barray_ptr *src_coef_arrays,
0171          jpeg_transform_info *info));
0172 /* Execute the actual transformation, if any */
0173 EXTERN(void) jtransform_execute_transform
0174     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0175          jvirt_barray_ptr *src_coef_arrays,
0176          jpeg_transform_info *info));
0177 /* Determine whether lossless transformation is perfectly
0178  * possible for a specified image and transformation.
0179  */
0180 EXTERN(boolean) jtransform_perfect_transform
0181     JPP((JDIMENSION image_width, JDIMENSION image_height,
0182          int MCU_width, int MCU_height,
0183          JXFORM_CODE transform));
0184 
0185 /* jtransform_execute_transform used to be called
0186  * jtransform_execute_transformation, but some compilers complain about
0187  * routine names that long.  This macro is here to avoid breaking any
0188  * old source code that uses the original name...
0189  */
0190 #define jtransform_execute_transformation   jtransform_execute_transform
0191 
0192 #endif /* TRANSFORMS_SUPPORTED */
0193 
0194 
0195 /*
0196  * Support for copying optional markers from source to destination file.
0197  */
0198 
0199 typedef enum {
0200     JCOPYOPT_NONE,      /* copy no optional markers */
0201     JCOPYOPT_COMMENTS,  /* copy only comment (COM) markers */
0202     JCOPYOPT_ALL        /* copy all optional markers */
0203 } JCOPY_OPTION;
0204 
0205 #define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS /* recommended default */
0206 
0207 /* Setup decompression object to save desired markers in memory */
0208 EXTERN(void) jcopy_markers_setup
0209     JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
0210 /* Copy markers saved in the given source object to the destination object */
0211 EXTERN(void) jcopy_markers_execute
0212     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0213          JCOPY_OPTION option));