jutils.cpp (5443B)
1 /* 2 * jutils.c 3 * 4 * Copyright (C) 1991-1995, Thomas G. Lane. 5 * This file is part of the Independent JPEG Group's software. 6 * For conditions of distribution and use, see the accompanying README file. 7 * 8 * This file contains tables and miscellaneous utility routines needed 9 * for both compression and decompression. 10 * Note we prefix all global names with "j" to minimize conflicts with 11 * a surrounding application. 12 */ 13 14 #define JPEG_INTERNALS 15 #include "jinclude.h" 16 #include "jpeglib.h" 17 18 19 /* 20 * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element 21 * of a DCT block read in natural order (left to right, top to bottom). 22 */ 23 24 const int jpeg_zigzag_order[DCTSIZE2] = { 25 0, 1, 5, 6, 14, 15, 27, 28, 26 2, 4, 7, 13, 16, 26, 29, 42, 27 3, 8, 12, 17, 25, 30, 41, 43, 28 9, 11, 18, 24, 31, 40, 44, 53, 29 10, 19, 23, 32, 39, 45, 52, 54, 30 20, 22, 33, 38, 46, 51, 55, 60, 31 21, 34, 37, 47, 50, 56, 59, 61, 32 35, 36, 48, 49, 57, 58, 62, 63 33 }; 34 35 /* 36 * jpeg_natural_order[i] is the natural-order position of the i'th element 37 * of zigzag order. 38 * 39 * When reading corrupted data, the Huffman decoders could attempt 40 * to reference an entry beyond the end of this array (if the decoded 41 * zero run length reaches past the end of the block). To prevent 42 * wild stores without adding an inner-loop test, we put some extra 43 * "63"s after the real entries. This will cause the extra coefficient 44 * to be stored in location 63 of the block, not somewhere random. 45 * The worst case would be a run-length of 15, which means we need 16 46 * fake entries. 47 */ 48 49 const int jpeg_natural_order[DCTSIZE2 + 16] = { 50 0, 1, 8, 16, 9, 2, 3, 10, 51 17, 24, 32, 25, 18, 11, 4, 5, 52 12, 19, 26, 33, 40, 48, 41, 34, 53 27, 20, 13, 6, 7, 14, 21, 28, 54 35, 42, 49, 56, 57, 50, 43, 36, 55 29, 22, 15, 23, 30, 37, 44, 51, 56 58, 59, 52, 45, 38, 31, 39, 46, 57 53, 60, 61, 54, 47, 55, 62, 63, 58 63, 63, 63, 63, 63, 63, 63, 63,/* extra entries for safety in decoder */ 59 63, 63, 63, 63, 63, 63, 63, 63 60 }; 61 62 63 /* 64 * Arithmetic utilities 65 */ 66 67 GLOBAL long 68 jdiv_round_up( long a, long b ) { 69 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */ 70 /* Assumes a >= 0, b > 0 */ 71 return ( a + b - 1L ) / b; 72 } 73 74 75 GLOBAL long 76 jround_up( long a, long b ) { 77 /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */ 78 /* Assumes a >= 0, b > 0 */ 79 a += b - 1L; 80 return a - ( a % b ); 81 } 82 83 84 /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays 85 * and coefficient-block arrays. This won't work on 80x86 because the arrays 86 * are FAR and we're assuming a small-pointer memory model. However, some 87 * DOS compilers provide far-pointer versions of memcpy() and memset() even 88 * in the small-model libraries. These will be used if USE_FMEM is defined. 89 * Otherwise, the routines below do it the hard way. (The performance cost 90 * is not all that great, because these routines aren't very heavily used.) 91 */ 92 93 #ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */ 94 #define FMEMCOPY( dest, src, size ) MEMCOPY( dest, src, size ) 95 #define FMEMZERO( target, size ) MEMZERO( target, size ) 96 #else /* 80x86 case, define if we can */ 97 #ifdef USE_FMEM 98 #define FMEMCOPY( dest, src, size ) _fmemcpy( (void FAR *)( dest ), (const void FAR *)( src ), (size_t)( size ) ) 99 #define FMEMZERO( target, size ) _fmemset( (void FAR *)( target ), 0, (size_t)( size ) ) 100 #endif 101 #endif 102 103 104 GLOBAL void 105 jcopy_sample_rows( JSAMPARRAY input_array, int source_row, 106 JSAMPARRAY output_array, int dest_row, 107 int num_rows, JDIMENSION num_cols ) { 108 /* Copy some rows of samples from one place to another. 109 * num_rows rows are copied from input_array[source_row++] 110 * to output_array[dest_row++]; these areas may overlap for duplication. 111 * The source and destination arrays must be at least as wide as num_cols. 112 */ 113 register JSAMPROW inptr, outptr; 114 #ifdef FMEMCOPY 115 register size_t count = (size_t) ( num_cols * SIZEOF( JSAMPLE ) ); 116 #else 117 register JDIMENSION count; 118 #endif 119 register int row; 120 121 input_array += source_row; 122 output_array += dest_row; 123 124 for ( row = num_rows; row > 0; row-- ) { 125 inptr = *input_array++; 126 outptr = *output_array++; 127 #ifdef FMEMCOPY 128 FMEMCOPY( outptr, inptr, count ); 129 #else 130 for ( count = num_cols; count > 0; count-- ) { 131 *outptr++ = *inptr++; 132 } /* needn't bother with GETJSAMPLE() here */ 133 #endif 134 } 135 } 136 137 138 GLOBAL void 139 jcopy_block_row( JBLOCKROW input_row, JBLOCKROW output_row, 140 JDIMENSION num_blocks ) { 141 /* Copy a row of coefficient blocks from one place to another. */ 142 #ifdef FMEMCOPY 143 FMEMCOPY( output_row, input_row, num_blocks * ( DCTSIZE2 * SIZEOF( JCOEF ) ) ); 144 #else 145 register JCOEFPTR inptr, outptr; 146 register long count; 147 148 inptr = (JCOEFPTR) input_row; 149 outptr = (JCOEFPTR) output_row; 150 for ( count = (long) num_blocks * DCTSIZE2; count > 0; count-- ) { 151 *outptr++ = *inptr++; 152 } 153 #endif 154 } 155 156 157 GLOBAL void 158 jzero_far( void FAR * target, size_t bytestozero ) { 159 /* Zero out a chunk of FAR memory. */ 160 /* This might be sample-array data, block-array data, or alloc_large data. */ 161 #ifdef FMEMZERO 162 FMEMZERO( target, bytestozero ); 163 #else 164 register char FAR * ptr = (char FAR *) target; 165 register size_t count; 166 167 for ( count = bytestozero; count > 0; count-- ) { 168 *ptr++ = 0; 169 } 170 #endif 171 }