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