ImageReader.st
changeset 3243 26e0680294a0
parent 2646 86e3f7203efb
child 3260 e4e8e2b99900
--- a/ImageReader.st	Wed Jul 26 16:48:43 2000 +0200
+++ b/ImageReader.st	Fri Jul 28 13:54:58 2000 +0200
@@ -268,19 +268,10 @@
     whiteShiftTable = (char *) malloc(sizeof(char) * 8192);
     if (! whiteShiftTable) {
 	goto fail1;
-/*
-	free(whiteCountTable); whiteCountTable = (short *)0;
-	return;
-*/
     }
     blackCountTable = (short *) malloc(sizeof(short) * 8192);
     if (! blackCountTable) {
 	goto fail2;
-/*
-	free(whiteShiftTable); whiteShiftTable = (char *)0;
-	free(whiteCountTable); whiteCountTable = (short *)0;
-	return;
-*/
     }
     blackShiftTable = (char *) malloc(sizeof(char) * 8192);
     if (! blackShiftTable) {
@@ -843,6 +834,8 @@
 
 /*
  * BMP file read/decompression
+ *
+ * depth1 to 8bit ...
  */
 static int
 loadBMP1to8(w, h, fp, dest, szDest)
@@ -862,12 +855,19 @@
 	for (j=bitnum=0; j<padw; j++,bitnum++) {
 	    if ((bitnum&7) == 0) { /* read the next byte */
 		c = getc(fp);
-		if (c == EOF) return 0;
+		if (c == EOF) {
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: premature EOF\n"); 
+		    }
+		    return 0;
+		}
 		bitnum = 0;
 	    }
 	    if (j<w) {
 		if (pp >= (dest+szDest)) {
-		    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+		    }
 		    return 0;
 		}
 		*pp++ = (c & 0x80) ? 1 : 0;
@@ -880,6 +880,9 @@
     return 1;
 }
 
+/*
+ * depth2 to 8bit ...
+ */
 static int
 loadBMP2to8(w, h, fp, dest, szDest)
     int w, h;
@@ -903,7 +906,9 @@
 	    }
 	    if (j<w) {
 		if (pp >= (dest+szDest)) {
-		    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+		    }
 		    return 0;
 		}
 		*pp++ = (c & 0xC0) >> 6;
@@ -916,6 +921,9 @@
     return 1;
 }
 
+/*
+ * depth4 to 8bit ...
+ */
 static int
 loadBMP4to8(w, h, comp, fp, dest, szDest)
     int w, h;
@@ -943,7 +951,9 @@
 
 		if (j<w) {
 		    if (pp >= (dest+szDest)) {
-			fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+			if (@global(InfoPrinting) == true) {
+			    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+			}
 			return 0;
 		    }
 		    *pp++ = (c & 0xf0) >> 4;
@@ -952,70 +962,78 @@
 	    }
 	    if (ferror(fp)) return 0;
 	}
-    } else {
-	if (comp == 2) {  /* read RLE4 compressed data */
-	    x = y = 0;  
-	    pp = dest + x + (h-y-1)*w;
+	if (ferror(fp)) return 0;
+	return 1;
+    }
+    if (comp == 2) {  /* read RLE4 compressed data */
+	x = y = 0;  
+	pp = dest + x + (h-y-1)*w;
 
-	    while (y<h) {
-		c = getc(fp); if (c == EOF) return 0;
+	while (y<h) {
+	    c = getc(fp); if (c == EOF) return 0;
 
-		if (c) {                                   /* encoded mode */
-		    c &= 0xFF;
-		    c1 = getc(fp);
-		    if (c1 == EOF) return 0;
-		    for (i=0; i<c; i++,x++,pp++) {
-			if (pp >= (dest+szDest)) {
+	    if (c) {                                   /* encoded mode */
+		c &= 0xFF;
+		c1 = getc(fp);
+		if (c1 == EOF) return 0;
+		for (i=0; i<c; i++,x++,pp++) {
+		    if (pp >= (dest+szDest)) {
+			if (@global(InfoPrinting) == true) {
 			    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
-			    return 0;
 			}
-			*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
+			return 0;
 		    }
-		} else {    
-		    /* c==0x00  :  escape codes */
-		    c = getc(fp);  
-		    if (c == EOF) return 0;
+		    *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
+		}
+	    } else {    
+		/* c==0x00  :  escape codes */
+		c = getc(fp);  
+		if (c == EOF) return 0;
 
-		    if (c == 0x00) {                    /* end of line */
-			x=0;  y++;  pp = dest + x + (h-y-1)*w;
-		    } else 
-			if (c == 0x01) break;               /* end of pic8 */
+		if (c == 0x00) {                    /* end of line */
+		    x=0;  y++;  pp = dest + x + (h-y-1)*w;
+		} else 
+		    if (c == 0x01) break;               /* end of pic8 */
 
-			else if (c == 0x02) {                /* delta */
-			    c = getc(fp);  
-			    if (c == EOF) return 0;
-			    x += (c & 0xFF);
-			    c = getc(fp);  
-			    if (c == EOF) return 0;
-			    y += (c & 0xFF);
-			    pp = dest + x + (h-y-1)*w;
-			} else {                        /* absolute mode */
-			    c &= 0xFF;
-			    for (i=0; i<c; i++, x++, pp++) {
-				if ((i&1) == 0) {
-				    c1 = getc(fp);
-				    if (c1 == EOF) return 0;
-				}
-				if (pp >= (dest+szDest)) {
+		    else if (c == 0x02) {                /* delta */
+			c = getc(fp);  
+			if (c == EOF) return 0;
+			x += (c & 0xFF);
+			c = getc(fp);  
+			if (c == EOF) return 0;
+			y += (c & 0xFF);
+			pp = dest + x + (h-y-1)*w;
+		    } else {                        /* absolute mode */
+			c &= 0xFF;
+			for (i=0; i<c; i++, x++, pp++) {
+			    if ((i&1) == 0) {
+				c1 = getc(fp);
+				if (c1 == EOF) return 0;
+			    }
+			    if (pp >= (dest+szDest)) {
+				if (@global(InfoPrinting) == true) {
 				    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
-				    return 0;
 				}
-				*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
+				return 0;
 			    }
-
-			    if (((c&3)==1) || ((c&3)==2)) getc(fp);  /* read pad byte */
+			    *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
 			}
-		}  /* escape processing */
-		if (ferror(fp)) return 0;
-	    }  /* while */
-	} else {
-	    return 0;
-	}
+
+			if (((c&3)==1) || ((c&3)==2)) getc(fp);  /* read pad byte */
+		    }
+	    }  /* escape processing */
+	    if (ferror(fp)) return 0;
+	}  /* while */
+	if (ferror(fp)) return 0;
+	return 1;
     }
-    if (ferror(fp)) return 0;
-    return 1;
+
+    return 0;  /* bad comp */
 }
 
+/*
+ * depth8 to 8bit ...
+ */
 static int
 loadBMP8(w, h, comp, fp, dest, szDest)
     int w, h;
@@ -1034,79 +1052,136 @@
 
 	    for (j=0; j<padw; j++) {
 		c = getc(fp);  
-		if (c==EOF) return 0;
-
+		if (c==EOF) {
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: BMP premature EOF [%d]\n", __LINE__);
+			fprintf(stderr, "h=%d i=%d w=%d j=%d padw=%d\n", h, i, w, j, padw);
+		    }
+		    return 0;
+		}
 		if (j<w) {
 		    if (pp >= (dest+szDest)) {
-			fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+			if (@global(InfoPrinting) == true) {
+			    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+			}
 			return 0;
 		    }
 		    *pp++ = c;
 		}
 	    }
-	    if (ferror(fp)) return 0;
+	    if (ferror(fp)) {
+		if (@global(InfoPrinting) == true) {
+		    fprintf(stderr, "ImageReader [warning]: BMP ferror set\n");
+		}
+		return 0;
+	    }
+	}
+	if (ferror(fp)) {
+	    if (@global(InfoPrinting) == true) {
+		fprintf(stderr, "ImageReader [warning]: BMP ferror set at end\n");
+	    }
+	    return 0;
 	}
-    } else {
-	if (comp == 1) {  /* RLE8 compressed */
-	    x = y = 0;  
-	    pp = dest + x + (h-y-1)*w;
+	return 1;
+    }
+    if (comp == 1) {  /* RLE8 compressed */
+	x = y = 0;  
+	pp = dest + x + (h-y-1)*w;
 
-	    while (y<h) {
+	while (y<h) {
+	    c = getc(fp);  
+	    if (c == EOF) {
+		if (@global(InfoPrinting) == true) {
+		    fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
+		}
+		return 0;
+	    }
+	    if (c) {                                   /* encoded mode */
+		c1 = getc(fp);
+		if (c1 == EOF) {
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
+		    }
+		    return 0;
+		}
+		c &= 0xFF;
+
+		for (i=0; i<c; i++,x++,pp++) {
+		    if (pp >= (dest+szDest)) {
+			if (@global(InfoPrinting) == true) {
+			    fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n");
+			}
+			return 0;
+		    }
+		    *pp = c1;
+		}
+	    } else {    
+		/* c==0x00  :  escape codes */
 		c = getc(fp);  
-		if (c == EOF) return 0;
-
-		if (c) {                                   /* encoded mode */
-		    c1 = getc(fp);
-		    if (c1 == EOF) return 0;
-		    c &= 0xFF;
-
-		    for (i=0; i<c; i++,x++,pp++) {
-			if (pp >= (dest+szDest)) {
-			    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
+		if (c == EOF) {
+		    if (@global(InfoPrinting) == true) {
+			fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
+		    }
+		    return 0;
+		}
+		if (c == 0x00) {                    /* end of line */
+		    x=0;  y++;  pp = dest + x + (h-y-1)*w;
+		} else 
+		    if (c == 0x01) break;               /* end of pic8 */
+		    else if (c == 0x02) {               /* delta */
+			c = getc(fp);
+			if (c == EOF) {
+			    if (@global(InfoPrinting) == true) {
+				fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
+			    }
 			    return 0;
 			}
-			*pp = c1;
-		    }
-		} else {    
-		    /* c==0x00  :  escape codes */
-		    c = getc(fp);  
-		    if (c == EOF) return 0;
+			x += (c & 0xFF);
+			c = getc(fp);  
+			if (c == EOF) {
+			    if (@global(InfoPrinting) == true) {
+				fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__);
+			    }
+			    return 0;
+			}
+			y += (c & 0xFF);
+			pp = dest + x + (h-y-1)*w;
+		    } else {                            /* absolute mode */
+			c &= 0xFF;
+			for (i=0; i<c; i++, x++, pp++) {
+			    c1 = getc(fp);
+			    if (c1 == EOF) return 0;
+			    if (pp >= (dest+szDest)) {
+				if (@global(InfoPrinting) == true) {
+				    fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n");
+				}
+				return 0;
+			    }
+			    *pp = c1;
+			}
 
-		    if (c == 0x00) {                    /* end of line */
-			x=0;  y++;  pp = dest + x + (h-y-1)*w;
-		    } else 
-			if (c == 0x01) break;               /* end of pic8 */
-			else if (c == 0x02) {               /* delta */
-			    c = getc(fp);
-			    if (c == EOF) return 0;
-			    x += (c & 0xFF);
-			    c = getc(fp);  
-			    if (c == EOF) return 0;
-			    y += (c & 0xFF);
-			    pp = dest + x + (h-y-1)*w;
-			} else {                            /* absolute mode */
-			    c &= 0xFF;
-			    for (i=0; i<c; i++, x++, pp++) {
-				c1 = getc(fp);
-				if (c1 == EOF) return 0;
-				if (pp >= (dest+szDest)) {
-				    fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n");
-				    return 0;
-				}
-				*pp = c1;
-			    }
-
-			    if (c & 1) getc(fp);  /* odd length run: read an extra pad byte */
-			}
-		}  
-		if (ferror(fp)) return 0;
-	    } 
-	} else {
+			if (c & 1) getc(fp);  /* odd length run: read an extra pad byte */
+		    }
+	    }  
+	    if (ferror(fp)) {
+		if (@global(InfoPrinting) == true) {
+		    fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set\n");
+		}
+		return 0;
+	    }
+	} 
+	if (ferror(fp)) {
+	    if (@global(InfoPrinting) == true) {
+		fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set at end\n");
+	    }
 	    return 0;
 	}
+	return 1;
     }
-    if (ferror(fp)) return 0;
-    return 1;
+    if (@global(InfoPrinting) == true) {
+	fprintf(stderr, "ImageReader [warning]: BMP unhandled compression: %d\n", comp);
+    }
+    return 0;
 }
 
 %}
@@ -2068,5 +2143,5 @@
 !ImageReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.69 1999-05-06 11:18:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.70 2000-07-28 11:54:58 cg Exp $'
 ! !