shithub: riscv

Download patch

ref: d7a4fdee99c295a2d201f70ae908a7bec43f4c04
parent: ccb624e2bb67161590f490f671f611a44fcbd61e
author: cinap_lenrek <[email protected]>
date: Sun Feb 22 20:20:42 EST 2015

jpg: handle progressive non-interleaved mode

--- a/sys/src/cmd/jpg/readjpg.c
+++ b/sys/src/cmd/jpg/readjpg.c
@@ -632,7 +632,7 @@
 	ss = h->ss;
 	Ns = ss[0];
 	if((Ns!=3 && Ns!=1) || Ns!=h->Nf)
-		jpgerror(h, "ReadJPG: can't handle scan not 3 components");
+		jpgerror(h, "ReadJPG: can't handle scan not 1 or 3 components");
 
 	image = jpgmalloc(h, sizeof(Rawimage), 1);
 	h->image = image;
@@ -843,7 +843,7 @@
 	ss = h->ss;
 	Ns = ss[0];
 	Nf = h->Nf;
-	if((Ns!=3 && Ns!=1) || Ns!=Nf)
+	if(Ns!=3 && Ns!=1)
 		jpgerror(h, "ReadJPG: image must have 1 or 3 components");
 
 	image = jpgmalloc(h, sizeof(Rawimage), 1);
@@ -897,25 +897,26 @@
 	int block, t, diff, qt, *dc, bn;
 	Huffman *dcht;
 	uchar *ss;
-	int Td[3], DC[3], blockno[3];
+	int i, Td[3], DC[3], blockno[3];
 
 	ss= h->ss;
 	Ns = ss[0];
-	if(Ns!=h->Nf)
-		jpgerror(h, "ReadJPG: can't handle progressive with Nf!=Ns in DC scan");
+	if(Ns!=1 && Ns!=h->Nf)
+		jpgerror(h, "ReadJPG: can't handle progressive with Ns!=1 and Nf!=Ns in DC scan");
 
 	/* initialize data structures */
 	h->cnt = 0;
 	h->sr = 0;
 	h->peek = -1;
-	for(comp=0; comp<Ns; comp++){
+
+	for(i=0; i<Ns; i++){
 		/*
 		 * JPEG requires scan components to be in same order as in frame,
 		 * so if both have 3 we know scan is Y Cb Cr and there's no need to
 		 * reorder
 		 */
-		nibbles(ss[2+2*comp], &Td[comp], &z);	/* z is ignored */
-		DC[comp] = 0;
+		nibbles(ss[2+2*i], &Td[i], &z);	/* z is ignored */
+		DC[i] = 0;
 	}
 
 	ri = h->ri;
@@ -923,23 +924,25 @@
 	nmcu = h->nacross*h->ndown;
 	memset(blockno, 0, sizeof blockno);
 	for(mcu=0; mcu<nmcu; ){
-		for(comp=0; comp<Ns; comp++){
-			dcht = &h->dcht[Td[comp]];
+		for(i=0; i<Ns; i++){
+			if(Ns != 1) comp = i;
+
+			dcht = &h->dcht[Td[i]];
 			qt = h->qt[h->comp[comp].Tq][0];
 			dc = h->dccoeff[comp];
-			bn = blockno[comp];
+			bn = blockno[i];
 
 			for(block=0; block<h->nblock[comp]; block++){
 				if(Ah == 0){
 					t = decode(h, dcht);
 					diff = receive(h, t);
-					DC[comp] += diff;
-					dc[bn] = qt*DC[comp]<<Al;
+					DC[i] += diff;
+					dc[bn] = qt*DC[i]<<Al;
 				}else
 					dc[bn] |= qt*receivebit(h)<<Al;
 				bn++;
 			}
-			blockno[comp] = bn;
+			blockno[i] = bn;
 		}
 
 		/* process restart marker, if present */
@@ -946,8 +949,8 @@
 		mcu++;
 		if(ri>0 && mcu<nmcu && mcu%ri==0){
 			restart(h, mcu);
-			for(comp=0; comp<Ns; comp++)
-				DC[comp] = 0;
+			for(i=0; i<Ns; i++)
+				DC[i] = 0;
 		}
 	}
 }