author | Claus Gittinger <cg@exept.de> |
Sat, 08 Jun 1996 13:04:12 +0200 | |
changeset 803 | 899f7eb32ef2 |
parent 798 | 31ed4a1d4b4a |
child 806 | 8d8a58e12c08 |
permissions | -rw-r--r-- |
4 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 3 |
All Rights Reserved |
4 | 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:#Depth4Image |
|
579 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images' |
|
1 | 18 |
! |
19 |
||
46 | 20 |
!Depth4Image class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 25 |
All Rights Reserved |
4 | 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 |
! |
|
4 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
38 |
this class represents 16-color (4 bit / pixel) images. |
|
39 |
Most images coming from the windows world are represented as Depth4Images. |
|
40 |
It mainly consists of methods already implemented in Image, |
|
41 |
reimplemented here for more performance. |
|
611 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
45 |
||
46 |
[see also:] |
|
47 |
Depth1Image Depth2Image Depth8Image Depth16Image Depth24Image |
|
48 |
ImageReader |
|
46 | 49 |
" |
50 |
! ! |
|
4 | 51 |
|
89 | 52 |
!Depth4Image class methodsFor:'queries'! |
53 |
||
54 |
imageDepth |
|
579 | 55 |
"return the depth of images represented by instances of |
56 |
this class - here we return 4" |
|
57 |
||
89 | 58 |
^ 4 |
579 | 59 |
|
60 |
"Modified: 20.4.1996 / 23:40:18 / cg" |
|
89 | 61 |
! ! |
62 |
||
81 | 63 |
!Depth4Image methodsFor:'accessing'! |
1 | 64 |
|
65 |
atX:x y:y |
|
66 |
"retrieve a pixel at x/y; return a color. |
|
67 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
68 |
x = width-1, y=height-1 for lower right pixel" |
|
69 |
||
46 | 70 |
|lineIndex "{ Class: SmallInteger }" |
71 |
byte "{ Class: SmallInteger }" |
|
72 |
value "{ Class: SmallInteger }" | |
|
1 | 73 |
|
74 |
lineIndex := (self bytesPerRow * y) + 1. |
|
75 |
||
76 |
"left pixel in high bits" |
|
77 |
byte := bytes at:(lineIndex + (x // 2)). |
|
78 |
x even ifTrue:[ |
|
746 | 79 |
value := (byte bitShift:-4) bitAnd:16rF. |
1 | 80 |
] ifFalse:[ |
746 | 81 |
value := byte bitAnd:16rF. |
1 | 82 |
]. |
83 |
photometric == #whiteIs0 ifTrue:[ |
|
746 | 84 |
^ Color gray:100 - (100 / 15 * value) |
1 | 85 |
]. |
86 |
photometric == #blackIs0 ifTrue:[ |
|
746 | 87 |
^ Color gray:(100 / 15 * value) |
1 | 88 |
]. |
89 |
photometric ~~ #palette ifTrue:[ |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
90 |
^ self colorFromValue:value |
1 | 91 |
]. |
89 | 92 |
^ colorMap at:(value + 1) |
746 | 93 |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
94 |
"Modified: 8.6.1996 / 10:52:43 / cg" |
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 |
||
46 | 102 |
|lineIndex "{ Class: SmallInteger }" |
103 |
index "{ Class: SmallInteger }" |
|
104 |
byte "{ Class: SmallInteger }" | |
|
1 | 105 |
|
106 |
lineIndex := (self bytesPerRow * y) + 1. |
|
107 |
||
108 |
"left pixel is in high bits" |
|
109 |
index := lineIndex + (x // 2). |
|
110 |
byte := bytes at:index. |
|
111 |
x even ifTrue:[ |
|
81 | 112 |
byte := (byte bitAnd:16rF) bitOr:(aPixelValue bitShift:4) |
1 | 113 |
] ifFalse:[ |
81 | 114 |
byte := (byte bitAnd:16rF0) bitOr:aPixelValue |
1 | 115 |
]. |
116 |
bytes at:index put:byte |
|
280 | 117 |
! |
118 |
||
119 |
valueAtX:x y:y |
|
120 |
"retrieve a pixel at x/y; return a pixelValue. |
|
121 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
122 |
x = width-1, y=height-1 for lower right pixel" |
|
123 |
||
124 |
|lineIndex "{ Class: SmallInteger }" |
|
125 |
byte "{ Class: SmallInteger }" | |
|
126 |
||
127 |
lineIndex := (self bytesPerRow * y) + 1. |
|
128 |
||
129 |
"left pixel in high bits" |
|
130 |
byte := bytes at:(lineIndex + (x // 2)). |
|
131 |
x even ifTrue:[ |
|
132 |
^ (byte bitShift:-4) bitAnd:16rF. |
|
133 |
]. |
|
134 |
^ byte bitAnd:16rF. |
|
81 | 135 |
! ! |
136 |
||
798 | 137 |
!Depth4Image methodsFor:'converting'! |
138 |
||
139 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
|
140 |
"return the dithered monochrome bits for the receiver image; |
|
141 |
with a constant ditherMatrix, this can be used for thresholding. |
|
142 |
Redefined to make use of knowing that pixels are 4-bit values." |
|
143 |
||
144 |
|f dH nDither |
|
145 |
greyMap monoBits |
|
146 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
147 |
bytesPerRow "{Class: SmallInteger }" |
|
148 |
w "{Class: SmallInteger }" |
|
149 |
h "{Class: SmallInteger }"| |
|
150 |
||
151 |
nDither := ditherMatrix size. |
|
152 |
dH := nDither / dW. |
|
153 |
||
154 |
w := width. |
|
155 |
h := height. |
|
156 |
||
157 |
bytesPerRow := self bytesPerRow. |
|
158 |
||
159 |
bytesPerMonoRow := w + 7 // 8. |
|
160 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
161 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
162 |
^ nil |
|
163 |
]. |
|
164 |
||
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
165 |
greyMap := self greyByteMapForRange:nDither. |
798 | 166 |
|
167 |
%{ |
|
168 |
int __dW = __intVal(dW); |
|
169 |
int __dH = __intVal(dH); |
|
170 |
int __byte; |
|
171 |
int __dT; |
|
172 |
int __dstIdx = 0; |
|
173 |
int __srcIdx = 0; |
|
174 |
int __bitCnt; |
|
175 |
int __inByte; |
|
176 |
int __grey; |
|
177 |
int __w = __intVal(w); |
|
178 |
int __h = __intVal(h); |
|
179 |
int __x; |
|
180 |
int __y; |
|
181 |
int __oX, __oY, __dY; |
|
182 |
int __nextDst; |
|
183 |
int __nextSrc; |
|
184 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
185 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
186 |
||
187 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
188 |
char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
189 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
190 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
191 |
||
192 |
__oY = __dY = 0; |
|
193 |
for (__y=0; __y<__h; __y++) { |
|
194 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
195 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
196 |
||
197 |
__byte = 0; |
|
198 |
__bitCnt = 8; |
|
199 |
||
200 |
__oX = 0; |
|
201 |
||
202 |
for (__x=0; __x<__w; __x++) { |
|
203 |
if (__x & 1) { |
|
204 |
/* odd */ |
|
205 |
__grey = __inByte & 0xF; |
|
206 |
__srcIdx++; |
|
207 |
} else { |
|
208 |
__inByte = __bytes[__srcIdx]; /* 0..255 */ |
|
209 |
__grey = (__inByte >> 4) & 0xF; |
|
210 |
} |
|
211 |
||
212 |
__grey = __greyMap[__grey]; |
|
213 |
__dT = __ditherMatrix[__dY + __oX]; |
|
214 |
||
215 |
__oX++; |
|
216 |
if (__oX == __dW) __oX = 0; |
|
217 |
||
218 |
__byte = __byte << 1; |
|
219 |
if (__grey > __dT) { |
|
220 |
__byte = __byte | 1; /* white */ |
|
221 |
} |
|
222 |
||
223 |
__bitCnt--; |
|
224 |
if (__bitCnt == 0) { |
|
225 |
__monoBits[__dstIdx] = __byte; |
|
226 |
__dstIdx++; |
|
227 |
__byte = 0; |
|
228 |
__bitCnt = 8; |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
if (__bitCnt != 8) { |
|
233 |
__byte = __byte << __bitCnt; |
|
234 |
__monoBits[__dstIdx] = __byte; |
|
235 |
} |
|
236 |
||
237 |
__oY++; __dY += __dW; |
|
238 |
if (__oY == __dH) { |
|
239 |
__oY = 0; |
|
240 |
__dY = 0; |
|
241 |
} |
|
242 |
||
243 |
__srcIdx = __nextSrc; |
|
244 |
__dstIdx = __nextDst; |
|
245 |
} |
|
246 |
%}. |
|
247 |
||
248 |
^ monoBits |
|
249 |
! ! |
|
250 |
||
89 | 251 |
!Depth4Image methodsFor:'enumerating'! |
81 | 252 |
|
798 | 253 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
1 | 254 |
"perform aBlock for each pixel from x1 to x2 in row y. |
255 |
The block is passed the color at each pixel. |
|
256 |
This method allows slighly faster processing of an |
|
257 |
image than using atX:y:, since some processing can be |
|
258 |
avoided when going from pixel to pixel. However, for |
|
259 |
real image processing, specialized methods should be written." |
|
260 |
||
261 |
|srcIndex "{ Class: SmallInteger }" |
|
262 |
byte "{ Class: SmallInteger }" |
|
263 |
shift "{ Class: SmallInteger }" |
|
264 |
value "{ Class: SmallInteger }" |
|
265 |
x1 "{ Class: SmallInteger }" |
|
266 |
x2 "{ Class: SmallInteger }" |
|
89 | 267 |
colors p | |
1 | 268 |
|
798 | 269 |
(p := photometric) == #palette ifTrue:[ |
270 |
colors := colorMap. |
|
271 |
] ifFalse:[ |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
272 |
(p == #whiteIs0 or:[p == #blackIs0]) ifTrue:[ |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
273 |
colors := Array new:16. |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
274 |
0 to:15 do:[:i | |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
275 |
colors at:(i+1) put:(Color gray:100 - (100 / 15 * i)) |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
276 |
]. |
798 | 277 |
p == #blackIs0 ifTrue:[ |
278 |
colors reverse |
|
746 | 279 |
] |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
280 |
] ifFalse:[ |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
281 |
^ super colorsAtY:y from:xLow to:xHigh do:aBlock. |
746 | 282 |
] |
1 | 283 |
]. |
284 |
||
285 |
x1 := xLow. |
|
286 |
x2 := xHigh. |
|
287 |
srcIndex := (self bytesPerRow * y) + 1. |
|
288 |
srcIndex := srcIndex + (x1 // 2). |
|
12 | 289 |
x1 even ifTrue:[ |
746 | 290 |
shift := -4 |
12 | 291 |
] ifFalse:[ |
746 | 292 |
shift := 0 |
12 | 293 |
]. |
1 | 294 |
|
295 |
x1 to:x2 do:[:x | |
|
746 | 296 |
shift == 0 ifTrue:[ |
297 |
byte := bytes at:srcIndex. |
|
298 |
value := byte bitAnd:16rF. |
|
299 |
shift := -4. |
|
300 |
srcIndex := srcIndex + 1. |
|
301 |
] ifFalse:[ |
|
302 |
byte := bytes at:srcIndex. |
|
303 |
value := (byte bitShift:-4) bitAnd:16rF. |
|
304 |
shift := 0 |
|
305 |
]. |
|
306 |
aBlock value:x value:(colors at:(value + 1)). |
|
1 | 307 |
] |
746 | 308 |
|
798 | 309 |
"Created: 7.6.1996 / 19:12:33 / cg" |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
310 |
"Modified: 8.6.1996 / 10:16:05 / cg" |
280 | 311 |
! |
312 |
||
798 | 313 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
280 | 314 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
315 |
The block is passed the pixelValue at each pixel. |
|
316 |
This method allows slighly faster processing of an |
|
317 |
image than using valueAtX:y:, since some processing can be |
|
318 |
avoided when going from pixel to pixel. However, for |
|
319 |
real image processing, specialized methods should be written." |
|
320 |
||
321 |
|srcIndex "{ Class: SmallInteger }" |
|
322 |
byte "{ Class: SmallInteger }" |
|
323 |
shift "{ Class: SmallInteger }" |
|
324 |
pixelValue "{ Class: SmallInteger }" |
|
325 |
x1 "{ Class: SmallInteger }" |
|
326 |
x2 "{ Class: SmallInteger }" |
|
327 |
| |
|
328 |
||
329 |
x1 := xLow. |
|
330 |
x2 := xHigh. |
|
331 |
srcIndex := (self bytesPerRow * y) + 1. |
|
332 |
srcIndex := srcIndex + (x1 // 2). |
|
333 |
x1 even ifTrue:[ |
|
798 | 334 |
shift := -4 |
280 | 335 |
] ifFalse:[ |
798 | 336 |
shift := 0 |
280 | 337 |
]. |
338 |
||
339 |
x1 to:x2 do:[:x | |
|
798 | 340 |
shift == 0 ifTrue:[ |
341 |
byte := bytes at:srcIndex. |
|
342 |
pixelValue := byte bitAnd:16rF. |
|
343 |
shift := -4. |
|
344 |
srcIndex := srcIndex + 1. |
|
345 |
] ifFalse:[ |
|
346 |
byte := bytes at:srcIndex. |
|
347 |
pixelValue := (byte bitShift:-4) bitAnd:16rF. |
|
348 |
shift := 0 |
|
349 |
]. |
|
350 |
aBlock value:x value:pixelValue. |
|
280 | 351 |
] |
798 | 352 |
|
353 |
"Created: 7.6.1996 / 19:09:45 / cg" |
|
1 | 354 |
! ! |
355 |
||
356 |
!Depth4Image methodsFor:'magnification'! |
|
357 |
||
358 |
magnifyRowFrom:srcBytes offset:srcStart |
|
81 | 359 |
into:dstBytes offset:dstStart factor:mX |
1 | 360 |
|
89 | 361 |
"magnify a single pixel row - can only magnify by integer factors. |
362 |
Specially tuned for factor 2." |
|
1 | 363 |
|
364 |
%{ |
|
365 |
unsigned char *srcP, *dstP; |
|
366 |
int _mag; |
|
367 |
REGISTER int i; |
|
368 |
REGISTER unsigned char _byte; |
|
369 |
int _pixels; |
|
370 |
REGISTER int outcnt, bits, bit; |
|
97 | 371 |
OBJ w = _INST(width); |
1 | 372 |
|
97 | 373 |
if (__bothSmallInteger(srcStart, dstStart) |
374 |
&& __bothSmallInteger(w, mX) |
|
35 | 375 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
81 | 376 |
_mag = _intVal(mX); |
377 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
378 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
97 | 379 |
_pixels = _intVal(w); |
1 | 380 |
|
81 | 381 |
switch (_mag) { |
382 |
case 1: |
|
383 |
break; |
|
1 | 384 |
|
81 | 385 |
case 2: |
386 |
_byte = *srcP++; |
|
387 |
while (_pixels) { |
|
388 |
bit = (_byte >> 4) & 0xF; |
|
389 |
bits = (bit << 4) | bit; |
|
390 |
*dstP++ = bits; |
|
1 | 391 |
|
81 | 392 |
if (--_pixels) { |
393 |
bit = _byte & 0xF; |
|
394 |
bits = (bit << 4) | bit; |
|
395 |
*dstP++ = bits; |
|
396 |
_byte = *srcP++; |
|
397 |
_pixels--; |
|
398 |
} |
|
399 |
} |
|
400 |
break; |
|
1 | 401 |
|
81 | 402 |
default: |
403 |
bits = 0, outcnt = 0; |
|
404 |
_byte = *srcP++; |
|
405 |
while (_pixels) { |
|
406 |
bit = (_byte >> 4) & 0xF; |
|
1 | 407 |
|
81 | 408 |
for (i=_mag; i>0; i--) { |
409 |
bits = (bits << 4) | bit; |
|
410 |
outcnt++; |
|
411 |
if (outcnt == 2) { |
|
412 |
*dstP++ = bits; |
|
413 |
bits = 0; |
|
414 |
outcnt = 0; |
|
415 |
} |
|
416 |
} |
|
1 | 417 |
|
81 | 418 |
if (--_pixels) { |
419 |
bit = _byte & 0xF; |
|
420 |
for (i=_mag; i>0; i--) { |
|
421 |
bits = (bits << 4) | bit; |
|
422 |
outcnt++; |
|
423 |
if (outcnt == 2) { |
|
424 |
*dstP++ = bits; |
|
425 |
bits = 0; |
|
426 |
outcnt = 0; |
|
427 |
} |
|
428 |
} |
|
429 |
_byte = *srcP++; |
|
430 |
_pixels--; |
|
431 |
} |
|
432 |
} |
|
1 | 433 |
|
81 | 434 |
break; |
435 |
} |
|
436 |
RETURN (self); |
|
1 | 437 |
} |
438 |
%} |
|
439 |
. |
|
440 |
self primitiveFailed |
|
441 |
! ! |
|
280 | 442 |
|
443 |
!Depth4Image methodsFor:'queries'! |
|
444 |
||
445 |
bitsPerPixel |
|
446 |
"return the number of bits per pixel" |
|
447 |
||
448 |
^ 4 |
|
449 |
! |
|
450 |
||
451 |
bitsPerRow |
|
452 |
"return the number of bits in one scanline of the image" |
|
453 |
||
454 |
^ width * 4 |
|
455 |
! |
|
456 |
||
457 |
bytesPerRow |
|
458 |
"return the number of bytes in one scanline of the image" |
|
459 |
||
460 |
|nbytes| |
|
461 |
||
462 |
nbytes := width // 2. |
|
463 |
width odd ifTrue:[ |
|
464 |
^ nbytes + 1 |
|
465 |
]. |
|
466 |
^ nbytes |
|
467 |
! |
|
468 |
||
469 |
usedValues |
|
470 |
"return a collection of color values used in the receiver." |
|
471 |
||
472 |
|useFlags usedValues| |
|
473 |
||
474 |
useFlags := Array new:16 withAll:false. |
|
475 |
width even ifFalse:[ |
|
798 | 476 |
0 to:self height - 1 do:[:y | |
477 |
self valuesAtY:y from:0 to:self width - 1 do:[:x :pixel | |
|
478 |
useFlags at:(pixel + 1) put:true |
|
479 |
] |
|
480 |
]. |
|
280 | 481 |
] ifTrue:[ |
798 | 482 |
bytes usedValues do:[:byte | |
483 |
useFlags at:(byte bitShift:-4)+1 put:true. |
|
484 |
useFlags at:(byte bitAnd:2r1111)+1 put:true. |
|
485 |
]. |
|
280 | 486 |
]. |
487 |
usedValues := OrderedCollection new. |
|
488 |
1 to:16 do:[:i | (useFlags at:i) ifTrue:[usedValues add:(i-1)]]. |
|
489 |
^ usedValues |
|
798 | 490 |
|
491 |
"Modified: 7.6.1996 / 19:09:14 / cg" |
|
280 | 492 |
! ! |
493 |
||
494 |
!Depth4Image class methodsFor:'documentation'! |
|
495 |
||
496 |
version |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
497 |
^ '$Header: /cvs/stx/stx/libview/Depth4Image.st,v 1.19 1996-06-08 11:03:32 cg Exp $' |
280 | 498 |
! ! |