author | Claus Gittinger <cg@exept.de> |
Tue, 14 Jan 1997 18:02:37 +0100 | |
changeset 365 | 47e0d48fd769 |
parent 303 | bb6be6b01032 |
child 398 | aef700d15416 |
permissions | -rw-r--r-- |
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:'' |
|
259 | 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:] |
|
234 | 48 |
Image Form Icon |
200 | 49 |
BlitImageReader FaceReader GIFReader JPEGReader PCXReader |
210 | 50 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
209 | 51 |
XBMReader XPMReader XWDReader |
21 | 52 |
" |
53 |
! ! |
|
2 | 54 |
|
28 | 55 |
!PBMReader class methodsFor:'initialization'! |
56 |
||
57 |
initialize |
|
200 | 58 |
"install myself in the Image classes fileFormat table |
59 |
for the `.pbm', '.pgm' and '.pnm' extensions." |
|
60 |
||
28 | 61 |
Image fileFormats at:'.pbm' put:self. |
62 |
Image fileFormats at:'.pgm' put:self. |
|
63 |
Image fileFormats at:'.pnm' put:self. |
|
64 |
! ! |
|
65 |
||
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
66 |
!PBMReader class methodsFor:'testing'! |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
67 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
68 |
isValidImageFile:aFileName |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
69 |
"return true, if aFileName contains a PBM image" |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
70 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
71 |
|inStream pnmType| |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
72 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
73 |
inStream := self streamReadingFile:aFileName. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
74 |
inStream isNil ifTrue:[^ false]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
75 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
76 |
inStream text. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
77 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
78 |
inStream next ~~ $P ifTrue:[^ false]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
79 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
80 |
pnmType := inStream next. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
81 |
(#( $1 $4 $5 $6 ) includes:pnmType) ifFalse:[^ false]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
82 |
^ true |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
83 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
84 |
"Created: 18.6.1996 / 13:58:59 / cg" |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
85 |
"Modified: 18.6.1996 / 14:06:39 / cg" |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
86 |
! ! |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
87 |
|
2 | 88 |
!PBMReader methodsFor:'private'! |
89 |
||
46 | 90 |
skipPBMJunkOn:aStream |
200 | 91 |
"this method removes any superfluous characters from the input stream." |
2 | 92 |
|
93 |
| char foundNL| |
|
94 |
||
95 |
[ |
|
200 | 96 |
char := aStream peek. |
97 |
char == $# ifTrue:[ |
|
98 |
"Start of a comment. Skip to end-of-line." |
|
46 | 99 |
"/ foundNL := (aStream skipUpTo: Character cr) notNil. |
200 | 100 |
foundNL := (aStream skipThrough: Character cr) notNil. |
101 |
foundNL ifFalse: [ |
|
102 |
"Must be EOF" |
|
103 |
^self |
|
104 |
]. |
|
105 |
char := aStream peek]. |
|
106 |
aStream atEnd not and: [char isSeparator] |
|
2 | 107 |
] whileTrue: [aStream next] |
108 |
! |
|
109 |
||
46 | 110 |
skipXPMJunkOn:aStream |
200 | 111 |
"this method removes any superfluous characters from the input stream." |
2 | 112 |
|
113 |
| char | |
|
114 |
||
115 |
[ |
|
200 | 116 |
char := aStream peek. |
117 |
aStream atEnd not and: [char isSeparator not] |
|
2 | 118 |
] whileTrue: [aStream next]. |
119 |
||
120 |
[aStream atEnd not and: [char isSeparator]] whileTrue: [ |
|
200 | 121 |
aStream next. char := aStream peek |
2 | 122 |
]. |
123 |
aStream atEnd ifTrue: [^char]. |
|
124 |
(char isDigit) ifTrue: [ ^char ]. |
|
125 |
(char == $") ifTrue: [ |
|
200 | 126 |
aStream next. |
127 |
char := aStream peek. |
|
128 |
(char isLetterOrDigit |
|
129 |
or: [(char == $#) |
|
130 |
or: [char == Character space]]) ifFalse:[ |
|
131 |
^ self skipXPMJunkOn: aStream |
|
132 |
] ifTrue: [^char] |
|
2 | 133 |
]. |
134 |
||
135 |
^self skipXPMJunkOn: aStream. |
|
136 |
! ! |
|
137 |
||
200 | 138 |
!PBMReader methodsFor:'reading from file'! |
139 |
||
140 |
fromStream:aStream |
|
141 |
"read a Portable bitmap file format as of Jef Poskanzers Portable Bitmap Package. |
|
142 |
supported are PBM, PGB and PNM files." |
|
143 |
||
144 |
| pnmType | |
|
145 |
||
146 |
inStream := aStream. |
|
147 |
inStream text. |
|
148 |
||
149 |
inStream next == $P ifFalse:[ |
|
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
150 |
'PBMREADER: PNM format' errorPrintNL. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
151 |
^nil |
200 | 152 |
]. |
153 |
pnmType := inStream next. |
|
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
154 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
155 |
(pnmType == $1) ifTrue: [ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
156 |
^ self readDepth1AsciiPBMStream:aStream |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
157 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
158 |
(pnmType == $4) ifTrue: [ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
159 |
^ self readDepth1PBMStream:aStream |
200 | 160 |
]. |
161 |
pnmType == $5 ifTrue: [ |
|
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
162 |
^ self readDepth8PGMStream:aStream |
200 | 163 |
]. |
164 |
pnmType == $6 ifTrue: [ |
|
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
165 |
^ self readDepth24PPMStream:aStream |
200 | 166 |
]. |
167 |
'PBMREADER: No recognized PNM file format' errorPrintNL. |
|
168 |
^ nil |
|
169 |
||
170 |
" |
|
171 |
PBMReader fromFile:'bitmaps/testimg.ppm' |
|
172 |
PBMReader fromFile:'../../fileIn/bitmaps/keyboard.pbm' |
|
173 |
" |
|
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
174 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
175 |
"Modified: 18.6.1996 / 14:13:38 / cg" |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
176 |
! |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
177 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
178 |
readDepth1AsciiPBMStream:aStream |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
179 |
"import portable bitmap (PBM); P1 is already read" |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
180 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
181 |
|rowBuffer line n bits rowIdx dstIdx bytesPerRow char| |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
182 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
183 |
self skipPBMJunkOn:aStream. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
184 |
width := Integer readFrom:aStream. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
185 |
width > 0 ifFalse: [ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
186 |
'PBMREADER: Invalid width' errorPrintNL. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
187 |
^ nil |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
188 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
189 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
190 |
self skipPBMJunkOn:aStream. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
191 |
height := Integer readFrom:aStream. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
192 |
height > 0 ifFalse: [ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
193 |
'PBMREADER: Invalid height' errorPrintNL. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
194 |
^ nil |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
195 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
196 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
197 |
aStream nextLine "skipThrough: Character cr". |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
198 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
199 |
bytesPerRow := (width + 7) // 8. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
200 |
data := ByteArray new:bytesPerRow * height. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
201 |
rowIdx := 1. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
202 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
203 |
1 to:height do:[:row | |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
204 |
dstIdx := rowIdx. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
205 |
bits := 0. n := 0. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
206 |
1 to:width do:[:col | |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
207 |
char := aStream next. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
208 |
[char notNil and:[char isSeparator]] whileTrue:[ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
209 |
char := aStream next |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
210 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
211 |
bits := bits bitShift:1. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
212 |
char == $1 ifTrue:[ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
213 |
bits := bits bitOr:1 |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
214 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
215 |
n := n + 1. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
216 |
n == 8 ifTrue:[ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
217 |
data at:dstIdx put:bits. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
218 |
dstIdx := dstIdx + 1. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
219 |
bits := 0. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
220 |
n := 0. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
221 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
222 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
223 |
n ~~ 0 ifTrue:[ |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
224 |
data at:dstIdx put:bits. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
225 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
226 |
rowIdx := rowIdx + bytesPerRow |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
227 |
]. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
228 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
229 |
photometric := #whiteIs0. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
230 |
samplesPerPixel := 1. |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
231 |
bitsPerSample := #(1). |
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
232 |
|
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
233 |
"Modified: 18.6.1996 / 14:16:48 / cg" |
200 | 234 |
! |
235 |
||
236 |
readDepth1PBMStream:aStream |
|
237 |
"import portable bitmap (PBM); P4 is already read" |
|
238 |
||
239 |
self skipPBMJunkOn:aStream. |
|
240 |
width := Integer readFrom:aStream. |
|
241 |
width > 0 ifFalse: [ |
|
242 |
'PBMREADER: Invalid width' errorPrintNL. |
|
243 |
^ nil |
|
244 |
]. |
|
245 |
||
246 |
self skipPBMJunkOn:aStream. |
|
247 |
height := Integer readFrom:aStream. |
|
248 |
height > 0 ifFalse: [ |
|
249 |
'PBMREADER: Invalid height' errorPrintNL. |
|
250 |
^ nil |
|
251 |
]. |
|
252 |
||
253 |
aStream nextLine "skipThrough: Character cr". |
|
254 |
aStream binary. |
|
255 |
data := aStream contents. |
|
256 |
||
257 |
photometric := #blackIs0. |
|
258 |
samplesPerPixel := 1. |
|
259 |
bitsPerSample := #(1). |
|
260 |
! |
|
261 |
||
262 |
readDepth24PPMStream:aStream |
|
263 |
"import portable pixmap (PPM); P6 is already read" |
|
264 |
||
265 |
| maxval | |
|
266 |
||
267 |
self skipPBMJunkOn:aStream. |
|
268 |
width := Integer readFrom:aStream. |
|
269 |
width > 0 ifFalse: [ |
|
270 |
'PBMREADER: Invalid width' errorPrintNL. |
|
271 |
^ nil |
|
272 |
]. |
|
273 |
||
274 |
self skipPBMJunkOn:aStream. |
|
275 |
height := Integer readFrom:aStream. |
|
276 |
height > 0 ifFalse: [ |
|
277 |
'PBMREADER: Invalid height' errorPrintNL. |
|
278 |
^ nil |
|
279 |
]. |
|
280 |
||
281 |
self skipPBMJunkOn:aStream. |
|
282 |
maxval := Integer readFrom:aStream. |
|
283 |
maxval >= 256 ifTrue: [ |
|
284 |
'PBMREADER: format error' errorPrintNL. |
|
285 |
^ nil |
|
286 |
]. |
|
287 |
||
288 |
aStream skipThrough: Character cr. |
|
289 |
aStream binary. |
|
290 |
||
291 |
data := aStream contents. |
|
292 |
photometric := #rgb. |
|
293 |
samplesPerPixel := 3. |
|
294 |
bitsPerSample := #(8 8 8). |
|
295 |
! |
|
296 |
||
297 |
readDepth8PGMStream:aStream |
|
298 |
"import portable gray map (PGM); P5 is already read" |
|
299 |
||
300 |
|maxval| |
|
301 |
||
302 |
self skipPBMJunkOn:aStream. |
|
303 |
width := Integer readFrom:aStream. |
|
304 |
width > 0 ifFalse:[ |
|
305 |
'PBMREADER: Invalid width' errorPrintNL. |
|
306 |
^ nil |
|
307 |
]. |
|
308 |
self skipPBMJunkOn:aStream. |
|
309 |
height := Integer readFrom:aStream. |
|
310 |
height > 0 ifFalse:[ |
|
311 |
'PBMREADER: Invalid height' errorPrintNL. |
|
312 |
^ nil |
|
313 |
]. |
|
314 |
self skipPBMJunkOn:aStream. |
|
315 |
maxval := Integer readFrom:aStream. |
|
316 |
maxval >= 256 ifTrue:[ |
|
317 |
'PBMREADER: Invalid format' errorPrintNL. |
|
318 |
^ nil |
|
319 |
]. |
|
320 |
aStream skipThrough: Character cr. |
|
321 |
aStream binary. |
|
322 |
data := aStream contents. |
|
323 |
||
324 |
photometric := #blackIs0. |
|
325 |
samplesPerPixel := 1. |
|
326 |
bitsPerSample := #(8). |
|
327 |
! ! |
|
328 |
||
51 | 329 |
!PBMReader methodsFor:'testing '! |
330 |
||
331 |
canRepresent:anImage |
|
332 |
"return true, if anImage can be represented in my file format. |
|
333 |
Currently only B&W and Depth8 images are supported." |
|
334 |
||
335 |
|depth| |
|
336 |
||
337 |
anImage photometric == #rgb ifTrue:[ |
|
338 |
^ false "/ not yet implemented |
|
339 |
]. |
|
340 |
(depth := anImage depth) == 1 ifTrue:[^ true]. |
|
341 |
depth == 8 ifTrue:[^ true]. |
|
342 |
^ false |
|
343 |
! ! |
|
344 |
||
2 | 345 |
!PBMReader methodsFor:'writing to file'! |
346 |
||
347 |
save:image onFile:aFileName |
|
348 |
"save image as PBM/PGM/PNM file on aFileName" |
|
349 |
||
350 |
outStream := FileStream newFileNamed:aFileName. |
|
351 |
outStream isNil ifTrue:[ |
|
46 | 352 |
'PBMREADER: create error' errorPrintNL. |
28 | 353 |
^ nil |
2 | 354 |
]. |
355 |
||
356 |
width := image width. |
|
357 |
height := image height. |
|
358 |
photometric := image photometric. |
|
359 |
samplesPerPixel := image samplesPerPixel. |
|
360 |
bitsPerSample := image bitsPerSample. |
|
361 |
colorMap := image colorMap. |
|
46 | 362 |
data := image bits. |
2 | 363 |
|
364 |
photometric == #rgb ifTrue:[ |
|
46 | 365 |
^ self writePNMFileOn:outStream |
2 | 366 |
]. |
367 |
samplesPerPixel == 1 ifTrue:[ |
|
28 | 368 |
((bitsPerSample at:1) == 1) ifTrue:[ |
46 | 369 |
^ self writePBMFileOn:outStream |
28 | 370 |
]. |
371 |
((bitsPerSample at:1) == 8) ifTrue:[ |
|
46 | 372 |
^ self writePGMFileOn:outStream |
28 | 373 |
]. |
2 | 374 |
]. |
375 |
self error:'format not supported'. |
|
46 | 376 |
|
377 |
" |
|
378 |
|img| |
|
379 |
||
380 |
img := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
381 |
img inspect. |
|
382 |
PBMReader save:img onFile:'test.pbm'. |
|
383 |
img := Image fromFile:'test.pbm'. |
|
384 |
img inspect. |
|
385 |
||
386 |
||
387 |
|img mono| |
|
388 |
||
389 |
img := Image fromFile:'bitmaps/garfield.gif'. |
|
390 |
img inspect. |
|
391 |
mono := img asMonochromeFormOn:Display. |
|
392 |
img := mono asImage. |
|
393 |
img inspect. |
|
394 |
PBMReader save:img onFile:'test.pbm'. |
|
395 |
img := Image fromFile:'test.pbm'. |
|
396 |
img inspect. |
|
397 |
" |
|
21 | 398 |
! |
399 |
||
46 | 400 |
writePBMFileOn:aStream |
401 |
"Saves the receivers image on the file fileName in Portable Bitmap format. |
|
402 |
See the class method pbmSyntax for details of the format." |
|
403 |
||
404 |
aStream nextPutAll:'P4'; cr. |
|
405 |
aStream nextPutAll:'# Converted from Smalltalk Form on '. |
|
406 |
aStream nextPutAll:Date today printString. |
|
407 |
aStream nextPutAll:' at ', Time now printString. |
|
408 |
aStream cr. |
|
409 |
aStream nextPutAll:width printString. |
|
410 |
aStream space. |
|
411 |
aStream nextPutAll:height printString. |
|
412 |
aStream cr. |
|
413 |
||
414 |
aStream binary. |
|
415 |
photometric == #blackIs0 ifTrue:[ |
|
416 |
aStream nextPutAll:data. |
|
417 |
] ifFalse:[ |
|
418 |
data invert. |
|
419 |
aStream nextPutAll:data. |
|
420 |
data invert. |
|
421 |
]. |
|
422 |
aStream close. |
|
21 | 423 |
! |
424 |
||
46 | 425 |
writePGMFileOn:outStream |
200 | 426 |
"raise an error - this is not yet implemented" |
427 |
||
428 |
self error:'not yet implemented' |
|
429 |
! |
|
430 |
||
431 |
writePNMFileOn:outStream |
|
432 |
"raise an error - this is not yet implemented" |
|
433 |
||
21 | 434 |
self error:'not yet implemented' |
2 | 435 |
! ! |
436 |
||
200 | 437 |
!PBMReader class methodsFor:'documentation'! |
2 | 438 |
|
200 | 439 |
version |
303
bb6be6b01032
support P1 (ascii-depth1) format
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
440 |
^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.20 1996-06-18 15:13:38 cg Exp $' |
2 | 441 |
! ! |
200 | 442 |
PBMReader initialize! |