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