2 * Copyright (C) 2001, Novell Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Novell nor the names of the contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
35 * a tiny jpeg decoder.
37 * written in August 2001 by Michael Schroeder <mls@suse.de>
41 #define __LITTLE_ENDIAN
46 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
47 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
48 #define ITOINT(a) ((a) >> ISHIFT)
64 int (*func) __P((void *));
68 /*********************************/
73 struct dec_hufftbl *dhuff;
74 struct enc_hufftbl *ehuff;
78 int dc; /* old dc value */
82 int next; /* when to switch to next scan */
84 int cid; /* component id */
85 int hv; /* horiz/vert, copied from comp */
86 int tq; /* quant tbl, copied from comp */
89 /*********************************/
91 #define DECBITS 10 /* seems to be the optimum */
96 unsigned char vals[256];
97 unsigned int llvals[1 << DECBITS];
100 static void decode_mcus
101 __P((struct in *, int *, int, struct scan *, int *));
102 static int dec_readmarker __P((struct in *));
103 static void dec_makehuff
104 __P((struct dec_hufftbl *, int *, unsigned char *));
106 static void setinput __P((struct in *, unsigned char *));
107 /*********************************/
112 static void idctqtab __P((unsigned char *, PREC *));
113 static void idct __P((int *, int *, PREC *, PREC, int));
114 static void scaleidctqtab __P((PREC *, PREC));
116 /*********************************/
118 static void initcol __P((PREC[][64]));
120 static void col221111 __P((int *, unsigned char *, int));
121 static void col221111_16 __P((int *, unsigned char *, int));
122 static void col221111_32 __P((int *, unsigned char *, int));
124 /*********************************/
137 static unsigned char *datap;
139 static int getbyte(void)
144 static int getword(void)
160 int nc; /* number of components */
161 int ns; /* number of scans */
162 int dri; /* restart interval */
163 int nm; /* mcus til next marker */
164 int rm; /* next restart marker */
167 static struct jpginfo info;
168 static struct comp comps[MAXCOMP];
170 static struct scan dscans[MAXCOMP];
172 static unsigned char quant[4][64];
174 static struct dec_hufftbl dhuff[4];
176 #define dec_huffdc (dhuff + 0)
177 #define dec_huffac (dhuff + 2)
181 static int readtables(int till)
183 int m, l, i, j, lq, pq, tq;
187 if (getbyte() != 0xff)
189 if ((m = getbyte()) == till)
206 for (i = 0; i < 64; i++)
207 quant[tq][i] = getbyte();
216 unsigned char huffvals[256];
222 if (tc > 1 || th > 1)
224 for (i = 0; i < 16; i++)
225 hufflen[i] = getbyte();
228 for (i = 0; i < 16; i++) {
229 for (j = 0; j < hufflen[i]; j++)
230 huffvals[k++] = getbyte();
233 dec_makehuff(dhuff + tt, hufflen, huffvals);
239 info.dri = getword();
252 static void dec_initscans(void)
256 info.nm = info.dri + 1;
258 for (i = 0; i < info.ns; i++)
262 static int dec_checkmarker(void)
266 if (dec_readmarker(&in) != info.rm)
269 info.rm = (info.rm + 1) & ~0x08;
270 for (i = 0; i < info.ns; i++)
275 int jpeg_check_size(unsigned char *buf, int width, int height)
283 if (height != getword() || width != getword())
288 int jpeg_decode(unsigned char *buf, unsigned char *pic,
289 int width, int height, int depth,
290 struct jpeg_decdata *decdata)
292 int i, j, m, tac, tdc;
293 int mcusx, mcusy, mx, my;
296 if (!decdata || !buf || !pic)
299 if (getbyte() != 0xff)
301 if (getbyte() != M_SOI)
303 if (readtables(M_SOF0))
304 return ERR_BAD_TABLES;
309 if (((getword() + 15) & ~15) != height)
310 return ERR_HEIGHT_MISMATCH;
311 if (((getword() + 15) & ~15) != width)
312 return ERR_WIDTH_MISMATCH;
313 if ((height & 15) || (width & 15))
314 return ERR_BAD_WIDTH_OR_HEIGHT;
316 if (info.nc > MAXCOMP)
317 return ERR_TOO_MANY_COMPPS;
318 for (i = 0; i < info.nc; i++) {
320 comps[i].cid = getbyte();
321 comps[i].hv = getbyte();
322 v = comps[i].hv & 15;
323 h = comps[i].hv >> 4;
324 comps[i].tq = getbyte();
326 return ERR_ILLEGAL_HV;
328 return ERR_QUANT_TABLE_SELECTOR;
330 if (readtables(M_SOS))
331 return ERR_BAD_TABLES;
335 return ERR_NOT_YCBCR_221111;
336 for (i = 0; i < 3; i++) {
337 dscans[i].cid = getbyte();
341 if (tdc > 1 || tac > 1)
342 return ERR_QUANT_TABLE_SELECTOR;
343 for (j = 0; j < info.nc; j++)
344 if (comps[j].cid == dscans[i].cid)
347 return ERR_UNKNOWN_CID_IN_SCAN;
348 dscans[i].hv = comps[j].hv;
349 dscans[i].tq = comps[j].tq;
350 dscans[i].hudc.dhuff = dec_huffdc + tdc;
351 dscans[i].huac.dhuff = dec_huffac + tac;
358 if (i != 0 || j != 63 || m != 0)
359 return ERR_NOT_SEQUENTIAL_DCT;
361 if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
362 return ERR_NOT_YCBCR_221111;
364 if (dscans[0].hv != 0x22 || dscans[1].hv != 0x11
365 || dscans[2].hv != 0x11)
366 return ERR_NOT_YCBCR_221111;
372 idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
373 idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
374 idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
375 initcol(decdata->dquant);
376 setinput(&in, datap);
382 img[len + 2] = M_EOF;
387 dscans[0].next = 6 - 4;
388 dscans[1].next = 6 - 4 - 1;
389 dscans[2].next = 6 - 4 - 1 - 1; /* 411 encoding */
390 for (my = 0; my < mcusy; my++) {
391 for (mx = 0; mx < mcusx; mx++) {
392 if (info.dri && !--info.nm)
393 if (dec_checkmarker())
394 return ERR_WRONG_MARKER;
396 decode_mcus(&in, decdata->dcts, 6, dscans, max);
397 idct(decdata->dcts, decdata->out, decdata->dquant[0],
398 IFIX(128.5), max[0]);
399 idct(decdata->dcts + 64, decdata->out + 64, decdata->dquant[0],
400 IFIX(128.5), max[1]);
401 idct(decdata->dcts + 128, decdata->out + 128,
402 decdata->dquant[0], IFIX(128.5), max[2]);
403 idct(decdata->dcts + 192, decdata->out + 192,
404 decdata->dquant[0], IFIX(128.5), max[3]);
405 idct(decdata->dcts + 256, decdata->out + 256,
406 decdata->dquant[1], IFIX(0.5), max[4]);
407 idct(decdata->dcts + 320, decdata->out + 320,
408 decdata->dquant[2], IFIX(0.5), max[5]);
412 col221111_32(decdata->out,
413 pic + (my * 16 * mcusx + mx) * 16 * 4,
417 col221111(decdata->out,
418 pic + (my * 16 * mcusx + mx) * 16 * 3,
422 col221111_16(decdata->out,
423 pic + (my * 16 * mcusx + mx) * (16 * 2),
427 return ERR_DEPTH_MISMATCH;
433 m = dec_readmarker(&in);
440 /****************************************************************/
441 /************** huffman decoder ***************/
442 /****************************************************************/
444 static int fillbits __P((struct in *, int, unsigned int));
446 __P((struct in *, struct dec_hufftbl *, int *, int, int));
448 static void setinput(struct in *in, unsigned char *p)
456 static int fillbits(struct in *in, int le, unsigned int bi)
462 in->bits = bi << 16, le += 16;
467 if (b == 0xff && (m = *in->p++) != 0) {
469 if (in->func && (m = in->func(in->data)) == 0)
474 bi = bi << 16, le += 16;
480 in->bits = bi; /* tmp... 2 return values needed */
484 static int dec_readmarker(struct in *in)
488 in->left = fillbits(in, in->left, in->bits);
489 if ((m = in->marker) == 0)
496 #define LEBI_DCL int le, bi
497 #define LEBI_GET(in) (le = in->left, bi = in->bits)
498 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
500 #define GETBITS(in, n) ( \
501 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
503 bi >> le & ((1 << (n)) - 1) \
506 #define UNGETBITS(in, n) ( \
511 static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp,
518 UNGETBITS(in, i & 127);
523 (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++);
525 in->marker = M_BADHUFF;
528 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
532 if (i == 0) { /* sigh, 0xf0 is 11 bit */
538 if (c < (1 << (i - 1)))
544 #define DEC_REC(in, hu, r, i) ( \
545 r = GETBITS(in, DECBITS), \
549 UNGETBITS(in, i & 127), \
556 i = dec_rec2(in, hu, &r, r, i), \
562 static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc,
565 struct dec_hufftbl *hu;
569 memset(dct, 0, n * 64 * sizeof(*dct));
573 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
578 t = DEC_REC(in, hu, r, t);
579 if (t == 0 && r == 0) {
594 static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen,
595 unsigned char *huffvals)
597 int code, k, i, j, d, x, c, v;
598 for (i = 0; i < (1 << DECBITS); i++)
604 * value v already known, run r, backup u bits:
605 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
606 * value unknown, size b bits, run r, backup u bits:
607 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
608 * value and size unknown:
609 * 0000000000000000 0000 0000 0 0000000
614 for (i = 0; i < 16; i++, code <<= 1) { /* sizes */
616 for (j = 0; j < hufflen[i]; j++) {
617 hu->vals[k] = *huffvals++;
619 c = code << (DECBITS - 1 - i);
620 v = hu->vals[k] & 0x0f; /* size */
621 for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
622 if (v + i < DECBITS) { /* both fit in table */
623 x = d >> (DECBITS - 1 - v - i);
624 if (v && x < (1 << (v - 1)))
626 x = x << 16 | (hu->vals[k] & 0xf0) << 4 |
627 (DECBITS - (i + 1 + v)) | 128;
629 x = v << 16 | (hu->vals[k] & 0xf0) << 4 |
631 hu->llvals[c | d] = x;
637 hu->maxcode[i] = code;
639 hu->maxcode[16] = 0x20000; /* always terminate decode */
642 /****************************************************************/
643 /************** idct ***************/
644 /****************************************************************/
646 #define ONE ((PREC)IFIX(1.))
647 #define S2 ((PREC)IFIX(0.382683432))
648 #define C2 ((PREC)IFIX(0.923879532))
649 #define C4 ((PREC)IFIX(0.707106781))
651 #define S22 ((PREC)IFIX(2 * 0.382683432))
652 #define C22 ((PREC)IFIX(2 * 0.923879532))
653 #define IC4 ((PREC)IFIX(1 / 0.707106781))
655 #define C3IC1 ((PREC)IFIX(0.847759065)) /* c3/c1 */
656 #define C5IC1 ((PREC)IFIX(0.566454497)) /* c5/c1 */
657 #define C7IC1 ((PREC)IFIX(0.198912367)) /* c7/c1 */
659 #define XPP(a,b) (t = a + b, b = a - b, a = t)
660 #define XMP(a,b) (t = a - b, b = a + b, a = t)
661 #define XPM(a,b) (t = a + b, b = b - a, a = t)
663 #define ROT(a,b,s,c) ( t = IMULT(a + b, s), \
664 a = IMULT(a, c - s) + t, \
665 b = IMULT(b, c + s) - t)
671 t2 = IMULT(t2, IC4) - t3, \
677 t5 = IMULT(t5, IC4), \
678 ROT(t4, t6, S22, C22), \
688 static unsigned char zig2[64] = {
689 0, 2, 3, 9, 10, 20, 21, 35,
690 14, 16, 25, 31, 39, 46, 50, 57,
691 5, 7, 12, 18, 23, 33, 37, 48,
692 27, 29, 41, 44, 52, 55, 59, 62,
693 15, 26, 30, 40, 45, 51, 56, 58,
694 1, 4, 8, 11, 19, 22, 34, 36,
695 28, 42, 43, 53, 54, 60, 61, 63,
696 6, 13, 17, 24, 32, 38, 47, 49
699 void idct(int *in, int *out, PREC * quant, PREC off, int max)
701 PREC t0, t1, t2, t3, t4, t5, t6, t7, t;
704 unsigned char *zig2p;
708 t0 += in[0] * quant[0];
709 for (i = 0; i < 64; i++)
715 for (i = 0; i < 8; i++) {
717 t0 += in[j] * quant[j];
719 t5 = in[j] * quant[j];
721 t2 = in[j] * quant[j];
723 t7 = in[j] * quant[j];
725 t1 = in[j] * quant[j];
727 t4 = in[j] * quant[j];
729 t3 = in[j] * quant[j];
731 t6 = in[j] * quant[j];
744 for (i = 0; i < 8; i++) {
754 out[8 * i + 0] = ITOINT(t0);
755 out[8 * i + 1] = ITOINT(t1);
756 out[8 * i + 2] = ITOINT(t2);
757 out[8 * i + 3] = ITOINT(t3);
758 out[8 * i + 4] = ITOINT(t4);
759 out[8 * i + 5] = ITOINT(t5);
760 out[8 * i + 6] = ITOINT(t6);
761 out[8 * i + 7] = ITOINT(t7);
765 static unsigned char zig[64] = {
766 0, 1, 5, 6, 14, 15, 27, 28,
767 2, 4, 7, 13, 16, 26, 29, 42,
768 3, 8, 12, 17, 25, 30, 41, 43,
769 9, 11, 18, 24, 31, 40, 44, 53,
770 10, 19, 23, 32, 39, 45, 52, 54,
771 20, 22, 33, 38, 46, 51, 55, 60,
772 21, 34, 37, 47, 50, 56, 59, 61,
773 35, 36, 48, 49, 57, 58, 62, 63
776 static PREC aaidct[8] = {
777 IFIX(0.3535533906), IFIX(0.4903926402),
778 IFIX(0.4619397663), IFIX(0.4157348062),
779 IFIX(0.3535533906), IFIX(0.2777851165),
780 IFIX(0.1913417162), IFIX(0.0975451610)
784 static void idctqtab(unsigned char *qin, PREC * qout)
788 for (i = 0; i < 8; i++)
789 for (j = 0; j < 8; j++)
790 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
791 IMULT(aaidct[i], aaidct[j]);
794 static void scaleidctqtab(PREC * q, PREC sc)
798 for (i = 0; i < 64; i++)
799 q[i] = IMULT(q[i], sc);
802 /****************************************************************/
803 /************** color decoder ***************/
804 /****************************************************************/
809 * YCbCr Color transformation:
811 * y:0..255 Cb:-128..127 Cr:-128..127
813 * R = Y + 1.40200 * Cr
814 * G = Y - 0.34414 * Cb - 0.71414 * Cr
815 * B = Y + 1.77200 * Cb
820 * Cg = 0.19421 * Cb + .50937 * Cr;
826 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
829 static void initcol(PREC q[][64])
831 scaleidctqtab(q[1], IFIX(1.77200));
832 scaleidctqtab(q[2], IFIX(1.40200));
835 /* This is optimized for the stupid sun SUNWspro compiler. */
836 #define STORECLAMP(a,x) \
839 (unsigned int)(x) >= 256 ? \
840 ((a) = (x) < 0 ? 0 : 255) \
845 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
849 #define CBCRCG(yin, xin) \
851 cb = outc[0 +yin*8+xin], \
852 cr = outc[64+yin*8+xin], \
853 cg = (50 * cb + 130 * cr + 128) >> 8 \
858 #define CBCRCG(yin, xin) \
860 cb = outc[0 +yin*8+xin], \
861 cr = outc[64+yin*8+xin], \
862 cg = (3 * cb + 8 * cr) >> 4 \
867 #define PIC(yin, xin, p, xout) \
869 y = outy[(yin) * 8 + xin], \
870 STORECLAMP(p[(xout) * 3 + 0], y + cr), \
871 STORECLAMP(p[(xout) * 3 + 1], y - cg), \
872 STORECLAMP(p[(xout) * 3 + 2], y + cb) \
875 #ifdef __LITTLE_ENDIAN
876 #define PIC_16(yin, xin, p, xout, add) \
878 y = outy[(yin) * 8 + xin], \
879 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
880 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
881 ((CLAMP(y + cb + add*2+1) ) >> 3), \
882 p[(xout) * 2 + 0] = y & 0xff, \
883 p[(xout) * 2 + 1] = y >> 8 \
887 #define PIC_16(yin, xin, p, xout, add) \
889 y = outy[(yin) * 8 + xin], \
890 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 7) | \
891 ((CLAMP(y - cg + add*2+1) & 0xf8) << 2) | \
892 ((CLAMP(y + cb + add*2+1) ) >> 3), \
893 p[(xout) * 2 + 0] = y >> 8, \
894 p[(xout) * 2 + 1] = y & 0xff \
897 #define PIC_16(yin, xin, p, xout, add) \
899 y = outy[(yin) * 8 + xin], \
900 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
901 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
902 ((CLAMP(y + cb + add*2+1) ) >> 3), \
903 p[(xout) * 2 + 0] = y >> 8, \
904 p[(xout) * 2 + 1] = y & 0xff \
909 #define PIC_32(yin, xin, p, xout) \
911 y = outy[(yin) * 8 + xin], \
912 STORECLAMP(p[(xout) * 4 + 0], y + cr), \
913 STORECLAMP(p[(xout) * 4 + 1], y - cg), \
914 STORECLAMP(p[(xout) * 4 + 2], y + cb), \
915 p[(xout) * 4 + 3] = 0 \
918 #define PIC221111(xin) \
921 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
922 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
923 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
924 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
927 #define PIC221111_16(xin) \
930 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
931 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
932 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
933 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2) \
936 #define PIC221111_32(xin) \
939 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
940 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
941 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
942 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
945 static void col221111(int *out, unsigned char *pic, int width)
948 unsigned char *pic0, *pic1;
956 for (i = 2; i > 0; i--) {
957 for (j = 4; j > 0; j--) {
958 for (k = 0; k < 8; k++) {
966 outy += 64 * 2 - 16 * 4;
970 static void col221111_16(int *out, unsigned char *pic, int width)
973 unsigned char *pic0, *pic1;
981 for (i = 2; i > 0; i--) {
982 for (j = 4; j > 0; j--) {
983 for (k = 0; k < 8; k++) {
991 outy += 64 * 2 - 16 * 4;
995 static void col221111_32(int *out, unsigned char *pic, int width)
998 unsigned char *pic0, *pic1;
1005 outc = out + 64 * 4;
1006 for (i = 2; i > 0; i--) {
1007 for (j = 4; j > 0; j--) {
1008 for (k = 0; k < 8; k++) {
1016 outy += 64 * 2 - 16 * 4;