ImageReader.st
changeset 4776 39b960081256
parent 4714 3ea57c207759
child 4912 b2fd9c2d5e11
--- a/ImageReader.st	Wed May 30 14:53:08 2007 +0200
+++ b/ImageReader.st	Wed May 30 16:43:54 2007 +0200
@@ -880,14 +880,22 @@
 }
 
 /*
+ * a getc either from file fp (if not NULL) or cp (if fp is NULL)
+ */
+#define GETC(fp, cp)    (fp ? getc(fp) : *cp++)
+#define FERROR(fp)      (fp && ferror(fp))
+
+/*
  * BMP file read/decompression
  *
  * depth1 to 8bit ...
+ * load from file fp (if not NULL) or cp (if fp is NULL)
  */
 static int
-loadBMP1to8(w, h, fp, dest, szDest)
+loadBMP1to8(w, h, fp, cp, dest, szDest)
     int w, h;
     FILE *fp;
+    unsigned char *cp;
     unsigned char *dest;
     int szDest;
 {
@@ -901,7 +909,7 @@
 	pp = dest + (i * w);
 	for (j=bitnum=0; j<padw; j++,bitnum++) {
 	    if ((bitnum&7) == 0) { /* read the next byte */
-		c = getc(fp);
+		c = GETC(fp, cp);
 		if (c == EOF) {
 		    if (@global(InfoPrinting) == true) {
 			console_fprintf(stderr, "ImageReader [warning]: premature EOF\n");
@@ -921,20 +929,22 @@
 		c <<= 1;
 	    }
 	}
-	if (ferror(fp)) return 0;
+	if (FERROR(fp)) return 0;
     }
-    if (ferror(fp)) return 0;
+    if (FERROR(fp)) return 0;
     return 1;
 }
 
 /*
  * depth2 to 8bit ...
+ * load from file fp (if not NULL) or cp (if fp is NULL)
  */
 static int
-loadBMP2to8(w, h, fp, dest, szDest)
+loadBMP2to8(w, h, fp, cp, dest, szDest)
     int w, h;
     FILE *fp;
     unsigned char *dest;
+    unsigned char *cp;
     int szDest;
 {
     int   i,j,c,bitnum,padw;
@@ -947,7 +957,7 @@
 	pp = dest + (i * w);
 	for (j=bitnum=0; j<padw; j++,bitnum++) {
 	    if ((bitnum&3) == 0) { /* read the next byte */
-		c = getc(fp);
+		c = GETC(fp, cp);
 		if (c == EOF) return 0;
 		bitnum = 0;
 	    }
@@ -962,19 +972,21 @@
 		c <<= 2;
 	    }
 	}
-	if (ferror(fp)) return 0;
+	if (FERROR(fp)) return 0;
     }
-    if (ferror(fp)) return 0;
+    if (FERROR(fp)) return 0;
     return 1;
 }
 
 /*
  * depth4 to 8bit ...
+ * load from file fp (if not NULL) or cp (if fp is NULL)
  */
 static int
-loadBMP4to8(w, h, comp, fp, dest, szDest)
+loadBMP4to8(w, h, comp, fp, cp, dest, szDest)
     int w, h;
     FILE *fp;
+    unsigned char *cp;
     unsigned char *dest;
     int szDest;
 {
@@ -991,7 +1003,7 @@
 
 	    for (j=nybnum=0; j<padw; j++,nybnum++) {
 		if ((nybnum & 1) == 0) { /* read next byte */
-		    c = getc(fp);
+		    c = GETC(fp, cp);
 		    if (c == EOF) return 0;
 		    nybnum = 0;
 		}
@@ -1007,9 +1019,9 @@
 		    c <<= 4;
 		}
 	    }
-	    if (ferror(fp)) return 0;
+	    if (FERROR(fp)) return 0;
 	}
-	if (ferror(fp)) return 0;
+	if (FERROR(fp)) return 0;
 	return 1;
     }
     if (comp == 2) {  /* read RLE4 compressed data */
@@ -1017,11 +1029,11 @@
 	pp = dest + x + (h-y-1)*w;
 
 	while (y<h) {
-	    c = getc(fp); if (c == EOF) return 0;
+	    c = GETC(fp, cp); if (c == EOF) return 0;
 
 	    if (c) {                                   /* encoded mode */
 		c &= 0xFF;
-		c1 = getc(fp);
+		c1 = GETC(fp, cp);
 		if (c1 == EOF) return 0;
 		for (i=0; i<c; i++,x++,pp++) {
 		    if (pp >= (dest+szDest)) {
@@ -1034,7 +1046,7 @@
 		}
 	    } else {
 		/* c==0x00  :  escape codes */
-		c = getc(fp);
+		c = GETC(fp, cp);
 		if (c == EOF) return 0;
 
 		if (c == 0x00) {                    /* end of line */
@@ -1043,10 +1055,10 @@
 		    if (c == 0x01) break;               /* end of pic8 */
 
 		    else if (c == 0x02) {                /* delta */
-			c = getc(fp);
+			c = GETC(fp, cp);
 			if (c == EOF) return 0;
 			x += (c & 0xFF);
-			c = getc(fp);
+			c = GETC(fp, cp);
 			if (c == EOF) return 0;
 			y += (c & 0xFF);
 			pp = dest + x + (h-y-1)*w;
@@ -1054,7 +1066,7 @@
 			c &= 0xFF;
 			for (i=0; i<c; i++, x++, pp++) {
 			    if ((i&1) == 0) {
-				c1 = getc(fp);
+				c1 = GETC(fp, cp);
 				if (c1 == EOF) return 0;
 			    }
 			    if (pp >= (dest+szDest)) {
@@ -1066,12 +1078,12 @@
 			    *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
 			}
 
-			if (((c&3)==1) || ((c&3)==2)) getc(fp);  /* read pad byte */
+			if (((c&3)==1) || ((c&3)==2)) GETC(fp, cp);  /* read pad byte */
 		    }
 	    }  /* escape processing */
-	    if (ferror(fp)) return 0;
+	    if (FERROR(fp)) return 0;
 	}  /* while */
-	if (ferror(fp)) return 0;
+	if (FERROR(fp)) return 0;
 	return 1;
     }
 
@@ -1080,11 +1092,13 @@
 
 /*
  * depth8 to 8bit ...
+ * load from file fp (if not NULL) or cp (if fp is NULL)
  */
 static int
-loadBMP8(w, h, comp, fp, dest, szDest)
+loadBMP8(w, h, comp, fp, cp, dest, szDest)
     int w, h;
     FILE *fp;
+    unsigned char *cp;
     unsigned char *dest;
     int szDest;
 {
@@ -1098,7 +1112,7 @@
 	    pp = dest + (i * w);
 
 	    for (j=0; j<padw; j++) {
-		c = getc(fp);
+		c = GETC(fp, cp);
 		if (c==EOF) {
 		    if (@global(InfoPrinting) == true) {
 			console_fprintf(stderr, "ImageReader [warning]: BMP premature EOF [%d]\n", __LINE__);
@@ -1116,14 +1130,14 @@
 		    *pp++ = c;
 		}
 	    }
-	    if (ferror(fp)) {
+	    if (FERROR(fp)) {
 		if (@global(InfoPrinting) == true) {
 		    console_fprintf(stderr, "ImageReader [warning]: BMP ferror set\n");
 		}
 		return 0;
 	    }
 	}
-	if (ferror(fp)) {
+	if (FERROR(fp)) {
 	    if (@global(InfoPrinting) == true) {
 		console_fprintf(stderr, "ImageReader [warning]: BMP ferror set at end\n");
 	    }
@@ -1136,7 +1150,7 @@
 	pp = dest + x + (h-y-1)*w;
 
 	while (y<h) {
-	    c = getc(fp);
+	    c = GETC(fp, cp);
 	    if (c == EOF) {
 		if (@global(InfoPrinting) == true) {
 		    console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
@@ -1144,7 +1158,7 @@
 		return 0;
 	    }
 	    if (c) {                                   /* encoded mode */
-		c1 = getc(fp);
+		c1 = GETC(fp, cp);
 		if (c1 == EOF) {
 		    if (@global(InfoPrinting) == true) {
 			console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
@@ -1164,7 +1178,7 @@
 		}
 	    } else {
 		/* c==0x00  :  escape codes */
-		c = getc(fp);
+		c = GETC(fp, cp);
 		if (c == EOF) {
 		    if (@global(InfoPrinting) == true) {
 			console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
@@ -1176,7 +1190,7 @@
 		} else
 		    if (c == 0x01) break;               /* end of pic8 */
 		    else if (c == 0x02) {               /* delta */
-			c = getc(fp);
+			c = GETC(fp, cp);
 			if (c == EOF) {
 			    if (@global(InfoPrinting) == true) {
 				console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
@@ -1184,7 +1198,7 @@
 			    return 0;
 			}
 			x += (c & 0xFF);
-			c = getc(fp);
+			c = GETC(fp, cp);
 			if (c == EOF) {
 			    if (@global(InfoPrinting) == true) {
 				console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
@@ -1196,7 +1210,7 @@
 		    } else {                            /* absolute mode */
 			c &= 0xFF;
 			for (i=0; i<c; i++, x++, pp++) {
-			    c1 = getc(fp);
+			    c1 = GETC(fp, cp);
 			    if (c1 == EOF) return 0;
 			    if (pp >= (dest+szDest)) {
 				if (@global(InfoPrinting) == true) {
@@ -1207,17 +1221,17 @@
 			    *pp = c1;
 			}
 
-			if (c & 1) getc(fp);  /* odd length run: read an extra pad byte */
+			if (c & 1) GETC(fp, cp);  /* odd length run: read an extra pad byte */
 		    }
 	    }
-	    if (ferror(fp)) {
+	    if (FERROR(fp)) {
 		if (@global(InfoPrinting) == true) {
 		    console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set\n");
 		}
 		return 0;
 	    }
 	}
-	if (ferror(fp)) {
+	if (FERROR(fp)) {
 	    if (@global(InfoPrinting) == true) {
 		console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set at end\n");
 	    }
@@ -1485,20 +1499,39 @@
     "load bmp-1 bit per pixel imagedata. A helper for BMP image reader.
      Calls primitive c function for speed"
 
-    |f|
+    |f bytes offset|
 
-    aStream isExternalStream ifFalse:[^ false].
-    f := aStream filePointer.
-    f isNil ifTrue:[^ false].
+    aStream isExternalStream ifTrue:[
+	f := aStream filePointer.
+	f isNil ifTrue:[^ false].
+    ] ifFalse:[
+	aStream isInternalByteStream ifTrue:[
+	    bytes := aStream collection.
+	    bytes isNil ifTrue:[^ false].
+	    offset := aStream position.
+	] ifFalse:[
+	    ^ false
+	]
+    ].
 %{
+    FILE *__f = NULL;
+    char *__cp = NULL;
+
+    if (f != nil) {
+	__f = __FILEVal(f);
+    } else {
+	__cp = __stringVal(bytes);
+	__cp += __intVal(offset);
+    }
+
     if (! (__bothSmallInteger(width, height)
 	   && __isByteArray(aByteArray))) {
 	RETURN (false);
     }
     if (loadBMP1to8(__intVal(width), __intVal(height),
-		 __FILEVal(f),
-		__ByteArrayInstPtr(aByteArray)->ba_element,
-		__byteArraySize(aByteArray) )) {
+		    __f, __cp,
+		    __ByteArrayInstPtr(aByteArray)->ba_element,
+		    __byteArraySize(aByteArray) )) {
 	RETURN (true);
     }
     RETURN (false);
@@ -1511,20 +1544,39 @@
     "load bmp-2 bit per pixel imagedata. A helper for BMP image reader.
      Calls primitive c function for speed"
 
-    |f|
+    |f bytes offset|
 
-    aStream isExternalStream ifFalse:[^ false].
-    f := aStream filePointer.
-    f isNil ifTrue:[^ false].
+    aStream isExternalStream ifTrue:[
+	f := aStream filePointer.
+	f isNil ifTrue:[^ false].
+    ] ifFalse:[
+	aStream isInternalByteStream ifTrue:[
+	    bytes := aStream collection.
+	    bytes isNil ifTrue:[^ false].
+	    offset := aStream position.
+	] ifFalse:[
+	    ^ false
+	]
+    ].
 %{
+    FILE *__f = NULL;
+    char *__cp = NULL;
+
+    if (f != nil) {
+	__f = __FILEVal(f);
+    } else {
+	__cp = __stringVal(bytes);
+	__cp += __intVal(offset);
+    }
+
     if (! (__bothSmallInteger(width, height)
 	   && __isByteArray(aByteArray))) {
 	RETURN (false);
     }
     if (loadBMP2to8(__intVal(width), __intVal(height),
-	     __FILEVal(f),
-	     __ByteArrayInstPtr(aByteArray)->ba_element,
-	     __byteArraySize(aByteArray) )) {
+		    __f, __cp,
+		    __ByteArrayInstPtr(aByteArray)->ba_element,
+		    __byteArraySize(aByteArray) )) {
 	RETURN (true);
     }
     RETURN (false);
@@ -1537,21 +1589,40 @@
     "load bmp-4 bit per pixel imagedata. A helper for BMP image reader.
      Calls primitive c function for speed"
 
-    |f|
+    |f bytes offset|
 
-    aStream isExternalStream ifFalse:[^ false].
-    f := aStream filePointer.
-    f isNil ifTrue:[^ false].
+    aStream isExternalStream ifTrue:[
+	f := aStream filePointer.
+	f isNil ifTrue:[^ false].
+    ] ifFalse:[
+	aStream isInternalByteStream ifTrue:[
+	    bytes := aStream collection.
+	    bytes isNil ifTrue:[^ false].
+	    offset := aStream position.
+	] ifFalse:[
+	    ^ false
+	]
+    ].
 %{
+    FILE *__f = NULL;
+    char *__cp = NULL;
+
+    if (f != nil) {
+	__f = __FILEVal(f);
+    } else {
+	__cp = __stringVal(bytes);
+	__cp += __intVal(offset);
+    }
+
     if (! (__bothSmallInteger(width, height)
 	   && __isSmallInteger(compression)
 	   && __isByteArray(aByteArray))) {
 	RETURN (false);
     }
     if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression),
-	     __FILEVal(f),
-	     __ByteArrayInstPtr(aByteArray)->ba_element,
-	     __byteArraySize(aByteArray) )) {
+		    __f, __cp,
+		    __ByteArrayInstPtr(aByteArray)->ba_element,
+		    __byteArraySize(aByteArray) )) {
 	RETURN (true);
     }
     RETURN (false);
@@ -1564,21 +1635,40 @@
     "load bmp-8 bit per pixel imagedata. A helper for BMP image reader.
      Calls primitive c function for speed"
 
-|f|
+    |f bytes offset|
 
-    aStream isExternalStream ifFalse:[^ false].
-    f := aStream filePointer.
-    f isNil ifTrue:[^ false].
+    aStream isExternalStream ifTrue:[
+	f := aStream filePointer.
+	f isNil ifTrue:[^ false].
+    ] ifFalse:[
+	aStream isInternalByteStream ifTrue:[
+	    bytes := aStream collection.
+	    bytes isNil ifTrue:[^ false].
+	    offset := aStream position.
+	] ifFalse:[
+	    ^ false
+	]
+    ].
 %{
+    FILE *__f = NULL;
+    char *__cp = NULL;
+
+    if (f != nil) {
+	__f = __FILEVal(f);
+    } else {
+	__cp = __stringVal(bytes);
+	__cp += __intVal(offset);
+    }
+
     if (! (__bothSmallInteger(width, height)
 	   && __isSmallInteger(compression)
 	   && __isByteArray(aByteArray))) {
 	RETURN (false);
     }
     if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression),
-	     __FILEVal(f),
-	     __ByteArrayInstPtr(aByteArray)->ba_element,
-	     __byteArraySize(aByteArray) )) {
+		    __f, __cp,
+		    __ByteArrayInstPtr(aByteArray)->ba_element,
+		    __byteArraySize(aByteArray) )) {
 	RETURN (true);
     }
     RETURN (false);
@@ -2329,5 +2419,5 @@
 !ImageReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.91 2007-01-24 13:36:53 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.92 2007-05-30 14:43:54 cg Exp $'
 ! !