]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jutils.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jutils.cpp
1 /*
2
3  * jutils.c
4
5  *
6
7  * Copyright (C) 1991-1995, Thomas G. Lane.
8
9  * This file is part of the Independent JPEG Group's software.
10
11  * For conditions of distribution and use, see the accompanying README file.
12
13  *
14
15  * This file contains tables and miscellaneous utility routines needed
16
17  * for both compression and decompression.
18
19  * Note we prefix all global names with "j" to minimize conflicts with
20
21  * a surrounding application.
22
23  */
24
25
26
27 #define JPEG_INTERNALS
28
29 #include "jinclude.h"
30
31 #include "radiant_jpeglib.h"
32
33
34
35
36
37 /*
38
39  * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
40
41  * of a DCT block read in natural order (left to right, top to bottom).
42
43  */
44
45
46
47 const int jpeg_zigzag_order[DCTSIZE2] = {
48
49    0,  1,  5,  6, 14, 15, 27, 28,
50
51    2,  4,  7, 13, 16, 26, 29, 42,
52
53    3,  8, 12, 17, 25, 30, 41, 43,
54
55    9, 11, 18, 24, 31, 40, 44, 53,
56
57   10, 19, 23, 32, 39, 45, 52, 54,
58
59   20, 22, 33, 38, 46, 51, 55, 60,
60
61   21, 34, 37, 47, 50, 56, 59, 61,
62
63   35, 36, 48, 49, 57, 58, 62, 63
64
65 };
66
67
68
69 /*
70
71  * jpeg_natural_order[i] is the natural-order position of the i'th element
72
73  * of zigzag order.
74
75  *
76
77  * When reading corrupted data, the Huffman decoders could attempt
78
79  * to reference an entry beyond the end of this array (if the decoded
80
81  * zero run length reaches past the end of the block).  To prevent
82
83  * wild stores without adding an inner-loop test, we put some extra
84
85  * "63"s after the real entries.  This will cause the extra coefficient
86
87  * to be stored in location 63 of the block, not somewhere random.
88
89  * The worst case would be a run-length of 15, which means we need 16
90
91  * fake entries.
92
93  */
94
95
96
97 const int jpeg_natural_order[DCTSIZE2+16] = {
98
99   0,  1,  8, 16,  9,  2,  3, 10,
100
101  17, 24, 32, 25, 18, 11,  4,  5,
102
103  12, 19, 26, 33, 40, 48, 41, 34,
104
105  27, 20, 13,  6,  7, 14, 21, 28,
106
107  35, 42, 49, 56, 57, 50, 43, 36,
108
109  29, 22, 15, 23, 30, 37, 44, 51,
110
111  58, 59, 52, 45, 38, 31, 39, 46,
112
113  53, 60, 61, 54, 47, 55, 62, 63,
114
115  63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
116
117  63, 63, 63, 63, 63, 63, 63, 63
118
119 };
120
121
122
123
124
125 /*
126
127  * Arithmetic utilities
128
129  */
130
131
132
133 GLOBAL long
134
135 jdiv_round_up (long a, long b)
136
137 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
138
139 /* Assumes a >= 0, b > 0 */
140
141 {
142
143   return (a + b - 1L) / b;
144
145 }
146
147
148
149
150
151 GLOBAL long
152
153 jround_up (long a, long b)
154
155 /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
156
157 /* Assumes a >= 0, b > 0 */
158
159 {
160
161   a += b - 1L;
162
163   return a - (a % b);
164
165 }
166
167
168
169
170
171 /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
172
173  * and coefficient-block arrays.  This won't work on 80x86 because the arrays
174
175  * are FAR and we're assuming a small-pointer memory model.  However, some
176
177  * DOS compilers provide far-pointer versions of memcpy() and memset() even
178
179  * in the small-model libraries.  These will be used if USE_FMEM is defined.
180
181  * Otherwise, the routines below do it the hard way.  (The performance cost
182
183  * is not all that great, because these routines aren't very heavily used.)
184
185  */
186
187
188
189 #ifndef NEED_FAR_POINTERS       /* normal case, same as regular macros */
190
191 #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
192
193 #define FMEMZERO(target,size)   MEMZERO(target,size)
194
195 #else                           /* 80x86 case, define if we can */
196
197 #ifdef USE_FMEM
198
199 #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
200
201 #define FMEMZERO(target,size)   _fmemset((void FAR *)(target), 0, (size_t)(size))
202
203 #endif
204
205 #endif
206
207
208
209
210
211 GLOBAL void
212
213 jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
214
215                    JSAMPARRAY output_array, int dest_row,
216
217                    int num_rows, JDIMENSION num_cols)
218
219 /* Copy some rows of samples from one place to another.
220
221  * num_rows rows are copied from input_array[source_row++]
222
223  * to output_array[dest_row++]; these areas may overlap for duplication.
224
225  * The source and destination arrays must be at least as wide as num_cols.
226
227  */
228
229 {
230
231   register JSAMPROW inptr, outptr;
232
233 #ifdef FMEMCOPY
234
235   register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
236
237 #else
238
239   register JDIMENSION count;
240
241 #endif
242
243   register int row;
244
245
246
247   input_array += source_row;
248
249   output_array += dest_row;
250
251
252
253   for (row = num_rows; row > 0; row--) {
254
255     inptr = *input_array++;
256
257     outptr = *output_array++;
258
259 #ifdef FMEMCOPY
260
261     FMEMCOPY(outptr, inptr, count);
262
263 #else
264
265     for (count = num_cols; count > 0; count--)
266
267       *outptr++ = *inptr++;     /* needn't bother with GETJSAMPLE() here */
268
269 #endif
270
271   }
272
273 }
274
275
276
277
278
279 GLOBAL void
280
281 jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
282
283                  JDIMENSION num_blocks)
284
285 /* Copy a row of coefficient blocks from one place to another. */
286
287 {
288
289 #ifdef FMEMCOPY
290
291   FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
292
293 #else
294
295   register JCOEFPTR inptr, outptr;
296
297   register long count;
298
299
300
301   inptr = (JCOEFPTR) input_row;
302
303   outptr = (JCOEFPTR) output_row;
304
305   for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
306
307     *outptr++ = *inptr++;
308
309   }
310
311 #endif
312
313 }
314
315
316
317
318
319 GLOBAL void
320
321 jzero_far (void FAR * target, size_t bytestozero)
322
323 /* Zero out a chunk of FAR memory. */
324
325 /* This might be sample-array data, block-array data, or alloc_large data. */
326
327 {
328
329 #ifdef FMEMZERO
330
331   FMEMZERO(target, bytestozero);
332
333 #else
334
335   register char FAR * ptr = (char FAR *) target;
336
337   register size_t count;
338
339
340
341   for (count = bytestozero; count > 0; count--) {
342
343     *ptr++ = 0;
344
345   }
346
347 #endif
348
349 }
350