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