kusano 7d535a
/*
kusano 7d535a
 * transupp.h
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2017, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains declarations for image transformation routines and
kusano 7d535a
 * other utility code used by the jpegtran sample application.  These are
kusano 7d535a
 * NOT part of the core JPEG library.  But we keep these routines separate
kusano 7d535a
 * from jpegtran.c to ease the task of maintaining jpegtran-like programs
kusano 7d535a
 * that have other user interfaces.
kusano 7d535a
 *
kusano 7d535a
 * NOTE: all the routines declared here have very specific requirements
kusano 7d535a
 * about when they are to be executed during the reading and writing of the
kusano 7d535a
 * source and destination files.  See the comments in transupp.c, or see
kusano 7d535a
 * jpegtran.c for an example of correct usage.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* If you happen not to want the image transform support, disable it here */
kusano 7d535a
#ifndef TRANSFORMS_SUPPORTED
shun-iwasawa 82a8f5
#define TRANSFORMS_SUPPORTED  1         /* 0 disables transform code */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Although rotating and flipping data expressed as DCT coefficients is not
kusano 7d535a
 * hard, there is an asymmetry in the JPEG format specification for images
kusano 7d535a
 * whose dimensions aren't multiples of the iMCU size.  The right and bottom
kusano 7d535a
 * image edges are padded out to the next iMCU boundary with junk data; but
kusano 7d535a
 * no padding is possible at the top and left edges.  If we were to flip
kusano 7d535a
 * the whole image including the pad data, then pad garbage would become
kusano 7d535a
 * visible at the top and/or left, and real pixels would disappear into the
kusano 7d535a
 * pad margins --- perhaps permanently, since encoders & decoders may not
kusano 7d535a
 * bother to preserve DCT blocks that appear to be completely outside the
kusano 7d535a
 * nominal image area.  So, we have to exclude any partial iMCUs from the
kusano 7d535a
 * basic transformation.
kusano 7d535a
 *
kusano 7d535a
 * Transpose is the only transformation that can handle partial iMCUs at the
kusano 7d535a
 * right and bottom edges completely cleanly.  flip_h can flip partial iMCUs
kusano 7d535a
 * at the bottom, but leaves any partial iMCUs at the right edge untouched.
kusano 7d535a
 * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
kusano 7d535a
 * The other transforms are defined as combinations of these basic transforms
kusano 7d535a
 * and process edge blocks in a way that preserves the equivalence.
kusano 7d535a
 *
kusano 7d535a
 * The "trim" option causes untransformable partial iMCUs to be dropped;
kusano 7d535a
 * this is not strictly lossless, but it usually gives the best-looking
kusano 7d535a
 * result for odd-size images.  Note that when this option is active,
kusano 7d535a
 * the expected mathematical equivalences between the transforms may not hold.
kusano 7d535a
 * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
kusano 7d535a
 * followed by -rot 180 -trim trims both edges.)
kusano 7d535a
 *
kusano 7d535a
 * We also offer a lossless-crop option, which discards data outside a given
kusano 7d535a
 * image region but losslessly preserves what is inside.  Like the rotate and
kusano 7d535a
 * flip transforms, lossless crop is restricted by the JPEG format: the upper
kusano 7d535a
 * left corner of the selected region must fall on an iMCU boundary.  If this
kusano 7d535a
 * does not hold for the given crop parameters, we silently move the upper left
kusano 7d535a
 * corner up and/or left to make it so, simultaneously increasing the region
kusano 7d535a
 * dimensions to keep the lower right crop corner unchanged.  (Thus, the
kusano 7d535a
 * output image covers at least the requested region, but may cover more.)
kusano 7d535a
 * The adjustment of the region dimensions may be optionally disabled.
kusano 7d535a
 *
kusano 7d535a
 * We also provide a lossless-resize option, which is kind of a lossless-crop
kusano 7d535a
 * operation in the DCT coefficient block domain - it discards higher-order
kusano 7d535a
 * coefficients and losslessly preserves lower-order coefficients of a
kusano 7d535a
 * sub-block.
kusano 7d535a
 *
kusano 7d535a
 * Rotate/flip transform, resize, and crop can be requested together in a
kusano 7d535a
 * single invocation.  The crop is applied last --- that is, the crop region
kusano 7d535a
 * is specified in terms of the destination image after transform/resize.
kusano 7d535a
 *
kusano 7d535a
 * We also offer a "force to grayscale" option, which simply discards the
kusano 7d535a
 * chrominance channels of a YCbCr image.  This is lossless in the sense that
kusano 7d535a
 * the luminance channel is preserved exactly.  It's not the same kind of
kusano 7d535a
 * thing as the rotate/flip transformations, but it's convenient to handle it
kusano 7d535a
 * as part of this package, mainly because the transformation routines have to
kusano 7d535a
 * be aware of the option to know how many components to work on.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Codes for supported types of image transformations.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef enum {
shun-iwasawa 82a8f5
  JXFORM_NONE,            /* no transformation */
shun-iwasawa 82a8f5
  JXFORM_FLIP_H,          /* horizontal flip */
shun-iwasawa 82a8f5
  JXFORM_FLIP_V,          /* vertical flip */
shun-iwasawa 82a8f5
  JXFORM_TRANSPOSE,       /* transpose across UL-to-LR axis */
shun-iwasawa 82a8f5
  JXFORM_TRANSVERSE,      /* transpose across UR-to-LL axis */
shun-iwasawa 82a8f5
  JXFORM_ROT_90,          /* 90-degree clockwise rotation */
shun-iwasawa 82a8f5
  JXFORM_ROT_180,         /* 180-degree rotation */
shun-iwasawa 82a8f5
  JXFORM_ROT_270          /* 270-degree clockwise (or 90 ccw) */
kusano 7d535a
} JXFORM_CODE;
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Codes for crop parameters, which can individually be unspecified,
kusano 7d535a
 * positive or negative for xoffset or yoffset,
kusano 7d535a
 * positive or forced for width or height.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef enum {
shun-iwasawa 82a8f5
  JCROP_UNSET,
shun-iwasawa 82a8f5
  JCROP_POS,
shun-iwasawa 82a8f5
  JCROP_NEG,
shun-iwasawa 82a8f5
  JCROP_FORCE
kusano 7d535a
} JCROP_CODE;
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Transform parameters struct.
kusano 7d535a
 * NB: application must not change any elements of this struct after
kusano 7d535a
 * calling jtransform_request_workspace.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  /* Options: set by caller */
shun-iwasawa 82a8f5
  JXFORM_CODE transform;        /* image transform operator */
shun-iwasawa 82a8f5
  boolean perfect;              /* if TRUE, fail if partial MCUs are requested */
shun-iwasawa 82a8f5
  boolean trim;                 /* if TRUE, trim partial MCUs as needed */
shun-iwasawa 82a8f5
  boolean force_grayscale;      /* if TRUE, convert color image to grayscale */
shun-iwasawa 82a8f5
  boolean crop;                 /* if TRUE, crop source image */
shun-iwasawa 82a8f5
  boolean slow_hflip;  /* For best performance, the JXFORM_FLIP_H transform
shun-iwasawa 82a8f5
                          normally modifies the source coefficients in place.
shun-iwasawa 82a8f5
                          Setting this to TRUE will instead use a slower,
shun-iwasawa 82a8f5
                          double-buffered algorithm, which leaves the source
shun-iwasawa 82a8f5
                          coefficients in tact (necessary if other transformed
shun-iwasawa 82a8f5
                          images must be generated from the same set of
shun-iwasawa 82a8f5
                          coefficients. */
kusano 7d535a
kusano 7d535a
  /* Crop parameters: application need not set these unless crop is TRUE.
kusano 7d535a
   * These can be filled in by jtransform_parse_crop_spec().
kusano 7d535a
   */
shun-iwasawa 82a8f5
  JDIMENSION crop_width;        /* Width of selected region */
shun-iwasawa 82a8f5
  JCROP_CODE crop_width_set;    /* (forced disables adjustment) */
shun-iwasawa 82a8f5
  JDIMENSION crop_height;       /* Height of selected region */
shun-iwasawa 82a8f5
  JCROP_CODE crop_height_set;   /* (forced disables adjustment) */
shun-iwasawa 82a8f5
  JDIMENSION crop_xoffset;      /* X offset of selected region */
shun-iwasawa 82a8f5
  JCROP_CODE crop_xoffset_set;  /* (negative measures from right edge) */
shun-iwasawa 82a8f5
  JDIMENSION crop_yoffset;      /* Y offset of selected region */
shun-iwasawa 82a8f5
  JCROP_CODE crop_yoffset_set;  /* (negative measures from bottom edge) */
kusano 7d535a
kusano 7d535a
  /* Internal workspace: caller should not touch these */
shun-iwasawa 82a8f5
  int num_components;           /* # of components in workspace */
shun-iwasawa 82a8f5
  jvirt_barray_ptr *workspace_coef_arrays; /* workspace for transformations */
shun-iwasawa 82a8f5
  JDIMENSION output_width;      /* cropped destination dimensions */
kusano 7d535a
  JDIMENSION output_height;
shun-iwasawa 82a8f5
  JDIMENSION x_crop_offset;     /* destination crop offsets measured in iMCUs */
kusano 7d535a
  JDIMENSION y_crop_offset;
shun-iwasawa 82a8f5
  int iMCU_sample_width;        /* destination iMCU size */
kusano 7d535a
  int iMCU_sample_height;
kusano 7d535a
} jpeg_transform_info;
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
kusano 7d535a
/* Parse a crop specification (written in X11 geometry style) */
shun-iwasawa 82a8f5
EXTERN(boolean) jtransform_parse_crop_spec(jpeg_transform_info *info,
shun-iwasawa 82a8f5
                                           const char *spec);
kusano 7d535a
/* Request any required workspace */
shun-iwasawa 82a8f5
EXTERN(boolean) jtransform_request_workspace(j_decompress_ptr srcinfo,
shun-iwasawa 82a8f5
                                             jpeg_transform_info *info);
kusano 7d535a
/* Adjust output image parameters */
kusano 7d535a
EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
shun-iwasawa 82a8f5
  (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
shun-iwasawa 82a8f5
   jvirt_barray_ptr *src_coef_arrays, jpeg_transform_info *info);
kusano 7d535a
/* Execute the actual transformation, if any */
shun-iwasawa 82a8f5
EXTERN(void) jtransform_execute_transform(j_decompress_ptr srcinfo,
shun-iwasawa 82a8f5
                                          j_compress_ptr dstinfo,
shun-iwasawa 82a8f5
                                          jvirt_barray_ptr *src_coef_arrays,
shun-iwasawa 82a8f5
                                          jpeg_transform_info *info);
kusano 7d535a
/* Determine whether lossless transformation is perfectly
kusano 7d535a
 * possible for a specified image and transformation.
kusano 7d535a
 */
shun-iwasawa 82a8f5
EXTERN(boolean) jtransform_perfect_transform(JDIMENSION image_width,
shun-iwasawa 82a8f5
                                             JDIMENSION image_height,
shun-iwasawa 82a8f5
                                             int MCU_width, int MCU_height,
shun-iwasawa 82a8f5
                                             JXFORM_CODE transform);
kusano 7d535a
kusano 7d535a
/* jtransform_execute_transform used to be called
kusano 7d535a
 * jtransform_execute_transformation, but some compilers complain about
kusano 7d535a
 * routine names that long.  This macro is here to avoid breaking any
kusano 7d535a
 * old source code that uses the original name...
kusano 7d535a
 */
shun-iwasawa 82a8f5
#define jtransform_execute_transformation       jtransform_execute_transform
kusano 7d535a
kusano 7d535a
#endif /* TRANSFORMS_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Support for copying optional markers from source to destination file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef enum {
shun-iwasawa 82a8f5
  JCOPYOPT_NONE,          /* copy no optional markers */
shun-iwasawa 82a8f5
  JCOPYOPT_COMMENTS,      /* copy only comment (COM) markers */
shun-iwasawa 82a8f5
  JCOPYOPT_ALL,           /* copy all optional markers */
shun-iwasawa 82a8f5
  JCOPYOPT_ALL_EXCEPT_ICC /* copy all optional markers except APP2 */
kusano 7d535a
} JCOPY_OPTION;
kusano 7d535a
shun-iwasawa 82a8f5
#define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS     /* recommended default */
kusano 7d535a
kusano 7d535a
/* Setup decompression object to save desired markers in memory */
shun-iwasawa 82a8f5
EXTERN(void) jcopy_markers_setup(j_decompress_ptr srcinfo,
shun-iwasawa 82a8f5
                                 JCOPY_OPTION option);
kusano 7d535a
/* Copy markers saved in the given source object to the destination object */
shun-iwasawa 82a8f5
EXTERN(void) jcopy_markers_execute(j_decompress_ptr srcinfo,
shun-iwasawa 82a8f5
                                   j_compress_ptr dstinfo,
shun-iwasawa 82a8f5
                                   JCOPY_OPTION option);