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