kusano 7d535a
/*
kusano 7d535a
 * jutils.c
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * It was modified by The libjpeg-turbo Project to include only code
shun-iwasawa 82a8f5
 * relevant to libjpeg-turbo.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains 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
shun-iwasawa 82a8f5
#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
shun-iwasawa 82a8f5
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
kusano 7d535a
/*
kusano 7d535a
 * Arithmetic utilities
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(long)
shun-iwasawa 82a8f5
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)
shun-iwasawa 82a8f5
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
GLOBAL(void)
shun-iwasawa 82a8f5
jcopy_sample_rows(JSAMPARRAY input_array, int source_row,
shun-iwasawa 82a8f5
                  JSAMPARRAY output_array, int dest_row, int num_rows,
shun-iwasawa 82a8f5
                  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;
shun-iwasawa 82a8f5
  register size_t count = (size_t)(num_cols * sizeof(JSAMPLE));
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++;
shun-iwasawa 82a8f5
    MEMCOPY(outptr, inptr, count);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
shun-iwasawa 82a8f5
jcopy_block_row(JBLOCKROW input_row, JBLOCKROW output_row,
shun-iwasawa 82a8f5
                JDIMENSION num_blocks)
kusano 7d535a
/* Copy a row of coefficient blocks from one place to another. */
kusano 7d535a
{
shun-iwasawa 82a8f5
  MEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * sizeof(JCOEF)));
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jzero_far(void *target, size_t bytestozero)
shun-iwasawa 82a8f5
/* Zero out a chunk of memory. */
shun-iwasawa 82a8f5
/* This might be sample-array data, block-array data, or alloc_large data. */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  MEMZERO(target, bytestozero);
kusano 7d535a
}