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