8 /* Constants, all preceded by byte 0xFF */
9 SOF =0xC0, /* Start of Frame */
10 SOF2 =0xC2, /* Start of Frame; progressive Huffman */
11 JPG =0xC8, /* Reserved for JPEG extensions */
12 DHT =0xC4, /* Define Huffman Tables */
13 DAC =0xCC, /* Arithmetic coding conditioning */
14 RST =0xD0, /* Restart interval termination */
15 RST7 =0xD7, /* Restart interval termination (highest value) */
16 SOI =0xD8, /* Start of Image */
17 EOI =0xD9, /* End of Image */
18 SOS =0xDA, /* Start of Scan */
19 DQT =0xDB, /* Define quantization tables */
20 DNL =0xDC, /* Define number of lines */
21 DRI =0xDD, /* Define restart interval */
22 DHP =0xDE, /* Define hierarchical progression */
23 EXP =0xDF, /* Expand reference components */
24 APPn =0xE0, /* Reserved for application segments */
25 JPGn =0xF0, /* Reserved for JPEG extensions */
26 COM =0xFE, /* Comment */
32 typedef struct Framecomp Framecomp;
33 typedef struct Header Header;
34 typedef struct Huffman Huffman;
36 struct Framecomp /* Frame component specifier from SOF marker */
46 int *size; /* malloc'ed */
47 int *code; /* malloc'ed */
48 int *val; /* malloc'ed */
63 /* variables in i/o routines */
64 int sr; /* shift register, right aligned */
65 int cnt; /* # bits in right part of sr */
76 int qt[4][64]; /* quantization tables */
82 uchar *sf; /* start of frame; do better later */
83 uchar *ss; /* start of scan; do better later */
84 int ri; /* restart interval */
86 /* progressive scan */
90 int **accoeff[3]; /* only need 8 bits plus quantization */
99 static uchar clamp[NCLAMP];
101 static Rawimage *readslave(Header*, int);
102 static int readsegment(Header*, int*);
103 static void quanttables(Header*, uchar*, int);
104 static void huffmantables(Header*, uchar*, int);
105 static void soiheader(Header*);
106 static int nextbyte(Header*, int);
107 static int int2(uchar*, int);
108 static void nibbles(int, int*, int*);
109 static int receive(Header*, int);
110 static int receiveEOB(Header*, int);
111 static int receivebit(Header*);
112 static void restart(Header*, int);
113 static int decode(Header*, Huffman*);
114 static Rawimage* baselinescan(Header*, int);
115 static void progressivescan(Header*, int);
116 static Rawimage* progressiveIDCT(Header*, int);
117 static void idct(int*);
118 static void colormap1(Header*, int, Rawimage*, int*, int, int);
119 static void colormapall1(Header*, int, Rawimage*, int*, int*, int*, int, int);
120 static void colormap(Header*, int, Rawimage*, int**, int**, int**, int, int, int, int, int*, int*);
121 static void jpgerror(Header*, char*, ...);
123 static char readerr[] = "ReadJPG: read error: %r";
124 static char memerr[] = "ReadJPG: malloc failed: %r";
126 static int zig[64] = {
127 0, 1, 8, 16, 9, 2, 3, 10, 17, /* 0-7 */
128 24, 32, 25, 18, 11, 4, 5, /* 8-15 */
129 12, 19, 26, 33, 40, 48, 41, 34, /* 16-23 */
130 27, 20, 13, 6, 7, 14, 21, 28, /* 24-31 */
131 35, 42, 49, 56, 57, 50, 43, 36, /* 32-39 */
132 29, 22, 15, 23, 30, 37, 44, 51, /* 40-47 */
133 58, 59, 52, 45, 38, 31, 39, 46, /* 48-55 */
134 53, 60, 61, 54, 47, 55, 62, 63 /* 56-63 */
147 for(k=0; k<CLAMPOFF; k++)
149 for(; k<CLAMPOFF+256; k++)
150 clamp[k] = k-CLAMPOFF;
157 jpgmalloc(Header *h, int n, int clear)
181 jpgfreeall(Header *h, int freeimage)
188 clear(&h->dccoeff[i]);
192 for(j=0; j<h->naccoeff[i]; j++)
193 clear(&h->accoeff[i][j]);
194 clear(&h->accoeff[i]);
197 clear(&h->dcht[i].size);
198 clear(&h->acht[i].size);
199 clear(&h->dcht[i].code);
200 clear(&h->acht[i].code);
201 clear(&h->dcht[i].val);
202 clear(&h->acht[i].val);
207 for(j=0; j<h->ndata[i]; j++)
208 clear(&h->data[i][j]);
211 if(freeimage && h->image!=nil){
213 clear(&h->image->cmap);
215 clear(&h->image->chans[i]);
222 jpgerror(Header *h, char *fmt, ...)
227 vseprint(h->err, h->err+sizeof h->err, fmt, arg);
232 longjmp(h->errlab, 1);
236 Breadjpg(Biobuf *b, int colorspace)
238 Rawimage *r, **array;
243 if(colorspace!=CYCbCr && colorspace!=CRGB){
244 errstr(buf, sizeof buf); /* throw it away */
245 werrstr("ReadJPG: unknown color space");
249 h = malloc(sizeof(Header));
250 array = malloc(2*sizeof(Rawimage*));
251 if(h==nil || array==nil){
257 memset(h, 0, sizeof(Header));
259 errstr(buf, sizeof buf); /* throw it away */
260 if(setjmp(h->errlab))
263 r = readslave(h, colorspace);
272 readjpg(int fd, int colorspace)
277 if(Binit(&b, fd, OREAD) < 0)
279 a = Breadjpg(&b, colorspace);
286 readslave(Header *header, int colorspace)
289 int nseg, i, H, V, m, n;
296 header->buf = jpgmalloc(header, 4096, 0);
298 while(header->err[0] == '\0'){
300 n = readsegment(header, &m);
307 if(nseg==1 && strncmp((char*)b, "JFIF", 4)==0) /* JFIF header; check version */
309 sprint(header->err, "ReadJPG: can't handle JFIF version %d.%2d", b[5], b[6]);
312 case APPn+1: case APPn+2: case APPn+3: case APPn+4: case APPn+5:
313 case APPn+6: case APPn+7: case APPn+8: case APPn+9: case APPn+10:
314 case APPn+11: case APPn+12: case APPn+13: case APPn+14: case APPn+15:
318 quanttables(header, b, n);
323 header->Y = int2(b, 1);
324 header->X = int2(b, 3);
326 for(i=0; i<header->Nf; i++){
327 header->comp[i].C = b[6+3*i+0];
328 nibbles(b[6+3*i+1], &H, &V);
330 jpgerror(header, "non-positive sampling factor (Hsamp or Vsamp)");
331 /* hack: colormap1() doesnt handle resampling */
334 header->comp[i].H = H;
335 header->comp[i].V = V;
336 header->comp[i].Tq = b[6+3*i+2];
344 switch(header->mode){
346 image = baselinescan(header, colorspace);
349 progressivescan(header, colorspace);
352 sprint(header->err, "unrecognized or unspecified encoding %d", header->mode);
358 huffmantables(header, b, n);
362 header->ri = int2(b, 0);
369 if(header->mode == SOF2)
370 image = progressiveIDCT(header, colorspace);
374 sprint(header->err, "ReadJPG: unknown marker %.2x", m);
381 /* readsegment is called after reading scan, which can have */
382 /* read ahead a byte. so we must check peek here */
392 }else if(Bread(h->fd, &x, 1) != 1)
393 jpgerror(h, readerr);
404 while((c=readbyte(h)) == 0)
417 int2(uchar *buf, int n)
419 return (buf[n]<<8) + buf[n+1];
424 nibbles(int b, int *p0, int *p1)
436 jpgerror(h, "ReadJPG: unrecognized marker in header");
444 readsegment(Header *h, int *markerp)
449 if((m = marker(h)) == EOI){
453 if(Bread(h->fd, tmp, 2) != 2)
455 jpgerror(h, readerr);
462 /* zero in case of short read later */
463 h->buf = jpgmalloc(h, n+1, 1); /* +1 for sentinel */
466 /* accept short reads to cope with some real-world jpegs */
467 if(Bread(h->fd, h->buf, n) < 0)
475 huffmantable(Header *h, uchar *b)
478 int Tc, th, n, nsize, i, j, k, v, cnt, code, si, sr;
481 nibbles(b[0], &Tc, &th);
483 jpgerror(h, "ReadJPG: unknown Huffman table class %d", Tc);
484 if(th>3 || (h->mode==SOF && th>1))
485 jpgerror(h, "ReadJPG: unknown Huffman table index %d", th);
497 t->size = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
499 for(i=1; i<=16; i++){
506 /* initialize HUFFVAL */
507 t->val = jpgmalloc(h, nsize*sizeof(int), 1);
508 for(i=0; i<nsize; i++)
512 t->code = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
518 t->code[k++] = code++;
519 while(t->size[k] == si);
525 }while(t->size[k] != si);
528 /* flow chart F-25 */
541 t->mincode[i] = t->code[j];
543 t->maxcode[i] = t->code[j];
548 /* create byte-indexed fast path tables */
549 maxcode = t->maxcode;
550 /* stupid startup algorithm: just run machine for each byte value */
559 if(code <= maxcode[i])
569 t->value[v] = t->val[t->valptr[i]+(code-t->mincode[i])];
580 huffmantables(Header *h, uchar *b, int n)
584 for(l=0; l<n; l+=17+mt)
585 mt = huffmantable(h, &b[l]);
590 quanttable(Header *h, uchar *b)
594 nibbles(b[0], &pq, &tq);
596 jpgerror(h, "ReadJPG: unknown quantization table class %d", pq);
598 jpgerror(h, "ReadJPG: unknown quantization table index %d", tq);
604 q[i] = int2(b, 1+2*i);
611 quanttables(Header *h, uchar *b, int n)
615 for(l=0; l<n; l+=1+m)
616 m = quanttable(h, &b[l]);
621 baselinescan(Header *h, int colorspace)
623 int Ns, z, k, m, Hmax, Vmax, comp;
624 int allHV1, nblock, ri, mcu, nacross, nmcu;
625 Huffman *dcht, *acht;
626 int block, t, diff, *qt;
629 int Td[3], Ta[3], H[3], V[3], DC[3];
634 if((Ns!=3 && Ns!=1) || Ns!=h->Nf)
635 jpgerror(h, "ReadJPG: can't handle scan not 3 components");
637 image = jpgmalloc(h, sizeof(Rawimage), 1);
639 image->r = Rect(0, 0, h->X, h->Y);
642 image->chanlen = h->X*h->Y;
646 image->giftrindex = 0;
648 image->chandesc = colorspace;
650 image->chandesc = CY;
651 image->nchans = h->Nf;
652 for(k=0; k<h->Nf; k++)
653 image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
655 /* compute maximum H and V */
658 for(comp=0; comp<Ns; comp++){
659 if(h->comp[comp].H > Hmax)
660 Hmax = h->comp[comp].H;
661 if(h->comp[comp].V > Vmax)
662 Vmax = h->comp[comp].V;
665 /* initialize data structures */
668 for(comp=0; comp<Ns; comp++){
669 /* JPEG requires scan components to be in same order as in frame, */
670 /* so if both have 3 we know scan is Y Cb Cr and there's no need to */
672 nibbles(ss[2+2*comp], &Td[comp], &Ta[comp]);
673 H[comp] = h->comp[comp].H;
674 V[comp] = h->comp[comp].V;
675 nblock = H[comp]*V[comp];
678 data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
679 h->ndata[comp] = nblock;
681 for(m=0; m<nblock; m++)
682 data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
690 nacross = ((h->X+(8*Hmax-1))/(8*Hmax));
691 nmcu = ((h->Y+(8*Vmax-1))/(8*Vmax))*nacross;
692 for(mcu=0; mcu<nmcu; ){
693 for(comp=0; comp<Ns; comp++){
694 dcht = &h->dcht[Td[comp]];
695 acht = &h->acht[Ta[comp]];
696 qt = h->qt[h->comp[comp].Tq];
698 for(block=0; block<H[comp]*V[comp]; block++){
701 diff = receive(h, t);
705 zz = data[comp][block];
706 memset(zz, 0, 8*8*sizeof(int));
707 zz[0] = qt[0]*DC[comp];
717 z = receive(h, t&0xF);
721 zz[zig[k]] = z*qt[k];
730 /* rotate colors to RGB and assign to bytes */
731 if(Ns == 1) /* very easy */
732 colormap1(h, colorspace, image, data[0][0], mcu, nacross);
733 else if(allHV1) /* fairly easy */
734 colormapall1(h, colorspace, image, data[0][0], data[1][0], data[2][0], mcu, nacross);
735 else /* miserable general case */
736 colormap(h, colorspace, image, data[0], data[1], data[2], mcu, nacross, Hmax, Vmax, H, V);
737 /* process restart marker, if present */
739 if(ri>0 && mcu<nmcu && mcu%ri==0){
741 for(comp=0; comp<Ns; comp++)
750 restart(Header *h, int mcu)
752 int rest, rst, nskip;
758 rst = nextbyte(h, 1);
760 }while(rst>=0 && rst!=0xFF);
762 rst = nextbyte(h, 1);
765 }while(rst>=0 && (rst&~7)!=RST);
767 sprint(h->err, "ReadJPG: skipped %d bytes at restart %d\n", nskip-2, rest);
769 jpgerror(h, readerr);
770 if((rst&7) != (rest&7))
771 jpgerror(h, "ReadJPG: expected RST%d got %d", rest&7, rst&7);
778 progressiveIDCT(Header *h, int colorspace)
780 int k, m, comp, block, Nf, bn;
781 int allHV1, nblock, mcu, nmcu;
782 int H[3], V[3], blockno[3];
783 int *dccoeff, **accoeff;
790 for(comp=0; comp<Nf; comp++){
791 H[comp] = h->comp[comp].H;
792 V[comp] = h->comp[comp].V;
793 nblock = h->nblock[comp];
796 h->ndata[comp] = nblock;
797 data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
798 for(m=0; m<nblock; m++)
799 data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
802 memset(blockno, 0, sizeof blockno);
803 nmcu = h->nacross*h->ndown;
804 for(mcu=0; mcu<nmcu; mcu++){
805 for(comp=0; comp<Nf; comp++){
806 dccoeff = h->dccoeff[comp];
807 accoeff = h->accoeff[comp];
809 for(block=0; block<h->nblock[comp]; block++){
810 zz = data[comp][block];
811 memset(zz, 0, 8*8*sizeof(int));
815 zz[zig[k]] = accoeff[bn][k];
823 /* rotate colors to RGB and assign to bytes */
824 if(Nf == 1) /* very easy */
825 colormap1(h, colorspace, h->image, data[0][0], mcu, h->nacross);
826 else if(allHV1) /* fairly easy */
827 colormapall1(h, colorspace, h->image, data[0][0], data[1][0], data[2][0], mcu, h->nacross);
828 else /* miserable general case */
829 colormap(h, colorspace, h->image, data[0], data[1], data[2], mcu, h->nacross, h->Hmax, h->Vmax, H, V);
837 progressiveinit(Header *h, int colorspace)
839 int Nf, Ns, j, k, nmcu, comp;
846 if((Ns!=3 && Ns!=1) || Ns!=Nf)
847 jpgerror(h, "ReadJPG: image must have 1 or 3 components");
849 image = jpgmalloc(h, sizeof(Rawimage), 1);
851 image->r = Rect(0, 0, h->X, h->Y);
854 image->chanlen = h->X*h->Y;
858 image->giftrindex = 0;
860 image->chandesc = colorspace;
862 image->chandesc = CY;
863 image->nchans = h->Nf;
865 image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
866 h->nblock[k] = h->comp[k].H*h->comp[k].V;
869 /* compute maximum H and V */
872 for(comp=0; comp<Nf; comp++){
873 if(h->comp[comp].H > h->Hmax)
874 h->Hmax = h->comp[comp].H;
875 if(h->comp[comp].V > h->Vmax)
876 h->Vmax = h->comp[comp].V;
878 h->nacross = ((h->X+(8*h->Hmax-1))/(8*h->Hmax));
879 h->ndown = ((h->Y+(8*h->Vmax-1))/(8*h->Vmax));
880 nmcu = h->nacross*h->ndown;
883 h->dccoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int), 1);
884 h->accoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int*), 1);
885 h->naccoeff[k] = h->nblock[k]*nmcu;
886 for(j=0; j<h->nblock[k]*nmcu; j++)
887 h->accoeff[k][j] = jpgmalloc(h, 64*sizeof(int), 1);
894 progressivedc(Header *h, int comp, int Ah, int Al)
896 int Ns, z, ri, mcu, nmcu;
897 int block, t, diff, qt, *dc, bn;
900 int Td[3], DC[3], blockno[3];
905 jpgerror(h, "ReadJPG: can't handle progressive with Nf!=Ns in DC scan");
907 /* initialize data structures */
911 for(comp=0; comp<Ns; comp++){
913 * JPEG requires scan components to be in same order as in frame,
914 * so if both have 3 we know scan is Y Cb Cr and there's no need to
917 nibbles(ss[2+2*comp], &Td[comp], &z); /* z is ignored */
923 nmcu = h->nacross*h->ndown;
924 memset(blockno, 0, sizeof blockno);
925 for(mcu=0; mcu<nmcu; ){
926 for(comp=0; comp<Ns; comp++){
927 dcht = &h->dcht[Td[comp]];
928 qt = h->qt[h->comp[comp].Tq][0];
929 dc = h->dccoeff[comp];
932 for(block=0; block<h->nblock[comp]; block++){
935 diff = receive(h, t);
937 dc[bn] = qt*DC[comp]<<Al;
939 dc[bn] |= qt*receivebit(h)<<Al;
945 /* process restart marker, if present */
947 if(ri>0 && mcu<nmcu && mcu%ri==0){
949 for(comp=0; comp<Ns; comp++)
957 progressiveac(Header *h, int comp, int Al)
959 int Ns, Ss, Se, z, k, eobrun, x, y, nver, tmcu, blockno, *acc, rs;
960 int ri, mcu, nacross, ndown, nmcu, nhor;
962 int *qt, rrrr, ssss, q;
969 jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
975 nacross = h->nacross*H;
982 /* initialize data structures */
986 nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
992 qt = h->qt[h->comp[comp].Tq];
993 nmcu = nacross*ndown;
995 for(y=0; y<nver; y++){
996 for(x=0; x<nhor; x++){
1003 /* arrange blockno to be in same sequence as original scan calculation. */
1004 tmcu = x/H + (nacross/H)*(y/V);
1005 blockno = tmcu*H*V + H*(y%V) + x%H;
1006 acc = h->accoeff[comp][blockno];
1009 rs = decode(h, acht);
1010 /* XXX remove rrrr ssss as in baselinescan */
1011 nibbles(rs, &rrrr, &ssss);
1016 eobrun = receiveEOB(h, rrrr)-1;
1021 z = receive(h, ssss);
1025 acc[k] = z*qt[k]<<Al;
1031 /* process restart marker, if present */
1033 if(ri>0 && mcu<nmcu && mcu%ri==0){
1042 increment(Header *h, int acc[], int k, int Pt)
1046 if(receivebit(h) != 0)
1055 progressiveacinc(Header *h, int comp, int Al)
1057 int Ns, i, z, k, Ss, Se, Ta, **ac, H, V;
1058 int ri, mcu, nacross, ndown, nhor, nver, eobrun, nzeros, pending, x, y, tmcu, blockno, q, nmcu;
1060 int *qt, rrrr, ssss, *acc, rs;
1066 jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
1069 H = h->comp[comp].H;
1070 V = h->comp[comp].V;
1072 nacross = h->nacross*H;
1075 nhor = (h->X+q-1)/q;
1077 nver = (h->Y+q-1)/q;
1079 /* initialize data structures */
1083 nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
1087 ac = h->accoeff[comp];
1088 acht = &h->acht[Ta];
1089 qt = h->qt[h->comp[comp].Tq];
1090 nmcu = nacross*ndown;
1094 for(y=0; y<nver; y++){
1095 for(x=0; x<nhor; x++){
1098 /* arrange blockno to be in same sequence as original scan calculation. */
1099 tmcu = x/H + (nacross/H)*(y/V);
1100 blockno = tmcu*H*V + H*(y%V) + x%H;
1104 jpgerror(h, "ReadJPG: zeros pending at block start");
1105 for(k=Ss; k<=Se; k++)
1106 increment(h, acc, k, qt[k]<<Al);
1114 increment(h, acc, k, qt[k]<<Al);
1115 else if(nzeros-- == 0)
1120 rs = decode(h, acht);
1121 nibbles(rs, &rrrr, &ssss);
1126 eobrun = receiveEOB(h, rrrr)-1;
1128 increment(h, acc, k, qt[k]<<Al);
1133 for(i=0; i<16; k++){
1134 increment(h, acc, k, qt[k]<<Al);
1140 jpgerror(h, "ReadJPG: ssss!=1 in progressive increment");
1142 pending = receivebit(h);
1145 pending *= qt[k]<<Al;
1149 /* process restart marker, if present */
1151 if(ri>0 && mcu<nmcu && mcu%ri==0){
1161 progressivescan(Header *h, int colorspace)
1164 int Ns, Ss, Ah, Al, c, comp, i;
1166 if(h->dccoeff[0] == nil)
1167 progressiveinit(h, colorspace);
1172 nibbles(ss[3+2*Ns], &Ah, &Al);
1175 for(i=0; i<h->Nf; i++)
1176 if(h->comp[i].C == c)
1179 jpgerror(h, "ReadJPG: bad component index in scan header");
1182 progressivedc(h, comp, Ah, Al);
1186 progressiveac(h, comp, Al);
1189 progressiveacinc(h, comp, Al);
1193 c1 = 2871, /* 1.402 * 2048 */
1194 c2 = 705, /* 0.34414 * 2048 */
1195 c3 = 1463, /* 0.71414 * 2048 */
1196 c4 = 3629, /* 1.772 * 2048 */
1201 colormap1(Header *h, int colorspace, Rawimage *image, int data[8*8], int mcu, int nacross)
1204 int x, y, dx, dy, minx, miny;
1208 pic = image->chans[0];
1209 minx = 8*(mcu%nacross);
1213 miny = 8*(mcu/nacross);
1217 pici = miny*h->X+minx;
1219 for(y=0; y<dy; y++){
1220 for(x=0; x<dx; x++){
1221 r = clamp[(data[k+x]+128)+CLAMPOFF];
1231 colormapall1(Header *h, int colorspace, Rawimage *image, int data0[8*8], int data1[8*8], int data2[8*8], int mcu, int nacross)
1233 uchar *rpic, *gpic, *bpic, *rp, *gp, *bp;
1235 int x, y, dx, dy, minx, miny;
1236 int r, g, b, k, pici;
1239 rpic = image->chans[0];
1240 gpic = image->chans[1];
1241 bpic = image->chans[2];
1242 minx = 8*(mcu%nacross);
1246 miny = 8*(mcu/nacross);
1250 pici = miny*h->X+minx;
1252 for(y=0; y<dy; y++){
1259 if(colorspace == CYCbCr)
1260 for(x=0; x<dx; x++){
1261 *rp++ = clamp[*p0++ + 128 + CLAMPOFF];
1262 *gp++ = clamp[*p1++ + 128 + CLAMPOFF];
1263 *bp++ = clamp[*p2++ + 128 + CLAMPOFF];
1266 for(x=0; x<dx; x++){
1267 Y = (*p0++ + 128) << 11;
1273 *rp++ = clamp[(r>>11)+CLAMPOFF];
1274 *gp++ = clamp[(g>>11)+CLAMPOFF];
1275 *bp++ = clamp[(b>>11)+CLAMPOFF];
1284 colormap(Header *h, int colorspace, Rawimage *image, int *data0[8*8], int *data1[8*8], int *data2[8*8], int mcu, int nacross, int Hmax, int Vmax, int *H, int *V)
1286 uchar *rpic, *gpic, *bpic;
1287 int x, y, dx, dy, minx, miny;
1288 int r, g, b, pici, H0, H1, H2;
1289 int t, b0, b1, b2, y0, y1, y2, x0, x1, x2;
1292 rpic = image->chans[0];
1293 gpic = image->chans[1];
1294 bpic = image->chans[2];
1295 minx = 8*Hmax*(mcu%nacross);
1299 miny = 8*Vmax*(mcu/nacross);
1303 pici = miny*h->X+minx;
1307 for(y=0; y<dy; y++){
1309 b0 = H0*(t/(8*Vmax));
1310 y0 = 8*((t/Vmax)&7);
1312 b1 = H1*(t/(8*Vmax));
1313 y1 = 8*((t/Vmax)&7);
1315 b2 = H2*(t/(8*Vmax));
1316 y2 = 8*((t/Vmax)&7);
1320 for(x=0; x<dx; x++){
1321 if(colorspace == CYCbCr){
1322 rpic[pici+x] = clamp[data0[b0][y0+x0++*H0/Hmax] + 128 + CLAMPOFF];
1323 gpic[pici+x] = clamp[data1[b1][y1+x1++*H1/Hmax] + 128 + CLAMPOFF];
1324 bpic[pici+x] = clamp[data2[b2][y2+x2++*H2/Hmax] + 128 + CLAMPOFF];
1326 Y = (data0[b0][y0+x0++*H0/Hmax]+128)<<11;
1327 Cb = data1[b1][y1+x1++*H1/Hmax];
1328 Cr = data2[b2][y2+x2++*H2/Hmax];
1332 rpic[pici+x] = clamp[(r>>11)+CLAMPOFF];
1333 gpic[pici+x] = clamp[(g>>11)+CLAMPOFF];
1334 bpic[pici+x] = clamp[(b>>11)+CLAMPOFF];
1336 if(x0*H0/Hmax >= 8){
1340 if(x1*H1/Hmax >= 8){
1344 if(x2*H2/Hmax >= 8){
1354 * decode next 8-bit value from entropy-coded input. chart F-26
1358 decode(Header *h, Huffman *t)
1360 int code, v, cnt, sr, i;
1363 maxcode = t->maxcode;
1367 code = (h->sr>>(h->cnt-8))&0xFF;
1370 h->cnt -= t->shift[code];
1380 for(i = 9; i<17; i++){
1384 if(code <= maxcode[i])
1388 sr = nextbyte(h, 0);
1398 return t->val[t->valptr[i]+(code-t->mincode[i])];
1402 * load next byte of input
1406 nextbyte(Header *h, int marker)
1416 jpgerror(h, "truncated file");
1426 jpgerror(h, "truncated file");
1429 jpgerror(h, "ReadJPG: DNL marker unimplemented");
1430 /* decoder is reading into marker; satisfy it and restore state */
1436 h->sr = (h->sr<<8) | b;
1441 * return next s bits of input, MSB first, and level shift it
1445 receive(Header *h, int s)
1452 v = h->sr >> h->cnt;
1462 * return next s bits of input, decode as EOB
1466 receiveEOB(Header *h, int s)
1473 v = h->sr >> h->cnt;
1482 * return next bit of input
1486 receivebit(Header *h)
1491 return (h->sr >> h->cnt) & 1;
1495 * Scaled integer implementation.
1496 * inverse two dimensional DCT, Chen-Wang algorithm
1497 * (IEEE ASSP-32, pp. 803-816, Aug. 1984)
1498 * 32-bit integer arithmetic (8 bit coefficients)
1499 * 11 mults, 29 adds per DCT
1501 * coefficients extended to 12 bit for IEEE1180-1990 compliance
1505 W1 = 2841, /* 2048*sqrt(2)*cos(1*pi/16)*/
1506 W2 = 2676, /* 2048*sqrt(2)*cos(2*pi/16)*/
1507 W3 = 2408, /* 2048*sqrt(2)*cos(3*pi/16)*/
1508 W5 = 1609, /* 2048*sqrt(2)*cos(5*pi/16)*/
1509 W6 = 1108, /* 2048*sqrt(2)*cos(6*pi/16)*/
1510 W7 = 565, /* 2048*sqrt(2)*cos(7*pi/16)*/
1512 W1pW7 = 3406, /* W1+W7*/
1513 W1mW7 = 2276, /* W1-W7*/
1514 W3pW5 = 4017, /* W3+W5*/
1515 W3mW5 = 799, /* W3-W5*/
1516 W2pW6 = 3784, /* W2+W6*/
1517 W2mW6 = 1567, /* W2-W6*/
1519 R2 = 181 /* 256/sqrt(2)*/
1526 int x, y, eighty, v;
1527 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
1530 /* transform horizontally*/
1533 /* if all non-DC components are zero, just propagate the DC term*/
1536 if(p[2]==0 && p[3]==0)
1537 if(p[4]==0 && p[5]==0)
1538 if(p[6]==0 && p[7]==0){
1551 x0 = (p[0]<<11)+128;
1581 x2 = (R2*(x4+x5)+128)>>8;
1582 x4 = (R2*(x4-x5)+128)>>8;
1593 /* transform vertically*/
1595 /* if all non-DC components are zero, just propagate the DC term*/
1598 if(p[8*2]==0 && p[8*3]==0)
1599 if(p[8*4]==0 && p[8*5]==0)
1600 if(p[8*6]==0 && p[8*7]==0){
1613 x0 = (p[8*0]<<8)+8192;
1622 x8 = W7*(x4+x5) + 4;
1623 x4 = (x8+W1mW7*x4)>>3;
1624 x5 = (x8-W1pW7*x5)>>3;
1625 x8 = W3*(x6+x7) + 4;
1626 x6 = (x8-W3mW5*x6)>>3;
1627 x7 = (x8-W3pW5*x7)>>3;
1631 x1 = W6*(x3+x2) + 4;
1632 x2 = (x1-W2pW6*x2)>>3;
1633 x3 = (x1+W2mW6*x3)>>3;
1643 x2 = (R2*(x4+x5)+128)>>8;
1644 x4 = (R2*(x4-x5)+128)>>8;
1646 p[8*0] = (x7+x1)>>14;
1647 p[8*1] = (x3+x2)>>14;
1648 p[8*2] = (x0+x4)>>14;
1649 p[8*3] = (x8+x6)>>14;
1650 p[8*4] = (x8-x6)>>14;
1651 p[8*5] = (x0-x4)>>14;
1652 p[8*6] = (x3-x2)>>14;
1653 p[8*7] = (x7-x1)>>14;