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