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