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