2
|
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:#PBMReader
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
PBMReader comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1992-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
this class provides methods for loading and saving Portable BitMap-file images
|
|
26 |
(Jef Poskanzers portable bitmap package)..
|
|
27 |
|
16
|
28 |
$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.4 1994-01-08 17:15:55 claus Exp $
|
2
|
29 |
written Aug 93 by claus
|
|
30 |
'!
|
|
31 |
|
|
32 |
!PBMReader methodsFor:'private'!
|
|
33 |
|
|
34 |
skipPBMJunkOn: aStream
|
|
35 |
"This method removes any superfluous characters from the input stream."
|
|
36 |
|
|
37 |
| char foundNL|
|
|
38 |
|
|
39 |
[
|
|
40 |
char := aStream peek.
|
|
41 |
char == $# ifTrue:[
|
|
42 |
"Start of a comment. Skip to end-of-line."
|
|
43 |
foundNL := (aStream skipUpTo: Character cr) notNil.
|
|
44 |
foundNL ifFalse: [
|
|
45 |
"Must be EOF"
|
|
46 |
^self
|
|
47 |
].
|
|
48 |
char := aStream peek].
|
|
49 |
aStream atEnd not and: [char isSeparator]
|
|
50 |
] whileTrue: [aStream next]
|
|
51 |
!
|
|
52 |
|
|
53 |
skipXPMJunkOn: aStream
|
|
54 |
"This method removes any superfluous characters from the input stream."
|
|
55 |
|
|
56 |
| char |
|
|
57 |
|
|
58 |
[
|
|
59 |
char := aStream peek.
|
|
60 |
aStream atEnd not and: [char isSeparator not]
|
|
61 |
] whileTrue: [aStream next].
|
|
62 |
|
|
63 |
[aStream atEnd not and: [char isSeparator]] whileTrue: [
|
|
64 |
aStream next. char := aStream peek
|
|
65 |
].
|
|
66 |
aStream atEnd ifTrue: [^char].
|
|
67 |
(char isDigit) ifTrue: [ ^char ].
|
|
68 |
(char == $") ifTrue: [
|
|
69 |
aStream next.
|
|
70 |
char := aStream peek.
|
|
71 |
((char isAlphaNumeric or: [char = $#]) or: [char = Character space]) ifFalse:[
|
|
72 |
^self skipXPMJunkOn: aStream
|
|
73 |
] ifTrue: [^char]
|
|
74 |
].
|
|
75 |
|
|
76 |
^self skipXPMJunkOn: aStream.
|
|
77 |
! !
|
|
78 |
|
|
79 |
!PBMReader methodsFor:'writing to file'!
|
|
80 |
|
|
81 |
save:image onFile:aFileName
|
|
82 |
"save image as PBM/PGM/PNM file on aFileName"
|
|
83 |
|
|
84 |
outStream := FileStream newFileNamed:aFileName.
|
|
85 |
outStream isNil ifTrue:[
|
|
86 |
'create error' printNewline.
|
|
87 |
^ nil
|
|
88 |
].
|
|
89 |
|
|
90 |
width := image width.
|
|
91 |
height := image height.
|
|
92 |
photometric := image photometric.
|
|
93 |
samplesPerPixel := image samplesPerPixel.
|
|
94 |
bitsPerSample := image bitsPerSample.
|
|
95 |
colorMap := image colorMap.
|
|
96 |
|
|
97 |
photometric == #rgb ifTrue:[
|
|
98 |
^ self writePNMFile
|
|
99 |
].
|
|
100 |
samplesPerPixel == 1 ifTrue:[
|
|
101 |
((bitsPerSample at:1) == 1) ifTrue:[
|
|
102 |
^ self writePBMFile
|
|
103 |
].
|
|
104 |
((bitsPerSample at:1) == 8) ifTrue:[
|
|
105 |
^ self writePGMFile
|
|
106 |
].
|
|
107 |
].
|
|
108 |
self error:'format not supported'.
|
|
109 |
! !
|
|
110 |
|
|
111 |
!PBMReader methodsFor:'reading from file'!
|
|
112 |
|
|
113 |
fromFile:fileName
|
|
114 |
"read a Portable bitmap file format as of Jef Poskanzers Portable Bitmap Package.
|
|
115 |
supported are PBM, PGB and PNM files."
|
|
116 |
|
|
117 |
| pnmType |
|
|
118 |
|
10
|
119 |
inStream := self class streamReadingFile:fileName.
|
|
120 |
inStream isNil ifTrue:[^ nil].
|
|
121 |
|
2
|
122 |
inStream next == $P ifFalse:[
|
|
123 |
('not PNM format in ', fileName) printNewline.
|
|
124 |
inStream close.
|
|
125 |
^nil
|
|
126 |
].
|
|
127 |
pnmType := inStream next.
|
|
128 |
inStream close.
|
|
129 |
pnmType == $4 ifTrue: [
|
|
130 |
^ self readDepth1PBMFile:fileName
|
|
131 |
].
|
|
132 |
pnmType == $5 ifTrue: [
|
|
133 |
^ self readDepth8PGMFile:fileName
|
|
134 |
].
|
|
135 |
pnmType == $6 ifTrue: [
|
|
136 |
^ self readDepth24PPMFile:fileName
|
|
137 |
].
|
|
138 |
('No recognized pnm file format in ', fileName) printNewline.
|
|
139 |
^ nil
|
|
140 |
|
|
141 |
"PBMReader fromFile:'bitmaps/testimg.ppm'"
|
|
142 |
!
|
|
143 |
|
|
144 |
readDepth1PBMFile:fileName
|
|
145 |
"import portable bitmap (PBM)"
|
|
146 |
|
10
|
147 |
inStream := self class streamReadingFile:fileName.
|
|
148 |
inStream isNil ifTrue:[^ nil].
|
2
|
149 |
|
|
150 |
(inStream next == $P) ifFalse: [
|
|
151 |
inStream close.
|
|
152 |
self error: 'not a pbm file format'
|
|
153 |
].
|
|
154 |
|
|
155 |
(inStream next == $4) ifFalse:[
|
|
156 |
inStream close.
|
|
157 |
self error: 'not a pbm file format'
|
|
158 |
].
|
|
159 |
|
|
160 |
self skipPBMJunkOn: inStream.
|
|
161 |
width := Integer readFrom: inStream.
|
|
162 |
width > 0 ifFalse: [inStream close. self error: 'Invalid width'].
|
|
163 |
|
|
164 |
self skipPBMJunkOn: inStream.
|
|
165 |
height := Integer readFrom: inStream.
|
|
166 |
height > 0 ifFalse: [inStream close. self error: 'Invalid height'].
|
|
167 |
|
|
168 |
inStream nextLine "skipThrough: Character cr".
|
|
169 |
inStream binary.
|
|
170 |
data := inStream contents.
|
|
171 |
inStream close.
|
|
172 |
|
|
173 |
photometric := #whiteIs0.
|
|
174 |
samplesPerPixel := 1.
|
|
175 |
bitsPerSample := #(1).
|
|
176 |
!
|
|
177 |
|
|
178 |
readDepth8PGMFile:fileName
|
|
179 |
"import portable gray map (PGM)"
|
|
180 |
|
|
181 |
|maxval|
|
|
182 |
|
10
|
183 |
inStream := self class streamReadingFile:fileName.
|
|
184 |
inStream isNil ifTrue:[^ nil].
|
|
185 |
|
2
|
186 |
inStream next == $P ifFalse:[
|
|
187 |
inStream close.
|
|
188 |
self error: 'not a pgm file format'
|
|
189 |
].
|
|
190 |
inStream next == $5 ifFalse:[
|
|
191 |
inStream close.
|
|
192 |
self error: 'not a pgm file format'
|
|
193 |
].
|
|
194 |
self skipPBMJunkOn: inStream.
|
|
195 |
width := Integer readFrom: inStream.
|
|
196 |
width > 0 ifFalse:[
|
|
197 |
inStream close.
|
|
198 |
self error: 'pgm read error'
|
|
199 |
].
|
|
200 |
self skipPBMJunkOn: inStream.
|
|
201 |
height := Integer readFrom: inStream.
|
|
202 |
height > 0 ifFalse:[
|
|
203 |
inStream close.
|
|
204 |
self error: 'pgm read error'
|
|
205 |
].
|
|
206 |
self skipPBMJunkOn: inStream.
|
|
207 |
maxval := Integer readFrom: inStream.
|
|
208 |
maxval >= 256 ifTrue:[
|
|
209 |
inStream close.
|
|
210 |
self error: 'pgm read error'
|
|
211 |
].
|
|
212 |
inStream skipThrough: Character cr.
|
|
213 |
inStream binary.
|
|
214 |
data := inStream contents.
|
|
215 |
|
|
216 |
photometric := #blackIs0.
|
|
217 |
samplesPerPixel := 1.
|
|
218 |
bitsPerSample := #(8).
|
|
219 |
!
|
|
220 |
|
|
221 |
readDepth24PPMFile: fileName
|
|
222 |
"import portable pixmap (PPM)"
|
|
223 |
|
|
224 |
| maxval |
|
|
225 |
|
10
|
226 |
inStream := self class streamReadingFile:fileName.
|
|
227 |
inStream isNil ifTrue:[^ nil].
|
|
228 |
|
2
|
229 |
(inStream next == $P) ifFalse: [
|
|
230 |
inStream close.
|
|
231 |
self error: 'not a ppm file format'
|
|
232 |
].
|
|
233 |
|
|
234 |
(inStream next == $6) ifFalse: [
|
|
235 |
inStream close.
|
|
236 |
self error: 'not a ppm file format'
|
|
237 |
].
|
|
238 |
|
|
239 |
self skipPBMJunkOn: inStream.
|
|
240 |
width := Integer readFrom: inStream.
|
|
241 |
width > 0 ifFalse: [inStream close. self error: 'ppm read error'].
|
|
242 |
|
|
243 |
self skipPBMJunkOn: inStream.
|
|
244 |
height := Integer readFrom: inStream.
|
|
245 |
height > 0 ifFalse: [inStream close. self error: 'ppm read error'].
|
|
246 |
|
|
247 |
self skipPBMJunkOn: inStream.
|
|
248 |
maxval := Integer readFrom: inStream.
|
|
249 |
maxval >= 256 ifTrue: [inStream close. self error: 'ppm read error'].
|
|
250 |
|
|
251 |
inStream skipThrough: Character cr.
|
|
252 |
inStream binary.
|
|
253 |
|
|
254 |
|
|
255 |
data := inStream contents.
|
|
256 |
photometric := #rgb.
|
|
257 |
samplesPerPixel := 3.
|
|
258 |
bitsPerSample := #(8 8 8).
|
|
259 |
! !
|