File indexing completed on 2024-05-12 04:19:46

0001 /*
0002  * transupp.h
0003  *
0004  * Copyright (C) 1997-2013, 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 current JPEG format: the
0055  * upper left corner of the selected region must fall on an iMCU boundary.  If
0056  * this does not hold for the given crop parameters, we silently move the upper
0057  * left corner up and/or left to make it so, simultaneously increasing the
0058  * region 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  * A complementary lossless-wipe option is provided to discard (gray out) data
0063  * inside a given image region while losslessly preserving what is outside.
0064  *
0065  * We also provide a lossless-resize option, which is kind of a lossless-crop
0066  * operation in the DCT coefficient block domain - it discards higher-order
0067  * coefficients and losslessly preserves lower-order coefficients of a
0068  * sub-block.
0069  *
0070  * Rotate/flip transform, resize, and crop can be requested together in a
0071  * single invocation.  The crop is applied last --- that is, the crop region
0072  * is specified in terms of the destination image after transform/resize.
0073  *
0074  * We also offer a "force to grayscale" option, which simply discards the
0075  * chrominance channels of a YCbCr image.  This is lossless in the sense that
0076  * the luminance channel is preserved exactly.  It's not the same kind of
0077  * thing as the rotate/flip transformations, but it's convenient to handle it
0078  * as part of this package, mainly because the transformation routines have to
0079  * be aware of the option to know how many components to work on.
0080  */
0081 
0082 
0083 /* Short forms of external names for systems with brain-damaged linkers. */
0084 
0085 #ifdef NEED_SHORT_EXTERNAL_NAMES
0086 #define jtransform_parse_crop_spec  jTrParCrop
0087 #define jtransform_request_workspace    jTrRequest
0088 #define jtransform_adjust_parameters    jTrAdjust
0089 #define jtransform_execute_transform    jTrExec
0090 #define jtransform_perfect_transform    jTrPerfect
0091 #define jcopy_markers_setup     jCMrkSetup
0092 #define jcopy_markers_execute       jCMrkExec
0093 #endif /* NEED_SHORT_EXTERNAL_NAMES */
0094 
0095 
0096 /*
0097  * Codes for supported types of image transformations.
0098  */
0099 
0100 typedef enum {
0101     JXFORM_NONE,        /* no transformation */
0102     JXFORM_FLIP_H,      /* horizontal flip */
0103     JXFORM_FLIP_V,      /* vertical flip */
0104     JXFORM_TRANSPOSE,   /* transpose across UL-to-LR axis */
0105     JXFORM_TRANSVERSE,  /* transpose across UR-to-LL axis */
0106     JXFORM_ROT_90,      /* 90-degree clockwise rotation */
0107     JXFORM_ROT_180,     /* 180-degree rotation */
0108     JXFORM_ROT_270,     /* 270-degree clockwise (or 90 ccw) */
0109     JXFORM_WIPE     /* wipe */
0110 } JXFORM_CODE;
0111 
0112 /*
0113  * Codes for crop parameters, which can individually be unspecified,
0114  * positive or negative for xoffset or yoffset,
0115  * positive or forced for width or height.
0116  */
0117 
0118 typedef enum {
0119         JCROP_UNSET,
0120         JCROP_POS,
0121         JCROP_NEG,
0122         JCROP_FORCE
0123 } JCROP_CODE;
0124 
0125 /*
0126  * Transform parameters struct.
0127  * NB: application must not change any elements of this struct after
0128  * calling jtransform_request_workspace.
0129  */
0130 
0131 typedef struct {
0132   /* Options: set by caller */
0133   JXFORM_CODE transform;    /* image transform operator */
0134   boolean perfect;      /* if TRUE, fail if partial MCUs are requested */
0135   boolean trim;         /* if TRUE, trim partial MCUs as needed */
0136   boolean force_grayscale;  /* if TRUE, convert color image to grayscale */
0137   boolean crop;         /* if TRUE, crop or wipe source image */
0138 
0139   /* Crop parameters: application need not set these unless crop is TRUE.
0140    * These can be filled in by jtransform_parse_crop_spec().
0141    */
0142   JDIMENSION crop_width;    /* Width of selected region */
0143   JCROP_CODE crop_width_set;    /* (forced disables adjustment) */
0144   JDIMENSION crop_height;   /* Height of selected region */
0145   JCROP_CODE crop_height_set;   /* (forced disables adjustment) */
0146   JDIMENSION crop_xoffset;  /* X offset of selected region */
0147   JCROP_CODE crop_xoffset_set;  /* (negative measures from right edge) */
0148   JDIMENSION crop_yoffset;  /* Y offset of selected region */
0149   JCROP_CODE crop_yoffset_set;  /* (negative measures from bottom edge) */
0150 
0151   /* Internal workspace: caller should not touch these */
0152   int num_components;       /* # of components in workspace */
0153   jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
0154   JDIMENSION output_width;  /* cropped destination dimensions */
0155   JDIMENSION output_height;
0156   JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */
0157   JDIMENSION y_crop_offset;
0158   JDIMENSION drop_width;    /* drop/wipe dimensions measured in iMCUs */
0159   JDIMENSION drop_height;
0160   int iMCU_sample_width;    /* destination iMCU size */
0161   int iMCU_sample_height;
0162 } jpeg_transform_info;
0163 
0164 
0165 #if TRANSFORMS_SUPPORTED
0166 
0167 /* Parse a crop specification (written in X11 geometry style) */
0168 EXTERN(boolean) jtransform_parse_crop_spec
0169     JPP((jpeg_transform_info *info, const char *spec));
0170 /* Request any required workspace */
0171 EXTERN(boolean) jtransform_request_workspace
0172     JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
0173 /* Adjust output image parameters */
0174 EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
0175     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0176          jvirt_barray_ptr *src_coef_arrays,
0177          jpeg_transform_info *info));
0178 /* Execute the actual transformation, if any */
0179 EXTERN(void) jtransform_execute_transform
0180     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0181          jvirt_barray_ptr *src_coef_arrays,
0182          jpeg_transform_info *info));
0183 /* Determine whether lossless transformation is perfectly
0184  * possible for a specified image and transformation.
0185  */
0186 EXTERN(boolean) jtransform_perfect_transform
0187     JPP((JDIMENSION image_width, JDIMENSION image_height,
0188          int MCU_width, int MCU_height,
0189          JXFORM_CODE transform));
0190 
0191 /* jtransform_execute_transform used to be called
0192  * jtransform_execute_transformation, but some compilers complain about
0193  * routine names that long.  This macro is here to avoid breaking any
0194  * old source code that uses the original name...
0195  */
0196 #define jtransform_execute_transformation   jtransform_execute_transform
0197 
0198 #endif /* TRANSFORMS_SUPPORTED */
0199 
0200 
0201 /*
0202  * Support for copying optional markers from source to destination file.
0203  */
0204 
0205 typedef enum {
0206     JCOPYOPT_NONE,      /* copy no optional markers */
0207     JCOPYOPT_COMMENTS,  /* copy only comment (COM) markers */
0208     JCOPYOPT_ALL        /* copy all optional markers */
0209 } JCOPY_OPTION;
0210 
0211 #define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS /* recommended default */
0212 
0213 /* Setup decompression object to save desired markers in memory */
0214 EXTERN(void) jcopy_markers_setup
0215     JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
0216 /* Copy markers saved in the given source object to the destination object */
0217 EXTERN(void) jcopy_markers_execute
0218     JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
0219          JCOPY_OPTION option));