author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 17:05:49 +0100 | |
changeset 114 | e577a2f332d0 |
parent 99 | a656b0c9dd21 |
child 208 | bd41881b2a0d |
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 |
||
43 | 13 |
'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 2:18:51 am'! |
14 |
||
0 | 15 |
ImageReader subclass:#SunRasterReader |
28 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
32 | 19 |
category:'Graphics-Images support' |
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 |
version |
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.16 1995-11-11 16:05:02 cg Exp $' |
21 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
32 | 44 |
this class provides methods for loading Sun Raster file images. |
45 |
No image writing is implemented. |
|
21 | 46 |
" |
47 |
! ! |
|
0 | 48 |
|
28 | 49 |
!SunRasterReader class methodsFor:'initialization'! |
50 |
||
51 |
initialize |
|
52 |
Image fileFormats at:'.icon' put:self. |
|
53 |
! ! |
|
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: [ |
|
28 | 69 |
inStream close. |
70 |
^ true |
|
3 | 71 |
]. |
72 |
||
83 | 73 |
inStream isPositionable ifFalse:[^ false]. |
74 |
||
3 | 75 |
"try sun bitmap image format" |
76 |
inStream text. |
|
16 | 77 |
inStream reset. |
35 | 78 |
|
79 |
"must start with a comment" |
|
80 |
inStream skipSeparators. |
|
81 |
inStream next ~~ $/ ifTrue:[^ false]. |
|
82 |
inStream next ~~ $* ifTrue:[^ false]. |
|
83 |
||
21 | 84 |
(inStream skipThroughAll: 'idth') isNil ifTrue: [ |
28 | 85 |
inStream close. |
86 |
^ false |
|
3 | 87 |
]. |
21 | 88 |
inStream next; skipSeparators. |
89 |
nr := Integer readFrom: inStream. |
|
90 |
(nr isNil or:[nr <= 0]) ifTrue: [ |
|
28 | 91 |
inStream close. |
92 |
^ false |
|
3 | 93 |
]. |
94 |
||
21 | 95 |
(inStream skipThroughAll: 'eight') isNil ifTrue: [ |
28 | 96 |
inStream close. |
97 |
^ false |
|
3 | 98 |
]. |
21 | 99 |
inStream next; skipSeparators. |
100 |
nr := Integer readFrom: inStream. |
|
101 |
(nr isNil or:[nr <= 0]) ifTrue: [ |
|
28 | 102 |
inStream close. |
103 |
^ false |
|
3 | 104 |
]. |
105 |
||
106 |
inStream close. |
|
107 |
^ true |
|
108 |
! ! |
|
109 |
||
0 | 110 |
!SunRasterReader methodsFor:'reading from file'! |
111 |
||
43 | 112 |
fromStream: aStream |
0 | 113 |
| rasterType mapType mapBytes imageWords form depth |
114 |
rMap gMap bMap mapLen |
|
43 | 115 |
bits a b c index pos| |
0 | 116 |
|
43 | 117 |
inStream := aStream. |
118 |
||
119 |
aStream binary. |
|
0 | 120 |
|
43 | 121 |
pos := aStream position. |
122 |
((aStream nextWord == 16r59A6) |
|
123 |
and:[aStream nextWord == 16r6A95]) ifFalse: [ |
|
44 | 124 |
"/ 'SUNReader: not a SunRaster file' errorPrintNL. |
125 |
aStream position:pos. |
|
126 |
^ self fromSunIconStream:aStream |
|
0 | 127 |
]. |
128 |
||
43 | 129 |
width := aStream nextLong. |
130 |
height := aStream nextLong. |
|
0 | 131 |
|
43 | 132 |
depth := aStream nextLong. |
133 |
aStream nextLong. "Ignore the image length since I can't rely on it anyway." |
|
134 |
rasterType := aStream nextLong. |
|
135 |
mapType := aStream nextLong. "Ignore the raster maptype." |
|
136 |
mapBytes := aStream nextLong. |
|
0 | 137 |
|
138 |
depth = 8 ifTrue: [ |
|
44 | 139 |
mapLen := (mapBytes // 3). |
140 |
rMap := ByteArray uninitializedNew:mapLen. |
|
141 |
gMap := ByteArray uninitializedNew:mapLen. |
|
142 |
bMap := ByteArray uninitializedNew:mapLen. |
|
143 |
aStream nextBytes:mapLen into:rMap. |
|
144 |
aStream nextBytes:mapLen into:gMap. |
|
145 |
aStream nextBytes:mapLen into:bMap. |
|
0 | 146 |
|
44 | 147 |
data := ByteArray uninitializedNew:(width * height). |
148 |
aStream nextBytes:(width * height) into:data. |
|
0 | 149 |
|
44 | 150 |
photometric := #palette. |
151 |
samplesPerPixel := 1. |
|
152 |
bitsPerSample := #(8). |
|
153 |
colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap. |
|
154 |
^ self |
|
0 | 155 |
]. |
156 |
depth ~~ 1 ifTrue: [ |
|
44 | 157 |
'SUNReader: only depth 1 and 8 supported' errorPrintNL. |
158 |
^ nil |
|
0 | 159 |
]. |
160 |
||
161 |
form := nil. |
|
162 |
||
43 | 163 |
aStream skip: mapBytes. "Skip the color map." |
164 |
imageWords := (width / 16) ceiling * height. |
|
3 | 165 |
data := ByteArray uninitializedNew:(imageWords * 2). |
0 | 166 |
|
167 |
(rasterType between: 0 and: 2) ifFalse: [ |
|
44 | 168 |
'SUNReader: Unknown raster file rasterType' errorPrintNL. |
169 |
^ nil |
|
0 | 170 |
]. |
171 |
||
172 |
(rasterType = 2) ifFalse: [ |
|
44 | 173 |
"no compression of bytes" |
174 |
aStream nextBytes:(imageWords * 2) into:data |
|
0 | 175 |
] ifTrue: [ |
44 | 176 |
"run length compression of bytes" |
0 | 177 |
|
44 | 178 |
index := 1. |
179 |
a := aStream next. |
|
180 |
[a notNil] whileTrue: [ |
|
181 |
(a = 128) ifFalse: [ |
|
182 |
data at:index put: a. |
|
183 |
index := index + 1 |
|
184 |
] ifTrue: [ |
|
185 |
b := aStream next. |
|
186 |
b = 0 ifTrue: [ |
|
187 |
data at:index put:128 . |
|
188 |
index := index + 1 |
|
189 |
] ifFalse: [ |
|
190 |
c := aStream next. |
|
191 |
1 to:(b+1) do:[:i | |
|
192 |
data at:index put:c. |
|
193 |
index := index + 1 |
|
194 |
] |
|
195 |
] |
|
196 |
]. |
|
197 |
a := aStream next |
|
198 |
]. |
|
0 | 199 |
]. |
200 |
photometric := #whiteIs0. |
|
201 |
samplesPerPixel := 1. |
|
202 |
bitsPerSample := #(1). |
|
203 |
||
43 | 204 |
" |
205 |
Image fromFile:'bitmaps/founders.im8' |
|
206 |
Image fromFile:'bitmaps/bf.im8' |
|
207 |
SunRasterReader fromStream:'bitmaps/founders.im8' asFilename readStream |
|
208 |
SunRasterReader fromStream:'bitmaps/bf.im8' asFilename readStream |
|
209 |
" |
|
3 | 210 |
! |
211 |
||
43 | 212 |
fromSunIconStream: aStream |
21 | 213 |
|index word |
214 |
w "{ Class: SmallInteger }" |
|
215 |
h "{ Class: SmallInteger }"| |
|
3 | 216 |
|
43 | 217 |
inStream := aStream. |
44 | 218 |
aStream text. |
3 | 219 |
|
43 | 220 |
(aStream skipThroughAll:'idth') isNil ifTrue: [ |
44 | 221 |
'SUNReader: Not a Sun Raster/Icon File' errorPrintNL. |
222 |
^nil |
|
3 | 223 |
]. |
43 | 224 |
aStream next; skipSeparators. "skip $=" |
225 |
width := Integer readFrom: aStream. |
|
21 | 226 |
(width isNil or:[width <= 0]) ifTrue: [ |
44 | 227 |
'SUNReader: format error (expected number)' errorPrintNL. |
228 |
^ nil |
|
21 | 229 |
]. |
230 |
w := width. |
|
3 | 231 |
|
43 | 232 |
(aStream skipThroughAll:'eight') isNil ifTrue: [ |
44 | 233 |
'SUNReader: format error (expected height)' errorPrintNL. |
234 |
^ nil |
|
21 | 235 |
]. |
43 | 236 |
aStream next; skipSeparators. "skip $=" |
237 |
height := Integer readFrom: aStream. |
|
21 | 238 |
(height isNil or:[height <= 0]) ifTrue: [ |
44 | 239 |
'SUNReader: format error (expected number)' errorPrintNL. |
240 |
^nil |
|
21 | 241 |
]. |
242 |
h := height. |
|
3 | 243 |
|
244 |
data := ByteArray uninitializedNew:((width + 7 // 8) * height). |
|
245 |
photometric := #whiteIs0. |
|
246 |
samplesPerPixel := 1. |
|
247 |
bitsPerSample := #(1). |
|
248 |
||
249 |
index := 0. |
|
21 | 250 |
1 to:h do: [:row | |
44 | 251 |
1 to: (w + 15 // 16) do: [:col | |
252 |
"rows are rounded up to next multiple of 16 bits" |
|
253 |
(aStream skipThroughAll:'0x') isNil ifTrue: [^ nil]. |
|
254 |
word := Integer readFrom:aStream radix:16. |
|
255 |
word isNil ifTrue:[ |
|
256 |
'SUNReader: format error' errorPrintNL. |
|
257 |
^ nil |
|
258 |
]. |
|
259 |
data at: (index _ index + 1) put: (word bitShift:-8). |
|
260 |
data at: (index _ index + 1) put: (word bitAnd:16rFF). |
|
261 |
] |
|
3 | 262 |
]. |
0 | 263 |
! ! |
43 | 264 |
|
265 |
SunRasterReader initialize! |