Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jutils.c |
| 3 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 4 | * Copyright (C) 1991-1994, 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 | * |
| 8 | * This file contains miscellaneous utility routines needed for both |
| 9 | * compression and decompression. |
| 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 | /* |
| 20 | * Arithmetic utilities |
| 21 | */ |
| 22 | |
| 23 | GLOBAL long |
| 24 | jdiv_round_up (long a, long b) |
| 25 | /* Compute a/b rounded up to next integer, ie, ceil(a/b) */ |
| 26 | /* Assumes a >= 0, b > 0 */ |
| 27 | { |
| 28 | return (a + b - 1L) / b; |
| 29 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | GLOBAL long |
| 33 | jround_up (long a, long b) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 34 | /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */ |
| 35 | /* Assumes a >= 0, b > 0 */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 36 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 37 | a += b - 1L; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 38 | return a - (a % b); |
| 39 | } |
| 40 | |
| 41 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 42 | /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays |
| 43 | * and coefficient-block arrays. This won't work on 80x86 because the arrays |
| 44 | * are FAR and we're assuming a small-pointer memory model. However, some |
| 45 | * DOS compilers provide far-pointer versions of memcpy() and memset() even |
| 46 | * in the small-model libraries. These will be used if USE_FMEM is defined. |
| 47 | * Otherwise, the routines below do it the hard way. (The performance cost |
| 48 | * is not all that great, because these routines aren't very heavily used.) |
| 49 | */ |
| 50 | |
| 51 | #ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */ |
| 52 | #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size) |
| 53 | #define FMEMZERO(target,size) MEMZERO(target,size) |
| 54 | #else /* 80x86 case, define if we can */ |
| 55 | #ifdef USE_FMEM |
| 56 | #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size)) |
| 57 | #define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size)) |
| 58 | #endif |
| 59 | #endif |
| 60 | |
| 61 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 62 | GLOBAL void |
| 63 | jcopy_sample_rows (JSAMPARRAY input_array, int source_row, |
| 64 | JSAMPARRAY output_array, int dest_row, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 65 | int num_rows, JDIMENSION num_cols) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 66 | /* Copy some rows of samples from one place to another. |
| 67 | * num_rows rows are copied from input_array[source_row++] |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 68 | * to output_array[dest_row++]; these areas may overlap for duplication. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 69 | * The source and destination arrays must be at least as wide as num_cols. |
| 70 | */ |
| 71 | { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 72 | register JSAMPROW inptr, outptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 73 | #ifdef FMEMCOPY |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 74 | register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE)); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 75 | #else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 76 | register JDIMENSION count; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 77 | #endif |
| 78 | register int row; |
| 79 | |
| 80 | input_array += source_row; |
| 81 | output_array += dest_row; |
| 82 | |
| 83 | for (row = num_rows; row > 0; row--) { |
| 84 | inptr = *input_array++; |
| 85 | outptr = *output_array++; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 86 | #ifdef FMEMCOPY |
| 87 | FMEMCOPY(outptr, inptr, count); |
| 88 | #else |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 89 | for (count = num_cols; count > 0; count--) |
| 90 | *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 91 | #endif |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | GLOBAL void |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 97 | jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, |
| 98 | JDIMENSION num_blocks) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 99 | /* Copy a row of coefficient blocks from one place to another. */ |
| 100 | { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 101 | #ifdef FMEMCOPY |
| 102 | FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF))); |
| 103 | #else |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 104 | register JCOEFPTR inptr, outptr; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 105 | register long count; |
| 106 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 107 | inptr = (JCOEFPTR) input_row; |
| 108 | outptr = (JCOEFPTR) output_row; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame^] | 109 | for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 110 | *outptr++ = *inptr++; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 111 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 112 | #endif |
| 113 | } |
| 114 | |
| 115 | |
| 116 | GLOBAL void |
| 117 | jzero_far (void FAR * target, size_t bytestozero) |
| 118 | /* Zero out a chunk of FAR memory. */ |
| 119 | /* This might be sample-array data, block-array data, or alloc_medium data. */ |
| 120 | { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 121 | #ifdef FMEMZERO |
| 122 | FMEMZERO(target, bytestozero); |
| 123 | #else |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 124 | register char FAR * ptr = (char FAR *) target; |
| 125 | register size_t count; |
| 126 | |
| 127 | for (count = bytestozero; count > 0; count--) { |
| 128 | *ptr++ = 0; |
| 129 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 130 | #endif |
| 131 | } |