XPMReader.st
changeset 1737 a1ed08195ee7
parent 1710 ad35e6a18e98
child 1743 2f206c9f1319
--- a/XPMReader.st	Fri Apr 04 19:07:10 2003 +0200
+++ b/XPMReader.st	Fri Apr 04 19:07:54 2003 +0200
@@ -13,7 +13,7 @@
 "{ Package: 'stx:libview2' }"
 
 ImageReader subclass:#XPMReader
-	instanceVariableNames:''
+	instanceVariableNames:'charsPerPixel maskPixelValue characterTranslation'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Graphics-Images-Support'
@@ -124,7 +124,7 @@
 
     "
      XPMReader isValidImageFile:'fooBar'    
-     XPMReader isValidImageFile:'bitmaps/xpmBitmaps/device_images/ljet.xpm'      
+     XPMReader isValidImageFile:'../../goodies/bitmaps/xpmBitmaps/device_images/ljet.xpm'      
      XPMReader isValidImageFile:'bitmaps/gifImages/garfield.gif' 
     "
 
@@ -151,70 +151,24 @@
     ^ s
 !
 
-fromStream:aStream
-    "read an XPM-image from aStream. Return the receiver 
-     (with all relevant instance variables set for the image) 
-     or nil on error"
-
-    |line 
-     srcIndex "{ Class: SmallInteger }"
-     dstIndex "{ Class: SmallInteger }"
-     colorName monoName greyName grey4Name symbolicName colorMapSize redMap greenMap blueMap
-     charsPerPixel xlation s bitsPerPixel lineDone maskPixelValue
-     state key lastKey lastChar1 lastChar2 c1 c2 lastXLation|
-
-    inStream := aStream.
-
-    line := aStream nextLine.
-    (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
-        ^ self fileFormatError:'format error (expected XPM)'.
-    ].
-
-    line := aStream nextLine.
-    [line notNil and:[(line startsWith:'/*') or:[line isBlank or:[(line startsWith:' *')]]]] whileTrue:[
-        line := aStream nextLine.
-    ].
-    (line notNil and:[line startsWith:'static char']) ifFalse:[
-        ^ self fileFormatError:'format error (expected static char)'.
-    ].
-    line := aStream nextLine.
-    (line notNil and:[line startsWith:'/*']) ifTrue:[
-        [line notNil 
-         and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[
-            line := aStream nextLine.
-        ].
-    ].
-    line notNil ifTrue:[
-        line := line withoutSeparators
-    ].
-    (line notNil and:[line startsWith:'"']) ifFalse:[
-        ^ self fileFormatError:'format error (expected "ww hh nn mm)'.
-    ].
-    s := ReadStream on:line.
-    s next.  "skip quote"
-    width := Integer readFrom:s.
-    height := Integer readFrom:s.
-    colorMapSize := Integer readFrom:s.
-    charsPerPixel := Integer readFrom:s.
-    charsPerPixel ~~ 1 ifTrue:[
-        xlation := Dictionary new:colorMapSize.
-    ] ifFalse:[
-        xlation := Array new:256.
-    ].
+readColorMap:colorMapSize
+    |redMap greenMap blueMap s key lineDone state
+     symbolicName monoName greyName grey4Name colorName|
 
     redMap := ByteArray new:colorMapSize.
     greenMap := ByteArray new:colorMapSize.
     blueMap := ByteArray new:colorMapSize.
     colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
+
     1 to:colorMapSize do:[:colorIndex |
         |index line color t word|
 
-        line := aStream nextLine.
+        line := inStream nextLine.
         [line notNil and:[line startsWith:'/*']] whileTrue:[
             [line notNil and:[(line endsWith:'*/') not]] whileTrue:[
-                line := aStream nextLine.
+                line := inStream nextLine.
             ].
-            line := aStream nextLine.
+            line := inStream nextLine.
         ].
         line notNil ifTrue:[
             line := line withoutSeparators
@@ -227,10 +181,10 @@
         s next. "skip quote"
         charsPerPixel ~~ 1 ifTrue:[
             key := s next:charsPerPixel.
-            xlation at:key put:colorIndex - 1.
+            characterTranslation at:key put:colorIndex - 1.
         ] ifFalse:[
             index := s next asciiValue.
-            xlation at:index put:colorIndex - 1.
+            characterTranslation at:index put:colorIndex - 1.
         ].
 
         lineDone := false.
@@ -339,6 +293,58 @@
             blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100).
         ].
     ].
+!
+
+readImage
+    "read an XPM-image from my inStream. Return the receiver 
+     (with all relevant instance variables set for the image) 
+     or nil on error"
+
+    |line 
+     srcIndex "{ Class: SmallInteger }"
+     dstIndex "{ Class: SmallInteger }"
+     colorMapSize   
+     s bitsPerPixel key lastKey lastChar1 lastChar2 c1 c2 lastXLation|
+
+    line := inStream nextLine.
+    (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
+        ^ self fileFormatError:'format error (expected XPM)'.
+    ].
+
+    line := inStream nextLine.
+    [line notNil and:[(line startsWith:'/*') or:[line isBlank or:[(line startsWith:' *')]]]] whileTrue:[
+        line := inStream nextLine.
+    ].
+    (line notNil and:[line startsWith:'static char']) ifFalse:[
+        ^ self fileFormatError:'format error (expected static char)'.
+    ].
+    line := inStream nextLine.
+    (line notNil and:[line startsWith:'/*']) ifTrue:[
+        [line notNil 
+         and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[
+            line := inStream nextLine.
+        ].
+    ].
+    line notNil ifTrue:[
+        line := line withoutSeparators
+    ].
+    (line notNil and:[line startsWith:'"']) ifFalse:[
+        ^ self fileFormatError:'format error (expected "ww hh nn mm)'.
+    ].
+    s := ReadStream on:line.
+    s next.  "skip quote"
+    width := Integer readFrom:s.
+    height := Integer readFrom:s.
+    colorMapSize := Integer readFrom:s.
+    charsPerPixel := Integer readFrom:s.
+
+    charsPerPixel ~~ 1 ifTrue:[
+        characterTranslation := Dictionary new:colorMapSize.
+    ] ifFalse:[
+        characterTranslation := Array new:256.
+    ].
+
+    self readColorMap:colorMapSize.
 
     "actually, could make it an image with less depth most of the time ..."
 
@@ -355,9 +361,9 @@
 
     dstIndex := 1.
     1 to:height do:[:row |
-        line := aStream nextLine withoutSpaces.
+        line := inStream nextLine withoutSpaces.
         [line notNil and:[line startsWith:'/*']] whileTrue:[
-            line := aStream nextLine withoutSpaces.
+            line := inStream nextLine withoutSpaces.
         ].
         line notNil ifTrue:[
             line := line withoutSeparators
@@ -370,7 +376,7 @@
             1 to:width do:[:col |
                 key := line at:srcIndex.
                 key ~~ lastKey ifTrue:[
-                    lastXLation := xlation at:key asciiValue.
+                    lastXLation := characterTranslation at:key asciiValue.
                     lastKey := key
                 ].
                 data at:dstIndex put:lastXLation.
@@ -390,7 +396,7 @@
                     (c1 ~~ lastChar1 or:[c2 ~~ lastChar2]) ifTrue:[
                         key at:1 put:c1.
                         key at:2 put:c2.
-                        lastXLation := xlation at:key.
+                        lastXLation := characterTranslation at:key.
                         lastChar1 := c1.
                         lastChar2 := c2.
                     ].
@@ -410,9 +416,9 @@
                 s next. "/ skip dquote
                 1 to:width do:[:col |
                     key := s next:charsPerPixel.
-"/                data at:dstIndex put:(xlation at:key).
+"/                data at:dstIndex put:(characterTranslation at:key).
                     key ~= lastKey ifTrue:[
-                        lastXLation := xlation at:key.
+                        lastXLation := characterTranslation at:key.
                         lastKey := key
                     ].
                     data at:dstIndex put:lastXLation.
@@ -437,10 +443,7 @@
     ].
 
     "
-     XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream)
-     XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream)
-     XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) 
-     XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream)
+     XPMReader fromStream:('../../goodies/bitmaps/xpmBitmaps/FATAL.xpm' asFilename readStream)
     "
 
     "Created: / 24.9.1995 / 06:20:06 / claus"
@@ -573,7 +576,7 @@
 !XPMReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.49 2003-03-02 18:39:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.50 2003-04-04 17:07:22 cg Exp $'
 ! !
 
 XPMReader initialize!