2
|
1 |
"
|
21
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
3 |
All Rights Reserved
|
2
|
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:#PBMReader
|
28
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
32
|
17 |
category:'Graphics-Images support'
|
2
|
18 |
!
|
|
19 |
|
|
20 |
PBMReader comment:'
|
21
|
21 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
22 |
All Rights Reserved
|
23
|
23 |
|
32
|
24 |
$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.8 1994-11-17 14:29:38 claus Exp $
|
21
|
25 |
'!
|
2
|
26 |
|
21
|
27 |
!PBMReader class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
32 |
All Rights Reserved
|
2
|
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 |
!
|
2
|
42 |
|
21
|
43 |
version
|
|
44 |
"
|
32
|
45 |
$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.8 1994-11-17 14:29:38 claus Exp $
|
21
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
32
|
51 |
this class provides methods for loading and saving Portable BitMap-file
|
|
52 |
images (Jef Poskanzers portable bitmap package).
|
|
53 |
Currently, only writing of 1-bit and 8-bit images is supported.
|
21
|
54 |
"
|
|
55 |
! !
|
2
|
56 |
|
28
|
57 |
!PBMReader class methodsFor:'initialization'!
|
|
58 |
|
|
59 |
initialize
|
|
60 |
Image fileFormats at:'.pbm' put:self.
|
|
61 |
Image fileFormats at:'.pgm' put:self.
|
|
62 |
Image fileFormats at:'.pnm' put:self.
|
|
63 |
! !
|
|
64 |
|
2
|
65 |
!PBMReader methodsFor:'private'!
|
|
66 |
|
|
67 |
skipPBMJunkOn: aStream
|
|
68 |
"This method removes any superfluous characters from the input stream."
|
|
69 |
|
|
70 |
| char foundNL|
|
|
71 |
|
|
72 |
[
|
28
|
73 |
char := aStream peek.
|
|
74 |
char == $# ifTrue:[
|
|
75 |
"Start of a comment. Skip to end-of-line."
|
|
76 |
foundNL := (aStream skipUpTo: Character cr) notNil.
|
|
77 |
foundNL ifFalse: [
|
|
78 |
"Must be EOF"
|
|
79 |
^self
|
|
80 |
].
|
|
81 |
char := aStream peek].
|
|
82 |
aStream atEnd not and: [char isSeparator]
|
2
|
83 |
] whileTrue: [aStream next]
|
|
84 |
!
|
|
85 |
|
|
86 |
skipXPMJunkOn: aStream
|
|
87 |
"This method removes any superfluous characters from the input stream."
|
|
88 |
|
|
89 |
| char |
|
|
90 |
|
|
91 |
[
|
28
|
92 |
char := aStream peek.
|
|
93 |
aStream atEnd not and: [char isSeparator not]
|
2
|
94 |
] whileTrue: [aStream next].
|
|
95 |
|
|
96 |
[aStream atEnd not and: [char isSeparator]] whileTrue: [
|
28
|
97 |
aStream next. char := aStream peek
|
2
|
98 |
].
|
|
99 |
aStream atEnd ifTrue: [^char].
|
|
100 |
(char isDigit) ifTrue: [ ^char ].
|
|
101 |
(char == $") ifTrue: [
|
28
|
102 |
aStream next.
|
|
103 |
char := aStream peek.
|
|
104 |
(char isLetterOrDigit
|
|
105 |
or: [(char == $#)
|
|
106 |
or: [char == Character space]]) ifFalse:[
|
|
107 |
^ self skipXPMJunkOn: aStream
|
|
108 |
] ifTrue: [^char]
|
2
|
109 |
].
|
|
110 |
|
|
111 |
^self skipXPMJunkOn: aStream.
|
|
112 |
! !
|
|
113 |
|
|
114 |
!PBMReader methodsFor:'writing to file'!
|
|
115 |
|
|
116 |
save:image onFile:aFileName
|
|
117 |
"save image as PBM/PGM/PNM file on aFileName"
|
|
118 |
|
|
119 |
outStream := FileStream newFileNamed:aFileName.
|
|
120 |
outStream isNil ifTrue:[
|
28
|
121 |
'create error' errorPrintNL.
|
|
122 |
^ nil
|
2
|
123 |
].
|
|
124 |
|
|
125 |
width := image width.
|
|
126 |
height := image height.
|
|
127 |
photometric := image photometric.
|
|
128 |
samplesPerPixel := image samplesPerPixel.
|
|
129 |
bitsPerSample := image bitsPerSample.
|
|
130 |
colorMap := image colorMap.
|
|
131 |
|
|
132 |
photometric == #rgb ifTrue:[
|
28
|
133 |
^ self writePNMFile
|
2
|
134 |
].
|
|
135 |
samplesPerPixel == 1 ifTrue:[
|
28
|
136 |
((bitsPerSample at:1) == 1) ifTrue:[
|
|
137 |
^ self writePBMFile
|
|
138 |
].
|
|
139 |
((bitsPerSample at:1) == 8) ifTrue:[
|
|
140 |
^ self writePGMFile
|
|
141 |
].
|
2
|
142 |
].
|
|
143 |
self error:'format not supported'.
|
21
|
144 |
!
|
|
145 |
|
|
146 |
writePNMFile
|
|
147 |
self error:'not yet implemented'
|
|
148 |
!
|
|
149 |
|
|
150 |
writePBMFile
|
|
151 |
self error:'not yet implemented'
|
|
152 |
!
|
|
153 |
|
|
154 |
writePGMFile
|
|
155 |
self error:'not yet implemented'
|
2
|
156 |
! !
|
|
157 |
|
|
158 |
!PBMReader methodsFor:'reading from file'!
|
|
159 |
|
|
160 |
fromFile:fileName
|
|
161 |
"read a Portable bitmap file format as of Jef Poskanzers Portable Bitmap Package.
|
|
162 |
supported are PBM, PGB and PNM files."
|
|
163 |
|
|
164 |
| pnmType |
|
|
165 |
|
10
|
166 |
inStream := self class streamReadingFile:fileName.
|
|
167 |
inStream isNil ifTrue:[^ nil].
|
|
168 |
|
2
|
169 |
inStream next == $P ifFalse:[
|
28
|
170 |
('not PNM format in ', fileName) errorPrintNL.
|
|
171 |
inStream close.
|
|
172 |
^nil
|
2
|
173 |
].
|
|
174 |
pnmType := inStream next.
|
|
175 |
inStream close.
|
|
176 |
pnmType == $4 ifTrue: [
|
28
|
177 |
^ self readDepth1PBMFile:fileName
|
2
|
178 |
].
|
|
179 |
pnmType == $5 ifTrue: [
|
28
|
180 |
^ self readDepth8PGMFile:fileName
|
2
|
181 |
].
|
|
182 |
pnmType == $6 ifTrue: [
|
28
|
183 |
^ self readDepth24PPMFile:fileName
|
2
|
184 |
].
|
21
|
185 |
('No recognized pnm file format in ', fileName) errorPrintNL.
|
2
|
186 |
^ nil
|
|
187 |
|
|
188 |
"PBMReader fromFile:'bitmaps/testimg.ppm'"
|
|
189 |
!
|
|
190 |
|
|
191 |
readDepth1PBMFile:fileName
|
|
192 |
"import portable bitmap (PBM)"
|
|
193 |
|
10
|
194 |
inStream := self class streamReadingFile:fileName.
|
|
195 |
inStream isNil ifTrue:[^ nil].
|
2
|
196 |
|
|
197 |
(inStream next == $P) ifFalse: [
|
28
|
198 |
inStream close.
|
|
199 |
'not a pbm file format' errorPrintNL.
|
|
200 |
^ nil
|
2
|
201 |
].
|
|
202 |
|
|
203 |
(inStream next == $4) ifFalse:[
|
28
|
204 |
inStream close.
|
|
205 |
'not a pbm file format' errorPrintNL.
|
|
206 |
^ nil
|
2
|
207 |
].
|
|
208 |
|
|
209 |
self skipPBMJunkOn: inStream.
|
|
210 |
width := Integer readFrom: inStream.
|
21
|
211 |
width > 0 ifFalse: [
|
28
|
212 |
inStream close.
|
|
213 |
'Invalid width' errorPrintNL.
|
|
214 |
^ nil
|
21
|
215 |
].
|
2
|
216 |
|
|
217 |
self skipPBMJunkOn: inStream.
|
|
218 |
height := Integer readFrom: inStream.
|
21
|
219 |
height > 0 ifFalse: [
|
28
|
220 |
inStream close.
|
|
221 |
'Invalid height' errorPrintNL.
|
|
222 |
^ nil
|
21
|
223 |
].
|
2
|
224 |
|
|
225 |
inStream nextLine "skipThrough: Character cr".
|
|
226 |
inStream binary.
|
|
227 |
data := inStream contents.
|
|
228 |
inStream close.
|
|
229 |
|
|
230 |
photometric := #whiteIs0.
|
|
231 |
samplesPerPixel := 1.
|
|
232 |
bitsPerSample := #(1).
|
|
233 |
!
|
|
234 |
|
|
235 |
readDepth8PGMFile:fileName
|
|
236 |
"import portable gray map (PGM)"
|
|
237 |
|
|
238 |
|maxval|
|
|
239 |
|
10
|
240 |
inStream := self class streamReadingFile:fileName.
|
|
241 |
inStream isNil ifTrue:[^ nil].
|
|
242 |
|
2
|
243 |
inStream next == $P ifFalse:[
|
28
|
244 |
inStream close.
|
|
245 |
'not a pgm file format' errorPrintNL.
|
|
246 |
^ nil
|
2
|
247 |
].
|
|
248 |
inStream next == $5 ifFalse:[
|
28
|
249 |
inStream close.
|
|
250 |
'not a pgm file format' errorPrintNL.
|
|
251 |
^ nil
|
2
|
252 |
].
|
|
253 |
self skipPBMJunkOn: inStream.
|
|
254 |
width := Integer readFrom: inStream.
|
|
255 |
width > 0 ifFalse:[
|
28
|
256 |
inStream close.
|
|
257 |
'pgm read error' errorPrintNL.
|
|
258 |
^ nil
|
2
|
259 |
].
|
|
260 |
self skipPBMJunkOn: inStream.
|
|
261 |
height := Integer readFrom: inStream.
|
|
262 |
height > 0 ifFalse:[
|
28
|
263 |
inStream close.
|
|
264 |
'pgm read error' errorPrintNL.
|
|
265 |
^ nil
|
2
|
266 |
].
|
|
267 |
self skipPBMJunkOn: inStream.
|
|
268 |
maxval := Integer readFrom: inStream.
|
|
269 |
maxval >= 256 ifTrue:[
|
28
|
270 |
inStream close.
|
|
271 |
'pgm read error' errorPrintNL.
|
|
272 |
^ nil
|
2
|
273 |
].
|
|
274 |
inStream skipThrough: Character cr.
|
|
275 |
inStream binary.
|
|
276 |
data := inStream contents.
|
|
277 |
|
|
278 |
photometric := #blackIs0.
|
|
279 |
samplesPerPixel := 1.
|
|
280 |
bitsPerSample := #(8).
|
|
281 |
!
|
|
282 |
|
|
283 |
readDepth24PPMFile: fileName
|
|
284 |
"import portable pixmap (PPM)"
|
|
285 |
|
|
286 |
| maxval |
|
|
287 |
|
10
|
288 |
inStream := self class streamReadingFile:fileName.
|
|
289 |
inStream isNil ifTrue:[^ nil].
|
|
290 |
|
2
|
291 |
(inStream next == $P) ifFalse: [
|
28
|
292 |
inStream close.
|
|
293 |
self error: 'not a ppm file format' errorPrintNL.
|
|
294 |
^ nil
|
2
|
295 |
].
|
|
296 |
|
|
297 |
(inStream next == $6) ifFalse: [
|
28
|
298 |
inStream close.
|
|
299 |
self error: 'not a ppm file format' errorPrintNL.
|
|
300 |
^ nil
|
2
|
301 |
].
|
|
302 |
|
|
303 |
self skipPBMJunkOn: inStream.
|
|
304 |
width := Integer readFrom: inStream.
|
21
|
305 |
width > 0 ifFalse: [
|
28
|
306 |
inStream close.
|
|
307 |
self error: 'ppm read error' errorPrintNL.
|
|
308 |
^ nil
|
21
|
309 |
].
|
2
|
310 |
|
|
311 |
self skipPBMJunkOn: inStream.
|
|
312 |
height := Integer readFrom: inStream.
|
21
|
313 |
height > 0 ifFalse: [
|
28
|
314 |
inStream close.
|
|
315 |
self error: 'ppm read error' errorPrintNL.
|
|
316 |
^ nil
|
21
|
317 |
].
|
2
|
318 |
|
|
319 |
self skipPBMJunkOn: inStream.
|
|
320 |
maxval := Integer readFrom: inStream.
|
21
|
321 |
maxval >= 256 ifTrue: [
|
28
|
322 |
inStream close.
|
|
323 |
self error: 'ppm read error' errorPrintNL.
|
|
324 |
^ nil
|
21
|
325 |
].
|
2
|
326 |
|
|
327 |
inStream skipThrough: Character cr.
|
|
328 |
inStream binary.
|
|
329 |
|
|
330 |
data := inStream contents.
|
|
331 |
photometric := #rgb.
|
|
332 |
samplesPerPixel := 3.
|
|
333 |
bitsPerSample := #(8 8 8).
|
|
334 |
! !
|