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