XPMReader.st
changeset 46 c49b204c2ef0
parent 41 66edc847b9c8
child 96 948318b2fbd4
equal deleted inserted replaced
45:f94fc6118d0a 46:c49b204c2ef0
    21 
    21 
    22 XPMReader comment:'
    22 XPMReader comment:'
    23 COPYRIGHT (c) 1994 by Claus Gittinger
    23 COPYRIGHT (c) 1994 by Claus Gittinger
    24 	      All Rights Reserved
    24 	      All Rights Reserved
    25 
    25 
    26 $Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.6 1995-02-18 15:52:52 claus Exp $
    26 $Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.7 1995-02-22 01:19:52 claus Exp $
    27 '!
    27 '!
    28 
    28 
    29 !XPMReader class methodsFor:'documentation'!
    29 !XPMReader class methodsFor:'documentation'!
    30 
    30 
    31 copyright
    31 copyright
    42 "
    42 "
    43 !
    43 !
    44 
    44 
    45 version
    45 version
    46 "
    46 "
    47 $Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.6 1995-02-18 15:52:52 claus Exp $
    47 $Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.7 1995-02-22 01:19:52 claus Exp $
    48 "
    48 "
    49 !
    49 !
    50 
    50 
    51 documentation
    51 documentation
    52 "
    52 "
   130 
   130 
   131     inStream := aStream.
   131     inStream := aStream.
   132 
   132 
   133     line := aStream nextLine.
   133     line := aStream nextLine.
   134     (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
   134     (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
   135         'XPM: format error (expected XPM)' errorPrintNL.
   135 	'XPM: format error (expected XPM)' errorPrintNL.
   136         ^ nil
   136 	^ nil
   137     ].
   137     ].
   138 
   138 
   139     line := aStream nextLine.
   139     line := aStream nextLine.
   140     [line notNil and:[line startsWith:'/*']] whileTrue:[
   140     [line notNil and:[line startsWith:'/*']] whileTrue:[
   141         line := aStream nextLine.
   141 	line := aStream nextLine.
   142     ].
   142     ].
   143     (line notNil and:[line startsWith:'static char']) ifFalse:[
   143     (line notNil and:[line startsWith:'static char']) ifFalse:[
   144         'XPM: format error (expected static char)' errorPrintNL.
   144 	'XPM: format error (expected static char)' errorPrintNL.
   145         ^ nil
   145 	^ nil
   146     ].
   146     ].
   147     line := aStream nextLine.
   147     line := aStream nextLine.
   148     [line notNil and:[line startsWith:'/*']] whileTrue:[
   148     [line notNil and:[line startsWith:'/*']] whileTrue:[
   149         line := aStream nextLine.
   149 	line := aStream nextLine.
   150     ].
   150     ].
   151     (line notNil and:[line startsWith:'"']) ifFalse:[
   151     (line notNil and:[line startsWith:'"']) ifFalse:[
   152         'XPM: format error (expected "ww hh nn mm)' errorPrintNL.
   152 	'XPM: format error (expected "ww hh nn mm)' errorPrintNL.
   153         ^ nil
   153 	^ nil
   154     ].
   154     ].
   155     s := ReadStream on:line.
   155     s := ReadStream on:line.
   156     s next.  "skip quote"
   156     s next.  "skip quote"
   157     width := Integer readFrom:s.
   157     width := Integer readFrom:s.
   158     height := Integer readFrom:s.
   158     height := Integer readFrom:s.
   159     colorMapSize := Integer readFrom:s.
   159     colorMapSize := Integer readFrom:s.
   160     charsPerPixel := Integer readFrom:s.
   160     charsPerPixel := Integer readFrom:s.
   161     charsPerPixel ~~ 1 ifTrue:[
   161     charsPerPixel ~~ 1 ifTrue:[
   162         'XPM: can only handle single-character xpm-files' errorPrintNL.
   162 	'XPM: can only handle single-character xpm-files' errorPrintNL.
   163         ^ nil
   163 	^ nil
   164     ].
   164     ].
   165     xlation := Array new:256.
   165     xlation := Array new:256.
   166 
   166 
   167     redMap := Array new:colorMapSize.
   167     redMap := Array new:colorMapSize.
   168     greenMap := Array new:colorMapSize.
   168     greenMap := Array new:colorMapSize.
   169     blueMap := Array new:colorMapSize.
   169     blueMap := Array new:colorMapSize.
   170     colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
   170     colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
   171 
   171 
   172     1 to:colorMapSize do:[:colorIndex |
   172     1 to:colorMapSize do:[:colorIndex |
   173         |index line color|
   173 	|index line color|
   174 
   174 
   175         line := aStream nextLine.
   175 	line := aStream nextLine.
   176         [line notNil and:[line startsWith:'/*']] whileTrue:[
   176 	[line notNil and:[line startsWith:'/*']] whileTrue:[
   177             line := aStream nextLine.
   177 	    line := aStream nextLine.
   178         ].
   178 	].
   179         (line notNil and:[line startsWith:'"']) ifFalse:[
   179 	(line notNil and:[line startsWith:'"']) ifFalse:[
   180             'XPM: format error (expected color spec)' errorPrintNL.
   180 	    'XPM: format error (expected color spec)' errorPrintNL.
   181             ^ nil
   181 	    ^ nil
   182         ].
   182 	].
   183 
   183 
   184         s := ReadStream on:line.
   184 	s := ReadStream on:line.
   185         s next. "skip quote"
   185 	s next. "skip quote"
   186         index := s next asciiValue.
   186 	index := s next asciiValue.
   187         xlation at:index put:colorIndex - 1.
   187 	xlation at:index put:colorIndex - 1.
   188 
   188 
   189         lineDone := false.
   189 	lineDone := false.
   190         [lineDone] whileFalse:[
   190 	[lineDone] whileFalse:[
   191             s skipSeparators.
   191 	    s skipSeparators.
   192             char := s peek.
   192 	    char := s peek.
   193             char == $" ifTrue:[
   193 	    char == $" ifTrue:[
   194                 lineDone := true
   194 		lineDone := true
   195             ] ifFalse:[
   195 	    ] ifFalse:[
   196                 char == $s ifTrue:[
   196 		char == $s ifTrue:[
   197                     "
   197 		    "
   198                      symbolic name
   198 		     symbolic name
   199                     "
   199 		    "
   200                     s next.
   200 		    s next.
   201                     s skipSeparators.
   201 		    s skipSeparators.
   202                     s nextWord.    
   202 		    self colorNameFrom:s.  
   203                     s skipSeparators.
   203 		    s skipSeparators.
   204                 ] ifFalse:[
   204 		] ifFalse:[
   205                     char == $m ifTrue:[
   205 		    char == $m ifTrue:[
   206                         "
   206 			"
   207                          monochrome data
   207 			 monochrome data
   208                         "
   208 			"
   209                         s next.
   209 			s next.
   210                         s skipSeparators.
   210 			s skipSeparators.
   211                         s nextWord.
   211 			self colorNameFrom:s.
   212                         s skipSeparators.
   212 			s skipSeparators.
   213                     ] ifFalse:[
   213 		    ] ifFalse:[
   214                         (char == $g) ifTrue:[
   214 			(char == $g) ifTrue:[
   215                             "
   215 			    "
   216                              greyscale data
   216 			     greyscale data
   217                             "
   217 			    "
   218                             s next.
   218 			    s next.
   219                             s peek == 4 ifTrue:[s next].
   219 			    s peek == 4 ifTrue:[s next].
   220                             s skipSeparators.
   220 			    s skipSeparators.
   221                             s nextWord.
   221 			    self colorNameFrom:s.
   222                             s skipSeparators.
   222 			    s skipSeparators.
   223                         ] ifFalse:[
   223 			] ifFalse:[
   224                             (char == $c) ifTrue:[
   224 			    (char == $c) ifTrue:[
   225                                 "
   225 				"
   226                                  color data
   226 				 color data
   227                                 "
   227 				"
   228                                 s next.
   228 				s next.
   229                                 s skipSeparators.
   229 				s skipSeparators.
   230                                 colorName := self colorNameFrom:s.
   230 				colorName := self colorNameFrom:s.
   231                                 s skipSeparators.
   231 				s skipSeparators.
   232                             ] ifFalse:[
   232 			    ] ifFalse:[
   233                                 'XPM: format error (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL.
   233 				'XPM: format error got: ' errorPrint.
   234                                 ^ nil
   234 "/                                char errorPrint. '(' errorPrint. char asciiValue printString errorPrint.
   235                             ].
   235 "/                                '); ' errorPrint.
   236                         ]
   236 				 '(expected ''c'',''m'',''g'' or ''s'')' errorPrintNL.
   237                     ]
   237 				^ nil
   238                 ]
   238 			    ].
   239             ].
   239 			]
   240         ].
   240 		    ]
   241         ((colorName = 'none') or:[colorName = 'None']) ifTrue:[
   241 		]
   242             "mhmh must add mask to Image-instances soon ..."
   242 	    ].
   243             color := Color white
   243 	].
   244         ] ifFalse:[
   244 	((colorName = 'none') or:[colorName = 'None']) ifTrue:[
   245             color := Color name:colorName.
   245 	    "mhmh must add mask to Image-instances soon ..."
   246         ].
   246 	    color := Color white
   247         redMap at:colorIndex put:(color red * 255 // 100).
   247 	] ifFalse:[
   248         greenMap at:colorIndex put:(color green * 255 // 100).
   248 	    color := Color name:colorName.
   249         blueMap at:colorIndex put:(color blue * 255 // 100).
   249 	].
       
   250 	redMap at:colorIndex put:(color red * 255 // 100).
       
   251 	greenMap at:colorIndex put:(color green * 255 // 100).
       
   252 	blueMap at:colorIndex put:(color blue * 255 // 100).
   250     ].
   253     ].
   251 
   254 
   252     "actually, could make it an image with less depth most of the time ..."
   255     "actually, could make it an image with less depth most of the time ..."
   253 
   256 
   254 "
   257 "
   257     bitsPerPixel := 8.
   260     bitsPerPixel := 8.
   258     data := ByteArray new:(width * height).
   261     data := ByteArray new:(width * height).
   259 
   262 
   260     dstIndex := 1.
   263     dstIndex := 1.
   261     1 to:height do:[:row |
   264     1 to:height do:[:row |
   262         line := aStream nextLine withoutSpaces.
   265 	line := aStream nextLine withoutSpaces.
   263         [line notNil and:[line startsWith:'/*']] whileTrue:[
   266 	[line notNil and:[line startsWith:'/*']] whileTrue:[
   264             line := aStream nextLine withoutSpaces.
   267 	    line := aStream nextLine withoutSpaces.
   265         ].
   268 	].
   266         (line notNil and:[line startsWith:'"']) ifFalse:[
   269 	(line notNil and:[line startsWith:'"']) ifFalse:[
   267             'XPM: format error (expected pixels)' errorPrintNL.
   270 	    'XPM: format error (expected pixels)' errorPrintNL.
   268             ^ nil
   271 	    ^ nil
   269         ].
   272 	].
   270         srcIndex := 2.
   273 	srcIndex := 2.
   271         1 to: width do:[:col |
   274 	1 to: width do:[:col |
   272             |char|
   275 	    |char|
   273 
   276 
   274             char := line at:srcIndex.
   277 	    char := line at:srcIndex.
   275             data at:dstIndex put:(xlation at:char asciiValue).
   278 	    data at:dstIndex put:(xlation at:char asciiValue).
   276             srcIndex := srcIndex + 1.
   279 	    srcIndex := srcIndex + 1.
   277             dstIndex := dstIndex + 1
   280 	    dstIndex := dstIndex + 1
   278         ]
   281 	]
   279     ].
   282     ].
   280 
   283 
   281     photometric := #palette.
   284     photometric := #palette.
   282     samplesPerPixel := 1.
   285     samplesPerPixel := 1.
   283     bitsPerSample := Array with:bitsPerPixel.
   286     bitsPerSample := Array with:bitsPerPixel.