kusano 7d535a
/*
kusano 7d535a
 * transupp.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1997-2012, 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 image transformation routines and other utility code
kusano 7d535a
 * used by the jpegtran sample application.  These are NOT part of the core
kusano 7d535a
 * JPEG library.  But we keep these routines separate from jpegtran.c to
kusano 7d535a
 * ease the task of maintaining jpegtran-like programs that have other user
kusano 7d535a
 * interfaces.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* Although this file really shouldn't have access to the library internals,
kusano 7d535a
 * it's helpful to let it call jround_up() and jcopy_block_row().
kusano 7d535a
 */
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "transupp.h"		/* My own external interface */
kusano 7d535a
#include <ctype.h>		/* to declare isdigit() */</ctype.h>
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Lossless image transformation routines.  These routines work on DCT
kusano 7d535a
 * coefficient arrays and thus do not require any lossy decompression
kusano 7d535a
 * or recompression of the image.
kusano 7d535a
 * Thanks to Guido Vollbeding for the initial design and code of this feature,
kusano 7d535a
 * and to Ben Jackson for introducing the cropping feature.
kusano 7d535a
 *
kusano 7d535a
 * Horizontal flipping is done in-place, using a single top-to-bottom
kusano 7d535a
 * pass through the virtual source array.  It will thus be much the
kusano 7d535a
 * fastest option for images larger than main memory.
kusano 7d535a
 *
kusano 7d535a
 * The other routines require a set of destination virtual arrays, so they
kusano 7d535a
 * need twice as much memory as jpegtran normally does.  The destination
kusano 7d535a
 * arrays are always written in normal scan order (top to bottom) because
kusano 7d535a
 * the virtual array manager expects this.  The source arrays will be scanned
kusano 7d535a
 * in the corresponding order, which means multiple passes through the source
kusano 7d535a
 * arrays for most of the transforms.  That could result in much thrashing
kusano 7d535a
 * if the image is larger than main memory.
kusano 7d535a
 *
kusano 7d535a
 * If cropping or trimming is involved, the destination arrays may be smaller
kusano 7d535a
 * than the source arrays.  Note it is not possible to do horizontal flip
kusano 7d535a
 * in-place when a nonzero Y crop offset is specified, since we'd have to move
kusano 7d535a
 * data from one block row to another but the virtual array manager doesn't
kusano 7d535a
 * guarantee we can touch more than one row at a time.  So in that case,
kusano 7d535a
 * we have to use a separate destination array.
kusano 7d535a
 *
kusano 7d535a
 * Some notes about the operating environment of the individual transform
kusano 7d535a
 * routines:
kusano 7d535a
 * 1. Both the source and destination virtual arrays are allocated from the
kusano 7d535a
 *    source JPEG object, and therefore should be manipulated by calling the
kusano 7d535a
 *    source's memory manager.
kusano 7d535a
 * 2. The destination's component count should be used.  It may be smaller
kusano 7d535a
 *    than the source's when forcing to grayscale.
kusano 7d535a
 * 3. Likewise the destination's sampling factors should be used.  When
kusano 7d535a
 *    forcing to grayscale the destination's sampling factors will be all 1,
kusano 7d535a
 *    and we may as well take that as the effective iMCU size.
kusano 7d535a
 * 4. When "trim" is in effect, the destination's dimensions will be the
kusano 7d535a
 *    trimmed values but the source's will be untrimmed.
kusano 7d535a
 * 5. When "crop" is in effect, the destination's dimensions will be the
kusano 7d535a
 *    cropped values but the source's will be uncropped.  Each transform
kusano 7d535a
 *    routine is responsible for picking up source data starting at the
kusano 7d535a
 *    correct X and Y offset for the crop region.  (The X and Y offsets
kusano 7d535a
 *    passed to the transform routines are measured in iMCU blocks of the
kusano 7d535a
 *    destination.)
kusano 7d535a
 * 6. All the routines assume that the source and destination buffers are
kusano 7d535a
 *    padded out to a full iMCU boundary.  This is true, although for the
kusano 7d535a
 *    source buffer it is an undocumented property of jdcoefct.c.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	 JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	 jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	 jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* Crop.  This is only used when no rotate/flip is requested with the crop. */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* We simply have to copy the right amount of data (the destination's
kusano 7d535a
   * image size) starting at the given X and Y offsets in the source.
kusano 7d535a
   */
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	 dst_blk_y + y_crop_blocks,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
kusano 7d535a
			dst_buffer[offset_y],
kusano 7d535a
			compptr->width_in_blocks);
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_flip_h_no_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
		   JDIMENSION x_crop_offset,
kusano 7d535a
		   jvirt_barray_ptr *src_coef_arrays)
kusano 7d535a
/* Horizontal flip; done in-place, so no separate dest array is required.
kusano 7d535a
 * NB: this only works when y_crop_offset is zero.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols, comp_width, blk_x, blk_y, x_crop_blocks;
kusano 7d535a
  int ci, k, offset_y;
kusano 7d535a
  JBLOCKARRAY buffer;
kusano 7d535a
  JCOEFPTR ptr1, ptr2;
kusano 7d535a
  JCOEF temp1, temp2;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Horizontal mirroring of DCT blocks is accomplished by swapping
kusano 7d535a
   * pairs of blocks in-place.  Within a DCT block, we perform horizontal
kusano 7d535a
   * mirroring by changing the signs of odd-numbered columns.
kusano 7d535a
   * Partial iMCUs at the right edge are left untouched.
kusano 7d535a
   */
kusano 7d535a
  MCU_cols = srcinfo->output_width /
kusano 7d535a
    (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_width = MCU_cols * compptr->h_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    for (blk_y = 0; blk_y < compptr->height_in_blocks;
kusano 7d535a
	 blk_y += compptr->v_samp_factor) {
kusano 7d535a
      buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	/* Do the mirroring */
kusano 7d535a
	for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
kusano 7d535a
	  ptr1 = buffer[offset_y][blk_x];
kusano 7d535a
	  ptr2 = buffer[offset_y][comp_width - blk_x - 1];
kusano 7d535a
	  /* this unrolled loop doesn't need to know which row it's on... */
kusano 7d535a
	  for (k = 0; k < DCTSIZE2; k += 2) {
kusano 7d535a
	    temp1 = *ptr1;	/* swap even column */
kusano 7d535a
	    temp2 = *ptr2;
kusano 7d535a
	    *ptr1++ = temp2;
kusano 7d535a
	    *ptr2++ = temp1;
kusano 7d535a
	    temp1 = *ptr1;	/* swap odd column with sign change */
kusano 7d535a
	    temp2 = *ptr2;
kusano 7d535a
	    *ptr1++ = -temp2;
kusano 7d535a
	    *ptr2++ = -temp1;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
	if (x_crop_blocks > 0) {
kusano 7d535a
	  /* Now left-justify the portion of the data to be kept.
kusano 7d535a
	   * We can't use a single jcopy_block_row() call because that routine
kusano 7d535a
	   * depends on memcpy(), whose behavior is unspecified for overlapping
kusano 7d535a
	   * source and destination areas.  Sigh.
kusano 7d535a
	   */
kusano 7d535a
	  for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) {
kusano 7d535a
	    jcopy_block_row(buffer[offset_y] + blk_x + x_crop_blocks,
kusano 7d535a
			    buffer[offset_y] + blk_x,
kusano 7d535a
			    (JDIMENSION) 1);
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	   JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	   jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	   jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* Horizontal flip in general cropping case */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, k, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JBLOCKROW src_row_ptr, dst_row_ptr;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Here we must output into a separate array because we can't touch
kusano 7d535a
   * different rows of a single virtual array simultaneously.  Otherwise,
kusano 7d535a
   * this is essentially the same as the routine above.
kusano 7d535a
   */
kusano 7d535a
  MCU_cols = srcinfo->output_width /
kusano 7d535a
    (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_width = MCU_cols * compptr->h_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	 dst_blk_y + y_crop_blocks,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	dst_row_ptr = dst_buffer[offset_y];
kusano 7d535a
	src_row_ptr = src_buffer[offset_y];
kusano 7d535a
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
kusano 7d535a
	  if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	    /* Do the mirrorable blocks */
kusano 7d535a
	    dst_ptr = dst_row_ptr[dst_blk_x];
kusano 7d535a
	    src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
kusano 7d535a
	    /* this unrolled loop doesn't need to know which row it's on... */
kusano 7d535a
	    for (k = 0; k < DCTSIZE2; k += 2) {
kusano 7d535a
	      *dst_ptr++ = *src_ptr++;	 /* copy even column */
kusano 7d535a
	      *dst_ptr++ = - *src_ptr++; /* copy odd column with sign change */
kusano 7d535a
	    }
kusano 7d535a
	  } else {
kusano 7d535a
	    /* Copy last partial block(s) verbatim */
kusano 7d535a
	    jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
kusano 7d535a
			    dst_row_ptr + dst_blk_x,
kusano 7d535a
			    (JDIMENSION) 1);
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	   JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	   jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	   jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* Vertical flip */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JBLOCKROW src_row_ptr, dst_row_ptr;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* We output into a separate array because we can't touch different
kusano 7d535a
   * rows of the source virtual array simultaneously.  Otherwise, this
kusano 7d535a
   * is a pretty straightforward analog of horizontal flip.
kusano 7d535a
   * Within a DCT block, vertical mirroring is done by changing the signs
kusano 7d535a
   * of odd-numbered rows.
kusano 7d535a
   * Partial iMCUs at the bottom edge are copied verbatim.
kusano 7d535a
   */
kusano 7d535a
  MCU_rows = srcinfo->output_height /
kusano 7d535a
    (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_height = MCU_rows * compptr->v_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	/* Row is within the mirrorable area. */
kusano 7d535a
	src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	   comp_height - y_crop_blocks - dst_blk_y -
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor,
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      } else {
kusano 7d535a
	/* Bottom-edge blocks will be copied verbatim. */
kusano 7d535a
	src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	   dst_blk_y + y_crop_blocks,
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      }
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	  /* Row is within the mirrorable area. */
kusano 7d535a
	  dst_row_ptr = dst_buffer[offset_y];
kusano 7d535a
	  src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
kusano 7d535a
	  src_row_ptr += x_crop_blocks;
kusano 7d535a
	  for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
kusano 7d535a
	       dst_blk_x++) {
kusano 7d535a
	    dst_ptr = dst_row_ptr[dst_blk_x];
kusano 7d535a
	    src_ptr = src_row_ptr[dst_blk_x];
kusano 7d535a
	    for (i = 0; i < DCTSIZE; i += 2) {
kusano 7d535a
	      /* copy even row */
kusano 7d535a
	      for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		*dst_ptr++ = *src_ptr++;
kusano 7d535a
	      /* copy odd row with sign change */
kusano 7d535a
	      for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		*dst_ptr++ = - *src_ptr++;
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	} else {
kusano 7d535a
	  /* Just copy row verbatim. */
kusano 7d535a
	  jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
kusano 7d535a
			  dst_buffer[offset_y],
kusano 7d535a
			  compptr->width_in_blocks);
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	      JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	      jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	      jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* Transpose source into destination */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_x, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Transposing pixels within a block just requires transposing the
kusano 7d535a
   * DCT coefficients.
kusano 7d535a
   * Partial iMCUs at the edges require no special treatment; we simply
kusano 7d535a
   * process all the available DCT blocks for every component.
kusano 7d535a
   */
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
kusano 7d535a
	     dst_blk_x += compptr->h_samp_factor) {
kusano 7d535a
	  src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	    ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	     dst_blk_x + x_crop_blocks,
kusano 7d535a
	     (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
kusano 7d535a
	    dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
kusano 7d535a
	    src_ptr = src_buffer[offset_x][dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
	    for (i = 0; i < DCTSIZE; i++)
kusano 7d535a
	      for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	   JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	   jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	   jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* 90 degree rotation is equivalent to
kusano 7d535a
 *   1. Transposing the image;
kusano 7d535a
 *   2. Horizontal mirroring.
kusano 7d535a
 * These two steps are merged into a single processing routine.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_x, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Because of the horizontal mirror step, we can't process partial iMCUs
kusano 7d535a
   * at the (output) right edge properly.  They just get transposed and
kusano 7d535a
   * not mirrored.
kusano 7d535a
   */
kusano 7d535a
  MCU_cols = srcinfo->output_height /
kusano 7d535a
    (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_width = MCU_cols * compptr->h_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
kusano 7d535a
	     dst_blk_x += compptr->h_samp_factor) {
kusano 7d535a
	  if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	    /* Block is within the mirrorable area. */
kusano 7d535a
	    src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	      ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	       comp_width - x_crop_blocks - dst_blk_x -
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor,
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  } else {
kusano 7d535a
	    /* Edge blocks are transposed but not mirrored. */
kusano 7d535a
	    src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	      ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	       dst_blk_x + x_crop_blocks,
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  }
kusano 7d535a
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
kusano 7d535a
	    dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
kusano 7d535a
	    if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	      /* Block is within the mirrorable area. */
kusano 7d535a
	      src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
kusano 7d535a
		[dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		i++;
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
	      }
kusano 7d535a
	    } else {
kusano 7d535a
	      /* Edge blocks are transposed but not mirrored. */
kusano 7d535a
	      src_ptr = src_buffer[offset_x]
kusano 7d535a
		[dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i++)
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	    JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	    jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	    jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* 270 degree rotation is equivalent to
kusano 7d535a
 *   1. Horizontal mirroring;
kusano 7d535a
 *   2. Transposing the image.
kusano 7d535a
 * These two steps are merged into a single processing routine.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_x, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Because of the horizontal mirror step, we can't process partial iMCUs
kusano 7d535a
   * at the (output) bottom edge properly.  They just get transposed and
kusano 7d535a
   * not mirrored.
kusano 7d535a
   */
kusano 7d535a
  MCU_rows = srcinfo->output_width /
kusano 7d535a
    (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_height = MCU_rows * compptr->v_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
kusano 7d535a
	     dst_blk_x += compptr->h_samp_factor) {
kusano 7d535a
	  src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	    ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	     dst_blk_x + x_crop_blocks,
kusano 7d535a
	     (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
kusano 7d535a
	    dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
kusano 7d535a
	    if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	      /* Block is within the mirrorable area. */
kusano 7d535a
	      src_ptr = src_buffer[offset_x]
kusano 7d535a
		[comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++) {
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		  j++;
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
		}
kusano 7d535a
	      }
kusano 7d535a
	    } else {
kusano 7d535a
	      /* Edge blocks are transposed but not mirrored. */
kusano 7d535a
	      src_ptr = src_buffer[offset_x]
kusano 7d535a
		[dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i++)
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	    JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	    jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	    jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* 180 degree rotation is equivalent to
kusano 7d535a
 *   1. Vertical mirroring;
kusano 7d535a
 *   2. Horizontal mirroring.
kusano 7d535a
 * These two steps are merged into a single processing routine.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JBLOCKROW src_row_ptr, dst_row_ptr;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  MCU_cols = srcinfo->output_width /
kusano 7d535a
    (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
kusano 7d535a
  MCU_rows = srcinfo->output_height /
kusano 7d535a
    (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_width = MCU_cols * compptr->h_samp_factor;
kusano 7d535a
    comp_height = MCU_rows * compptr->v_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	/* Row is within the vertically mirrorable area. */
kusano 7d535a
	src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	   comp_height - y_crop_blocks - dst_blk_y -
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor,
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      } else {
kusano 7d535a
	/* Bottom-edge rows are only mirrored horizontally. */
kusano 7d535a
	src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	   dst_blk_y + y_crop_blocks,
kusano 7d535a
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
      }
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	dst_row_ptr = dst_buffer[offset_y];
kusano 7d535a
	if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	  /* Row is within the mirrorable area. */
kusano 7d535a
	  src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
kusano 7d535a
	  for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
kusano 7d535a
	    dst_ptr = dst_row_ptr[dst_blk_x];
kusano 7d535a
	    if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	      /* Process the blocks that can be mirrored both ways. */
kusano 7d535a
	      src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i += 2) {
kusano 7d535a
		/* For even row, negate every odd column. */
kusano 7d535a
		for (j = 0; j < DCTSIZE; j += 2) {
kusano 7d535a
		  *dst_ptr++ = *src_ptr++;
kusano 7d535a
		  *dst_ptr++ = - *src_ptr++;
kusano 7d535a
		}
kusano 7d535a
		/* For odd row, negate every even column. */
kusano 7d535a
		for (j = 0; j < DCTSIZE; j += 2) {
kusano 7d535a
		  *dst_ptr++ = - *src_ptr++;
kusano 7d535a
		  *dst_ptr++ = *src_ptr++;
kusano 7d535a
		}
kusano 7d535a
	      }
kusano 7d535a
	    } else {
kusano 7d535a
	      /* Any remaining right-edge blocks are only mirrored vertically. */
kusano 7d535a
	      src_ptr = src_row_ptr[x_crop_blocks + dst_blk_x];
kusano 7d535a
	      for (i = 0; i < DCTSIZE; i += 2) {
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  *dst_ptr++ = *src_ptr++;
kusano 7d535a
		for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		  *dst_ptr++ = - *src_ptr++;
kusano 7d535a
	      }
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	} else {
kusano 7d535a
	  /* Remaining rows are just mirrored horizontally. */
kusano 7d535a
	  src_row_ptr = src_buffer[offset_y];
kusano 7d535a
	  for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
kusano 7d535a
	    if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	      /* Process the blocks that can be mirrored. */
kusano 7d535a
	      dst_ptr = dst_row_ptr[dst_blk_x];
kusano 7d535a
	      src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
kusano 7d535a
	      for (i = 0; i < DCTSIZE2; i += 2) {
kusano 7d535a
		*dst_ptr++ = *src_ptr++;
kusano 7d535a
		*dst_ptr++ = - *src_ptr++;
kusano 7d535a
	      }
kusano 7d535a
	    } else {
kusano 7d535a
	      /* Any remaining right-edge blocks are only copied. */
kusano 7d535a
	      jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
kusano 7d535a
			      dst_row_ptr + dst_blk_x,
kusano 7d535a
			      (JDIMENSION) 1);
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
	       JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
kusano 7d535a
	       jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
	       jvirt_barray_ptr *dst_coef_arrays)
kusano 7d535a
/* Transverse transpose is equivalent to
kusano 7d535a
 *   1. 180 degree rotation;
kusano 7d535a
 *   2. Transposition;
kusano 7d535a
 * or
kusano 7d535a
 *   1. Horizontal mirroring;
kusano 7d535a
 *   2. Transposition;
kusano 7d535a
 *   3. Horizontal mirroring.
kusano 7d535a
 * These steps are merged into a single processing routine.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
kusano 7d535a
  JDIMENSION x_crop_blocks, y_crop_blocks;
kusano 7d535a
  int ci, i, j, offset_x, offset_y;
kusano 7d535a
  JBLOCKARRAY src_buffer, dst_buffer;
kusano 7d535a
  JCOEFPTR src_ptr, dst_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  MCU_cols = srcinfo->output_height /
kusano 7d535a
    (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
kusano 7d535a
  MCU_rows = srcinfo->output_width /
kusano 7d535a
    (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    comp_width = MCU_cols * compptr->h_samp_factor;
kusano 7d535a
    comp_height = MCU_rows * compptr->v_samp_factor;
kusano 7d535a
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
kusano 7d535a
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
kusano 7d535a
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
kusano 7d535a
	 dst_blk_y += compptr->v_samp_factor) {
kusano 7d535a
      dst_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
kusano 7d535a
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
kusano 7d535a
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
kusano 7d535a
	     dst_blk_x += compptr->h_samp_factor) {
kusano 7d535a
	  if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
	    /* Block is within the mirrorable area. */
kusano 7d535a
	    src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	      ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	       comp_width - x_crop_blocks - dst_blk_x -
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor,
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  } else {
kusano 7d535a
	    src_buffer = (*srcinfo->mem->access_virt_barray)
kusano 7d535a
	      ((j_common_ptr) srcinfo, src_coef_arrays[ci],
kusano 7d535a
	       dst_blk_x + x_crop_blocks,
kusano 7d535a
	       (JDIMENSION) compptr->h_samp_factor, FALSE);
kusano 7d535a
	  }
kusano 7d535a
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
kusano 7d535a
	    dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
kusano 7d535a
	    if (y_crop_blocks + dst_blk_y < comp_height) {
kusano 7d535a
	      if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
		/* Block is within the mirrorable area. */
kusano 7d535a
		src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
kusano 7d535a
		  [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
kusano 7d535a
		for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++) {
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		    j++;
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
		  }
kusano 7d535a
		  i++;
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++) {
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
		    j++;
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		  }
kusano 7d535a
		}
kusano 7d535a
	      } else {
kusano 7d535a
		/* Right-edge blocks are mirrored in y only */
kusano 7d535a
		src_ptr = src_buffer[offset_x]
kusano 7d535a
		  [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
kusano 7d535a
		for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++) {
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		    j++;
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
		  }
kusano 7d535a
		}
kusano 7d535a
	      }
kusano 7d535a
	    } else {
kusano 7d535a
	      if (x_crop_blocks + dst_blk_x < comp_width) {
kusano 7d535a
		/* Bottom-edge blocks are mirrored in x only */
kusano 7d535a
		src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
kusano 7d535a
		  [dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
		for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
		  i++;
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
kusano 7d535a
		}
kusano 7d535a
	      } else {
kusano 7d535a
		/* At lower right corner, just transpose, no mirroring */
kusano 7d535a
		src_ptr = src_buffer[offset_x]
kusano 7d535a
		  [dst_blk_y + offset_y + y_crop_blocks];
kusano 7d535a
		for (i = 0; i < DCTSIZE; i++)
kusano 7d535a
		  for (j = 0; j < DCTSIZE; j++)
kusano 7d535a
		    dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
kusano 7d535a
	      }
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Parse an unsigned integer: subroutine for jtransform_parse_crop_spec.
kusano 7d535a
 * Returns TRUE if valid integer found, FALSE if not.
kusano 7d535a
 * *strptr is advanced over the digit string, and *result is set to its value.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(boolean)
kusano 7d535a
jt_read_integer (const char ** strptr, JDIMENSION * result)
kusano 7d535a
{
kusano 7d535a
  const char * ptr = *strptr;
kusano 7d535a
  JDIMENSION val = 0;
kusano 7d535a
kusano 7d535a
  for (; isdigit(*ptr); ptr++) {
kusano 7d535a
    val = val * 10 + (JDIMENSION) (*ptr - '0');
kusano 7d535a
  }
kusano 7d535a
  *result = val;
kusano 7d535a
  if (ptr == *strptr)
kusano 7d535a
    return FALSE;		/* oops, no digits */
kusano 7d535a
  *strptr = ptr;
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Parse a crop specification (written in X11 geometry style).
kusano 7d535a
 * The routine returns TRUE if the spec string is valid, FALSE if not.
kusano 7d535a
 *
kusano 7d535a
 * The crop spec string should have the format
kusano 7d535a
 *	<width>[f]x<height>[f]{+-}<xoffset>{+-}<yoffset></yoffset></xoffset></height></width>
kusano 7d535a
 * where width, height, xoffset, and yoffset are unsigned integers.
kusano 7d535a
 * Each of the elements can be omitted to indicate a default value.
kusano 7d535a
 * (A weakness of this style is that it is not possible to omit xoffset
kusano 7d535a
 * while specifying yoffset, since they look alike.)
kusano 7d535a
 *
kusano 7d535a
 * This code is loosely based on XParseGeometry from the X11 distribution.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jtransform_parse_crop_spec (jpeg_transform_info *info, const char *spec)
kusano 7d535a
{
kusano 7d535a
  info->crop = FALSE;
kusano 7d535a
  info->crop_width_set = JCROP_UNSET;
kusano 7d535a
  info->crop_height_set = JCROP_UNSET;
kusano 7d535a
  info->crop_xoffset_set = JCROP_UNSET;
kusano 7d535a
  info->crop_yoffset_set = JCROP_UNSET;
kusano 7d535a
kusano 7d535a
  if (isdigit(*spec)) {
kusano 7d535a
    /* fetch width */
kusano 7d535a
    if (! jt_read_integer(&spec, &info->crop_width))
kusano 7d535a
      return FALSE;
kusano 7d535a
    if (*spec == 'f' || *spec == 'F') {
kusano 7d535a
      spec++;
kusano 7d535a
      info->crop_width_set = JCROP_FORCE;
kusano 7d535a
    } else
kusano 7d535a
      info->crop_width_set = JCROP_POS;
kusano 7d535a
  }
kusano 7d535a
  if (*spec == 'x' || *spec == 'X') {
kusano 7d535a
    /* fetch height */
kusano 7d535a
    spec++;
kusano 7d535a
    if (! jt_read_integer(&spec, &info->crop_height))
kusano 7d535a
      return FALSE;
kusano 7d535a
    if (*spec == 'f' || *spec == 'F') {
kusano 7d535a
      spec++;
kusano 7d535a
      info->crop_height_set = JCROP_FORCE;
kusano 7d535a
    } else
kusano 7d535a
      info->crop_height_set = JCROP_POS;
kusano 7d535a
  }
kusano 7d535a
  if (*spec == '+' || *spec == '-') {
kusano 7d535a
    /* fetch xoffset */
kusano 7d535a
    info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
kusano 7d535a
    spec++;
kusano 7d535a
    if (! jt_read_integer(&spec, &info->crop_xoffset))
kusano 7d535a
      return FALSE;
kusano 7d535a
  }
kusano 7d535a
  if (*spec == '+' || *spec == '-') {
kusano 7d535a
    /* fetch yoffset */
kusano 7d535a
    info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
kusano 7d535a
    spec++;
kusano 7d535a
    if (! jt_read_integer(&spec, &info->crop_yoffset))
kusano 7d535a
      return FALSE;
kusano 7d535a
  }
kusano 7d535a
  /* We had better have gotten to the end of the string. */
kusano 7d535a
  if (*spec != '\0')
kusano 7d535a
    return FALSE;
kusano 7d535a
  info->crop = TRUE;
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Trim off any partial iMCUs on the indicated destination edge */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
trim_right_edge (jpeg_transform_info *info, JDIMENSION full_width)
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_cols;
kusano 7d535a
kusano 7d535a
  MCU_cols = info->output_width / info->iMCU_sample_width;
kusano 7d535a
  if (MCU_cols > 0 && info->x_crop_offset + MCU_cols ==
kusano 7d535a
      full_width / info->iMCU_sample_width)
kusano 7d535a
    info->output_width = MCU_cols * info->iMCU_sample_width;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
trim_bottom_edge (jpeg_transform_info *info, JDIMENSION full_height)
kusano 7d535a
{
kusano 7d535a
  JDIMENSION MCU_rows;
kusano 7d535a
kusano 7d535a
  MCU_rows = info->output_height / info->iMCU_sample_height;
kusano 7d535a
  if (MCU_rows > 0 && info->y_crop_offset + MCU_rows ==
kusano 7d535a
      full_height / info->iMCU_sample_height)
kusano 7d535a
    info->output_height = MCU_rows * info->iMCU_sample_height;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Request any required workspace.
kusano 7d535a
 *
kusano 7d535a
 * This routine figures out the size that the output image will be
kusano 7d535a
 * (which implies that all the transform parameters must be set before
kusano 7d535a
 * it is called).
kusano 7d535a
 *
kusano 7d535a
 * We allocate the workspace virtual arrays from the source decompression
kusano 7d535a
 * object, so that all the arrays (both the original data and the workspace)
kusano 7d535a
 * will be taken into account while making memory management decisions.
kusano 7d535a
 * Hence, this routine must be called after jpeg_read_header (which reads
kusano 7d535a
 * the image dimensions) and before jpeg_read_coefficients (which realizes
kusano 7d535a
 * the source's virtual arrays).
kusano 7d535a
 *
kusano 7d535a
 * This function returns FALSE right away if -perfect is given
kusano 7d535a
 * and transformation is not perfect.  Otherwise returns TRUE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jtransform_request_workspace (j_decompress_ptr srcinfo,
kusano 7d535a
			      jpeg_transform_info *info)
kusano 7d535a
{
kusano 7d535a
  jvirt_barray_ptr *coef_arrays;
kusano 7d535a
  boolean need_workspace, transpose_it;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JDIMENSION xoffset, yoffset;
kusano 7d535a
  JDIMENSION width_in_iMCUs, height_in_iMCUs;
kusano 7d535a
  JDIMENSION width_in_blocks, height_in_blocks;
kusano 7d535a
  int ci, h_samp_factor, v_samp_factor;
kusano 7d535a
kusano 7d535a
  /* Determine number of components in output image */
kusano 7d535a
  if (info->force_grayscale &&
kusano 7d535a
      srcinfo->jpeg_color_space == JCS_YCbCr &&
kusano 7d535a
      srcinfo->num_components == 3)
kusano 7d535a
    /* We'll only process the first component */
kusano 7d535a
    info->num_components = 1;
kusano 7d535a
  else
kusano 7d535a
    /* Process all the components */
kusano 7d535a
    info->num_components = srcinfo->num_components;
kusano 7d535a
kusano 7d535a
  /* Compute output image dimensions and related values. */
kusano 7d535a
  jpeg_core_output_dimensions(srcinfo);
kusano 7d535a
kusano 7d535a
  /* Return right away if -perfect is given and transformation is not perfect.
kusano 7d535a
   */
kusano 7d535a
  if (info->perfect) {
kusano 7d535a
    if (info->num_components == 1) {
kusano 7d535a
      if (!jtransform_perfect_transform(srcinfo->output_width,
kusano 7d535a
	  srcinfo->output_height,
kusano 7d535a
	  srcinfo->min_DCT_h_scaled_size,
kusano 7d535a
	  srcinfo->min_DCT_v_scaled_size,
kusano 7d535a
	  info->transform))
kusano 7d535a
	return FALSE;
kusano 7d535a
    } else {
kusano 7d535a
      if (!jtransform_perfect_transform(srcinfo->output_width,
kusano 7d535a
	  srcinfo->output_height,
kusano 7d535a
	  srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size,
kusano 7d535a
	  srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size,
kusano 7d535a
	  info->transform))
kusano 7d535a
	return FALSE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* If there is only one output component, force the iMCU size to be 1;
kusano 7d535a
   * else use the source iMCU size.  (This allows us to do the right thing
kusano 7d535a
   * when reducing color to grayscale, and also provides a handy way of
kusano 7d535a
   * cleaning up "funny" grayscale images whose sampling factors are not 1x1.)
kusano 7d535a
   */
kusano 7d535a
  switch (info->transform) {
kusano 7d535a
  case JXFORM_TRANSPOSE:
kusano 7d535a
  case JXFORM_TRANSVERSE:
kusano 7d535a
  case JXFORM_ROT_90:
kusano 7d535a
  case JXFORM_ROT_270:
kusano 7d535a
    info->output_width = srcinfo->output_height;
kusano 7d535a
    info->output_height = srcinfo->output_width;
kusano 7d535a
    if (info->num_components == 1) {
kusano 7d535a
      info->iMCU_sample_width = srcinfo->min_DCT_v_scaled_size;
kusano 7d535a
      info->iMCU_sample_height = srcinfo->min_DCT_h_scaled_size;
kusano 7d535a
    } else {
kusano 7d535a
      info->iMCU_sample_width =
kusano 7d535a
	srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
kusano 7d535a
      info->iMCU_sample_height =
kusano 7d535a
	srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    info->output_width = srcinfo->output_width;
kusano 7d535a
    info->output_height = srcinfo->output_height;
kusano 7d535a
    if (info->num_components == 1) {
kusano 7d535a
      info->iMCU_sample_width = srcinfo->min_DCT_h_scaled_size;
kusano 7d535a
      info->iMCU_sample_height = srcinfo->min_DCT_v_scaled_size;
kusano 7d535a
    } else {
kusano 7d535a
      info->iMCU_sample_width =
kusano 7d535a
	srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
kusano 7d535a
      info->iMCU_sample_height =
kusano 7d535a
	srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* If cropping has been requested, compute the crop area's position and
kusano 7d535a
   * dimensions, ensuring that its upper left corner falls at an iMCU boundary.
kusano 7d535a
   */
kusano 7d535a
  if (info->crop) {
kusano 7d535a
    /* Insert default values for unset crop parameters */
kusano 7d535a
    if (info->crop_xoffset_set == JCROP_UNSET)
kusano 7d535a
      info->crop_xoffset = 0;	/* default to +0 */
kusano 7d535a
    if (info->crop_yoffset_set == JCROP_UNSET)
kusano 7d535a
      info->crop_yoffset = 0;	/* default to +0 */
kusano 7d535a
    if (info->crop_xoffset >= info->output_width ||
kusano 7d535a
	info->crop_yoffset >= info->output_height)
kusano 7d535a
      ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
kusano 7d535a
    if (info->crop_width_set == JCROP_UNSET)
kusano 7d535a
      info->crop_width = info->output_width - info->crop_xoffset;
kusano 7d535a
    if (info->crop_height_set == JCROP_UNSET)
kusano 7d535a
      info->crop_height = info->output_height - info->crop_yoffset;
kusano 7d535a
    /* Ensure parameters are valid */
kusano 7d535a
    if (info->crop_width <= 0 || info->crop_width > info->output_width ||
kusano 7d535a
	info->crop_height <= 0 || info->crop_height > info->output_height ||
kusano 7d535a
	info->crop_xoffset > info->output_width - info->crop_width ||
kusano 7d535a
	info->crop_yoffset > info->output_height - info->crop_height)
kusano 7d535a
      ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
kusano 7d535a
    /* Convert negative crop offsets into regular offsets */
kusano 7d535a
    if (info->crop_xoffset_set == JCROP_NEG)
kusano 7d535a
      xoffset = info->output_width - info->crop_width - info->crop_xoffset;
kusano 7d535a
    else
kusano 7d535a
      xoffset = info->crop_xoffset;
kusano 7d535a
    if (info->crop_yoffset_set == JCROP_NEG)
kusano 7d535a
      yoffset = info->output_height - info->crop_height - info->crop_yoffset;
kusano 7d535a
    else
kusano 7d535a
      yoffset = info->crop_yoffset;
kusano 7d535a
    /* Now adjust so that upper left corner falls at an iMCU boundary */
kusano 7d535a
    if (info->crop_width_set == JCROP_FORCE)
kusano 7d535a
      info->output_width = info->crop_width;
kusano 7d535a
    else
kusano 7d535a
      info->output_width =
kusano 7d535a
        info->crop_width + (xoffset % info->iMCU_sample_width);
kusano 7d535a
    if (info->crop_height_set == JCROP_FORCE)
kusano 7d535a
      info->output_height = info->crop_height;
kusano 7d535a
    else
kusano 7d535a
      info->output_height =
kusano 7d535a
        info->crop_height + (yoffset % info->iMCU_sample_height);
kusano 7d535a
    /* Save x/y offsets measured in iMCUs */
kusano 7d535a
    info->x_crop_offset = xoffset / info->iMCU_sample_width;
kusano 7d535a
    info->y_crop_offset = yoffset / info->iMCU_sample_height;
kusano 7d535a
  } else {
kusano 7d535a
    info->x_crop_offset = 0;
kusano 7d535a
    info->y_crop_offset = 0;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Figure out whether we need workspace arrays,
kusano 7d535a
   * and if so whether they are transposed relative to the source.
kusano 7d535a
   */
kusano 7d535a
  need_workspace = FALSE;
kusano 7d535a
  transpose_it = FALSE;
kusano 7d535a
  switch (info->transform) {
kusano 7d535a
  case JXFORM_NONE:
kusano 7d535a
    if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
kusano 7d535a
      need_workspace = TRUE;
kusano 7d535a
    /* No workspace needed if neither cropping nor transforming */
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_FLIP_H:
kusano 7d535a
    if (info->trim)
kusano 7d535a
      trim_right_edge(info, srcinfo->output_width);
kusano 7d535a
    if (info->y_crop_offset != 0)
kusano 7d535a
      need_workspace = TRUE;
kusano 7d535a
    /* do_flip_h_no_crop doesn't need a workspace array */
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_FLIP_V:
kusano 7d535a
    if (info->trim)
kusano 7d535a
      trim_bottom_edge(info, srcinfo->output_height);
kusano 7d535a
    /* Need workspace arrays having same dimensions as source image. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_TRANSPOSE:
kusano 7d535a
    /* transpose does NOT have to trim anything */
kusano 7d535a
    /* Need workspace arrays having transposed dimensions. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    transpose_it = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_TRANSVERSE:
kusano 7d535a
    if (info->trim) {
kusano 7d535a
      trim_right_edge(info, srcinfo->output_height);
kusano 7d535a
      trim_bottom_edge(info, srcinfo->output_width);
kusano 7d535a
    }
kusano 7d535a
    /* Need workspace arrays having transposed dimensions. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    transpose_it = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_90:
kusano 7d535a
    if (info->trim)
kusano 7d535a
      trim_right_edge(info, srcinfo->output_height);
kusano 7d535a
    /* Need workspace arrays having transposed dimensions. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    transpose_it = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_180:
kusano 7d535a
    if (info->trim) {
kusano 7d535a
      trim_right_edge(info, srcinfo->output_width);
kusano 7d535a
      trim_bottom_edge(info, srcinfo->output_height);
kusano 7d535a
    }
kusano 7d535a
    /* Need workspace arrays having same dimensions as source image. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_270:
kusano 7d535a
    if (info->trim)
kusano 7d535a
      trim_bottom_edge(info, srcinfo->output_width);
kusano 7d535a
    /* Need workspace arrays having transposed dimensions. */
kusano 7d535a
    need_workspace = TRUE;
kusano 7d535a
    transpose_it = TRUE;
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Allocate workspace if needed.
kusano 7d535a
   * Note that we allocate arrays padded out to the next iMCU boundary,
kusano 7d535a
   * so that transform routines need not worry about missing edge blocks.
kusano 7d535a
   */
kusano 7d535a
  if (need_workspace) {
kusano 7d535a
    coef_arrays = (jvirt_barray_ptr *)
kusano 7d535a
      (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
kusano 7d535a
	SIZEOF(jvirt_barray_ptr) * info->num_components);
kusano 7d535a
    width_in_iMCUs = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) info->output_width,
kusano 7d535a
		    (long) info->iMCU_sample_width);
kusano 7d535a
    height_in_iMCUs = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) info->output_height,
kusano 7d535a
		    (long) info->iMCU_sample_height);
kusano 7d535a
    for (ci = 0; ci < info->num_components; ci++) {
kusano 7d535a
      compptr = srcinfo->comp_info + ci;
kusano 7d535a
      if (info->num_components == 1) {
kusano 7d535a
	/* we're going to force samp factors to 1x1 in this case */
kusano 7d535a
	h_samp_factor = v_samp_factor = 1;
kusano 7d535a
      } else if (transpose_it) {
kusano 7d535a
	h_samp_factor = compptr->v_samp_factor;
kusano 7d535a
	v_samp_factor = compptr->h_samp_factor;
kusano 7d535a
      } else {
kusano 7d535a
	h_samp_factor = compptr->h_samp_factor;
kusano 7d535a
	v_samp_factor = compptr->v_samp_factor;
kusano 7d535a
      }
kusano 7d535a
      width_in_blocks = width_in_iMCUs * h_samp_factor;
kusano 7d535a
      height_in_blocks = height_in_iMCUs * v_samp_factor;
kusano 7d535a
      coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
kusano 7d535a
	((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
kusano 7d535a
	 width_in_blocks, height_in_blocks, (JDIMENSION) v_samp_factor);
kusano 7d535a
    }
kusano 7d535a
    info->workspace_coef_arrays = coef_arrays;
kusano 7d535a
  } else
kusano 7d535a
    info->workspace_coef_arrays = NULL;
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Transpose destination image parameters */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
transpose_critical_parameters (j_compress_ptr dstinfo)
kusano 7d535a
{
kusano 7d535a
  int tblno, i, j, ci, itemp;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JQUANT_TBL *qtblptr;
kusano 7d535a
  JDIMENSION jtemp;
kusano 7d535a
  UINT16 qtemp;
kusano 7d535a
kusano 7d535a
  /* Transpose image dimensions */
kusano 7d535a
  jtemp = dstinfo->image_width;
kusano 7d535a
  dstinfo->image_width = dstinfo->image_height;
kusano 7d535a
  dstinfo->image_height = jtemp;
kusano 7d535a
  itemp = dstinfo->min_DCT_h_scaled_size;
kusano 7d535a
  dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size;
kusano 7d535a
  dstinfo->min_DCT_v_scaled_size = itemp;
kusano 7d535a
kusano 7d535a
  /* Transpose sampling factors */
kusano 7d535a
  for (ci = 0; ci < dstinfo->num_components; ci++) {
kusano 7d535a
    compptr = dstinfo->comp_info + ci;
kusano 7d535a
    itemp = compptr->h_samp_factor;
kusano 7d535a
    compptr->h_samp_factor = compptr->v_samp_factor;
kusano 7d535a
    compptr->v_samp_factor = itemp;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Transpose quantization tables */
kusano 7d535a
  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
kusano 7d535a
    qtblptr = dstinfo->quant_tbl_ptrs[tblno];
kusano 7d535a
    if (qtblptr != NULL) {
kusano 7d535a
      for (i = 0; i < DCTSIZE; i++) {
kusano 7d535a
	for (j = 0; j < i; j++) {
kusano 7d535a
	  qtemp = qtblptr->quantval[i*DCTSIZE+j];
kusano 7d535a
	  qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
kusano 7d535a
	  qtblptr->quantval[j*DCTSIZE+i] = qtemp;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Adjust Exif image parameters.
kusano 7d535a
 *
kusano 7d535a
 * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
adjust_exif_parameters (JOCTET FAR * data, unsigned int length,
kusano 7d535a
			JDIMENSION new_width, JDIMENSION new_height)
kusano 7d535a
{
kusano 7d535a
  boolean is_motorola; /* Flag for byte order */
kusano 7d535a
  unsigned int number_of_tags, tagnum;
kusano 7d535a
  unsigned int firstoffset, offset;
kusano 7d535a
  JDIMENSION new_value;
kusano 7d535a
kusano 7d535a
  if (length < 12) return; /* Length of an IFD entry */
kusano 7d535a
kusano 7d535a
  /* Discover byte order */
kusano 7d535a
  if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49)
kusano 7d535a
    is_motorola = FALSE;
kusano 7d535a
  else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D)
kusano 7d535a
    is_motorola = TRUE;
kusano 7d535a
  else
kusano 7d535a
    return;
kusano 7d535a
kusano 7d535a
  /* Check Tag Mark */
kusano 7d535a
  if (is_motorola) {
kusano 7d535a
    if (GETJOCTET(data[2]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[3]) != 0x2A) return;
kusano 7d535a
  } else {
kusano 7d535a
    if (GETJOCTET(data[3]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[2]) != 0x2A) return;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Get first IFD offset (offset to IFD0) */
kusano 7d535a
  if (is_motorola) {
kusano 7d535a
    if (GETJOCTET(data[4]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[5]) != 0) return;
kusano 7d535a
    firstoffset = GETJOCTET(data[6]);
kusano 7d535a
    firstoffset <<= 8;
kusano 7d535a
    firstoffset += GETJOCTET(data[7]);
kusano 7d535a
  } else {
kusano 7d535a
    if (GETJOCTET(data[7]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[6]) != 0) return;
kusano 7d535a
    firstoffset = GETJOCTET(data[5]);
kusano 7d535a
    firstoffset <<= 8;
kusano 7d535a
    firstoffset += GETJOCTET(data[4]);
kusano 7d535a
  }
kusano 7d535a
  if (firstoffset > length - 2) return; /* check end of data segment */
kusano 7d535a
kusano 7d535a
  /* Get the number of directory entries contained in this IFD */
kusano 7d535a
  if (is_motorola) {
kusano 7d535a
    number_of_tags = GETJOCTET(data[firstoffset]);
kusano 7d535a
    number_of_tags <<= 8;
kusano 7d535a
    number_of_tags += GETJOCTET(data[firstoffset+1]);
kusano 7d535a
  } else {
kusano 7d535a
    number_of_tags = GETJOCTET(data[firstoffset+1]);
kusano 7d535a
    number_of_tags <<= 8;
kusano 7d535a
    number_of_tags += GETJOCTET(data[firstoffset]);
kusano 7d535a
  }
kusano 7d535a
  if (number_of_tags == 0) return;
kusano 7d535a
  firstoffset += 2;
kusano 7d535a
kusano 7d535a
  /* Search for ExifSubIFD offset Tag in IFD0 */
kusano 7d535a
  for (;;) {
kusano 7d535a
    if (firstoffset > length - 12) return; /* check end of data segment */
kusano 7d535a
    /* Get Tag number */
kusano 7d535a
    if (is_motorola) {
kusano 7d535a
      tagnum = GETJOCTET(data[firstoffset]);
kusano 7d535a
      tagnum <<= 8;
kusano 7d535a
      tagnum += GETJOCTET(data[firstoffset+1]);
kusano 7d535a
    } else {
kusano 7d535a
      tagnum = GETJOCTET(data[firstoffset+1]);
kusano 7d535a
      tagnum <<= 8;
kusano 7d535a
      tagnum += GETJOCTET(data[firstoffset]);
kusano 7d535a
    }
kusano 7d535a
    if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */
kusano 7d535a
    if (--number_of_tags == 0) return;
kusano 7d535a
    firstoffset += 12;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Get the ExifSubIFD offset */
kusano 7d535a
  if (is_motorola) {
kusano 7d535a
    if (GETJOCTET(data[firstoffset+8]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[firstoffset+9]) != 0) return;
kusano 7d535a
    offset = GETJOCTET(data[firstoffset+10]);
kusano 7d535a
    offset <<= 8;
kusano 7d535a
    offset += GETJOCTET(data[firstoffset+11]);
kusano 7d535a
  } else {
kusano 7d535a
    if (GETJOCTET(data[firstoffset+11]) != 0) return;
kusano 7d535a
    if (GETJOCTET(data[firstoffset+10]) != 0) return;
kusano 7d535a
    offset = GETJOCTET(data[firstoffset+9]);
kusano 7d535a
    offset <<= 8;
kusano 7d535a
    offset += GETJOCTET(data[firstoffset+8]);
kusano 7d535a
  }
kusano 7d535a
  if (offset > length - 2) return; /* check end of data segment */
kusano 7d535a
kusano 7d535a
  /* Get the number of directory entries contained in this SubIFD */
kusano 7d535a
  if (is_motorola) {
kusano 7d535a
    number_of_tags = GETJOCTET(data[offset]);
kusano 7d535a
    number_of_tags <<= 8;
kusano 7d535a
    number_of_tags += GETJOCTET(data[offset+1]);
kusano 7d535a
  } else {
kusano 7d535a
    number_of_tags = GETJOCTET(data[offset+1]);
kusano 7d535a
    number_of_tags <<= 8;
kusano 7d535a
    number_of_tags += GETJOCTET(data[offset]);
kusano 7d535a
  }
kusano 7d535a
  if (number_of_tags < 2) return;
kusano 7d535a
  offset += 2;
kusano 7d535a
kusano 7d535a
  /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
kusano 7d535a
  do {
kusano 7d535a
    if (offset > length - 12) return; /* check end of data segment */
kusano 7d535a
    /* Get Tag number */
kusano 7d535a
    if (is_motorola) {
kusano 7d535a
      tagnum = GETJOCTET(data[offset]);
kusano 7d535a
      tagnum <<= 8;
kusano 7d535a
      tagnum += GETJOCTET(data[offset+1]);
kusano 7d535a
    } else {
kusano 7d535a
      tagnum = GETJOCTET(data[offset+1]);
kusano 7d535a
      tagnum <<= 8;
kusano 7d535a
      tagnum += GETJOCTET(data[offset]);
kusano 7d535a
    }
kusano 7d535a
    if (tagnum == 0xA002 || tagnum == 0xA003) {
kusano 7d535a
      if (tagnum == 0xA002)
kusano 7d535a
	new_value = new_width; /* ExifImageWidth Tag */
kusano 7d535a
      else
kusano 7d535a
	new_value = new_height; /* ExifImageHeight Tag */
kusano 7d535a
      if (is_motorola) {
kusano 7d535a
	data[offset+2] = 0; /* Format = unsigned long (4 octets) */
kusano 7d535a
	data[offset+3] = 4;
kusano 7d535a
	data[offset+4] = 0; /* Number Of Components = 1 */
kusano 7d535a
	data[offset+5] = 0;
kusano 7d535a
	data[offset+6] = 0;
kusano 7d535a
	data[offset+7] = 1;
kusano 7d535a
	data[offset+8] = 0;
kusano 7d535a
	data[offset+9] = 0;
kusano 7d535a
	data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF);
kusano 7d535a
	data[offset+11] = (JOCTET)(new_value & 0xFF);
kusano 7d535a
      } else {
kusano 7d535a
	data[offset+2] = 4; /* Format = unsigned long (4 octets) */
kusano 7d535a
	data[offset+3] = 0;
kusano 7d535a
	data[offset+4] = 1; /* Number Of Components = 1 */
kusano 7d535a
	data[offset+5] = 0;
kusano 7d535a
	data[offset+6] = 0;
kusano 7d535a
	data[offset+7] = 0;
kusano 7d535a
	data[offset+8] = (JOCTET)(new_value & 0xFF);
kusano 7d535a
	data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF);
kusano 7d535a
	data[offset+10] = 0;
kusano 7d535a
	data[offset+11] = 0;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    offset += 12;
kusano 7d535a
  } while (--number_of_tags);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Adjust output image parameters as needed.
kusano 7d535a
 *
kusano 7d535a
 * This must be called after jpeg_copy_critical_parameters()
kusano 7d535a
 * and before jpeg_write_coefficients().
kusano 7d535a
 *
kusano 7d535a
 * The return value is the set of virtual coefficient arrays to be written
kusano 7d535a
 * (either the ones allocated by jtransform_request_workspace, or the
kusano 7d535a
 * original source data arrays).  The caller will need to pass this value
kusano 7d535a
 * to jpeg_write_coefficients().
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(jvirt_barray_ptr *)
kusano 7d535a
jtransform_adjust_parameters (j_decompress_ptr srcinfo,
kusano 7d535a
			      j_compress_ptr dstinfo,
kusano 7d535a
			      jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
			      jpeg_transform_info *info)
kusano 7d535a
{
kusano 7d535a
  /* If force-to-grayscale is requested, adjust destination parameters */
kusano 7d535a
  if (info->force_grayscale) {
kusano 7d535a
    /* First, ensure we have YCbCr or grayscale data, and that the source's
kusano 7d535a
     * Y channel is full resolution.  (No reasonable person would make Y
kusano 7d535a
     * be less than full resolution, so actually coping with that case
kusano 7d535a
     * isn't worth extra code space.  But we check it to avoid crashing.)
kusano 7d535a
     */
kusano 7d535a
    if (((dstinfo->jpeg_color_space == JCS_YCbCr &&
kusano 7d535a
	  dstinfo->num_components == 3) ||
kusano 7d535a
	 (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
kusano 7d535a
	  dstinfo->num_components == 1)) &&
kusano 7d535a
	srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor &&
kusano 7d535a
	srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) {
kusano 7d535a
      /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
kusano 7d535a
       * properly.  Among other things, it sets the target h_samp_factor &
kusano 7d535a
       * v_samp_factor to 1, which typically won't match the source.
kusano 7d535a
       * We have to preserve the source's quantization table number, however.
kusano 7d535a
       */
kusano 7d535a
      int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
kusano 7d535a
      jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
kusano 7d535a
      dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
kusano 7d535a
    } else {
kusano 7d535a
      /* Sorry, can't do it */
kusano 7d535a
      ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    }
kusano 7d535a
  } else if (info->num_components == 1) {
kusano 7d535a
    /* For a single-component source, we force the destination sampling factors
kusano 7d535a
     * to 1x1, with or without force_grayscale.  This is useful because some
kusano 7d535a
     * decoders choke on grayscale images with other sampling factors.
kusano 7d535a
     */
kusano 7d535a
    dstinfo->comp_info[0].h_samp_factor = 1;
kusano 7d535a
    dstinfo->comp_info[0].v_samp_factor = 1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Correct the destination's image dimensions as necessary
kusano 7d535a
   * for rotate/flip, resize, and crop operations.
kusano 7d535a
   */
kusano 7d535a
  dstinfo->jpeg_width = info->output_width;
kusano 7d535a
  dstinfo->jpeg_height = info->output_height;
kusano 7d535a
kusano 7d535a
  /* Transpose destination image parameters */
kusano 7d535a
  switch (info->transform) {
kusano 7d535a
  case JXFORM_TRANSPOSE:
kusano 7d535a
  case JXFORM_TRANSVERSE:
kusano 7d535a
  case JXFORM_ROT_90:
kusano 7d535a
  case JXFORM_ROT_270:
kusano 7d535a
    transpose_critical_parameters(dstinfo);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Adjust Exif properties */
kusano 7d535a
  if (srcinfo->marker_list != NULL &&
kusano 7d535a
      srcinfo->marker_list->marker == JPEG_APP0+1 &&
kusano 7d535a
      srcinfo->marker_list->data_length >= 6 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[0]) == 0x45 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[1]) == 0x78 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[2]) == 0x69 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[3]) == 0x66 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[4]) == 0 &&
kusano 7d535a
      GETJOCTET(srcinfo->marker_list->data[5]) == 0) {
kusano 7d535a
    /* Suppress output of JFIF marker */
kusano 7d535a
    dstinfo->write_JFIF_header = FALSE;
kusano 7d535a
    /* Adjust Exif image parameters */
kusano 7d535a
    if (dstinfo->jpeg_width != srcinfo->image_width ||
kusano 7d535a
	dstinfo->jpeg_height != srcinfo->image_height)
kusano 7d535a
      /* Align data segment to start of TIFF structure for parsing */
kusano 7d535a
      adjust_exif_parameters(srcinfo->marker_list->data + 6,
kusano 7d535a
	srcinfo->marker_list->data_length - 6,
kusano 7d535a
	dstinfo->jpeg_width, dstinfo->jpeg_height);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Return the appropriate output data set */
kusano 7d535a
  if (info->workspace_coef_arrays != NULL)
kusano 7d535a
    return info->workspace_coef_arrays;
kusano 7d535a
  return src_coef_arrays;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Execute the actual transformation, if any.
kusano 7d535a
 *
kusano 7d535a
 * This must be called *after* jpeg_write_coefficients, because it depends
kusano 7d535a
 * on jpeg_write_coefficients to have computed subsidiary values such as
kusano 7d535a
 * the per-component width and height fields in the destination object.
kusano 7d535a
 *
kusano 7d535a
 * Note that some transformations will modify the source data arrays!
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jtransform_execute_transform (j_decompress_ptr srcinfo,
kusano 7d535a
			      j_compress_ptr dstinfo,
kusano 7d535a
			      jvirt_barray_ptr *src_coef_arrays,
kusano 7d535a
			      jpeg_transform_info *info)
kusano 7d535a
{
kusano 7d535a
  jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
kusano 7d535a
kusano 7d535a
  /* Note: conditions tested here should match those in switch statement
kusano 7d535a
   * in jtransform_request_workspace()
kusano 7d535a
   */
kusano 7d535a
  switch (info->transform) {
kusano 7d535a
  case JXFORM_NONE:
kusano 7d535a
    if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
kusano 7d535a
      do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
	      src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_FLIP_H:
kusano 7d535a
    if (info->y_crop_offset != 0)
kusano 7d535a
      do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
		src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    else
kusano 7d535a
      do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset,
kusano 7d535a
			src_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_FLIP_V:
kusano 7d535a
    do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
	      src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_TRANSPOSE:
kusano 7d535a
    do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
		 src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_TRANSVERSE:
kusano 7d535a
    do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
		  src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_90:
kusano 7d535a
    do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
	      src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_180:
kusano 7d535a
    do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
	       src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_ROT_270:
kusano 7d535a
    do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
kusano 7d535a
	       src_coef_arrays, dst_coef_arrays);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* jtransform_perfect_transform
kusano 7d535a
 *
kusano 7d535a
 * Determine whether lossless transformation is perfectly
kusano 7d535a
 * possible for a specified image and transformation.
kusano 7d535a
 *
kusano 7d535a
 * Inputs:
kusano 7d535a
 *   image_width, image_height: source image dimensions.
kusano 7d535a
 *   MCU_width, MCU_height: pixel dimensions of MCU.
kusano 7d535a
 *   transform: transformation identifier.
kusano 7d535a
 * Parameter sources from initialized jpeg_struct
kusano 7d535a
 * (after reading source header):
kusano 7d535a
 *   image_width = cinfo.image_width
kusano 7d535a
 *   image_height = cinfo.image_height
kusano 7d535a
 *   MCU_width = cinfo.max_h_samp_factor * cinfo.block_size
kusano 7d535a
 *   MCU_height = cinfo.max_v_samp_factor * cinfo.block_size
kusano 7d535a
 * Result:
kusano 7d535a
 *   TRUE = perfect transformation possible
kusano 7d535a
 *   FALSE = perfect transformation not possible
kusano 7d535a
 *           (may use custom action then)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height,
kusano 7d535a
			     int MCU_width, int MCU_height,
kusano 7d535a
			     JXFORM_CODE transform)
kusano 7d535a
{
kusano 7d535a
  boolean result = TRUE; /* initialize TRUE */
kusano 7d535a
kusano 7d535a
  switch (transform) {
kusano 7d535a
  case JXFORM_FLIP_H:
kusano 7d535a
  case JXFORM_ROT_270:
kusano 7d535a
    if (image_width % (JDIMENSION) MCU_width)
kusano 7d535a
      result = FALSE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_FLIP_V:
kusano 7d535a
  case JXFORM_ROT_90:
kusano 7d535a
    if (image_height % (JDIMENSION) MCU_height)
kusano 7d535a
      result = FALSE;
kusano 7d535a
    break;
kusano 7d535a
  case JXFORM_TRANSVERSE:
kusano 7d535a
  case JXFORM_ROT_180:
kusano 7d535a
    if (image_width % (JDIMENSION) MCU_width)
kusano 7d535a
      result = FALSE;
kusano 7d535a
    if (image_height % (JDIMENSION) MCU_height)
kusano 7d535a
      result = FALSE;
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return result;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* TRANSFORMS_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Setup decompression object to save desired markers in memory.
kusano 7d535a
 * This must be called before jpeg_read_header() to have the desired effect.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
kusano 7d535a
{
kusano 7d535a
#ifdef SAVE_MARKERS_SUPPORTED
kusano 7d535a
  int m;
kusano 7d535a
kusano 7d535a
  /* Save comments except under NONE option */
kusano 7d535a
  if (option != JCOPYOPT_NONE) {
kusano 7d535a
    jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
kusano 7d535a
  }
kusano 7d535a
  /* Save all types of APPn markers iff ALL option */
kusano 7d535a
  if (option == JCOPYOPT_ALL) {
kusano 7d535a
    for (m = 0; m < 16; m++)
kusano 7d535a
      jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
kusano 7d535a
  }
kusano 7d535a
#endif /* SAVE_MARKERS_SUPPORTED */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* Copy markers saved in the given source object to the destination object.
kusano 7d535a
 * This should be called just after jpeg_start_compress() or
kusano 7d535a
 * jpeg_write_coefficients().
kusano 7d535a
 * Note that those routines will have written the SOI, and also the
kusano 7d535a
 * JFIF APP0 or Adobe APP14 markers if selected.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
kusano 7d535a
		       JCOPY_OPTION option)
kusano 7d535a
{
kusano 7d535a
  jpeg_saved_marker_ptr marker;
kusano 7d535a
kusano 7d535a
  /* In the current implementation, we don't actually need to examine the
kusano 7d535a
   * option flag here; we just copy everything that got saved.
kusano 7d535a
   * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
kusano 7d535a
   * if the encoder library already wrote one.
kusano 7d535a
   */
kusano 7d535a
  for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
kusano 7d535a
    if (dstinfo->write_JFIF_header &&
kusano 7d535a
	marker->marker == JPEG_APP0 &&
kusano 7d535a
	marker->data_length >= 5 &&
kusano 7d535a
	GETJOCTET(marker->data[0]) == 0x4A &&
kusano 7d535a
	GETJOCTET(marker->data[1]) == 0x46 &&
kusano 7d535a
	GETJOCTET(marker->data[2]) == 0x49 &&
kusano 7d535a
	GETJOCTET(marker->data[3]) == 0x46 &&
kusano 7d535a
	GETJOCTET(marker->data[4]) == 0)
kusano 7d535a
      continue;			/* reject duplicate JFIF */
kusano 7d535a
    if (dstinfo->write_Adobe_marker &&
kusano 7d535a
	marker->marker == JPEG_APP0+14 &&
kusano 7d535a
	marker->data_length >= 5 &&
kusano 7d535a
	GETJOCTET(marker->data[0]) == 0x41 &&
kusano 7d535a
	GETJOCTET(marker->data[1]) == 0x64 &&
kusano 7d535a
	GETJOCTET(marker->data[2]) == 0x6F &&
kusano 7d535a
	GETJOCTET(marker->data[3]) == 0x62 &&
kusano 7d535a
	GETJOCTET(marker->data[4]) == 0x65)
kusano 7d535a
      continue;			/* reject duplicate Adobe */
kusano 7d535a
#ifdef NEED_FAR_POINTERS
kusano 7d535a
    /* We could use jpeg_write_marker if the data weren't FAR... */
kusano 7d535a
    {
kusano 7d535a
      unsigned int i;
kusano 7d535a
      jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
kusano 7d535a
      for (i = 0; i < marker->data_length; i++)
kusano 7d535a
	jpeg_write_m_byte(dstinfo, marker->data[i]);
kusano 7d535a
    }
kusano 7d535a
#else
kusano 7d535a
    jpeg_write_marker(dstinfo, marker->marker,
kusano 7d535a
		      marker->data, marker->data_length);
kusano 7d535a
#endif
kusano 7d535a
  }
kusano 7d535a
}