diff -r c2e206361c7b -r 66edc847b9c8 XPMReader.st --- a/XPMReader.st Sat Feb 18 16:32:43 1995 +0100 +++ b/XPMReader.st Sat Feb 18 16:52:52 1995 +0100 @@ -10,6 +10,8 @@ hereby transferred. " +'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 2:24:00 am'! + ImageReader subclass:#XPMReader instanceVariableNames:'' classVariableNames:'' @@ -21,7 +23,7 @@ COPYRIGHT (c) 1994 by Claus Gittinger All Rights Reserved -$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.5 1995-02-06 00:39:52 claus Exp $ +$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.6 1995-02-18 15:52:52 claus Exp $ '! !XPMReader class methodsFor:'documentation'! @@ -42,7 +44,7 @@ version " -$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.5 1995-02-06 00:39:52 claus Exp $ +$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.6 1995-02-18 15:52:52 claus Exp $ " ! @@ -115,9 +117,10 @@ ^ s ! -fromFile:aFileName - "read an XPM-image from aFileName. return the receiver (with all - relevant instance variables set for the image) or nil on error" +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 }" @@ -125,36 +128,29 @@ colorName colorMapSize redMap greenMap blueMap charsPerPixel xlation s bitsPerPixel char lineDone| - inStream := self class streamReadingFile:aFileName. - inStream isNil ifTrue:[ - 'XPM: file open error' errorPrintNL. - ^ nil - ]. + inStream := aStream. - line := inStream nextLine. + line := aStream nextLine. (line notNil and:[line startsWith:'/* XPM']) ifFalse:[ - 'XPM: format error (expected XPM)' errorPrintNL. - inStream close. - ^ nil + 'XPM: format error (expected XPM)' errorPrintNL. + ^ nil ]. - line := inStream nextLine. + line := aStream nextLine. [line notNil and:[line startsWith:'/*']] whileTrue:[ - line := inStream nextLine. + line := aStream nextLine. ]. (line notNil and:[line startsWith:'static char']) ifFalse:[ - 'XPM: format error (expected static char)' errorPrintNL. - inStream close. - ^ nil + 'XPM: format error (expected static char)' errorPrintNL. + ^ nil ]. - line := inStream nextLine. + line := aStream nextLine. [line notNil and:[line startsWith:'/*']] whileTrue:[ - line := inStream nextLine. + line := aStream nextLine. ]. (line notNil and:[line startsWith:'"']) ifFalse:[ - 'XPM: format error (expected "ww hh nn mm)' errorPrintNL. - inStream close. - ^ nil + 'XPM: format error (expected "ww hh nn mm)' errorPrintNL. + ^ nil ]. s := ReadStream on:line. s next. "skip quote" @@ -163,97 +159,94 @@ colorMapSize := Integer readFrom:s. charsPerPixel := Integer readFrom:s. charsPerPixel ~~ 1 ifTrue:[ - 'XPM: can only handle single-character xpm-files' errorPrintNL. - ^ nil + 'XPM: can only handle single-character xpm-files' errorPrintNL. + ^ nil ]. xlation := Array new:256. redMap := Array new:colorMapSize. greenMap := Array new:colorMapSize. blueMap := Array new:colorMapSize. -"/ colorMap := Array with:redMap with:greenMap with:blueMap. colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. 1 to:colorMapSize do:[:colorIndex | - |index line color| + |index line color| - line := inStream nextLine. - [line notNil and:[line startsWith:'/*']] whileTrue:[ - line := inStream nextLine. - ]. - (line notNil and:[line startsWith:'"']) ifFalse:[ - 'XPM: format error (expected color spec)' errorPrintNL. - inStream close. - ^ nil - ]. + line := aStream nextLine. + [line notNil and:[line startsWith:'/*']] whileTrue:[ + line := aStream nextLine. + ]. + (line notNil and:[line startsWith:'"']) ifFalse:[ + 'XPM: format error (expected color spec)' errorPrintNL. + ^ nil + ]. - s := ReadStream on:line. - s next. "skip quote" - index := s next asciiValue. - xlation at:index put:colorIndex - 1. + s := ReadStream on:line. + s next. "skip quote" + index := s next asciiValue. + xlation at:index put:colorIndex - 1. - lineDone := false. - [lineDone] whileFalse:[ - s skipSeparators. - char := s peek. - char == $" ifTrue:[ - lineDone := true - ] ifFalse:[ - char == $s ifTrue:[ - " - symbolic name - " - s next. - s skipSeparators. - s nextWord. - s skipSeparators. - ] ifFalse:[ - char == $m ifTrue:[ - " - monochrome data - " - s next. - s skipSeparators. - s nextWord. - s skipSeparators. - ] ifFalse:[ - (char == $g) ifTrue:[ - " - greyscale data - " - s next. - s peek == 4 ifTrue:[s next]. - s skipSeparators. - s nextWord. - s skipSeparators. - ] ifFalse:[ - (char == $c) ifTrue:[ - " - color data - " - s next. - s skipSeparators. - colorName := self colorNameFrom:s. - s skipSeparators. - ] ifFalse:[ - 'XPM: format error (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. - inStream close. - ^ nil - ]. - ] - ] - ] - ]. - ]. - ((colorName = 'none') or:[colorName = 'None']) ifTrue:[ - "mhmh must add mask to Image-instances soon ..." - color := Color white - ] ifFalse:[ - color := Color name:colorName. - ]. - redMap at:colorIndex put:(color red * 255 // 100). - greenMap at:colorIndex put:(color green * 255 // 100). - blueMap at:colorIndex put:(color blue * 255 // 100). + lineDone := false. + [lineDone] whileFalse:[ + s skipSeparators. + char := s peek. + char == $" ifTrue:[ + lineDone := true + ] ifFalse:[ + char == $s ifTrue:[ + " + symbolic name + " + s next. + s skipSeparators. + s nextWord. + s skipSeparators. + ] ifFalse:[ + char == $m ifTrue:[ + " + monochrome data + " + s next. + s skipSeparators. + s nextWord. + s skipSeparators. + ] ifFalse:[ + (char == $g) ifTrue:[ + " + greyscale data + " + s next. + s peek == 4 ifTrue:[s next]. + s skipSeparators. + s nextWord. + s skipSeparators. + ] ifFalse:[ + (char == $c) ifTrue:[ + " + color data + " + s next. + s skipSeparators. + colorName := self colorNameFrom:s. + s skipSeparators. + ] ifFalse:[ + 'XPM: format error (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. + ^ nil + ]. + ] + ] + ] + ]. + ]. + ((colorName = 'none') or:[colorName = 'None']) ifTrue:[ + "mhmh must add mask to Image-instances soon ..." + color := Color white + ] ifFalse:[ + color := Color name:colorName. + ]. + redMap at:colorIndex put:(color red * 255 // 100). + greenMap at:colorIndex put:(color green * 255 // 100). + blueMap at:colorIndex put:(color blue * 255 // 100). ]. "actually, could make it an image with less depth most of the time ..." @@ -266,24 +259,23 @@ dstIndex := 1. 1 to:height do:[:row | - line := inStream nextLine withoutSpaces. - [line notNil and:[line startsWith:'/*']] whileTrue:[ - line := inStream nextLine withoutSpaces. - ]. - (line notNil and:[line startsWith:'"']) ifFalse:[ - 'XPM: format error (expected pixels)' errorPrintNL. - inStream close. - ^ nil - ]. - srcIndex := 2. - 1 to: width do:[:col | - |char| + line := aStream nextLine withoutSpaces. + [line notNil and:[line startsWith:'/*']] whileTrue:[ + line := aStream nextLine withoutSpaces. + ]. + (line notNil and:[line startsWith:'"']) ifFalse:[ + 'XPM: format error (expected pixels)' errorPrintNL. + ^ nil + ]. + srcIndex := 2. + 1 to: width do:[:col | + |char| - char := line at:srcIndex. - data at:dstIndex put:(xlation at:char asciiValue). - srcIndex := srcIndex + 1. - dstIndex := dstIndex + 1 - ] + char := line at:srcIndex. + data at:dstIndex put:(xlation at:char asciiValue). + srcIndex := srcIndex + 1. + dstIndex := dstIndex + 1 + ] ]. photometric := #palette. @@ -291,8 +283,11 @@ bitsPerSample := Array with:bitsPerPixel. " - XPMReader fromFile:'../fileIn/bitmaps/magtape.xpm' - XPMReader fromFile:'../fileIn/bitmaps/pixmap.xpm' - XPMReader fromFile:'../fileIn/bitmaps/ljet.xpm' + 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 initialize!