1 /* libhpojip -- HP OfficeJet image-processing library. */
3 /* Copyright (C) 1995-2002 Hewlett-Packard Company
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
13 * NON-INFRINGEMENT. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * In addition, as a special exception, Hewlett-Packard Company
21 * gives permission to link the code of this program with any
22 * version of the OpenSSL library which is distributed under a
23 * license identical to that listed in the included LICENSE.OpenSSL
24 * file, and distribute linked combinations including the two.
25 * You must obey the GNU General Public License in all respects
26 * for all of the code used other than OpenSSL. If you modify
27 * this file, you may extend this exception to your version of the
28 * file, but you are not obligated to do so. If you do not wish to
29 * do so, delete this exception statement from your version.
32 /* Original author: Mark Overton and others.
34 * Ported to Linux by David Paschal.
37 /*____________________________________________________________________________
39 | xjpg_dct.c | Computes forward and inverse DCT for JPEG |
40 |____________|_______________________________________________________________|
42 | Mark Overton, May 1997 |
43 |____________________________________________________________________________|
49 /*____________________________________________________________________________
51 | SUB_AND_ADD | replaces a with a-b, and b with a+b using no temp registers |
52 |_____________|______________________________________________________________|
54 #define SUB_AND_ADD(a,b) { \
62 /*____________________________________________________________________________
64 | MUL_ROUND | computes x = round(x*c), using no temp registers |
65 |___________|________________________________________________________________|
67 | c is assumed to have CONST_FRAC_BITS bits of fraction. |
68 |____________________________________________________________________________|
70 #if (defined _WINDOWS) && !(defined _WIN32)
72 /* We are compiling for 16-bit Windows 3.1 */
74 #define MUL_ROUND(c,x) { \
76 product = (long)(x) * ((long)(c) << (16-CONST_FRAC_BITS)); \
77 x = (product+0x8000L) >> 16; \
82 #define MUL_ROUND(c,x) { \
83 x = (short)(x) * (short)(c); \
84 x = ((x)+(1l<<(CONST_FRAC_BITS-1))) >> CONST_FRAC_BITS; \
91 /*____________________________________________________________________________
93 | dct_forward | computes DCT for JPEG |
94 |_____________|______________________________________________________________|
96 | This is the DCT algorithm based on the small FFT Winograd transform |
97 | from Trans. IEICE, vol. E 71(11), 1095-1097, Nov. 1988 |
99 | Input: 'block' is 64 level-shifted pixels (-128..127 each). |
101 | Output: 'block' is the DCT (64 words). |
102 | These values need to be scaled by the forward correction matrix |
103 | for the Winograd DCT. |
104 |____________________________________________________________________________|
106 void dct_forward (register int *block_p)
108 #define CONST_FRAC_BITS 14 /* bits of frac in CONST_1-CONST_5 below */
110 #define CONST_1 (23170/2) /* 15 bits of frac shifted down to 14 */
111 #define CONST_2 (17734/2)
112 #define CONST_3 (23170/2)
113 #define CONST_4 (42813/2) /* this one wouldn't fit with 15 bits of frac */
114 #define CONST_5 (12540/2)
117 int d0, d1, d2, d3, d4, d5, d6, d7;
123 for (data_p=block_p; data_p<block_p+64; data_p+=8)
147 MUL_ROUND (CONST_1, d6)
152 /* At this point, the only live math vars are in: d0, d1, d2, d4 */
155 MUL_ROUND (CONST_3, d7)
160 MUL_ROUND (CONST_5, d6)
161 MUL_ROUND (CONST_4, d1)
168 MUL_ROUND (CONST_2, d4)
175 /*********************/
176 /* Transform Columns */
177 /*********************/
179 for (data_p=block_p; data_p<block_p+8; data_p++)
205 MUL_ROUND (CONST_1, d6)
210 /* At this point, the only live math vars are in: d0, d1, d2, d4 */
213 MUL_ROUND (CONST_3, d7)
218 MUL_ROUND (CONST_5, d6)
219 MUL_ROUND (CONST_4, d1)
226 MUL_ROUND (CONST_2, d4)
233 #undef CONST_FRAC_BITS
239 } /* end of dct_forward */
243 /*____________________________________________________________________________
245 | dct_inverse | computes inverse DCT for JPEG |
246 |_____________|______________________________________________________________|
248 | This is the DCT algorithm based on the small FFT Winograd transform |
249 | from Trans. IEICE, vol. E 71(11), 1095-1097, Nov. 1988 |
251 | Input: 'block' is the DCT (64 words). |
252 | These values are assumed to have been scaled by the inverse |
253 | correction matrix for the Winograd DCT. |
255 | Output: 'block' is 64 level-shifted pixels. These values will have |
256 | as many bits of fraction as the input DCT had. After rounding |
257 | and level-shifting, you must clamp these values to 0..255. |
258 |____________________________________________________________________________|
260 void dct_inverse (register int *block_p)
262 #define CONST_FRAC_BITS 13 /* bits of frac in CONST_1-CONST_5 below */
264 #define CONST_1 ((46341+2)/4) /* 15 bits of frac shifted down to 13 */
265 #define CONST_2 ((85627+2)/4)
266 #define CONST_3 ((46341+2)/4)
267 #define CONST_4 ((35468+2)/4)
268 #define CONST_5 ((25080+2)/4)
271 int d0, d1, d2, d3, d4, d5, d6, d7, tmp;
273 /*********************/
274 /* Transform Columns */
275 /*********************/
277 for (data_p=block_p; data_p<block_p+8; data_p++)
295 MUL_ROUND (CONST_1, d2)
307 MUL_ROUND (CONST_5, d6);
308 MUL_ROUND (CONST_4, d1)
311 MUL_ROUND (CONST_3, d7)
313 MUL_ROUND (CONST_2, d5)
334 for (data_p=block_p; data_p<block_p+64; data_p+=8)
350 MUL_ROUND (CONST_1, d2)
362 MUL_ROUND (CONST_5, d6)
363 MUL_ROUND (CONST_4, d1)
366 MUL_ROUND (CONST_3, d7)
368 MUL_ROUND (CONST_2, d5)
385 #undef CONST_FRAC_BITS
391 } /* end of dct_inverse */