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