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