kusano 7d535a
/*
kusano 7d535a
 * jutils.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2009-2011 by 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 tables and miscellaneous utility routines needed
kusano 7d535a
 * for both compression and decompression.
kusano 7d535a
 * Note we prefix all global names with "j" to minimize conflicts with
kusano 7d535a
 * a surrounding application.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
kusano 7d535a
 * of a DCT block read in natural order (left to right, top to bottom).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if 0				/* This table is not actually needed in v6a */
kusano 7d535a
kusano 7d535a
const int jpeg_zigzag_order[DCTSIZE2] = {
kusano 7d535a
   0,  1,  5,  6, 14, 15, 27, 28,
kusano 7d535a
   2,  4,  7, 13, 16, 26, 29, 42,
kusano 7d535a
   3,  8, 12, 17, 25, 30, 41, 43,
kusano 7d535a
   9, 11, 18, 24, 31, 40, 44, 53,
kusano 7d535a
  10, 19, 23, 32, 39, 45, 52, 54,
kusano 7d535a
  20, 22, 33, 38, 46, 51, 55, 60,
kusano 7d535a
  21, 34, 37, 47, 50, 56, 59, 61,
kusano 7d535a
  35, 36, 48, 49, 57, 58, 62, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * jpeg_natural_order[i] is the natural-order position of the i'th element
kusano 7d535a
 * of zigzag order.
kusano 7d535a
 *
kusano 7d535a
 * When reading corrupted data, the Huffman decoders could attempt
kusano 7d535a
 * to reference an entry beyond the end of this array (if the decoded
kusano 7d535a
 * zero run length reaches past the end of the block).  To prevent
kusano 7d535a
 * wild stores without adding an inner-loop test, we put some extra
kusano 7d535a
 * "63"s after the real entries.  This will cause the extra coefficient
kusano 7d535a
 * to be stored in location 63 of the block, not somewhere random.
kusano 7d535a
 * The worst case would be a run-length of 15, which means we need 16
kusano 7d535a
 * fake entries.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order[DCTSIZE2+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2,  3, 10,
kusano 7d535a
 17, 24, 32, 25, 18, 11,  4,  5,
kusano 7d535a
 12, 19, 26, 33, 40, 48, 41, 34,
kusano 7d535a
 27, 20, 13,  6,  7, 14, 21, 28,
kusano 7d535a
 35, 42, 49, 56, 57, 50, 43, 36,
kusano 7d535a
 29, 22, 15, 23, 30, 37, 44, 51,
kusano 7d535a
 58, 59, 52, 45, 38, 31, 39, 46,
kusano 7d535a
 53, 60, 61, 54, 47, 55, 62, 63,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order7[7*7+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2,  3, 10,
kusano 7d535a
 17, 24, 32, 25, 18, 11,  4,  5,
kusano 7d535a
 12, 19, 26, 33, 40, 48, 41, 34,
kusano 7d535a
 27, 20, 13,  6, 14, 21, 28, 35,
kusano 7d535a
 42, 49, 50, 43, 36, 29, 22, 30,
kusano 7d535a
 37, 44, 51, 52, 45, 38, 46, 53,
kusano 7d535a
 54,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order6[6*6+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2,  3, 10,
kusano 7d535a
 17, 24, 32, 25, 18, 11,  4,  5,
kusano 7d535a
 12, 19, 26, 33, 40, 41, 34, 27,
kusano 7d535a
 20, 13, 21, 28, 35, 42, 43, 36,
kusano 7d535a
 29, 37, 44, 45,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order5[5*5+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2,  3, 10,
kusano 7d535a
 17, 24, 32, 25, 18, 11,  4, 12,
kusano 7d535a
 19, 26, 33, 34, 27, 20, 28, 35,
kusano 7d535a
 36,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order4[4*4+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2,  3, 10,
kusano 7d535a
 17, 24, 25, 18, 11, 19, 26, 27,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order3[3*3+16] = {
kusano 7d535a
  0,  1,  8, 16,  9,  2, 10, 17,
kusano 7d535a
 18,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
const int jpeg_natural_order2[2*2+16] = {
kusano 7d535a
  0,  1,  8,  9,
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
kusano 7d535a
 63, 63, 63, 63, 63, 63, 63, 63
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Arithmetic utilities
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jdiv_round_up (long a, long b)
kusano 7d535a
/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
kusano 7d535a
/* Assumes a >= 0, b > 0 */
kusano 7d535a
{
kusano 7d535a
  return (a + b - 1L) / b;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jround_up (long a, long b)
kusano 7d535a
/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
kusano 7d535a
/* Assumes a >= 0, b > 0 */
kusano 7d535a
{
kusano 7d535a
  a += b - 1L;
kusano 7d535a
  return a - (a % b);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
kusano 7d535a
 * and coefficient-block arrays.  This won't work on 80x86 because the arrays
kusano 7d535a
 * are FAR and we're assuming a small-pointer memory model.  However, some
kusano 7d535a
 * DOS compilers provide far-pointer versions of memcpy() and memset() even
kusano 7d535a
 * in the small-model libraries.  These will be used if USE_FMEM is defined.
kusano 7d535a
 * Otherwise, the routines below do it the hard way.  (The performance cost
kusano 7d535a
 * is not all that great, because these routines aren't very heavily used.)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef NEED_FAR_POINTERS	/* normal case, same as regular macro */
kusano 7d535a
#define FMEMCOPY(dest,src,size)	MEMCOPY(dest,src,size)
kusano 7d535a
#else				/* 80x86 case, define if we can */
kusano 7d535a
#ifdef USE_FMEM
kusano 7d535a
#define FMEMCOPY(dest,src,size)	_fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
kusano 7d535a
#else
kusano 7d535a
/* This function is for use by the FMEMZERO macro defined in jpegint.h.
kusano 7d535a
 * Do not call this function directly, use the FMEMZERO macro instead.
kusano 7d535a
 */
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jzero_far (void FAR * target, size_t bytestozero)
kusano 7d535a
/* Zero out a chunk of FAR memory. */
kusano 7d535a
/* This might be sample-array data, block-array data, or alloc_large data. */
kusano 7d535a
{
kusano 7d535a
  register char FAR * ptr = (char FAR *) target;
kusano 7d535a
  register size_t count;
kusano 7d535a
kusano 7d535a
  for (count = bytestozero; count > 0; count--) {
kusano 7d535a
    *ptr++ = 0;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
kusano 7d535a
		   JSAMPARRAY output_array, int dest_row,
kusano 7d535a
		   int num_rows, JDIMENSION num_cols)
kusano 7d535a
/* Copy some rows of samples from one place to another.
kusano 7d535a
 * num_rows rows are copied from input_array[source_row++]
kusano 7d535a
 * to output_array[dest_row++]; these areas may overlap for duplication.
kusano 7d535a
 * The source and destination arrays must be at least as wide as num_cols.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
#ifdef FMEMCOPY
kusano 7d535a
  register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
kusano 7d535a
#else
kusano 7d535a
  register JDIMENSION count;
kusano 7d535a
#endif
kusano 7d535a
  register int row;
kusano 7d535a
kusano 7d535a
  input_array += source_row;
kusano 7d535a
  output_array += dest_row;
kusano 7d535a
kusano 7d535a
  for (row = num_rows; row > 0; row--) {
kusano 7d535a
    inptr = *input_array++;
kusano 7d535a
    outptr = *output_array++;
kusano 7d535a
#ifdef FMEMCOPY
kusano 7d535a
    FMEMCOPY(outptr, inptr, count);
kusano 7d535a
#else
kusano 7d535a
    for (count = num_cols; count > 0; count--)
kusano 7d535a
      *outptr++ = *inptr++;	/* needn't bother with GETJSAMPLE() here */
kusano 7d535a
#endif
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
kusano 7d535a
		 JDIMENSION num_blocks)
kusano 7d535a
/* Copy a row of coefficient blocks from one place to another. */
kusano 7d535a
{
kusano 7d535a
#ifdef FMEMCOPY
kusano 7d535a
  FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
kusano 7d535a
#else
kusano 7d535a
  register JCOEFPTR inptr, outptr;
kusano 7d535a
  register long count;
kusano 7d535a
kusano 7d535a
  inptr = (JCOEFPTR) input_row;
kusano 7d535a
  outptr = (JCOEFPTR) output_row;
kusano 7d535a
  for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
kusano 7d535a
    *outptr++ = *inptr++;
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
}