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
|
200
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Images support'
|
2
|
18 |
!
|
|
19 |
|
21
|
20 |
!PBMReader class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
25 |
All Rights Reserved
|
2
|
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 |
!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
32
|
38 |
this class provides methods for loading and saving Portable BitMap-file
|
|
39 |
images (Jef Poskanzers portable bitmap package).
|
200
|
40 |
|
46
|
41 |
Reading is supported for 1bit (pbm), greyscale (pgm) and 24bit (ppm) formats.
|
|
42 |
Currently, only writing of 1-bit images (Pbm) is supported.
|
|
43 |
|
|
44 |
Q: should we bring this one to perfection and base all others on
|
|
45 |
pipe-readers to the various pbmplus converters ?
|
200
|
46 |
|
|
47 |
[See also:]
|
|
48 |
BlitImageReader FaceReader GIFReader JPEGReader PCXReader
|
210
|
49 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader
|
209
|
50 |
XBMReader XPMReader XWDReader
|
21
|
51 |
"
|
|
52 |
! !
|
2
|
53 |
|
28
|
54 |
!PBMReader class methodsFor:'initialization'!
|
|
55 |
|
|
56 |
initialize
|
200
|
57 |
"install myself in the Image classes fileFormat table
|
|
58 |
for the `.pbm', '.pgm' and '.pnm' extensions."
|
|
59 |
|
28
|
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 |
|
46
|
67 |
skipPBMJunkOn:aStream
|
200
|
68 |
"this method removes any superfluous characters from the input stream."
|
2
|
69 |
|
|
70 |
| char foundNL|
|
|
71 |
|
|
72 |
[
|
200
|
73 |
char := aStream peek.
|
|
74 |
char == $# ifTrue:[
|
|
75 |
"Start of a comment. Skip to end-of-line."
|
46
|
76 |
"/ foundNL := (aStream skipUpTo: Character cr) notNil.
|
200
|
77 |
foundNL := (aStream skipThrough: Character cr) notNil.
|
|
78 |
foundNL ifFalse: [
|
|
79 |
"Must be EOF"
|
|
80 |
^self
|
|
81 |
].
|
|
82 |
char := aStream peek].
|
|
83 |
aStream atEnd not and: [char isSeparator]
|
2
|
84 |
] whileTrue: [aStream next]
|
|
85 |
!
|
|
86 |
|
46
|
87 |
skipXPMJunkOn:aStream
|
200
|
88 |
"this method removes any superfluous characters from the input stream."
|
2
|
89 |
|
|
90 |
| char |
|
|
91 |
|
|
92 |
[
|
200
|
93 |
char := aStream peek.
|
|
94 |
aStream atEnd not and: [char isSeparator not]
|
2
|
95 |
] whileTrue: [aStream next].
|
|
96 |
|
|
97 |
[aStream atEnd not and: [char isSeparator]] whileTrue: [
|
200
|
98 |
aStream next. char := aStream peek
|
2
|
99 |
].
|
|
100 |
aStream atEnd ifTrue: [^char].
|
|
101 |
(char isDigit) ifTrue: [ ^char ].
|
|
102 |
(char == $") ifTrue: [
|
200
|
103 |
aStream next.
|
|
104 |
char := aStream peek.
|
|
105 |
(char isLetterOrDigit
|
|
106 |
or: [(char == $#)
|
|
107 |
or: [char == Character space]]) ifFalse:[
|
|
108 |
^ self skipXPMJunkOn: aStream
|
|
109 |
] ifTrue: [^char]
|
2
|
110 |
].
|
|
111 |
|
|
112 |
^self skipXPMJunkOn: aStream.
|
|
113 |
! !
|
|
114 |
|
200
|
115 |
!PBMReader methodsFor:'reading from file'!
|
|
116 |
|
|
117 |
fromStream:aStream
|
|
118 |
"read a Portable bitmap file format as of Jef Poskanzers Portable Bitmap Package.
|
|
119 |
supported are PBM, PGB and PNM files."
|
|
120 |
|
|
121 |
| pnmType |
|
|
122 |
|
|
123 |
inStream := aStream.
|
|
124 |
inStream text.
|
|
125 |
|
|
126 |
inStream next == $P ifFalse:[
|
|
127 |
'PBMREADER: PNM format' errorPrintNL.
|
|
128 |
^nil
|
|
129 |
].
|
|
130 |
pnmType := inStream next.
|
|
131 |
pnmType == $4 ifTrue: [
|
|
132 |
^ self readDepth1PBMStream:aStream
|
|
133 |
].
|
|
134 |
pnmType == $5 ifTrue: [
|
|
135 |
^ self readDepth8PGMStream:aStream
|
|
136 |
].
|
|
137 |
pnmType == $6 ifTrue: [
|
|
138 |
^ self readDepth24PPMStream:aStream
|
|
139 |
].
|
|
140 |
'PBMREADER: No recognized PNM file format' errorPrintNL.
|
|
141 |
^ nil
|
|
142 |
|
|
143 |
"
|
|
144 |
PBMReader fromFile:'bitmaps/testimg.ppm'
|
|
145 |
PBMReader fromFile:'../../fileIn/bitmaps/keyboard.pbm'
|
|
146 |
"
|
|
147 |
!
|
|
148 |
|
|
149 |
readDepth1PBMStream:aStream
|
|
150 |
"import portable bitmap (PBM); P4 is already read"
|
|
151 |
|
|
152 |
self skipPBMJunkOn:aStream.
|
|
153 |
width := Integer readFrom:aStream.
|
|
154 |
width > 0 ifFalse: [
|
|
155 |
'PBMREADER: Invalid width' errorPrintNL.
|
|
156 |
^ nil
|
|
157 |
].
|
|
158 |
|
|
159 |
self skipPBMJunkOn:aStream.
|
|
160 |
height := Integer readFrom:aStream.
|
|
161 |
height > 0 ifFalse: [
|
|
162 |
'PBMREADER: Invalid height' errorPrintNL.
|
|
163 |
^ nil
|
|
164 |
].
|
|
165 |
|
|
166 |
aStream nextLine "skipThrough: Character cr".
|
|
167 |
aStream binary.
|
|
168 |
data := aStream contents.
|
|
169 |
|
|
170 |
photometric := #blackIs0.
|
|
171 |
samplesPerPixel := 1.
|
|
172 |
bitsPerSample := #(1).
|
|
173 |
!
|
|
174 |
|
|
175 |
readDepth24PPMStream:aStream
|
|
176 |
"import portable pixmap (PPM); P6 is already read"
|
|
177 |
|
|
178 |
| maxval |
|
|
179 |
|
|
180 |
self skipPBMJunkOn:aStream.
|
|
181 |
width := Integer readFrom:aStream.
|
|
182 |
width > 0 ifFalse: [
|
|
183 |
'PBMREADER: Invalid width' errorPrintNL.
|
|
184 |
^ nil
|
|
185 |
].
|
|
186 |
|
|
187 |
self skipPBMJunkOn:aStream.
|
|
188 |
height := Integer readFrom:aStream.
|
|
189 |
height > 0 ifFalse: [
|
|
190 |
'PBMREADER: Invalid height' errorPrintNL.
|
|
191 |
^ nil
|
|
192 |
].
|
|
193 |
|
|
194 |
self skipPBMJunkOn:aStream.
|
|
195 |
maxval := Integer readFrom:aStream.
|
|
196 |
maxval >= 256 ifTrue: [
|
|
197 |
'PBMREADER: format error' errorPrintNL.
|
|
198 |
^ nil
|
|
199 |
].
|
|
200 |
|
|
201 |
aStream skipThrough: Character cr.
|
|
202 |
aStream binary.
|
|
203 |
|
|
204 |
data := aStream contents.
|
|
205 |
photometric := #rgb.
|
|
206 |
samplesPerPixel := 3.
|
|
207 |
bitsPerSample := #(8 8 8).
|
|
208 |
!
|
|
209 |
|
|
210 |
readDepth8PGMStream:aStream
|
|
211 |
"import portable gray map (PGM); P5 is already read"
|
|
212 |
|
|
213 |
|maxval|
|
|
214 |
|
|
215 |
self skipPBMJunkOn:aStream.
|
|
216 |
width := Integer readFrom:aStream.
|
|
217 |
width > 0 ifFalse:[
|
|
218 |
'PBMREADER: Invalid width' errorPrintNL.
|
|
219 |
^ nil
|
|
220 |
].
|
|
221 |
self skipPBMJunkOn:aStream.
|
|
222 |
height := Integer readFrom:aStream.
|
|
223 |
height > 0 ifFalse:[
|
|
224 |
'PBMREADER: Invalid height' errorPrintNL.
|
|
225 |
^ nil
|
|
226 |
].
|
|
227 |
self skipPBMJunkOn:aStream.
|
|
228 |
maxval := Integer readFrom:aStream.
|
|
229 |
maxval >= 256 ifTrue:[
|
|
230 |
'PBMREADER: Invalid format' errorPrintNL.
|
|
231 |
^ nil
|
|
232 |
].
|
|
233 |
aStream skipThrough: Character cr.
|
|
234 |
aStream binary.
|
|
235 |
data := aStream contents.
|
|
236 |
|
|
237 |
photometric := #blackIs0.
|
|
238 |
samplesPerPixel := 1.
|
|
239 |
bitsPerSample := #(8).
|
|
240 |
! !
|
|
241 |
|
51
|
242 |
!PBMReader methodsFor:'testing '!
|
|
243 |
|
|
244 |
canRepresent:anImage
|
|
245 |
"return true, if anImage can be represented in my file format.
|
|
246 |
Currently only B&W and Depth8 images are supported."
|
|
247 |
|
|
248 |
|depth|
|
|
249 |
|
|
250 |
anImage photometric == #rgb ifTrue:[
|
|
251 |
^ false "/ not yet implemented
|
|
252 |
].
|
|
253 |
(depth := anImage depth) == 1 ifTrue:[^ true].
|
|
254 |
depth == 8 ifTrue:[^ true].
|
|
255 |
^ false
|
|
256 |
! !
|
|
257 |
|
2
|
258 |
!PBMReader methodsFor:'writing to file'!
|
|
259 |
|
|
260 |
save:image onFile:aFileName
|
|
261 |
"save image as PBM/PGM/PNM file on aFileName"
|
|
262 |
|
|
263 |
outStream := FileStream newFileNamed:aFileName.
|
|
264 |
outStream isNil ifTrue:[
|
46
|
265 |
'PBMREADER: create error' errorPrintNL.
|
28
|
266 |
^ nil
|
2
|
267 |
].
|
|
268 |
|
|
269 |
width := image width.
|
|
270 |
height := image height.
|
|
271 |
photometric := image photometric.
|
|
272 |
samplesPerPixel := image samplesPerPixel.
|
|
273 |
bitsPerSample := image bitsPerSample.
|
|
274 |
colorMap := image colorMap.
|
46
|
275 |
data := image bits.
|
2
|
276 |
|
|
277 |
photometric == #rgb ifTrue:[
|
46
|
278 |
^ self writePNMFileOn:outStream
|
2
|
279 |
].
|
|
280 |
samplesPerPixel == 1 ifTrue:[
|
28
|
281 |
((bitsPerSample at:1) == 1) ifTrue:[
|
46
|
282 |
^ self writePBMFileOn:outStream
|
28
|
283 |
].
|
|
284 |
((bitsPerSample at:1) == 8) ifTrue:[
|
46
|
285 |
^ self writePGMFileOn:outStream
|
28
|
286 |
].
|
2
|
287 |
].
|
|
288 |
self error:'format not supported'.
|
46
|
289 |
|
|
290 |
"
|
|
291 |
|img|
|
|
292 |
|
|
293 |
img := Image fromFile:'bitmaps/SBrowser.xbm'.
|
|
294 |
img inspect.
|
|
295 |
PBMReader save:img onFile:'test.pbm'.
|
|
296 |
img := Image fromFile:'test.pbm'.
|
|
297 |
img inspect.
|
|
298 |
|
|
299 |
|
|
300 |
|img mono|
|
|
301 |
|
|
302 |
img := Image fromFile:'bitmaps/garfield.gif'.
|
|
303 |
img inspect.
|
|
304 |
mono := img asMonochromeFormOn:Display.
|
|
305 |
img := mono asImage.
|
|
306 |
img inspect.
|
|
307 |
PBMReader save:img onFile:'test.pbm'.
|
|
308 |
img := Image fromFile:'test.pbm'.
|
|
309 |
img inspect.
|
|
310 |
"
|
21
|
311 |
!
|
|
312 |
|
46
|
313 |
writePBMFileOn:aStream
|
|
314 |
"Saves the receivers image on the file fileName in Portable Bitmap format.
|
|
315 |
See the class method pbmSyntax for details of the format."
|
|
316 |
|
|
317 |
aStream nextPutAll:'P4'; cr.
|
|
318 |
aStream nextPutAll:'# Converted from Smalltalk Form on '.
|
|
319 |
aStream nextPutAll:Date today printString.
|
|
320 |
aStream nextPutAll:' at ', Time now printString.
|
|
321 |
aStream cr.
|
|
322 |
aStream nextPutAll:width printString.
|
|
323 |
aStream space.
|
|
324 |
aStream nextPutAll:height printString.
|
|
325 |
aStream cr.
|
|
326 |
|
|
327 |
aStream binary.
|
|
328 |
photometric == #blackIs0 ifTrue:[
|
|
329 |
aStream nextPutAll:data.
|
|
330 |
] ifFalse:[
|
|
331 |
data invert.
|
|
332 |
aStream nextPutAll:data.
|
|
333 |
data invert.
|
|
334 |
].
|
|
335 |
aStream close.
|
21
|
336 |
!
|
|
337 |
|
46
|
338 |
writePGMFileOn:outStream
|
200
|
339 |
"raise an error - this is not yet implemented"
|
|
340 |
|
|
341 |
self error:'not yet implemented'
|
|
342 |
!
|
|
343 |
|
|
344 |
writePNMFileOn:outStream
|
|
345 |
"raise an error - this is not yet implemented"
|
|
346 |
|
21
|
347 |
self error:'not yet implemented'
|
2
|
348 |
! !
|
|
349 |
|
200
|
350 |
!PBMReader class methodsFor:'documentation'!
|
2
|
351 |
|
200
|
352 |
version
|
210
|
353 |
^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.17 1996-04-23 11:06:16 cg Exp $'
|
2
|
354 |
! !
|
200
|
355 |
PBMReader initialize!
|