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 |
|
|
13 |
ImageReader subclass:#SunRasterReader
|
208
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Images support'
|
0
|
18 |
!
|
|
19 |
|
21
|
20 |
!SunRasterReader class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
28
|
25 |
All Rights Reserved
|
0
|
26 |
|
21
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
0
|
35 |
|
21
|
36 |
documentation
|
|
37 |
"
|
208
|
38 |
this class provides methods for loading Sun Raster and
|
|
39 |
Sun Icon file images.
|
|
40 |
|
32
|
41 |
No image writing is implemented.
|
208
|
42 |
|
|
43 |
[See also:]
|
|
44 |
BlitImageReader FaceReader JPEGReader GIFReader PBMReader PCXReader
|
|
45 |
ST80FormReader TIFFReader WindowsIconReader
|
209
|
46 |
XBMReader XPMReader XWDReader
|
21
|
47 |
"
|
|
48 |
! !
|
0
|
49 |
|
28
|
50 |
!SunRasterReader class methodsFor:'initialization'!
|
|
51 |
|
|
52 |
initialize
|
208
|
53 |
"install myself in the Image classes fileFormat table
|
|
54 |
for the `.icon' extension."
|
|
55 |
|
28
|
56 |
Image fileFormats at:'.icon' put:self.
|
|
57 |
! !
|
|
58 |
|
3
|
59 |
!SunRasterReader class methodsFor:'testing'!
|
|
60 |
|
|
61 |
isValidImageFile:aFileName
|
|
62 |
"return true, if aFileName contains a sunraster image"
|
|
63 |
|
21
|
64 |
|inStream nr|
|
3
|
65 |
|
11
|
66 |
inStream := self streamReadingFile:aFileName.
|
3
|
67 |
inStream isNil ifTrue:[^ false].
|
|
68 |
|
|
69 |
"try sun raster"
|
|
70 |
inStream binary.
|
21
|
71 |
((inStream nextWord == 16r59A6)
|
|
72 |
and:[inStream nextWord == 16r6A95]) ifTrue: [
|
28
|
73 |
inStream close.
|
|
74 |
^ true
|
3
|
75 |
].
|
|
76 |
|
83
|
77 |
inStream isPositionable ifFalse:[^ false].
|
|
78 |
|
3
|
79 |
"try sun bitmap image format"
|
|
80 |
inStream text.
|
16
|
81 |
inStream reset.
|
35
|
82 |
|
|
83 |
"must start with a comment"
|
|
84 |
inStream skipSeparators.
|
|
85 |
inStream next ~~ $/ ifTrue:[^ false].
|
|
86 |
inStream next ~~ $* ifTrue:[^ false].
|
|
87 |
|
21
|
88 |
(inStream skipThroughAll: 'idth') isNil ifTrue: [
|
28
|
89 |
inStream close.
|
|
90 |
^ false
|
3
|
91 |
].
|
21
|
92 |
inStream next; skipSeparators.
|
|
93 |
nr := Integer readFrom: inStream.
|
|
94 |
(nr isNil or:[nr <= 0]) ifTrue: [
|
28
|
95 |
inStream close.
|
|
96 |
^ false
|
3
|
97 |
].
|
|
98 |
|
21
|
99 |
(inStream skipThroughAll: 'eight') isNil ifTrue: [
|
28
|
100 |
inStream close.
|
|
101 |
^ false
|
3
|
102 |
].
|
21
|
103 |
inStream next; skipSeparators.
|
|
104 |
nr := Integer readFrom: inStream.
|
|
105 |
(nr isNil or:[nr <= 0]) ifTrue: [
|
28
|
106 |
inStream close.
|
|
107 |
^ false
|
3
|
108 |
].
|
|
109 |
|
|
110 |
inStream close.
|
|
111 |
^ true
|
|
112 |
! !
|
|
113 |
|
0
|
114 |
!SunRasterReader methodsFor:'reading from file'!
|
|
115 |
|
43
|
116 |
fromStream: aStream
|
208
|
117 |
"read an image in my format from aStream.
|
|
118 |
Dtermine if its a raster or icon file."
|
|
119 |
|
0
|
120 |
| rasterType mapType mapBytes imageWords form depth
|
|
121 |
rMap gMap bMap mapLen
|
43
|
122 |
bits a b c index pos|
|
0
|
123 |
|
43
|
124 |
inStream := aStream.
|
|
125 |
|
|
126 |
aStream binary.
|
0
|
127 |
|
43
|
128 |
pos := aStream position.
|
|
129 |
((aStream nextWord == 16r59A6)
|
|
130 |
and:[aStream nextWord == 16r6A95]) ifFalse: [
|
44
|
131 |
"/ 'SUNReader: not a SunRaster file' errorPrintNL.
|
208
|
132 |
aStream position:pos.
|
|
133 |
^ self fromSunIconStream:aStream
|
0
|
134 |
].
|
|
135 |
|
43
|
136 |
width := aStream nextLong.
|
|
137 |
height := aStream nextLong.
|
0
|
138 |
|
43
|
139 |
depth := aStream nextLong.
|
|
140 |
aStream nextLong. "Ignore the image length since I can't rely on it anyway."
|
|
141 |
rasterType := aStream nextLong.
|
|
142 |
mapType := aStream nextLong. "Ignore the raster maptype."
|
|
143 |
mapBytes := aStream nextLong.
|
0
|
144 |
|
|
145 |
depth = 8 ifTrue: [
|
208
|
146 |
mapLen := (mapBytes // 3).
|
|
147 |
rMap := ByteArray uninitializedNew:mapLen.
|
|
148 |
gMap := ByteArray uninitializedNew:mapLen.
|
|
149 |
bMap := ByteArray uninitializedNew:mapLen.
|
|
150 |
aStream nextBytes:mapLen into:rMap.
|
|
151 |
aStream nextBytes:mapLen into:gMap.
|
|
152 |
aStream nextBytes:mapLen into:bMap.
|
0
|
153 |
|
208
|
154 |
data := ByteArray uninitializedNew:(width * height).
|
|
155 |
aStream nextBytes:(width * height) into:data.
|
0
|
156 |
|
208
|
157 |
photometric := #palette.
|
|
158 |
samplesPerPixel := 1.
|
|
159 |
bitsPerSample := #(8).
|
|
160 |
colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
|
|
161 |
^ self
|
0
|
162 |
].
|
|
163 |
depth ~~ 1 ifTrue: [
|
208
|
164 |
'SUNReader: only depth 1 and 8 supported' errorPrintNL.
|
|
165 |
^ nil
|
0
|
166 |
].
|
|
167 |
|
|
168 |
form := nil.
|
|
169 |
|
43
|
170 |
aStream skip: mapBytes. "Skip the color map."
|
|
171 |
imageWords := (width / 16) ceiling * height.
|
3
|
172 |
data := ByteArray uninitializedNew:(imageWords * 2).
|
0
|
173 |
|
|
174 |
(rasterType between: 0 and: 2) ifFalse: [
|
208
|
175 |
'SUNReader: Unknown raster file rasterType' errorPrintNL.
|
|
176 |
^ nil
|
0
|
177 |
].
|
|
178 |
|
|
179 |
(rasterType = 2) ifFalse: [
|
208
|
180 |
"no compression of bytes"
|
|
181 |
aStream nextBytes:(imageWords * 2) into:data
|
0
|
182 |
] ifTrue: [
|
208
|
183 |
"run length compression of bytes"
|
0
|
184 |
|
208
|
185 |
index := 1.
|
|
186 |
a := aStream next.
|
|
187 |
[a notNil] whileTrue: [
|
|
188 |
(a = 128) ifFalse: [
|
|
189 |
data at:index put: a.
|
|
190 |
index := index + 1
|
|
191 |
] ifTrue: [
|
|
192 |
b := aStream next.
|
|
193 |
b = 0 ifTrue: [
|
|
194 |
data at:index put:128 .
|
|
195 |
index := index + 1
|
|
196 |
] ifFalse: [
|
|
197 |
c := aStream next.
|
|
198 |
1 to:(b+1) do:[:i |
|
|
199 |
data at:index put:c.
|
|
200 |
index := index + 1
|
|
201 |
]
|
|
202 |
]
|
|
203 |
].
|
|
204 |
a := aStream next
|
|
205 |
].
|
0
|
206 |
].
|
|
207 |
photometric := #whiteIs0.
|
|
208 |
samplesPerPixel := 1.
|
|
209 |
bitsPerSample := #(1).
|
|
210 |
|
43
|
211 |
"
|
|
212 |
Image fromFile:'bitmaps/founders.im8'
|
|
213 |
Image fromFile:'bitmaps/bf.im8'
|
|
214 |
SunRasterReader fromStream:'bitmaps/founders.im8' asFilename readStream
|
|
215 |
SunRasterReader fromStream:'bitmaps/bf.im8' asFilename readStream
|
|
216 |
"
|
208
|
217 |
|
|
218 |
"Modified: 23.4.1996 / 12:59:31 / cg"
|
3
|
219 |
!
|
|
220 |
|
208
|
221 |
fromSunIconStream:aStream
|
|
222 |
"helper: read an image in icon format from aStream"
|
|
223 |
|
21
|
224 |
|index word
|
|
225 |
w "{ Class: SmallInteger }"
|
|
226 |
h "{ Class: SmallInteger }"|
|
3
|
227 |
|
43
|
228 |
inStream := aStream.
|
44
|
229 |
aStream text.
|
3
|
230 |
|
43
|
231 |
(aStream skipThroughAll:'idth') isNil ifTrue: [
|
208
|
232 |
'SUNReader: Not a Sun Raster/Icon File' errorPrintNL.
|
|
233 |
^nil
|
3
|
234 |
].
|
43
|
235 |
aStream next; skipSeparators. "skip $="
|
|
236 |
width := Integer readFrom: aStream.
|
21
|
237 |
(width isNil or:[width <= 0]) ifTrue: [
|
208
|
238 |
'SUNReader: format error (expected number)' errorPrintNL.
|
|
239 |
^ nil
|
21
|
240 |
].
|
|
241 |
w := width.
|
3
|
242 |
|
43
|
243 |
(aStream skipThroughAll:'eight') isNil ifTrue: [
|
208
|
244 |
'SUNReader: format error (expected height)' errorPrintNL.
|
|
245 |
^ nil
|
21
|
246 |
].
|
43
|
247 |
aStream next; skipSeparators. "skip $="
|
|
248 |
height := Integer readFrom: aStream.
|
21
|
249 |
(height isNil or:[height <= 0]) ifTrue: [
|
208
|
250 |
'SUNReader: format error (expected number)' errorPrintNL.
|
|
251 |
^nil
|
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:[
|
|
267 |
'SUNReader: format error' errorPrintNL.
|
|
268 |
^ nil
|
|
269 |
].
|
|
270 |
data at: (index := index + 1) put: (word bitShift:-8).
|
|
271 |
data at: (index := index + 1) put: (word bitAnd:16rFF).
|
|
272 |
]
|
3
|
273 |
].
|
208
|
274 |
|
|
275 |
"Modified: 23.4.1996 / 12:59:07 / cg"
|
0
|
276 |
! !
|
43
|
277 |
|
208
|
278 |
!SunRasterReader class methodsFor:'documentation'!
|
|
279 |
|
|
280 |
version
|
209
|
281 |
^ '$Header: /cvs/stx/stx/libview2/Attic/SunReader.st,v 1.18 1996-04-23 11:02:16 cg Exp $'
|
208
|
282 |
! !
|
43
|
283 |
SunRasterReader initialize!
|