author | Claus Gittinger <cg@exept.de> |
Thu, 24 Apr 1997 18:08:20 +0200 | |
changeset 560 | 4cce4f58bbbf |
parent 550 | 254480517864 |
child 562 | 921472d151d9 |
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:'' |
|
259 | 17 |
category:'Graphics-Images-Support' |
22 | 18 |
! |
19 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
20 |
!XPMReader class methodsFor:'documentation'! |
22 | 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 |
Only understands single-word color names (i.e. names with spaces |
52 |
are not supported) |
|
53 |
||
459 | 54 |
Image writing is only supported for images with less than about 50 |
55 |
colors (single byte encoding). If present, the mask must be a |
|
56 |
single bit mask (i.e. no alpha channel). |
|
57 |
Due to the algorithm, writing may be slow for big images |
|
22 | 58 |
|
59 |
Suggestions: adapt & use the XPM library here. |
|
203 | 60 |
|
61 |
||
62 |
[See also:] |
|
234 | 63 |
Image Form Icon |
203 | 64 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 65 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
203 | 66 |
XBMReader XWDReader |
22 | 67 |
" |
68 |
! ! |
|
69 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
70 |
!XPMReader class methodsFor:'initialization'! |
28 | 71 |
|
72 |
initialize |
|
203 | 73 |
"tell Image-class, that a new fileReader is present |
400 | 74 |
for the '.xpm' and '.pm' extensions." |
203 | 75 |
|
398 | 76 |
Image addReader:self suffix:'xpm'. |
400 | 77 |
Image addReader:self suffix:'pm'. |
398 | 78 |
|
400 | 79 |
"Modified: 1.2.1997 / 15:10:29 / cg" |
28 | 80 |
! ! |
81 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
82 |
!XPMReader class methodsFor:'testing'! |
22 | 83 |
|
460 | 84 |
canRepresent:anImage |
85 |
"return true, if anImage can be represented in my file format. |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
86 |
Currently only images with less than 80 colors are supported." |
460 | 87 |
|
88 |
anImage depth <= 6 ifTrue:[^ true]. |
|
89 |
anImage photometric == #palette ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
90 |
anImage colorMap size < 80 ifTrue:[^ true]. |
460 | 91 |
]. |
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
92 |
^ anImage usedColors size < 80 |
460 | 93 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
94 |
"Modified: 27.2.1997 / 12:40:22 / cg" |
460 | 95 |
! |
96 |
||
22 | 97 |
isValidImageFile:aFileName |
98 |
"return true, if aFileName contains an x-bitmap-file image" |
|
99 |
||
100 |
|line inStream | |
|
101 |
||
102 |
inStream := self streamReadingFile:aFileName. |
|
103 |
inStream isNil ifTrue:[^ false]. |
|
104 |
||
550 | 105 |
Stream readErrorSignal handle:[:ex | |
106 |
line := nil |
|
107 |
] do:[ |
|
108 |
line := inStream nextLine. |
|
109 |
]. |
|
22 | 110 |
inStream close. |
111 |
line isNil ifTrue:[^ false]. |
|
112 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
113 |
^ true |
|
114 |
||
115 |
" |
|
116 |
XPMReader isValidImageFile:'bitmaps/magtape.xpm' |
|
117 |
XPMReader isValidImageFile:'bitmaps/ljet.xpm' |
|
118 |
XPMReader isValidImageFile:'bitmaps/garfield.gif' |
|
550 | 119 |
" |
120 |
||
121 |
"Modified: 21.4.1997 / 17:10:35 / cg" |
|
22 | 122 |
! ! |
123 |
||
124 |
!XPMReader methodsFor:'reading from file'! |
|
125 |
||
126 |
colorNameFrom:aStream |
|
127 |
"read either a color-name or value specified in X-notation |
|
128 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
129 |
||
130 |
|s| |
|
131 |
||
132 |
aStream peek == $# ifTrue:[ |
|
28 | 133 |
aStream next. |
134 |
s := '#'. |
|
24 | 135 |
] ifFalse:[ |
28 | 136 |
s := ''. |
22 | 137 |
]. |
28 | 138 |
[aStream peek isLetterOrDigit] whileTrue:[ |
139 |
s := s copyWith:aStream next |
|
24 | 140 |
]. |
141 |
^ s |
|
22 | 142 |
! |
143 |
||
41 | 144 |
fromStream:aStream |
145 |
"read an XPM-image from aStream. Return the receiver |
|
146 |
(with all relevant instance variables set for the image) |
|
147 |
or nil on error" |
|
22 | 148 |
|
149 |
|line |
|
150 |
srcIndex "{ Class: SmallInteger }" |
|
151 |
dstIndex "{ Class: SmallInteger }" |
|
470 | 152 |
colorName monoName greyName grey4Name symbolicName colorMapSize redMap greenMap blueMap |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
153 |
charsPerPixel xlation s bitsPerPixel char lineDone maskPixelValue |
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
154 |
state key| |
22 | 155 |
|
41 | 156 |
inStream := aStream. |
22 | 157 |
|
41 | 158 |
line := aStream nextLine. |
22 | 159 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
160 |
'XPM: format error (expected XPM)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
161 |
^ nil |
22 | 162 |
]. |
163 |
||
41 | 164 |
line := aStream nextLine. |
107 | 165 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank]]] whileTrue:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
166 |
line := aStream nextLine. |
22 | 167 |
]. |
168 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
169 |
'XPM: format error (expected static char)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
170 |
^ nil |
22 | 171 |
]. |
41 | 172 |
line := aStream nextLine. |
107 | 173 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
174 |
[line notNil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
175 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
176 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
177 |
]. |
22 | 178 |
]. |
471
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
179 |
line notNil ifTrue:[ |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
180 |
line := line withoutSeparators |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
181 |
]. |
22 | 182 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
183 |
'XPM: format error (expected "ww hh nn mm)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
184 |
^ nil |
22 | 185 |
]. |
186 |
s := ReadStream on:line. |
|
187 |
s next. "skip quote" |
|
188 |
width := Integer readFrom:s. |
|
189 |
height := Integer readFrom:s. |
|
190 |
colorMapSize := Integer readFrom:s. |
|
191 |
charsPerPixel := Integer readFrom:s. |
|
192 |
charsPerPixel ~~ 1 ifTrue:[ |
|
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
193 |
xlation := Dictionary new:colorMapSize. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
194 |
] ifFalse:[ |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
195 |
xlation := Array new:256. |
22 | 196 |
]. |
197 |
||
107 | 198 |
redMap := ByteArray new:colorMapSize. |
199 |
greenMap := ByteArray new:colorMapSize. |
|
200 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 201 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
22 | 202 |
1 to:colorMapSize do:[:colorIndex | |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
203 |
|index line color t word| |
22 | 204 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
205 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
206 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
207 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
208 |
]. |
471
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
209 |
line notNil ifTrue:[ |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
210 |
line := line withoutSeparators |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
211 |
]. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
212 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
213 |
'XPM: format error (expected color spec)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
214 |
^ nil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
215 |
]. |
24 | 216 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
217 |
s := ReadStream on:line. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
218 |
s next. "skip quote" |
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
219 |
charsPerPixel ~~ 1 ifTrue:[ |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
220 |
key := s next:charsPerPixel. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
221 |
xlation at:key put:colorIndex - 1. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
222 |
] ifFalse:[ |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
223 |
index := s next asciiValue. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
224 |
xlation at:index put:colorIndex - 1. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
225 |
]. |
22 | 226 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
227 |
lineDone := false. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
228 |
state := nil. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
229 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
230 |
[lineDone] whileFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
231 |
s skipSeparators. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
232 |
s peek == $# ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
233 |
word := self colorNameFrom:s |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
234 |
] ifFalse:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
235 |
word := s nextAlphaNumericWord. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
236 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
237 |
word isNil ifTrue:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
238 |
lineDone := true |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
239 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
240 |
word = 's' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
241 |
"/ symbolic name ... |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
242 |
state := $s. symbolicName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
243 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
244 |
word = 'm' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
245 |
"/ monochrome data |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
246 |
state := $m. monoName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
247 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
248 |
word = 'g' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
249 |
"/ grey data |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
250 |
state := $g. greyName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
251 |
] ifFalse:[ |
470 | 252 |
word = 'g4' ifTrue:[ |
253 |
"/ grey data |
|
254 |
state := $G. grey4Name := ''. |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
255 |
] ifFalse:[ |
470 | 256 |
word = 'c' ifTrue:[ |
257 |
"/ color data |
|
258 |
state := $c. colorName := ''. |
|
259 |
] ifFalse:[ |
|
260 |
"/ append to name |
|
261 |
state isNil ifTrue:[ |
|
262 |
'XPM: format error got: ' errorPrint. |
|
263 |
word errorPrint. |
|
264 |
' (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. |
|
265 |
^ nil |
|
266 |
]. |
|
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
267 |
|
470 | 268 |
state == $m ifTrue:[ |
269 |
monoName := monoName , ' ' , word. |
|
270 |
]. |
|
271 |
state == $g ifTrue:[ |
|
272 |
greyName := greyName , ' ' , word. |
|
273 |
]. |
|
274 |
state == $G ifTrue:[ |
|
275 |
grey4Name := grey4Name , ' ' , word. |
|
276 |
]. |
|
277 |
state == $c ifTrue:[ |
|
278 |
colorName := colorName , ' ' , word. |
|
279 |
]. |
|
280 |
state == $s ifTrue:[ |
|
281 |
symbolicName := symbolicName , ' ' , word. |
|
282 |
]. |
|
283 |
(word startsWith:'#') ifTrue:[ |
|
284 |
state := nil. |
|
285 |
] |
|
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
286 |
] |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
287 |
] |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
288 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
289 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
290 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
291 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
292 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
293 |
|
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
294 |
colorName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
295 |
colorName := colorName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
296 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
297 |
monoName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
298 |
monoName := monoName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
299 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
300 |
greyName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
301 |
greyName := greyName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
302 |
]. |
470 | 303 |
grey4Name notNil ifTrue:[ |
304 |
grey4Name := grey4Name withoutSeparators |
|
305 |
]. |
|
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
306 |
symbolicName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
307 |
symbolicName := symbolicName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
308 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
309 |
|
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
310 |
"/ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
311 |
"/ for now - ignore everything, except |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
312 |
"/ colorName (if there is one) |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
313 |
"/ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
314 |
colorName isNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
315 |
colorName := greyName. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
316 |
colorName isNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
317 |
colorName := monoName. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
318 |
] |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
319 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
320 |
|
470 | 321 |
((colorName = 'none') |
322 |
or:[colorName = 'None' |
|
323 |
or:[colorName = 'NONE']]) ifTrue:[ |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
324 |
color := Color noColor. "/ white |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
325 |
redMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
326 |
greenMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
327 |
blueMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
328 |
maskPixelValue := colorIndex-1. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
329 |
] ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
330 |
color := Color name:colorName. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
331 |
redMap at:colorIndex put:(color red asFloat * 255.0 // 100). |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
332 |
greenMap at:colorIndex put:(color green asFloat * 255.0 // 100). |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
333 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100). |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
334 |
]. |
22 | 335 |
]. |
336 |
||
337 |
"actually, could make it an image with less depth most of the time ..." |
|
338 |
||
339 |
" |
|
340 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1. |
|
341 |
" |
|
342 |
bitsPerPixel := 8. |
|
343 |
data := ByteArray new:(width * height). |
|
344 |
||
345 |
dstIndex := 1. |
|
346 |
1 to:height do:[:row | |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
347 |
line := aStream nextLine withoutSpaces. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
348 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
349 |
line := aStream nextLine withoutSpaces. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
350 |
]. |
471
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
351 |
line notNil ifTrue:[ |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
352 |
line := line withoutSeparators |
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
353 |
]. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
354 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
355 |
'XPM: format error (expected pixels)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
356 |
^ nil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
357 |
]. |
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
358 |
charsPerPixel ~~ 1 ifTrue:[ |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
359 |
s := line readStream. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
360 |
s next. "/ skip dquote |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
361 |
1 to: width do:[:col | |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
362 |
|key| |
22 | 363 |
|
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
364 |
key := s next:charsPerPixel. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
365 |
data at:dstIndex put:(xlation at:key). |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
366 |
dstIndex := dstIndex + 1 |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
367 |
] |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
368 |
] ifFalse:[ |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
369 |
srcIndex := 2. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
370 |
1 to: width do:[:col | |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
371 |
|char| |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
372 |
|
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
373 |
char := line at:srcIndex. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
374 |
data at:dstIndex put:(xlation at:char asciiValue). |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
375 |
srcIndex := srcIndex + 1. |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
376 |
dstIndex := dstIndex + 1 |
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
377 |
] |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
378 |
] |
22 | 379 |
]. |
380 |
||
381 |
photometric := #palette. |
|
382 |
samplesPerPixel := 1. |
|
383 |
bitsPerSample := Array with:bitsPerPixel. |
|
384 |
||
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
385 |
maskPixelValue notNil ifTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
386 |
self buildMaskFromColor:maskPixelValue |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
387 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
388 |
|
24 | 389 |
" |
41 | 390 |
XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream) |
391 |
XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream) |
|
392 |
XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) |
|
393 |
XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream) |
|
107 | 394 |
" |
395 |
||
396 |
"Created: 24.9.1995 / 06:20:06 / claus" |
|
397 |
"Modified: 24.9.1995 / 07:07:33 / claus" |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
398 |
"Modified: 5.7.1996 / 17:27:59 / stefan" |
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
399 |
"Modified: 4.4.1997 / 15:01:28 / cg" |
22 | 400 |
! ! |
41 | 401 |
|
459 | 402 |
!XPMReader methodsFor:'writing to a file'! |
403 |
||
404 |
colorNameOf:aColor |
|
405 |
#(white black red green blue |
|
406 |
yellow magenta cyan orange) do:[:aStandardColorName | |
|
407 |
aColor = (Color name:aStandardColorName) ifTrue:[ |
|
408 |
^ aStandardColorName. |
|
409 |
] |
|
410 |
]. |
|
411 |
^ '#' |
|
412 |
, (aColor redByte hexPrintString:2) |
|
413 |
, (aColor greenByte hexPrintString:2) |
|
414 |
, (aColor blueByte hexPrintString:2) |
|
415 |
||
416 |
"Created: 27.2.1997 / 11:48:40 / cg" |
|
417 |
! |
|
418 |
||
419 |
save:image onFile:aFileName |
|
420 |
"save image as XPM file on aFileName. |
|
421 |
Caveat: currently, only a maximum of roughly 50 colors is handled |
|
422 |
(i.e. very simple images)" |
|
423 |
||
424 |
|usedColors nColorsUsed nColors nChars baseName map maskColorIndex |
|
425 |
isMasked imageMask| |
|
426 |
||
427 |
usedColors := image usedColors. |
|
428 |
nColors := nColorsUsed := usedColors size. |
|
429 |
(imageMask := image mask) notNil ifTrue:[ |
|
430 |
nColors := nColors + 1. |
|
431 |
maskColorIndex := nColors. |
|
432 |
]. |
|
433 |
nColors > 256 ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
434 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
435 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
436 |
errorString:('XPMReader cannot represent this image (too many colors)'). |
459 | 437 |
]. |
438 |
||
439 |
map := ($a to: $z) asOrderedCollection. |
|
440 |
map addAll:($A to: $Z) asOrderedCollection. |
|
441 |
map addAll:($0 to: $9) asOrderedCollection. |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
442 |
map addAll:#($. $, $` $^ $* $: $; $< $> $? $% $# $& $( $) $- $+ $=) asOrderedCollection. |
459 | 443 |
nChars := 1. |
444 |
||
445 |
nColors > map size ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
446 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
447 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
448 |
errorString:('XPMReader cannot represent this image (too many colors)'). |
459 | 449 |
]. |
450 |
||
451 |
outStream := FileStream newFileNamed:aFileName. |
|
452 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
453 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
454 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
455 |
errorString:('file creation error: ' , aFileName asString). |
459 | 456 |
]. |
457 |
||
458 |
baseName := aFileName asFilename baseName asFilename withoutSuffix asString. |
|
459 |
||
460 |
outStream nextPutLine:'/* XPM */'. |
|
461 |
outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'. |
|
462 |
outStream nextPutLine:'/* width height ncolors chars_per_pixel */'. |
|
463 |
outStream nextPutLine:'"' , image width printString , ' ' |
|
464 |
, image height printString , ' ' |
|
465 |
, nColors printString , ' ' |
|
466 |
, nChars printString , '",'. |
|
467 |
||
468 |
||
469 |
outStream nextPutLine:'/* colors */'. |
|
470 |
1 to:nColorsUsed do:[:idx | |
|
471 |
|clr| |
|
472 |
||
473 |
clr := usedColors at:idx. |
|
474 |
outStream nextPutLine:'"' , (map at:idx) asString , |
|
475 |
' ' , 'c ' , (self colorNameOf:clr) , |
|
476 |
'",'. |
|
477 |
]. |
|
478 |
maskColorIndex notNil ifTrue:[ |
|
479 |
outStream nextPutLine:'"' , (map at:maskColorIndex) asString , |
|
480 |
' c none",'. |
|
481 |
]. |
|
482 |
||
483 |
outStream nextPutLine:'/* pixels */'. |
|
484 |
||
485 |
maskColorIndex isNil ifTrue:[ |
|
486 |
isMasked := false |
|
487 |
]. |
|
560 | 488 |
|
459 | 489 |
0 to:image height-1 do:[:y | |
490 |
outStream nextPutAll:'"'. |
|
491 |
0 to:image width-1 do:[:x | |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
492 |
|clr idx| |
459 | 493 |
|
494 |
maskColorIndex notNil ifTrue:[ |
|
560 | 495 |
isMasked := (imageMask pixelAtX:x y:y) == 0 |
459 | 496 |
]. |
497 |
isMasked ifTrue:[ |
|
498 |
outStream nextPut:(map at:maskColorIndex) |
|
499 |
] ifFalse:[ |
|
500 |
clr := image atX:x y:y. |
|
501 |
idx := usedColors indexOf:clr. |
|
502 |
outStream nextPut:(map at:idx) |
|
503 |
] |
|
504 |
]. |
|
505 |
outStream nextPutLine:'",'. |
|
506 |
]. |
|
469
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
507 |
outStream nextPutLine:'};'. |
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
508 |
outStream close. |
459 | 509 |
|
560 | 510 |
"Modified: 24.4.1997 / 16:56:08 / cg" |
459 | 511 |
! ! |
512 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
513 |
!XPMReader class methodsFor:'documentation'! |
203 | 514 |
|
515 |
version |
|
560 | 516 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.30 1997-04-24 16:08:20 cg Exp $' |
203 | 517 |
! ! |
41 | 518 |
XPMReader initialize! |