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