XBMReader.st
changeset 51 ac84315b8181
parent 41 66edc847b9c8
child 53 4f5e734bc59f
--- a/XBMReader.st	Thu Mar 23 17:51:31 1995 +0100
+++ b/XBMReader.st	Thu Mar 23 17:54:02 1995 +0100
@@ -23,7 +23,7 @@
 COPYRIGHT (c) 1992 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.10 1995-02-18 15:52:48 claus Exp $
+$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.11 1995-03-23 16:53:49 claus Exp $
 '!
 
 !XBMReader class methodsFor:'documentation'!
@@ -44,7 +44,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.10 1995-02-18 15:52:48 claus Exp $
+$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.11 1995-03-23 16:53:49 claus Exp $
 "
 !
 
@@ -99,6 +99,16 @@
     ].
     inStream close.
     ^ true
+!
+
+canRepresent:anImage
+    "return true, if anImage can be represented in my file format"
+
+    |photometric|
+
+    (anImage depth ~~ 1) ifTrue:[^ false.].
+    (((photometric := anImage photometric) ~~ #blackIs0) and:[photometric ~~ #whiteIs0]) ifTrue:[^ false.].
+    ^ true
 ! !
 
 !XBMReader methodsFor:'reading from file'!
@@ -117,28 +127,28 @@
 
     line := aStream nextLine.
     line isNil ifTrue:[
-        'XBMReader: short file' errorPrintNL.
-        ^ nil
+	'XBMReader: short file' errorPrintNL.
+	^ nil
     ].
 
     [line startsWith:'#'] whileFalse:[
-        line := aStream nextLine
+	line := aStream nextLine
     ].
 
     (line startsWith:'#define') ifFalse:[
-        'XBMReader: format error (expected #define)' errorPrintNL.
-        ^ nil
+	'XBMReader: format error (expected #define)' errorPrintNL.
+	^ nil
     ].
 
     index := line indexOf:(Character space).
     index := line indexOf:(Character space) startingAt:(index + 1).
     (index == 0) ifTrue:[
-        'XBMReader: format error' errorPrintNL.
-        ^ nil
+	'XBMReader: format error' errorPrintNL.
+	^ nil
     ].
     ((line copyTo:index - 1) endsWith:'width') ifFalse:[
-        'XBMReader: format error (expected width)' errorPrintNL.
-        ^ nil
+	'XBMReader: format error (expected width)' errorPrintNL.
+	^ nil
     ].
     line := line copyFrom:(index + 1).
     width := Number readFromString:line.
@@ -147,19 +157,19 @@
     index := line indexOf:(Character space).
     index := line indexOf:(Character space) startingAt:(index + 1).
     (index == 0) ifTrue:[
-        'XBMReader: format error' errorPrintNL.
-        ^ nil
+	'XBMReader: format error' errorPrintNL.
+	^ nil
     ].
     ((line copyTo:index - 1) endsWith:'height') ifFalse:[
-        'XBMReader: format error (expected height)' errorPrintNL.
-        ^ nil
+	'XBMReader: format error (expected height)' errorPrintNL.
+	^ nil
     ].
     line := line copyFrom:(index + 1).
     height := Number readFromString:line.
 
     bytesPerRow := width // 8.
     ((width \\ 8) ~~ 0) ifTrue:[
-        bytesPerRow := bytesPerRow + 1
+	bytesPerRow := bytesPerRow + 1
     ].
 
     reverseBits := self class reverseBits.
@@ -169,25 +179,25 @@
 
     line := aStream nextLine.
     [line startsWith:'#'] whileTrue:[
-        line := aStream nextLine
+	line := aStream nextLine
     ].
 
     line := aStream nextLine.
     [line notNil] whileTrue:[
-        index := 1.
-        [index ~~ 0] whileTrue:[
-            index := line indexOf:$x startingAt:index.
-            (index ~~ 0) ifTrue:[
-                index := index + 1.
-                hi := (line at:index) digitValue.
-                index := index + 1.
-                lo := (line at:index) digitValue.
-                val := (hi bitShift:4) bitOr:lo.
-                data at:dstIndex put:(reverseBits at:(val + 1)).
-                dstIndex := dstIndex + 1
-            ]
-        ].
-        line := aStream nextLine
+	index := 1.
+	[index ~~ 0] whileTrue:[
+	    index := line indexOf:$x startingAt:index.
+	    (index ~~ 0) ifTrue:[
+		index := index + 1.
+		hi := (line at:index) digitValue.
+		index := index + 1.
+		lo := (line at:index) digitValue.
+		val := (hi bitShift:4) bitOr:lo.
+		data at:dstIndex put:(reverseBits at:(val + 1)).
+		dstIndex := dstIndex + 1
+	    ]
+	].
+	line := aStream nextLine
     ].
     photometric := #whiteIs0.
     samplesPerPixel := 1.
@@ -207,6 +217,11 @@
      srcIndex "{ Class: SmallInteger }"
      rowBytes "{ Class: SmallInteger }" |
 
+    (self class canRepresent:image) ifFalse:[
+	self error:'can only save depth 1 B&W images'.
+	^ nil.
+    ].
+
     outStream := FileStream newFileNamed:aFileName.
     outStream isNil ifTrue:[
 	'create error' errorPrintNL. 
@@ -220,14 +235,6 @@
     bitsPerSample := image bitsPerSample.
     colorMap := image colorMap.
 
-    ((samplesPerPixel ~~ 1)
-    or:[((bitsPerSample at:1) ~~ 1)
-    or:[(photometric ~~ #blackIs0) and:[photometric ~~ #whiteIs0]]]) ifTrue:[
-	self error:'can only save Depth1Images'.
-	outStream close.
-	^ nil.
-    ].
-
     outStream nextPutAll: '#define xbm_width '.
     outStream nextPutAll:(width printString).
     outStream cr.
@@ -246,6 +253,9 @@
 	rowBytes timesRepeat:[
 	    outStream nextPutAll: '0x'.
 	    bits := data at:srcIndex. srcIndex := srcIndex + 1.
+	    photometric == #blackIs0 ifTrue:[
+		bits := bits bitInvert bitAnd:16rFF
+	    ].
 	    byte := (reverseBits at:(bits + 1)).
 	    byte < 16 ifTrue:[
 		outStream nextPut:$0