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