110 ]. |
110 ]. |
111 inStream close. |
111 inStream close. |
112 ^ ok |
112 ^ ok |
113 |
113 |
114 "Modified: / 7.12.1998 / 14:27:11 / cg" |
114 "Modified: / 7.12.1998 / 14:27:11 / cg" |
|
115 ! ! |
|
116 |
|
117 !PBMReader methodsFor:'private-reading'! |
|
118 |
|
119 readMaxVal |
|
120 self skipPBMJunk. |
|
121 maxVal := Integer readFrom:inStream onError:nil. |
|
122 (maxVal isNil or:[maxVal >= 256]) ifTrue: [ |
|
123 self fileFormatError:'Invalid format'. |
|
124 ^ false. |
|
125 ]. |
|
126 ^ true |
|
127 ! |
|
128 |
|
129 readWidthAndHeight |
|
130 self skipPBMJunk. |
|
131 width := Integer readFrom:inStream onError:0. |
|
132 width > 0 ifFalse: [ |
|
133 self fileFormatError:'Invalid width'. |
|
134 ^ false |
|
135 ]. |
|
136 |
|
137 self skipPBMJunk. |
|
138 height := Integer readFrom:inStream onError:0. |
|
139 height > 0 ifFalse: [ |
|
140 self fileFormatError:'Invalid height'. |
|
141 ^ false |
|
142 ]. |
|
143 |
|
144 self reportDimension. |
|
145 ^ true |
|
146 ! |
|
147 |
|
148 readWidthAndHeightAndMaxVal |
|
149 (self readWidthAndHeight) ifFalse:[ ^ false ]. |
|
150 ^ self readMaxVal |
|
151 ! |
|
152 |
|
153 skipPBMJunk |
|
154 "this method removes any superfluous characters from the input stream." |
|
155 |
|
156 | char foundNL| |
|
157 |
|
158 [ |
|
159 char := inStream peek. |
|
160 [char == $#] whileTrue:[ |
|
161 "Start of a comment. Skip to end-of-line." |
|
162 "/ foundNL := (aStream skipUpTo: Character cr) notNil. |
|
163 foundNL := (inStream skipThrough: Character cr) notNil. |
|
164 foundNL ifFalse: [ |
|
165 "Must be EOF" |
|
166 ^self |
|
167 ]. |
|
168 char := inStream peek |
|
169 ]. |
|
170 inStream atEnd not and: [char isSeparator] |
|
171 ] whileTrue: [inStream next] |
|
172 |
|
173 "Created: / 3.2.1998 / 17:20:37 / cg" |
|
174 "Modified: / 7.9.1998 / 15:49:07 / cg" |
|
175 ! ! |
|
176 |
|
177 !PBMReader methodsFor:'private-writing'! |
|
178 |
|
179 writeCommonHeader:format on:aStream |
|
180 "common header for P4, P5 and P5 formats" |
|
181 |
|
182 aStream nextPutAll:format; cr. |
|
183 aStream nextPutAll:'# From Smalltalk/X on '. |
|
184 aStream nextPutAll:(Date today printString). |
|
185 aStream nextPutAll:' at '; nextPutAll:(Time now printString). |
|
186 aStream cr. |
|
187 aStream nextPutAll:(width printString); space; nextPutAll:(height printString). |
|
188 |
|
189 "Created: / 14.10.1997 / 20:01:05 / cg" |
|
190 "Modified: / 1.4.1998 / 14:30:47 / cg" |
115 ! ! |
191 ! ! |
116 |
192 |
117 !PBMReader methodsFor:'reading'! |
193 !PBMReader methodsFor:'reading'! |
118 |
194 |
119 fromStream:aStream |
195 fromStream:aStream |
324 |
400 |
325 "Created: / 7.9.1998 / 15:44:05 / cg" |
401 "Created: / 7.9.1998 / 15:44:05 / cg" |
326 "Modified: / 7.9.1998 / 15:50:36 / cg" |
402 "Modified: / 7.9.1998 / 15:50:36 / cg" |
327 ! ! |
403 ! ! |
328 |
404 |
329 !PBMReader methodsFor:'reading-private'! |
|
330 |
|
331 readMaxVal |
|
332 self skipPBMJunk. |
|
333 maxVal := Integer readFrom:inStream onError:nil. |
|
334 (maxVal isNil or:[maxVal >= 256]) ifTrue: [ |
|
335 self fileFormatError:'Invalid format'. |
|
336 ^ false. |
|
337 ]. |
|
338 ^ true |
|
339 ! |
|
340 |
|
341 readWidthAndHeight |
|
342 self skipPBMJunk. |
|
343 width := Integer readFrom:inStream onError:0. |
|
344 width > 0 ifFalse: [ |
|
345 self fileFormatError:'Invalid width'. |
|
346 ^ false |
|
347 ]. |
|
348 |
|
349 self skipPBMJunk. |
|
350 height := Integer readFrom:inStream onError:0. |
|
351 height > 0 ifFalse: [ |
|
352 self fileFormatError:'Invalid height'. |
|
353 ^ false |
|
354 ]. |
|
355 |
|
356 self reportDimension. |
|
357 ^ true |
|
358 ! |
|
359 |
|
360 readWidthAndHeightAndMaxVal |
|
361 (self readWidthAndHeight) ifFalse:[ ^ false ]. |
|
362 ^ self readMaxVal |
|
363 ! |
|
364 |
|
365 skipPBMJunk |
|
366 "this method removes any superfluous characters from the input stream." |
|
367 |
|
368 | char foundNL| |
|
369 |
|
370 [ |
|
371 char := inStream peek. |
|
372 [char == $#] whileTrue:[ |
|
373 "Start of a comment. Skip to end-of-line." |
|
374 "/ foundNL := (aStream skipUpTo: Character cr) notNil. |
|
375 foundNL := (inStream skipThrough: Character cr) notNil. |
|
376 foundNL ifFalse: [ |
|
377 "Must be EOF" |
|
378 ^self |
|
379 ]. |
|
380 char := inStream peek |
|
381 ]. |
|
382 inStream atEnd not and: [char isSeparator] |
|
383 ] whileTrue: [inStream next] |
|
384 |
|
385 "Created: / 3.2.1998 / 17:20:37 / cg" |
|
386 "Modified: / 7.9.1998 / 15:49:07 / cg" |
|
387 ! ! |
|
388 |
|
389 !PBMReader methodsFor:'writing'! |
405 !PBMReader methodsFor:'writing'! |
390 |
406 |
391 save:image onStream:aStream |
407 save:image onStream:aStream |
392 "save image as PBM/PGM/PNM file on aFileName" |
408 "save image as PBM/PGM/PNM file on aFileName" |
393 |
409 |
540 " |
556 " |
541 |
557 |
542 "Modified: 14.10.1997 / 20:07:08 / cg" |
558 "Modified: 14.10.1997 / 20:07:08 / cg" |
543 ! ! |
559 ! ! |
544 |
560 |
545 !PBMReader methodsFor:'writing-private'! |
|
546 |
|
547 writeCommonHeader:format on:aStream |
|
548 "common header for P4, P5 and P5 formats" |
|
549 |
|
550 aStream nextPutAll:format; cr. |
|
551 aStream nextPutAll:'# From Smalltalk/X on '. |
|
552 aStream nextPutAll:(Date today printString). |
|
553 aStream nextPutAll:' at '; nextPutAll:(Time now printString). |
|
554 aStream cr. |
|
555 aStream nextPutAll:(width printString); space; nextPutAll:(height printString). |
|
556 |
|
557 "Created: / 14.10.1997 / 20:01:05 / cg" |
|
558 "Modified: / 1.4.1998 / 14:30:47 / cg" |
|
559 ! ! |
|
560 |
|
561 !PBMReader class methodsFor:'documentation'! |
561 !PBMReader class methodsFor:'documentation'! |
562 |
562 |
563 version |
563 version |
564 ^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.41 2003-11-19 15:27:07 cg Exp $' |
564 ^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.42 2003-11-19 15:38:51 cg Exp $' |
565 ! ! |
565 ! ! |
566 |
566 |
567 PBMReader initialize! |
567 PBMReader initialize! |