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(sizeof(Header));
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 header->comp[i].H = H;
332 header->comp[i].V = V;
333 header->comp[i].Tq = b[6+3*i+2];
341 switch(header->mode){
343 image = baselinescan(header, colorspace);
346 progressivescan(header, colorspace);
349 sprint(header->err, "unrecognized or unspecified encoding %d", header->mode);
355 huffmantables(header, b, n);
359 header->ri = int2(b, 0);
366 if(header->mode == SOF2)
367 image = progressiveIDCT(header, colorspace);
371 sprint(header->err, "ReadJPG: unknown marker %.2x", m);
378 /* readsegment is called after reading scan, which can have */
379 /* read ahead a byte. so we must check peek here */
389 }else if(Bread(h->fd, &x, 1) != 1)
390 jpgerror(h, readerr);
400 while((c=readbyte(h)) == 0)
401 fprint(2, "ReadJPG: skipping zero byte at offset %lld\n", Boffset(h->fd));
403 jpgerror(h, "ReadJPG: expecting marker; found 0x%x at offset %lld\n", c, Boffset(h->fd));
411 int2(uchar *buf, int n)
413 return (buf[n]<<8) + buf[n+1];
418 nibbles(int b, int *p0, int *p1)
430 jpgerror(h, "ReadJPG: unrecognized marker in header");
438 readsegment(Header *h, int *markerp)
449 jpgerror(h, "ReadJPG: expecting marker; saw %.2x at offset %lld", m, Boffset(h->fd));
451 if(Breadn(h->fd, tmp, 2) != 2)
453 jpgerror(h, readerr);
460 /* zero in case of short read later */
461 h->buf = jpgmalloc(h, n+1, 1); /* +1 for sentinel */
464 /* accept short reads to cope with some real-world jpegs */
465 if(Breadn(h->fd, h->buf, n) < 0)
473 huffmantable(Header *h, uchar *b)
476 int Tc, th, n, nsize, i, j, k, v, cnt, code, si, sr, m;
479 nibbles(b[0], &Tc, &th);
481 jpgerror(h, "ReadJPG: unknown Huffman table class %d", Tc);
482 if(th>3 || (h->mode==SOF && th>1))
483 jpgerror(h, "ReadJPG: unknown Huffman table index %d", th);
493 t->size = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
495 for(i=1; i<=16; i++){
502 /* initialize HUFFVAL */
503 t->val = jpgmalloc(h, nsize*sizeof(int), 1);
504 for(i=0; i<nsize; i++)
508 t->code = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
514 t->code[k++] = code++;
515 while(t->size[k] == si);
521 }while(t->size[k] != si);
524 /* flow chart F-25 */
537 t->mincode[i] = t->code[j];
539 t->maxcode[i] = t->code[j];
544 /* create byte-indexed fast path tables */
545 maxcode = t->maxcode;
546 /* stupid startup algorithm: just run machine for each byte value */
556 if(code <= maxcode[i])
568 t->value[v] = t->val[t->valptr[i]+(code-t->mincode[i])];
579 huffmantables(Header *h, uchar *b, int n)
583 for(l=0; l<n; l+=17+mt)
584 mt = huffmantable(h, &b[l]);
589 quanttable(Header *h, uchar *b)
593 nibbles(b[0], &pq, &tq);
595 jpgerror(h, "ReadJPG: unknown quantization table class %d", pq);
597 jpgerror(h, "ReadJPG: unknown quantization table index %d", tq);
603 q[i] = int2(b, 1+2*i);
610 quanttables(Header *h, uchar *b, int n)
614 for(l=0; l<n; l+=1+m)
615 m = quanttable(h, &b[l]);
620 baselinescan(Header *h, int colorspace)
622 int Ns, z, k, m, Hmax, Vmax, comp;
623 int allHV1, nblock, ri, mcu, nacross, nmcu;
624 Huffman *dcht, *acht;
625 int block, t, diff, *qt;
628 int Td[3], Ta[3], H[3], V[3], DC[3];
633 if((Ns!=3 && Ns!=1) || Ns!=h->Nf)
634 jpgerror(h, "ReadJPG: can't handle scan not 3 components");
636 image = jpgmalloc(h, sizeof(Rawimage), 1);
638 image->r = Rect(0, 0, h->X, h->Y);
641 image->chanlen = h->X*h->Y;
645 image->giftrindex = 0;
647 image->chandesc = colorspace;
649 image->chandesc = CY;
650 image->nchans = h->Nf;
651 for(k=0; k<h->Nf; k++)
652 image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
654 /* compute maximum H and V */
657 for(comp=0; comp<Ns; comp++){
658 if(h->comp[comp].H > Hmax)
659 Hmax = h->comp[comp].H;
660 if(h->comp[comp].V > Vmax)
661 Vmax = h->comp[comp].V;
664 /* initialize data structures */
667 for(comp=0; comp<Ns; comp++){
668 /* JPEG requires scan components to be in same order as in frame, */
669 /* so if both have 3 we know scan is Y Cb Cr and there's no need to */
671 nibbles(ss[2+2*comp], &Td[comp], &Ta[comp]);
672 H[comp] = h->comp[comp].H;
673 V[comp] = h->comp[comp].V;
674 nblock = H[comp]*V[comp];
677 data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
678 h->ndata[comp] = nblock;
680 for(m=0; m<nblock; m++)
681 data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
689 nacross = ((h->X+(8*Hmax-1))/(8*Hmax));
690 nmcu = ((h->Y+(8*Vmax-1))/(8*Vmax))*nacross;
691 for(mcu=0; mcu<nmcu; ){
692 for(comp=0; comp<Ns; comp++){
693 dcht = &h->dcht[Td[comp]];
694 acht = &h->acht[Ta[comp]];
695 qt = h->qt[h->comp[comp].Tq];
697 for(block=0; block<H[comp]*V[comp]; block++){
700 diff = receive(h, t);
704 zz = data[comp][block];
705 memset(zz, 0, 8*8*sizeof(int));
706 zz[0] = qt[0]*DC[comp];
717 z = receive(h, t&0xF);
718 zz[zig[k]] = z*qt[k];
729 /* rotate colors to RGB and assign to bytes */
730 if(Ns == 1) /* very easy */
731 colormap1(h, colorspace, image, data[0][0], mcu, nacross);
732 else if(allHV1) /* fairly easy */
733 colormapall1(h, colorspace, image, data[0][0], data[1][0], data[2][0], mcu, nacross);
734 else /* miserable general case */
735 colormap(h, colorspace, image, data[0], data[1], data[2], mcu, nacross, Hmax, Vmax, H, V);
736 /* process restart marker, if present */
738 if(ri>0 && mcu<nmcu && mcu%ri==0){
740 for(comp=0; comp<Ns; comp++)
749 restart(Header *h, int mcu)
751 int rest, rst, nskip;
757 rst = nextbyte(h, 1);
759 }while(rst>=0 && rst!=0xFF);
761 rst = nextbyte(h, 1);
764 }while(rst>=0 && (rst&~7)!=RST);
766 sprint(h->err, "ReadJPG: skipped %d bytes at restart %d\n", nskip-2, rest);
768 jpgerror(h, readerr);
769 if((rst&7) != (rest&7))
770 jpgerror(h, "ReadJPG: expected RST%d got %d", rest&7, rst&7);
777 progressiveIDCT(Header *h, int colorspace)
779 int k, m, comp, block, Nf, bn;
780 int allHV1, nblock, mcu, nmcu;
781 int H[3], V[3], blockno[3];
782 int *dccoeff, **accoeff;
789 for(comp=0; comp<Nf; comp++){
790 H[comp] = h->comp[comp].H;
791 V[comp] = h->comp[comp].V;
792 nblock = h->nblock[comp];
795 h->ndata[comp] = nblock;
796 data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
797 for(m=0; m<nblock; m++)
798 data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
801 memset(blockno, 0, sizeof blockno);
802 nmcu = h->nacross*h->ndown;
803 for(mcu=0; mcu<nmcu; mcu++){
804 for(comp=0; comp<Nf; comp++){
805 dccoeff = h->dccoeff[comp];
806 accoeff = h->accoeff[comp];
808 for(block=0; block<h->nblock[comp]; block++){
809 zz = data[comp][block];
810 memset(zz, 0, 8*8*sizeof(int));
814 zz[zig[k]] = accoeff[bn][k];
822 /* rotate colors to RGB and assign to bytes */
823 if(Nf == 1) /* very easy */
824 colormap1(h, colorspace, h->image, data[0][0], mcu, h->nacross);
825 else if(allHV1) /* fairly easy */
826 colormapall1(h, colorspace, h->image, data[0][0], data[1][0], data[2][0], mcu, h->nacross);
827 else /* miserable general case */
828 colormap(h, colorspace, h->image, data[0], data[1], data[2], mcu, h->nacross, h->Hmax, h->Vmax, H, V);
836 progressiveinit(Header *h, int colorspace)
838 int Nf, Ns, j, k, nmcu, comp;
845 if((Ns!=3 && Ns!=1) || Ns!=Nf)
846 jpgerror(h, "ReadJPG: image must have 1 or 3 components");
848 image = jpgmalloc(h, sizeof(Rawimage), 1);
850 image->r = Rect(0, 0, h->X, h->Y);
853 image->chanlen = h->X*h->Y;
857 image->giftrindex = 0;
859 image->chandesc = colorspace;
861 image->chandesc = CY;
862 image->nchans = h->Nf;
864 image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
865 h->nblock[k] = h->comp[k].H*h->comp[k].V;
868 /* compute maximum H and V */
871 for(comp=0; comp<Nf; comp++){
872 if(h->comp[comp].H > h->Hmax)
873 h->Hmax = h->comp[comp].H;
874 if(h->comp[comp].V > h->Vmax)
875 h->Vmax = h->comp[comp].V;
877 h->nacross = ((h->X+(8*h->Hmax-1))/(8*h->Hmax));
878 h->ndown = ((h->Y+(8*h->Vmax-1))/(8*h->Vmax));
879 nmcu = h->nacross*h->ndown;
882 h->dccoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int), 1);
883 h->accoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int*), 1);
884 h->naccoeff[k] = h->nblock[k]*nmcu;
885 for(j=0; j<h->nblock[k]*nmcu; j++)
886 h->accoeff[k][j] = jpgmalloc(h, 64*sizeof(int), 1);
893 progressivedc(Header *h, int comp, int Ah, int Al)
895 int Ns, z, ri, mcu, nmcu;
896 int block, t, diff, qt, *dc, bn;
899 int Td[3], DC[3], blockno[3];
904 jpgerror(h, "ReadJPG: can't handle progressive with Nf!=Ns in DC scan");
906 /* initialize data structures */
910 for(comp=0; comp<Ns; comp++){
912 * JPEG requires scan components to be in same order as in frame,
913 * so if both have 3 we know scan is Y Cb Cr and there's no need to
916 nibbles(ss[2+2*comp], &Td[comp], &z); /* z is ignored */
922 nmcu = h->nacross*h->ndown;
923 memset(blockno, 0, sizeof blockno);
924 for(mcu=0; mcu<nmcu; ){
925 for(comp=0; comp<Ns; comp++){
926 dcht = &h->dcht[Td[comp]];
927 qt = h->qt[h->comp[comp].Tq][0];
928 dc = h->dccoeff[comp];
931 for(block=0; block<h->nblock[comp]; block++){
934 diff = receive(h, t);
936 dc[bn] = qt*DC[comp]<<Al;
938 dc[bn] |= qt*receivebit(h)<<Al;
944 /* process restart marker, if present */
946 if(ri>0 && mcu<nmcu && mcu%ri==0){
948 for(comp=0; comp<Ns; comp++)
956 progressiveac(Header *h, int comp, int Al)
958 int Ns, Ss, Se, z, k, eobrun, x, y, nver, tmcu, blockno, *acc, rs;
959 int ri, mcu, nacross, ndown, nmcu, nhor;
961 int *qt, rrrr, ssss, q;
968 jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
974 nacross = h->nacross*H;
981 /* initialize data structures */
985 nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
991 qt = h->qt[h->comp[comp].Tq];
992 nmcu = nacross*ndown;
994 for(y=0; y<nver; y++){
995 for(x=0; x<nhor; x++){
1002 /* arrange blockno to be in same sequence as original scan calculation. */
1003 tmcu = x/H + (nacross/H)*(y/V);
1004 blockno = tmcu*H*V + H*(y%V) + x%H;
1005 acc = h->accoeff[comp][blockno];
1008 rs = decode(h, acht);
1009 /* XXX remove rrrr ssss as in baselinescan */
1010 nibbles(rs, &rrrr, &ssss);
1015 eobrun = receiveEOB(h, rrrr)-1;
1021 z = receive(h, ssss);
1022 acc[k] = z*qt[k]<<Al;
1030 /* process restart marker, if present */
1032 if(ri>0 && mcu<nmcu && mcu%ri==0){
1041 increment(Header *h, int acc[], int k, int Pt)
1045 if(receivebit(h) != 0)
1054 progressiveacinc(Header *h, int comp, int Al)
1056 int Ns, i, z, k, Ss, Se, Ta, **ac, H, V;
1057 int ri, mcu, nacross, ndown, nhor, nver, eobrun, nzeros, pending, x, y, tmcu, blockno, q, nmcu;
1059 int *qt, rrrr, ssss, *acc, rs;
1065 jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
1068 H = h->comp[comp].H;
1069 V = h->comp[comp].V;
1071 nacross = h->nacross*H;
1074 nhor = (h->X+q-1)/q;
1076 nver = (h->Y+q-1)/q;
1078 /* initialize data structures */
1082 nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
1086 ac = h->accoeff[comp];
1087 acht = &h->acht[Ta];
1088 qt = h->qt[h->comp[comp].Tq];
1089 nmcu = nacross*ndown;
1093 for(y=0; y<nver; y++){
1094 for(x=0; x<nhor; x++){
1097 /* arrange blockno to be in same sequence as original scan calculation. */
1098 tmcu = x/H + (nacross/H)*(y/V);
1099 blockno = tmcu*H*V + H*(y%V) + x%H;
1103 jpgerror(h, "ReadJPG: zeros pending at block start");
1104 for(k=Ss; k<=Se; k++)
1105 increment(h, acc, k, qt[k]<<Al);
1113 increment(h, acc, k, qt[k]<<Al);
1114 else if(nzeros-- == 0)
1119 rs = decode(h, acht);
1120 nibbles(rs, &rrrr, &ssss);
1125 eobrun = receiveEOB(h, rrrr)-1;
1127 increment(h, acc, k, qt[k]<<Al);
1132 for(i=0; i<16; k++){
1133 increment(h, acc, k, qt[k]<<Al);
1139 jpgerror(h, "ReadJPG: ssss!=1 in progressive increment");
1141 pending = receivebit(h);
1144 pending *= qt[k]<<Al;
1148 /* process restart marker, if present */
1150 if(ri>0 && mcu<nmcu && mcu%ri==0){
1160 progressivescan(Header *h, int colorspace)
1163 int Ns, Ss, Ah, Al, c, comp, i;
1165 if(h->dccoeff[0] == nil)
1166 progressiveinit(h, colorspace);
1171 nibbles(ss[3+2*Ns], &Ah, &Al);
1174 for(i=0; i<h->Nf; i++)
1175 if(h->comp[i].C == c)
1178 jpgerror(h, "ReadJPG: bad component index in scan header");
1181 progressivedc(h, comp, Ah, Al);
1185 progressiveac(h, comp, Al);
1188 progressiveacinc(h, comp, Al);
1192 c1 = 2871, /* 1.402 * 2048 */
1193 c2 = 705, /* 0.34414 * 2048 */
1194 c3 = 1463, /* 0.71414 * 2048 */
1195 c4 = 3629, /* 1.772 * 2048 */
1200 colormap1(Header *h, int colorspace, Rawimage *image, int data[8*8], int mcu, int nacross)
1203 int x, y, dx, dy, minx, miny;
1207 pic = image->chans[0];
1208 minx = 8*(mcu%nacross);
1212 miny = 8*(mcu/nacross);
1216 pici = miny*h->X+minx;
1218 for(y=0; y<dy; y++){
1219 for(x=0; x<dx; x++){
1220 r = clamp[(data[k+x]+128)+CLAMPOFF];
1230 colormapall1(Header *h, int colorspace, Rawimage *image, int data0[8*8], int data1[8*8], int data2[8*8], int mcu, int nacross)
1232 uchar *rpic, *gpic, *bpic, *rp, *gp, *bp;
1234 int x, y, dx, dy, minx, miny;
1235 int r, g, b, k, pici;
1238 rpic = image->chans[0];
1239 gpic = image->chans[1];
1240 bpic = image->chans[2];
1241 minx = 8*(mcu%nacross);
1245 miny = 8*(mcu/nacross);
1249 pici = miny*h->X+minx;
1251 for(y=0; y<dy; y++){
1258 if(colorspace == CYCbCr)
1259 for(x=0; x<dx; x++){
1260 *rp++ = clamp[*p0++ + 128 + CLAMPOFF];
1261 *gp++ = clamp[*p1++ + 128 + CLAMPOFF];
1262 *bp++ = clamp[*p2++ + 128 + CLAMPOFF];
1265 for(x=0; x<dx; x++){
1266 Y = (*p0++ + 128) << 11;
1272 *rp++ = clamp[(r>>11)+CLAMPOFF];
1273 *gp++ = clamp[(g>>11)+CLAMPOFF];
1274 *bp++ = clamp[(b>>11)+CLAMPOFF];
1283 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)
1285 uchar *rpic, *gpic, *bpic;
1286 int x, y, dx, dy, minx, miny;
1287 int r, g, b, pici, H0, H1, H2;
1288 int t, b0, b1, b2, y0, y1, y2, x0, x1, x2;
1291 rpic = image->chans[0];
1292 gpic = image->chans[1];
1293 bpic = image->chans[2];
1294 minx = 8*Hmax*(mcu%nacross);
1298 miny = 8*Vmax*(mcu/nacross);
1302 pici = miny*h->X+minx;
1306 for(y=0; y<dy; y++){
1308 b0 = H0*(t/(8*Vmax));
1309 y0 = 8*((t/Vmax)&7);
1311 b1 = H1*(t/(8*Vmax));
1312 y1 = 8*((t/Vmax)&7);
1314 b2 = H2*(t/(8*Vmax));
1315 y2 = 8*((t/Vmax)&7);
1319 for(x=0; x<dx; x++){
1320 if(colorspace == CYCbCr){
1321 rpic[pici+x] = clamp[data0[b0][y0+x0++*H0/Hmax] + 128 + CLAMPOFF];
1322 gpic[pici+x] = clamp[data1[b1][y1+x1++*H1/Hmax] + 128 + CLAMPOFF];
1323 bpic[pici+x] = clamp[data2[b2][y2+x2++*H2/Hmax] + 128 + CLAMPOFF];
1325 Y = (data0[b0][y0+x0++*H0/Hmax]+128)<<11;
1326 Cb = data1[b1][y1+x1++*H1/Hmax];
1327 Cr = data2[b2][y2+x2++*H2/Hmax];
1331 rpic[pici+x] = clamp[(r>>11)+CLAMPOFF];
1332 gpic[pici+x] = clamp[(g>>11)+CLAMPOFF];
1333 bpic[pici+x] = clamp[(b>>11)+CLAMPOFF];
1335 if(x0*H0/Hmax >= 8){
1339 if(x1*H1/Hmax >= 8){
1343 if(x2*H2/Hmax >= 8){
1353 * decode next 8-bit value from entropy-coded input. chart F-26
1357 decode(Header *h, Huffman *t)
1359 int code, v, cnt, m, sr, i;
1363 maxcode = t->maxcode;
1367 code = (h->sr>>(h->cnt-8))&0xFF;
1370 h->cnt -= t->shift[code];
1386 if(code <= maxcode[i])
1391 sr = nextbyte(h, 0);
1399 fprint(2, "badly encoded %dx%d JPEG file; ignoring bad value\n", h->X, h->Y);
1404 return t->val[t->valptr[i]+(code-t->mincode[i])];
1408 * load next byte of input
1412 nextbyte(Header *h, int marker)
1422 jpgerror(h, "truncated file");
1432 jpgerror(h, "truncated file");
1435 jpgerror(h, "ReadJPG: DNL marker unimplemented");
1436 /* decoder is reading into marker; satisfy it and restore state */
1442 h->sr = (h->sr<<8) | b;
1447 * return next s bits of input, MSB first, and level shift it
1451 receive(Header *h, int s)
1458 v = h->sr >> h->cnt;
1468 * return next s bits of input, decode as EOB
1472 receiveEOB(Header *h, int s)
1479 v = h->sr >> h->cnt;
1488 * return next bit of input
1492 receivebit(Header *h)
1497 return (h->sr >> h->cnt) & 1;
1501 * Scaled integer implementation.
1502 * inverse two dimensional DCT, Chen-Wang algorithm
1503 * (IEEE ASSP-32, pp. 803-816, Aug. 1984)
1504 * 32-bit integer arithmetic (8 bit coefficients)
1505 * 11 mults, 29 adds per DCT
1507 * coefficients extended to 12 bit for IEEE1180-1990 compliance
1511 W1 = 2841, /* 2048*sqrt(2)*cos(1*pi/16)*/
1512 W2 = 2676, /* 2048*sqrt(2)*cos(2*pi/16)*/
1513 W3 = 2408, /* 2048*sqrt(2)*cos(3*pi/16)*/
1514 W5 = 1609, /* 2048*sqrt(2)*cos(5*pi/16)*/
1515 W6 = 1108, /* 2048*sqrt(2)*cos(6*pi/16)*/
1516 W7 = 565, /* 2048*sqrt(2)*cos(7*pi/16)*/
1518 W1pW7 = 3406, /* W1+W7*/
1519 W1mW7 = 2276, /* W1-W7*/
1520 W3pW5 = 4017, /* W3+W5*/
1521 W3mW5 = 799, /* W3-W5*/
1522 W2pW6 = 3784, /* W2+W6*/
1523 W2mW6 = 1567, /* W2-W6*/
1525 R2 = 181 /* 256/sqrt(2)*/
1532 int x, y, eighty, v;
1533 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
1536 /* transform horizontally*/
1539 /* if all non-DC components are zero, just propagate the DC term*/
1542 if(p[2]==0 && p[3]==0)
1543 if(p[4]==0 && p[5]==0)
1544 if(p[6]==0 && p[7]==0){
1557 x0 = (p[0]<<11)+128;
1587 x2 = (R2*(x4+x5)+128)>>8;
1588 x4 = (R2*(x4-x5)+128)>>8;
1599 /* transform vertically*/
1601 /* if all non-DC components are zero, just propagate the DC term*/
1604 if(p[8*2]==0 && p[8*3]==0)
1605 if(p[8*4]==0 && p[8*5]==0)
1606 if(p[8*6]==0 && p[8*7]==0){
1619 x0 = (p[8*0]<<8)+8192;
1628 x8 = W7*(x4+x5) + 4;
1629 x4 = (x8+W1mW7*x4)>>3;
1630 x5 = (x8-W1pW7*x5)>>3;
1631 x8 = W3*(x6+x7) + 4;
1632 x6 = (x8-W3mW5*x6)>>3;
1633 x7 = (x8-W3pW5*x7)>>3;
1637 x1 = W6*(x3+x2) + 4;
1638 x2 = (x1-W2pW6*x2)>>3;
1639 x3 = (x1+W2mW6*x3)>>3;
1649 x2 = (R2*(x4+x5)+128)>>8;
1650 x4 = (R2*(x4-x5)+128)>>8;
1652 p[8*0] = (x7+x1)>>14;
1653 p[8*1] = (x3+x2)>>14;
1654 p[8*2] = (x0+x4)>>14;
1655 p[8*3] = (x8+x6)>>14;
1656 p[8*4] = (x8-x6)>>14;
1657 p[8*5] = (x0-x4)>>14;
1658 p[8*6] = (x3-x2)>>14;
1659 p[8*7] = (x7-x1)>>14;