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