3
|
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 |
"
|
1
|
12 |
|
|
13 |
Image subclass:#Depth1Image
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Display Objects'
|
|
18 |
!
|
|
19 |
|
3
|
20 |
Depth1Image comment:'
|
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
54
|
23 |
|
|
24 |
$Header: /cvs/stx/stx/libview/Depth1Image.st,v 1.7 1994-08-05 01:13:52 claus Exp $
|
46
|
25 |
'!
|
3
|
26 |
|
46
|
27 |
!Depth1Image class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
32 |
All Rights Reserved
|
3
|
33 |
|
46
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
3
|
42 |
|
46
|
43 |
version
|
|
44 |
"
|
54
|
45 |
$Header: /cvs/stx/stx/libview/Depth1Image.st,v 1.7 1994-08-05 01:13:52 claus Exp $
|
46
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class represents bilevel (1 bit / pixel) images.
|
|
52 |
It mainly consists of methods already implemented in Image,
|
|
53 |
reimplemented here for more performance.
|
|
54 |
"
|
|
55 |
! !
|
3
|
56 |
|
1
|
57 |
!Depth1Image methodsFor:'accessing'!
|
|
58 |
|
|
59 |
bitsPerPixel
|
|
60 |
"return the number of bits per pixel"
|
|
61 |
|
|
62 |
^ 1
|
|
63 |
!
|
|
64 |
|
|
65 |
bitsPerRow
|
|
66 |
"return the number of bits in one scanline of the image"
|
|
67 |
|
|
68 |
^ width
|
|
69 |
!
|
|
70 |
|
|
71 |
bitsPerSample
|
|
72 |
"return the number of bits per sample.
|
|
73 |
The return value is an array of bits-per-plane."
|
|
74 |
|
|
75 |
^ #(1)
|
|
76 |
!
|
|
77 |
|
|
78 |
bytesPerRow
|
|
79 |
"return the number of bytes in one scanline of the image"
|
|
80 |
|
|
81 |
|nbytes|
|
|
82 |
|
|
83 |
nbytes := width // 8.
|
|
84 |
((width \\ 8) ~~ 0) ifTrue:[
|
|
85 |
^ nbytes + 1
|
|
86 |
].
|
|
87 |
^ nbytes
|
|
88 |
!
|
|
89 |
|
|
90 |
samplesPerPixel
|
|
91 |
"return the number of samples per pixel in the image."
|
|
92 |
|
|
93 |
^ 1
|
|
94 |
!
|
|
95 |
|
|
96 |
valueAtX:x y:y
|
|
97 |
"retrieve a pixel at x/y; return a pixel value.
|
|
98 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
99 |
x = width-1, y=height-1 for lower right pixel"
|
|
100 |
|
|
101 |
|bytesPerRow "{Class: SmallInteger}"
|
|
102 |
index "{Class: SmallInteger}"
|
|
103 |
byte "{Class: SmallInteger}"
|
|
104 |
mask "{Class: SmallInteger}"|
|
|
105 |
|
|
106 |
%{ /* NOCONTEXT */
|
35
|
107 |
if (__isByteArray(_INST(bytes))
|
1
|
108 |
&& _isSmallInteger(x)
|
|
109 |
&& _isSmallInteger(y)
|
|
110 |
&& _isSmallInteger(_INST(width)) ) {
|
46
|
111 |
int _w = _intVal(_INST(width));
|
|
112 |
int _y = _intVal(y);
|
|
113 |
int _x = _intVal(x);
|
|
114 |
unsigned _byte;
|
|
115 |
_byte = _ByteArrayInstPtr(_INST(bytes))->ba_element[(_w + 7) / 8 * _y + (_x / 8)];
|
|
116 |
RETURN( (_byte & (0x80 >> (_x % 8))) ? _MKSMALLINT(1) : _MKSMALLINT(0) );
|
1
|
117 |
}
|
|
118 |
%}
|
|
119 |
.
|
|
120 |
bytesPerRow := width // 8.
|
|
121 |
((width \\ 8) ~~ 0) ifTrue:[
|
|
122 |
bytesPerRow := bytesPerRow + 1
|
|
123 |
].
|
|
124 |
index := (bytesPerRow * y) + 1 + (x // 8).
|
|
125 |
|
|
126 |
"left pixel is in high bit"
|
|
127 |
byte := bytes at:index.
|
|
128 |
mask := #(16r80 16r40 16r20 16r10 16r08 16r04 16r02 16r01) at:((x \\ 8) + 1).
|
|
129 |
(byte bitAnd:mask) == 0 ifTrue:[^ 0].
|
|
130 |
^ 1
|
|
131 |
!
|
|
132 |
|
|
133 |
atX:x y:y
|
|
134 |
"retrieve a pixel at x/y; return a color.
|
|
135 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
136 |
x = width-1, y=height-1 for lower right pixel"
|
|
137 |
|
38
|
138 |
|lineIndex "{ Class: SmallInteger }"
|
|
139 |
byte "{ Class: SmallInteger }"
|
|
140 |
shift "{ Class: SmallInteger }"
|
|
141 |
value "{ Class: SmallInteger }"|
|
1
|
142 |
|
|
143 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
144 |
|
|
145 |
"left pixel is in high bit"
|
|
146 |
byte := bytes at:(lineIndex + (x // 8)).
|
|
147 |
shift := #(-7 -6 -5 -4 -3 -2 -1 0) at:((x \\ 8) + 1).
|
|
148 |
value := (byte bitShift:shift) bitAnd:1.
|
|
149 |
photometric == #whiteIs0 ifTrue:[
|
|
150 |
(value == 0) ifTrue:[
|
|
151 |
^ Color white
|
|
152 |
].
|
|
153 |
^ Color black
|
|
154 |
].
|
|
155 |
photometric == #blackIs0 ifTrue:[
|
|
156 |
(value == 0) ifTrue:[
|
|
157 |
^ Color black
|
|
158 |
].
|
|
159 |
^ Color white
|
|
160 |
].
|
|
161 |
photometric ~~ #palette ifTrue:[
|
|
162 |
self error:'format not supported'.
|
|
163 |
^ nil
|
|
164 |
].
|
38
|
165 |
value := value + 1.
|
|
166 |
^ Color red:(((colorMap at:1) at:value) * (100.0 / 255.0))
|
|
167 |
green:(((colorMap at:2) at:value) * (100.0 / 255.0))
|
|
168 |
blue:(((colorMap at:3) at:value) * (100.0 / 255.0))
|
1
|
169 |
!
|
|
170 |
|
|
171 |
atX:x y:y putValue:aPixelValue
|
|
172 |
"set the pixel at x/y to aPixelValue.
|
|
173 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
174 |
x = width-1, y=height-1 for lower right pixel"
|
|
175 |
|
|
176 |
|bytesPerRow "{Class: SmallInteger}"
|
|
177 |
index "{Class: SmallInteger}"
|
|
178 |
byte "{Class: SmallInteger}"
|
|
179 |
mask "{Class: SmallInteger}"|
|
|
180 |
|
|
181 |
%{ /* NOCONTEXT */
|
35
|
182 |
if (__isByteArray(_INST(bytes))
|
1
|
183 |
&& _isSmallInteger(x)
|
|
184 |
&& _isSmallInteger(y)
|
|
185 |
&& _isSmallInteger(_INST(width)) ) {
|
|
186 |
int _w = _intVal(_INST(width));
|
|
187 |
int _y = _intVal(y);
|
|
188 |
int _x = _intVal(x);
|
46
|
189 |
int _idx;
|
1
|
190 |
|
46
|
191 |
_idx = (_w + 7) / 8 * _y + (_x / 8);
|
|
192 |
if (aPixelValue == _MKSMALLINT(0)) {
|
1
|
193 |
_ByteArrayInstPtr(_INST(bytes))->ba_element[_idx] &= ~(0x80 >> (_x % 8));
|
46
|
194 |
} else {
|
1
|
195 |
_ByteArrayInstPtr(_INST(bytes))->ba_element[_idx] |= (0x80 >> (_x % 8));
|
46
|
196 |
}
|
1
|
197 |
RETURN( self );
|
|
198 |
}
|
|
199 |
%}
|
|
200 |
.
|
|
201 |
bytesPerRow := width // 8.
|
|
202 |
((width \\ 8) ~~ 0) ifTrue:[
|
|
203 |
bytesPerRow := bytesPerRow + 1
|
|
204 |
].
|
|
205 |
index := (bytesPerRow * y) + 1 + (x // 8).
|
|
206 |
|
|
207 |
"left pixel is in high bit"
|
|
208 |
byte := bytes at:index.
|
|
209 |
mask := #(16r80 16r40 16r20 16r10 16r08 16r04 16r02 16r01) at:((x \\ 8) + 1).
|
|
210 |
aPixelValue == 0 ifTrue:[
|
|
211 |
byte := byte bitAnd:(mask bitInvert)
|
|
212 |
] ifFalse:[
|
|
213 |
byte := byte bitOr:mask
|
|
214 |
].
|
|
215 |
bytes at:index put:byte
|
|
216 |
!
|
|
217 |
|
|
218 |
atX:x y:y put:aColor
|
|
219 |
"set the pixel at x/y to aColor.
|
|
220 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
221 |
x = width-1, y=height-1 for lower right pixel.
|
|
222 |
This method checks, if it is possible to store the color
|
|
223 |
in the image; i.e. for b/w images, the color MUST be black
|
|
224 |
or white; for palette images it must be present in the palette."
|
|
225 |
|
38
|
226 |
|clr0 clr1|
|
1
|
227 |
|
|
228 |
photometric == #whiteIs0 ifTrue:[
|
46
|
229 |
clr0 := Color whilte.
|
|
230 |
clr1 := Color black.
|
1
|
231 |
] ifFalse:[
|
|
232 |
photometric == #blackIs0 ifTrue:[
|
46
|
233 |
clr0 := Color black.
|
|
234 |
clr1 := Color whilte.
|
1
|
235 |
] ifFalse:[
|
|
236 |
photometric ~~ #palette ifTrue:[
|
|
237 |
self error:'format not supported'.
|
|
238 |
^ nil
|
|
239 |
].
|
46
|
240 |
clr0 := colorMap at:1.
|
|
241 |
clr1 := colorMap at:2.
|
1
|
242 |
]
|
|
243 |
].
|
38
|
244 |
aColor = clr0 ifTrue:[
|
|
245 |
self atX:x y:y putValue:0.
|
46
|
246 |
^ self
|
38
|
247 |
].
|
|
248 |
aColor = clr1 ifTrue:[
|
|
249 |
self atX:x y:y putValue:1.
|
46
|
250 |
^ self
|
38
|
251 |
].
|
|
252 |
"
|
|
253 |
the color to be stored is not in the images
|
|
254 |
colormap
|
|
255 |
"
|
|
256 |
self error:'invalid color'
|
1
|
257 |
!
|
|
258 |
|
|
259 |
atY:y from:xLow to:xHigh do:aBlock
|
|
260 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
261 |
The block is passed the color at each pixel.
|
|
262 |
This method allows slighly faster processing of an
|
|
263 |
image than using atX:y:, since some processing can be
|
|
264 |
avoided when going from pixel to pixel. However, for
|
|
265 |
real image processing, specialized methods should be written."
|
|
266 |
|
|
267 |
|srcIndex "{ Class: SmallInteger }"
|
|
268 |
byte "{ Class: SmallInteger }"
|
|
269 |
mask "{ Class: SmallInteger }"
|
|
270 |
x1 "{ Class: SmallInteger }"
|
|
271 |
x2 "{ Class: SmallInteger }"
|
|
272 |
color0 color1 color|
|
|
273 |
|
|
274 |
x1 := xLow.
|
|
275 |
x2 := xHigh.
|
|
276 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
277 |
|
|
278 |
"left pixel is in high bit"
|
|
279 |
|
|
280 |
photometric == #whiteIs0 ifTrue:[
|
|
281 |
color0 := Color white.
|
|
282 |
color1 := Color black
|
|
283 |
] ifFalse:[
|
|
284 |
photometric == #blackIs0 ifTrue:[
|
|
285 |
color0 := Color black.
|
|
286 |
color1 := Color white
|
|
287 |
] ifFalse:[
|
|
288 |
photometric == #palette ifTrue:[
|
|
289 |
color0 := Color red:(((colorMap at:1) at:1) * 100 / 255)
|
|
290 |
green:(((colorMap at:2) at:1) * 100 / 255)
|
|
291 |
blue:(((colorMap at:3) at:1) * 100 / 255).
|
|
292 |
color1 := Color red:(((colorMap at:1) at:2) * 100 / 255)
|
|
293 |
green:(((colorMap at:2) at:2) * 100 / 255)
|
|
294 |
blue:(((colorMap at:3) at:2) * 100 / 255).
|
|
295 |
] ifFalse:[
|
|
296 |
self error:'format not supported'.
|
|
297 |
^ nil
|
|
298 |
]
|
|
299 |
]
|
|
300 |
].
|
|
301 |
|
|
302 |
srcIndex := srcIndex + (x1 // 8).
|
12
|
303 |
mask := #[2r10000000
|
1
|
304 |
2r01000000
|
|
305 |
2r00100000
|
|
306 |
2r00010000
|
|
307 |
2r00001000
|
|
308 |
2r00000100
|
|
309 |
2r00000010
|
12
|
310 |
2r00000001] at:((x1 \\ 8) + 1).
|
1
|
311 |
|
|
312 |
x1 to:x2 do:[:x |
|
|
313 |
byte := bytes at:srcIndex.
|
|
314 |
(byte bitAnd:mask) == 0 ifTrue:[
|
|
315 |
color := color0
|
|
316 |
] ifFalse:[
|
|
317 |
color := color1
|
|
318 |
].
|
|
319 |
aBlock value:x value:color.
|
|
320 |
|
|
321 |
mask := mask bitShift:-1.
|
|
322 |
mask == 0 ifTrue:[
|
|
323 |
mask := 2r10000000.
|
|
324 |
srcIndex := srcIndex + 1
|
|
325 |
]
|
|
326 |
]
|
|
327 |
! !
|
|
328 |
|
|
329 |
!Depth1Image methodsFor:'converting greyscale images'!
|
|
330 |
|
|
331 |
greyImageAsFormOn:aDevice
|
|
332 |
"convert a greyscale image to a device form"
|
|
333 |
|
|
334 |
|f|
|
|
335 |
|
|
336 |
f := Form width:width height:height fromArray:bytes on:aDevice.
|
|
337 |
photometric == #blackIs0 ifTrue:[
|
|
338 |
f function:#xor.
|
|
339 |
f paint:(Color colorId:1).
|
|
340 |
f fillRectangleX:0 y:0 width:width height:height
|
|
341 |
].
|
|
342 |
^ f
|
46
|
343 |
!
|
|
344 |
|
|
345 |
greyImageAsMonoFormOn:aDevice
|
|
346 |
"convert to a monochrome form - thats easy"
|
|
347 |
|
|
348 |
^ self greyImageAsFormOn:aDevice
|
1
|
349 |
! !
|
|
350 |
|
|
351 |
!Depth1Image methodsFor:'magnification'!
|
|
352 |
|
|
353 |
magnifyRowFrom:srcBytes offset:srcStart
|
|
354 |
into:dstBytes offset:dstStart factor:mX
|
|
355 |
|
|
356 |
"magnify a single pixel row - can only magnify by integer factors"
|
|
357 |
|
|
358 |
%{
|
|
359 |
unsigned char *srcP, *dstP;
|
|
360 |
int _mag;
|
|
361 |
REGISTER int i;
|
|
362 |
REGISTER unsigned char _byte;
|
|
363 |
int _pixels, bpr;
|
|
364 |
REGISTER int outcnt, bits, bit, mask, incnt;
|
|
365 |
int shift;
|
|
366 |
unsigned char byte1, byte2, byte3, byte4;
|
|
367 |
|
|
368 |
/* helper for monochrome magnification by 2 */
|
|
369 |
static unsigned char mag1[16] = {0x00, 0x03, 0x0c, 0x0f, 0x30, 0x33, 0x3c, 0x3f,
|
|
370 |
0xc0, 0xc3, 0xcc, 0xcf, 0xf0, 0xf3, 0xfc, 0xff};
|
|
371 |
|
|
372 |
if (_isSmallInteger(srcStart) && _isSmallInteger(dstStart)
|
|
373 |
&& _isSmallInteger(_INST(width)) && _isSmallInteger(mX)
|
35
|
374 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) {
|
1
|
375 |
_mag = _intVal(mX);
|
|
376 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
|
|
377 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
|
|
378 |
_pixels = _intVal(_INST(width));
|
|
379 |
|
|
380 |
switch (_mag) {
|
|
381 |
case 1:
|
|
382 |
break;
|
|
383 |
|
|
384 |
case 2:
|
|
385 |
/* tuned for this common case */
|
|
386 |
while (_pixels > 0) {
|
|
387 |
_byte = *srcP++;
|
|
388 |
*dstP++ = mag1[ _byte >> 4 ];
|
|
389 |
if (_pixels > 4) {
|
|
390 |
*dstP++ = mag1[ _byte & 0x0F ];
|
|
391 |
}
|
|
392 |
_pixels -= 8;
|
|
393 |
}
|
|
394 |
break;
|
|
395 |
|
|
396 |
case 4:
|
12
|
397 |
/* tuned for this common case */
|
1
|
398 |
while (_pixels > 0) {
|
|
399 |
_byte = *srcP++;
|
|
400 |
byte1 = mag1[_byte >> 4];
|
|
401 |
byte2 = mag1[byte1 & 0xF];
|
|
402 |
byte1 = mag1[byte1 >> 4];
|
|
403 |
byte3 = mag1[ _byte & 0x0F ];
|
|
404 |
byte4 = mag1[byte3 & 0xF];
|
|
405 |
byte3 = mag1[byte3 >> 4];
|
|
406 |
|
|
407 |
*dstP++ = byte1;
|
|
408 |
if (_pixels > 2) {
|
|
409 |
*dstP++ = byte2;
|
|
410 |
if (_pixels > 4) {
|
|
411 |
*dstP++ = byte3;
|
|
412 |
if (_pixels > 6) {
|
|
413 |
*dstP++ = byte4;
|
|
414 |
}
|
|
415 |
}
|
|
416 |
}
|
|
417 |
_pixels -= 8;
|
|
418 |
}
|
|
419 |
break;
|
|
420 |
|
|
421 |
default:
|
|
422 |
bits = 0, incnt = 0, outcnt = 0;
|
|
423 |
mask = 0x80;
|
|
424 |
_byte = *srcP++;
|
|
425 |
while (_pixels--) {
|
|
426 |
if (_byte & mask)
|
|
427 |
bit = 1;
|
|
428 |
else
|
|
429 |
bit = 0;
|
|
430 |
incnt++;
|
|
431 |
if (incnt == 8) {
|
|
432 |
incnt = 0;
|
|
433 |
mask = 0x80;
|
|
434 |
_byte = *srcP++;
|
|
435 |
} else {
|
|
436 |
mask >>= 1;
|
|
437 |
}
|
|
438 |
|
|
439 |
for (i=_mag; i>0; i--) {
|
|
440 |
bits = (bits << 1) | bit;
|
|
441 |
outcnt++;
|
|
442 |
if (outcnt == 8) {
|
|
443 |
*dstP++ = bits;
|
|
444 |
bits = 0;
|
|
445 |
outcnt = 0;
|
|
446 |
}
|
|
447 |
}
|
|
448 |
}
|
|
449 |
break;
|
|
450 |
}
|
|
451 |
RETURN (self);
|
|
452 |
}
|
|
453 |
%}
|
|
454 |
.
|
|
455 |
self primitiveFailed
|
|
456 |
! !
|