3912
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2017 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 |
"{ Package: 'stx:libview2' }"
|
|
13 |
|
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
|
16 |
ImageReader subclass:#WBMPReader
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Graphics-Images-Readers'
|
|
21 |
!
|
|
22 |
|
|
23 |
!WBMPReader class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2017 by Claus Gittinger
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
this class provides methods for loading and saving wbmp-bitmap-file images.
|
|
42 |
The Wireless Application Protocol Bitmap Format (shortened to Wireless Bitmap)
|
|
43 |
and with file extension .wbmp is a monochrome graphics file format optimized
|
|
44 |
for mobile computing devices.
|
|
45 |
Only monochrome images can be represented in this format.
|
|
46 |
Both reading and writing of images is supported.
|
|
47 |
|
|
48 |
[See also:]
|
|
49 |
Image Form Icon
|
|
50 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader
|
|
51 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader
|
|
52 |
XPMReader XWDReader
|
|
53 |
|
|
54 |
[author:]
|
|
55 |
Claus Gittinger
|
|
56 |
"
|
|
57 |
!
|
|
58 |
|
|
59 |
examples
|
|
60 |
"
|
|
61 |
Reading from a file:
|
|
62 |
[exBegin]
|
|
63 |
|image|
|
|
64 |
|
|
65 |
image := Image fromFile:('../../goodies/bitmaps/wbmpImages/smile.wbpm').
|
|
66 |
image inspect
|
|
67 |
[exEnd]
|
|
68 |
|
|
69 |
Writing to a file:
|
|
70 |
[exBegin]
|
|
71 |
|img|
|
|
72 |
|
|
73 |
img := Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm'.
|
|
74 |
WBMPReader save:img onFile:'../../goodies/bitmaps/wbmpBitmaps/TicTacToe.wbmp'.
|
|
75 |
img saveOn:'../../goodies/bitmaps/wbmpBitmaps/TicTacToe2.wbmp'.
|
|
76 |
[exEnd]
|
|
77 |
"
|
|
78 |
! !
|
|
79 |
|
|
80 |
!WBMPReader class methodsFor:'initialization'!
|
|
81 |
|
|
82 |
initialize
|
|
83 |
"tell Image-class, that a new fileReader is present
|
|
84 |
for the '.xbm' extension."
|
|
85 |
|
|
86 |
MIMETypes defineImageType:'image/vnd.wap.wbmp' suffix:'wbmp' reader:self.
|
|
87 |
|
|
88 |
"Modified: / 22-02-2017 / 13:48:27 / cg"
|
|
89 |
! !
|
|
90 |
|
|
91 |
!WBMPReader class methodsFor:'testing'!
|
|
92 |
|
|
93 |
canRepresent:anImage
|
|
94 |
"return true, if anImage can be represented in my file format"
|
|
95 |
|
|
96 |
|photometric clr0 clr1|
|
|
97 |
|
|
98 |
(anImage depth == 1) ifTrue:[
|
|
99 |
photometric := anImage photometric.
|
|
100 |
((photometric == #blackIs0) or:[photometric == #whiteIs0]) ifTrue:[^ true].
|
|
101 |
|
|
102 |
photometric == #palette ifTrue:[
|
|
103 |
clr0 := anImage colorFromValue:0.
|
|
104 |
clr1 := anImage colorFromValue:1.
|
|
105 |
(clr0 = Color white and:[clr1 = Color black]) ifTrue:[^true].
|
|
106 |
(clr1 = Color white and:[clr0 = Color black]) ifTrue:[^true].
|
|
107 |
].
|
|
108 |
].
|
|
109 |
('WBPMReader [info]: image depth is not 1 (only b&w images).') infoPrintCR.
|
|
110 |
^ false
|
|
111 |
|
|
112 |
"Modified: / 22-02-2017 / 13:54:10 / cg"
|
|
113 |
!
|
|
114 |
|
|
115 |
isValidImageFile:aFileName
|
|
116 |
"return true, if aFileName contains an wbmp-bitmap-file image.
|
|
117 |
Bad design: it has no file header;
|
|
118 |
so we check the dimension against the size of the file.
|
|
119 |
Very fuzzy."
|
|
120 |
|
|
121 |
|good inStream wbmpType fixedHeader width height bytesPerLine position|
|
|
122 |
|
|
123 |
inStream := self streamReadingFile:aFileName.
|
|
124 |
inStream isNil ifTrue:[^ false].
|
|
125 |
|
|
126 |
good := false.
|
|
127 |
|
|
128 |
wbmpType := inStream nextByte.
|
|
129 |
wbmpType == 0 ifTrue:[
|
|
130 |
fixedHeader := inStream nextByte.
|
|
131 |
fixedHeader == 0 ifTrue:[
|
|
132 |
|
|
133 |
width := self nextUINTvarFrom:inStream.
|
|
134 |
(width notNil and:[width between:1 and:2048]) ifTrue:[ "/ otherwise assume unreasonable width
|
|
135 |
height := self nextUINTvarFrom:inStream.
|
|
136 |
(height notNil and:[height between:1 and:2048]) ifTrue:[ "/ otherwise assume unreasonable height
|
|
137 |
bytesPerLine := (width+7) // 8.
|
|
138 |
position := inStream position.
|
|
139 |
(inStream size == ((bytesPerLine * height) + position)) ifTrue:[
|
|
140 |
good := true.
|
|
141 |
].
|
|
142 |
]
|
|
143 |
]
|
|
144 |
]
|
|
145 |
].
|
|
146 |
inStream close.
|
|
147 |
^ good
|
|
148 |
|
|
149 |
"
|
|
150 |
WBMPReader isValidImageFile:'../../goodies/bitmaps/wbmpImages/smile.wbmp'
|
|
151 |
"
|
|
152 |
|
|
153 |
"Modified: / 22-02-2017 / 14:16:41 / cg"
|
|
154 |
! !
|
|
155 |
|
|
156 |
!WBMPReader class methodsFor:'utilities'!
|
|
157 |
|
|
158 |
nextPutUINTvar:anInteger flag:mask on:aStream
|
|
159 |
anInteger > 16r7F ifTrue:[
|
|
160 |
self nextPutUINTvar:(anInteger bitShift:-7) flag:16r80 on:aStream.
|
|
161 |
].
|
|
162 |
aStream nextPutByte:((anInteger bitAnd:16r7F) bitOr:mask)
|
|
163 |
|
|
164 |
"
|
|
165 |
ByteArray streamContents:[:s | self nextPutUINTvar:0 flag:0 on:s ]
|
|
166 |
"
|
|
167 |
|
|
168 |
"Created: / 22-02-2017 / 14:42:05 / cg"
|
|
169 |
!
|
|
170 |
|
|
171 |
nextPutUINTvar:anInteger on:aStream
|
|
172 |
"write an uintvar onto a stream (see wikipedia: uintvar)"
|
|
173 |
|
|
174 |
self nextPutUINTvar:anInteger flag:0 on:aStream
|
|
175 |
|
|
176 |
"
|
|
177 |
(ByteArray streamContents:[:s | self nextPutUINTvar:0 on:s ]) hexPrintStringWithSeparator:$:
|
|
178 |
(ByteArray streamContents:[:s | self nextPutUINTvar:16r7F on:s ]) hexPrintStringWithSeparator:$:
|
|
179 |
(ByteArray streamContents:[:s | self nextPutUINTvar:16r80 on:s ]) hexPrintStringWithSeparator:$:
|
|
180 |
(ByteArray streamContents:[:s | self nextPutUINTvar:16rFF on:s ]) hexPrintStringWithSeparator:$:
|
|
181 |
|
|
182 |
1 to:10000 do:[:n |
|
|
183 |
|bytes n2|
|
|
184 |
|
|
185 |
bytes := (ByteArray streamContents:[:s | self nextPutUINTvar:n on:s]).
|
|
186 |
n2 := self nextUINTvarFrom:(bytes readStream).
|
|
187 |
self assert:(n == n2)
|
|
188 |
].
|
|
189 |
"
|
|
190 |
|
|
191 |
"Created: / 22-02-2017 / 14:42:59 / cg"
|
|
192 |
!
|
|
193 |
|
|
194 |
nextUINTvarFrom:inStream
|
|
195 |
"read an uintvar from a stream (see wikipedia: uintvar)"
|
|
196 |
|
|
197 |
|val byte|
|
|
198 |
|
|
199 |
val := 0.
|
|
200 |
[
|
|
201 |
byte := inStream nextByte.
|
|
202 |
byte isNil ifTrue:[^ nil].
|
|
203 |
val := (val bitShift:7) bitOr:(byte bitAnd:16r7F).
|
|
204 |
(byte bitTest:16r80)
|
|
205 |
] whileTrue.
|
|
206 |
^ val.
|
|
207 |
|
|
208 |
"Created: / 22-02-2017 / 14:11:37 / cg"
|
|
209 |
! !
|
|
210 |
|
|
211 |
!WBMPReader methodsFor:'reading'!
|
|
212 |
|
|
213 |
fromStream:aStream
|
|
214 |
"read an image in xbm format from aStream.
|
|
215 |
Leave image description in instance variables.
|
|
216 |
(i.e. to get the image, ask with image)."
|
|
217 |
|
|
218 |
|wbmpType fixedHeader bytesPerLine|
|
|
219 |
|
|
220 |
wbmpType := inStream nextByte.
|
|
221 |
wbmpType == 0 ifTrue:[
|
|
222 |
fixedHeader := inStream nextByte.
|
|
223 |
fixedHeader == 0 ifTrue:[
|
|
224 |
width := self class nextUINTvarFrom:inStream.
|
|
225 |
(width notNil and:[width between:1 and:2048]) ifTrue:[ "/ otherwise assume unreasonable width
|
|
226 |
height := self class nextUINTvarFrom:inStream.
|
|
227 |
(height notNil and:[height between:1 and:2048]) ifTrue:[ "/ otherwise assume unreasonable height
|
|
228 |
bytesPerLine := (width+7) // 8.
|
|
229 |
data := inStream nextBytes:(bytesPerLine * height).
|
|
230 |
photometric := #blackIs0.
|
|
231 |
samplesPerPixel := 1.
|
|
232 |
bitsPerSample := #(1).
|
|
233 |
^ self.
|
|
234 |
]
|
|
235 |
]
|
|
236 |
]
|
|
237 |
].
|
|
238 |
^ self fileFormatError:'not valid WBMP format'.
|
|
239 |
|
|
240 |
"
|
|
241 |
WBMPReader fromFile:'../../goodies/bitmaps/wbmpImages/smile.wbmp'
|
|
242 |
WBMPReader fromFile:'../../goodies/bitmaps/wbmpImages/edge.wbmp'
|
|
243 |
WBMPReader fromFile:'../../goodies/bitmaps/wbmpImages/thumb.wbmp'
|
|
244 |
"
|
|
245 |
|
|
246 |
"Modified (format): / 22-02-2017 / 14:28:30 / cg"
|
|
247 |
! !
|
|
248 |
|
|
249 |
!WBMPReader methodsFor:'writing'!
|
|
250 |
|
|
251 |
save:image onStream:aStream
|
|
252 |
"save image as XBM cdata on aStream.
|
|
253 |
Only depth1 b&w images can be represented in this format."
|
|
254 |
|
|
255 |
|srcIndex "{ Class: SmallInteger }"
|
|
256 |
rowBytes "{ Class: SmallInteger }" |
|
|
257 |
|
|
258 |
(self class canRepresent:image) ifFalse:[
|
|
259 |
^ Image cannotRepresentImageSignal
|
|
260 |
raiseWith:image
|
|
261 |
errorString:('WBMP format only supports monochrome images').
|
|
262 |
].
|
|
263 |
|
|
264 |
image mask notNil ifTrue:[
|
|
265 |
Image informationLostQuerySignal
|
|
266 |
raiseWith:image
|
|
267 |
errorString:('WBMP format does not support an imageMask').
|
|
268 |
].
|
|
269 |
|
|
270 |
outStream := aStream.
|
|
271 |
outStream nextPutByte:0. "/ type: always 0
|
|
272 |
outStream nextPutByte:0. "/ fixed header: always 0
|
|
273 |
self class nextPutUINTvar:(width := image width) on:aStream.
|
|
274 |
self class nextPutUINTvar:(height := image height) on:aStream.
|
|
275 |
|
|
276 |
rowBytes := (width + 7) // 8.
|
|
277 |
photometric := image photometric.
|
|
278 |
|
|
279 |
data := image bits.
|
|
280 |
data size == (rowBytes*height) ifTrue:[
|
|
281 |
"/ no extra padding
|
|
282 |
|
|
283 |
(photometric == #blackIs0) ifTrue:[
|
|
284 |
outStream nextPutBytes:data.
|
|
285 |
^ self.
|
|
286 |
].
|
|
287 |
(photometric == #whiteIs0) ifTrue:[
|
|
288 |
data := data copy invert.
|
|
289 |
outStream nextPutBytes:data.
|
|
290 |
^ self.
|
|
291 |
].
|
|
292 |
].
|
|
293 |
srcIndex := 1.
|
|
294 |
1 to:height do:[:rowIdx |
|
|
295 |
|row|
|
|
296 |
|
|
297 |
row := data copyFrom:srcIndex to:srcIndex+rowBytes-1.
|
|
298 |
photometric == #whiteIs0 ifTrue:[
|
|
299 |
row invert
|
|
300 |
].
|
|
301 |
outStream nextPutBytes:row.
|
|
302 |
srcIndex := srcIndex + (image bytesPerRow).
|
|
303 |
].
|
|
304 |
|
|
305 |
"
|
|
306 |
WBMPReader
|
|
307 |
save:(Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm')
|
|
308 |
onFile:'../../goodies/bitmaps/wbmpBitmaps/TicTacToe.wbmp'
|
|
309 |
"
|
|
310 |
|
|
311 |
"Modified: / 22-02-2017 / 14:54:02 / cg"
|
|
312 |
! !
|
|
313 |
|
|
314 |
!WBMPReader class methodsFor:'documentation'!
|
|
315 |
|
|
316 |
version
|
|
317 |
^ '$Header$'
|
|
318 |
!
|
|
319 |
|
|
320 |
version_CVS
|
|
321 |
^ '$Header$'
|
|
322 |
! !
|
|
323 |
|
|
324 |
|
|
325 |
WBMPReader initialize!
|