blob: 4ba2a543c806c04dfbcd2f85750c1167c4f1fda4 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jutils.c
3 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +00004 * Copyright (C) 1991-1995, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * 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. Lanebc79e061995-08-02 00:00:00 +00008 * This file contains tables and miscellaneous utility routines needed
9 * for both compression and decompression.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000010 * Note we prefix all global names with "j" to minimize conflicts with
11 * a surrounding application.
12 */
13
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#include "jpeglib.h"
17
18
19/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000020 * 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
24const 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
49const 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/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000064 * Arithmetic utilities
65 */
66
67GLOBAL long
68jdiv_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{
72 return (a + b - 1L) / b;
73}
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000074
75
76GLOBAL long
77jround_up (long a, long b)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
79/* Assumes a >= 0, b > 0 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000080{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 a += b - 1L;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000082 return a - (a % b);
83}
84
85
Thomas G. Lane88aeed41992-12-10 00:00:00 +000086/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
87 * and coefficient-block arrays. This won't work on 80x86 because the arrays
88 * are FAR and we're assuming a small-pointer memory model. However, some
89 * DOS compilers provide far-pointer versions of memcpy() and memset() even
90 * in the small-model libraries. These will be used if USE_FMEM is defined.
91 * Otherwise, the routines below do it the hard way. (The performance cost
92 * is not all that great, because these routines aren't very heavily used.)
93 */
94
95#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
96#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
97#define FMEMZERO(target,size) MEMZERO(target,size)
98#else /* 80x86 case, define if we can */
99#ifdef USE_FMEM
100#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
101#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
102#endif
103#endif
104
105
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000106GLOBAL void
107jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
108 JSAMPARRAY output_array, int dest_row,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 int num_rows, JDIMENSION num_cols)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000110/* Copy some rows of samples from one place to another.
111 * num_rows rows are copied from input_array[source_row++]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000112 * to output_array[dest_row++]; these areas may overlap for duplication.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000113 * The source and destination arrays must be at least as wide as num_cols.
114 */
115{
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000116 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000117#ifdef FMEMCOPY
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000118 register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000119#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 register JDIMENSION count;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000121#endif
122 register int row;
123
124 input_array += source_row;
125 output_array += dest_row;
126
127 for (row = num_rows; row > 0; row--) {
128 inptr = *input_array++;
129 outptr = *output_array++;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000130#ifdef FMEMCOPY
131 FMEMCOPY(outptr, inptr, count);
132#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000133 for (count = num_cols; count > 0; count--)
134 *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000135#endif
136 }
137}
138
139
140GLOBAL void
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
142 JDIMENSION num_blocks)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000143/* Copy a row of coefficient blocks from one place to another. */
144{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000145#ifdef FMEMCOPY
146 FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
147#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000148 register JCOEFPTR inptr, outptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000149 register long count;
150
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000151 inptr = (JCOEFPTR) input_row;
152 outptr = (JCOEFPTR) output_row;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153 for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000154 *outptr++ = *inptr++;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000155 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000156#endif
157}
158
159
160GLOBAL void
161jzero_far (void FAR * target, size_t bytestozero)
162/* Zero out a chunk of FAR memory. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163/* This might be sample-array data, block-array data, or alloc_large data. */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000164{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000165#ifdef FMEMZERO
166 FMEMZERO(target, bytestozero);
167#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000168 register char FAR * ptr = (char FAR *) target;
169 register size_t count;
170
171 for (count = bytestozero; count > 0; count--) {
172 *ptr++ = 0;
173 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000174#endif
175}