3
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
81
|
3 |
All Rights Reserved
|
3
|
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:#Depth2Image
|
81
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
118
|
17 |
category:'Graphics-Images'
|
1
|
18 |
!
|
|
19 |
|
46
|
20 |
!Depth2Image class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
81
|
25 |
All Rights Reserved
|
3
|
26 |
|
46
|
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 |
!
|
3
|
35 |
|
46
|
36 |
documentation
|
|
37 |
"
|
|
38 |
this class represents four-color (2 bit / pixel) images
|
|
39 |
(as used mainly on the NeXT).
|
|
40 |
It mainly consists of methods already implemented in Image,
|
|
41 |
reimplemented here for more performance.
|
|
42 |
"
|
|
43 |
! !
|
3
|
44 |
|
89
|
45 |
!Depth2Image class methodsFor:'queries'!
|
|
46 |
|
|
47 |
imageDepth
|
|
48 |
^ 2
|
|
49 |
! !
|
|
50 |
|
81
|
51 |
!Depth2Image methodsFor:'accessing'!
|
1
|
52 |
|
|
53 |
atX:x y:y
|
|
54 |
"retrieve a pixel at x/y; return a color.
|
|
55 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
56 |
x = width-1, y=height-1 for lower right pixel"
|
|
57 |
|
38
|
58 |
|lineIndex "{ Class: SmallInteger }"
|
|
59 |
byte "{ Class: SmallInteger }"
|
|
60 |
shift "{ Class: SmallInteger }"
|
89
|
61 |
value "{ Class: SmallInteger }"
|
154
|
62 |
p grey|
|
1
|
63 |
|
|
64 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
65 |
|
|
66 |
"left pixel in high bits"
|
|
67 |
byte := bytes at:(lineIndex + (x // 4)).
|
154
|
68 |
|
1
|
69 |
shift := #(-6 -4 -2 0) at:((x \\ 4) + 1).
|
|
70 |
value := (byte bitShift:shift) bitAnd:3.
|
89
|
71 |
p := photometric.
|
|
72 |
p == #whiteIs0 ifTrue:[
|
|
73 |
value := 3 - value.
|
|
74 |
p := #blackIs0
|
1
|
75 |
].
|
154
|
76 |
p == #blackIs0 ifTrue:[
|
81
|
77 |
(value == 0) ifTrue:[
|
|
78 |
^ Color black
|
|
79 |
].
|
154
|
80 |
(value == 3) ifTrue:[
|
|
81 |
^ Color white
|
81
|
82 |
].
|
154
|
83 |
(value == 1) ifTrue:[
|
|
84 |
grey := 33
|
|
85 |
] ifFalse:[
|
|
86 |
grey := 67
|
81
|
87 |
].
|
154
|
88 |
^ Color grey:grey
|
1
|
89 |
].
|
|
90 |
photometric ~~ #palette ifTrue:[
|
81
|
91 |
self error:'format not supported'.
|
|
92 |
^ nil
|
1
|
93 |
].
|
89
|
94 |
^ colorMap at:(value+1).
|
1
|
95 |
!
|
|
96 |
|
|
97 |
atX:x y:y putValue:aPixelValue
|
|
98 |
"set the pixel at x/y to aPixelValue.
|
|
99 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
100 |
x = width-1, y=height-1 for lower right pixel"
|
|
101 |
|
38
|
102 |
|lineIndex "{ Class: SmallInteger }"
|
|
103 |
index "{ Class: SmallInteger }"
|
|
104 |
byte "{ Class: SmallInteger }"
|
|
105 |
shift "{ Class: SmallInteger }" |
|
1
|
106 |
|
|
107 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
108 |
|
|
109 |
"left pixel is in high bits"
|
|
110 |
index := lineIndex + (x // 4).
|
|
111 |
byte := bytes at:index.
|
|
112 |
shift := #(6 4 2 0) at:((x \\ 4) + 1).
|
|
113 |
byte := (byte bitAnd:(3 bitShift:shift) bitInvert) bitOr:(aPixelValue bitShift:shift).
|
|
114 |
bytes at:index put:byte
|
|
115 |
!
|
|
116 |
|
280
|
117 |
valueAtX:x y:y
|
|
118 |
"retrieve a pixel at x/y; return a pixel value.
|
|
119 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
120 |
x = width-1, y=height-1 for lower right pixel"
|
1
|
121 |
|
280
|
122 |
|lineIndex "{ Class: SmallInteger }"
|
|
123 |
byte "{ Class: SmallInteger }"
|
|
124 |
shift "{ Class: SmallInteger }" |
|
|
125 |
|
|
126 |
lineIndex := (self bytesPerRow * y) + 1.
|
1
|
127 |
|
|
128 |
"left pixel in high bits"
|
280
|
129 |
byte := bytes at:(lineIndex + (x // 4)).
|
|
130 |
shift := #(-6 -4 -2 0) at:((x \\ 4) + 1).
|
|
131 |
^ (byte bitShift:shift) bitAnd:3.
|
1
|
132 |
! !
|
|
133 |
|
|
134 |
!Depth2Image methodsFor:'converting greyscale images'!
|
|
135 |
|
|
136 |
greyImageAsMonoFormOn:aDevice
|
|
137 |
"return a (thresholded) monochrome Form from the picture."
|
|
138 |
|
103
|
139 |
|bytesPerRow
|
1
|
140 |
bytesPerMonoRow monoData
|
|
141 |
left4pixel "{Class: SmallInteger }"
|
|
142 |
right4pixel "{Class: SmallInteger }"
|
|
143 |
byte "{Class: SmallInteger }"
|
|
144 |
srcIndex "{Class: SmallInteger }"
|
|
145 |
dstIndex "{Class: SmallInteger }"
|
|
146 |
nextSrc "{Class: SmallInteger }"
|
|
147 |
nextDst "{Class: SmallInteger }"
|
|
148 |
bitNumber "{Class: SmallInteger }"
|
|
149 |
w "{Class: SmallInteger }"
|
|
150 |
h "{Class: SmallInteger }" |
|
|
151 |
|
|
152 |
w := width.
|
|
153 |
h := height.
|
|
154 |
|
|
155 |
bytesPerRow := self bytesPerRow.
|
|
156 |
bytesPerMonoRow := w // 8.
|
|
157 |
((w \\ 8) ~~ 0) ifTrue:[
|
81
|
158 |
bytesPerMonoRow := bytesPerMonoRow + 1
|
1
|
159 |
].
|
|
160 |
monoData := ByteArray uninitializedNew:(bytesPerMonoRow * h).
|
|
161 |
|
|
162 |
"2 bit -> 1 bit extract; take most significant bit"
|
|
163 |
|
|
164 |
srcIndex := 1.
|
|
165 |
dstIndex := 1.
|
|
166 |
1 to:h do:[:count |
|
81
|
167 |
nextSrc := srcIndex + bytesPerRow.
|
|
168 |
nextDst := dstIndex + bytesPerMonoRow.
|
|
169 |
bitNumber := 1.
|
|
170 |
[bitNumber <= w] whileTrue:[
|
|
171 |
left4pixel := bytes at:srcIndex.
|
|
172 |
srcIndex := srcIndex + 1.
|
|
173 |
byte := 0.
|
|
174 |
((left4pixel bitAnd:16r80) ~~ 0) ifTrue:[
|
|
175 |
byte := byte bitOr:2r10000000
|
|
176 |
].
|
|
177 |
((left4pixel bitAnd:16r20) ~~ 0) ifTrue:[
|
|
178 |
byte := byte bitOr:2r01000000
|
|
179 |
].
|
|
180 |
((left4pixel bitAnd:16r08) ~~ 0) ifTrue:[
|
|
181 |
byte := byte bitOr:2r00100000
|
|
182 |
].
|
|
183 |
((left4pixel bitAnd:16r02) ~~ 0) ifTrue:[
|
|
184 |
byte := byte bitOr:2r00010000
|
|
185 |
].
|
|
186 |
bitNumber := bitNumber + 4.
|
|
187 |
(bitNumber <= w) ifTrue:[
|
|
188 |
right4pixel := bytes at:srcIndex.
|
|
189 |
srcIndex := srcIndex + 1.
|
|
190 |
((right4pixel bitAnd:16r80) ~~ 0) ifTrue:[
|
|
191 |
byte := byte bitOr:2r00001000
|
|
192 |
].
|
|
193 |
((right4pixel bitAnd:16r20) ~~ 0) ifTrue:[
|
|
194 |
byte := byte bitOr:2r00000100
|
|
195 |
].
|
|
196 |
((right4pixel bitAnd:16r08) ~~ 0) ifTrue:[
|
|
197 |
byte := byte bitOr:2r00000010
|
|
198 |
].
|
|
199 |
((right4pixel bitAnd:16r02) ~~ 0) ifTrue:[
|
|
200 |
byte := byte bitOr:2r00000001
|
|
201 |
].
|
|
202 |
bitNumber := bitNumber + 4
|
|
203 |
].
|
|
204 |
monoData at:dstIndex put:byte.
|
|
205 |
dstIndex := dstIndex + 1
|
|
206 |
].
|
|
207 |
srcIndex := nextSrc.
|
|
208 |
dstIndex := nextDst
|
1
|
209 |
].
|
|
210 |
^ Form width:w height:h fromArray:monoData on:aDevice
|
|
211 |
! !
|
|
212 |
|
280
|
213 |
!Depth2Image methodsFor:'enumerating'!
|
|
214 |
|
|
215 |
atY:y from:xLow to:xHigh do:aBlock
|
|
216 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
217 |
The block is passed the color at each pixel.
|
|
218 |
This method allows slighly faster processing of an
|
|
219 |
image than using atX:y:, since some processing can be
|
|
220 |
avoided when going from pixel to pixel. However, for
|
|
221 |
real image processing, specialized methods should be written."
|
|
222 |
|
|
223 |
|srcIndex "{ Class: SmallInteger }"
|
|
224 |
byte "{ Class: SmallInteger }"
|
|
225 |
shift "{ Class: SmallInteger }"
|
|
226 |
value "{ Class: SmallInteger }"
|
|
227 |
x1 "{ Class: SmallInteger }"
|
|
228 |
x2 "{ Class: SmallInteger }"
|
|
229 |
color0 color1 color2 color3 color|
|
|
230 |
|
|
231 |
photometric == #whiteIs0 ifTrue:[
|
|
232 |
color0 := Color white.
|
|
233 |
color1 := Color grey:67.
|
|
234 |
color2 := Color grey:33.
|
|
235 |
color3 := Color black
|
|
236 |
] ifFalse:[
|
|
237 |
photometric == #blackIs0 ifTrue:[
|
|
238 |
color0 := Color black.
|
|
239 |
color1 := Color grey:33.
|
|
240 |
color2 := Color grey:67.
|
|
241 |
color3 := Color white
|
|
242 |
] ifFalse:[
|
|
243 |
photometric == #palette ifTrue:[
|
|
244 |
color0 := colorMap at:1.
|
|
245 |
color1 := colorMap at:2.
|
|
246 |
color2 := colorMap at:3.
|
|
247 |
color3 := colorMap at:4.
|
|
248 |
] ifFalse:[
|
|
249 |
self error:'format not supported'.
|
|
250 |
^ nil
|
|
251 |
]
|
|
252 |
]
|
|
253 |
].
|
|
254 |
|
|
255 |
"left pixel in high bits"
|
|
256 |
|
|
257 |
x1 := xLow.
|
|
258 |
x2 := xHigh.
|
|
259 |
|
|
260 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
261 |
srcIndex := srcIndex + (x1 // 4).
|
|
262 |
shift := #(-6 -4 -2 0) at:((x1 \\ 4) + 1).
|
|
263 |
|
|
264 |
x1 to:x2 do:[:x |
|
|
265 |
byte := bytes at:srcIndex.
|
|
266 |
value := (byte bitShift:shift) bitAnd:3.
|
|
267 |
|
|
268 |
(value == 0) ifTrue:[
|
|
269 |
color := color0
|
|
270 |
] ifFalse:[
|
|
271 |
(value == 1) ifTrue:[
|
|
272 |
color := color1
|
|
273 |
] ifFalse:[
|
|
274 |
(value == 2) ifTrue:[
|
|
275 |
color := color2
|
|
276 |
] ifFalse:[
|
|
277 |
color := color3
|
|
278 |
]
|
|
279 |
]
|
|
280 |
].
|
|
281 |
aBlock value:x value:color.
|
|
282 |
|
|
283 |
shift == 0 ifTrue:[
|
|
284 |
shift := -6.
|
|
285 |
srcIndex := srcIndex + 1
|
|
286 |
] ifFalse:[
|
|
287 |
shift := shift + 2.
|
|
288 |
]
|
|
289 |
].
|
|
290 |
!
|
|
291 |
|
|
292 |
valueAtY:y from:xLow to:xHigh do:aBlock
|
|
293 |
"perform aBlock for each pixelValue from x1 to x2 in row y.
|
|
294 |
The block is passed the x coordinate and the pixelValue at each pixel.
|
|
295 |
This method allows slighly faster processing of an
|
|
296 |
image than using valueAtX:y:, since some processing can be
|
|
297 |
avoided when going from pixel to pixel. However, for
|
|
298 |
real image processing, specialized methods should be written."
|
|
299 |
|
|
300 |
|srcIndex "{ Class: SmallInteger }"
|
|
301 |
byte "{ Class: SmallInteger }"
|
|
302 |
shift "{ Class: SmallInteger }"
|
|
303 |
value "{ Class: SmallInteger }"
|
|
304 |
x1 "{ Class: SmallInteger }"
|
|
305 |
x2 "{ Class: SmallInteger }"|
|
|
306 |
|
|
307 |
"left pixel in high bits"
|
|
308 |
|
|
309 |
x1 := xLow.
|
|
310 |
x2 := xHigh.
|
|
311 |
|
|
312 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
313 |
srcIndex := srcIndex + (x1 // 4).
|
|
314 |
shift := #(-6 -4 -2 0) at:((x1 \\ 4) + 1).
|
|
315 |
|
|
316 |
x1 to:x2 do:[:x |
|
|
317 |
byte := bytes at:srcIndex.
|
|
318 |
value := (byte bitShift:shift) bitAnd:3.
|
|
319 |
|
|
320 |
aBlock value:x value:value.
|
|
321 |
|
|
322 |
shift == 0 ifTrue:[
|
|
323 |
shift := -6.
|
|
324 |
srcIndex := srcIndex + 1
|
|
325 |
] ifFalse:[
|
|
326 |
shift := shift + 2.
|
|
327 |
]
|
|
328 |
].
|
|
329 |
! !
|
|
330 |
|
1
|
331 |
!Depth2Image methodsFor:'magnification'!
|
|
332 |
|
|
333 |
magnifyRowFrom:srcBytes offset:srcStart
|
81
|
334 |
into:dstBytes offset:dstStart factor:mX
|
1
|
335 |
|
|
336 |
"magnify a single pixel row - can only magnify by integer factors"
|
|
337 |
|
|
338 |
%{
|
|
339 |
unsigned char *srcP, *dstP;
|
|
340 |
int _mag;
|
|
341 |
REGISTER int i;
|
|
342 |
REGISTER unsigned char _byte;
|
|
343 |
int _pixels;
|
|
344 |
REGISTER int outcnt, bits, bit, mask, incnt;
|
|
345 |
int shift;
|
97
|
346 |
OBJ w = _INST(width);
|
1
|
347 |
|
|
348 |
/* helper for two-plane magnification by 2 */
|
|
349 |
static unsigned char mag2[16] = {0x00, 0x05, 0x0a, 0x0f, 0x50, 0x55, 0x5a, 0x5f,
|
81
|
350 |
0xa0, 0xa5, 0xaa, 0xaf, 0xf0, 0xf5, 0xfa, 0xff};
|
1
|
351 |
|
97
|
352 |
if (__bothSmallInteger(srcStart, dstStart)
|
|
353 |
&& __bothSmallInteger(w, mX)
|
35
|
354 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) {
|
81
|
355 |
_mag = _intVal(mX);
|
|
356 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
|
|
357 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
|
97
|
358 |
_pixels = _intVal(w);
|
1
|
359 |
|
81
|
360 |
switch (_mag) {
|
|
361 |
case 1:
|
|
362 |
break;
|
1
|
363 |
|
81
|
364 |
case 2:
|
|
365 |
/* tuned for this common case */
|
|
366 |
while (_pixels > 0) {
|
|
367 |
_byte = *srcP++;
|
|
368 |
*dstP++ = mag2[ _byte >> 4 ];
|
|
369 |
if (_pixels > 2) {
|
|
370 |
*dstP++ = mag2[ _byte & 0x0F ];
|
|
371 |
}
|
|
372 |
_pixels -= 4;
|
|
373 |
}
|
|
374 |
break;
|
1
|
375 |
|
81
|
376 |
default:
|
|
377 |
bits = 0, incnt = 0, outcnt = 0;
|
|
378 |
shift = 6;
|
|
379 |
_byte = *srcP++;
|
|
380 |
while (_pixels--) {
|
|
381 |
bit = (_byte >> shift) & 3;
|
|
382 |
incnt++;
|
|
383 |
if (incnt == 4) {
|
|
384 |
incnt = 0;
|
|
385 |
shift = 6;
|
|
386 |
_byte = *srcP++;
|
|
387 |
} else {
|
|
388 |
shift -= 2;
|
|
389 |
}
|
1
|
390 |
|
81
|
391 |
for (i=_mag; i>0; i--) {
|
|
392 |
bits = (bits << 2) | bit;
|
|
393 |
outcnt++;
|
|
394 |
if (outcnt == 4) {
|
|
395 |
*dstP++ = bits;
|
|
396 |
bits = 0;
|
|
397 |
outcnt = 0;
|
|
398 |
}
|
|
399 |
}
|
|
400 |
}
|
|
401 |
break;
|
1
|
402 |
|
81
|
403 |
}
|
|
404 |
RETURN (self);
|
1
|
405 |
}
|
|
406 |
%}
|
|
407 |
.
|
|
408 |
self primitiveFailed
|
|
409 |
! !
|
280
|
410 |
|
|
411 |
!Depth2Image methodsFor:'queries'!
|
|
412 |
|
|
413 |
bitsPerPixel
|
|
414 |
"return the number of bits per pixel"
|
|
415 |
|
|
416 |
^ 2
|
|
417 |
!
|
|
418 |
|
|
419 |
bitsPerRow
|
|
420 |
"return the number of bits in one scanline of the image"
|
|
421 |
|
|
422 |
^ width * 2
|
|
423 |
!
|
|
424 |
|
|
425 |
bitsPerSample
|
|
426 |
"return the number of bits per sample.
|
|
427 |
The return value is an array of bits-per-plane."
|
|
428 |
|
|
429 |
^ #(2)
|
|
430 |
!
|
|
431 |
|
|
432 |
bytesPerRow
|
|
433 |
"return the number of bytes in one scanline of the image"
|
|
434 |
|
|
435 |
|nbytes|
|
|
436 |
|
|
437 |
nbytes := width // 4.
|
|
438 |
((width \\ 4) ~~ 0) ifTrue:[
|
|
439 |
^ nbytes + 1
|
|
440 |
].
|
|
441 |
^ nbytes
|
|
442 |
!
|
|
443 |
|
|
444 |
samplesPerPixel
|
|
445 |
"return the number of samples per pixel in the image."
|
|
446 |
|
|
447 |
^ 1
|
|
448 |
!
|
|
449 |
|
|
450 |
usedColors
|
|
451 |
"return a collection of colors used in the receiver.
|
|
452 |
For depth1 images, this is very easy"
|
|
453 |
|
|
454 |
photometric ~~ #palette ifTrue:[
|
|
455 |
^ Array with:Color black
|
|
456 |
with:(Color grey:33)
|
|
457 |
with:(Color grey:67)
|
|
458 |
with:Color black.
|
|
459 |
].
|
|
460 |
^ colorMap
|
|
461 |
!
|
|
462 |
|
|
463 |
usedValues
|
|
464 |
"return a collection of color values used in the receiver."
|
|
465 |
|
|
466 |
"actually, this is wrong - we have to look if those are
|
|
467 |
really used. However, assume that we dont care for
|
|
468 |
those extra colors here ..."
|
|
469 |
|
|
470 |
^ #(0 1 2 3)
|
|
471 |
! !
|
|
472 |
|
|
473 |
!Depth2Image class methodsFor:'documentation'!
|
|
474 |
|
|
475 |
version
|
|
476 |
^ '$Header: /cvs/stx/stx/libview/Depth2Image.st,v 1.15 1995-12-07 11:16:18 cg Exp $'
|
|
477 |
! !
|