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