iljpgdedct.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 librararies 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: iljpgdedct.c /main/3 1995/10/23 15:55:19 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1992 Hewlett-Packard Co.
  27. ***
  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).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. #include "iljpgdecodeint.h"
  40. #include <math.h>
  41. #include <stdlib.h>
  42. /* Macros to check if "clipValue" (an int) is outside range 0..255, and
  43. to branch to point named by second macro if so, which clips and returns.
  44. This is done to avoid taking a branch on the common case of value
  45. not out of range - significant speedup on RISC machine.
  46. */
  47. #define ILJPG_CLIP_256(_gotoLabel, _returnLabel) \
  48. if ((clipValue) >> 8) goto _gotoLabel; \
  49. _returnLabel:
  50. #define ILJPG_CLIP_256_LABEL(_gotoLabel, _returnLabel) \
  51. _gotoLabel: \
  52. if ((clipValue) > 255) clipValue = 255; else clipValue = 0; \
  53. goto _returnLabel;
  54. /* compute 2-D DCT descaling matrix */
  55. static void _il_fwft_rev_scale (
  56. iljpgPtr q, /* pointer to quantization matrix */
  57. float *s /* pointer to pointer to descaling matrix */
  58. )
  59. {
  60. register int i, j, prevValue, value, QIndex;
  61. double a, c0, b[8], pi;
  62. register iljpgPtr qptr;
  63. register float *sptr;
  64. pi = 4.0 * atan(1.0);
  65. c0 = 1.0 / (2.0 * 0.707106718);
  66. a = 1.0 / (2 * c0);
  67. b[0] = a / 2.0;
  68. for (i = 1; i < 8; i++) {
  69. a = cos (i * pi / 16.0) / 2.0;
  70. b[i] = a;
  71. }
  72. /* descaling matrix including dequantization effects */
  73. /* Note: given Q table is zigzag'd, as in JIF stream. De-zigzag *qptr.
  74. Also: if an entry in the Q table is zero, then: if it is the first entry,
  75. store 16 (?) in the Q table, otherwise store the previous Q table value.
  76. */
  77. sptr = s;
  78. qptr = q;
  79. prevValue = 16; /* in case 1st Q table value is 0 */
  80. QIndex = 0;
  81. for (i = 0; i < 8; i++) {
  82. for (j = 0; j < 8; j++) {
  83. value = qptr[_iljpgZigzagTable[QIndex++]];
  84. if (value == 0)
  85. value = prevValue;
  86. *sptr++ = b[i] * b[j] * value;
  87. prevValue = value;
  88. }
  89. }
  90. }
  91. /* -------------------- _iljpgDeDCTInit -------------------------- */
  92. /* Called by iljpgDecode() to init for DCT decoding.
  93. */
  94. ILJPG_PRIVATE_EXTERN
  95. iljpgError _iljpgDeDCTInit (
  96. iljpgDecodePrivPtr pPriv
  97. )
  98. {
  99. iljpgDataPtr pData;
  100. register int i;
  101. pData = pPriv->pData;
  102. /* Build a "rev scale" table for each QTable; store into private */
  103. for (i = 0; i < 4; i++)
  104. pPriv->DCTRevScaleTables[i] = (float *)NULL;
  105. for (i = 0; i < 4; i++) {
  106. if (pData->QTables[i]) {
  107. if (!(pPriv->DCTRevScaleTables[i] = (float *)ILJPG_MALLOC(sizeof(float) * 64)))
  108. return ILJPG_ERROR_DECODE_MALLOC;
  109. _il_fwft_rev_scale (pData->QTables[i], pPriv->DCTRevScaleTables[i]);
  110. }
  111. }
  112. return 0;
  113. }
  114. /* -------------------- _iljpgDeDCTCleanup -------------------------- */
  115. /* Called by iljpgDecode() to cleanup after DCT decoding.
  116. */
  117. ILJPG_PRIVATE_EXTERN
  118. iljpgError _iljpgDeDCTCleanup (
  119. iljpgDecodePrivPtr pPriv
  120. )
  121. {
  122. register int i;
  123. /* Free any "rev scale" tables that were allocated in iljpgDeDCTInit() */
  124. for (i = 0; i < 4; i++) {
  125. if (pPriv->DCTRevScaleTables[i])
  126. ILJPG_FREE (pPriv->DCTRevScaleTables[i]);
  127. }
  128. return 0;
  129. }
  130. /*
  131. NAME
  132. float implementation of inverse quantize and inverse dct.
  133. input and output in integer format.
  134. Output is clamped to the range 0-255.
  135. PURPOSE
  136. perform inverse quantization and inverse DCT of a 8x8 array. During
  137. inverse quantization, the input array is processed in dezigzag order.
  138. NOTES
  139. includes the +128 shift in inverse DCT as per JPEG spec. This shift is
  140. accomplished by shifting the DC value by 128 prior to performing the
  141. inverse DCT.
  142. The constants b1,..,b5 used by the fast DCT method are declared as
  143. register variables; this allows the 'c89' compiler to perform the
  144. multplies as floating point 32 bit multiplies rather than promoting
  145. to double followed by a double to float conversion.
  146. (4/29/92, V. Bhaskaran)
  147. METHOD
  148. 8x8 INVERSE DCT
  149. This implementation is based on computation of a 8 point inverse DCT
  150. using a 16 point Winograd Fourier Transform.
  151. The winograd fourier transform method requires 5 multiplies and 29
  152. additions for a 8 point inverse dct.
  153. Let T[] be a 8x8 input DCT array for which we need to perform the
  154. inverse DCT.
  155. | t00 t01 t02 t03 t04 t05 t06 t07 |
  156. | t10 t11 t12 t13 t14 t15 t16 t17 |
  157. | t20 t21 t22 t23 t24 t25 t26 t27 |
  158. T[] = | t30 t31 t32 t33 t34 t35 t36 t37 |
  159. | t40 t41 t42 t43 t44 t45 t46 t47 |
  160. | t50 t51 t52 t53 t54 t55 t56 t57 |
  161. | t60 t61 t62 t63 t64 t65 t66 t67 |
  162. | t70 t71 t72 t73 t74 t75 t76 t77 |
  163. 1. The T[] matrix values are descaled by the inverse DCT denormalization
  164. values and the quantization matrix values.
  165. If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
  166. then,
  167. X(i,j) = s(i,j) * t(i,j)
  168. Here, S[] is the descaling matrix and includes quantization matrix.
  169. Define a[n] as
  170. a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
  171. and,
  172. C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
  173. Define b[n] as
  174. b0 = a[0]
  175. bi = 2 a[i], i = 1,2,...,7
  176. Then, descaling matrix S[] has the following structure
  177. | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
  178. | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
  179. | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
  180. | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
  181. | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
  182. | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
  183. | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
  184. | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
  185. Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
  186. during the compression stage.
  187. Note that in the above descaling matrix description,
  188. bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
  189. The descaling matrix can be precomputed at the start of the component
  190. scan (JPEG terminology).
  191. 2. After T[] has been descaled, for each column of the descaled matrix,
  192. X[], perform a 8 point inverse dct and write results back to X[] in
  193. column order.
  194. Note that input to 8 point inverse dct is out of order, i.e. x0, x4,
  195. x2, x6, x5, x1, x7, x3, but output is in order.
  196. y0, y1, y2, y3, y4, y5, y6, y7
  197. 3. After all eight column inverse dcts have been computed, perform
  198. 8 point inverse dct on each row of X[] and write results to Y[]
  199. in row order.
  200. 4. The Y[] matrix values must be rounded to meet the specifications of
  201. the input data that was compressed. Typically, for image data, this
  202. implies restricting Y[] to take on values only in the range 0 - 255.
  203. NOTES
  204. Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
  205. bhaskara@hplvab.hpl.hp.com
  206. Version: 0 (5-29-92).
  207. */
  208. /* -------------------- _iljpgDeDCTFull -------------------------- */
  209. /* Do a full 8x8 inverse DCT, from *pSrc to *pDst, where each "scan line"
  210. in the 8x8 block pointed to by pDst is "nBytesPerRow" bytes away.
  211. pRevScale is from DCTRevScaleTables[i], where i is the Q table index
  212. for this component.
  213. */
  214. ILJPG_PRIVATE_EXTERN
  215. void _iljpgDeDCTFull (
  216. register int *pSrc,
  217. long nBytesPerRow,
  218. iljpgPtr ix, /* RETURNED */
  219. register float *pRevScale
  220. )
  221. {
  222. register int i;
  223. register int *zptr;
  224. float ox[64];
  225. register float in0, in1, in2, in3, in4, in5, in6, in7;
  226. register float tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  227. register float tmp;
  228. register int clipValue;
  229. register iljpgPtr ixaddr;
  230. register float *oxaddr;
  231. /* Constants needed by the 16 point Winograd Fourier Transform for inv. DCT */
  232. register float b1 = 1.41421356;
  233. register float b2 = -2.61312587;
  234. register float b3 = 1.41421356;
  235. register float b4 = 1.08239220;
  236. register float b5 = 0.76536686;
  237. oxaddr = ox;
  238. zptr = &_iljpgZigzagTable[0]; /* zigzag scanning order */
  239. tmp = 128.0; /* JPEG +128 shift in spatial domain for DCT output */
  240. /* do 1-D inverse DCT along columns. setup input data, which is out of order */
  241. for (i = 0; i < 8; i++, oxaddr++, zptr++, pRevScale++, tmp = 0.0) {
  242. in0 = *(pSrc + zptr[0]) * pRevScale[0] + tmp; /* Add 128.0 to first DCT only */
  243. in1 = *(pSrc + zptr[32]) * pRevScale[32];
  244. in2 = *(pSrc + zptr[16]) * pRevScale[16];
  245. in3 = *(pSrc + zptr[48]) * pRevScale[48];
  246. in4 = *(pSrc + zptr[40]) * pRevScale[40];
  247. in5 = *(pSrc + zptr[8]) * pRevScale[8];
  248. in6 = *(pSrc + zptr[56]) * pRevScale[56];
  249. in7 = *(pSrc + zptr[24]) * pRevScale[24];
  250. /* Stage 0 */
  251. tmp4 = in4 - in7;
  252. tmp5 = in5 + in6;
  253. tmp6 = in5 - in6;
  254. tmp7 = in4 + in7;
  255. /* Stage 1 */
  256. tmp2 = in2 - in3;
  257. tmp3 = in2 + in3;
  258. in5 = tmp5 - tmp7;
  259. in7 = tmp5 + tmp7;
  260. /* Stage 1.1 */
  261. tmp = tmp4 - tmp6;
  262. tmp = b5 * tmp;
  263. /* Stage 2 */
  264. in2 = b1 * tmp2;
  265. in4 = b2 * tmp4;
  266. tmp5 = b3 * in5;
  267. in6 = b4 * tmp6;
  268. tmp0 = in0 + in1;
  269. tmp1 = in0 - in1;
  270. in2 -= tmp3;
  271. in4 += tmp;
  272. in6 -= tmp;
  273. /* Stage 3 */
  274. in6 -= in7;
  275. tmp5 -= in6;
  276. /* Stage 4 */
  277. in0 = tmp0 + tmp3;
  278. in1 = tmp1 + in2;
  279. tmp2 = tmp1 - in2;
  280. in3 = tmp0 - tmp3;
  281. tmp4 = in4 + tmp5;
  282. /* Stage 5 */
  283. *oxaddr = in0 + in7; /* y0 */
  284. *(oxaddr+8) = in1 + in6; /* y1 */
  285. *(oxaddr+16) = tmp2 + tmp5; /* y2 */
  286. *(oxaddr+24) = in3 - tmp4; /* y3 */
  287. *(oxaddr+32) = in3 + tmp4; /* y4 */
  288. *(oxaddr+40) = tmp2 - tmp5; /* y5 */
  289. *(oxaddr+48) = in1 - in6; /* y6 */
  290. *(oxaddr+56) = in0 - in7; /* y7 */
  291. }
  292. ixaddr = ix;
  293. oxaddr = ox;
  294. for (i = 0; i < 8; i++) { /* do 1-D inverse DCT along rows */
  295. /* setup input data - input data is out of order */
  296. in0 = *oxaddr;
  297. in1 = *(oxaddr+4);
  298. in2 = *(oxaddr+2);
  299. in3 = *(oxaddr+6);
  300. in4 = *(oxaddr+5);
  301. in5 = *(oxaddr+1);
  302. in6 = *(oxaddr+7);
  303. in7 = *(oxaddr+3);
  304. /* Stage 0 */
  305. tmp4 = in4 - in7;
  306. tmp5 = in5 + in6;
  307. tmp6 = in5 - in6;
  308. tmp7 = in4 + in7;
  309. /* Stage 1 */
  310. tmp2 = in2 - in3;
  311. tmp3 = in2 + in3;
  312. in5 = tmp5 - tmp7;
  313. in7 = tmp5 + tmp7;
  314. /* Stage 1.1 */
  315. tmp = tmp4 - tmp6;
  316. tmp = b5 * tmp;
  317. /* Stage 2 */
  318. tmp0 = in0 + in1;
  319. tmp1 = in0 - in1;
  320. in2 = b1 * tmp2;
  321. in4 = b2 * tmp4 + tmp;
  322. tmp5 = b3 * in5;
  323. in6 = b4 * tmp6 - tmp;
  324. in2 -= tmp3;
  325. /* Stage 2.1 */
  326. in6 -= in7;
  327. tmp5 -= in6;
  328. /* Stage 3 */
  329. in0 = tmp0 + tmp3;
  330. in1 = tmp1 + in2;
  331. tmp2 = tmp1 - in2;
  332. in3 = tmp0 - tmp3;
  333. tmp4 = in4 + tmp5;
  334. /* Stage 4: clip values to 0..255 and store */
  335. clipValue = (int)(in0 + in7); /* y0 */
  336. ILJPG_CLIP_256 (fClipG0, fClipR0)
  337. *ixaddr = clipValue;
  338. clipValue = (int)(in1 + in6); /* y1 */
  339. ILJPG_CLIP_256 (fClipG1, fClipR1)
  340. *(ixaddr+1) = clipValue;
  341. clipValue = (int)(tmp2 + tmp5); /* y2 */
  342. ILJPG_CLIP_256 (fClipG2, fClipR2)
  343. *(ixaddr+2) = clipValue;
  344. clipValue = (int)(in3 - tmp4); /* y3 */
  345. ILJPG_CLIP_256 (fClipG3, fClipR3)
  346. *(ixaddr+3) = clipValue;
  347. clipValue = (int)(in3 + tmp4); /* y4 */
  348. ILJPG_CLIP_256 (fClipG4, fClipR4)
  349. *(ixaddr+4) = clipValue;
  350. clipValue = (int)(tmp2 - tmp5); /* y5 */
  351. ILJPG_CLIP_256 (fClipG5, fClipR5)
  352. *(ixaddr+5) = clipValue;
  353. clipValue = (int)(in1 - in6); /* y6 */
  354. ILJPG_CLIP_256 (fClipG6, fClipR6)
  355. *(ixaddr+6) = clipValue;
  356. clipValue = (int)(in0 - in7); /* y7 */
  357. ILJPG_CLIP_256 (fClipG7, fClipR7)
  358. *(ixaddr+7) = clipValue;
  359. oxaddr += 8;
  360. ixaddr += nBytesPerRow;
  361. }
  362. /* Goto points for above clip macros */
  363. return;
  364. ILJPG_CLIP_256_LABEL (fClipG0, fClipR0)
  365. ILJPG_CLIP_256_LABEL (fClipG1, fClipR1)
  366. ILJPG_CLIP_256_LABEL (fClipG2, fClipR2)
  367. ILJPG_CLIP_256_LABEL (fClipG3, fClipR3)
  368. ILJPG_CLIP_256_LABEL (fClipG4, fClipR4)
  369. ILJPG_CLIP_256_LABEL (fClipG5, fClipR5)
  370. ILJPG_CLIP_256_LABEL (fClipG6, fClipR6)
  371. ILJPG_CLIP_256_LABEL (fClipG7, fClipR7)
  372. }
  373. /*
  374. float implementation of inverse quantize and inverse dct.
  375. Assumes that only the first 4x4 submatrix of DCT
  376. coefficients is non-zero.
  377. Input and output in integer format.
  378. This reduces the fast inverse DCT multiply and add count
  379. from 80 multiplies, 464 additions to 60 multiplies,
  380. 252 additions.
  381. PURPOSE
  382. perform inverse quantization and inverse DCT of a 8x8 array.
  383. Data is dezigzagged during the inverse quantization process.
  384. NOTES
  385. includes the +128 shift in inverse DCT as per JPEG spec. This shift is
  386. accomplished by shifting the DC value by 128 prior to performing the
  387. inverse DCT.
  388. METHOD
  389. 8x8 INVERSE DCT
  390. This implementation is based on computation of a 8 point inverse DCT
  391. using a 16 point Winograd Fourier Transform.
  392. The winograd fourier transform method requires 5 multiplies and 29
  393. additions for a 8 point inverse dct.
  394. Let T[] be a 8x8 input DCT array for which we need to perform the
  395. inverse DCT.
  396. Note that the matrix shown below suggests that the DCT matrix has
  397. only 16 non-zero coefficients and the coefficient locations are
  398. as indicated in the matrix shown below.
  399. | t00 t01 t02 t03 --- --- --- --- |
  400. | t10 t11 t12 t13 --- --- --- --- |
  401. | t20 t21 t22 t23 --- --- --- --- |
  402. T[] = | t30 t31 t32 t33 --- --- --- --- |
  403. | --- --- --- --- --- --- --- --- |
  404. | --- --- --- --- --- --- --- --- |
  405. | --- --- --- --- --- --- --- --- |
  406. | --- --- --- --- --- --- --- --- |
  407. 1. The T[] matrix values are descaled by the inverse DCT denormalization
  408. values and the quantization matrix values.
  409. If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
  410. then,
  411. X(i,j) = s(i,j) * t(i,j)
  412. Here, S[] is the descaling matrix and includes quantization matrix.
  413. Since only 1/4th of the T[] matrix is non-zero we need to descale
  414. only these components. The rest are simply accounted for by setting
  415. the corresponding locations in X[] to zero.
  416. Define a[n] as
  417. a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
  418. and,
  419. C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
  420. Define b[n] as
  421. b0 = a[0]
  422. bi = 2 a[i], i = 1,2,...,7
  423. Then, descaling matrix S[] has the following structure
  424. | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
  425. | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
  426. | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
  427. | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
  428. | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
  429. | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
  430. | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
  431. | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
  432. Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
  433. during the compression stage.
  434. Note that in the above descaling matrix description,
  435. bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
  436. The descaling matrix can be precomputed at the start of the component
  437. scan (JPEG terminology).
  438. 2. After T[] has been descaled, for each column of the descaled matrix,
  439. X[], perform a 8 point inverse dct and write results back to X[] in
  440. column order. A pruned dct which uses 4 input points and generates
  441. 8 output points is used.
  442. Note that input to 8 point inverse dct is out of order, i.e. x0, x4,
  443. x2, x6, x5, x1, x7, x3, but output is in order.
  444. y0, y1, y2, y3, y4, y5, y6, y7
  445. Since we know that only the first four components of each column is
  446. nonzero, we exploit this in pruning our fast DCT computation, thus,
  447. reducing the multiplies/adds by a factor of two.
  448. 3. After the first four column inverse dcts have been computed, perform
  449. 8 point inverse dct on each row of X[] and write results to Y[]
  450. in row order.
  451. Since we know that the first four components along each row could
  452. be non-zero (the remaining have to be zero as per the structure of
  453. the X[] matrix), we can exploit this in pruning the fast DCT
  454. computation, and thereby, reducing the multiplies/adds by a factor
  455. of two.
  456. 4. The Y[] matrix values must be rounded to meet the specifications of
  457. the input data that was compressed. Typically, for image data, this
  458. implies restricting Y[] to take on values only in the range 0 - 255.
  459. NOTES
  460. Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
  461. Version: 0 (5-29-92).
  462. */
  463. /* -------------------- _iljpgDeDCT4x4 -------------------------- */
  464. /* Do an inverse DCT, from *pSrc to *pDst, each a ptr to 64 ints.
  465. Assumes that only the top-left 4x4 DCT coefficients are non-zero.
  466. pRevScale is from DCTRevScaleTables[i], where i is the Q table index
  467. for this component.
  468. */
  469. ILJPG_PRIVATE_EXTERN
  470. void _iljpgDeDCT4x4 (
  471. register int *pSrc,
  472. long nBytesPerRow,
  473. iljpgPtr ix, /* RETURNED */
  474. register float *pRevScale
  475. )
  476. {
  477. register int i;
  478. register int *zptr;
  479. float ox[64];
  480. register float in0, in2, in3, in4, in5, in7;
  481. register float tmp0, tmp1, tmp2, tmp5, tmp6, tmp7;
  482. register float tmp;
  483. register float *oxaddr;
  484. register int clipValue;
  485. register iljpgPtr ixaddr;
  486. /* Constants needed by the 16 point Winograd Fourier Transform for inv. DCT */
  487. register float b1 = 1.41421356;
  488. register float b2 = -2.61312587;
  489. register float b3 = 1.41421356;
  490. register float b4 = 1.08239220;
  491. register float b5 = 0.76536686;
  492. #ifdef NOTDEF
  493. orxptr = ox;
  494. rsptr = pRevScale;
  495. rzptr = &_iljpgZigzagTable[0];
  496. /* descale-dequantize dct coefficients - need to do this for 4x4 submatrix */
  497. for (i = 0; i < 4; i++, orxptr += 8, rsptr += 8, rzptr += 8) {
  498. for (j = 0, ocxptr = orxptr, csptr = rsptr, czptr = rzptr;
  499. j < 4; j++, ocxptr++, czptr++) {
  500. k = *(pSrc + *czptr);
  501. *ocxptr = (k * *csptr++);
  502. }
  503. }
  504. *ox += 128.0; /* JPEG +128 shift in spatial domain for DCT output */
  505. #endif
  506. oxaddr = ox;
  507. zptr = &_iljpgZigzagTable[0]; /* zigzag scanning order */
  508. tmp = 128.0; /* JPEG +128 shift in spatial domain for DCT output */
  509. /* do 1-D inverse DCT along columns. setup input data, which is out of order */
  510. for (i = 0; i < 4; i++, oxaddr++, zptr++, pRevScale++, tmp = 0.0) {
  511. in0 = *(pSrc + zptr[0]) * pRevScale[0] + tmp; /* Add 128.0 to first DCT only */
  512. in2 = *(pSrc + zptr[16]) * pRevScale[16];
  513. in5 = *(pSrc + zptr[8]) * pRevScale[8];
  514. in7 = *(pSrc + zptr[24]) * pRevScale[24];
  515. #ifdef NOTDEF
  516. in0 = *oxaddr;
  517. in2 = *(oxaddr+16);
  518. in5 = *(oxaddr+8);
  519. in7 = *(oxaddr+24);
  520. #endif
  521. /* Stage 1 */
  522. tmp5 = in5 - in7;
  523. tmp7 = in5 + in7;
  524. /* Stage 2 */
  525. tmp = -in7 -in5;
  526. tmp = b5 * tmp;
  527. tmp2 = b1 * in2;
  528. tmp6 = b4 * in5;
  529. in4 = b2 * -in7;
  530. in5 = b3 * tmp5;
  531. in4 += tmp;
  532. tmp6 -= tmp;
  533. tmp2 -= in2;
  534. /* Stage 3 */
  535. tmp6 -= tmp7;
  536. in5 -= tmp6;
  537. /* Stage 4 */
  538. tmp0 = in0 + in2;
  539. tmp1 = in0 + tmp2;
  540. in3 = in0 - in2;
  541. in4 += in5;
  542. tmp2 = in0 - tmp2;
  543. /* Stage 5 */
  544. *oxaddr = tmp0 + tmp7; /* y0 */
  545. *(oxaddr+8) = tmp1 + tmp6; /* y1 */
  546. *(oxaddr+16) = tmp2 + in5; /* y2 */
  547. *(oxaddr+24) = in3 - in4; /* y3 */
  548. *(oxaddr+32) = in3 + in4; /* y4 */
  549. *(oxaddr+40) = tmp2 - in5; /* y5 */
  550. *(oxaddr+48) = tmp1 - tmp6; /* y6 */
  551. *(oxaddr+56) = tmp0 - tmp7; /* y7 */
  552. }
  553. ixaddr = ix;
  554. oxaddr = ox;
  555. for (i = 0; i < 8; i++) { /* do 1-D inverse DCT along rows */
  556. /* setup input data - input data is out of order */
  557. in0 = *oxaddr;
  558. in2 = *(oxaddr+2);
  559. in5 = *(oxaddr+1);
  560. in7 = *(oxaddr+3);
  561. /* Stage 1 */
  562. tmp5 = in5 - in7;
  563. tmp7 = in5 + in7;
  564. /* Stage 2 */
  565. tmp = -in7 -in5;
  566. tmp = b5 * tmp;
  567. tmp2 = b1 * in2;
  568. tmp6 = b4 * in5;
  569. in4 = b2 * -in7;
  570. in5 = b3 * tmp5;
  571. in4 += tmp;
  572. tmp6 -= tmp;
  573. /* Stage 3 */
  574. tmp2 -= in2;
  575. /* Stage 3.1 */
  576. tmp6 -= tmp7;
  577. in5 -= tmp6;
  578. /* Stage 4 */
  579. tmp0 = in0 + in2;
  580. tmp1 = in0 + tmp2;
  581. in3 = in0 - in2;
  582. in4 += in5;
  583. tmp2 = in0 - tmp2;
  584. /* Stage 5 */
  585. clipValue = (int)(tmp0 + tmp7); /* y0 */
  586. ILJPG_CLIP_256 (pClipG0, pClipR0)
  587. *ixaddr = clipValue;
  588. clipValue = (int)(tmp1 + tmp6); /* y1 */
  589. ILJPG_CLIP_256 (pClipG1, pClipR1)
  590. *(ixaddr+1) = clipValue;
  591. clipValue = (int)(tmp2 + in5); /* y2 */
  592. ILJPG_CLIP_256 (pClipG2, pClipR2)
  593. *(ixaddr+2) = clipValue;
  594. clipValue = (int)(in3 - in4); /* y3 */
  595. ILJPG_CLIP_256 (pClipG3, pClipR3)
  596. *(ixaddr+3) = clipValue;
  597. clipValue = (int)(in3 + in4); /* y4 */
  598. ILJPG_CLIP_256 (pClipG4, pClipR4)
  599. *(ixaddr+4) = clipValue;
  600. clipValue = (int)(tmp2 - in5); /* y5 */
  601. ILJPG_CLIP_256 (pClipG5, pClipR5)
  602. *(ixaddr+5) = clipValue;
  603. clipValue = (int)(tmp1 - tmp6); /* y6 */
  604. ILJPG_CLIP_256 (pClipG6, pClipR6)
  605. *(ixaddr+6) = clipValue;
  606. clipValue = (int)(tmp0 - tmp7); /* y7 */
  607. ILJPG_CLIP_256 (pClipG7, pClipR7)
  608. *(ixaddr+7) = clipValue;
  609. ixaddr += nBytesPerRow;
  610. oxaddr += 8;
  611. }
  612. /* Goto points for above clip macros */
  613. return;
  614. ILJPG_CLIP_256_LABEL (pClipG0, pClipR0)
  615. ILJPG_CLIP_256_LABEL (pClipG1, pClipR1)
  616. ILJPG_CLIP_256_LABEL (pClipG2, pClipR2)
  617. ILJPG_CLIP_256_LABEL (pClipG3, pClipR3)
  618. ILJPG_CLIP_256_LABEL (pClipG4, pClipR4)
  619. ILJPG_CLIP_256_LABEL (pClipG5, pClipR5)
  620. ILJPG_CLIP_256_LABEL (pClipG6, pClipR6)
  621. ILJPG_CLIP_256_LABEL (pClipG7, pClipR7)
  622. }
  623. /*
  624. float implementation of inverse quantize and inverse dct.
  625. PURPOSE
  626. perform inverse quantization and inverse DCT of a 8x8 array.
  627. Assumes that all coefficients of 8x8 DCT matrix except DC coefficient
  628. are zero.
  629. Input and output in integer format.
  630. NOTES
  631. includes the +128 shift in inverse DCT as per JPEG spec. This shift is
  632. accomplished by shifting the DC value by 128 prior to performing the
  633. inverse DCT.
  634. METHOD
  635. 8x8 INVERSE DCT
  636. Let T[] be a 8x8 input DCT array for which we need to perform the
  637. inverse DCT.
  638. Note that the matrix shown below suggests that the DCT matrix has
  639. only 16 non-zero coefficients and the coefficient locations are
  640. as indicated in the matrix shown below.
  641. | t00 --- --- --- --- --- --- --- |
  642. | --- --- --- --- --- --- --- --- |
  643. | --- --- --- --- --- --- --- --- |
  644. T[] = | --- --- --- --- --- --- --- --- |
  645. | --- --- --- --- --- --- --- --- |
  646. | --- --- --- --- --- --- --- --- |
  647. | --- --- --- --- --- --- --- --- |
  648. | --- --- --- --- --- --- --- --- |
  649. 1. The T[] matrix values are descaled by the inverse DCT denormalization
  650. values and the quantization matrix values.
  651. If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and X[] = {y(i,j)},
  652. then,
  653. X(i,j) = s(i,j) * t(i,j)
  654. Here, S[] is the descaling matrix and includes quantization matrix.
  655. Since only 1/4th of the T[] matrix is non-zero we need to descale
  656. only these components. The rest are simply accounted for by setting
  657. the corresponding locations in X[] to zero.
  658. Define a[n] as
  659. a[n] = cos (n pi / 16) / 2 C[n], pi = 3.1415....
  660. and,
  661. C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
  662. Define b[n] as
  663. b0 = a[0]
  664. bi = 2 a[i], i = 1,2,...,7
  665. Then, descaling matrix S[] has the following structure
  666. | b0b0q00 b1b0q01 b2b0q02 b3b0q03 b4b0q04 b5b0q05 b6b0q06 b7b0q07 |
  667. | b0b1q10 b1b1q11 b2b1q12 b3b1q13 b4b1q14 b5b1q15 b6b1q16 b7b1q17 |
  668. | b0b2q20 b1b2q21 b2b2q22 b3b2q23 b4b2q24 b5b2q25 b6b2q26 b7b2q27 |
  669. | b0b3q30 b1b3q31 b2b3q32 b3b3q33 b4b3q34 b5b3q35 b6b3q36 b7b3q37 |
  670. | b0b4q40 b1b4q41 b2b4q42 b3b4q43 b4b4q44 b5b4q45 b6b4q46 b7b4q47 |
  671. | b0b5q50 b1b5q51 b2b5q52 b3b5q53 b4b5q54 b5b5q55 b6b5q56 b7b5q57 |
  672. | b0b6q60 b1b6q61 b2b6q62 b3b6q63 b4b6q64 b5b6q65 b6b6q66 b7b6q67 |
  673. | b0b7q70 b1b7q71 b2b7q72 b3b7q73 b4b7q74 b5b7q75 b6b7q76 b7b7q77 |
  674. Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
  675. during the compression stage.
  676. Note that in the above descaling matrix description,
  677. bibj = bjbi, and bibjqji implies multiplying bi,bj and qji.
  678. The descaling matrix can be precomputed at the start of the component
  679. scan (JPEG terminology).
  680. 2. After T[] has been descaled, compute y(0,0) = t(0,0) * s(0,0) + 128.
  681. set y(i,j) = y(0,0), for all i = 0,7, j = 0,7.
  682. 4. The Y[] matrix values must be rounded to meet the specifications of
  683. the input data that was compressed. Typically, for image data, this
  684. implies restricting Y[] to take on values only in the range 0 - 255.
  685. NOTES
  686. Author: V. Bhaskaran, HPL, PaloAlto. Telnet: 7-7153.
  687. Version: 0 (5-29-92).
  688. */
  689. /* -------------------- _iljpgDeDCTDCOnly -------------------------- */
  690. /* Do an inverse DCT, from *pSrc to *pDst, each a ptr to 64 ints.
  691. Assumes that only the top-left coefficient (the DC) is non-zero.
  692. pRevScale is from DCTRevScaleTables[i], where i is the Q table index
  693. for this component.
  694. */
  695. ILJPG_PRIVATE_EXTERN
  696. void _iljpgDeDCTDCOnly (
  697. int *pSrc,
  698. long nBytesPerRow,
  699. register iljpgPtr pDst, /* RETURNED */
  700. float *pRevScale
  701. )
  702. {
  703. register int i, dc;
  704. register int j;
  705. j = *pSrc;
  706. j = (j < -2047) ? -2047 : ((j > 2047) ? 2047 : j);
  707. dc = (int)(j * *pRevScale + 128.0);
  708. if (dc < 0) dc = 0; else if (dc > 255) dc = 255;
  709. /*
  710. since only DC value is nonzero, inverse DCT is simply a copy of the
  711. descaled and dequantized DC value copied to rest of 8x8 array.
  712. */
  713. for (i = 0; i < 8; i++, pDst += nBytesPerRow) {
  714. pDst[0] = dc;
  715. pDst[1] = dc;
  716. pDst[2] = dc;
  717. pDst[3] = dc;
  718. pDst[4] = dc;
  719. pDst[5] = dc;
  720. pDst[6] = dc;
  721. pDst[7] = dc;
  722. }
  723. }