author | Claus Gittinger <cg@exept.de> |
Thu, 27 Feb 1997 12:05:52 +0100 | |
changeset 460 | 2c2215c50c28 |
parent 459 | 9cdd02f994a4 |
child 461 | bacef118f54a |
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. |
|
86 |
Currently only images with less than 50 colors are supported." |
|
87 |
||
88 |
anImage depth <= 6 ifTrue:[^ true]. |
|
89 |
anImage photometric == #palette ifTrue:[ |
|
90 |
anImage colorMap size < 50 ifTrue:[^ true]. |
|
91 |
]. |
|
92 |
^ anImage usedColors size < 50 |
|
93 |
||
94 |
"Modified: 27.2.1997 / 12:05:34 / cg" |
|
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 |
||
105 |
line := inStream nextLine. |
|
106 |
inStream close. |
|
107 |
line isNil ifTrue:[^ false]. |
|
108 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
109 |
^ true |
|
110 |
||
111 |
" |
|
112 |
XPMReader isValidImageFile:'bitmaps/magtape.xpm' |
|
113 |
XPMReader isValidImageFile:'bitmaps/ljet.xpm' |
|
114 |
XPMReader isValidImageFile:'bitmaps/garfield.gif' |
|
115 |
" |
|
116 |
! ! |
|
117 |
||
118 |
!XPMReader methodsFor:'reading from file'! |
|
119 |
||
120 |
colorNameFrom:aStream |
|
121 |
"read either a color-name or value specified in X-notation |
|
122 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
123 |
||
124 |
|s| |
|
125 |
||
126 |
aStream peek == $# ifTrue:[ |
|
28 | 127 |
aStream next. |
128 |
s := '#'. |
|
24 | 129 |
] ifFalse:[ |
28 | 130 |
s := ''. |
22 | 131 |
]. |
28 | 132 |
[aStream peek isLetterOrDigit] whileTrue:[ |
133 |
s := s copyWith:aStream next |
|
24 | 134 |
]. |
135 |
^ s |
|
22 | 136 |
! |
137 |
||
41 | 138 |
fromStream:aStream |
139 |
"read an XPM-image from aStream. Return the receiver |
|
140 |
(with all relevant instance variables set for the image) |
|
141 |
or nil on error" |
|
22 | 142 |
|
143 |
|line |
|
144 |
srcIndex "{ Class: SmallInteger }" |
|
145 |
dstIndex "{ Class: SmallInteger }" |
|
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
146 |
colorName monoName greyName symbolicName colorMapSize redMap greenMap blueMap |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
147 |
charsPerPixel xlation s bitsPerPixel char lineDone maskPixelValue |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
148 |
state| |
22 | 149 |
|
41 | 150 |
inStream := aStream. |
22 | 151 |
|
41 | 152 |
line := aStream nextLine. |
22 | 153 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
154 |
'XPM: format error (expected XPM)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
155 |
^ nil |
22 | 156 |
]. |
157 |
||
41 | 158 |
line := aStream nextLine. |
107 | 159 |
[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
|
160 |
line := aStream nextLine. |
22 | 161 |
]. |
162 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
163 |
'XPM: format error (expected static char)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
164 |
^ nil |
22 | 165 |
]. |
41 | 166 |
line := aStream nextLine. |
107 | 167 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
168 |
[line notNil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
169 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
170 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
171 |
]. |
22 | 172 |
]. |
173 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
174 |
'XPM: format error (expected "ww hh nn mm)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
175 |
^ nil |
22 | 176 |
]. |
177 |
s := ReadStream on:line. |
|
178 |
s next. "skip quote" |
|
179 |
width := Integer readFrom:s. |
|
180 |
height := Integer readFrom:s. |
|
181 |
colorMapSize := Integer readFrom:s. |
|
182 |
charsPerPixel := Integer readFrom:s. |
|
183 |
charsPerPixel ~~ 1 ifTrue:[ |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
184 |
'XPM: can only handle single-character xpm-files' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
185 |
^ nil |
22 | 186 |
]. |
187 |
xlation := Array new:256. |
|
188 |
||
107 | 189 |
redMap := ByteArray new:colorMapSize. |
190 |
greenMap := ByteArray new:colorMapSize. |
|
191 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 192 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
22 | 193 |
|
194 |
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
|
195 |
|index line color t word| |
22 | 196 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
197 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
198 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
199 |
line := aStream nextLine. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
200 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
201 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
202 |
'XPM: format error (expected color spec)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
203 |
^ nil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
204 |
]. |
24 | 205 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
206 |
s := ReadStream on:line. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
207 |
s next. "skip quote" |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
208 |
index := s next asciiValue. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
209 |
xlation at:index put:colorIndex - 1. |
22 | 210 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
211 |
lineDone := false. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
212 |
state := nil. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
213 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
214 |
[lineDone] whileFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
215 |
s skipSeparators. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
216 |
s peek == $# ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
217 |
word := self colorNameFrom:s |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
218 |
] ifFalse:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
219 |
word := s nextAlphaNumericWord. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
220 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
221 |
word isNil ifTrue:[ |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
222 |
lineDone := true |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
223 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
224 |
word = 's' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
225 |
"/ symbolic name ... |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
226 |
state := $s. symbolicName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
227 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
228 |
word = 'm' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
229 |
"/ monochrome data |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
230 |
state := $m. monoName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
231 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
232 |
word = 'g' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
233 |
"/ grey data |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
234 |
state := $g. greyName := ''. |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
235 |
] ifFalse:[ |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
236 |
word = 'c' ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
237 |
"/ color data |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
238 |
state := $c. colorName := ''. |
316
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 |
"/ append to name |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
241 |
state isNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
242 |
'XPM: format error got: ' errorPrint. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
243 |
word errorPrint. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
244 |
' (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
245 |
^ nil |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
246 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
247 |
|
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
248 |
state == $m ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
249 |
monoName := monoName , ' ' , word. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
250 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
251 |
state == $g ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
252 |
greyName := greyName , ' ' , word. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
253 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
254 |
state == $c ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
255 |
colorName := colorName , ' ' , word. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
256 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
257 |
state == $s ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
258 |
symbolicName := symbolicName , ' ' , word. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
259 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
260 |
(word startsWith:'#') ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
261 |
state := nil. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
262 |
] |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
263 |
] |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
264 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
265 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
266 |
] |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
267 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
268 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
269 |
|
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
270 |
colorName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
271 |
colorName := colorName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
272 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
273 |
monoName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
274 |
monoName := monoName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
275 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
276 |
greyName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
277 |
greyName := greyName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
278 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
279 |
symbolicName notNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
280 |
symbolicName := symbolicName withoutSeparators |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
281 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
282 |
|
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
283 |
"/ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
284 |
"/ for now - ignore everything, except |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
285 |
"/ colorName (if there is one) |
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 |
colorName isNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
288 |
colorName := greyName. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
289 |
colorName isNil ifTrue:[ |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
290 |
colorName := monoName. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
291 |
] |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
292 |
]. |
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
293 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
294 |
((colorName = 'none') or:[colorName = 'None']) ifTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
295 |
color := Color noColor. "/ white |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
296 |
redMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
297 |
greenMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
298 |
blueMap at:colorIndex put:0. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
299 |
maskPixelValue := colorIndex-1. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
300 |
] ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
301 |
color := Color name:colorName. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
302 |
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
|
303 |
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
|
304 |
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
|
305 |
]. |
22 | 306 |
]. |
307 |
||
308 |
"actually, could make it an image with less depth most of the time ..." |
|
309 |
||
310 |
" |
|
311 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1. |
|
312 |
" |
|
313 |
bitsPerPixel := 8. |
|
314 |
data := ByteArray new:(width * height). |
|
315 |
||
316 |
dstIndex := 1. |
|
317 |
1 to:height do:[:row | |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
318 |
line := aStream nextLine withoutSpaces. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
319 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
320 |
line := aStream nextLine withoutSpaces. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
321 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
322 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
323 |
'XPM: format error (expected pixels)' errorPrintNL. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
324 |
^ nil |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
325 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
326 |
srcIndex := 2. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
327 |
1 to: width do:[:col | |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
328 |
|char| |
22 | 329 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
330 |
char := line at:srcIndex. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
331 |
data at:dstIndex put:(xlation at:char asciiValue). |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
332 |
srcIndex := srcIndex + 1. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
333 |
dstIndex := dstIndex + 1 |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
334 |
] |
22 | 335 |
]. |
336 |
||
337 |
photometric := #palette. |
|
338 |
samplesPerPixel := 1. |
|
339 |
bitsPerSample := Array with:bitsPerPixel. |
|
340 |
||
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
341 |
maskPixelValue notNil ifTrue:[ |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
342 |
self buildMaskFromColor:maskPixelValue |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
343 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
344 |
|
24 | 345 |
" |
41 | 346 |
XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream) |
347 |
XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream) |
|
348 |
XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) |
|
349 |
XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream) |
|
107 | 350 |
" |
351 |
||
352 |
"Created: 24.9.1995 / 06:20:06 / claus" |
|
353 |
"Modified: 24.9.1995 / 07:07:33 / claus" |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
354 |
"Modified: 5.7.1996 / 17:27:59 / stefan" |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
355 |
"Modified: 24.1.1997 / 14:45:02 / cg" |
22 | 356 |
! ! |
41 | 357 |
|
459 | 358 |
!XPMReader methodsFor:'writing to a file'! |
359 |
||
360 |
colorNameOf:aColor |
|
361 |
#(white black red green blue |
|
362 |
yellow magenta cyan orange) do:[:aStandardColorName | |
|
363 |
aColor = (Color name:aStandardColorName) ifTrue:[ |
|
364 |
^ aStandardColorName. |
|
365 |
] |
|
366 |
]. |
|
367 |
^ '#' |
|
368 |
, (aColor redByte hexPrintString:2) |
|
369 |
, (aColor greenByte hexPrintString:2) |
|
370 |
, (aColor blueByte hexPrintString:2) |
|
371 |
||
372 |
"Created: 27.2.1997 / 11:48:40 / cg" |
|
373 |
! |
|
374 |
||
375 |
save:image onFile:aFileName |
|
376 |
"save image as XPM file on aFileName. |
|
377 |
Caveat: currently, only a maximum of roughly 50 colors is handled |
|
378 |
(i.e. very simple images)" |
|
379 |
||
380 |
|usedColors nColorsUsed nColors nChars baseName map maskColorIndex |
|
381 |
isMasked imageMask| |
|
382 |
||
383 |
usedColors := image usedColors. |
|
384 |
nColors := nColorsUsed := usedColors size. |
|
385 |
(imageMask := image mask) notNil ifTrue:[ |
|
386 |
nColors := nColors + 1. |
|
387 |
maskColorIndex := nColors. |
|
388 |
]. |
|
389 |
nColors > 256 ifTrue:[ |
|
390 |
'XPMReader [error]: cannot (yet) create multiBbyte representation (too many colors)' errorPrintCR. |
|
391 |
^ nil |
|
392 |
]. |
|
393 |
||
394 |
map := ($a to: $z) asOrderedCollection. |
|
395 |
map addAll:($A to: $Z) asOrderedCollection. |
|
396 |
map addAll:($0 to: $9) asOrderedCollection. |
|
397 |
map addAll:#($. $, $`) asOrderedCollection. |
|
398 |
nChars := 1. |
|
399 |
||
400 |
nColors > map size ifTrue:[ |
|
401 |
'XPMReader [error]: cannot (yet) create 2-byte representation (too many colors)' errorPrintCR. |
|
402 |
^ nil |
|
403 |
]. |
|
404 |
||
405 |
outStream := FileStream newFileNamed:aFileName. |
|
406 |
outStream isNil ifTrue:[ |
|
407 |
'XPMReader [warning]: create error' errorPrintCR. |
|
408 |
^ nil |
|
409 |
]. |
|
410 |
||
411 |
baseName := aFileName asFilename baseName asFilename withoutSuffix asString. |
|
412 |
||
413 |
outStream nextPutLine:'/* XPM */'. |
|
414 |
outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'. |
|
415 |
outStream nextPutLine:'/* width height ncolors chars_per_pixel */'. |
|
416 |
outStream nextPutLine:'"' , image width printString , ' ' |
|
417 |
, image height printString , ' ' |
|
418 |
, nColors printString , ' ' |
|
419 |
, nChars printString , '",'. |
|
420 |
||
421 |
||
422 |
outStream nextPutLine:'/* colors */'. |
|
423 |
1 to:nColorsUsed do:[:idx | |
|
424 |
|clr| |
|
425 |
||
426 |
clr := usedColors at:idx. |
|
427 |
outStream nextPutLine:'"' , (map at:idx) asString , |
|
428 |
' ' , 'c ' , (self colorNameOf:clr) , |
|
429 |
'",'. |
|
430 |
]. |
|
431 |
maskColorIndex notNil ifTrue:[ |
|
432 |
outStream nextPutLine:'"' , (map at:maskColorIndex) asString , |
|
433 |
' c none",'. |
|
434 |
]. |
|
435 |
||
436 |
outStream nextPutLine:'/* pixels */'. |
|
437 |
||
438 |
maskColorIndex isNil ifTrue:[ |
|
439 |
isMasked := false |
|
440 |
]. |
|
441 |
0 to:image height-1 do:[:y | |
|
442 |
outStream nextPutAll:'"'. |
|
443 |
0 to:image width-1 do:[:x | |
|
444 |
|clr isMasked idx| |
|
445 |
||
446 |
maskColorIndex notNil ifTrue:[ |
|
447 |
isMasked := (imageMask valueAtX:x y:y) == 0 |
|
448 |
]. |
|
449 |
isMasked ifTrue:[ |
|
450 |
outStream nextPut:(map at:maskColorIndex) |
|
451 |
] ifFalse:[ |
|
452 |
clr := image atX:x y:y. |
|
453 |
idx := usedColors indexOf:clr. |
|
454 |
outStream nextPut:(map at:idx) |
|
455 |
] |
|
456 |
]. |
|
457 |
outStream nextPutLine:'",'. |
|
458 |
]. |
|
459 |
outStream nextPutLine:'};' |
|
460 |
||
461 |
"Modified: 27.2.1997 / 11:49:34 / cg" |
|
462 |
! ! |
|
463 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
464 |
!XPMReader class methodsFor:'documentation'! |
203 | 465 |
|
466 |
version |
|
460 | 467 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.23 1997-02-27 11:05:52 cg Exp $' |
203 | 468 |
! ! |
41 | 469 |
XPMReader initialize! |