kusano 7d535a
/*
kusano 7d535a
 * transupp.h
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README 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
kusano 7d535a
#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
/* Short forms of external names for systems with brain-damaged linkers. */
kusano 7d535a
kusano 7d535a
#ifdef NEED_SHORT_EXTERNAL_NAMES
kusano 7d535a
#define jtransform_parse_crop_spec	jTrParCrop
kusano 7d535a
#define jtransform_request_workspace	jTrRequest
kusano 7d535a
#define jtransform_adjust_parameters	jTrAdjust
kusano 7d535a
#define jtransform_execute_transform	jTrExec
kusano 7d535a
#define jtransform_perfect_transform	jTrPerfect
kusano 7d535a
#define jcopy_markers_setup		jCMrkSetup
kusano 7d535a
#define jcopy_markers_execute		jCMrkExec
kusano 7d535a
#endif /* NEED_SHORT_EXTERNAL_NAMES */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Codes for supported types of image transformations.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef enum {
kusano 7d535a
	JXFORM_NONE,		/* no transformation */
kusano 7d535a
	JXFORM_FLIP_H,		/* horizontal flip */
kusano 7d535a
	JXFORM_FLIP_V,		/* vertical flip */
kusano 7d535a
	JXFORM_TRANSPOSE,	/* transpose across UL-to-LR axis */
kusano 7d535a
	JXFORM_TRANSVERSE,	/* transpose across UR-to-LL axis */
kusano 7d535a
	JXFORM_ROT_90,		/* 90-degree clockwise rotation */
kusano 7d535a
	JXFORM_ROT_180,		/* 180-degree rotation */
kusano 7d535a
	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 {
kusano 7d535a
        JCROP_UNSET,
kusano 7d535a
        JCROP_POS,
kusano 7d535a
        JCROP_NEG,
kusano 7d535a
        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 */
kusano 7d535a
  JXFORM_CODE transform;	/* image transform operator */
kusano 7d535a
  boolean perfect;		/* if TRUE, fail if partial MCUs are requested */
kusano 7d535a
  boolean trim;			/* if TRUE, trim partial MCUs as needed */
kusano 7d535a
  boolean force_grayscale;	/* if TRUE, convert color image to grayscale */
kusano 7d535a
  boolean crop;			/* if TRUE, crop source image */
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
   */
kusano 7d535a
  JDIMENSION crop_width;	/* Width of selected region */
kusano 7d535a
  JCROP_CODE crop_width_set;	/* (forced disables adjustment) */
kusano 7d535a
  JDIMENSION crop_height;	/* Height of selected region */
kusano 7d535a
  JCROP_CODE crop_height_set;	/* (forced disables adjustment) */
kusano 7d535a
  JDIMENSION crop_xoffset;	/* X offset of selected region */
kusano 7d535a
  JCROP_CODE crop_xoffset_set;	/* (negative measures from right edge) */
kusano 7d535a
  JDIMENSION crop_yoffset;	/* Y offset of selected region */
kusano 7d535a
  JCROP_CODE crop_yoffset_set;	/* (negative measures from bottom edge) */
kusano 7d535a
kusano 7d535a
  /* Internal workspace: caller should not touch these */
kusano 7d535a
  int num_components;		/* # of components in workspace */
kusano 7d535a
  jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
kusano 7d535a
  JDIMENSION output_width;	/* cropped destination dimensions */
kusano 7d535a
  JDIMENSION output_height;
kusano 7d535a
  JDIMENSION x_crop_offset;	/* destination crop offsets measured in iMCUs */
kusano 7d535a
  JDIMENSION y_crop_offset;
kusano 7d535a
  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) */
kusano 7d535a
EXTERN(boolean) jtransform_parse_crop_spec
kusano 7d535a
	JPP((jpeg_transform_info *info, const char *spec));
kusano 7d535a
/* Request any required workspace */
kusano 7d535a
EXTERN(boolean) jtransform_request_workspace
kusano 7d535a
	JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
kusano 7d535a
/* Adjust output image parameters */
kusano 7d535a
EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
kusano 7d535a
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	     jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	     jpeg_transform_info *info));
kusano 7d535a
/* Execute the actual transformation, if any */
kusano 7d535a
EXTERN(void) jtransform_execute_transform
kusano 7d535a
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	     jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	     jpeg_transform_info *info));
kusano 7d535a
/* Determine whether lossless transformation is perfectly
kusano 7d535a
 * possible for a specified image and transformation.
kusano 7d535a
 */
kusano 7d535a
EXTERN(boolean) jtransform_perfect_transform
kusano 7d535a
	JPP((JDIMENSION image_width, JDIMENSION image_height,
kusano 7d535a
	     int MCU_width, int MCU_height,
kusano 7d535a
	     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
 */
kusano 7d535a
#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 {
kusano 7d535a
	JCOPYOPT_NONE,		/* copy no optional markers */
kusano 7d535a
	JCOPYOPT_COMMENTS,	/* copy only comment (COM) markers */
kusano 7d535a
	JCOPYOPT_ALL		/* copy all optional markers */
kusano 7d535a
} JCOPY_OPTION;
kusano 7d535a
kusano 7d535a
#define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS	/* recommended default */
kusano 7d535a
kusano 7d535a
/* Setup decompression object to save desired markers in memory */
kusano 7d535a
EXTERN(void) jcopy_markers_setup
kusano 7d535a
	JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
kusano 7d535a
/* Copy markers saved in the given source object to the destination object */
kusano 7d535a
EXTERN(void) jcopy_markers_execute
kusano 7d535a
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	     JCOPY_OPTION option));