author | Claus Gittinger <cg@exept.de> |
Wed, 19 Nov 2003 16:28:36 +0100 | |
changeset 1846 | d29322944b05 |
parent 1805 | 93f557cbe600 |
child 3588 | b1560f509caa |
permissions | -rw-r--r-- |
0 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 3 |
All Rights Reserved |
0 | 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 |
||
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
13 |
"{ Package: 'stx:libview2' }" |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
14 |
|
0 | 15 |
ImageReader subclass:#SunRasterReader |
208 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
19 |
category:'Graphics-Images-Readers' |
0 | 20 |
! |
21 |
||
21 | 22 |
!SunRasterReader class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 27 |
All Rights Reserved |
0 | 28 |
|
21 | 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 |
! |
|
0 | 37 |
|
21 | 38 |
documentation |
39 |
" |
|
208 | 40 |
this class provides methods for loading Sun Raster and |
41 |
Sun Icon file images. |
|
42 |
||
32 | 43 |
No image writing is implemented. |
208 | 44 |
|
45 |
[See also:] |
|
234 | 46 |
Image Form Icon |
208 | 47 |
BlitImageReader FaceReader JPEGReader GIFReader PBMReader PCXReader |
210 | 48 |
ST80FormReader TargaReader TIFFReader WindowsIconReader |
209 | 49 |
XBMReader XPMReader XWDReader |
21 | 50 |
" |
51 |
! ! |
|
0 | 52 |
|
28 | 53 |
!SunRasterReader class methodsFor:'initialization'! |
54 |
||
55 |
initialize |
|
208 | 56 |
"install myself in the Image classes fileFormat table |
399 | 57 |
for the `.icon' and '.im8' extensions." |
208 | 58 |
|
647 | 59 |
MIMETypes defineImageType:nil suffix:'icon' reader:self. |
60 |
MIMETypes defineImageType:nil suffix:'im8' reader:self. |
|
398 | 61 |
|
399 | 62 |
"Modified: 1.2.1997 / 15:08:40 / cg" |
28 | 63 |
! ! |
64 |
||
3 | 65 |
!SunRasterReader class methodsFor:'testing'! |
66 |
||
67 |
isValidImageFile:aFileName |
|
68 |
"return true, if aFileName contains a sunraster image" |
|
69 |
||
21 | 70 |
|inStream nr| |
3 | 71 |
|
11 | 72 |
inStream := self streamReadingFile:aFileName. |
3 | 73 |
inStream isNil ifTrue:[^ false]. |
74 |
||
75 |
"try sun raster" |
|
76 |
inStream binary. |
|
21 | 77 |
((inStream nextWord == 16r59A6) |
78 |
and:[inStream nextWord == 16r6A95]) ifTrue: [ |
|
28 | 79 |
inStream close. |
80 |
^ true |
|
3 | 81 |
]. |
82 |
||
83 | 83 |
inStream isPositionable ifFalse:[^ false]. |
84 |
||
3 | 85 |
"try sun bitmap image format" |
86 |
inStream text. |
|
16 | 87 |
inStream reset. |
35 | 88 |
|
89 |
"must start with a comment" |
|
90 |
inStream skipSeparators. |
|
91 |
inStream next ~~ $/ ifTrue:[^ false]. |
|
92 |
inStream next ~~ $* ifTrue:[^ false]. |
|
93 |
||
21 | 94 |
(inStream skipThroughAll: 'idth') isNil ifTrue: [ |
28 | 95 |
inStream close. |
96 |
^ false |
|
3 | 97 |
]. |
21 | 98 |
inStream next; skipSeparators. |
99 |
nr := Integer readFrom: inStream. |
|
100 |
(nr isNil or:[nr <= 0]) ifTrue: [ |
|
28 | 101 |
inStream close. |
102 |
^ false |
|
3 | 103 |
]. |
104 |
||
21 | 105 |
(inStream skipThroughAll: 'eight') isNil ifTrue: [ |
28 | 106 |
inStream close. |
107 |
^ false |
|
3 | 108 |
]. |
21 | 109 |
inStream next; skipSeparators. |
110 |
nr := Integer readFrom: inStream. |
|
111 |
(nr isNil or:[nr <= 0]) ifTrue: [ |
|
28 | 112 |
inStream close. |
113 |
^ false |
|
3 | 114 |
]. |
115 |
||
116 |
inStream close. |
|
117 |
^ true |
|
118 |
! ! |
|
119 |
||
1805 | 120 |
!SunRasterReader methodsFor:'reading'! |
0 | 121 |
|
43 | 122 |
fromStream: aStream |
208 | 123 |
"read an image in my format from aStream. |
124 |
Dtermine if its a raster or icon file." |
|
125 |
||
0 | 126 |
| rasterType mapType mapBytes imageWords form depth |
127 |
rMap gMap bMap mapLen |
|
918 | 128 |
a b c index pos| |
0 | 129 |
|
43 | 130 |
inStream := aStream. |
131 |
||
132 |
aStream binary. |
|
0 | 133 |
|
43 | 134 |
pos := aStream position. |
135 |
((aStream nextWord == 16r59A6) |
|
136 |
and:[aStream nextWord == 16r6A95]) ifFalse: [ |
|
44 | 137 |
"/ 'SUNReader: not a SunRaster file' errorPrintNL. |
208 | 138 |
aStream position:pos. |
139 |
^ self fromSunIconStream:aStream |
|
0 | 140 |
]. |
141 |
||
43 | 142 |
width := aStream nextLong. |
143 |
height := aStream nextLong. |
|
1846 | 144 |
self reportDimension. |
0 | 145 |
|
43 | 146 |
depth := aStream nextLong. |
147 |
aStream nextLong. "Ignore the image length since I can't rely on it anyway." |
|
148 |
rasterType := aStream nextLong. |
|
149 |
mapType := aStream nextLong. "Ignore the raster maptype." |
|
150 |
mapBytes := aStream nextLong. |
|
0 | 151 |
|
152 |
depth = 8 ifTrue: [ |
|
208 | 153 |
mapLen := (mapBytes // 3). |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
154 |
rMap := aStream nextBytes:mapLen. |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
155 |
gMap := aStream nextBytes:mapLen. |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
156 |
bMap := aStream nextBytes:mapLen. |
1759
f52382e5a9b8
slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
157 |
colorMap := MappedPalette redVector:rMap greenVector:gMap blueVector:bMap. |
0 | 158 |
|
208 | 159 |
data := ByteArray uninitializedNew:(width * height). |
160 |
aStream nextBytes:(width * height) into:data. |
|
0 | 161 |
|
208 | 162 |
photometric := #palette. |
163 |
samplesPerPixel := 1. |
|
164 |
bitsPerSample := #(8). |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
165 |
|
208 | 166 |
^ self |
0 | 167 |
]. |
168 |
depth ~~ 1 ifTrue: [ |
|
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
169 |
^ self fileFormatError:'only depth 1 and 8 supported'. |
0 | 170 |
]. |
171 |
||
172 |
form := nil. |
|
173 |
||
43 | 174 |
aStream skip: mapBytes. "Skip the color map." |
175 |
imageWords := (width / 16) ceiling * height. |
|
3 | 176 |
data := ByteArray uninitializedNew:(imageWords * 2). |
0 | 177 |
|
178 |
(rasterType between: 0 and: 2) ifFalse: [ |
|
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
179 |
^ self fileFormatError:'Unknown raster file rasterType'. |
0 | 180 |
]. |
181 |
||
182 |
(rasterType = 2) ifFalse: [ |
|
208 | 183 |
"no compression of bytes" |
184 |
aStream nextBytes:(imageWords * 2) into:data |
|
0 | 185 |
] ifTrue: [ |
208 | 186 |
"run length compression of bytes" |
0 | 187 |
|
208 | 188 |
index := 1. |
189 |
a := aStream next. |
|
190 |
[a notNil] whileTrue: [ |
|
191 |
(a = 128) ifFalse: [ |
|
192 |
data at:index put: a. |
|
193 |
index := index + 1 |
|
194 |
] ifTrue: [ |
|
195 |
b := aStream next. |
|
196 |
b = 0 ifTrue: [ |
|
197 |
data at:index put:128 . |
|
198 |
index := index + 1 |
|
199 |
] ifFalse: [ |
|
200 |
c := aStream next. |
|
201 |
1 to:(b+1) do:[:i | |
|
202 |
data at:index put:c. |
|
203 |
index := index + 1 |
|
204 |
] |
|
205 |
] |
|
206 |
]. |
|
207 |
a := aStream next |
|
208 |
]. |
|
0 | 209 |
]. |
210 |
photometric := #whiteIs0. |
|
211 |
samplesPerPixel := 1. |
|
212 |
bitsPerSample := #(1). |
|
213 |
||
43 | 214 |
" |
215 |
Image fromFile:'bitmaps/founders.im8' |
|
216 |
Image fromFile:'bitmaps/bf.im8' |
|
217 |
SunRasterReader fromStream:'bitmaps/founders.im8' asFilename readStream |
|
218 |
SunRasterReader fromStream:'bitmaps/bf.im8' asFilename readStream |
|
219 |
" |
|
208 | 220 |
|
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
221 |
"Modified: / 3.2.1998 / 18:00:35 / cg" |
3 | 222 |
! |
223 |
||
208 | 224 |
fromSunIconStream:aStream |
225 |
"helper: read an image in icon format from aStream" |
|
226 |
||
21 | 227 |
|index word |
228 |
w "{ Class: SmallInteger }" |
|
229 |
h "{ Class: SmallInteger }"| |
|
3 | 230 |
|
43 | 231 |
inStream := aStream. |
44 | 232 |
aStream text. |
3 | 233 |
|
43 | 234 |
(aStream skipThroughAll:'idth') isNil ifTrue: [ |
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
235 |
^ self fileFormatError:'Not a Sun Raster/Icon File'. |
3 | 236 |
]. |
43 | 237 |
aStream next; skipSeparators. "skip $=" |
238 |
width := Integer readFrom: aStream. |
|
21 | 239 |
(width isNil or:[width <= 0]) ifTrue: [ |
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
240 |
^ self fileFormatError:'format error (expected number)'. |
21 | 241 |
]. |
242 |
w := width. |
|
3 | 243 |
|
43 | 244 |
(aStream skipThroughAll:'eight') isNil ifTrue: [ |
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
245 |
^ self fileFormatError:'format error (expected height)'. |
208 | 246 |
^ nil |
21 | 247 |
]. |
43 | 248 |
aStream next; skipSeparators. "skip $=" |
249 |
height := Integer readFrom: aStream. |
|
21 | 250 |
(height isNil or:[height <= 0]) ifTrue: [ |
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
251 |
^ self fileFormatError:'format error (expected number)'. |
21 | 252 |
]. |
253 |
h := height. |
|
3 | 254 |
|
255 |
data := ByteArray uninitializedNew:((width + 7 // 8) * height). |
|
256 |
photometric := #whiteIs0. |
|
257 |
samplesPerPixel := 1. |
|
258 |
bitsPerSample := #(1). |
|
259 |
||
260 |
index := 0. |
|
21 | 261 |
1 to:h do: [:row | |
208 | 262 |
1 to: (w + 15 // 16) do: [:col | |
263 |
"rows are rounded up to next multiple of 16 bits" |
|
264 |
(aStream skipThroughAll:'0x') isNil ifTrue: [^ nil]. |
|
265 |
word := Integer readFrom:aStream radix:16. |
|
266 |
word isNil ifTrue:[ |
|
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
267 |
^ self fileFormatError:'format error'. |
208 | 268 |
]. |
269 |
data at: (index := index + 1) put: (word bitShift:-8). |
|
270 |
data at: (index := index + 1) put: (word bitAnd:16rFF). |
|
271 |
] |
|
3 | 272 |
]. |
208 | 273 |
|
813
4d473c1a3c05
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
274 |
"Modified: / 3.2.1998 / 18:01:15 / cg" |
0 | 275 |
! ! |
43 | 276 |
|
208 | 277 |
!SunRasterReader class methodsFor:'documentation'! |
278 |
||
279 |
version |
|
1846 | 280 |
^ '$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.31 2003-11-19 15:25:57 cg Exp $' |
208 | 281 |
! ! |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
282 |
|
43 | 283 |
SunRasterReader initialize! |