*** empty log message ***
authorclaus
Fri, 28 Oct 1994 04:16:37 +0100
changeset 29 e04e1aceff6f
parent 28 8daff0234d2e
child 30 9638bc775850
*** empty log message ***
PCXReader.st
--- a/PCXReader.st	Mon Oct 10 03:34:22 1994 +0100
+++ b/PCXReader.st	Fri Oct 28 04:16:37 1994 +0100
@@ -23,9 +23,40 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.1 1994-10-10 02:32:51 claus Exp $
+$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.2 1994-10-28 03:16:37 claus Exp $
 '!
 
+!PCXReader class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1994 by Claus Gittinger
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+!
+
+version
+"
+$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.2 1994-10-28 03:16:37 claus Exp $
+"
+!
+
+documentation
+"
+    this class provides methods for loading PCX bitmap files.
+    Due to not having too many examples for testing, this could fail
+    to read some files. (especially, I have no uncompressed files
+    for testing).
+"
+! !
+
 !PCXReader class methodsFor:'testing'!
 
 isValidPCXHeader:aHeader
@@ -47,57 +78,23 @@
 isValidImageFile:aFilename
     "return true, if aFilename contains a PCX image"
 
-    |fileSize header inStream|
+    |count header inStream|
 
     inStream := self streamReadingFile:aFilename.
     inStream isNil ifTrue:[^ false].
 
     inStream binary.
-    fileSize := inStream size.
-
-    fileSize < 128 ifTrue:[
-	inStream close.
-	^ false
-    ].
 
     header := ByteArray uninitializedNew:128.
-    inStream nextBytes:128 into:header.
+    count := inStream nextBytes:128 into:header.
     inStream close.
 
-    (self isValidPCXHeader:header) ifFalse:[
+    ((count == 128) and:[self isValidPCXHeader:header]) ifFalse:[
 	^ false
     ].
     ^ true
 ! !
 
-!PCXReader class methodsFor:'documentation'!
-
-copyright
-"
- COPYRIGHT (c) 1994 by Claus Gittinger
-	      All Rights Reserved
-
- This software is furnished under a license and may be used
- only in accordance with the terms of that license and with the
- inclusion of the above copyright notice.   This software may not
- be provided or otherwise made available to, or used by, any
- other person.  No title to or ownership of the software is
- hereby transferred.
-"
-!
-
-version
-"
-$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.1 1994-10-10 02:32:51 claus Exp $
-"
-!
-
-documentation
-"
-    this class provides methods for loading PCX bitmap files..
-"
-! !
-
 !PCXReader class methodsFor:'initialization'!
 
 initialize
@@ -118,8 +115,12 @@
       h        "{Class: SmallInteger }"
       byte     "{Class: SmallInteger }"
       nByte    "{Class: SmallInteger }"
-      idx2
-      dataBytes bytesPerRow value|
+      bytesPerRow "{Class: SmallInteger }"
+      value       "{Class: SmallInteger }"
+      idx2        "{Class: SmallInteger }"
+      dataBytes buffer 
+      bufferIndex "{Class: SmallInteger }"
+      nBuffer     "{Class: SmallInteger }"|
 
     version := header at:2.
 "/    'version=' print. version printNL.
@@ -176,22 +177,36 @@
 	].
     ].
 
-    data := dataBytes := ByteArray new:(height * bytesPerRow).
+    compression == 1 ifTrue:[
+	data := dataBytes := ByteArray new:(height * bytesPerRow).
 
-    compression == 1 ifTrue:[
+	buffer := ByteArray new:4096.
+	bufferIndex := 1.
+	nBuffer := 0.
+
 	rowIndex := 1.
 	h := height.
 	1 to:h do:[:row |
 	    dstIndex := rowIndex.
 	    endIndex := dstIndex + bytesPerRow.
-	    [dstIndex <= endIndex] whileTrue:[
-		byte := inStream nextByte.
-		((byte bitAnd:16rC0) ~~ 16rC0) ifTrue:[
+	    [dstIndex < endIndex] whileTrue:[
+		nBuffer == 0 ifTrue:[
+		    nBuffer := inStream nextBytes:4096 into:buffer.
+		    bufferIndex := 1.
+		].
+		byte := buffer at:bufferIndex.
+		bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
+		((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
 		    dataBytes at:dstIndex put:byte.
 		    dstIndex := dstIndex + 1.
 		] ifFalse:[
 		    nByte := byte bitAnd:2r00111111.
-		    value := inStream nextByte.
+		    nBuffer == 0 ifTrue:[
+			nBuffer := inStream nextBytes:4096 into:buffer.
+			bufferIndex := 1.
+		    ].
+		    value := buffer at:bufferIndex.
+		    bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
 		    value notNil ifTrue:[
 			idx2 := endIndex min:(dstIndex + nByte - 1).
 			dataBytes from:dstIndex to:idx2 put:value.
@@ -201,21 +216,61 @@
 	    ].
 	    rowIndex := rowIndex + bytesPerRow
 	]
+
+"/        rowIndex := 1.
+"/        h := height.
+"/        1 to:h do:[:row |
+"/            dstIndex := rowIndex.
+"/            endIndex := dstIndex + bytesPerRow.
+"/            [dstIndex < endIndex] whileTrue:[
+"/                byte := inStream next.
+"/                ((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
+"/                    dataBytes at:dstIndex put:byte.
+"/                    dstIndex := dstIndex + 1.
+"/                ] ifFalse:[
+"/                    nByte := byte bitAnd:2r00111111.
+"/                    value := inStream next.
+"/                    value notNil ifTrue:[
+"/                        idx2 := endIndex min:(dstIndex + nByte - 1).
+"/                        dataBytes from:dstIndex to:idx2 put:value.
+"/                        dstIndex := dstIndex + nByte.
+"/                    ]
+"/                ].
+"/            ].
+"/            rowIndex := rowIndex + bytesPerRow
+"/        ]
     ] ifFalse:[
 	"
 	 actually untested ...
 	"
-	inStream nextBytes:(height * bytesPerRow) into:data
+	data := dataBytes := ByteArray uninitializedNew:(height * bytesPerRow).
+	inStream nextBytes:(height * bytesPerRow) into:data.
+	nBuffer := 0.
     ].
 
     (version == 5) ifTrue:[
 	"read the 256-entry colormap"
 
-	(byte := inStream next) == 16rC0 ifFalse:[
-	   'PCXREADER: no valid 256-entry palette' printNL.
+	nBuffer ~~ 0 ifTrue:[
+	    byte := buffer at:bufferIndex.
+	    bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
+	] ifFalse:[
+	    byte := inStream next
+	].
+
+	byte == 16rC0 ifFalse:[
+	   'PCXREADER: no valid 256-entry palette (got' errorPrint. 
+	   byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintNL.
 	].
 	rawMap := ByteArray uninitializedNew:(256*3).
-	inStream nextBytes:(256*3) into:rawMap.
+	nBuffer ~~ 0 ifTrue:[
+	    rawMap replaceFrom:1 to:(256*3) with:buffer startingAt:bufferIndex.
+	    nBuffer < (256*3) ifTrue:[
+		inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1
+	    ]
+	] ifFalse:[
+	    inStream nextBytes:(256*3) into:rawMap.
+	].
 	rMap := Array new:256.
 	gMap := Array new:256.
 	bMap := Array new:256.
@@ -237,9 +292,8 @@
 
     "
      |i f|
-     i := Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'.
-     f := i asFormOn:Display.
-     v displayOpaqueForm:(f magnifyBy:2@2) x:5 y:5
+     i := Image fromFile:'somefile.pcx'.
+     i inspect.
     "
 !