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