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 |
|
|
22 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
this class provides methods for loading Sun Raster file images
|
|
26 |
|
11
|
27 |
$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.4 1993-12-11 01:33:32 claus Exp $
|
0
|
28 |
written Summer 91 by claus
|
|
29 |
'!
|
|
30 |
|
3
|
31 |
!SunRasterReader class methodsFor:'testing'!
|
|
32 |
|
|
33 |
isValidImageFile:aFileName
|
|
34 |
"return true, if aFileName contains a sunraster image"
|
|
35 |
|
|
36 |
|inStream|
|
|
37 |
|
11
|
38 |
inStream := self streamReadingFile:aFileName.
|
3
|
39 |
inStream isNil ifTrue:[^ false].
|
|
40 |
|
|
41 |
"try sun raster"
|
|
42 |
inStream binary.
|
|
43 |
((inStream nextWord = 16r59A6)
|
|
44 |
and:[inStream nextWord = 16r6A95]) ifTrue: [
|
|
45 |
inStream close.
|
|
46 |
^ true
|
|
47 |
].
|
|
48 |
|
|
49 |
"try sun bitmap image format"
|
|
50 |
inStream text.
|
|
51 |
inStream skip:-2.
|
|
52 |
(inStream skipToAll: 'idth') isNil ifTrue: [
|
|
53 |
inStream close.
|
|
54 |
^ false
|
|
55 |
].
|
|
56 |
inStream skip: 5; skipSeparators.
|
|
57 |
(Integer readFrom: inStream) <= 0 ifTrue: [
|
|
58 |
inStream close.
|
|
59 |
^ false
|
|
60 |
].
|
|
61 |
|
|
62 |
(inStream skipToAll: 'eight') isNil ifTrue: [
|
|
63 |
inStream close.
|
|
64 |
^ false
|
|
65 |
].
|
|
66 |
inStream skip: 6; skipSeparators.
|
|
67 |
(Integer readFrom: inStream) <= 0 ifTrue: [
|
|
68 |
inStream close.
|
|
69 |
^ false
|
|
70 |
].
|
|
71 |
|
|
72 |
inStream close.
|
|
73 |
^ true
|
|
74 |
! !
|
|
75 |
|
0
|
76 |
!SunRasterReader methodsFor:'reading from file'!
|
|
77 |
|
|
78 |
fromFile: aFilename
|
|
79 |
| rasterType mapType mapBytes imageWords form depth
|
|
80 |
rMap gMap bMap mapLen
|
|
81 |
bits a b c index|
|
|
82 |
|
11
|
83 |
inStream := self class streamReadingFile:aFilename.
|
|
84 |
inStream isNil ifTrue:[^ nil].
|
0
|
85 |
|
|
86 |
inStream binary.
|
|
87 |
|
|
88 |
((inStream nextWord = 16r59A6)
|
|
89 |
and:[inStream nextWord = 16r6A95]) ifFalse: [
|
|
90 |
inStream close.
|
3
|
91 |
^ self fromSunIconFile:aFilename
|
0
|
92 |
].
|
|
93 |
|
|
94 |
width := inStream nextLong.
|
|
95 |
height := inStream nextLong.
|
|
96 |
|
|
97 |
depth := inStream nextLong.
|
|
98 |
inStream nextLong. "Ignore the image length since I can't rely on it anyway."
|
|
99 |
rasterType _ inStream nextLong.
|
|
100 |
mapType := inStream nextLong. "Ignore the raster maptype."
|
|
101 |
mapBytes := inStream nextLong.
|
|
102 |
|
|
103 |
depth = 8 ifTrue: [
|
|
104 |
mapLen := (mapBytes // 3).
|
3
|
105 |
rMap := ByteArray uninitializedNew:mapLen.
|
|
106 |
gMap := ByteArray uninitializedNew:mapLen.
|
|
107 |
bMap := ByteArray uninitializedNew:mapLen.
|
0
|
108 |
inStream nextBytes:mapLen into:rMap.
|
|
109 |
inStream nextBytes:mapLen into:gMap.
|
|
110 |
inStream nextBytes:mapLen into:bMap.
|
|
111 |
|
3
|
112 |
data := ByteArray uninitializedNew:(width * height).
|
0
|
113 |
inStream nextBytes:(width * height) into:data.
|
|
114 |
|
|
115 |
photometric := #palette.
|
|
116 |
samplesPerPixel := 1.
|
|
117 |
bitsPerSample := #(8).
|
|
118 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
119 |
inStream close.
|
|
120 |
^ self
|
|
121 |
].
|
|
122 |
depth ~~ 1 ifTrue: [
|
|
123 |
inStream close.
|
|
124 |
self error: 'Raster file is not monochrome'
|
|
125 |
].
|
|
126 |
|
|
127 |
form := nil.
|
|
128 |
|
|
129 |
inStream skip: mapBytes. "Skip the color map."
|
|
130 |
imageWords _ (width / 16) ceiling * height.
|
3
|
131 |
data := ByteArray uninitializedNew:(imageWords * 2).
|
0
|
132 |
|
|
133 |
(rasterType between: 0 and: 2) ifFalse: [
|
|
134 |
inStream close.
|
|
135 |
self error: 'Unknown raster file rasterType'
|
|
136 |
].
|
|
137 |
|
|
138 |
(rasterType = 2) ifFalse: [
|
|
139 |
"no compression of bytes"
|
|
140 |
inStream nextBytes:(imageWords * 2) into:data
|
|
141 |
] ifTrue: [
|
|
142 |
"run length compression of bytes"
|
|
143 |
|
3
|
144 |
bits _ ByteArray uninitializedNew: imageWords * 2.
|
0
|
145 |
index := 1.
|
|
146 |
a _ inStream next.
|
|
147 |
[a notNil] whileTrue: [
|
|
148 |
(a = 128) ifFalse: [
|
|
149 |
bits at:index put: a.
|
|
150 |
index := index + 1
|
|
151 |
] ifTrue: [
|
|
152 |
b _ inStream next.
|
|
153 |
b = 0 ifTrue: [
|
|
154 |
bits at:index put:128 .
|
|
155 |
index := index + 1
|
|
156 |
] ifFalse: [
|
|
157 |
c := inStream next.
|
|
158 |
1 to:(b+1) do:[:i |
|
|
159 |
bits at:index put:c.
|
|
160 |
index := index + 1
|
|
161 |
]
|
|
162 |
]
|
|
163 |
].
|
|
164 |
a _ inStream next
|
|
165 |
].
|
|
166 |
1 to: imageWords do: [:i | form bitsWordAt: i put: (bits wordAt: i)]
|
|
167 |
].
|
|
168 |
photometric := #whiteIs0.
|
|
169 |
samplesPerPixel := 1.
|
|
170 |
bitsPerSample := #(1).
|
|
171 |
inStream close
|
|
172 |
|
|
173 |
"Image fromFile:'../fileIn/bitmaps/founders.im8'"
|
3
|
174 |
!
|
|
175 |
|
|
176 |
fromSunIconFile: aFilename
|
|
177 |
| index word |
|
|
178 |
|
11
|
179 |
inStream := self class streamReadingFile:aFilename.
|
|
180 |
inStream isNil ifTrue:[^ nil].
|
3
|
181 |
|
|
182 |
(inStream skipToAll: 'idth') isNil ifTrue: [
|
|
183 |
'Not a Sun Raster/Icon File' printNewline.
|
|
184 |
^nil
|
|
185 |
].
|
|
186 |
inStream skip: 5; skipSeparators.
|
|
187 |
(width := Integer readFrom: inStream) <= 0 ifTrue: [^nil].
|
|
188 |
|
|
189 |
(inStream skipToAll: 'eight') isNil ifTrue: [^nil].
|
|
190 |
inStream skip: 6; skipSeparators.
|
|
191 |
(height := Integer readFrom: inStream) <= 0 ifTrue: [^nil].
|
|
192 |
|
|
193 |
data := ByteArray uninitializedNew:((width + 7 // 8) * height).
|
|
194 |
photometric := #whiteIs0.
|
|
195 |
samplesPerPixel := 1.
|
|
196 |
bitsPerSample := #(1).
|
|
197 |
|
|
198 |
index := 0.
|
|
199 |
1 to: height do: [:row |
|
|
200 |
1 to: (width + 15 // 16) do: [:col |
|
|
201 |
"rows are rounded up to next multiple of 16 bits"
|
|
202 |
(inStream skipToAll: '0x') isNil ifTrue: [^nil]. inStream skip: 2.
|
|
203 |
word := Integer readFrom: inStream radix: 16.
|
|
204 |
data at: (index _ index + 1) put: (word bitShift:-8).
|
|
205 |
data at: (index _ index + 1) put: (word bitAnd:16rFF).
|
|
206 |
]
|
|
207 |
].
|
|
208 |
inStream close.
|
0
|
209 |
! !
|