author | Claus Gittinger <cg@exept.de> |
Tue, 23 Apr 1996 13:06:56 +0200 | |
changeset 210 | 5405de794686 |
parent 203 | a38debd57097 |
child 234 | b6352d13e792 |
permissions | -rw-r--r-- |
22 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
28 | 3 |
All Rights Reserved |
22 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ImageReader subclass:#XPMReader |
|
203 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images support' |
|
22 | 18 |
! |
19 |
||
20 |
!XPMReader class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
28 | 25 |
All Rights Reserved |
22 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
this class provides methods for loading x-pixmap-file (xpm) images. |
|
203 | 39 |
|
24 | 40 |
These images are used (in X) for palette images (see ctwm or hp-vue for a lot |
203 | 41 |
of them). |
42 |
The code here is a hack - it may not work for all images |
|
43 |
(it works for the testfiles I got here). |
|
24 | 44 |
|
203 | 45 |
Limitations: |
46 |
only reads the full-color specification, ignoring monochrome |
|
47 |
and greyscale info. |
|
24 | 48 |
|
203 | 49 |
Can only handle single-character index. |
24 | 50 |
|
203 | 51 |
Does not (currently) handle none-colors (i.e. for image-masks). |
107 | 52 |
|
203 | 53 |
Only understands single-word color names (i.e. names with spaces |
54 |
are not supported) |
|
55 |
||
56 |
Image writing is not (yet) supported |
|
22 | 57 |
|
58 |
Suggestions: adapt & use the XPM library here. |
|
203 | 59 |
|
60 |
||
61 |
[See also:] |
|
62 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
210 | 63 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
203 | 64 |
XBMReader XWDReader |
22 | 65 |
" |
66 |
! ! |
|
67 |
||
28 | 68 |
!XPMReader class methodsFor:'initialization'! |
69 |
||
70 |
initialize |
|
203 | 71 |
"tell Image-class, that a new fileReader is present |
72 |
for the '.xpm' extension." |
|
73 |
||
28 | 74 |
Image fileFormats at:'.xpm' put:self. |
75 |
! ! |
|
76 |
||
22 | 77 |
!XPMReader class methodsFor:'testing'! |
78 |
||
79 |
isValidImageFile:aFileName |
|
80 |
"return true, if aFileName contains an x-bitmap-file image" |
|
81 |
||
82 |
|line inStream | |
|
83 |
||
84 |
inStream := self streamReadingFile:aFileName. |
|
85 |
inStream isNil ifTrue:[^ false]. |
|
86 |
||
87 |
line := inStream nextLine. |
|
88 |
inStream close. |
|
89 |
line isNil ifTrue:[^ false]. |
|
90 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
91 |
^ true |
|
92 |
||
93 |
" |
|
94 |
XPMReader isValidImageFile:'bitmaps/magtape.xpm' |
|
95 |
XPMReader isValidImageFile:'bitmaps/ljet.xpm' |
|
96 |
XPMReader isValidImageFile:'bitmaps/garfield.gif' |
|
97 |
" |
|
98 |
! ! |
|
99 |
||
100 |
!XPMReader methodsFor:'reading from file'! |
|
101 |
||
102 |
colorNameFrom:aStream |
|
103 |
"read either a color-name or value specified in X-notation |
|
104 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
105 |
||
106 |
|s| |
|
107 |
||
108 |
aStream peek == $# ifTrue:[ |
|
28 | 109 |
aStream next. |
110 |
s := '#'. |
|
24 | 111 |
] ifFalse:[ |
28 | 112 |
s := ''. |
22 | 113 |
]. |
28 | 114 |
[aStream peek isLetterOrDigit] whileTrue:[ |
115 |
s := s copyWith:aStream next |
|
24 | 116 |
]. |
117 |
^ s |
|
22 | 118 |
! |
119 |
||
41 | 120 |
fromStream:aStream |
121 |
"read an XPM-image from aStream. Return the receiver |
|
122 |
(with all relevant instance variables set for the image) |
|
123 |
or nil on error" |
|
22 | 124 |
|
125 |
|line |
|
126 |
srcIndex "{ Class: SmallInteger }" |
|
127 |
dstIndex "{ Class: SmallInteger }" |
|
128 |
colorName colorMapSize redMap greenMap blueMap |
|
129 |
charsPerPixel xlation s bitsPerPixel char lineDone| |
|
130 |
||
41 | 131 |
inStream := aStream. |
22 | 132 |
|
41 | 133 |
line := aStream nextLine. |
22 | 134 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
46 | 135 |
'XPM: format error (expected XPM)' errorPrintNL. |
136 |
^ nil |
|
22 | 137 |
]. |
138 |
||
41 | 139 |
line := aStream nextLine. |
107 | 140 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank]]] whileTrue:[ |
46 | 141 |
line := aStream nextLine. |
22 | 142 |
]. |
143 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
46 | 144 |
'XPM: format error (expected static char)' errorPrintNL. |
145 |
^ nil |
|
22 | 146 |
]. |
41 | 147 |
line := aStream nextLine. |
107 | 148 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
149 |
[line notNil |
|
150 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
|
151 |
line := aStream nextLine. |
|
152 |
]. |
|
22 | 153 |
]. |
154 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
46 | 155 |
'XPM: format error (expected "ww hh nn mm)' errorPrintNL. |
156 |
^ nil |
|
22 | 157 |
]. |
158 |
s := ReadStream on:line. |
|
159 |
s next. "skip quote" |
|
160 |
width := Integer readFrom:s. |
|
161 |
height := Integer readFrom:s. |
|
162 |
colorMapSize := Integer readFrom:s. |
|
163 |
charsPerPixel := Integer readFrom:s. |
|
164 |
charsPerPixel ~~ 1 ifTrue:[ |
|
46 | 165 |
'XPM: can only handle single-character xpm-files' errorPrintNL. |
166 |
^ nil |
|
22 | 167 |
]. |
168 |
xlation := Array new:256. |
|
169 |
||
107 | 170 |
redMap := ByteArray new:colorMapSize. |
171 |
greenMap := ByteArray new:colorMapSize. |
|
172 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 173 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
22 | 174 |
|
175 |
1 to:colorMapSize do:[:colorIndex | |
|
107 | 176 |
|index line color t| |
22 | 177 |
|
46 | 178 |
line := aStream nextLine. |
179 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
|
180 |
line := aStream nextLine. |
|
181 |
]. |
|
182 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
183 |
'XPM: format error (expected color spec)' errorPrintNL. |
|
184 |
^ nil |
|
185 |
]. |
|
24 | 186 |
|
46 | 187 |
s := ReadStream on:line. |
188 |
s next. "skip quote" |
|
189 |
index := s next asciiValue. |
|
190 |
xlation at:index put:colorIndex - 1. |
|
22 | 191 |
|
46 | 192 |
lineDone := false. |
193 |
[lineDone] whileFalse:[ |
|
194 |
s skipSeparators. |
|
195 |
char := s peek. |
|
196 |
char == $" ifTrue:[ |
|
197 |
lineDone := true |
|
198 |
] ifFalse:[ |
|
199 |
char == $s ifTrue:[ |
|
200 |
" |
|
201 |
symbolic name |
|
202 |
" |
|
203 |
s next. |
|
204 |
s skipSeparators. |
|
205 |
self colorNameFrom:s. |
|
206 |
s skipSeparators. |
|
207 |
] ifFalse:[ |
|
208 |
char == $m ifTrue:[ |
|
209 |
" |
|
210 |
monochrome data |
|
211 |
" |
|
212 |
s next. |
|
213 |
s skipSeparators. |
|
214 |
self colorNameFrom:s. |
|
215 |
s skipSeparators. |
|
216 |
] ifFalse:[ |
|
217 |
(char == $g) ifTrue:[ |
|
218 |
" |
|
219 |
greyscale data |
|
220 |
" |
|
221 |
s next. |
|
222 |
s peek == 4 ifTrue:[s next]. |
|
223 |
s skipSeparators. |
|
224 |
self colorNameFrom:s. |
|
225 |
s skipSeparators. |
|
226 |
] ifFalse:[ |
|
227 |
(char == $c) ifTrue:[ |
|
228 |
" |
|
229 |
color data |
|
230 |
" |
|
231 |
s next. |
|
232 |
s skipSeparators. |
|
233 |
colorName := self colorNameFrom:s. |
|
234 |
s skipSeparators. |
|
235 |
] ifFalse:[ |
|
236 |
'XPM: format error got: ' errorPrint. |
|
237 |
"/ char errorPrint. '(' errorPrint. char asciiValue printString errorPrint. |
|
238 |
"/ '); ' errorPrint. |
|
239 |
'(expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. |
|
240 |
^ nil |
|
241 |
]. |
|
242 |
] |
|
243 |
] |
|
244 |
] |
|
245 |
]. |
|
246 |
]. |
|
247 |
((colorName = 'none') or:[colorName = 'None']) ifTrue:[ |
|
248 |
"mhmh must add mask to Image-instances soon ..." |
|
116
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
249 |
color := Color noColor. "/ white |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
250 |
redMap at:colorIndex put:0. |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
251 |
greenMap at:colorIndex put:0. |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
252 |
blueMap at:colorIndex put:0. |
46 | 253 |
] ifFalse:[ |
254 |
color := Color name:colorName. |
|
116
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
255 |
redMap at:colorIndex put:(color red asFloat * 255.0 // 100). |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
256 |
greenMap at:colorIndex put:(color green asFloat * 255.0 // 100). |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
257 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100). |
46 | 258 |
]. |
22 | 259 |
]. |
260 |
||
261 |
"actually, could make it an image with less depth most of the time ..." |
|
262 |
||
263 |
" |
|
264 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1. |
|
265 |
" |
|
266 |
bitsPerPixel := 8. |
|
267 |
data := ByteArray new:(width * height). |
|
268 |
||
269 |
dstIndex := 1. |
|
270 |
1 to:height do:[:row | |
|
46 | 271 |
line := aStream nextLine withoutSpaces. |
272 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
|
273 |
line := aStream nextLine withoutSpaces. |
|
274 |
]. |
|
275 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
276 |
'XPM: format error (expected pixels)' errorPrintNL. |
|
277 |
^ nil |
|
278 |
]. |
|
279 |
srcIndex := 2. |
|
280 |
1 to: width do:[:col | |
|
281 |
|char| |
|
22 | 282 |
|
46 | 283 |
char := line at:srcIndex. |
284 |
data at:dstIndex put:(xlation at:char asciiValue). |
|
285 |
srcIndex := srcIndex + 1. |
|
286 |
dstIndex := dstIndex + 1 |
|
287 |
] |
|
22 | 288 |
]. |
289 |
||
290 |
photometric := #palette. |
|
291 |
samplesPerPixel := 1. |
|
292 |
bitsPerSample := Array with:bitsPerPixel. |
|
293 |
||
24 | 294 |
" |
41 | 295 |
XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream) |
296 |
XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream) |
|
297 |
XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) |
|
298 |
XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream) |
|
107 | 299 |
" |
300 |
||
301 |
"Created: 24.9.1995 / 06:20:06 / claus" |
|
302 |
"Modified: 24.9.1995 / 07:07:33 / claus" |
|
22 | 303 |
! ! |
41 | 304 |
|
203 | 305 |
!XPMReader class methodsFor:'documentation'! |
306 |
||
307 |
version |
|
210 | 308 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.15 1996-04-23 11:06:06 cg Exp $' |
203 | 309 |
! ! |
41 | 310 |
XPMReader initialize! |