made buildMask public and a class method (functionality also needed by JavaVM)
authorClaus Gittinger <cg@exept.de>
Fri, 16 Jan 1998 16:09:34 +0100
changeset 1983 03a93138c52a
parent 1982 55d0357449c2
child 1984 e34624152bf3
made buildMask public and a class method (functionality also needed by JavaVM)
ImageRdr.st
ImageReader.st
--- a/ImageRdr.st	Fri Jan 16 16:07:44 1998 +0100
+++ b/ImageRdr.st	Fri Jan 16 16:09:34 1998 +0100
@@ -468,7 +468,7 @@
     int i;
     int len;
     int codeLen = 9;
-    int ret = 1;	/* return success */
+    int ret = 1;        /* return success */
 
     scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer));
     if (! scratchBuffer) return 0;
@@ -511,7 +511,7 @@
 	code = (bits >> shift) & mask;
 	bits &= ~(mask << shift);
 	nBits -= codeLen;
-			
+                        
 	if (code == 257) break;
 	if (code == 256) {
 	    if (! inCount)
@@ -554,12 +554,12 @@
 		    /* add( string[oldcode] + first(string[code]) ) */
 		    len = stringLen[oldCode] + 1;
 		    if (nScratch < len) {
-		        newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
-		        if (! newBuffer) goto out;
-		        newBuffer->prev = scratchBuffer;
-		        scratchBuffer = newBuffer;
-		        scratchPtr = scratchBuffer->chars;
-		        nScratch = sizeof(scratchBuffer->chars);
+			newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
+			if (! newBuffer) goto out;
+			newBuffer->prev = scratchBuffer;
+			scratchBuffer = newBuffer;
+			scratchPtr = scratchBuffer->chars;
+			nScratch = sizeof(scratchBuffer->chars);
 		    }
 		    stringLen[nextCode] = len;
 		    strings[nextCode] = scratchPtr;
@@ -567,15 +567,15 @@
 		    scratchPtr += len-1;
 		    *scratchPtr++ = strings[code][0];
 		    nScratch -= len;
-	            nextCode++;
+		    nextCode++;
 		}
 	    } else {
 		if (oldCode == -1) {
 		    /* bad input */
 		    ret = 0;
 		    goto out;
-	        }
-	    
+		}
+            
 		/* writeString(string[oldCode] + first(string[oldCode]) ) */
 		len = stringLen[oldCode];
 		bcopy(strings[oldCode], to, len);
@@ -598,7 +598,7 @@
 		scratchPtr += len-1;
 		*scratchPtr++ = strings[oldCode][0];
 		nScratch -= len;
-	        nextCode++;
+		nextCode++;
 	    }
 	    oldCode = code;
 	    if (nextCode >= 511)
@@ -676,6 +676,10 @@
     static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64,
 			   128, 256, 512, 1024, 2048 };
 
+    if ((unsigned)codeLen > 12) {
+	fprintf(stderr, "ImageReader [warning]: bad codelen in gif-decode\n");
+	return 0;
+    }
     prefix = (unsigned short *)malloc(sizeof(short) * 4096);
     if (! prefix) return 0;
     suffix  = (unsigned short *)malloc(sizeof(short) * 4096);
@@ -1008,7 +1012,7 @@
     ImageReaders are created temporary to read an image from a stream.
     Normally, they are not directly used - instead, the image class is
     asked to read some file, and return an instance for it:
-        Image fromFile:<someFileName>
+	Image fromFile:<someFileName>
     The Image class will guess the images format and forward the task to
     some concrete ImageReaderClass.
     If that class finds, that the files format is incorrect, other readers
@@ -1020,9 +1024,9 @@
 
     See the implementation of #fromStream: in concrete subclasses.
     The public interfaces are:
-         <ConcreteReaderClass> fromFile:aFilename
+	 <ConcreteReaderClass> fromFile:aFilename
     or:
-         <ConcreteReaderClass> fromStream:aStream
+	 <ConcreteReaderClass> fromStream:aStream
 
     If you add a new reader, dont forget to add the method #isValidImageFile:
     which should return true, if this reader supports reading a given file.
@@ -1030,10 +1034,10 @@
     #canRepresent:anImage and return true from this method.
 
     [See also:]
-        Image Icon Form
+	Image Icon Form
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 "
 ! !
 
@@ -1187,39 +1191,39 @@
     srcIdx := srcStartIndex.
     dstIdx := dstStartIndex.
     [true] whileTrue:[
-        pixel := srcBytes at:srcIdx.
-        count := pixel bitAnd:16r7F.
-        count == 0 ifTrue:[
-            ^ self
-        ].
-        srcIdx := srcIdx + 1.
-        (pixel bitAnd:16r80) == 0 ifTrue:[
-            "/ run bytes
-            pixel := srcBytes at:srcIdx.
-            srcIdx := srcIdx + 1.
-            dstIncrement == 1 ifTrue:[
-                dstBytes from:dstIdx to:(dstIdx+count-1) put:pixel.
-                dstIdx := dstIdx + count.
-            ] ifFalse:[
-                1 to:count do:[:c |
-                    dstBytes at:dstIdx put:pixel.
-                    dstIdx := dstIdx + dstIncrement.
-                ]
-            ]
-        ] ifFalse:[
-            "/ verbatim bytes
-            dstIncrement == 1 ifTrue:[
-                dstBytes replaceFrom:dstIdx to:(dstIdx+count-1) with:srcBytes startingAt:srcIdx.
-                srcIdx := srcIdx + count.
-                dstIdx := dstIdx + count.
-            ] ifFalse:[
-                1 to:count do:[:c |
-                    dstBytes at:dstIdx put:(srcBytes at:srcIdx).
-                    srcIdx := srcIdx + 1.
-                    dstIdx := dstIdx + dstIncrement.
-                ]
-            ]
-        ]
+	pixel := srcBytes at:srcIdx.
+	count := pixel bitAnd:16r7F.
+	count == 0 ifTrue:[
+	    ^ self
+	].
+	srcIdx := srcIdx + 1.
+	(pixel bitAnd:16r80) == 0 ifTrue:[
+	    "/ run bytes
+	    pixel := srcBytes at:srcIdx.
+	    srcIdx := srcIdx + 1.
+	    dstIncrement == 1 ifTrue:[
+		dstBytes from:dstIdx to:(dstIdx+count-1) put:pixel.
+		dstIdx := dstIdx + count.
+	    ] ifFalse:[
+		1 to:count do:[:c |
+		    dstBytes at:dstIdx put:pixel.
+		    dstIdx := dstIdx + dstIncrement.
+		]
+	    ]
+	] ifFalse:[
+	    "/ verbatim bytes
+	    dstIncrement == 1 ifTrue:[
+		dstBytes replaceFrom:dstIdx to:(dstIdx+count-1) with:srcBytes startingAt:srcIdx.
+		srcIdx := srcIdx + count.
+		dstIdx := dstIdx + count.
+	    ] ifFalse:[
+		1 to:count do:[:c |
+		    dstBytes at:dstIdx put:(srcBytes at:srcIdx).
+		    srcIdx := srcIdx + 1.
+		    dstIdx := dstIdx + dstIncrement.
+		]
+	    ]
+	]
     ].
 
     "Modified: 23.4.1997 / 18:54:05 / cg"
@@ -1236,12 +1240,12 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP1to8(__intVal(width), __intVal(height), 
-                 __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+		 __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1260,12 +1264,12 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP2to8(__intVal(width), __intVal(height), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1284,13 +1288,13 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isSmallInteger(compression)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isSmallInteger(compression)
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1309,13 +1313,13 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isSmallInteger(compression)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isSmallInteger(compression)
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1323,6 +1327,110 @@
     "Modified: 22.4.1996 / 19:14:54 / cg"
 ! !
 
+!ImageReader class methodsFor:'image support'!
+
+buildMaskFromColor:maskPixelValue for:pixels width:width height:height
+    "helper for image formats, where an individual pixel value
+     has been defined as a mask-pixel (i.e. GIF).
+     Creates a maskImage, with zeros at positions where the image
+     has the given pixelValue; all other mask pixels are set to 1."
+
+    |maskArray bytesPerMaskRow mask|
+
+    bytesPerMaskRow := (width+7) // 8.
+
+    maskArray := ByteArray uninitializedNew:bytesPerMaskRow * height.
+
+%{
+    int __w = __intVal(width);
+    int __h = __intVal(height);
+    int __x, __y;
+    int __outBits, __nOut;
+    unsigned char *__inP, *__outP, *__nextOutRow;
+    int __bpr = __intVal(bytesPerMaskRow);
+    int __maskPixel = __intVal(maskPixelValue);
+#ifdef DEBUG
+    unsigned char *outEnd;
+#endif
+
+    if (! __isByteArray(pixels)) goto fail;
+    if (! __isByteArray(maskArray)) goto fail;
+
+    __inP = __ByteArrayInstPtr(pixels)->ba_element;
+    __outP = __ByteArrayInstPtr(maskArray)->ba_element;
+
+#ifdef DEBUG
+    outEnd = __outP + (__byteArraySize(maskArray));
+#endif
+/*
+printf("outP: %x outEnd: %x sz: %d\n", __outP, outEnd, __byteArraySize(maskArray));
+*/
+
+    for (__y=__h; __y>0; __y--) {
+	__outBits = 0;
+	__nOut = 8;
+	__nextOutRow = __outP + __bpr;
+
+	for (__x=__w; __x>=8; __x-=8) {
+	    if (__inP[0] != __maskPixel) { __outBits |= 0x80; };
+	    if (__inP[1] != __maskPixel) { __outBits |= 0x40; };
+	    if (__inP[2] != __maskPixel) { __outBits |= 0x20; };
+	    if (__inP[3] != __maskPixel) { __outBits |= 0x10; };
+	    if (__inP[4] != __maskPixel) { __outBits |= 0x08; };
+	    if (__inP[5] != __maskPixel) { __outBits |= 0x04; };
+	    if (__inP[6] != __maskPixel) { __outBits |= 0x02; };
+	    if (__inP[7] != __maskPixel) { __outBits |= 0x01; };
+	    __inP += 8;
+	    *__outP++ = __outBits;
+	    __outBits = 0;
+	}
+	for (; __x>0; __x--) {
+	    __outBits <<= 1;
+	    if (*__inP != __maskPixel) {
+		__outBits |= 1;
+	    }
+	    __inP++;
+
+/*
+ printf("x: %d  bits: %x\n", __x, __outBits);
+*/
+	    if (--__nOut == 0) {
+#ifdef DEBUG
+		if (__outP >= outEnd) {
+		 printf("oops\n");
+		 goto fail;
+		}
+#endif
+		*__outP = __outBits;
+
+		__outP++;
+		__nOut = 8;
+		__outBits = 0;
+	    }
+	}
+
+	if (__nOut != 8) {
+	    __outBits <<= __nOut;
+
+#ifdef DEBUG
+	    if (__outP >= outEnd) {
+	     printf("oops2\n");
+	     goto fail;
+	    }
+#endif
+	    *__outP = __outBits;
+
+	}
+	__outP = __nextOutRow;
+    }
+fail: ;
+%}.
+    mask := ImageMask width:width height:height fromArray:maskArray.
+    ^ mask
+
+    "Created: 21.6.1996 / 11:43:47 / cg"
+! !
+
 !ImageReader class methodsFor:'i/o support'!
 
 streamReadingFile:aFilename
@@ -1335,16 +1443,16 @@
 
     name := aFilename asString.
     ((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[
-        fn := Smalltalk getSystemFileName:name.
-        inStream := PipeStream readingFrom:'gunzip < ' , fn.
+	fn := Smalltalk getSystemFileName:name.
+	inStream := PipeStream readingFrom:'gunzip < ' , fn.
     ] ifFalse:[
-        inStream := Smalltalk systemFileStreamFor:aFilename.
-        inStream isNil ifTrue:[
-            inStream := Smalltalk bitmapFileStreamFor:aFilename
-        ]
+	inStream := Smalltalk systemFileStreamFor:aFilename.
+	inStream isNil ifTrue:[
+	    inStream := Smalltalk bitmapFileStreamFor:aFilename
+	]
     ].
     inStream isNil ifTrue:[
-        'ImageReader [warning]: open error on: ' infoPrint. aFilename infoPrintCR. 
+	'ImageReader [warning]: open error on: ' infoPrint. aFilename infoPrintCR. 
     ].
     ^ inStream
 
@@ -1361,7 +1469,7 @@
 
     reader := self readFile:aFileName.
     reader notNil ifTrue:[
-        ^ reader image
+	^ reader image
     ].
     ^ nil
 
@@ -1383,7 +1491,7 @@
 
     reader := self new fromStream:aStream.
     reader notNil ifTrue:[
-        ^ reader image
+	^ reader image
     ].
     ^ nil
 
@@ -1399,7 +1507,7 @@
 
     reader := self readFile:aFileName.
     reader notNil ifTrue:[
-        ^ reader images
+	^ reader images
     ].
     ^ nil
 
@@ -1416,7 +1524,7 @@
 
     reader := self new fromStream:aStream.
     reader notNil ifTrue:[
-        ^ reader images
+	^ reader images
     ].
     ^ nil
 
@@ -1431,8 +1539,8 @@
 
     inStream := self streamReadingFile:aFileName.
     inStream isNil ifTrue:[
-        'ImageReader [warning]: file open error' errorPrintCR.
-        ^ nil
+	'ImageReader [warning]: file open error' errorPrintCR.
+	^ nil
     ].
     reader := self new fromStream:inStream.
     inStream close.
@@ -1513,7 +1621,7 @@
     bitsPerRow := width * (self bitsPerPixel).
     bytesPerRow := bitsPerRow // 8.
     ((bitsPerRow \\ 8) ~~ 0) ifTrue:[
-        bytesPerRow := bytesPerRow + 1
+	bytesPerRow := bytesPerRow + 1
     ].
     ^ bytesPerRow
 
@@ -1568,27 +1676,24 @@
     |image depth|
 
     imageSequence size > 0 ifTrue:[
-        ^  self images first.
+	^  self images first.
     ].
 
     depth := self bitsPerPixel.
     image := (Image implementorForDepth:depth) new.
     image depth:depth.
-
-    "be carefull when reading from non-fileStreams"
-    (inStream respondsTo:#fileName) ifTrue:[
-        image fileName:inStream pathName.
+    inStream isFileStream ifTrue:[
+	image fileName:inStream pathName.
     ].
-
     image 
-        width:width 
-        height:height
-        photometric:photometric
-        samplesPerPixel:samplesPerPixel
-        bitsPerSample:bitsPerSample
-        colorMap:colorMap
-        bits:data
-        mask:mask.
+	width:width 
+	height:height
+	photometric:photometric
+	samplesPerPixel:samplesPerPixel
+	bitsPerSample:bitsPerSample
+	colorMap:colorMap
+	bits:data
+	mask:mask.
     ^ image
 
     "Modified: / 3.11.1997 / 14:55:00 / cg"
@@ -1606,12 +1711,12 @@
     |img|
 
     imageSequence notNil ifTrue:[
-        ^ imageSequence
+	^ imageSequence
     ].
 
     img := self image.
     img isNil ifTrue:[
-        ^ img
+	^ img
     ].
     ^ Array with:img
 
@@ -1635,7 +1740,7 @@
     |n|
 
     (n := imageSequence size) > 0 ifTrue:[
-        ^ n
+	^ n
     ].
     ^ 1
 
@@ -1733,7 +1838,7 @@
     outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb)
 ! !
 
-!ImageReader methodsFor:'image reading'!
+!ImageReader methodsFor:'image reading support'!
 
 buildMaskFromColor:maskPixelValue
     "helper for image formats, where an individual pixel value
@@ -1741,101 +1846,16 @@
      Creates a maskImage, with zeros at positions where the image
      has the given pixelValue; all other mask pixels are set to 1."
 
-    |maskArray bytesPerMaskRow|
-
-    bytesPerMaskRow := (width+7) // 8.
-
-    maskArray := ByteArray uninitializedNew:bytesPerMaskRow * height.
-
-%{
-    int __w = __intVal(__INST(width));
-    int __h = __intVal(__INST(height));
-    int __x, __y;
-    int __outBits, __nOut;
-    unsigned char *__inP, *__outP, *__nextOutRow;
-    int __bpr = __intVal(bytesPerMaskRow);
-    int __maskPixel = __intVal(maskPixelValue);
-#ifdef DEBUG
-    unsigned char *outEnd;
-#endif
-
-    if (! __isByteArray(__INST(data))) goto fail;
-    if (! __isByteArray(maskArray)) goto fail;
-
-    __inP = __ByteArrayInstPtr(__INST(data))->ba_element;
-    __outP = __ByteArrayInstPtr(maskArray)->ba_element;
-
-#ifdef DEBUG
-    outEnd = __outP + (__byteArraySize(maskArray));
-#endif
-/*
-printf("outP: %x outEnd: %x sz: %d\n", __outP, outEnd, __byteArraySize(maskArray));
-*/
-
-    for (__y=__h; __y>0; __y--) {
-        __outBits = 0;
-        __nOut = 8;
-        __nextOutRow = __outP + __bpr;
+    mask := self class 
+		buildMaskFromColor:maskPixelValue 
+		for:data
+		width:width
+		height:height
 
-        for (__x=__w; __x>=8; __x-=8) {
-            if (__inP[0] != __maskPixel) { __outBits |= 0x80; };
-            if (__inP[1] != __maskPixel) { __outBits |= 0x40; };
-            if (__inP[2] != __maskPixel) { __outBits |= 0x20; };
-            if (__inP[3] != __maskPixel) { __outBits |= 0x10; };
-            if (__inP[4] != __maskPixel) { __outBits |= 0x08; };
-            if (__inP[5] != __maskPixel) { __outBits |= 0x04; };
-            if (__inP[6] != __maskPixel) { __outBits |= 0x02; };
-            if (__inP[7] != __maskPixel) { __outBits |= 0x01; };
-            __inP += 8;
-            *__outP++ = __outBits;
-            __outBits = 0;
-        }
-        for (; __x>0; __x--) {
-            __outBits <<= 1;
-            if (*__inP != __maskPixel) {
-                __outBits |= 1;
-            }
-            __inP++;
+    "Modified: / 14.1.1998 / 14:48:33 / cg"
+! !
 
-/*
- printf("x: %d  bits: %x\n", __x, __outBits);
-*/
-            if (--__nOut == 0) {
-#ifdef DEBUG
-                if (__outP >= outEnd) {
-                 printf("oops\n");
-                 goto fail;
-                }
-#endif
-                *__outP = __outBits;
-
-                __outP++;
-                __nOut = 8;
-                __outBits = 0;
-            }
-        }
-
-        if (__nOut != 8) {
-            __outBits <<= __nOut;
-
-#ifdef DEBUG
-            if (__outP >= outEnd) {
-             printf("oops2\n");
-             goto fail;
-            }
-#endif
-            *__outP = __outBits;
-
-        }
-        __outP = __nextOutRow;
-    }
-fail: ;
-%}.
-    mask := ImageMask width:width height:height fromArray:maskArray.
-
-    "Created: 21.6.1996 / 11:43:47 / cg"
-
-!
+!ImageReader methodsFor:'image reading'!
 
 fromStream:aStream
     "read an image in my format from aStream.
@@ -1853,8 +1873,8 @@
     "save image in my format on aFile"
 
     ^ Image cannotRepresentImageSignal 
-        raiseWith:image
-        errorString:('image save not implemented for this format').
+	raiseWith:image
+	errorString:('image save not implemented for this format').
 
     "Modified: 10.4.1997 / 17:43:52 / cg"
 ! !
@@ -1869,5 +1889,5 @@
 !ImageReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Attic/ImageRdr.st,v 1.56 1998-01-15 15:38:11 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Attic/ImageRdr.st,v 1.57 1998-01-16 15:09:34 cg Exp $'
 ! !
--- a/ImageReader.st	Fri Jan 16 16:07:44 1998 +0100
+++ b/ImageReader.st	Fri Jan 16 16:09:34 1998 +0100
@@ -468,7 +468,7 @@
     int i;
     int len;
     int codeLen = 9;
-    int ret = 1;	/* return success */
+    int ret = 1;        /* return success */
 
     scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer));
     if (! scratchBuffer) return 0;
@@ -511,7 +511,7 @@
 	code = (bits >> shift) & mask;
 	bits &= ~(mask << shift);
 	nBits -= codeLen;
-			
+                        
 	if (code == 257) break;
 	if (code == 256) {
 	    if (! inCount)
@@ -554,12 +554,12 @@
 		    /* add( string[oldcode] + first(string[code]) ) */
 		    len = stringLen[oldCode] + 1;
 		    if (nScratch < len) {
-		        newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
-		        if (! newBuffer) goto out;
-		        newBuffer->prev = scratchBuffer;
-		        scratchBuffer = newBuffer;
-		        scratchPtr = scratchBuffer->chars;
-		        nScratch = sizeof(scratchBuffer->chars);
+			newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
+			if (! newBuffer) goto out;
+			newBuffer->prev = scratchBuffer;
+			scratchBuffer = newBuffer;
+			scratchPtr = scratchBuffer->chars;
+			nScratch = sizeof(scratchBuffer->chars);
 		    }
 		    stringLen[nextCode] = len;
 		    strings[nextCode] = scratchPtr;
@@ -567,15 +567,15 @@
 		    scratchPtr += len-1;
 		    *scratchPtr++ = strings[code][0];
 		    nScratch -= len;
-	            nextCode++;
+		    nextCode++;
 		}
 	    } else {
 		if (oldCode == -1) {
 		    /* bad input */
 		    ret = 0;
 		    goto out;
-	        }
-	    
+		}
+            
 		/* writeString(string[oldCode] + first(string[oldCode]) ) */
 		len = stringLen[oldCode];
 		bcopy(strings[oldCode], to, len);
@@ -598,7 +598,7 @@
 		scratchPtr += len-1;
 		*scratchPtr++ = strings[oldCode][0];
 		nScratch -= len;
-	        nextCode++;
+		nextCode++;
 	    }
 	    oldCode = code;
 	    if (nextCode >= 511)
@@ -676,6 +676,10 @@
     static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64,
 			   128, 256, 512, 1024, 2048 };
 
+    if ((unsigned)codeLen > 12) {
+	fprintf(stderr, "ImageReader [warning]: bad codelen in gif-decode\n");
+	return 0;
+    }
     prefix = (unsigned short *)malloc(sizeof(short) * 4096);
     if (! prefix) return 0;
     suffix  = (unsigned short *)malloc(sizeof(short) * 4096);
@@ -1008,7 +1012,7 @@
     ImageReaders are created temporary to read an image from a stream.
     Normally, they are not directly used - instead, the image class is
     asked to read some file, and return an instance for it:
-        Image fromFile:<someFileName>
+	Image fromFile:<someFileName>
     The Image class will guess the images format and forward the task to
     some concrete ImageReaderClass.
     If that class finds, that the files format is incorrect, other readers
@@ -1020,9 +1024,9 @@
 
     See the implementation of #fromStream: in concrete subclasses.
     The public interfaces are:
-         <ConcreteReaderClass> fromFile:aFilename
+	 <ConcreteReaderClass> fromFile:aFilename
     or:
-         <ConcreteReaderClass> fromStream:aStream
+	 <ConcreteReaderClass> fromStream:aStream
 
     If you add a new reader, dont forget to add the method #isValidImageFile:
     which should return true, if this reader supports reading a given file.
@@ -1030,10 +1034,10 @@
     #canRepresent:anImage and return true from this method.
 
     [See also:]
-        Image Icon Form
+	Image Icon Form
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 "
 ! !
 
@@ -1187,39 +1191,39 @@
     srcIdx := srcStartIndex.
     dstIdx := dstStartIndex.
     [true] whileTrue:[
-        pixel := srcBytes at:srcIdx.
-        count := pixel bitAnd:16r7F.
-        count == 0 ifTrue:[
-            ^ self
-        ].
-        srcIdx := srcIdx + 1.
-        (pixel bitAnd:16r80) == 0 ifTrue:[
-            "/ run bytes
-            pixel := srcBytes at:srcIdx.
-            srcIdx := srcIdx + 1.
-            dstIncrement == 1 ifTrue:[
-                dstBytes from:dstIdx to:(dstIdx+count-1) put:pixel.
-                dstIdx := dstIdx + count.
-            ] ifFalse:[
-                1 to:count do:[:c |
-                    dstBytes at:dstIdx put:pixel.
-                    dstIdx := dstIdx + dstIncrement.
-                ]
-            ]
-        ] ifFalse:[
-            "/ verbatim bytes
-            dstIncrement == 1 ifTrue:[
-                dstBytes replaceFrom:dstIdx to:(dstIdx+count-1) with:srcBytes startingAt:srcIdx.
-                srcIdx := srcIdx + count.
-                dstIdx := dstIdx + count.
-            ] ifFalse:[
-                1 to:count do:[:c |
-                    dstBytes at:dstIdx put:(srcBytes at:srcIdx).
-                    srcIdx := srcIdx + 1.
-                    dstIdx := dstIdx + dstIncrement.
-                ]
-            ]
-        ]
+	pixel := srcBytes at:srcIdx.
+	count := pixel bitAnd:16r7F.
+	count == 0 ifTrue:[
+	    ^ self
+	].
+	srcIdx := srcIdx + 1.
+	(pixel bitAnd:16r80) == 0 ifTrue:[
+	    "/ run bytes
+	    pixel := srcBytes at:srcIdx.
+	    srcIdx := srcIdx + 1.
+	    dstIncrement == 1 ifTrue:[
+		dstBytes from:dstIdx to:(dstIdx+count-1) put:pixel.
+		dstIdx := dstIdx + count.
+	    ] ifFalse:[
+		1 to:count do:[:c |
+		    dstBytes at:dstIdx put:pixel.
+		    dstIdx := dstIdx + dstIncrement.
+		]
+	    ]
+	] ifFalse:[
+	    "/ verbatim bytes
+	    dstIncrement == 1 ifTrue:[
+		dstBytes replaceFrom:dstIdx to:(dstIdx+count-1) with:srcBytes startingAt:srcIdx.
+		srcIdx := srcIdx + count.
+		dstIdx := dstIdx + count.
+	    ] ifFalse:[
+		1 to:count do:[:c |
+		    dstBytes at:dstIdx put:(srcBytes at:srcIdx).
+		    srcIdx := srcIdx + 1.
+		    dstIdx := dstIdx + dstIncrement.
+		]
+	    ]
+	]
     ].
 
     "Modified: 23.4.1997 / 18:54:05 / cg"
@@ -1236,12 +1240,12 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP1to8(__intVal(width), __intVal(height), 
-                 __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+		 __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1260,12 +1264,12 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP2to8(__intVal(width), __intVal(height), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1284,13 +1288,13 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isSmallInteger(compression)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isSmallInteger(compression)
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1309,13 +1313,13 @@
     f isNil ifTrue:[^ false].
 %{
     if (! (__bothSmallInteger(width, height)
-           && __isSmallInteger(compression)
-           && __isByteArray(aByteArray))) {
-        RETURN (false);
+	   && __isSmallInteger(compression)
+	   && __isByteArray(aByteArray))) {
+	RETURN (false);
     }
     if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), 
-             __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
-        RETURN (true);
+	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
+	RETURN (true);
     }
     RETURN (false);
 %}
@@ -1323,6 +1327,110 @@
     "Modified: 22.4.1996 / 19:14:54 / cg"
 ! !
 
+!ImageReader class methodsFor:'image support'!
+
+buildMaskFromColor:maskPixelValue for:pixels width:width height:height
+    "helper for image formats, where an individual pixel value
+     has been defined as a mask-pixel (i.e. GIF).
+     Creates a maskImage, with zeros at positions where the image
+     has the given pixelValue; all other mask pixels are set to 1."
+
+    |maskArray bytesPerMaskRow mask|
+
+    bytesPerMaskRow := (width+7) // 8.
+
+    maskArray := ByteArray uninitializedNew:bytesPerMaskRow * height.
+
+%{
+    int __w = __intVal(width);
+    int __h = __intVal(height);
+    int __x, __y;
+    int __outBits, __nOut;
+    unsigned char *__inP, *__outP, *__nextOutRow;
+    int __bpr = __intVal(bytesPerMaskRow);
+    int __maskPixel = __intVal(maskPixelValue);
+#ifdef DEBUG
+    unsigned char *outEnd;
+#endif
+
+    if (! __isByteArray(pixels)) goto fail;
+    if (! __isByteArray(maskArray)) goto fail;
+
+    __inP = __ByteArrayInstPtr(pixels)->ba_element;
+    __outP = __ByteArrayInstPtr(maskArray)->ba_element;
+
+#ifdef DEBUG
+    outEnd = __outP + (__byteArraySize(maskArray));
+#endif
+/*
+printf("outP: %x outEnd: %x sz: %d\n", __outP, outEnd, __byteArraySize(maskArray));
+*/
+
+    for (__y=__h; __y>0; __y--) {
+	__outBits = 0;
+	__nOut = 8;
+	__nextOutRow = __outP + __bpr;
+
+	for (__x=__w; __x>=8; __x-=8) {
+	    if (__inP[0] != __maskPixel) { __outBits |= 0x80; };
+	    if (__inP[1] != __maskPixel) { __outBits |= 0x40; };
+	    if (__inP[2] != __maskPixel) { __outBits |= 0x20; };
+	    if (__inP[3] != __maskPixel) { __outBits |= 0x10; };
+	    if (__inP[4] != __maskPixel) { __outBits |= 0x08; };
+	    if (__inP[5] != __maskPixel) { __outBits |= 0x04; };
+	    if (__inP[6] != __maskPixel) { __outBits |= 0x02; };
+	    if (__inP[7] != __maskPixel) { __outBits |= 0x01; };
+	    __inP += 8;
+	    *__outP++ = __outBits;
+	    __outBits = 0;
+	}
+	for (; __x>0; __x--) {
+	    __outBits <<= 1;
+	    if (*__inP != __maskPixel) {
+		__outBits |= 1;
+	    }
+	    __inP++;
+
+/*
+ printf("x: %d  bits: %x\n", __x, __outBits);
+*/
+	    if (--__nOut == 0) {
+#ifdef DEBUG
+		if (__outP >= outEnd) {
+		 printf("oops\n");
+		 goto fail;
+		}
+#endif
+		*__outP = __outBits;
+
+		__outP++;
+		__nOut = 8;
+		__outBits = 0;
+	    }
+	}
+
+	if (__nOut != 8) {
+	    __outBits <<= __nOut;
+
+#ifdef DEBUG
+	    if (__outP >= outEnd) {
+	     printf("oops2\n");
+	     goto fail;
+	    }
+#endif
+	    *__outP = __outBits;
+
+	}
+	__outP = __nextOutRow;
+    }
+fail: ;
+%}.
+    mask := ImageMask width:width height:height fromArray:maskArray.
+    ^ mask
+
+    "Created: 21.6.1996 / 11:43:47 / cg"
+! !
+
 !ImageReader class methodsFor:'i/o support'!
 
 streamReadingFile:aFilename
@@ -1335,16 +1443,16 @@
 
     name := aFilename asString.
     ((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[
-        fn := Smalltalk getSystemFileName:name.
-        inStream := PipeStream readingFrom:'gunzip < ' , fn.
+	fn := Smalltalk getSystemFileName:name.
+	inStream := PipeStream readingFrom:'gunzip < ' , fn.
     ] ifFalse:[
-        inStream := Smalltalk systemFileStreamFor:aFilename.
-        inStream isNil ifTrue:[
-            inStream := Smalltalk bitmapFileStreamFor:aFilename
-        ]
+	inStream := Smalltalk systemFileStreamFor:aFilename.
+	inStream isNil ifTrue:[
+	    inStream := Smalltalk bitmapFileStreamFor:aFilename
+	]
     ].
     inStream isNil ifTrue:[
-        'ImageReader [warning]: open error on: ' infoPrint. aFilename infoPrintCR. 
+	'ImageReader [warning]: open error on: ' infoPrint. aFilename infoPrintCR. 
     ].
     ^ inStream
 
@@ -1361,7 +1469,7 @@
 
     reader := self readFile:aFileName.
     reader notNil ifTrue:[
-        ^ reader image
+	^ reader image
     ].
     ^ nil
 
@@ -1383,7 +1491,7 @@
 
     reader := self new fromStream:aStream.
     reader notNil ifTrue:[
-        ^ reader image
+	^ reader image
     ].
     ^ nil
 
@@ -1399,7 +1507,7 @@
 
     reader := self readFile:aFileName.
     reader notNil ifTrue:[
-        ^ reader images
+	^ reader images
     ].
     ^ nil
 
@@ -1416,7 +1524,7 @@
 
     reader := self new fromStream:aStream.
     reader notNil ifTrue:[
-        ^ reader images
+	^ reader images
     ].
     ^ nil
 
@@ -1431,8 +1539,8 @@
 
     inStream := self streamReadingFile:aFileName.
     inStream isNil ifTrue:[
-        'ImageReader [warning]: file open error' errorPrintCR.
-        ^ nil
+	'ImageReader [warning]: file open error' errorPrintCR.
+	^ nil
     ].
     reader := self new fromStream:inStream.
     inStream close.
@@ -1513,7 +1621,7 @@
     bitsPerRow := width * (self bitsPerPixel).
     bytesPerRow := bitsPerRow // 8.
     ((bitsPerRow \\ 8) ~~ 0) ifTrue:[
-        bytesPerRow := bytesPerRow + 1
+	bytesPerRow := bytesPerRow + 1
     ].
     ^ bytesPerRow
 
@@ -1568,27 +1676,24 @@
     |image depth|
 
     imageSequence size > 0 ifTrue:[
-        ^  self images first.
+	^  self images first.
     ].
 
     depth := self bitsPerPixel.
     image := (Image implementorForDepth:depth) new.
     image depth:depth.
-
-    "be carefull when reading from non-fileStreams"
-    (inStream respondsTo:#fileName) ifTrue:[
-        image fileName:inStream pathName.
+    inStream isFileStream ifTrue:[
+	image fileName:inStream pathName.
     ].
-
     image 
-        width:width 
-        height:height
-        photometric:photometric
-        samplesPerPixel:samplesPerPixel
-        bitsPerSample:bitsPerSample
-        colorMap:colorMap
-        bits:data
-        mask:mask.
+	width:width 
+	height:height
+	photometric:photometric
+	samplesPerPixel:samplesPerPixel
+	bitsPerSample:bitsPerSample
+	colorMap:colorMap
+	bits:data
+	mask:mask.
     ^ image
 
     "Modified: / 3.11.1997 / 14:55:00 / cg"
@@ -1606,12 +1711,12 @@
     |img|
 
     imageSequence notNil ifTrue:[
-        ^ imageSequence
+	^ imageSequence
     ].
 
     img := self image.
     img isNil ifTrue:[
-        ^ img
+	^ img
     ].
     ^ Array with:img
 
@@ -1635,7 +1740,7 @@
     |n|
 
     (n := imageSequence size) > 0 ifTrue:[
-        ^ n
+	^ n
     ].
     ^ 1
 
@@ -1733,7 +1838,7 @@
     outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb)
 ! !
 
-!ImageReader methodsFor:'image reading'!
+!ImageReader methodsFor:'image reading support'!
 
 buildMaskFromColor:maskPixelValue
     "helper for image formats, where an individual pixel value
@@ -1741,101 +1846,16 @@
      Creates a maskImage, with zeros at positions where the image
      has the given pixelValue; all other mask pixels are set to 1."
 
-    |maskArray bytesPerMaskRow|
-
-    bytesPerMaskRow := (width+7) // 8.
-
-    maskArray := ByteArray uninitializedNew:bytesPerMaskRow * height.
-
-%{
-    int __w = __intVal(__INST(width));
-    int __h = __intVal(__INST(height));
-    int __x, __y;
-    int __outBits, __nOut;
-    unsigned char *__inP, *__outP, *__nextOutRow;
-    int __bpr = __intVal(bytesPerMaskRow);
-    int __maskPixel = __intVal(maskPixelValue);
-#ifdef DEBUG
-    unsigned char *outEnd;
-#endif
-
-    if (! __isByteArray(__INST(data))) goto fail;
-    if (! __isByteArray(maskArray)) goto fail;
-
-    __inP = __ByteArrayInstPtr(__INST(data))->ba_element;
-    __outP = __ByteArrayInstPtr(maskArray)->ba_element;
-
-#ifdef DEBUG
-    outEnd = __outP + (__byteArraySize(maskArray));
-#endif
-/*
-printf("outP: %x outEnd: %x sz: %d\n", __outP, outEnd, __byteArraySize(maskArray));
-*/
-
-    for (__y=__h; __y>0; __y--) {
-        __outBits = 0;
-        __nOut = 8;
-        __nextOutRow = __outP + __bpr;
+    mask := self class 
+		buildMaskFromColor:maskPixelValue 
+		for:data
+		width:width
+		height:height
 
-        for (__x=__w; __x>=8; __x-=8) {
-            if (__inP[0] != __maskPixel) { __outBits |= 0x80; };
-            if (__inP[1] != __maskPixel) { __outBits |= 0x40; };
-            if (__inP[2] != __maskPixel) { __outBits |= 0x20; };
-            if (__inP[3] != __maskPixel) { __outBits |= 0x10; };
-            if (__inP[4] != __maskPixel) { __outBits |= 0x08; };
-            if (__inP[5] != __maskPixel) { __outBits |= 0x04; };
-            if (__inP[6] != __maskPixel) { __outBits |= 0x02; };
-            if (__inP[7] != __maskPixel) { __outBits |= 0x01; };
-            __inP += 8;
-            *__outP++ = __outBits;
-            __outBits = 0;
-        }
-        for (; __x>0; __x--) {
-            __outBits <<= 1;
-            if (*__inP != __maskPixel) {
-                __outBits |= 1;
-            }
-            __inP++;
+    "Modified: / 14.1.1998 / 14:48:33 / cg"
+! !
 
-/*
- printf("x: %d  bits: %x\n", __x, __outBits);
-*/
-            if (--__nOut == 0) {
-#ifdef DEBUG
-                if (__outP >= outEnd) {
-                 printf("oops\n");
-                 goto fail;
-                }
-#endif
-                *__outP = __outBits;
-
-                __outP++;
-                __nOut = 8;
-                __outBits = 0;
-            }
-        }
-
-        if (__nOut != 8) {
-            __outBits <<= __nOut;
-
-#ifdef DEBUG
-            if (__outP >= outEnd) {
-             printf("oops2\n");
-             goto fail;
-            }
-#endif
-            *__outP = __outBits;
-
-        }
-        __outP = __nextOutRow;
-    }
-fail: ;
-%}.
-    mask := ImageMask width:width height:height fromArray:maskArray.
-
-    "Created: 21.6.1996 / 11:43:47 / cg"
-
-!
+!ImageReader methodsFor:'image reading'!
 
 fromStream:aStream
     "read an image in my format from aStream.
@@ -1853,8 +1873,8 @@
     "save image in my format on aFile"
 
     ^ Image cannotRepresentImageSignal 
-        raiseWith:image
-        errorString:('image save not implemented for this format').
+	raiseWith:image
+	errorString:('image save not implemented for this format').
 
     "Modified: 10.4.1997 / 17:43:52 / cg"
 ! !
@@ -1869,5 +1889,5 @@
 !ImageReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.56 1998-01-15 15:38:11 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.57 1998-01-16 15:09:34 cg Exp $'
 ! !