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