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