author | Claus Gittinger <cg@exept.de> |
Sat, 08 Jun 1996 13:09:57 +0200 | |
changeset 805 | 5573c2078b73 |
parent 798 | 31ed4a1d4b4a |
child 807 | c1b1e1d029fe |
permissions | -rw-r--r-- |
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:#Depth24Image |
|
579 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images' |
|
1 | 18 |
! |
19 |
||
46 | 20 |
!Depth24Image 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 true-color (24 bit / pixel) images. |
|
39 |
It mainly consists of methods already implemented in Image, |
|
40 |
reimplemented here for more performance. |
|
611 | 41 |
|
798 | 42 |
Only the #rgb format is supported here. |
43 |
||
611 | 44 |
[author:] |
45 |
Claus Gittinger |
|
46 |
||
47 |
[see also:] |
|
48 |
Depth1Image Depth2Image Depth4Image Depth8Image Depth16Image |
|
49 |
ImageReader |
|
46 | 50 |
" |
51 |
! ! |
|
3 | 52 |
|
89 | 53 |
!Depth24Image class methodsFor:'queries'! |
54 |
||
55 |
imageDepth |
|
579 | 56 |
"return the depth of images represented by instances of |
57 |
this class - here we return 24" |
|
58 |
||
89 | 59 |
^ 24 |
579 | 60 |
|
61 |
"Modified: 20.4.1996 / 23:39:50 / cg" |
|
89 | 62 |
! ! |
63 |
||
81 | 64 |
!Depth24Image methodsFor:'accessing'! |
1 | 65 |
|
66 |
atX:x y:y |
|
67 |
"retrieve a pixel at x/y; return a color. |
|
68 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
69 |
x = width-1, y=height-1 for lower right pixel" |
|
70 |
||
38 | 71 |
|index "{ Class: SmallInteger }" |
72 |
rVal gVal bVal| |
|
1 | 73 |
|
74 |
index := 1 + (((width * y) + x) * 3). |
|
75 |
rVal := bytes at:(index). |
|
76 |
gVal := bytes at:(index + 1). |
|
77 |
bVal := bytes at:(index + 2). |
|
78 |
||
79 |
photometric ~~ #rgb ifTrue:[ |
|
805 | 80 |
^ super atX:x y:y |
1 | 81 |
]. |
82 |
^ Color red:rVal * 100 / 255 |
|
805 | 83 |
green:gVal * 100 / 255 |
84 |
blue:bVal * 100 / 255 |
|
85 |
||
86 |
"Modified: 8.6.1996 / 10:45:05 / cg" |
|
1 | 87 |
! |
88 |
||
89 | 89 |
atX:x y:y put:aColor |
90 |
"set the pixel at x/y to aColor. |
|
91 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
92 |
x = width-1, y=height-1 for lower right pixel." |
|
93 |
||
94 |
|index "{ Class: SmallInteger }" |
|
95 |
val "{ Class: SmallInteger }" | |
|
96 |
||
97 |
index := 1 + (((width * y) + x) * 3). |
|
98 |
val := (aColor red / 100 * 255) rounded. |
|
99 |
bytes at:(index) put:val. |
|
100 |
val := (aColor green / 100 * 255) rounded. |
|
101 |
bytes at:(index + 1) put:val. |
|
102 |
val := (aColor blue / 100 * 255) rounded. |
|
103 |
bytes at:(index + 2) put:val. |
|
104 |
! |
|
105 |
||
1 | 106 |
atX:x y:y putValue:aPixelValue |
107 |
"set a pixel at x/y to aPixelValue, which is 24 bits RGB. |
|
108 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
109 |
x = width-1, y=height-1 for lower right pixel" |
|
110 |
||
111 |
|index "{ Class: SmallInteger }" |
|
112 |
val "{ Class: SmallInteger }" | |
|
113 |
||
114 |
index := 1 + (((width * y) + x) * 3). |
|
115 |
val := aPixelValue. |
|
116 |
bytes at:(index + 2) put:(val bitAnd:16rFF). |
|
117 |
val := val bitShift:-8. |
|
118 |
bytes at:(index + 1) put:(val bitAnd:16rFF). |
|
119 |
val := val bitShift:-8. |
|
120 |
bytes at:(index) put:val. |
|
121 |
! |
|
122 |
||
282 | 123 |
valueAtX:x y:y |
124 |
"retrieve a pixel at x/y; return a color. |
|
125 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
126 |
x = width-1, y=height-1 for lower right pixel" |
|
1 | 127 |
|
282 | 128 |
|index "{ Class: SmallInteger }" |
129 |
rVal "{ Class: SmallInteger }" |
|
130 |
gVal "{ Class: SmallInteger }" |
|
131 |
bVal "{ Class: SmallInteger }"| |
|
1 | 132 |
|
282 | 133 |
index := 1 + (((width * y) + x) * 3). |
134 |
rVal := bytes at:(index). |
|
135 |
gVal := bytes at:(index + 1). |
|
136 |
bVal := bytes at:(index + 2). |
|
137 |
^ (((rVal bitShift:8) bitOr:gVal) bitShift:8) bitOr:bVal |
|
1 | 138 |
! ! |
139 |
||
140 |
!Depth24Image methodsFor:'converting rgb images'! |
|
141 |
||
798 | 142 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
143 |
"return the dithered monochrome bits for the receiver image; |
|
144 |
with a constant ditherMatrix, this can be used for thresholding. |
|
145 |
Redefined to make use of knowing that pixels are 24-bit values." |
|
146 |
||
147 |
|f dH nDither |
|
148 |
greyMap monoBits |
|
149 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
150 |
bytesPerRow "{Class: SmallInteger }" |
|
151 |
w "{Class: SmallInteger }" |
|
152 |
h "{Class: SmallInteger }"| |
|
153 |
||
154 |
nDither := ditherMatrix size. |
|
155 |
dH := nDither / dW. |
|
156 |
||
157 |
w := width. |
|
158 |
h := height. |
|
159 |
||
160 |
bytesPerRow := self bytesPerRow. |
|
161 |
||
162 |
bytesPerMonoRow := w + 7 // 8. |
|
163 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
164 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
165 |
^ nil |
|
166 |
]. |
|
167 |
||
168 |
greyMap := ByteArray new:256. |
|
169 |
photometric ~~ #rgb ifTrue:[ |
|
170 |
self error:'invalid format'. |
|
171 |
^ nil |
|
172 |
]. |
|
173 |
||
174 |
%{ |
|
175 |
int __dW = __intVal(dW); |
|
176 |
int __dH = __intVal(dH); |
|
177 |
int __byte; |
|
178 |
int __nDither = __intVal(nDither); |
|
179 |
int __dT; |
|
180 |
int __dstIdx = 0; |
|
181 |
int __srcIdx = 0; |
|
182 |
int __bitCnt; |
|
183 |
int __grey; |
|
184 |
int __w = __intVal(w); |
|
185 |
int __h = __intVal(h); |
|
186 |
int __x; |
|
187 |
int __y; |
|
188 |
int __oX, __oY, __dY; |
|
189 |
int __nextDst; |
|
190 |
int __nextSrc; |
|
191 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
192 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
193 |
||
194 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
195 |
char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
196 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
197 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
198 |
||
199 |
__oY = __dY = 0; |
|
200 |
for (__y=0; __y<__h; __y++) { |
|
201 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
202 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
203 |
||
204 |
__byte = 0; |
|
205 |
__bitCnt = 8; |
|
206 |
||
207 |
__oX = 0; |
|
208 |
||
209 |
for (__x=0; __x<__w; __x++) { |
|
210 |
__grey = (__bytes[__srcIdx] * 3) /* 0.3*r + 0.6*g + b -> 0..2550 */ |
|
211 |
+ (__bytes[__srcIdx+1] * 6) |
|
212 |
+ __bytes[__srcIdx+2]; |
|
213 |
__grey = __grey * (__nDither+1) / 2550; /* 0 .. nDither+1 */ |
|
214 |
__srcIdx += 3; |
|
215 |
||
216 |
__dT = __ditherMatrix[__dY + __oX]; |
|
217 |
||
218 |
__oX++; |
|
219 |
if (__oX == __dW) __oX = 0; |
|
220 |
||
221 |
__byte = __byte << 1; |
|
222 |
if (__grey > __dT) { |
|
223 |
__byte = __byte | 1; /* white */ |
|
224 |
} |
|
225 |
||
226 |
__bitCnt--; |
|
227 |
if (__bitCnt == 0) { |
|
228 |
__monoBits[__dstIdx] = __byte; |
|
229 |
__dstIdx++; |
|
230 |
__byte = 0; |
|
231 |
__bitCnt = 8; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
if (__bitCnt != 8) { |
|
236 |
__byte = __byte << __bitCnt; |
|
237 |
__monoBits[__dstIdx] = __byte; |
|
238 |
} |
|
239 |
||
240 |
__oY++; __dY += __dW; |
|
241 |
if (__oY == __dH) { |
|
242 |
__oY = 0; |
|
243 |
__dY = 0; |
|
244 |
} |
|
245 |
||
246 |
__srcIdx = __nextSrc; |
|
247 |
__dstIdx = __nextDst; |
|
248 |
} |
|
249 |
%}. |
|
250 |
||
251 |
^ monoBits |
|
252 |
||
253 |
" |
|
254 |
|i f| |
|
255 |
||
256 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
257 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
258 |
" |
|
259 |
||
260 |
"Created: 7.6.1996 / 10:48:06 / cg" |
|
261 |
"Modified: 7.6.1996 / 11:08:50 / cg" |
|
262 |
! |
|
263 |
||
1 | 264 |
rgbImageAs2PlaneFormOn:aDevice |
265 |
"return a 2-bit device form for aDevice from the rgb picture, |
|
266 |
using a threshold algorithm. |
|
267 |
(i.e. grey value < 0.25 -> black // 0.25..0.5 -> darkgrey // |
|
268 |
0.5 .. 0.75 -> lightgrey // > 0.75 -> white)." |
|
269 |
||
270 |
|twoPlaneBits f |
|
271 |
map rMap gMap bMap |
|
89 | 272 |
failed |
1 | 273 |
r "{ Class: SmallInteger }" |
274 |
g "{ Class: SmallInteger }" |
|
275 |
b "{ Class: SmallInteger }" |
|
276 |
v "{ Class: SmallInteger }" |
|
277 |
w "{ Class: SmallInteger }" |
|
278 |
h "{ Class: SmallInteger }" |
|
279 |
srcIndex "{ Class: SmallInteger }" |
|
280 |
dstIndex "{ Class: SmallInteger }" |
|
281 |
bits "{ Class: SmallInteger }" |
|
282 |
bitCount "{ Class: SmallInteger }" | |
|
283 |
||
284 |
w := width. |
|
285 |
h := height. |
|
286 |
twoPlaneBits := ByteArray uninitializedNew:(((w * 2 + 7) // 8) * h). |
|
287 |
||
89 | 288 |
failed := true. |
1 | 289 |
%{ |
290 |
register unsigned char *srcPtr, *dstPtr; |
|
291 |
register _v, _bits, _bitCount; |
|
292 |
register j; |
|
293 |
register i; |
|
294 |
||
134 | 295 |
if ((__Class(_INST(bytes)) == ByteArray) |
296 |
&& (__Class(twoPlaneBits) == ByteArray)) { |
|
89 | 297 |
failed = false; |
81 | 298 |
srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
299 |
dstPtr = _ByteArrayInstPtr(twoPlaneBits)->ba_element; |
|
300 |
for (i=_intVal(h); i>0; i--) { |
|
301 |
_bitCount = 0; |
|
302 |
_bits = 0; |
|
303 |
for (j=_intVal(w); j>0; j--) { |
|
304 |
_v = (*srcPtr++ * 3); /* 0.3*r + 0.6*g + b */ |
|
305 |
_v += (*srcPtr++ * 6); |
|
306 |
_v += *srcPtr++; |
|
307 |
_v /= 10; |
|
308 |
_bits <<= 2; |
|
309 |
_bits |= (_v >> 6); /* take top 2 bits */ |
|
310 |
_bitCount++; |
|
311 |
if (_bitCount == 4) { |
|
312 |
*dstPtr++ = _bits; |
|
313 |
_bits = 0; |
|
314 |
_bitCount = 0; |
|
315 |
} |
|
316 |
} |
|
317 |
if (_bitCount != 0) { |
|
318 |
while (_bitCount++ != 4) _bits <<= 2; |
|
319 |
*dstPtr++ = _bits; |
|
320 |
} |
|
321 |
} |
|
1 | 322 |
} |
89 | 323 |
%}. |
324 |
failed ifTrue:[ |
|
325 |
"/ the above is equivalent to: |
|
326 |
"/ |
|
327 |
"/ srcIndex := 1. |
|
328 |
"/ dstIndex := 1. |
|
329 |
"/ 1 to:h do:[:row | |
|
330 |
"/ bitCount := 0. |
|
331 |
"/ bits := 0. |
|
332 |
"/ 1 to:w do:[:col | |
|
333 |
"/ r := bytes at:srcIndex. |
|
334 |
"/ srcIndex := srcIndex + 1. |
|
335 |
"/ g := bytes at:srcIndex. |
|
336 |
"/ srcIndex := srcIndex + 1. |
|
337 |
"/ b := bytes at:srcIndex. |
|
338 |
"/ srcIndex := srcIndex + 1. |
|
339 |
"/ v := ((3 * r) + (6 * g) + (1 * b)) // 10. |
|
340 |
"/ v := v bitShift:-6. "take 2 hi bits" |
|
341 |
"/ bits := (bits bitShift:2) bitOr:v. |
|
342 |
"/ bitCount := bitCount + 1. |
|
343 |
"/ (bitCount == 4) ifTrue:[ |
|
344 |
"/ twoPlaneBits at:dstIndex put:bits. |
|
345 |
"/ dstIndex := dstIndex + 1. |
|
346 |
"/ bits := 0. |
|
347 |
"/ bitCount := 0 |
|
348 |
"/ ] |
|
349 |
"/ ]. |
|
350 |
"/ (bitCount ~~ 0) ifTrue:[ |
|
351 |
"/ [bitCount == 4] whileFalse:[ |
|
352 |
"/ bitCount := bitCount + 1. |
|
353 |
"/ bits := bits bitShift:2. |
|
354 |
"/ ]. |
|
355 |
"/ twoPlaneBits at:dstIndex put:bits. |
|
356 |
"/ dstIndex := dstIndex + 1 |
|
357 |
"/ ] |
|
358 |
"/ ] |
|
359 |
self primitiveFailed. |
|
360 |
^ nil |
|
1 | 361 |
]. |
362 |
||
363 |
f := Form width:width height:height depth:2 on:aDevice. |
|
364 |
f isNil ifTrue:[^ nil]. |
|
365 |
f initGC. |
|
366 |
(aDevice blackpixel == 0) ifFalse:[ |
|
81 | 367 |
"have to invert bits" |
368 |
f function:#copyInverted |
|
1 | 369 |
]. |
370 |
aDevice drawBits:twoPlaneBits depth:2 width:width height:height |
|
81 | 371 |
x:0 y:0 |
372 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
1 | 373 |
^ f |
374 |
! |
|
375 |
||
376 |
rgbImageAs8BitGreyFormOn:aDevice |
|
377 |
"return an 8-bit greyForm from the rgb picture" |
|
378 |
||
379 |
|greyBits f v |
|
380 |
srcIndex "{ Class: SmallInteger }" |
|
381 |
dstIndex "{ Class: SmallInteger }" |
|
89 | 382 |
failed| |
1 | 383 |
|
384 |
greyBits := ByteArray uninitializedNew:(width * height). |
|
89 | 385 |
failed := false. |
1 | 386 |
%{ |
387 |
register unsigned char *srcPtr, *dstPtr; |
|
388 |
register _v; |
|
389 |
register j; |
|
390 |
register i; |
|
391 |
||
134 | 392 |
if ((__Class(_INST(bytes)) == ByteArray) |
393 |
&& (__Class(greyBits) == ByteArray)) { |
|
89 | 394 |
failed = false; |
81 | 395 |
srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
396 |
dstPtr = _ByteArrayInstPtr(greyBits)->ba_element; |
|
397 |
for (i=_intVal(_INST(height)); i>0; i--) { |
|
398 |
for (j=_intVal(_INST(width)); j>0; j--) { |
|
399 |
_v = (*srcPtr * 3); /* 0.3*r + 0.6*g + b */ |
|
400 |
_v += (*srcPtr++ * 6); |
|
401 |
_v += *srcPtr++; |
|
402 |
_v /= 10; |
|
403 |
*dstPtr++ = _v >> 4 ; |
|
404 |
} |
|
405 |
} |
|
1 | 406 |
} |
407 |
%} |
|
408 |
. |
|
89 | 409 |
failed ifTrue:[ |
410 |
"/ the above is equivalent to: |
|
411 |
"/ |
|
412 |
"/ srcIndex := 1. |
|
413 |
"/ dstIndex := 1. |
|
414 |
"/ |
|
415 |
"/ 1 to:height do:[:h | |
|
416 |
"/ 1 to:width do:[:w | |
|
417 |
"/ |v |
|
418 |
"/ r "{ Class: SmallInteger }" |
|
419 |
"/ g "{ Class: SmallInteger }" |
|
420 |
"/ b "{ Class: SmallInteger }"| |
|
421 |
"/ |
|
422 |
"/ r := bytes at:srcIndex. |
|
423 |
"/ srcIndex := srcIndex + 1. |
|
424 |
"/ g := bytes at:srcIndex. |
|
425 |
"/ srcIndex := srcIndex + 1. |
|
426 |
"/ b := bytes at:srcIndex. |
|
427 |
"/ srcIndex := srcIndex + 1. |
|
428 |
"/ |
|
429 |
"/ v := ((3 * r) + (6 * g) + (1 * b)) // 10. |
|
430 |
"/ v := v bitShift:-4. |
|
431 |
"/ greyBits at:dstIndex put:v. |
|
432 |
"/ dstIndex := dstIndex + 1 |
|
433 |
"/ ] |
|
434 |
"/ ] |
|
435 |
self primitiveFailed. |
|
436 |
^ nil |
|
1 | 437 |
]. |
438 |
||
439 |
f := Form width:width height:height depth:8 on:aDevice. |
|
440 |
f isNil ifTrue:[^ nil]. |
|
441 |
f initGC. |
|
442 |
aDevice drawBits:greyBits depth:8 width:width height:height |
|
81 | 443 |
x:0 y:0 |
444 |
into:(f id) x:0 y:0 |
|
445 |
width:width height:height with:(f gcId). |
|
1 | 446 |
^ f |
447 |
! |
|
448 |
||
282 | 449 |
rgbImageAsDitheredPseudoFormOn:aDevice |
450 |
"return a dithered pseudocolor form from the rgb-picture. |
|
451 |
This method depends on fixColors being allocated (see Color>>getColors*)" |
|
452 |
||
453 |
^ self rgbImageAsDitheredPseudoFormOn:aDevice |
|
454 |
colors:Color fixColors |
|
455 |
nRed:Color numFixRed |
|
456 |
nGreen:Color numFixGreen |
|
457 |
nBlue:Color numFixBlue |
|
458 |
! |
|
459 |
||
460 |
rgbImageAsDitheredPseudoFormOn:aDevice colors:fixColors nRed:nRed nGreen:nGreen nBlue:nBlue |
|
461 |
"return a dithered pseudocolor form from the rgb-picture" |
|
462 |
||
463 |
|pseudoBits f |
|
464 |
h "{ Class: SmallInteger }" |
|
465 |
w "{ Class: SmallInteger }" |
|
466 |
"/ eR "{Class: SmallInteger }" |
|
467 |
"/ eG "{Class: SmallInteger }" |
|
468 |
"/ eB "{Class: SmallInteger }" |
|
469 |
"/ wantR "{Class: SmallInteger }" |
|
470 |
"/ wantG "{Class: SmallInteger }" |
|
471 |
"/ wantB "{Class: SmallInteger }" |
|
472 |
fixR "{Class: SmallInteger }" |
|
473 |
fixG "{Class: SmallInteger }" |
|
474 |
fixB "{Class: SmallInteger }" |
|
475 |
srcIndex "{ Class: SmallInteger }" |
|
476 |
dstIndex "{ Class: SmallInteger }" |
|
477 |
deviceDepth has8BitImage |
|
478 |
fixIds failed| |
|
479 |
||
480 |
aDevice ~~ Display ifTrue:[^ nil]. |
|
481 |
||
482 |
fixR := nRed. |
|
483 |
fixR == 0 ifTrue:[ ^ nil]. |
|
484 |
fixG := nGreen. |
|
485 |
fixG == 0 ifTrue:[ ^ nil]. |
|
486 |
fixB := nBlue. |
|
487 |
fixB == 0 ifTrue:[ ^ nil]. |
|
488 |
"/ simple check |
|
489 |
(fixR * fixG * fixB) ~~ fixColors size ifTrue:[ |
|
490 |
self error:'invalid color array passed'. |
|
491 |
^ nil |
|
492 |
]. |
|
493 |
fixIds := (fixColors asArray collect:[:clr | clr colorId]) asByteArray. |
|
494 |
||
495 |
deviceDepth := aDevice depth. |
|
496 |
deviceDepth == 8 ifTrue:[ |
|
497 |
has8BitImage := true. |
|
498 |
] ifFalse:[ |
|
499 |
has8BitImage := false. |
|
500 |
aDevice supportedImageFormats do:[:fmt | |
|
501 |
(fmt at:#bitsPerPixel) == 8 ifTrue:[ |
|
502 |
has8BitImage := true. |
|
503 |
] |
|
504 |
] |
|
505 |
]. |
|
506 |
has8BitImage ifFalse:[^ nil]. |
|
507 |
||
508 |
'D24IMAGE: dithering ...' infoPrintNL. |
|
509 |
||
510 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
|
511 |
||
512 |
h := height. |
|
513 |
w := width. |
|
514 |
||
515 |
%{ |
|
516 |
int __x, __y; |
|
517 |
int __eR, __eG, __eB; |
|
518 |
int __wantR, __wantG, __wantB; |
|
519 |
unsigned char *srcP, *dstP; |
|
520 |
unsigned char *redP, *greenP, *blueP; |
|
521 |
int pix; |
|
522 |
unsigned char *idP; |
|
523 |
int __fR, __fG, __fB; |
|
524 |
int iR, iG, iB; |
|
525 |
int idx; |
|
526 |
||
527 |
if (__isByteArray(_INST(bytes)) |
|
528 |
&& __isByteArray(pseudoBits) |
|
529 |
&& __isByteArray(fixIds) |
|
530 |
&& __bothSmallInteger(fixR, fixG) |
|
531 |
&& __isSmallInteger(fixB)) { |
|
532 |
failed = false; |
|
533 |
||
534 |
srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
535 |
dstP = _ByteArrayInstPtr(pseudoBits)->ba_element; |
|
536 |
idP = _ByteArrayInstPtr(fixIds)->ba_element; |
|
537 |
__fR = _intVal(fixR)-1; |
|
538 |
__fG = _intVal(fixG)-1; |
|
539 |
__fB = _intVal(fixB)-1; |
|
540 |
||
541 |
for (__y=_intVal(h); __y>0; __y--) { |
|
542 |
__eR = __eG = __eB = 0; |
|
543 |
for (__x=_intVal(w); __x>0; __x--) { |
|
544 |
int t; |
|
545 |
int __want; |
|
546 |
||
547 |
/* |
|
548 |
* wR, wG and wB is the wanted r/g/b value; |
|
549 |
* compute the index into the dId table .. |
|
550 |
* values: 0..255; scale to 0..fR-1, 0..fG-1, 0..fB-1 |
|
551 |
* |
|
552 |
* bad kludge: knows how to index into FixColor table |
|
553 |
*/ |
|
554 |
__wantR = srcP[0] + __eR; |
|
555 |
__wantG = srcP[1] + __eG; |
|
556 |
__wantB = srcP[2] + __eB; |
|
557 |
srcP += 3; |
|
558 |
||
559 |
if (__wantR > 255) __want = 255; |
|
560 |
else if (__wantR < 0) __want = 0; |
|
561 |
else __want = __wantR; |
|
562 |
||
563 |
iR = __want * __fR / 128; |
|
564 |
iR = (iR / 2) + (iR & 1); |
|
565 |
idx = iR * (__fG+1); |
|
566 |
||
567 |
if (__wantG > 255) __want = 255; |
|
568 |
else if (__wantG < 0) __want = 0; |
|
569 |
else __want = __wantG; |
|
570 |
||
571 |
iG = __want * __fG / 128; |
|
572 |
iG = (iG / 2) + (iG & 1); |
|
573 |
idx = (idx + iG) * (__fB+1); |
|
574 |
||
575 |
if (__wantB > 255) __want = 255; |
|
576 |
else if (__wantB < 0) __want = 0; |
|
577 |
else __want = __wantB; |
|
578 |
||
579 |
iB = __want * __fB / 128; |
|
580 |
iB = (iB / 2) + (iB & 1); |
|
581 |
idx = idx + iB; |
|
582 |
||
583 |
/* |
|
584 |
* store the corresponding dither colorId |
|
585 |
*/ |
|
586 |
*dstP++ = idP[idx]; |
|
587 |
||
588 |
/* |
|
589 |
* the new error: |
|
590 |
*/ |
|
591 |
__eR = __wantR - (iR * 256 / __fR); |
|
592 |
__eG = __wantG - (iG * 256 / __fG); |
|
593 |
__eB = __wantB - (iB * 256 / __fB); |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
%}. |
|
598 |
failed ifTrue:[ |
|
599 |
self primitiveFailed. |
|
600 |
^ nil |
|
601 |
||
602 |
"/ for non-C programmers: |
|
603 |
"/ the above code is (roughly) equivalent to: |
|
604 |
"/ srcIndex := 1. |
|
605 |
"/ dstIndex := 1. |
|
606 |
"/ 1 to:h do:[:y | |
|
607 |
"/ eR := eG := eB := 0. |
|
608 |
"/ 1 to:w do:[:x | |
|
609 |
"/ |pixel "{ Class: SmallInteger }" |
|
610 |
"/ clr |
|
611 |
"/ idx "{ Class: SmallInteger }" |
|
612 |
"/ iR "{ Class: SmallInteger }" |
|
613 |
"/ iG "{ Class: SmallInteger }" |
|
614 |
"/ iB "{ Class: SmallInteger }" |
|
615 |
"/ wR "{ Class: SmallInteger }" |
|
616 |
"/ wG "{ Class: SmallInteger }" |
|
617 |
"/ wB "{ Class: SmallInteger }" | |
|
618 |
"/ |
|
619 |
"/ wantR := ((bytes at:srcIndex) + eR). srcIndex := srcIndex + 1. |
|
620 |
"/ wantG := ((bytes at:srcIndex) + eG). srcIndex := srcIndex + 1. |
|
621 |
"/ wantB := ((bytes at:srcIndex) + eB). srcIndex := srcIndex + 1. |
|
622 |
"/ wR := wantR. |
|
623 |
"/ wR > 255 ifTrue:[wR := 255] ifFalse:[wR < 0 ifTrue:[wR := 0]]. |
|
624 |
"/ wG := wantG. |
|
625 |
"/ wG > 255 ifTrue:[wG := 255] ifFalse:[wG < 0 ifTrue:[wG := 0]]. |
|
626 |
"/ wB := wantB. |
|
627 |
"/ wB > 255 ifTrue:[wB := 255] ifFalse:[wB < 0 ifTrue:[wB := 0]]. |
|
628 |
"/ |
|
629 |
"/ iR := wR * (fixR-1) // 128. |
|
630 |
"/ iR := (iR // 2) + (iR bitAnd:1). |
|
631 |
"/ iG := wG * (fixG-1) // 128. |
|
632 |
"/ iG := (iG // 2) + (iG bitAnd:1). |
|
633 |
"/ iB := wB * (fixB-1) // 128. |
|
634 |
"/ iB := (iB // 2) + (iB bitAnd:1). |
|
635 |
"/ idx := (iR * fixR + iG) * fixB + iB + 1. |
|
636 |
"/ |
|
637 |
"/ clr := fixColors at:idx. |
|
638 |
"/ |
|
639 |
"/ eR := wantR - (clr red * 2) asInteger. |
|
640 |
"/ eG := wantG - (clr green * 2) asInteger. |
|
641 |
"/ eB := wantB - (clr blue * 2) asInteger. |
|
642 |
"/ |
|
643 |
"/ pixel := clr colorId. |
|
644 |
"/ pseudoBits at:dstIndex put:pixel. |
|
645 |
"/ |
|
646 |
"/ dstIndex := dstIndex + 1 |
|
647 |
"/ ]. |
|
648 |
]. |
|
649 |
||
650 |
f := Form width:width height:height depth:aDevice depth on:aDevice. |
|
651 |
f isNil ifTrue:[^ nil]. |
|
652 |
f colorMap:fixColors. |
|
653 |
f initGC. |
|
654 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:aDevice depth |
|
655 |
width:width height:height |
|
656 |
x:0 y:0 |
|
657 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
658 |
^ f |
|
659 |
! |
|
660 |
||
661 |
rgbImageAsGreyFormOn:aDevice |
|
662 |
"convert an rgb image to a grey image for greyscale displays" |
|
663 |
||
664 |
|deviceDepth| |
|
665 |
||
666 |
deviceDepth := aDevice depth. |
|
667 |
||
668 |
"I have specially tuned methods for monochrome" |
|
669 |
(deviceDepth == 1) ifTrue:[ |
|
670 |
DitherAlgorithm == #error ifTrue:[ |
|
671 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
672 |
]. |
|
673 |
DitherAlgorithm == #pattern ifTrue:[ |
|
674 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
675 |
]. |
|
676 |
^ self rgbImageAsMonoFormOn:aDevice |
|
677 |
]. |
|
678 |
||
679 |
"and for 2plane greyscale (i.e. NeXTs)" |
|
680 |
(deviceDepth == 2) ifTrue:[ |
|
681 |
DitherAlgorithm == #error ifTrue:[ |
|
682 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
683 |
]. |
|
684 |
DitherAlgorithm == #pattern ifTrue:[ |
|
685 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
686 |
]. |
|
687 |
^ self rgbImageAs2PlaneFormOn:aDevice |
|
688 |
]. |
|
689 |
||
690 |
(deviceDepth == 8) ifTrue:[ |
|
691 |
^ self rgbImageAs8BitGreyFormOn:aDevice |
|
692 |
]. |
|
693 |
||
694 |
"mhmh need another converter ... |
|
695 |
till then we do:" |
|
696 |
DitherAlgorithm == #error ifTrue:[ |
|
697 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
698 |
]. |
|
699 |
DitherAlgorithm == #pattern ifTrue:[ |
|
700 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
701 |
]. |
|
702 |
^ self rgbImageAsMonoFormOn:aDevice |
|
703 |
! |
|
704 |
||
705 |
rgbImageAsMonoFormOn:aDevice |
|
706 |
"return a 1-bit monochrome form for aDevice from the rgb picture, |
|
707 |
using a threshold algorithm. |
|
708 |
(i.e. grey value < 0.5 -> black, grey value >= 0.5 -> white)." |
|
709 |
||
710 |
|monoBits f |
|
711 |
w "{ Class: SmallInteger }" |
|
712 |
h "{ Class: SmallInteger }" |
|
713 |
r "{ Class: SmallInteger }" |
|
714 |
g "{ Class: SmallInteger }" |
|
715 |
b "{ Class: SmallInteger }" |
|
716 |
v "{ Class: SmallInteger }" |
|
717 |
map rMap gMap bMap |
|
718 |
srcIndex "{ Class: SmallInteger }" |
|
719 |
dstIndex "{ Class: SmallInteger }" |
|
720 |
bits "{ Class: SmallInteger }" |
|
721 |
bitCount "{ Class: SmallInteger }" |
|
722 |
failed | |
|
723 |
||
724 |
w := width. |
|
725 |
h := height. |
|
726 |
monoBits := ByteArray uninitializedNew:(((w + 7) // 8) * h). |
|
727 |
failed := true. |
|
728 |
%{ |
|
729 |
register unsigned char *srcPtr, *dstPtr; |
|
730 |
register _v, _bits, _bitCount; |
|
731 |
register j; |
|
732 |
register i; |
|
733 |
||
734 |
if (__isByteArray(_INST(bytes)) |
|
735 |
&& __isByteArray(monoBits)) { |
|
736 |
failed = false; |
|
737 |
srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
738 |
dstPtr = _ByteArrayInstPtr(monoBits)->ba_element; |
|
739 |
for (i=_intVal(h); i>0; i--) { |
|
740 |
_bitCount = 0; |
|
741 |
_bits = 0; |
|
742 |
for (j=_intVal(w); j>0; j--) { |
|
743 |
_bits <<= 1; |
|
744 |
||
745 |
_v = (*srcPtr++ * 3); /* 0.3*r + 0.6*g + b */ |
|
746 |
_v += (*srcPtr++ * 6); |
|
747 |
_v += *srcPtr++; |
|
748 |
_v /= 10; |
|
749 |
if (_v & 0x80) |
|
750 |
_bits |= 1; |
|
751 |
||
752 |
_bitCount++; |
|
753 |
if (_bitCount == 8) { |
|
754 |
*dstPtr++ = _bits; |
|
755 |
_bits = 0; |
|
756 |
_bitCount = 0; |
|
757 |
} |
|
758 |
} |
|
759 |
if (_bitCount != 0) { |
|
760 |
while (_bitCount++ != 8) _bits <<= 1; |
|
761 |
*dstPtr++ = _bits; |
|
762 |
} |
|
763 |
} |
|
764 |
} |
|
765 |
%}. |
|
766 |
||
767 |
failed ifTrue:[ |
|
768 |
"/ the above is equivalent to: |
|
769 |
"/ |
|
770 |
"/ srcIndex := 1. |
|
771 |
"/ dstIndex := 1. |
|
772 |
"/ 1 to:h do:[:row | |
|
773 |
"/ bitCount := 0. |
|
774 |
"/ bits := 0. |
|
775 |
"/ 1 to:w do:[:col | |
|
776 |
"/ bits := bits bitShift:1. |
|
777 |
"/ |
|
778 |
"/ r := bytes at:srcIndex. |
|
779 |
"/ srcIndex := srcIndex + 1. |
|
780 |
"/ g := bytes at:srcIndex. |
|
781 |
"/ srcIndex := srcIndex + 1. |
|
782 |
"/ b := bytes at:srcIndex. |
|
783 |
"/ srcIndex := srcIndex + 1. |
|
784 |
"/ v := ((3 * r) + (6 * g) + (1 * b)) // 10. |
|
785 |
"/ ((v bitAnd:16r80) == 0) ifFalse:[ |
|
786 |
"/ bits := bits bitOr:1 |
|
787 |
"/ ]. |
|
788 |
"/ bitCount := bitCount + 1. |
|
789 |
"/ (bitCount == 8) ifTrue:[ |
|
790 |
"/ monoBits at:dstIndex put:bits. |
|
791 |
"/ dstIndex := dstIndex + 1. |
|
792 |
"/ bits := 0. |
|
793 |
"/ bitCount := 0 |
|
794 |
"/ ] |
|
795 |
"/ ]. |
|
796 |
"/ (bitCount ~~ 0) ifTrue:[ |
|
797 |
"/ [bitCount == 8] whileFalse:[ |
|
798 |
"/ bitCount := bitCount + 1. |
|
799 |
"/ bits := bits bitShift:1. |
|
800 |
"/ ]. |
|
801 |
"/ monoBits at:dstIndex put:bits. |
|
802 |
"/ dstIndex := dstIndex + 1 |
|
803 |
"/ ] |
|
804 |
"/ ] |
|
805 |
self primitiveFailed. |
|
806 |
^ nil |
|
807 |
]. |
|
808 |
||
809 |
f := Form width:w height:h depth:1 on:aDevice. |
|
810 |
f isNil ifTrue:[^ nil]. |
|
811 |
f initGC. |
|
812 |
(aDevice blackpixel == 0) ifFalse:[ |
|
813 |
"have to invert bits" |
|
814 |
f function:#copyInverted |
|
815 |
]. |
|
816 |
aDevice drawBits:monoBits depth:1 width:w height:h |
|
817 |
x:0 y:0 |
|
818 |
into:(f id) x:0 y:0 width:w height:h with:(f gcId). |
|
819 |
^ f |
|
820 |
! |
|
821 |
||
1 | 822 |
rgbImageAsPatternDitheredGreyFormOn:aDevice |
823 |
"return a dithered greyForm for aDevice from the palette picture. |
|
824 |
works for any destination depth. |
|
825 |
A slow algorithm, using draw into the form (which indirectly does |
|
826 |
the dither) - should be rewritten." |
|
827 |
||
828 |
|f depth |
|
829 |
nDither "{Class: SmallInteger }" |
|
830 |
nColors "{Class: SmallInteger }" |
|
831 |
v "{Class: SmallInteger }" |
|
832 |
h "{Class: SmallInteger }" |
|
833 |
w "{Class: SmallInteger }" |
|
834 |
srcIndex "{Class: SmallInteger }" |
|
835 |
dstIndex "{Class: SmallInteger }" |
|
836 |
mask "{Class: SmallInteger }" |
|
837 |
outBits "{Class: SmallInteger }" |
|
838 |
outCount "{Class: SmallInteger }" |
|
839 |
patternOffset "{Class: SmallInteger }" |
|
840 |
patternBits "{Class: SmallInteger }" |
|
841 |
run "{Class: SmallInteger }" |
|
842 |
r "{Class: SmallInteger }" |
|
843 |
g "{Class: SmallInteger }" |
|
844 |
b "{Class: SmallInteger }" |
|
845 |
index "{Class: SmallInteger }" |
|
846 |
p0 "{Class: SmallInteger }" |
|
847 |
p1 "{Class: SmallInteger }" |
|
848 |
map last clr |
|
849 |
patterns formBytes patternBytes |
|
850 |
pixel0bytes pixel1bytes ditherPattern |
|
851 |
ditherColors first delta| |
|
852 |
||
694 | 853 |
Transcript showCR:'dithering ..'. Transcript endEntry. |
1 | 854 |
|
855 |
h := height. |
|
856 |
w := width. |
|
857 |
||
858 |
nDither := NumberOfDitherColors. |
|
859 |
ditherColors := Array new:nDither. |
|
860 |
||
861 |
first := (100 / nDither / 2). |
|
862 |
delta := 100 / nDither. |
|
863 |
0 to:nDither-1 do:[:i | |
|
746 | 864 |
ditherColors at:i+1 put:(Color gray:(i * delta + first)). |
1 | 865 |
]. |
866 |
||
867 |
nColors := 256. |
|
868 |
map := Array new:256. |
|
869 |
1 to:256 do:[:i | |
|
694 | 870 |
v := i - 1. |
871 |
" v is now in the range 0 .. 255 " |
|
872 |
v := (v * (nDither - 1) // 255) rounded. |
|
873 |
" v is now 0 .. nDither-1 " |
|
874 |
map at:i put:(ditherColors at:(v + 1)) |
|
1 | 875 |
]. |
876 |
||
877 |
"tuning - code below is so slooow" |
|
878 |
||
879 |
"get the patterns, fill form bytes here" |
|
880 |
||
881 |
w := width. |
|
882 |
h := height. |
|
883 |
||
884 |
depth := aDevice depth. |
|
885 |
depth == 1 ifTrue:[ |
|
694 | 886 |
formBytes := ByteArray uninitializedNew:(w + 7 // 8) * h. |
887 |
patterns := Array new:nColors. |
|
888 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
889 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1 | 890 |
|
694 | 891 |
"extract dither patterns and values to use for 1/0 bits |
892 |
in those from the dithercolors" |
|
1 | 893 |
|
694 | 894 |
1 to:nColors do:[:i | |
895 |
clr := (map at:i) on:aDevice. |
|
896 |
ditherPattern := clr ditherForm. |
|
1 | 897 |
|
694 | 898 |
ditherPattern isNil ifTrue:[ |
899 |
patterns at:i put:#[2r11111111 |
|
900 |
2r11111111 |
|
901 |
2r11111111 |
|
902 |
2r11111111 |
|
903 |
2r11111111 |
|
904 |
2r11111111 |
|
905 |
2r11111111 |
|
906 |
2r11111111]. |
|
907 |
pixel0bytes at:i put:clr colorId. |
|
908 |
pixel1bytes at:i put:clr colorId |
|
909 |
] ifFalse:[ |
|
910 |
patterns at:i put:(ditherPattern bits). |
|
911 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
912 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
913 |
]. |
|
914 |
]. |
|
1 | 915 |
|
694 | 916 |
srcIndex := 1. |
917 |
dstIndex := 1. |
|
918 |
mask := 16r80. |
|
919 |
outBits := 0. |
|
920 |
patternOffset := 1. |
|
921 |
1 to:h do:[:dstY | |
|
922 |
last := nil. |
|
923 |
1 to:w do:[:dstX | |
|
924 |
r := bytes at:srcIndex. |
|
925 |
g := bytes at:(srcIndex + 1). |
|
926 |
b := bytes at:(srcIndex + 2). |
|
927 |
srcIndex := srcIndex + 3. |
|
1 | 928 |
|
694 | 929 |
v := ((3 * r) + (6 * g) + (1 * b)). "pixel grey value (*10)" |
930 |
v == last ifFalse:[ |
|
931 |
index := v // 10 + 1. "index into map" |
|
1 | 932 |
|
694 | 933 |
patternBytes := patterns at:index. "dither pattern for color" |
934 |
patternBits := patternBytes at:patternOffset. "dither row" |
|
935 |
p0 := pixel0bytes at:index. "value for 0-dither bit" |
|
936 |
p1 := pixel1bytes at:index. "value for 1-dither bit" |
|
937 |
last := v. |
|
938 |
]. |
|
939 |
outBits := outBits bitShift:1. |
|
1 | 940 |
|
694 | 941 |
(patternBits bitAnd:mask) == 0 ifTrue:[ |
942 |
outBits := outBits bitOr:p0. |
|
943 |
] ifFalse:[ |
|
944 |
outBits := outBits bitOr:p1 |
|
945 |
]. |
|
946 |
mask := mask bitShift:-1. |
|
947 |
mask == 0 ifTrue:[ |
|
948 |
mask := 16r80. |
|
949 |
formBytes at:dstIndex put:outBits. |
|
950 |
dstIndex := dstIndex + 1. |
|
951 |
outBits := 0 |
|
952 |
] |
|
953 |
]. |
|
954 |
mask == 16r80 ifFalse:[ |
|
955 |
[mask == 0] whileFalse:[ |
|
956 |
mask := mask bitShift:-1. |
|
957 |
outBits := outBits bitShift:1. |
|
958 |
]. |
|
959 |
formBytes at:dstIndex put:outBits. |
|
960 |
dstIndex := dstIndex + 1. |
|
961 |
mask := 16r80. |
|
962 |
outBits := 0 |
|
963 |
]. |
|
964 |
patternOffset := patternOffset + 1. |
|
965 |
patternOffset == 9 ifTrue:[ |
|
966 |
patternOffset := 1 |
|
967 |
] |
|
968 |
]. |
|
969 |
f := Form width:w height:h fromArray:formBytes. |
|
970 |
^ f |
|
1 | 971 |
]. |
972 |
||
973 |
depth == 2 ifTrue:[ |
|
694 | 974 |
formBytes := ByteArray uninitializedNew:(w * 2 + 7 // 8) * h. |
975 |
patterns := Array new:nColors. |
|
976 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
977 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1 | 978 |
|
694 | 979 |
"extract dither patterns and values to use for 1/0 bits |
980 |
in those from the dithercolors" |
|
1 | 981 |
|
694 | 982 |
1 to:nColors do:[:i | |
983 |
clr := (map at:i) on:aDevice. |
|
984 |
ditherPattern := clr ditherForm. |
|
1 | 985 |
|
694 | 986 |
ditherPattern isNil ifTrue:[ |
987 |
patterns at:i put:#[2r11111111 |
|
988 |
2r11111111 |
|
989 |
2r11111111 |
|
990 |
2r11111111 |
|
991 |
2r11111111 |
|
992 |
2r11111111 |
|
993 |
2r11111111 |
|
994 |
2r11111111]. |
|
995 |
pixel0bytes at:i put:clr colorId. |
|
996 |
pixel1bytes at:i put:clr colorId |
|
997 |
] ifFalse:[ |
|
998 |
patterns at:i put:(ditherPattern bits). |
|
999 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
1000 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
1001 |
]. |
|
1002 |
]. |
|
1 | 1003 |
|
694 | 1004 |
srcIndex := 1. |
1005 |
dstIndex := 1. |
|
1006 |
mask := 16r80. |
|
1007 |
outBits := 0. |
|
1008 |
patternOffset := 1. |
|
1009 |
1 to:h do:[:dstY | |
|
1010 |
last := nil. |
|
1011 |
outCount := 0. |
|
1012 |
mask := 16r80. |
|
1013 |
outBits := 0. |
|
1014 |
1 to:w do:[:dstX | |
|
1015 |
r := bytes at:srcIndex. |
|
1016 |
srcIndex := srcIndex + 1. |
|
1017 |
g := bytes at:srcIndex. |
|
1018 |
srcIndex := srcIndex + 1. |
|
1019 |
b := bytes at:srcIndex. |
|
1020 |
srcIndex := srcIndex + 1. |
|
1 | 1021 |
|
694 | 1022 |
v := ((3 * r) + (6 * g) + (1 * b)). "pixel grey value (*10)" |
1023 |
v == last ifFalse:[ |
|
1024 |
index := v // 10 + 1. "index into map" |
|
1 | 1025 |
|
694 | 1026 |
patternBytes := patterns at:index. "dither pattern for color" |
1027 |
patternBits := patternBytes at:patternOffset. "dither row" |
|
1028 |
p0 := pixel0bytes at:index. "value for 0-dither bit" |
|
1029 |
p1 := pixel1bytes at:index. "value for 1-dither bit" |
|
1030 |
last := v. |
|
1031 |
]. |
|
1032 |
outBits := outBits bitShift:2. |
|
1 | 1033 |
|
694 | 1034 |
(patternBits bitAnd:mask) == 0 ifTrue:[ |
1035 |
outBits := outBits bitOr:p0. |
|
1036 |
] ifFalse:[ |
|
1037 |
outBits := outBits bitOr:p1 |
|
1038 |
]. |
|
1039 |
mask := mask bitShift:-1. |
|
1040 |
outCount := outCount + 1. |
|
1041 |
outCount == 4 ifTrue:[ |
|
1042 |
formBytes at:dstIndex put:outBits. |
|
1043 |
dstIndex := dstIndex + 1. |
|
1044 |
outBits := 0. |
|
1045 |
outCount := 0. |
|
1046 |
mask == 0 ifTrue:[ |
|
1047 |
mask := 16r80. |
|
1048 |
] |
|
1049 |
] |
|
1050 |
]. |
|
1051 |
(outCount == 0) ifFalse:[ |
|
1052 |
[outCount == 4] whileFalse:[ |
|
1053 |
outCount := outCount + 1. |
|
1054 |
outBits := outBits bitShift:2. |
|
1055 |
]. |
|
1056 |
formBytes at:dstIndex put:outBits. |
|
1057 |
dstIndex := dstIndex + 1. |
|
1058 |
]. |
|
1059 |
patternOffset := patternOffset + 1. |
|
1060 |
patternOffset == 9 ifTrue:[ |
|
1061 |
patternOffset := 1 |
|
1062 |
] |
|
1063 |
]. |
|
1064 |
f := Form width:w height:h depth:depth. |
|
1065 |
f initGC. |
|
1066 |
f device drawBits:formBytes depth:2 |
|
1067 |
width:w height:h x:0 y:0 |
|
1068 |
into:f id x:0 y:0 width:w height:h with:f gcId. |
|
1069 |
^ f |
|
1 | 1070 |
]. |
1071 |
||
1072 |
"draw each pixel using dither color (let others do the dithering) |
|
1073 |
although the code is simple, its very slow" |
|
1074 |
||
1075 |
f := Form width:w height:h depth:(aDevice depth) on:aDevice. |
|
1076 |
f isNil ifTrue:[^ nil]. |
|
1077 |
f initGC. |
|
1078 |
"draw each pixel using dither color" |
|
1079 |
||
1080 |
srcIndex := 1. |
|
1081 |
0 to:h-1 do:[:dstY | |
|
694 | 1082 |
run := 0. |
1083 |
last := nil. |
|
1084 |
0 to:w-1 do:[:dstX | |
|
1085 |
r := bytes at:srcIndex. |
|
1086 |
srcIndex := srcIndex + 1. |
|
1087 |
g := bytes at:srcIndex. |
|
1088 |
srcIndex := srcIndex + 1. |
|
1089 |
b := bytes at:srcIndex. |
|
1090 |
srcIndex := srcIndex + 1. |
|
1 | 1091 |
|
694 | 1092 |
v := ((3 * r) + (6 * g) + (1 * b)) // 10. |
1 | 1093 |
|
694 | 1094 |
clr := map at:(v + 1). |
1 | 1095 |
|
694 | 1096 |
clr == last ifTrue:[ |
1097 |
run := run + 1 |
|
1098 |
] ifFalse:[ |
|
1099 |
(run ~~ 0) ifTrue:[ |
|
1100 |
f fillRectangleX:dstX-run y:dstY width:run height:1. |
|
1101 |
]. |
|
1102 |
run := 1. |
|
1103 |
f paint:clr. |
|
1104 |
last := clr |
|
1105 |
]. |
|
1106 |
]. |
|
1107 |
f fillRectangleX:width-run y:dstY width:run height:1. |
|
1 | 1108 |
]. |
1109 |
^ f |
|
694 | 1110 |
|
746 | 1111 |
"Modified: 28.5.1996 / 20:54:06 / cg" |
89 | 1112 |
! |
1 | 1113 |
|
1114 |
rgbImageAsPseudoFormOn:aDevice |
|
1115 |
"return a pseudocolor form from the rgb-picture" |
|
1116 |
||
1117 |
|pseudoBits f |
|
1118 |
r "{ Class: SmallInteger }" |
|
1119 |
g "{ Class: SmallInteger }" |
|
1120 |
b "{ Class: SmallInteger }" |
|
1121 |
srcIndex "{ Class: SmallInteger }" |
|
1122 |
dstIndex "{ Class: SmallInteger }" |
|
1123 |
rMask "{ Class: SmallInteger }" |
|
1124 |
gMask "{ Class: SmallInteger }" |
|
1125 |
bMask "{ Class: SmallInteger }" |
|
1126 |
redArray greenArray blueArray |
|
1127 |
dataSize "{ Class: SmallInteger }" |
|
1128 |
nColors "{ Class: SmallInteger }" |
|
1129 |
fit fitMap colors color |
|
1130 |
fast |
|
1131 |
colorIndex "{ Class: SmallInteger }" |
|
89 | 1132 |
depth nColorCells deep| |
1133 |
||
1134 |
Color fixColors notNil ifTrue:[ |
|
1135 |
f := self rgbImageAsDitheredPseudoFormOn:aDevice. |
|
1136 |
f notNil ifTrue:[^ f]. |
|
1137 |
]. |
|
1 | 1138 |
|
1139 |
"find used colors; build color-tree" |
|
1140 |
||
1141 |
fit := false. |
|
1142 |
fitMap := false. |
|
1143 |
depth := aDevice depth. |
|
1144 |
nColorCells := aDevice ncells. |
|
1145 |
||
89 | 1146 |
deep := (depth > 8). |
1147 |
||
1 | 1148 |
rMask := 2r11111111. |
1149 |
gMask := 2r11111111. |
|
1150 |
bMask := 2r11111111. |
|
1151 |
||
151 | 1152 |
'D24IMAGE: allocating colors ...' infoPrintNL. |
66 | 1153 |
|
1 | 1154 |
[fit] whileFalse:[ |
81 | 1155 |
[fitMap] whileFalse:[ |
1156 |
srcIndex := 1. |
|
1157 |
redArray := Array new:256. |
|
1 | 1158 |
|
89 | 1159 |
" |
1160 |
find used colors, build [r][g][b] 3-dimensional array |
|
1161 |
containing true for used colors |
|
1162 |
" |
|
81 | 1163 |
nColors := 0. |
1164 |
srcIndex := 1. |
|
1165 |
dataSize := bytes size. |
|
1166 |
[srcIndex < dataSize] whileTrue:[ |
|
1 | 1167 |
%{ |
81 | 1168 |
if (__isByteArray(_INST(bytes))) { |
1169 |
int sI = _intVal(srcIndex); |
|
1170 |
unsigned char *cp = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1 | 1171 |
|
315
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
1172 |
r = __MKSMALLINT((cp[sI - 1] & _intVal(rMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
1173 |
g = __MKSMALLINT((cp[sI] & _intVal(gMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
1174 |
b = __MKSMALLINT((cp[sI + 1] & _intVal(bMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
1175 |
srcIndex = __MKSMALLINT(sI + 3); |
81 | 1176 |
fast = true; |
1177 |
} else { |
|
1178 |
fast = false; |
|
1179 |
} |
|
1 | 1180 |
%} |
1181 |
. |
|
81 | 1182 |
fast ifFalse:[ |
1183 |
r := bytes at:srcIndex. |
|
1184 |
r := (r bitAnd:rMask) + 1. |
|
1185 |
srcIndex := srcIndex + 1. |
|
1186 |
g := bytes at:srcIndex. |
|
1187 |
g := (g bitAnd:gMask) + 1. |
|
1188 |
srcIndex := srcIndex + 1. |
|
1189 |
b := bytes at:srcIndex. |
|
1190 |
b := (b bitAnd:bMask) + 1. |
|
1191 |
srcIndex := srcIndex + 1 |
|
1192 |
]. |
|
1 | 1193 |
|
81 | 1194 |
greenArray := redArray at:r. |
1195 |
greenArray isNil ifTrue:[ |
|
1196 |
greenArray := Array new:256. |
|
1197 |
redArray at:r put:greenArray |
|
1198 |
]. |
|
1199 |
blueArray := greenArray at:g. |
|
1200 |
blueArray isNil ifTrue:[ |
|
89 | 1201 |
deep ifTrue:[blueArray := Array new:256] |
1202 |
ifFalse:[blueArray := ByteArray new:256]. |
|
81 | 1203 |
greenArray at:g put:blueArray |
1204 |
]. |
|
89 | 1205 |
(blueArray at:b) == 0 ifTrue:[ |
1206 |
blueArray at:b put:1. |
|
81 | 1207 |
nColors := nColors + 1. |
1208 |
(nColors > nColorCells) ifTrue:[ |
|
151 | 1209 |
'D24IMAGE: more than ' infoPrint. nColorCells infoPrint. ' colors' infoPrintNL. |
81 | 1210 |
srcIndex := dataSize + 1 |
1211 |
] |
|
1212 |
] |
|
1213 |
]. |
|
1 | 1214 |
|
81 | 1215 |
"again with less color bits if it does not fit colormap" |
1 | 1216 |
|
81 | 1217 |
(nColors <= nColorCells) ifTrue:[ |
1218 |
fitMap := true |
|
1219 |
] ifFalse:[ |
|
1220 |
"must try again - cutting off some bits" |
|
1221 |
(bMask == 2r11111111) ifTrue:[ |
|
1222 |
bMask := 2r11111110 |
|
1223 |
] ifFalse:[ |
|
89 | 1224 |
(bMask == 2r11111110) ifTrue:[ |
1225 |
bMask := 2r11111100 |
|
1226 |
] ifFalse:[ |
|
1227 |
rMask := (rMask bitShift:1) bitAnd:2r11111111. |
|
1228 |
gMask := (gMask bitShift:1) bitAnd:2r11111111. |
|
1229 |
bMask := (bMask bitShift:1) bitAnd:2r11111111 |
|
1230 |
] |
|
81 | 1231 |
]. |
151 | 1232 |
'D24IMAGE: too many colors; retry with less color resolution' infoPrintNL. |
38 | 1233 |
" |
1 | 1234 |
'masks:' print. rMask print. ' ' print. gMask print. ' ' print. |
1235 |
bMask printNewline |
|
38 | 1236 |
" |
81 | 1237 |
] |
1238 |
]. |
|
1 | 1239 |
|
151 | 1240 |
'D24IMAGE: ' infoPrint. nColors infoPrint. ' colors used' infoPrintNL. |
89 | 1241 |
|
81 | 1242 |
colors := Array new:nColors. |
1243 |
colorIndex := 1. |
|
1 | 1244 |
|
89 | 1245 |
" |
1246 |
now, we have reduced things to the number of colors |
|
1247 |
which are theoretically supported by the devices colormap. |
|
1248 |
allocate all used colors in walking over true entries in |
|
1249 |
the [r][g][b] table - this may still fail |
|
1250 |
" |
|
81 | 1251 |
fit := true. |
1 | 1252 |
|
81 | 1253 |
r := 0. |
1254 |
redArray do:[:greenArray | |
|
1255 |
(fit and:[greenArray notNil]) ifTrue:[ |
|
1256 |
g := 0. |
|
1257 |
greenArray do:[:blueArray | |
|
1258 |
(fit and:[blueArray notNil]) ifTrue:[ |
|
1259 |
b := 0. |
|
89 | 1260 |
blueArray do:[:present | |
1261 |
|id| |
|
1262 |
||
1263 |
(fit and:[present ~~ 0]) ifTrue:[ |
|
81 | 1264 |
color := Color red:(r * 100.0 / 255.0) |
1265 |
green:(g * 100.0 / 255.0) |
|
1266 |
blue:(b * 100.0 / 255.0). |
|
1267 |
color := color on:aDevice. |
|
89 | 1268 |
(id := color colorId) isNil ifTrue:[ |
81 | 1269 |
fit := false |
1270 |
] ifFalse:[ |
|
1271 |
colors at:colorIndex put:color. |
|
1272 |
colorIndex := colorIndex + 1. |
|
89 | 1273 |
blueArray at:(b + 1) put:id |
81 | 1274 |
] |
1275 |
]. |
|
1276 |
b := b + 1 |
|
1277 |
] |
|
1278 |
]. |
|
1279 |
g := g + 1 |
|
1280 |
] |
|
1281 |
]. |
|
1282 |
r := r + 1 |
|
1283 |
]. |
|
1 | 1284 |
|
81 | 1285 |
"again with less color bits if we did not get all colors" |
1 | 1286 |
|
81 | 1287 |
fit ifFalse:[ |
151 | 1288 |
'D24IMAGE: could not allocate color(s)' infoPrintNL. |
1 | 1289 |
|
81 | 1290 |
"free the allocated colors" |
1291 |
colors atAllPut:nil. |
|
1292 |
"a kludge - force immediate freeing of colors" |
|
112 | 1293 |
ObjectMemory scavenge; finalize. |
1 | 1294 |
|
81 | 1295 |
"cut off one more color-bit - cut off blue first" |
1296 |
(bMask == 2r11111111) ifTrue:[ |
|
1297 |
bMask := 2r11111110 |
|
1298 |
] ifFalse:[ |
|
1299 |
(bMask == 2r11111110) ifTrue:[ |
|
1300 |
bMask := 2r11111100 |
|
1301 |
] ifFalse:[ |
|
1302 |
rMask := (rMask bitShift:1) bitAnd:2r11111111. |
|
1303 |
gMask := (gMask bitShift:1) bitAnd:2r11111111. |
|
1304 |
bMask := (bMask bitShift:1) bitAnd:2r11111111 |
|
1305 |
] |
|
1306 |
]. |
|
1307 |
fitMap := false. |
|
1308 |
redArray := nil |
|
1309 |
] |
|
1 | 1310 |
]. |
1311 |
||
1312 |
"create pseudocolor bits and translate" |
|
1313 |
||
1314 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
|
1315 |
||
1316 |
srcIndex := 1. |
|
1317 |
dstIndex := 1. |
|
1318 |
||
89 | 1319 |
(rMask == 2r11111111 |
1320 |
and:[gMask == 2r11111111 |
|
1321 |
and:[bMask == 2r11111111]]) ifTrue:[ |
|
1322 |
[srcIndex < dataSize] whileTrue:[ |
|
1323 |
r := bytes at:srcIndex. |
|
1324 |
srcIndex := srcIndex + 1. |
|
1325 |
g := bytes at:srcIndex. |
|
1326 |
srcIndex := srcIndex + 1. |
|
1327 |
b := bytes at:srcIndex. |
|
1328 |
srcIndex := srcIndex + 1. |
|
1329 |
greenArray := redArray at:(r + 1). |
|
1330 |
blueArray := greenArray at:(g + 1). |
|
1331 |
pseudoBits at:dstIndex put:(blueArray at:(b + 1)). |
|
1332 |
dstIndex := dstIndex + 1 |
|
1333 |
] |
|
1334 |
] ifFalse:[ |
|
1335 |
[srcIndex < dataSize] whileTrue:[ |
|
1336 |
r := bytes at:srcIndex. |
|
1337 |
r := r bitAnd:rMask. |
|
1338 |
srcIndex := srcIndex + 1. |
|
1339 |
g := bytes at:srcIndex. |
|
1340 |
g := g bitAnd:gMask. |
|
1341 |
srcIndex := srcIndex + 1. |
|
1342 |
b := bytes at:srcIndex. |
|
1343 |
b := b bitAnd:bMask. |
|
1344 |
srcIndex := srcIndex + 1. |
|
1345 |
greenArray := redArray at:(r + 1). |
|
1346 |
blueArray := greenArray at:(g + 1). |
|
1347 |
pseudoBits at:dstIndex put:(blueArray at:(b + 1)). |
|
1348 |
dstIndex := dstIndex + 1 |
|
1349 |
] |
|
1 | 1350 |
]. |
1351 |
||
89 | 1352 |
f := Form width:width height:height depth:depth on:aDevice. |
1 | 1353 |
f isNil ifTrue:[^ nil]. |
1354 |
f colorMap:colors. |
|
1355 |
f initGC. |
|
89 | 1356 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:depth |
81 | 1357 |
width:width height:height |
1358 |
x:0 y:0 |
|
1359 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
1 | 1360 |
^ f |
194 | 1361 |
! |
1362 |
||
1363 |
rgbImageAsTrueColorFormOn:aDevice |
|
1364 |
"return a truecolor form from the rgb-picture." |
|
1365 |
||
1366 |
|bestFormat usedDeviceDepth usedDeviceBitsPerPixel depth |
|
1367 |
myDepth form imageBits destIndex srcIndex |
|
1368 |
rightShiftR rightShiftG rightShiftB shiftRed shiftGreen shiftBlue ok| |
|
1369 |
||
1370 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
236 | 1371 |
usedDeviceDepth := bestFormat at:#depth. |
1372 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
|
194 | 1373 |
|
1374 |
rightShiftR := (8 - aDevice bitsRed). |
|
1375 |
rightShiftG := (8 - aDevice bitsGreen). |
|
1376 |
rightShiftB := (8 - aDevice bitsBlue). |
|
1377 |
||
1378 |
shiftRed := aDevice shiftRed. |
|
1379 |
shiftGreen := aDevice shiftGreen. |
|
1380 |
shiftBlue := aDevice shiftBlue. |
|
1381 |
||
1382 |
"/ |
|
1383 |
"/ for now, only a few formats are supported |
|
1384 |
"/ |
|
1385 |
myDepth := self bitsPerPixel. |
|
1386 |
myDepth == usedDeviceBitsPerPixel ifTrue:[ |
|
1387 |
"/ |
|
1388 |
"/ first, the trivial case, where the depths match |
|
1389 |
"/ 24 bit/pixel |
|
1390 |
"/ |
|
1391 |
imageBits := bytes. |
|
1392 |
] ifFalse:[ |
|
1393 |
"/ |
|
1394 |
"/ 16 bit/pixel ... |
|
1395 |
"/ |
|
1396 |
(usedDeviceBitsPerPixel == 16) ifTrue:[ |
|
1397 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
|
1398 |
||
1399 |
"/ now, walk over the image and compose 16bit values from the r/g/b triples |
|
1400 |
||
1401 |
ok := false. |
|
1402 |
||
1403 |
%{ /* OPTIONAL */ |
|
1404 |
if (__bothSmallInteger(_INST(height),_INST(width)) |
|
1405 |
&& __bothSmallInteger(rightShiftR, shiftRed) |
|
1406 |
&& __bothSmallInteger(rightShiftG, shiftGreen) |
|
1407 |
&& __bothSmallInteger(rightShiftB, shiftBlue) |
|
1408 |
&& __isByteArray(_INST(bytes)) |
|
1409 |
&& __isByteArray(imageBits)) { |
|
1410 |
int rShRed = __intVal(rightShiftR), |
|
1411 |
rShGreen = __intVal(rightShiftG), |
|
1412 |
rShBlue = __intVal(rightShiftB), |
|
1413 |
lShRed = __intVal(shiftRed), |
|
1414 |
lShGreen = __intVal(shiftGreen), |
|
1415 |
lShBlue = __intVal(shiftBlue); |
|
1416 |
int x, y, w; |
|
1417 |
||
1418 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1419 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
1420 |
||
1421 |
w = __intVal(_INST(width)); |
|
1422 |
if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) { |
|
1423 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
1424 |
for (x=w; x > 0; x--) { |
|
1425 |
unsigned v; |
|
1426 |
||
1427 |
v = srcPtr[0] << lShRed; |
|
1428 |
v |= (srcPtr[1] << lShGreen); |
|
1429 |
v |= (srcPtr[2] << lShBlue); |
|
1430 |
# ifdef MSBFIRST |
|
1431 |
((short *)dstPtr)[0] = v; |
|
1432 |
# else |
|
1433 |
dstPtr[0] = (v>>8) & 0xFF; |
|
1434 |
dstPtr[1] = (v) & 0xFF; |
|
1435 |
# endif |
|
1436 |
dstPtr += 2; |
|
1437 |
srcPtr += 3; |
|
1438 |
} |
|
1439 |
} |
|
1440 |
} else { |
|
1441 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
1442 |
for (x=w; x > 0; x--) { |
|
1443 |
unsigned r, g, b, v; |
|
1444 |
||
1445 |
r = srcPtr[0] >> rShRed; |
|
1446 |
g = srcPtr[1] >> rShGreen; |
|
1447 |
b = srcPtr[2] >> rShBlue; |
|
1448 |
v = r << lShRed; |
|
1449 |
v |= (g << lShGreen); |
|
1450 |
v |= (b << lShBlue); |
|
1451 |
# ifdef MSBFIRST |
|
1452 |
((short *)dstPtr)[0] = v; |
|
1453 |
# else |
|
1454 |
dstPtr[0] = (v>>8) & 0xFF; |
|
1455 |
dstPtr[1] = (v) & 0xFF; |
|
1456 |
# endif |
|
1457 |
dstPtr += 2; |
|
1458 |
srcPtr += 3; |
|
1459 |
} |
|
1460 |
} |
|
1461 |
} |
|
1462 |
ok = true; |
|
1463 |
} |
|
1464 |
%}. |
|
1465 |
ok ifFalse:[ |
|
1466 |
"/ this fallback is only executed if type is not |
|
1467 |
"/ what the primitive expects; for example, if the bytes-instvar |
|
1468 |
"/ is not a ByteArray |
|
1469 |
||
1470 |
rightShiftR := rightShiftR negated. |
|
1471 |
rightShiftG := rightShiftG negated. |
|
1472 |
rightShiftB := rightShiftB negated. |
|
1473 |
||
1474 |
destIndex := 1. |
|
1475 |
srcIndex := 1. |
|
1476 |
||
1477 |
0 to:height-1 do:[:y | |
|
1478 |
0 to:width-1 do:[:x | |
|
1479 |
|r g b v| |
|
1480 |
||
1481 |
r := bytes at:srcIndex. |
|
1482 |
g := bytes at:(srcIndex + 1). |
|
1483 |
b := bytes at:(srcIndex + 2). |
|
1484 |
||
1485 |
r := r bitShift:rightShiftR. |
|
1486 |
g := g bitShift:rightShiftG. |
|
1487 |
b := b bitShift:rightShiftB. |
|
1488 |
||
1489 |
v := r bitShift:shiftRed. |
|
1490 |
v := v bitOr:(g bitShift:shiftGreen). |
|
1491 |
v := v bitOr:(b bitShift:shiftBlue). |
|
1492 |
||
1493 |
imageBits wordAt:destIndex put:v MSB:true. |
|
1494 |
destIndex := destIndex + 2. |
|
1495 |
srcIndex := srcIndex + 3. |
|
1496 |
] |
|
1497 |
] |
|
1498 |
] |
|
1499 |
] ifFalse:[ |
|
1500 |
"/ |
|
1501 |
"/ 32bit pixels |
|
1502 |
"/ |
|
1503 |
(usedDeviceBitsPerPixel == 32) ifTrue:[ |
|
1504 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
|
1505 |
||
1506 |
"/ now, walk over the image and compose 32bit values from the r/g/b triples |
|
1507 |
||
1508 |
ok := false. |
|
1509 |
||
1510 |
%{ /* OPTIONAL */ |
|
1511 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
1512 |
&& __bothSmallInteger(rightShiftR, shiftRed) |
|
1513 |
&& __bothSmallInteger(rightShiftG, shiftGreen) |
|
1514 |
&& __bothSmallInteger(rightShiftB, shiftBlue) |
|
1515 |
&& __isByteArray(_INST(bytes)) |
|
1516 |
&& __isByteArray(imageBits)) { |
|
1517 |
int rShRed = __intVal(rightShiftR), |
|
1518 |
rShGreen = __intVal(rightShiftG), |
|
1519 |
rShBlue = __intVal(rightShiftB), |
|
1520 |
lShRed = __intVal(shiftRed), |
|
1521 |
lShGreen = __intVal(shiftGreen), |
|
1522 |
lShBlue = __intVal(shiftBlue); |
|
1523 |
int x, y, w; |
|
1524 |
||
1525 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1526 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
1527 |
||
1528 |
w = __intVal(_INST(width)); |
|
1529 |
if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) { |
|
1530 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
1531 |
for (x=w; x > 0; x--) { |
|
1532 |
unsigned v; |
|
1533 |
||
1534 |
v = srcPtr[0] << lShRed; |
|
1535 |
v |= (srcPtr[1] << lShGreen); |
|
1536 |
v |= (srcPtr[2] << lShBlue); |
|
1537 |
# ifdef MSBFIRST |
|
1538 |
((int *)dstPtr)[0] = v; |
|
1539 |
# else |
|
1540 |
dstPtr[0] = (v>>24) & 0xFF; |
|
1541 |
dstPtr[1] = (v>>16) & 0xFF; |
|
1542 |
dstPtr[2] = (v>>8) & 0xFF; |
|
1543 |
dstPtr[3] = (v) & 0xFF; |
|
1544 |
# endif |
|
1545 |
dstPtr += 4; |
|
1546 |
srcPtr += 3; |
|
1547 |
} |
|
1548 |
} |
|
1549 |
} else { |
|
1550 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
1551 |
for (x=w; x > 0; x--) { |
|
1552 |
unsigned r, g, b, v; |
|
1553 |
||
1554 |
r = srcPtr[0] >> rShRed; |
|
1555 |
g = srcPtr[1] >> rShGreen; |
|
1556 |
b = srcPtr[2] >> rShBlue; |
|
1557 |
v = r << lShRed; |
|
1558 |
v |= (g << lShGreen); |
|
1559 |
v |= (b << lShBlue); |
|
1560 |
# ifdef MSBFIRST |
|
1561 |
((int *)dstPtr)[0] = v; |
|
1562 |
# else |
|
1563 |
dstPtr[0] = (v>>24) & 0xFF; |
|
1564 |
dstPtr[1] = (v>>16) & 0xFF; |
|
1565 |
dstPtr[2] = (v>>8) & 0xFF; |
|
1566 |
dstPtr[3] = (v) & 0xFF; |
|
1567 |
# endif |
|
1568 |
dstPtr += 4; |
|
1569 |
srcPtr += 3; |
|
1570 |
} |
|
1571 |
} |
|
1572 |
} |
|
1573 |
ok = true; |
|
1574 |
} |
|
1575 |
%}. |
|
1576 |
ok ifFalse:[ |
|
1577 |
"/ this fallback is only executed if type is not |
|
1578 |
"/ what the primitive expects; for example, if the bytes-instvar |
|
1579 |
"/ is not a ByteArray; or, if this class is autoload on non-compiling |
|
1580 |
"/ systems. |
|
1581 |
||
1582 |
rightShiftR := rightShiftR negated. |
|
1583 |
rightShiftG := rightShiftG negated. |
|
1584 |
rightShiftB := rightShiftB negated. |
|
1585 |
||
1586 |
destIndex := 1. |
|
1587 |
srcIndex := 1. |
|
1588 |
||
1589 |
0 to:height-1 do:[:y | |
|
1590 |
0 to:width-1 do:[:x | |
|
1591 |
|r g b v| |
|
1592 |
||
1593 |
r := bytes at:srcIndex. |
|
1594 |
g := bytes at:(srcIndex + 1). |
|
1595 |
b := bytes at:(srcIndex + 2). |
|
1596 |
||
1597 |
r := r bitShift:rightShiftR. |
|
1598 |
g := g bitShift:rightShiftG. |
|
1599 |
b := b bitShift:rightShiftB. |
|
1600 |
||
1601 |
v := r bitShift:shiftRed. |
|
1602 |
v := v bitOr:(g bitShift:shiftGreen). |
|
1603 |
v := v bitOr:(b bitShift:shiftBlue). |
|
1604 |
||
1605 |
imageBits doubleWordAt:destIndex put:v MSB:true. |
|
1606 |
destIndex := destIndex + 4. |
|
1607 |
srcIndex := srcIndex + 3. |
|
1608 |
] |
|
1609 |
] |
|
1610 |
] |
|
1611 |
]. |
|
1612 |
] |
|
1613 |
]. |
|
1614 |
||
1615 |
imageBits isNil ifTrue:[ |
|
1616 |
'IMAGE: unimplemented trueColor depth in rgbImageAsTrueColorFormOn:' errorPrintNL. |
|
1617 |
^ self rgbImageAsMonoFormOn:aDevice |
|
1618 |
]. |
|
1619 |
||
1620 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
|
1621 |
form isNil ifTrue:[ |
|
1622 |
'IMAGE: display bitmap creation failed' errorPrintNL. |
|
1623 |
^ nil |
|
1624 |
]. |
|
1625 |
form initGC. |
|
1626 |
||
1627 |
form |
|
1628 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
|
1629 |
width:width height:height |
|
1630 |
x:0 y:0 toX:0 y:0. |
|
1631 |
||
1632 |
^ form |
|
1633 |
||
1634 |
"Modified: 21.10.1995 / 19:30:11 / cg" |
|
1 | 1635 |
! ! |
1636 |
||
282 | 1637 |
!Depth24Image methodsFor:'enumerating'! |
1 | 1638 |
|
798 | 1639 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
282 | 1640 |
"perform aBlock for each pixel from x1 to x2 in row y. |
1641 |
The block is passed the color at each pixel. |
|
1642 |
This method allows slighly faster processing of an |
|
1643 |
image than using atX:y:, since some processing can be |
|
1644 |
avoided when going from pixel to pixel. However, for |
|
1645 |
real image processing, specialized methods should be written." |
|
1 | 1646 |
|
282 | 1647 |
|srcIndex "{ Class: SmallInteger }" |
1648 |
x1 "{ Class: SmallInteger }" |
|
1649 |
x2 "{ Class: SmallInteger }" |
|
1650 |
rVal "{ Class: SmallInteger }" |
|
1651 |
gVal "{ Class: SmallInteger }" |
|
1652 |
bVal "{ Class: SmallInteger }" |
|
1653 |
lastR lastG lastB lastColor| |
|
1654 |
||
1655 |
photometric ~~ #rgb ifTrue:[ |
|
805 | 1656 |
^ super colorsAtY:y from:xLow to:xHigh do:aBlock. |
282 | 1657 |
]. |
1658 |
||
1659 |
x1 := xLow. |
|
1660 |
x2 := xHigh. |
|
1661 |
||
1662 |
srcIndex := 1 + (((width * y) + x1) * 3). |
|
1 | 1663 |
|
282 | 1664 |
x1 to:x2 do:[:x | |
798 | 1665 |
rVal := bytes at:(srcIndex). |
1666 |
gVal := bytes at:(srcIndex + 1). |
|
1667 |
bVal := bytes at:(srcIndex + 2). |
|
1668 |
srcIndex := srcIndex + 3. |
|
1669 |
||
1670 |
(rVal == lastR and:[gVal == lastG and:[bVal == lastB]]) ifFalse:[ |
|
1671 |
lastColor := Color red:rVal * 100 / 255 |
|
1672 |
green:gVal * 100 / 255 |
|
1673 |
blue:bVal * 100 / 255. |
|
1674 |
lastR := rVal. |
|
1675 |
lastG := gVal. |
|
1676 |
lastB := bVal. |
|
1677 |
]. |
|
1678 |
aBlock value:x value:lastColor |
|
282 | 1679 |
] |
798 | 1680 |
|
1681 |
"Created: 7.6.1996 / 19:12:28 / cg" |
|
805 | 1682 |
"Modified: 8.6.1996 / 10:16:51 / cg" |
282 | 1683 |
! |
1 | 1684 |
|
798 | 1685 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
282 | 1686 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
1687 |
The block is passed the pixelValue at each pixel. |
|
1688 |
This method allows slighly faster processing of an |
|
1689 |
image than using valueAtX:y:, since some processing can be |
|
1690 |
avoided when going from pixel to pixel. However, for |
|
1691 |
real image processing, specialized methods should be written. |
|
1692 |
Notice that the pixelValue is the r/g/b value packed into an integer |
|
1693 |
(r bitShift:16) bitOr:(g bitSHift:8) bitOr:b" |
|
1694 |
||
1695 |
|srcIndex "{ Class: SmallInteger }" |
|
1696 |
x1 "{ Class: SmallInteger }" |
|
1697 |
x2 "{ Class: SmallInteger }" |
|
1698 |
r "{ Class: SmallInteger }" |
|
1699 |
g "{ Class: SmallInteger }" |
|
1700 |
b "{ Class: SmallInteger }"| |
|
1701 |
||
1702 |
x1 := xLow. |
|
1703 |
x2 := xHigh. |
|
1704 |
||
1705 |
srcIndex := 1 + (((width * y) + x1) * 3). |
|
1706 |
||
1707 |
x1 to:x2 do:[:x | |
|
798 | 1708 |
r := bytes at:(srcIndex). |
1709 |
g := bytes at:(srcIndex + 1). |
|
1710 |
b := bytes at:(srcIndex + 2). |
|
1711 |
srcIndex := srcIndex + 3. |
|
1712 |
aBlock value:x value:(((r bitShift:16) bitOr:(g bitShift:8)) bitOr:b) |
|
282 | 1713 |
] |
798 | 1714 |
|
1715 |
"Created: 7.6.1996 / 19:09:40 / cg" |
|
282 | 1716 |
! ! |
1717 |
||
1718 |
!Depth24Image methodsFor:'magnification'! |
|
1 | 1719 |
|
134 | 1720 |
hardMagnifiedBy:extent |
1 | 1721 |
"return a new image magnified by extent, aPoint. |
1722 |
This is the general magnification method, handling non-integral values" |
|
1723 |
||
1724 |
|mX mY |
|
1725 |
newWidth "{ Class: SmallInteger }" |
|
1726 |
newHeight "{ Class: SmallInteger }" |
|
1727 |
w "{ Class: SmallInteger }" |
|
1728 |
h "{ Class: SmallInteger }" |
|
1729 |
newImage newBytes |
|
1730 |
value "{ Class: SmallInteger }" |
|
1731 |
srcRowIdx "{ Class: SmallInteger }" |
|
1732 |
srcIndex "{ Class: SmallInteger }" |
|
1733 |
dstIndex "{ Class: SmallInteger }"| |
|
1734 |
||
1735 |
mX := extent x. |
|
1736 |
mY := extent y. |
|
1737 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
1738 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
1739 |
||
1740 |
newWidth := (width * mX) truncated. |
|
1741 |
newHeight := (height * mY) truncated. |
|
1742 |
newBytes := ByteArray uninitializedNew:(newWidth * 3 * newHeight). |
|
1743 |
||
1744 |
newImage := self species new. |
|
1745 |
newImage bits:newBytes. |
|
1746 |
newImage width:newWidth. |
|
1747 |
newImage height:newHeight. |
|
1748 |
newImage photometric:photometric. |
|
1749 |
newImage samplesPerPixel:samplesPerPixel. |
|
1750 |
newImage bitsPerSample:#(8 8 8). |
|
1751 |
||
1752 |
"walk over destination image fetching pixels from source image" |
|
1753 |
||
1754 |
mY := mY asFloat. |
|
1755 |
mX := mX asFloat. |
|
1756 |
%{ |
|
1757 |
REGISTER unsigned char *_dstP = _ByteArrayInstPtr(newBytes)->ba_element; |
|
1758 |
unsigned char *_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1759 |
unsigned char *_srcRowP, *sP; |
|
1760 |
int _width3 = _intVal(_INST(width)) * 3; |
|
1761 |
int _w = _intVal(newWidth) - 1; |
|
1762 |
int _h = _intVal(newHeight) - 1; |
|
1763 |
int _row, _col; |
|
1764 |
double _mX = _floatVal(mX); |
|
1765 |
double _mY = _floatVal(mY); |
|
1766 |
||
1767 |
for (_row = 0; _row <= _h; _row++) { |
|
81 | 1768 |
_srcRowP = _srcP + (_width3 * (int)((double)_row / _mY)); |
1769 |
for (_col = 0; _col <= _w; _col++) { |
|
1770 |
sP = _srcRowP + (((int)((double)_col / _mX)) * 3); |
|
1771 |
_dstP[0] = sP[0]; |
|
1772 |
_dstP[1] = sP[1]; |
|
1773 |
_dstP[2] = sP[2]; |
|
1774 |
_dstP += 3; |
|
1775 |
} |
|
1 | 1776 |
} |
1777 |
%} |
|
1778 |
. |
|
81 | 1779 |
" the above C-code is equivalent to: |
1780 |
||
1 | 1781 |
dstIndex := 1. |
1782 |
w := newWidth - 1. |
|
1783 |
h := newHeight - 1. |
|
1784 |
0 to:h do:[:row | |
|
81 | 1785 |
srcRowIdx := (width * 3 * (row // mY)) + 1. |
1786 |
0 to:w do:[:col | |
|
1787 |
srcIndex := srcRowIdx + ((col // mX) * 3). |
|
1788 |
value := bytes at:srcIndex. |
|
1789 |
newBytes at:dstIndex put:value. |
|
1790 |
value := bytes at:(srcIndex + 1). |
|
1791 |
newBytes at:(dstIndex + 1) put:value. |
|
1792 |
value := bytes at:(srcIndex + 2). |
|
1793 |
newBytes at:(dstIndex + 2) put:value. |
|
1794 |
dstIndex := dstIndex + 3 |
|
1795 |
] |
|
1 | 1796 |
]. |
1797 |
" |
|
1798 |
^ newImage |
|
282 | 1799 |
! |
1800 |
||
1801 |
magnifyRowFrom:srcBytes offset:srcStart |
|
1802 |
into:dstBytes offset:dstStart factor:mX |
|
1803 |
||
1804 |
"magnify a single pixel row - can only magnify by integer factors" |
|
1805 |
||
1806 |
%{ |
|
1807 |
unsigned char *srcP, *dstP; |
|
1808 |
int _mag; |
|
1809 |
REGISTER int i; |
|
1810 |
REGISTER unsigned char byte1, byte2, byte3; |
|
1811 |
int _pixels; |
|
1812 |
OBJ w = _INST(width); |
|
1813 |
||
1814 |
if (__bothSmallInteger(srcStart, dstStart) |
|
1815 |
&& __bothSmallInteger(w, mX) |
|
1816 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
|
1817 |
_mag = _intVal(mX); |
|
1818 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
1819 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
1820 |
_pixels = _intVal(w); |
|
1821 |
||
1822 |
while (_pixels--) { |
|
1823 |
byte1 = *srcP; |
|
1824 |
byte2 = *(srcP+1); |
|
1825 |
byte3 = *(srcP+2); |
|
1826 |
srcP += 3; |
|
1827 |
for (i=_mag; i>0; i--) { |
|
1828 |
*dstP = byte1; |
|
1829 |
*(dstP+1) = byte2; |
|
1830 |
*(dstP+2) = byte3; |
|
1831 |
dstP += 3; |
|
1832 |
} |
|
1833 |
} |
|
1834 |
RETURN (self); |
|
1835 |
} |
|
1836 |
%} |
|
1837 |
. |
|
1838 |
self primitiveFailed |
|
1 | 1839 |
! ! |
282 | 1840 |
|
1841 |
!Depth24Image methodsFor:'queries'! |
|
1842 |
||
1843 |
bitsPerPixel |
|
1844 |
"return the number of bits per pixel" |
|
1845 |
||
1846 |
^ 24 |
|
1847 |
! |
|
1848 |
||
1849 |
bitsPerRow |
|
1850 |
"return the number of bits in one scanline of the image" |
|
1851 |
||
1852 |
^ width * 24 |
|
1853 |
! |
|
1854 |
||
1855 |
bitsPerSample |
|
1856 |
"return the number of bits per sample. |
|
1857 |
The return value is an array of bits-per-plane." |
|
1858 |
||
1859 |
^ #(8 8 8) |
|
1860 |
! |
|
1861 |
||
805 | 1862 |
blueBitsOf:pixel |
1863 |
^ pixel bitAnd:16rFF. |
|
1864 |
||
1865 |
"Created: 8.6.1996 / 09:56:20 / cg" |
|
1866 |
! |
|
1867 |
||
1868 |
blueComponentOf:pixel |
|
1869 |
^ (100.0 / 255.0) * (pixel bitAnd:16rFF) |
|
1870 |
||
1871 |
"Created: 8.6.1996 / 08:42:44 / cg" |
|
1872 |
"Modified: 8.6.1996 / 09:57:41 / cg" |
|
1873 |
! |
|
1874 |
||
282 | 1875 |
bytesPerRow |
1876 |
"return the number of bytes in one scanline of the image" |
|
1877 |
||
1878 |
^ width * 3 |
|
1879 |
! |
|
1880 |
||
805 | 1881 |
greenBitsOf:pixel |
1882 |
^ (pixel bitShift:-8) bitAnd:16rFF. |
|
1883 |
||
1884 |
"Modified: 8.6.1996 / 08:56:28 / cg" |
|
1885 |
"Created: 8.6.1996 / 09:56:30 / cg" |
|
1886 |
! |
|
1887 |
||
1888 |
greenComponentOf:pixel |
|
1889 |
^ (100.0 / 255.0) * (pixel bitShift:-8) bitAnd:16rFF. |
|
1890 |
||
1891 |
"Created: 8.6.1996 / 08:42:37 / cg" |
|
1892 |
"Modified: 8.6.1996 / 09:57:32 / cg" |
|
1893 |
! |
|
1894 |
||
1895 |
redBitsOf:pixel |
|
1896 |
^ (pixel bitShift:-16) bitAnd:16rFF. |
|
1897 |
||
1898 |
"Modified: 8.6.1996 / 08:56:31 / cg" |
|
1899 |
"Created: 8.6.1996 / 09:56:39 / cg" |
|
1900 |
! |
|
1901 |
||
1902 |
redComponentOf:pixel |
|
1903 |
^ (100.0 / 255.0) * (pixel bitShift:-16) bitAnd:16rFF. |
|
1904 |
||
1905 |
"Created: 8.6.1996 / 08:42:25 / cg" |
|
1906 |
"Modified: 8.6.1996 / 09:57:23 / cg" |
|
1907 |
! |
|
1908 |
||
282 | 1909 |
samplesPerPixel |
1910 |
"return the number of samples per pixel in the image." |
|
1911 |
||
1912 |
^ 3 |
|
1913 |
! ! |
|
1914 |
||
1915 |
!Depth24Image class methodsFor:'documentation'! |
|
1916 |
||
1917 |
version |
|
805 | 1918 |
^ '$Header: /cvs/stx/stx/libview/Depth24Image.st,v 1.27 1996-06-08 11:09:27 cg Exp $' |
282 | 1919 |
! ! |