0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
SunRasterReader comment:'
|
21
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
23
|
23 |
|
|
24 |
$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.7 1994-08-05 01:15:11 claus Exp $
|
21
|
25 |
'!
|
0
|
26 |
|
21
|
27 |
!SunRasterReader class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
0
|
32 |
All Rights Reserved
|
|
33 |
|
21
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
0
|
42 |
|
21
|
43 |
version
|
|
44 |
"
|
23
|
45 |
$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.7 1994-08-05 01:15:11 claus Exp $
|
21
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class provides methods for loading Sun Raster file images
|
|
52 |
"
|
|
53 |
! !
|
0
|
54 |
|
3
|
55 |
!SunRasterReader class methodsFor:'testing'!
|
|
56 |
|
|
57 |
isValidImageFile:aFileName
|
|
58 |
"return true, if aFileName contains a sunraster image"
|
|
59 |
|
21
|
60 |
|inStream nr|
|
3
|
61 |
|
11
|
62 |
inStream := self streamReadingFile:aFileName.
|
3
|
63 |
inStream isNil ifTrue:[^ false].
|
|
64 |
|
|
65 |
"try sun raster"
|
|
66 |
inStream binary.
|
21
|
67 |
((inStream nextWord == 16r59A6)
|
|
68 |
and:[inStream nextWord == 16r6A95]) ifTrue: [
|
3
|
69 |
inStream close.
|
|
70 |
^ true
|
|
71 |
].
|
|
72 |
|
|
73 |
"try sun bitmap image format"
|
|
74 |
inStream text.
|
16
|
75 |
inStream reset.
|
21
|
76 |
(inStream skipThroughAll: 'idth') isNil ifTrue: [
|
3
|
77 |
inStream close.
|
|
78 |
^ false
|
|
79 |
].
|
21
|
80 |
inStream next; skipSeparators.
|
|
81 |
nr := Integer readFrom: inStream.
|
|
82 |
(nr isNil or:[nr <= 0]) ifTrue: [
|
3
|
83 |
inStream close.
|
|
84 |
^ false
|
|
85 |
].
|
|
86 |
|
21
|
87 |
(inStream skipThroughAll: 'eight') isNil ifTrue: [
|
3
|
88 |
inStream close.
|
|
89 |
^ false
|
|
90 |
].
|
21
|
91 |
inStream next; skipSeparators.
|
|
92 |
nr := Integer readFrom: inStream.
|
|
93 |
(nr isNil or:[nr <= 0]) ifTrue: [
|
3
|
94 |
inStream close.
|
|
95 |
^ false
|
|
96 |
].
|
|
97 |
|
|
98 |
inStream close.
|
|
99 |
^ true
|
|
100 |
! !
|
|
101 |
|
0
|
102 |
!SunRasterReader methodsFor:'reading from file'!
|
|
103 |
|
|
104 |
fromFile: aFilename
|
|
105 |
| rasterType mapType mapBytes imageWords form depth
|
|
106 |
rMap gMap bMap mapLen
|
|
107 |
bits a b c index|
|
|
108 |
|
11
|
109 |
inStream := self class streamReadingFile:aFilename.
|
|
110 |
inStream isNil ifTrue:[^ nil].
|
0
|
111 |
|
|
112 |
inStream binary.
|
|
113 |
|
21
|
114 |
((inStream nextWord == 16r59A6)
|
|
115 |
and:[inStream nextWord == 16r6A95]) ifFalse: [
|
0
|
116 |
inStream close.
|
3
|
117 |
^ self fromSunIconFile:aFilename
|
0
|
118 |
].
|
|
119 |
|
|
120 |
width := inStream nextLong.
|
|
121 |
height := inStream nextLong.
|
|
122 |
|
|
123 |
depth := inStream nextLong.
|
|
124 |
inStream nextLong. "Ignore the image length since I can't rely on it anyway."
|
|
125 |
rasterType _ inStream nextLong.
|
|
126 |
mapType := inStream nextLong. "Ignore the raster maptype."
|
|
127 |
mapBytes := inStream nextLong.
|
|
128 |
|
|
129 |
depth = 8 ifTrue: [
|
|
130 |
mapLen := (mapBytes // 3).
|
3
|
131 |
rMap := ByteArray uninitializedNew:mapLen.
|
|
132 |
gMap := ByteArray uninitializedNew:mapLen.
|
|
133 |
bMap := ByteArray uninitializedNew:mapLen.
|
0
|
134 |
inStream nextBytes:mapLen into:rMap.
|
|
135 |
inStream nextBytes:mapLen into:gMap.
|
|
136 |
inStream nextBytes:mapLen into:bMap.
|
|
137 |
|
3
|
138 |
data := ByteArray uninitializedNew:(width * height).
|
0
|
139 |
inStream nextBytes:(width * height) into:data.
|
|
140 |
|
|
141 |
photometric := #palette.
|
|
142 |
samplesPerPixel := 1.
|
|
143 |
bitsPerSample := #(8).
|
|
144 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
145 |
inStream close.
|
|
146 |
^ self
|
|
147 |
].
|
|
148 |
depth ~~ 1 ifTrue: [
|
|
149 |
inStream close.
|
|
150 |
self error: 'Raster file is not monochrome'
|
|
151 |
].
|
|
152 |
|
|
153 |
form := nil.
|
|
154 |
|
|
155 |
inStream skip: mapBytes. "Skip the color map."
|
|
156 |
imageWords _ (width / 16) ceiling * height.
|
3
|
157 |
data := ByteArray uninitializedNew:(imageWords * 2).
|
0
|
158 |
|
|
159 |
(rasterType between: 0 and: 2) ifFalse: [
|
|
160 |
inStream close.
|
|
161 |
self error: 'Unknown raster file rasterType'
|
|
162 |
].
|
|
163 |
|
|
164 |
(rasterType = 2) ifFalse: [
|
|
165 |
"no compression of bytes"
|
|
166 |
inStream nextBytes:(imageWords * 2) into:data
|
|
167 |
] ifTrue: [
|
|
168 |
"run length compression of bytes"
|
|
169 |
|
3
|
170 |
bits _ ByteArray uninitializedNew: imageWords * 2.
|
0
|
171 |
index := 1.
|
|
172 |
a _ inStream next.
|
|
173 |
[a notNil] whileTrue: [
|
|
174 |
(a = 128) ifFalse: [
|
|
175 |
bits at:index put: a.
|
|
176 |
index := index + 1
|
|
177 |
] ifTrue: [
|
|
178 |
b _ inStream next.
|
|
179 |
b = 0 ifTrue: [
|
|
180 |
bits at:index put:128 .
|
|
181 |
index := index + 1
|
|
182 |
] ifFalse: [
|
|
183 |
c := inStream next.
|
|
184 |
1 to:(b+1) do:[:i |
|
|
185 |
bits at:index put:c.
|
|
186 |
index := index + 1
|
|
187 |
]
|
|
188 |
]
|
|
189 |
].
|
|
190 |
a _ inStream next
|
|
191 |
].
|
|
192 |
1 to: imageWords do: [:i | form bitsWordAt: i put: (bits wordAt: i)]
|
|
193 |
].
|
|
194 |
photometric := #whiteIs0.
|
|
195 |
samplesPerPixel := 1.
|
|
196 |
bitsPerSample := #(1).
|
|
197 |
inStream close
|
|
198 |
|
|
199 |
"Image fromFile:'../fileIn/bitmaps/founders.im8'"
|
3
|
200 |
!
|
|
201 |
|
|
202 |
fromSunIconFile: aFilename
|
21
|
203 |
|index word
|
|
204 |
w "{ Class: SmallInteger }"
|
|
205 |
h "{ Class: SmallInteger }"|
|
3
|
206 |
|
11
|
207 |
inStream := self class streamReadingFile:aFilename.
|
|
208 |
inStream isNil ifTrue:[^ nil].
|
3
|
209 |
|
21
|
210 |
(inStream skipThroughAll:'idth') isNil ifTrue: [
|
|
211 |
'Not a Sun Raster/Icon File' errorPrintNewline.
|
|
212 |
inStream close.
|
3
|
213 |
^nil
|
|
214 |
].
|
21
|
215 |
inStream next; skipSeparators. "skip $="
|
|
216 |
width := Integer readFrom: inStream.
|
|
217 |
(width isNil or:[width <= 0]) ifTrue: [
|
|
218 |
'format error (expected number)' errorPrintNewline.
|
|
219 |
inStream close.
|
|
220 |
^ nil
|
|
221 |
].
|
|
222 |
w := width.
|
3
|
223 |
|
21
|
224 |
(inStream skipThroughAll:'eight') isNil ifTrue: [
|
|
225 |
'format error (expected height)' errorPrintNewline.
|
|
226 |
inStream close.
|
|
227 |
^ nil
|
|
228 |
].
|
|
229 |
inStream next; skipSeparators. "skip $="
|
|
230 |
height := Integer readFrom: inStream.
|
|
231 |
(height isNil or:[height <= 0]) ifTrue: [
|
|
232 |
'format error (expected number)' errorPrintNewline.
|
|
233 |
inStream close.
|
|
234 |
^nil
|
|
235 |
].
|
|
236 |
h := height.
|
3
|
237 |
|
|
238 |
data := ByteArray uninitializedNew:((width + 7 // 8) * height).
|
|
239 |
photometric := #whiteIs0.
|
|
240 |
samplesPerPixel := 1.
|
|
241 |
bitsPerSample := #(1).
|
|
242 |
|
|
243 |
index := 0.
|
21
|
244 |
1 to:h do: [:row |
|
|
245 |
1 to: (w + 15 // 16) do: [:col |
|
|
246 |
"rows are rounded up to next multiple of 16 bits"
|
|
247 |
(inStream skipThroughAll:'0x') isNil ifTrue: [^ nil].
|
|
248 |
word := Integer readFrom:inStream radix:16.
|
|
249 |
word isNil ifTrue:[
|
|
250 |
'format error' errorPrintNewline.
|
|
251 |
inStream close.
|
|
252 |
^ nil
|
|
253 |
].
|
|
254 |
data at: (index _ index + 1) put: (word bitShift:-8).
|
|
255 |
data at: (index _ index + 1) put: (word bitAnd:16rFF).
|
|
256 |
]
|
3
|
257 |
].
|
|
258 |
inStream close.
|
0
|
259 |
! !
|