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