author | Claus Gittinger <cg@exept.de> |
Fri, 04 Apr 2003 19:07:54 +0200 | |
changeset 1737 | a1ed08195ee7 |
parent 1710 | ad35e6a18e98 |
child 1743 | 2f206c9f1319 |
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 |
||
1400 | 13 |
"{ Package: 'stx:libview2' }" |
14 |
||
22 | 15 |
ImageReader subclass:#XPMReader |
1737 | 16 |
instanceVariableNames:'charsPerPixel maskPixelValue characterTranslation' |
203 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
259 | 19 |
category:'Graphics-Images-Support' |
22 | 20 |
! |
21 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
22 |
!XPMReader class methodsFor:'documentation'! |
22 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
28 | 27 |
All Rights Reserved |
22 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
this class provides methods for loading x-pixmap-file (xpm) images. |
|
203 | 41 |
|
24 | 42 |
These images are used (in X) for palette images (see ctwm or hp-vue for a lot |
203 | 43 |
of them). |
44 |
The code here is a hack - it may not work for all images |
|
45 |
(it works for the testfiles I got here). |
|
24 | 46 |
|
203 | 47 |
Limitations: |
48 |
only reads the full-color specification, ignoring monochrome |
|
49 |
and greyscale info. |
|
24 | 50 |
|
203 | 51 |
Can only handle single-character index. |
24 | 52 |
|
203 | 53 |
Only understands single-word color names (i.e. names with spaces |
54 |
are not supported) |
|
55 |
||
459 | 56 |
Image writing is only supported for images with less than about 50 |
57 |
colors (single byte encoding). If present, the mask must be a |
|
58 |
single bit mask (i.e. no alpha channel). |
|
59 |
Due to the algorithm, writing may be slow for big images |
|
22 | 60 |
|
61 |
Suggestions: adapt & use the XPM library here. |
|
203 | 62 |
|
63 |
||
64 |
[See also:] |
|
234 | 65 |
Image Form Icon |
203 | 66 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 67 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
203 | 68 |
XBMReader XWDReader |
22 | 69 |
" |
70 |
! ! |
|
71 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
72 |
!XPMReader class methodsFor:'initialization'! |
28 | 73 |
|
74 |
initialize |
|
203 | 75 |
"tell Image-class, that a new fileReader is present |
400 | 76 |
for the '.xpm' and '.pm' extensions." |
203 | 77 |
|
647 | 78 |
MIMETypes defineImageType:'image/x-xpixmap' suffix:'xpm' reader:self. |
79 |
MIMETypes defineImageType:nil suffix:'pm' reader:self. |
|
398 | 80 |
|
400 | 81 |
"Modified: 1.2.1997 / 15:10:29 / cg" |
28 | 82 |
! ! |
83 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
84 |
!XPMReader class methodsFor:'testing'! |
22 | 85 |
|
460 | 86 |
canRepresent:anImage |
87 |
"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
|
88 |
Currently only images with less than 80 colors are supported." |
460 | 89 |
|
90 |
anImage depth <= 6 ifTrue:[^ true]. |
|
91 |
anImage photometric == #palette ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
92 |
anImage colorMap size < 80 ifTrue:[^ true]. |
460 | 93 |
]. |
1495 | 94 |
anImage usedColors size < 80 ifTrue:[^ true]. |
1521 | 95 |
Transcript showCR:'XPMReader: too many colors in image.'. |
1495 | 96 |
^ false |
460 | 97 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
98 |
"Modified: 27.2.1997 / 12:40:22 / cg" |
460 | 99 |
! |
100 |
||
22 | 101 |
isValidImageFile:aFileName |
102 |
"return true, if aFileName contains an x-bitmap-file image" |
|
103 |
||
104 |
|line inStream | |
|
105 |
||
106 |
inStream := self streamReadingFile:aFileName. |
|
107 |
inStream isNil ifTrue:[^ false]. |
|
108 |
||
550 | 109 |
Stream readErrorSignal handle:[:ex | |
110 |
line := nil |
|
111 |
] do:[ |
|
1510
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
112 |
Stream lineTooLongErrorSignal handle:[:ex | |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
113 |
line := nil. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
114 |
Transcript showCR:'long line'. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
115 |
ex return. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
116 |
] do:[ |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
117 |
line := inStream nextLine. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
118 |
] |
550 | 119 |
]. |
22 | 120 |
inStream close. |
121 |
line isNil ifTrue:[^ false]. |
|
122 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
123 |
^ true |
|
124 |
||
125 |
" |
|
562
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
126 |
XPMReader isValidImageFile:'fooBar' |
1737 | 127 |
XPMReader isValidImageFile:'../../goodies/bitmaps/xpmBitmaps/device_images/ljet.xpm' |
562
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
128 |
XPMReader isValidImageFile:'bitmaps/gifImages/garfield.gif' |
550 | 129 |
" |
130 |
||
562
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
131 |
"Modified: 24.4.1997 / 20:29:40 / cg" |
22 | 132 |
! ! |
133 |
||
134 |
!XPMReader methodsFor:'reading from file'! |
|
135 |
||
136 |
colorNameFrom:aStream |
|
137 |
"read either a color-name or value specified in X-notation |
|
138 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
139 |
||
140 |
|s| |
|
141 |
||
142 |
aStream peek == $# ifTrue:[ |
|
28 | 143 |
aStream next. |
144 |
s := '#'. |
|
24 | 145 |
] ifFalse:[ |
28 | 146 |
s := ''. |
22 | 147 |
]. |
28 | 148 |
[aStream peek isLetterOrDigit] whileTrue:[ |
149 |
s := s copyWith:aStream next |
|
24 | 150 |
]. |
151 |
^ s |
|
22 | 152 |
! |
153 |
||
1737 | 154 |
readColorMap:colorMapSize |
155 |
|redMap greenMap blueMap s key lineDone state |
|
156 |
symbolicName monoName greyName grey4Name colorName| |
|
22 | 157 |
|
107 | 158 |
redMap := ByteArray new:colorMapSize. |
159 |
greenMap := ByteArray new:colorMapSize. |
|
160 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 161 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
1737 | 162 |
|
22 | 163 |
1 to:colorMapSize do:[:colorIndex | |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
164 |
|index line color t word| |
22 | 165 |
|
1737 | 166 |
line := inStream nextLine. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
167 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
168 |
[line notNil and:[(line endsWith:'*/') not]] whileTrue:[ |
1737 | 169 |
line := inStream nextLine. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
170 |
]. |
1737 | 171 |
line := inStream nextLine. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
172 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
173 |
line notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
174 |
line := line withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
175 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
176 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
177 |
^ self fileFormatError:'format error (expected color spec)'. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
178 |
]. |
24 | 179 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
180 |
s := ReadStream on:line. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
181 |
s next. "skip quote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
182 |
charsPerPixel ~~ 1 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
183 |
key := s next:charsPerPixel. |
1737 | 184 |
characterTranslation at:key put:colorIndex - 1. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
185 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
186 |
index := s next asciiValue. |
1737 | 187 |
characterTranslation at:index put:colorIndex - 1. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
188 |
]. |
22 | 189 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
190 |
lineDone := false. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
191 |
state := nil. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
192 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
193 |
[lineDone] whileFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
194 |
s skipSeparators. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
195 |
s peek == $# ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
196 |
word := self colorNameFrom:s |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
197 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
198 |
word := s nextAlphaNumericWord. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
199 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
200 |
word isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
201 |
lineDone := true |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
202 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
203 |
word = 's' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
204 |
"/ symbolic name ... |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
205 |
state := $s. symbolicName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
206 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
207 |
word = 'm' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
208 |
"/ monochrome data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
209 |
state := $m. monoName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
210 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
211 |
word = 'g' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
212 |
"/ grey data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
213 |
state := $g. greyName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
214 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
215 |
word = 'g4' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
216 |
"/ grey data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
217 |
state := $G. grey4Name := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
218 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
219 |
word = 'c' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
220 |
"/ color data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
221 |
state := $c. colorName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
222 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
223 |
"/ append to name |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
224 |
state isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
225 |
^ self fileFormatError:('format error got: ' |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
226 |
, word printString |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
227 |
, ' (expected ''c'',''m'',''g'' or ''s'')'). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
228 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
229 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
230 |
state == $m ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
231 |
monoName := monoName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
232 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
233 |
state == $g ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
234 |
greyName := greyName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
235 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
236 |
state == $G ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
237 |
grey4Name := grey4Name , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
238 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
239 |
state == $c ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
240 |
colorName := colorName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
241 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
242 |
state == $s ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
243 |
symbolicName := symbolicName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
244 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
245 |
(word startsWith:'#') ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
246 |
state := nil. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
247 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
248 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
249 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
250 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
251 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
252 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
253 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
254 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
255 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
256 |
colorName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
257 |
colorName := colorName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
258 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
259 |
monoName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
260 |
monoName := monoName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
261 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
262 |
greyName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
263 |
greyName := greyName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
264 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
265 |
grey4Name notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
266 |
grey4Name := grey4Name withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
267 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
268 |
symbolicName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
269 |
symbolicName := symbolicName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
270 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
271 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
272 |
"/ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
273 |
"/ for now - ignore everything, except |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
274 |
"/ colorName (if there is one) |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
275 |
"/ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
276 |
colorName isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
277 |
colorName := greyName. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
278 |
colorName isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
279 |
colorName := monoName. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
280 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
281 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
282 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
283 |
(colorName sameAs: 'none') ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
284 |
color := Color noColor. "/ white |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
285 |
redMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
286 |
greenMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
287 |
blueMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
288 |
maskPixelValue := colorIndex-1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
289 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
290 |
color := Color name:colorName ifIllegal:Color black. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
291 |
redMap at:colorIndex put:(color red asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
292 |
greenMap at:colorIndex put:(color green asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
293 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
294 |
]. |
22 | 295 |
]. |
1737 | 296 |
! |
297 |
||
298 |
readImage |
|
299 |
"read an XPM-image from my inStream. Return the receiver |
|
300 |
(with all relevant instance variables set for the image) |
|
301 |
or nil on error" |
|
302 |
||
303 |
|line |
|
304 |
srcIndex "{ Class: SmallInteger }" |
|
305 |
dstIndex "{ Class: SmallInteger }" |
|
306 |
colorMapSize |
|
307 |
s bitsPerPixel key lastKey lastChar1 lastChar2 c1 c2 lastXLation| |
|
308 |
||
309 |
line := inStream nextLine. |
|
310 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
|
311 |
^ self fileFormatError:'format error (expected XPM)'. |
|
312 |
]. |
|
313 |
||
314 |
line := inStream nextLine. |
|
315 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank or:[(line startsWith:' *')]]]] whileTrue:[ |
|
316 |
line := inStream nextLine. |
|
317 |
]. |
|
318 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
319 |
^ self fileFormatError:'format error (expected static char)'. |
|
320 |
]. |
|
321 |
line := inStream nextLine. |
|
322 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
|
323 |
[line notNil |
|
324 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
|
325 |
line := inStream nextLine. |
|
326 |
]. |
|
327 |
]. |
|
328 |
line notNil ifTrue:[ |
|
329 |
line := line withoutSeparators |
|
330 |
]. |
|
331 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
332 |
^ self fileFormatError:'format error (expected "ww hh nn mm)'. |
|
333 |
]. |
|
334 |
s := ReadStream on:line. |
|
335 |
s next. "skip quote" |
|
336 |
width := Integer readFrom:s. |
|
337 |
height := Integer readFrom:s. |
|
338 |
colorMapSize := Integer readFrom:s. |
|
339 |
charsPerPixel := Integer readFrom:s. |
|
340 |
||
341 |
charsPerPixel ~~ 1 ifTrue:[ |
|
342 |
characterTranslation := Dictionary new:colorMapSize. |
|
343 |
] ifFalse:[ |
|
344 |
characterTranslation := Array new:256. |
|
345 |
]. |
|
346 |
||
347 |
self readColorMap:colorMapSize. |
|
22 | 348 |
|
349 |
"actually, could make it an image with less depth most of the time ..." |
|
350 |
||
351 |
" |
|
352 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1. |
|
353 |
" |
|
1529 | 354 |
colorMapSize > 256 ifTrue:[ |
355 |
bitsPerPixel := 24. |
|
356 |
data := ByteArray new:(width * height * 3). |
|
357 |
] ifFalse:[ |
|
358 |
bitsPerPixel := 8. |
|
359 |
data := ByteArray new:(width * height). |
|
360 |
]. |
|
22 | 361 |
|
362 |
dstIndex := 1. |
|
363 |
1 to:height do:[:row | |
|
1737 | 364 |
line := inStream nextLine withoutSpaces. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
365 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
1737 | 366 |
line := inStream nextLine withoutSpaces. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
367 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
368 |
line notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
369 |
line := line withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
370 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
371 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
372 |
^ self fileFormatError:'format error (expected pixels)'. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
373 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
374 |
charsPerPixel == 1 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
375 |
srcIndex := 2. "skip dquote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
376 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
377 |
key := line at:srcIndex. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
378 |
key ~~ lastKey ifTrue:[ |
1737 | 379 |
lastXLation := characterTranslation at:key asciiValue. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
380 |
lastKey := key |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
381 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
382 |
data at:dstIndex put:lastXLation. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
383 |
srcIndex := srcIndex + 1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
384 |
dstIndex := dstIndex + 1 |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
385 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
386 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
387 |
charsPerPixel == 2 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
388 |
"/ sorry, but this ugly code does a lot for speed, |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
389 |
"/ when reading big Xpm files (factor=5 for banner8.xpm) ... |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
390 |
srcIndex := 2."skip dquote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
391 |
lastChar1 := lastChar2 := nil. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
392 |
key := String new:2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
393 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
394 |
c1 := line at:srcIndex. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
395 |
c2 := line at:srcIndex+1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
396 |
(c1 ~~ lastChar1 or:[c2 ~~ lastChar2]) ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
397 |
key at:1 put:c1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
398 |
key at:2 put:c2. |
1737 | 399 |
lastXLation := characterTranslation at:key. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
400 |
lastChar1 := c1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
401 |
lastChar2 := c2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
402 |
]. |
1529 | 403 |
bitsPerPixel == 24 ifTrue:[ |
404 |
data at:dstIndex put:(colorMap at:lastXLation+1) redByte. |
|
405 |
data at:dstIndex+1 put:(colorMap at:lastXLation+1) greenByte. |
|
406 |
data at:dstIndex+2 put:(colorMap at:lastXLation+1) blueByte. |
|
407 |
dstIndex := dstIndex + 3. |
|
408 |
] ifFalse:[ |
|
409 |
data at:dstIndex put:lastXLation. |
|
410 |
dstIndex := dstIndex + 1. |
|
411 |
]. |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
412 |
srcIndex := srcIndex + 2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
413 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
414 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
415 |
s := line readStream. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
416 |
s next. "/ skip dquote |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
417 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
418 |
key := s next:charsPerPixel. |
1737 | 419 |
"/ data at:dstIndex put:(characterTranslation at:key). |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
420 |
key ~= lastKey ifTrue:[ |
1737 | 421 |
lastXLation := characterTranslation at:key. |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
422 |
lastKey := key |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
423 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
424 |
data at:dstIndex put:lastXLation. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
425 |
dstIndex := dstIndex + 1 |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
426 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
427 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
428 |
] |
22 | 429 |
]. |
430 |
||
1529 | 431 |
bitsPerPixel == 24 ifTrue:[ |
432 |
photometric := #rgb. |
|
433 |
samplesPerPixel := 3. |
|
434 |
bitsPerSample := #(8 8 8). |
|
435 |
] ifFalse:[ |
|
436 |
photometric := #palette. |
|
437 |
samplesPerPixel := 1. |
|
438 |
bitsPerSample := Array with:bitsPerPixel. |
|
439 |
]. |
|
22 | 440 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
441 |
maskPixelValue notNil ifTrue:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
442 |
self buildMaskFromColor:maskPixelValue |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
443 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
444 |
|
24 | 445 |
" |
1737 | 446 |
XPMReader fromStream:('../../goodies/bitmaps/xpmBitmaps/FATAL.xpm' asFilename readStream) |
107 | 447 |
" |
448 |
||
817
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
449 |
"Created: / 24.9.1995 / 06:20:06 / claus" |
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
450 |
"Modified: / 24.9.1995 / 07:07:33 / claus" |
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
451 |
"Modified: / 5.7.1996 / 17:27:59 / stefan" |
1007 | 452 |
"Modified: / 27.7.1998 / 20:01:56 / cg" |
22 | 453 |
! ! |
41 | 454 |
|
1400 | 455 |
!XPMReader methodsFor:'writing to file'! |
459 | 456 |
|
457 |
colorNameOf:aColor |
|
955 | 458 |
"generate a name for a color. If its a standard color, |
459 |
return its name; otherwise return the hex representation." |
|
460 |
||
459 | 461 |
#(white black red green blue |
462 |
yellow magenta cyan orange) do:[:aStandardColorName | |
|
463 |
aColor = (Color name:aStandardColorName) ifTrue:[ |
|
464 |
^ aStandardColorName. |
|
465 |
] |
|
466 |
]. |
|
467 |
^ '#' |
|
468 |
, (aColor redByte hexPrintString:2) |
|
469 |
, (aColor greenByte hexPrintString:2) |
|
470 |
, (aColor blueByte hexPrintString:2) |
|
471 |
||
955 | 472 |
"Created: / 27.2.1997 / 11:48:40 / cg" |
473 |
"Modified: / 6.6.1998 / 20:58:49 / cg" |
|
459 | 474 |
! |
475 |
||
476 |
save:image onFile:aFileName |
|
477 |
"save image as XPM file on aFileName. |
|
478 |
Caveat: currently, only a maximum of roughly 50 colors is handled |
|
479 |
(i.e. very simple images)" |
|
480 |
||
481 |
|usedColors nColorsUsed nColors nChars baseName map maskColorIndex |
|
482 |
isMasked imageMask| |
|
483 |
||
1012
8dd3d70caedb
oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents:
1007
diff
changeset
|
484 |
usedColors := image usedColors asArray. |
459 | 485 |
nColors := nColorsUsed := usedColors size. |
486 |
(imageMask := image mask) notNil ifTrue:[ |
|
487 |
nColors := nColors + 1. |
|
488 |
maskColorIndex := nColors. |
|
489 |
]. |
|
490 |
nColors > 256 ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
491 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
492 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
493 |
errorString:('XPMReader cannot represent this image (too many colors)'). |
459 | 494 |
]. |
495 |
||
496 |
map := ($a to: $z) asOrderedCollection. |
|
497 |
map addAll:($A to: $Z) asOrderedCollection. |
|
498 |
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
|
499 |
map addAll:#($. $, $` $^ $* $: $; $< $> $? $% $# $& $( $) $- $+ $=) asOrderedCollection. |
459 | 500 |
nChars := 1. |
501 |
||
502 |
nColors > map size ifTrue:[ |
|
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
503 |
"/ ^ Image cannotRepresentImageSignal |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
504 |
"/ raiseWith:image |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
505 |
"/ errorString:('XPMReader cannot represent this image (too many colors)'). |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
506 |
map := OrderedCollection new. |
693 | 507 |
$a to: $j do:[:c1 | |
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
508 |
map addAll:(($a to: $z) collect:[:c2 | c1 asString , c2 asString]). |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
509 |
]. |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
510 |
nChars := 2. |
459 | 511 |
]. |
512 |
||
1710
ad35e6a18e98
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1650
diff
changeset
|
513 |
[ |
ad35e6a18e98
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1650
diff
changeset
|
514 |
outStream := aFileName asFilename newReadWriteStream. |
ad35e6a18e98
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1650
diff
changeset
|
515 |
] on:FileStream openErrorSignal do:[:ex| |
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
516 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
517 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
518 |
errorString:('file creation error: ' , aFileName asString). |
459 | 519 |
]. |
520 |
||
521 |
baseName := aFileName asFilename baseName asFilename withoutSuffix asString. |
|
522 |
||
523 |
outStream nextPutLine:'/* XPM */'. |
|
524 |
outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'. |
|
525 |
outStream nextPutLine:'/* width height ncolors chars_per_pixel */'. |
|
526 |
outStream nextPutLine:'"' , image width printString , ' ' |
|
527 |
, image height printString , ' ' |
|
528 |
, nColors printString , ' ' |
|
529 |
, nChars printString , '",'. |
|
530 |
||
531 |
||
532 |
outStream nextPutLine:'/* colors */'. |
|
533 |
1 to:nColorsUsed do:[:idx | |
|
534 |
|clr| |
|
535 |
||
536 |
clr := usedColors at:idx. |
|
537 |
outStream nextPutLine:'"' , (map at:idx) asString , |
|
538 |
' ' , 'c ' , (self colorNameOf:clr) , |
|
539 |
'",'. |
|
540 |
]. |
|
541 |
maskColorIndex notNil ifTrue:[ |
|
542 |
outStream nextPutLine:'"' , (map at:maskColorIndex) asString , |
|
543 |
' c none",'. |
|
544 |
]. |
|
545 |
||
546 |
outStream nextPutLine:'/* pixels */'. |
|
547 |
||
548 |
maskColorIndex isNil ifTrue:[ |
|
549 |
isMasked := false |
|
550 |
]. |
|
560 | 551 |
|
459 | 552 |
0 to:image height-1 do:[:y | |
553 |
outStream nextPutAll:'"'. |
|
554 |
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
|
555 |
|clr idx| |
459 | 556 |
|
557 |
maskColorIndex notNil ifTrue:[ |
|
560 | 558 |
isMasked := (imageMask pixelAtX:x y:y) == 0 |
459 | 559 |
]. |
560 |
isMasked ifTrue:[ |
|
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
561 |
outStream nextPutAll:(map at:maskColorIndex) asString |
459 | 562 |
] ifFalse:[ |
564 | 563 |
clr := image colorAtX:x y:y. |
459 | 564 |
idx := usedColors indexOf:clr. |
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
565 |
outStream nextPutAll:(map at:idx) asString |
459 | 566 |
] |
567 |
]. |
|
568 |
outStream nextPutLine:'",'. |
|
569 |
]. |
|
469
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
570 |
outStream nextPutLine:'};'. |
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
571 |
outStream close. |
459 | 572 |
|
1012
8dd3d70caedb
oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents:
1007
diff
changeset
|
573 |
"Modified: / 28.7.1998 / 21:52:13 / cg" |
459 | 574 |
! ! |
575 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
576 |
!XPMReader class methodsFor:'documentation'! |
203 | 577 |
|
578 |
version |
|
1737 | 579 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.50 2003-04-04 17:07:22 cg Exp $' |
203 | 580 |
! ! |
1650 | 581 |
|
41 | 582 |
XPMReader initialize! |