File indexing completed on 2024-05-19 04:21:26

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