iljpgendct.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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: iljpgendct.c /main/3 1995/10/23 15:56:59 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 "iljpgencodeint.h"
  40. #include <math.h>
  41. /* floating point implementation of dct and quantization.
  42. input and output in integer format.
  43. Output is clamped to the range -2047 to 2047.
  44. PURPOSE
  45. compute 8x8 2-D DCT and quantize the DCT results.
  46. NOTES
  47. DCT includes preshifting the input by -128; in our DCT, this is
  48. equivalent to preshifting the DC value by 8192 prior to scaling and
  49. quantizing the DCT output.
  50. Quantized DCT values in 8x8 array is output in zigzag order as per
  51. JPEG's zigzag scanning order.
  52. To use the code modules, the routine 'fwft_fwd_scale' must be
  53. invoked at the start of the compression process and during compression
  54. process, each time a new quantizer specification is required.
  55. METHOD
  56. This implementation is based on computation of a 8 point DCT using
  57. a 16 point Winograd Fourier Transform.
  58. The winograd fourier transform method requires 5 multiplies and 25
  59. additions for a 8 point dct.
  60. Let X[] be a 8x8 input array for which we need to perform the DCT
  61. and quantization.
  62. | x00 x01 x02 x03 x04 x05 x06 x07 |
  63. | x10 x11 x12 x13 x14 x15 x16 x17 |
  64. | x20 x21 x22 x23 x24 x25 x26 x27 |
  65. X[] = | x30 x31 x32 x33 x34 x35 x36 x37 |
  66. | x40 x41 x42 x43 x44 x45 x46 x47 |
  67. | x50 x51 x52 x53 x54 x55 x56 x57 |
  68. | x60 x61 x62 x63 x64 x65 x66 x67 |
  69. | x70 x71 x72 x73 x74 x75 x76 x77 |
  70. 1. For each row of X[], perform a 8 point dct and replace the row in X[]
  71. by the dct output.
  72. Note that input to 8 point dct is in order, i.e. x0, x1, ..., x7, but,
  73. output is out of order, i.e. y0, y4, y2, y6, y5, y1, y7, y3.
  74. 2. After all eight row dcts have been computed, for each column perform
  75. a 8 point dct and write it to 8x8 array T[] in column order.
  76. 3. After all eight column dcts have been performed, the resulting array
  77. is post-multiplied, point-by-point by a scaling matrix S[] to yield
  78. a quantized dct output for X[] as Y[].
  79. If we denote T[] = {t(i,j)}, S[] = {s(i,j)}, and Y[] = {y(i,j)},
  80. then,
  81. y(i,j) = s(i,j) * t(i,j)
  82. Define a[n] as
  83. a[n] = 2 C[n] / cos (n pi / 16), pi = 3.1415....
  84. and,
  85. C[0] = 1 / sqrt(2), C[1] = C[2] = ... = C[7] = 1,
  86. Define b[n] as
  87. b0 = a[0] / 8
  88. bi = a[i] / 16, i = 1,2,...,7
  89. Then, scaling matrix S[] has the following structure
  90. | b0b0/q00 b1b0/q01 b2b0/q02 b3b0/q03 b4b0/q04 b5b0/q05 b6b0/q06 b7b0/q07 |
  91. | b0b1/q10 b1b1/q11 b2b1/q12 b3b1/q13 b4b1/q14 b5b1/q15 b6b1/q16 b7b1/q17 |
  92. | b0b2/q20 b1b2/q21 b2b2/q22 b3b2/q23 b4b2/q24 b5b2/q25 b6b2/q26 b7b2/q27 |
  93. | b0b3/q30 b1b3/q31 b2b3/q32 b3b3/q33 b4b3/q34 b5b3/q35 b6b3/q36 b7b3/q37 |
  94. | b0b4/q40 b1b4/q41 b2b4/q42 b3b4/q43 b4b4/q44 b5b4/q45 b6b4/q46 b7b4/q47 |
  95. | b0b5/q50 b1b5/q51 b2b5/q52 b3b5/q53 b4b5/q54 b5b5/q55 b6b5/q56 b7b5/q57 |
  96. | b0b6/q60 b1b6/q61 b2b6/q62 b3b6/q63 b4b6/q64 b5b6/q65 b6b6/q66 b7b6/q67 |
  97. | b0b7/q70 b1b7/q71 b2b7/q72 b3b7/q73 b4b7/q74 b5b7/q75 b6b7/q76 b7b7/q77 |
  98. Note that q00,q01,...,q60,..,q77 is the quantization matrix specified
  99. during the compression stage.
  100. Note that in the above scaling matrix description,
  101. bibj = bjbi, and bibj implies multiplying bi with bj.
  102. The scaling matrix can be precomputed at the start of the component
  103. scan (JPEG terminology).
  104. 4. The Y[] matrix values must be rounded to meet JPEG specifications,
  105. i.e. if X[] is comprised of unsigned 8 bit values, Y[] must be adjusted
  106. to signed 12 bit values. (i.e. numbers in the range -2047, 2047).
  107. */
  108. /* ------------------------ _iljpgEnDCTScale ------------------------------- */
  109. /* Called by iljpgEncodeInit() to scale the Q table pointed to by "pSrc"
  110. into "pDst", each a 64 entry table.
  111. */
  112. ILJPG_PRIVATE iljpgError _iljpgEnDCTScale (
  113. iljpgPtr pSrc,
  114. float *pDst
  115. )
  116. {
  117. int i, j, k, temp;
  118. double a, c0, b[8], pi;
  119. float *sptr;
  120. iljpgPtr qptr;
  121. int *cptr;
  122. pi = 4.0 * atan(1.0);
  123. c0 = 1.0 / (2.0 * 0.707106718);
  124. a = 2 * c0;
  125. b[0] = a / 4.0;
  126. for (i = 1; i < 8; i++) {
  127. a = 2 / cos (i * pi / 16.0);
  128. b[i] = a / 8.0;
  129. }
  130. /* scale and quantize dct output after computing 2-D DCT.
  131. The Q table is in zigzag order: de-zigzag while accessing.
  132. */
  133. sptr = pDst;
  134. qptr = pSrc;
  135. for (i = 0, k = 0; i < 8; i++) {
  136. for (j = 0; j < 8; j++) {
  137. temp = qptr[_iljpgZigzagTable[k++]];
  138. if (temp == 0)
  139. return ILJPG_ERROR_ENCODE_Q_TABLE;
  140. *sptr++ = b[i] * b[j] / (float)temp;
  141. }
  142. }
  143. #ifdef DEBUG
  144. fprintf(fp_dbg,"\nforward dct routine.. post-scaling matrix\n");
  145. for (i = 0; i < 64; i++) {
  146. if ((i % 8) == 0) fprintf(fp_dbg,"\n");
  147. fprintf(fp_dbg,"%8.4f ",s[i]);
  148. }
  149. #endif
  150. return 0;
  151. }
  152. /* ------------------------ fwfwddct_8x8 ------------------------------------- */
  153. static void fwfwddct_8x8 (
  154. int *ix, /* pointer to 8x8 input array whose DCT is to be computed */
  155. float *ox /* pointer to 8x8 output array containing DCT coefficients */
  156. )
  157. {
  158. float in0, in1, in2, in3, in4, in5, in6, in7;
  159. float tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  160. float tmp;
  161. int *ixaddr;
  162. float *oxaddr;
  163. int i;
  164. int j;
  165. /* constants needed by the 16 point Winograd Fourier Transform method */
  166. float a1 = 0.707106718;
  167. float a2 = -0.541196100;
  168. float a3 = 0.707106718;
  169. float a4 = 1.306562963;
  170. float a5 = 0.382683432;
  171. #ifdef DEBUG
  172. ixaddr = ix;
  173. fprintf(fp_dbg,"\nforward dct routine.. before row dct\n");
  174. for (j = 0; j < 64; j++, ixaddr++) {
  175. if ((j % 8) == 0) fprintf(fp_dbg,"\n");
  176. fprintf(fp_dbg,"%8.4f ",(float)(*ixaddr));
  177. }
  178. #endif
  179. ixaddr = ix;
  180. oxaddr = ox;
  181. for (i = 0; i < 8; i++) {
  182. /* setup input data - 8 bit range */
  183. in0 = *ixaddr;
  184. in1 = *(ixaddr+1);
  185. in2 = *(ixaddr+2);
  186. in3 = *(ixaddr+3);
  187. in4 = *(ixaddr+4);
  188. in5 = *(ixaddr+5);
  189. in6 = *(ixaddr+6);
  190. in7 = *(ixaddr+7);
  191. #ifdef DEBUG
  192. fprintf(fp_dbg,"\nstart: %8.4f %8.4f %8.4f %8.4f\n", in0, in1, in2, in3);
  193. fprintf(fp_dbg," %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, in7);
  194. #endif
  195. /* Stage 0 - 9bits max, 1bit sign */
  196. tmp0 = in0 + in7;
  197. tmp1 = in1 + in6;
  198. tmp2 = in5 + in2;
  199. tmp3 = in3 + in4;
  200. tmp4 = in3 - in4;
  201. tmp5 = in2 - in5;
  202. tmp6 = in1 - in6;
  203. tmp7 = in0 - in7;
  204. #ifdef DEBUG
  205. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n", tmp0, tmp1, tmp2, tmp3);
  206. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n", tmp4, tmp5, tmp6, tmp7);
  207. #endif
  208. /* Stage 1 - 10bits max, 1 bit sign */
  209. in0 = tmp0 + tmp3;
  210. in1 = tmp1 + tmp2;
  211. in2 = tmp1 - tmp2;
  212. in3 = tmp0 - tmp3;
  213. in4 = - tmp4 - tmp5;
  214. in5 = tmp5 + tmp6;
  215. in6 = tmp6 + tmp7;
  216. #ifdef DEBUG
  217. fprintf(fp_dbg,"s1: %8.4f %8.4f %8.4f %8.4f\n", in0, in1, in2, in3);
  218. fprintf(fp_dbg,"s1: %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, tmp7);
  219. #endif
  220. /* Stage 2 - 11bits max, 1 bit sign */
  221. *oxaddr = in0 + in1; /* y0 */
  222. *(oxaddr+4) = in0 - in1; /* y4 */
  223. in2 = in2 + in3;
  224. #ifdef DEBUG
  225. fprintf(fp_dbg,"s2: %8.4f %8.4f %8.4f %8.4f\n",
  226. *oxaddr, *(oxaddr+4), in2, in3);
  227. fprintf(fp_dbg,"s2: %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, in7);
  228. #endif
  229. /* Stage 2.1 - 12bit for partial add, 1bit sign */
  230. tmp = (in6 + in4);
  231. tmp = a5 * tmp;
  232. /* Stage 3 - 13 bit, 1 bit sign */
  233. tmp2 = a1 * in2;
  234. tmp4 = a2 * in4 - tmp;
  235. tmp5 = a3 * in5;
  236. tmp6 = a4 * in6 - tmp;
  237. #ifdef DEBUG
  238. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n",
  239. *oxaddr, *(oxaddr+4), tmp2, in3);
  240. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n", tmp4, tmp5, tmp6, tmp7);
  241. #endif
  242. /* Stage 4 */
  243. *(oxaddr+2) = tmp2 + in3; /* y2 */
  244. *(oxaddr+6) = in3 - tmp2; /* y6 */
  245. in5 = tmp5 + tmp7;
  246. in7 = tmp7 - tmp5;
  247. #ifdef DEBUG
  248. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n",
  249. *oxaddr, *(oxaddr+4), *(oxaddr+2), *(oxaddr+6));
  250. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n",
  251. tmp4, in5, tmp6, in7);
  252. #endif
  253. /* Stage 5 */
  254. *(oxaddr+5) = tmp4 + in7; /* y5 */
  255. *(oxaddr+1) = in5 + tmp6; /* y1 */
  256. *(oxaddr+7) = in5 - tmp6; /* y7 */
  257. *(oxaddr+3) = in7 - tmp4; /* y3 */
  258. #ifdef DEBUG
  259. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n",
  260. *oxaddr, *(oxaddr+4), *(oxaddr+2), *(oxaddr+6));
  261. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n",
  262. *(oxaddr+5), *(oxaddr+1), *(oxaddr+7), *(oxaddr+3));
  263. #endif
  264. oxaddr += 8; /* pointer to next row of output */
  265. ixaddr += 8; /* pointer to next row of input */
  266. }
  267. #ifdef DEBUG
  268. fprintf(fp_dbg,"\nforward dct routine.. after row dct\n");
  269. oxaddr = ox;
  270. for (j = 0; j < 64; j++, oxaddr++) {
  271. if ((j % 8) == 0) fprintf(fp_dbg,"\n");
  272. fprintf(fp_dbg,"%8.4f ",*oxaddr);
  273. }
  274. #endif
  275. oxaddr = ox;
  276. for (i = 0; i < 8; i++) { /* perform 1-D DCT along columns */
  277. /* setup input data - 8 bit range */
  278. in0 = *oxaddr;
  279. in1 = *(oxaddr+8);
  280. in2 = *(oxaddr+16);
  281. in3 = *(oxaddr+24);
  282. in4 = *(oxaddr+32);
  283. in5 = *(oxaddr+40);
  284. in6 = *(oxaddr+48);
  285. in7 = *(oxaddr+56);
  286. #ifdef DEBUG
  287. fprintf(fp_dbg,"\nstart: %8.4f %8.4f %8.4f %8.4f\n", in0, in1, in2, in3);
  288. fprintf(fp_dbg,"\nstart: %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, in7);
  289. #endif
  290. /* Stage 0 - 9bits max, 1bit sign */
  291. tmp0 = in0 + in7;
  292. tmp1 = in1 + in6;
  293. tmp2 = in5 + in2;
  294. tmp3 = in3 + in4;
  295. tmp4 = in3 - in4;
  296. tmp5 = in2 - in5;
  297. tmp6 = in1 - in6;
  298. tmp7 = in0 - in7;
  299. #ifdef DEBUG
  300. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n", tmp0, tmp1, tmp2, tmp3);
  301. fprintf(fp_dbg,"s0: %8.4f %8.4f %8.4f %8.4f\n", tmp4, tmp5, tmp6, tmp7);
  302. #endif
  303. /* Stage 1 - 10bits max, 1 bit sign */
  304. in0 = tmp0 + tmp3;
  305. in1 = tmp1 + tmp2;
  306. in2 = tmp1 - tmp2;
  307. in3 = tmp0 - tmp3;
  308. in4 = - tmp4 - tmp5;
  309. in5 = tmp5 + tmp6;
  310. in6 = tmp6 + tmp7;
  311. #ifdef DEBUG
  312. fprintf(fp_dbg,"s1: %8.4f %8.4f %8.4f %8.4f\n", in0, in1, in2, in3);
  313. fprintf(fp_dbg,"s1: %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, tmp7);
  314. #endif
  315. /* Stage 2 - 11bits max, 1 bit sign */
  316. *oxaddr = in0 + in1; /* y0 */
  317. *(oxaddr+32) = in0 - in1; /* y4 */
  318. in2 += in3;
  319. #ifdef DEBUG
  320. fprintf(fp_dbg,"s2: %8.4f %8.4f %8.4f %8.4f\n", *oxaddr, *(oxaddr+32),
  321. in2, in3);
  322. fprintf(fp_dbg,"s2: %8.4f %8.4f %8.4f %8.4f\n", in4, in5, in6, tmp7);
  323. #endif
  324. /* Stage 2.1 - 12bit for partial add, 1bit sign */
  325. tmp = (in6 + in4);
  326. tmp = a5 * tmp;
  327. /* Stage 3 - 13 bit, 1 bit sign */
  328. tmp2 = a1 * in2;
  329. tmp4 = a2 * in4 - tmp;
  330. tmp5 = a3 * in5;
  331. tmp6 = a4 * in6 - tmp;
  332. #ifdef DEBUG
  333. fprintf(fp_dbg,"s3: %8.4f %8.4f %8.4f %8.4f\n",
  334. *oxaddr, *(oxaddr+32), tmp2, in3);
  335. fprintf(fp_dbg,"s3: %8.4f %8.4f %8.4f %8.4f\n",
  336. tmp4, tmp5, tmp6, tmp7);
  337. #endif
  338. /* Stage 4 */
  339. *(oxaddr+16) = tmp2 + in3; /* y2 */
  340. *(oxaddr+48) = in3 - tmp2; /* y6 */
  341. in5 = tmp5 + tmp7;
  342. in7 = tmp7 - tmp5;
  343. #ifdef DEBUG
  344. fprintf(fp_dbg,"s4: %8.4f %8.4f %8.4f %8.4f\n",
  345. *oxaddr, *(oxaddr+32), *(oxaddr+16), *(oxaddr+48));
  346. fprintf(fp_dbg,"s4: %8.4f %8.4f %8.4f %8.4f\n",
  347. tmp4, in5,tmp6,in7);
  348. #endif
  349. /* Stage 5 */
  350. *(oxaddr+40) = tmp4 + in7; /* y5 */
  351. *(oxaddr+8) = in5 + tmp6; /* y1 */
  352. *(oxaddr+56) = in5 - tmp6; /* y7 */
  353. *(oxaddr+24) = in7 - tmp4; /* y3 */
  354. #ifdef DEBUG
  355. fprintf(fp_dbg,"s5: %8.4f %8.4f %8.4f %8.4f\n",
  356. *oxaddr, *(oxaddr+32), *(oxaddr+16), *(oxaddr+48));
  357. fprintf(fp_dbg,"s5: %8.4f %8.4f %8.4f %8.4f\n",
  358. *(oxaddr+40), *(oxaddr+8), *(oxaddr+56), *(oxaddr+24));
  359. #endif
  360. oxaddr += 1; /* pointer to next column */
  361. }
  362. #ifdef DEBUG
  363. fprintf(fp_dbg,"\nforward dct routine.. after column dct\n");
  364. oxaddr = ox;
  365. for (j = 0; j < 64; j++, oxaddr++) {
  366. if ((j % 8) == 0) fprintf(fp_dbg,"\n");
  367. fprintf(fp_dbg,"%8.4f ",*oxaddr);
  368. }
  369. #endif
  370. }
  371. /* -------------------------------- _iljpgEnDCT ------------------------------- */
  372. /* DCT encode the 64 int matrix pointed to by "pSrc", storing the results back
  373. into the same matrix. "pScale" must point to the 64 float scaled/clipped
  374. Q table, as setup by _iljpgEnDCTInit().
  375. */
  376. ILJPG_PRIVATE _iljpgEnDCT (
  377. int *pSrc,
  378. float *pScale
  379. )
  380. {
  381. int i, value;
  382. float *sptr;
  383. int *ixptr;
  384. float *oxptr;
  385. int *zptr;
  386. float fvalue;
  387. float ox[64]; /* DCT coefficient buffer */
  388. fwfwddct_8x8(pSrc,ox); /* perform 1-D DCT along rows, then columns */
  389. /* scale dct and quantize dct values */
  390. oxptr = ox;
  391. sptr = pScale;
  392. ixptr = pSrc;
  393. *oxptr -= 8192; /* for JPEG shift by -128 */
  394. /* scale and quantize DC value. JPEG expects rounding during this process */
  395. /* clip quantized values to the range -2047 to 2047 */
  396. fvalue = *oxptr++ * *sptr++;
  397. if (fvalue < 0.0) fvalue -= 0.5;
  398. else fvalue += 0.5;
  399. value = (int)fvalue;
  400. if (value < -2047) value = -2047;
  401. else if (value > 2047) value = 2047;
  402. *ixptr = value;
  403. /* scale and quantize AC values. JPEG expects rounding during this process */
  404. /* clip quantized values to the range -1023 to 1023 */
  405. zptr = &_iljpgZigzagTable[1]; /* zigzag addressing included */
  406. for (i = 1; i < 64; i++) {
  407. fvalue = *oxptr++ * *sptr++;
  408. if (fvalue < 0.0) fvalue -= 0.5;
  409. else fvalue += 0.5;
  410. value = (int)fvalue;
  411. if (value < -1023) value = -1023;
  412. else if (value > 1023) value = 1023;
  413. *(ixptr + *zptr++) = value;
  414. }
  415. #ifdef DEBUG
  416. fprintf(fp_dbg,"\nforward dct routine.. after scaling\n");
  417. ixptr = pSrc;
  418. for (i = 0; i < 64; i++, ixptr++) {
  419. if ((i % 8) == 0) fprintf(fp_dbg,"\n");
  420. fprintf(fp_dbg,"%8.4f ",(float)(*ixptr));
  421. }
  422. #endif
  423. }