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