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