2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: iljpgdedct.c /main/3 1995/10/23 15:55:19 rswiston $ */
24 /**---------------------------------------------------------------------
26 *** (c)Copyright 1992 Hewlett-Packard Co.
28 *** RESTRICTED RIGHTS LEGEND
29 *** Use, duplication, or disclosure by the U.S. Government is subject to
30 *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
31 *** Technical Data and Computer Software clause in DFARS 252.227-7013.
32 *** Hewlett-Packard Company
33 *** 3000 Hanover Street
34 *** Palo Alto, CA 94304 U.S.A.
35 *** Rights for non-DOD U.S. Government Departments and Agencies are as set
36 *** forth in FAR 52.227-19(c)(1,2).
38 ***-------------------------------------------------------------------*/
40 #include "iljpgdecodeint.h"
44 /* Macros to check if "clipValue" (an int) is outside range 0..255, and
45 to branch to point named by second macro if so, which clips and returns.
46 This is done to avoid taking a branch on the common case of value
47 not out of range - significant speedup on RISC machine.
49 #define ILJPG_CLIP_256(_gotoLabel, _returnLabel) \
50 if ((clipValue) >> 8) goto _gotoLabel; \
53 #define ILJPG_CLIP_256_LABEL(_gotoLabel, _returnLabel) \
55 if ((clipValue) > 255) clipValue = 255; else clipValue = 0; \
59 /* compute 2-D DCT descaling matrix */
60 static void _il_fwft_rev_scale (
61 iljpgPtr q, /* pointer to quantization matrix */
62 float *s /* pointer to pointer to descaling matrix */
65 int i, j, prevValue, value, QIndex;
66 double a, c0, b[8], pi;
71 c0 = 1.0 / (2.0 * 0.707106718);
74 for (i = 1; i < 8; i++) {
75 a = cos (i * pi / 16.0) / 2.0;
79 /* descaling matrix including dequantization effects */
80 /* Note: given Q table is zigzag'd, as in JIF stream. De-zigzag *qptr.
81 Also: if an entry in the Q table is zero, then: if it is the first entry,
82 store 16 (?) in the Q table, otherwise store the previous Q table value.
87 prevValue = 16; /* in case 1st Q table value is 0 */
90 for (i = 0; i < 8; i++) {
91 for (j = 0; j < 8; j++) {
92 value = qptr[_iljpgZigzagTable[QIndex++]];
95 *sptr++ = b[i] * b[j] * value;
101 /* -------------------- _iljpgDeDCTInit -------------------------- */
102 /* Called by iljpgDecode() to init for DCT decoding.
105 iljpgError _iljpgDeDCTInit (
106 iljpgDecodePrivPtr pPriv
112 pData = pPriv->pData;
114 /* Build a "rev scale" table for each QTable; store into private */
115 for (i = 0; i < 4; i++)
116 pPriv->DCTRevScaleTables[i] = (float *)NULL;
118 for (i = 0; i < 4; i++) {
119 if (pData->QTables[i]) {
120 if (!(pPriv->DCTRevScaleTables[i] = (float *)ILJPG_MALLOC(sizeof(float) * 64)))
121 return ILJPG_ERROR_DECODE_MALLOC;
122 _il_fwft_rev_scale (pData->QTables[i], pPriv->DCTRevScaleTables[i]);
130 /* -------------------- _iljpgDeDCTCleanup -------------------------- */
131 /* Called by iljpgDecode() to cleanup after DCT decoding.
134 iljpgError _iljpgDeDCTCleanup (
135 iljpgDecodePrivPtr pPriv
140 /* Free any "rev scale" tables that were allocated in iljpgDeDCTInit() */
141 for (i = 0; i < 4; i++) {
142 if (pPriv->DCTRevScaleTables[i])
143 ILJPG_FREE (pPriv->DCTRevScaleTables[i]);
152 float implementation of inverse quantize and inverse dct.
153 input and output in integer format.
154 Output is clamped to the range 0-255.
156 perform inverse quantization and inverse DCT of a 8x8 array. During
157 inverse quantization, the input array is processed in dezigzag order.
160 includes the +128 shift in inverse DCT as per JPEG spec. This shift is
161 accomplished by shifting the DC value by 128 prior to performing the
164 The constants b1,..,b5 used by the fast DCT method are declared as
165 register variables; this allows the 'c89' compiler to perform the
166 multplies as floating point 32 bit multiplies rather than promoting
167 to double followed by a double to float conversion.
168 (4/29/92, V. Bhaskaran)
173 This implementation is based on computation of a 8 point inverse DCT
174 using a 16 point Winograd Fourier Transform.
175 The winograd fourier transform method requires 5 multiplies and 29
176 additions for a 8 point inverse dct.
178 Let T[] be a 8x8 input DCT array for which we need to perform the
181 | t00 t01 t02 t03 t04 t05 t06 t07 |
182 | t10 t11 t12 t13 t14 t15 t16 t17 |
183 | t20 t21 t22 t23 t24 t25 t26 t27 |
184 T[] = | t30 t31 t32 t33 t34 t35 t36 t37 |
185 | t40 t41 t42 t43 t44 t45 t46 t47 |
186 | t50 t51 t52 t53 t54 t55 t56 t57 |
187 | t60 t61 t62 t63 t64 t65 t66 t67 |
188 | t70 t71 t72 t73 t74 t75 t76 t77 |
190 1. The T[] matrix values are descaled by the inverse DCT denormalization
191 values and the quantization matrix values.
192 If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
194 X(i,j) = s(i,j) * t(i,j)
195 Here, S[] is the descaling matrix and includes quantization matrix.
198 a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
200 C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
203 bi = 2 a[i], i = 1,2,...,7
205 Then, descaling matrix S[] has the following structure
206 | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
207 | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
208 | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
209 | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
210 | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
211 | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
212 | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
213 | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
215 Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
216 during the compression stage.
217 Note that in the above descaling matrix description,
218 bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
219 The descaling matrix can be precomputed at the start of the component
220 scan (JPEG terminology).
222 2. After T[] has been descaled, for each column of the descaled matrix,
223 X[], perform a 8 point inverse dct and write results back to X[] in
225 Note that input to 8 point inverse dct is out of order, i.e. x0, x4,
226 x2, x6, x5, x1, x7, x3, but output is in order.
227 y0, y1, y2, y3, y4, y5, y6, y7
228 3. After all eight column inverse dcts have been computed, perform
229 8 point inverse dct on each row of X[] and write results to Y[]
231 4. The Y[] matrix values must be rounded to meet the specifications of
232 the input data that was compressed. Typically, for image data, this
233 implies restricting Y[] to take on values only in the range 0 - 255.
236 Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
237 bhaskara@hplvab.hpl.hp.com
238 Version: 0 (5-29-92).
242 /* -------------------- _iljpgDeDCTFull -------------------------- */
243 /* Do a full 8x8 inverse DCT, from *pSrc to *pDst, where each "scan line"
244 in the 8x8 block pointed to by pDst is "nBytesPerRow" bytes away.
245 pRevScale is from DCTRevScaleTables[i], where i is the Q table index
249 void _iljpgDeDCTFull (
252 iljpgPtr ix, /* RETURNED */
259 float in0, in1, in2, in3, in4, in5, in6, in7;
260 float tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
265 /* Constants needed by the 16 point Winograd Fourier Transform for inv. DCT */
266 float b1 = 1.41421356;
267 float b2 = -2.61312587;
268 float b3 = 1.41421356;
269 float b4 = 1.08239220;
270 float b5 = 0.76536686;
274 zptr = &_iljpgZigzagTable[0]; /* zigzag scanning order */
275 tmp = 128.0; /* JPEG +128 shift in spatial domain for DCT output */
277 /* do 1-D inverse DCT along columns. setup input data, which is out of order */
278 for (i = 0; i < 8; i++, oxaddr++, zptr++, pRevScale++, tmp = 0.0) {
280 in0 = *(pSrc + zptr[0]) * pRevScale[0] + tmp; /* Add 128.0 to first DCT only */
281 in1 = *(pSrc + zptr[32]) * pRevScale[32];
282 in2 = *(pSrc + zptr[16]) * pRevScale[16];
283 in3 = *(pSrc + zptr[48]) * pRevScale[48];
284 in4 = *(pSrc + zptr[40]) * pRevScale[40];
285 in5 = *(pSrc + zptr[8]) * pRevScale[8];
286 in6 = *(pSrc + zptr[56]) * pRevScale[56];
287 in7 = *(pSrc + zptr[24]) * pRevScale[24];
328 *oxaddr = in0 + in7; /* y0 */
329 *(oxaddr+8) = in1 + in6; /* y1 */
330 *(oxaddr+16) = tmp2 + tmp5; /* y2 */
331 *(oxaddr+24) = in3 - tmp4; /* y3 */
332 *(oxaddr+32) = in3 + tmp4; /* y4 */
333 *(oxaddr+40) = tmp2 - tmp5; /* y5 */
334 *(oxaddr+48) = in1 - in6; /* y6 */
335 *(oxaddr+56) = in0 - in7; /* y7 */
340 for (i = 0; i < 8; i++) { /* do 1-D inverse DCT along rows */
342 /* setup input data - input data is out of order */
372 in4 = b2 * tmp4 + tmp;
374 in6 = b4 * tmp6 - tmp;
388 /* Stage 4: clip values to 0..255 and store */
389 clipValue = (int)(in0 + in7); /* y0 */
390 ILJPG_CLIP_256 (fClipG0, fClipR0)
392 clipValue = (int)(in1 + in6); /* y1 */
393 ILJPG_CLIP_256 (fClipG1, fClipR1)
394 *(ixaddr+1) = clipValue;
395 clipValue = (int)(tmp2 + tmp5); /* y2 */
396 ILJPG_CLIP_256 (fClipG2, fClipR2)
397 *(ixaddr+2) = clipValue;
398 clipValue = (int)(in3 - tmp4); /* y3 */
399 ILJPG_CLIP_256 (fClipG3, fClipR3)
400 *(ixaddr+3) = clipValue;
401 clipValue = (int)(in3 + tmp4); /* y4 */
402 ILJPG_CLIP_256 (fClipG4, fClipR4)
403 *(ixaddr+4) = clipValue;
404 clipValue = (int)(tmp2 - tmp5); /* y5 */
405 ILJPG_CLIP_256 (fClipG5, fClipR5)
406 *(ixaddr+5) = clipValue;
407 clipValue = (int)(in1 - in6); /* y6 */
408 ILJPG_CLIP_256 (fClipG6, fClipR6)
409 *(ixaddr+6) = clipValue;
410 clipValue = (int)(in0 - in7); /* y7 */
411 ILJPG_CLIP_256 (fClipG7, fClipR7)
412 *(ixaddr+7) = clipValue;
415 ixaddr += nBytesPerRow;
418 /* Goto points for above clip macros */
421 ILJPG_CLIP_256_LABEL (fClipG0, fClipR0)
422 ILJPG_CLIP_256_LABEL (fClipG1, fClipR1)
423 ILJPG_CLIP_256_LABEL (fClipG2, fClipR2)
424 ILJPG_CLIP_256_LABEL (fClipG3, fClipR3)
425 ILJPG_CLIP_256_LABEL (fClipG4, fClipR4)
426 ILJPG_CLIP_256_LABEL (fClipG5, fClipR5)
427 ILJPG_CLIP_256_LABEL (fClipG6, fClipR6)
428 ILJPG_CLIP_256_LABEL (fClipG7, fClipR7)
434 float implementation of inverse quantize and inverse dct.
435 Assumes that only the first 4x4 submatrix of DCT
436 coefficients is non-zero.
437 Input and output in integer format.
439 This reduces the fast inverse DCT multiply and add count
440 from 80 multiplies, 464 additions to 60 multiplies,
443 perform inverse quantization and inverse DCT of a 8x8 array.
444 Data is dezigzagged during the inverse quantization process.
447 includes the +128 shift in inverse DCT as per JPEG spec. This shift is
448 accomplished by shifting the DC value by 128 prior to performing the
453 This implementation is based on computation of a 8 point inverse DCT
454 using a 16 point Winograd Fourier Transform.
455 The winograd fourier transform method requires 5 multiplies and 29
456 additions for a 8 point inverse dct.
458 Let T[] be a 8x8 input DCT array for which we need to perform the
460 Note that the matrix shown below suggests that the DCT matrix has
461 only 16 non-zero coefficients and the coefficient locations are
462 as indicated in the matrix shown below.
464 | t00 t01 t02 t03 --- --- --- --- |
465 | t10 t11 t12 t13 --- --- --- --- |
466 | t20 t21 t22 t23 --- --- --- --- |
467 T[] = | t30 t31 t32 t33 --- --- --- --- |
468 | --- --- --- --- --- --- --- --- |
469 | --- --- --- --- --- --- --- --- |
470 | --- --- --- --- --- --- --- --- |
471 | --- --- --- --- --- --- --- --- |
473 1. The T[] matrix values are descaled by the inverse DCT denormalization
474 values and the quantization matrix values.
475 If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
477 X(i,j) = s(i,j) * t(i,j)
478 Here, S[] is the descaling matrix and includes quantization matrix.
479 Since only 1/4th of the T[] matrix is non-zero we need to descale
480 only these components. The rest are simply accounted for by setting
481 the corresponding locations in X[] to zero.
484 a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
486 C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
489 bi = 2 a[i], i = 1,2,...,7
491 Then, descaling matrix S[] has the following structure
492 | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
493 | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
494 | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
495 | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
496 | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
497 | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
498 | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
499 | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
501 Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
502 during the compression stage.
503 Note that in the above descaling matrix description,
504 bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
505 The descaling matrix can be precomputed at the start of the component
506 scan (JPEG terminology).
508 2. After T[] has been descaled, for each column of the descaled matrix,
509 X[], perform a 8 point inverse dct and write results back to X[] in
510 column order. A pruned dct which uses 4 input points and generates
511 8 output points is used.
512 Note that input to 8 point inverse dct is out of order, i.e. x0, x4,
513 x2, x6, x5, x1, x7, x3, but output is in order.
514 y0, y1, y2, y3, y4, y5, y6, y7
515 Since we know that only the first four components of each column is
516 nonzero, we exploit this in pruning our fast DCT computation, thus,
517 reducing the multiplies/adds by a factor of two.
519 3. After the first four column inverse dcts have been computed, perform
520 8 point inverse dct on each row of X[] and write results to Y[]
522 Since we know that the first four components along each row could
523 be non-zero (the remaining have to be zero as per the structure of
524 the X[] matrix), we can exploit this in pruning the fast DCT
525 computation, and thereby, reducing the multiplies/adds by a factor
527 4. The Y[] matrix values must be rounded to meet the specifications of
528 the input data that was compressed. Typically, for image data, this
529 implies restricting Y[] to take on values only in the range 0 - 255.
532 Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
533 Version: 0 (5-29-92).
536 /* -------------------- _iljpgDeDCT4x4 -------------------------- */
537 /* Do an inverse DCT, from *pSrc to *pDst, each a ptr to 64 ints.
538 Assumes that only the top-left 4x4 DCT coefficients are non-zero.
539 pRevScale is from DCTRevScaleTables[i], where i is the Q table index
543 void _iljpgDeDCT4x4 (
546 iljpgPtr ix, /* RETURNED */
553 float in0, in2, in3, in4, in5, in7;
554 float tmp0, tmp1, tmp2, tmp5, tmp6, tmp7;
559 /* Constants needed by the 16 point Winograd Fourier Transform for inv. DCT */
560 float b1 = 1.41421356;
561 float b2 = -2.61312587;
562 float b3 = 1.41421356;
563 float b4 = 1.08239220;
564 float b5 = 0.76536686;
569 rzptr = &_iljpgZigzagTable[0];
570 /* descale-dequantize dct coefficients - need to do this for 4x4 submatrix */
571 for (i = 0; i < 4; i++, orxptr += 8, rsptr += 8, rzptr += 8) {
572 for (j = 0, ocxptr = orxptr, csptr = rsptr, czptr = rzptr;
573 j < 4; j++, ocxptr++, czptr++) {
574 k = *(pSrc + *czptr);
575 *ocxptr = (k * *csptr++);
579 *ox += 128.0; /* JPEG +128 shift in spatial domain for DCT output */
583 zptr = &_iljpgZigzagTable[0]; /* zigzag scanning order */
584 tmp = 128.0; /* JPEG +128 shift in spatial domain for DCT output */
586 /* do 1-D inverse DCT along columns. setup input data, which is out of order */
587 for (i = 0; i < 4; i++, oxaddr++, zptr++, pRevScale++, tmp = 0.0) {
589 in0 = *(pSrc + zptr[0]) * pRevScale[0] + tmp; /* Add 128.0 to first DCT only */
590 in2 = *(pSrc + zptr[16]) * pRevScale[16];
591 in5 = *(pSrc + zptr[8]) * pRevScale[8];
592 in7 = *(pSrc + zptr[24]) * pRevScale[24];
629 *oxaddr = tmp0 + tmp7; /* y0 */
630 *(oxaddr+8) = tmp1 + tmp6; /* y1 */
631 *(oxaddr+16) = tmp2 + in5; /* y2 */
632 *(oxaddr+24) = in3 - in4; /* y3 */
633 *(oxaddr+32) = in3 + in4; /* y4 */
634 *(oxaddr+40) = tmp2 - in5; /* y5 */
635 *(oxaddr+48) = tmp1 - tmp6; /* y6 */
636 *(oxaddr+56) = tmp0 - tmp7; /* y7 */
641 for (i = 0; i < 8; i++) { /* do 1-D inverse DCT along rows */
643 /* setup input data - input data is out of order */
680 clipValue = (int)(tmp0 + tmp7); /* y0 */
681 ILJPG_CLIP_256 (pClipG0, pClipR0)
683 clipValue = (int)(tmp1 + tmp6); /* y1 */
684 ILJPG_CLIP_256 (pClipG1, pClipR1)
685 *(ixaddr+1) = clipValue;
686 clipValue = (int)(tmp2 + in5); /* y2 */
687 ILJPG_CLIP_256 (pClipG2, pClipR2)
688 *(ixaddr+2) = clipValue;
689 clipValue = (int)(in3 - in4); /* y3 */
690 ILJPG_CLIP_256 (pClipG3, pClipR3)
691 *(ixaddr+3) = clipValue;
692 clipValue = (int)(in3 + in4); /* y4 */
693 ILJPG_CLIP_256 (pClipG4, pClipR4)
694 *(ixaddr+4) = clipValue;
695 clipValue = (int)(tmp2 - in5); /* y5 */
696 ILJPG_CLIP_256 (pClipG5, pClipR5)
697 *(ixaddr+5) = clipValue;
698 clipValue = (int)(tmp1 - tmp6); /* y6 */
699 ILJPG_CLIP_256 (pClipG6, pClipR6)
700 *(ixaddr+6) = clipValue;
701 clipValue = (int)(tmp0 - tmp7); /* y7 */
702 ILJPG_CLIP_256 (pClipG7, pClipR7)
703 *(ixaddr+7) = clipValue;
705 ixaddr += nBytesPerRow;
710 /* Goto points for above clip macros */
713 ILJPG_CLIP_256_LABEL (pClipG0, pClipR0)
714 ILJPG_CLIP_256_LABEL (pClipG1, pClipR1)
715 ILJPG_CLIP_256_LABEL (pClipG2, pClipR2)
716 ILJPG_CLIP_256_LABEL (pClipG3, pClipR3)
717 ILJPG_CLIP_256_LABEL (pClipG4, pClipR4)
718 ILJPG_CLIP_256_LABEL (pClipG5, pClipR5)
719 ILJPG_CLIP_256_LABEL (pClipG6, pClipR6)
720 ILJPG_CLIP_256_LABEL (pClipG7, pClipR7)
724 float implementation of inverse quantize and inverse dct.
726 perform inverse quantization and inverse DCT of a 8x8 array.
727 Assumes that all coefficients of 8x8 DCT matrix except DC coefficient
729 Input and output in integer format.
731 includes the +128 shift in inverse DCT as per JPEG spec. This shift is
732 accomplished by shifting the DC value by 128 prior to performing the
737 Let T[] be a 8x8 input DCT array for which we need to perform the
739 Note that the matrix shown below suggests that the DCT matrix has
740 only 16 non-zero coefficients and the coefficient locations are
741 as indicated in the matrix shown below.
743 | t00 --- --- --- --- --- --- --- |
744 | --- --- --- --- --- --- --- --- |
745 | --- --- --- --- --- --- --- --- |
746 T[] = | --- --- --- --- --- --- --- --- |
747 | --- --- --- --- --- --- --- --- |
748 | --- --- --- --- --- --- --- --- |
749 | --- --- --- --- --- --- --- --- |
750 | --- --- --- --- --- --- --- --- |
752 1. The T[] matrix values are descaled by the inverse DCT denormalization
753 values and the quantization matrix values.
754 If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
756 X(i,j) = s(i,j) * t(i,j)
757 Here, S[] is the descaling matrix and includes quantization matrix.
758 Since only 1/4th of the T[] matrix is non-zero we need to descale
759 only these components. The rest are simply accounted for by setting
760 the corresponding locations in X[] to zero.
763 a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
765 C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
768 bi = 2 a[i], i = 1,2,...,7
770 Then, descaling matrix S[] has the following structure
771 | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
772 | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
773 | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
774 | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
775 | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
776 | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
777 | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
778 | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
780 Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
781 during the compression stage.
782 Note that in the above descaling matrix description,
783 bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
784 The descaling matrix can be precomputed at the start of the component
785 scan (JPEG terminology).
787 2. After T[] has been descaled, compute y(0,0) = t(0,0) * s(0,0) + 128.
788 set y(i,j) = y(0,0), for all i = 0,7, j = 0,7.
789 4. The Y[] matrix values must be rounded to meet the specifications of
790 the input data that was compressed. Typically, for image data, this
791 implies restricting Y[] to take on values only in the range 0 - 255.
793 Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
794 Version: 0 (5-29-92).
797 /* -------------------- _iljpgDeDCTDCOnly -------------------------- */
798 /* Do an inverse DCT, from *pSrc to *pDst, each a ptr to 64 ints.
799 Assumes that only the top-left coefficient (the DC) is non-zero.
800 pRevScale is from DCTRevScaleTables[i], where i is the Q table index
804 void _iljpgDeDCTDCOnly (
807 iljpgPtr pDst, /* RETURNED */
815 j = (j < -2047) ? -2047 : ((j > 2047) ? 2047 : j);
816 dc = (int)(j * *pRevScale + 128.0);
817 if (dc < 0) dc = 0; else if (dc > 255) dc = 255;
820 since only DC value is nonzero, inverse DCT is simply a copy of the
821 descaled and dequantized DC value copied to rest of 8x8 array.
823 for (i = 0; i < 8; i++, pDst += nBytesPerRow) {