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 |
|
86
|
24 |
$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.15 1994-11-28 21:00:47 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 |
"
|
86
|
45 |
$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.15 1994-11-28 21:00:47 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 |
|
1
|
57 |
!Depth8Image methodsFor:'accessing'!
|
|
58 |
|
|
59 |
bitsPerPixel
|
|
60 |
"return the number of bits per pixel"
|
|
61 |
|
|
62 |
^ 8
|
|
63 |
!
|
|
64 |
|
|
65 |
bitsPerRow
|
|
66 |
"return the number of bits in one scanline of the image"
|
|
67 |
|
|
68 |
^ width * 8
|
|
69 |
!
|
|
70 |
|
|
71 |
bitsPerSample
|
|
72 |
"return the number of bits per sample.
|
|
73 |
The return value is an array of bits-per-plane."
|
|
74 |
|
|
75 |
^ #(8)
|
|
76 |
!
|
|
77 |
|
|
78 |
bytesPerRow
|
|
79 |
"return the number of bytes in one scanline of the image"
|
|
80 |
|
|
81 |
^ width
|
|
82 |
!
|
|
83 |
|
|
84 |
samplesPerPixel
|
|
85 |
"return the number of samples per pixel in the image."
|
|
86 |
|
|
87 |
^ 1
|
81
|
88 |
! !
|
|
89 |
|
|
90 |
!Depth8Image methodsFor:'accessing'!
|
1
|
91 |
|
|
92 |
atX:x y:y
|
|
93 |
"retrieve a pixel at x/y; return a color.
|
|
94 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
95 |
x = width-1, y=height-1 for lower right pixel"
|
|
96 |
|
38
|
97 |
|value "{ Class: SmallInteger }"
|
|
98 |
index "{ Class: SmallInteger }"|
|
1
|
99 |
|
|
100 |
index := (width * y) + 1 + x.
|
|
101 |
value := bytes at:index.
|
|
102 |
|
|
103 |
photometric == #whiteIs0 ifTrue:[
|
71
|
104 |
^ Color grey:100 - (100 / 255 * value)
|
1
|
105 |
].
|
|
106 |
photometric == #blackIs0 ifTrue:[
|
71
|
107 |
^ Color grey:(100 / 255 * value)
|
1
|
108 |
].
|
|
109 |
photometric ~~ #palette ifTrue:[
|
71
|
110 |
self error:'format not supported'.
|
|
111 |
^ nil
|
1
|
112 |
].
|
|
113 |
index := value + 1.
|
|
114 |
^ Color red:(((colorMap at:1) at:index) * 100 / 255)
|
71
|
115 |
green:(((colorMap at:2) at:index) * 100 / 255)
|
|
116 |
blue:(((colorMap at:3) at:index) * 100 / 255)
|
1
|
117 |
!
|
|
118 |
|
|
119 |
valueAtX:x y:y
|
|
120 |
"retrieve a pixel at x/y; return a pixelValue.
|
|
121 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
122 |
x = width-1, y=height-1 for lower right pixel"
|
|
123 |
|
|
124 |
|index "{ Class: SmallInteger }"|
|
|
125 |
|
|
126 |
index := (width * y) + 1 + x.
|
|
127 |
^ bytes at:index.
|
|
128 |
!
|
|
129 |
|
|
130 |
atX:x y:y putValue:aPixelValue
|
|
131 |
"set the pixel at x/y to aPixelValue.
|
|
132 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
133 |
x = width-1, y=height-1 for lower right pixel"
|
|
134 |
|
|
135 |
|index "{ Class: SmallInteger }"|
|
|
136 |
|
|
137 |
index := (width * y) + 1 + x.
|
|
138 |
bytes at:index put:aPixelValue.
|
81
|
139 |
! !
|
|
140 |
|
|
141 |
!Depth8Image methodsFor:'enumeration'!
|
|
142 |
|
|
143 |
valueAtY:y from:xLow to:xHigh do:aBlock
|
|
144 |
"perform aBlock for each pixelValue from x1 to x2 in row y.
|
|
145 |
The block is passed the pixelValue at each pixel.
|
|
146 |
This method allows slighly faster processing of an
|
|
147 |
image than using valueAtX:y:, since some processing can be
|
|
148 |
avoided when going from pixel to pixel. However, for
|
|
149 |
real image processing, specialized methods should be written."
|
|
150 |
|
|
151 |
|srcIndex "{ Class: SmallInteger }"
|
|
152 |
index "{ Class: SmallInteger }"
|
|
153 |
pixelValue "{ Class: SmallInteger }"
|
|
154 |
x1 "{ Class: SmallInteger }"
|
|
155 |
x2 "{ Class: SmallInteger }"|
|
|
156 |
|
|
157 |
x1 := xLow.
|
|
158 |
x2 := xHigh.
|
|
159 |
srcIndex := (width * y) + 1 + x1.
|
|
160 |
|
|
161 |
x1 to:x2 do:[:x |
|
|
162 |
pixelValue := bytes at:srcIndex.
|
|
163 |
srcIndex := srcIndex + 1.
|
|
164 |
aBlock value:x value:pixelValue
|
|
165 |
]
|
1
|
166 |
!
|
|
167 |
|
|
168 |
atY:y from:xLow to:xHigh do:aBlock
|
|
169 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
170 |
The block is passed the color at each pixel.
|
|
171 |
This method allows slighly faster processing of an
|
|
172 |
image than using atX:y:, since some processing can be
|
|
173 |
avoided when going from pixel to pixel. However, for
|
|
174 |
real image processing, specialized methods should be written."
|
|
175 |
|
|
176 |
|srcIndex "{ Class: SmallInteger }"
|
|
177 |
index "{ Class: SmallInteger }"
|
|
178 |
value "{ Class: SmallInteger }"
|
|
179 |
x1 "{ Class: SmallInteger }"
|
|
180 |
x2 "{ Class: SmallInteger }"
|
|
181 |
color colors last|
|
|
182 |
|
|
183 |
colors := Array new:256.
|
|
184 |
|
|
185 |
x1 := xLow.
|
|
186 |
x2 := xHigh.
|
|
187 |
srcIndex := (width * y) + 1 + x1.
|
|
188 |
|
|
189 |
x1 to:x2 do:[:x |
|
71
|
190 |
value := bytes at:srcIndex.
|
|
191 |
srcIndex := srcIndex + 1.
|
|
192 |
value ~~ last ifTrue:[
|
|
193 |
last := value.
|
|
194 |
index := value + 1.
|
|
195 |
color := colors at:index.
|
|
196 |
color isNil ifTrue:[
|
|
197 |
photometric == #whiteIs0 ifTrue:[
|
|
198 |
color := (Color grey:100 - (100 / 255 * value))
|
|
199 |
] ifFalse:[
|
|
200 |
photometric == #blackIs0 ifTrue:[
|
|
201 |
color := (Color grey:(100 / 255 * value))
|
|
202 |
] ifFalse:[
|
|
203 |
photometric == #palette ifTrue:[
|
|
204 |
color := (Color red:(((colorMap at:1) at:index) * 100 / 255)
|
|
205 |
green:(((colorMap at:2) at:index) * 100 / 255)
|
|
206 |
blue:(((colorMap at:3) at:index) * 100 / 255))
|
|
207 |
] ifFalse:[
|
|
208 |
self error:'format not supported'.
|
|
209 |
^ nil
|
|
210 |
]
|
|
211 |
]
|
|
212 |
].
|
|
213 |
colors at:index put:color
|
|
214 |
]
|
|
215 |
].
|
|
216 |
aBlock value:x value:color
|
1
|
217 |
]
|
|
218 |
! !
|
|
219 |
|
|
220 |
!Depth8Image methodsFor:'private'!
|
|
221 |
|
|
222 |
dither1PlaneUsingMap:map on:aDevice
|
|
223 |
"a helper for dithering palette and greyscale images"
|
|
224 |
|
|
225 |
|f
|
|
226 |
patterns formBytes
|
|
227 |
pixel0bytes pixel1bytes
|
|
228 |
clr ditherPattern
|
|
229 |
nDither "{Class: SmallInteger }"
|
|
230 |
nColors "{Class: SmallInteger }"
|
|
231 |
w "{Class: SmallInteger }"
|
|
232 |
h "{Class: SmallInteger }"
|
|
233 |
v "{Class: SmallInteger }"
|
|
234 |
srcIndex "{Class: SmallInteger }"
|
|
235 |
dstIndex "{Class: SmallInteger }"
|
|
236 |
mask "{Class: SmallInteger }"
|
|
237 |
outBits "{Class: SmallInteger }"
|
|
238 |
outCount "{Class: SmallInteger }"
|
|
239 |
patternOffset "{Class: SmallInteger }"
|
|
240 |
patternBytes
|
|
241 |
patternBits "{Class: SmallInteger }"
|
|
242 |
index "{Class: SmallInteger }"
|
|
243 |
p0 "{Class: SmallInteger }"
|
|
244 |
p1 "{Class: SmallInteger }" |
|
|
245 |
|
|
246 |
nColors := map size.
|
|
247 |
w := width.
|
|
248 |
h := height.
|
|
249 |
|
|
250 |
formBytes := ByteArray uninitializedNew:(w + 7 // 8) * h.
|
|
251 |
patterns := Array new:nColors.
|
|
252 |
pixel0bytes := ByteArray uninitializedNew:nColors.
|
|
253 |
pixel1bytes := ByteArray uninitializedNew:nColors.
|
|
254 |
|
38
|
255 |
"
|
|
256 |
extract dither patterns and values to use for 1/0 bits
|
|
257 |
in those from the dithercolors
|
|
258 |
"
|
1
|
259 |
1 to:nColors do:[:i |
|
71
|
260 |
clr := (map at:i) on:aDevice.
|
|
261 |
ditherPattern := clr ditherForm.
|
1
|
262 |
|
71
|
263 |
ditherPattern isNil ifTrue:[
|
|
264 |
patterns at:i put:#[2r11111111
|
|
265 |
2r11111111
|
|
266 |
2r11111111
|
|
267 |
2r11111111
|
|
268 |
2r11111111
|
|
269 |
2r11111111
|
|
270 |
2r11111111
|
|
271 |
2r11111111].
|
|
272 |
pixel0bytes at:i put:clr colorId.
|
|
273 |
pixel1bytes at:i put:clr colorId
|
|
274 |
] ifFalse:[
|
|
275 |
patterns at:i put:(ditherPattern bits).
|
|
276 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId.
|
|
277 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId.
|
|
278 |
].
|
1
|
279 |
].
|
|
280 |
|
|
281 |
%{
|
|
282 |
unsigned char *_srcP, *_dstP;
|
|
283 |
OBJ _patternBytes;
|
|
284 |
unsigned char _mask = 0x80;
|
|
285 |
unsigned char _outBits = 0;
|
|
286 |
unsigned char _last, _v, _patternBits, _p0, _p1;
|
|
287 |
int _h, _w;
|
|
288 |
int _patternOffset = 0;
|
|
289 |
|
|
290 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element;
|
|
291 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element;
|
|
292 |
for (_h = _intVal(h); _h; _h--) {
|
71
|
293 |
_last = -1;
|
|
294 |
for (_w = _intVal(w); _w; _w--) {
|
|
295 |
_v = *_srcP++;
|
|
296 |
if (_v != _last) {
|
|
297 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v];
|
|
298 |
if (__isByteArray(_patternBytes)) {
|
|
299 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset];
|
|
300 |
} else if (__isArray(_patternBytes)) {
|
|
301 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]);
|
|
302 |
}
|
|
303 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v];
|
|
304 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v];
|
|
305 |
_last = _v;
|
|
306 |
}
|
|
307 |
_outBits <<= 1;
|
|
308 |
if (_patternBits & _mask)
|
|
309 |
_outBits |= _p1;
|
|
310 |
else
|
|
311 |
_outBits |= _p0;
|
|
312 |
_mask >>= 1;
|
|
313 |
if (_mask == 0) {
|
|
314 |
_mask = 0x80;
|
|
315 |
*_dstP++ = _outBits;
|
|
316 |
_outBits = 0;
|
|
317 |
}
|
|
318 |
}
|
|
319 |
if (_mask != 0x80) {
|
|
320 |
while (_mask != 0) {
|
|
321 |
_outBits <<= 1;
|
|
322 |
_mask >>= 1;
|
|
323 |
}
|
|
324 |
*_dstP++ = _outBits;
|
|
325 |
_mask = 0x80;
|
|
326 |
_outBits = 0;
|
|
327 |
}
|
|
328 |
_patternOffset++;
|
|
329 |
if (_patternOffset == 8)
|
|
330 |
_patternOffset = 0;
|
1
|
331 |
}
|
|
332 |
%}
|
|
333 |
.
|
|
334 |
f := Form width:w height:h fromArray:formBytes.
|
|
335 |
^ f
|
|
336 |
!
|
|
337 |
|
|
338 |
dither2PlaneUsingMap:map on:aDevice
|
|
339 |
"a helper for dithering palette and greyscale images"
|
|
340 |
|
|
341 |
|f
|
|
342 |
patterns formBytes
|
|
343 |
pixel0bytes pixel1bytes
|
|
344 |
clr ditherPattern
|
|
345 |
nDither "{Class: SmallInteger }"
|
|
346 |
nColors "{Class: SmallInteger }"
|
|
347 |
w "{Class: SmallInteger }"
|
|
348 |
h "{Class: SmallInteger }"
|
|
349 |
v "{Class: SmallInteger }"
|
|
350 |
srcIndex "{Class: SmallInteger }"
|
|
351 |
dstIndex "{Class: SmallInteger }"
|
|
352 |
mask "{Class: SmallInteger }"
|
|
353 |
outBits "{Class: SmallInteger }"
|
|
354 |
outCount "{Class: SmallInteger }"
|
|
355 |
patternOffset "{Class: SmallInteger }"
|
|
356 |
patternBytes
|
|
357 |
patternBits "{Class: SmallInteger }"
|
|
358 |
index "{Class: SmallInteger }"
|
|
359 |
p0 "{Class: SmallInteger }"
|
|
360 |
p1 "{Class: SmallInteger }" |
|
|
361 |
|
|
362 |
nColors := map size.
|
|
363 |
w := width.
|
|
364 |
h := height.
|
|
365 |
|
|
366 |
formBytes := ByteArray uninitializedNew:(w * 2 + 7 // 8) * h.
|
|
367 |
patterns := Array new:nColors.
|
|
368 |
pixel0bytes := ByteArray uninitializedNew:nColors.
|
|
369 |
pixel1bytes := ByteArray uninitializedNew:nColors.
|
|
370 |
|
|
371 |
"extract dither patterns and values to use for 1/o bits
|
|
372 |
in those from the dithercolors"
|
|
373 |
|
|
374 |
1 to:nColors do:[:i |
|
71
|
375 |
clr := (map at:i) on:aDevice.
|
|
376 |
ditherPattern := clr ditherForm.
|
1
|
377 |
|
71
|
378 |
ditherPattern isNil ifTrue:[
|
|
379 |
patterns at:i put:#[2r11111111
|
|
380 |
2r11111111
|
|
381 |
2r11111111
|
|
382 |
2r11111111
|
|
383 |
2r11111111
|
|
384 |
2r11111111
|
|
385 |
2r11111111
|
|
386 |
2r11111111].
|
|
387 |
pixel0bytes at:i put:clr colorId.
|
|
388 |
pixel1bytes at:i put:clr colorId
|
|
389 |
] ifFalse:[
|
|
390 |
patterns at:i put:(ditherPattern bits).
|
|
391 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId.
|
|
392 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId.
|
|
393 |
].
|
1
|
394 |
].
|
|
395 |
|
|
396 |
%{
|
|
397 |
unsigned char *_srcP, *_dstP;
|
|
398 |
OBJ _patternBytes;
|
|
399 |
unsigned char _mask = 0x80;
|
|
400 |
unsigned char _outBits = 0;
|
|
401 |
unsigned char _last, _v, _patternBits, _p0, _p1;
|
|
402 |
int _h, _w;
|
|
403 |
int _patternOffset = 0;
|
|
404 |
int _outCount;
|
|
405 |
|
|
406 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element;
|
|
407 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element;
|
|
408 |
for (_h = _intVal(h); _h; _h--) {
|
71
|
409 |
_last = -1;
|
|
410 |
_outCount = 0;
|
|
411 |
for (_w = _intVal(w); _w; _w--) {
|
|
412 |
_v = *_srcP++;
|
|
413 |
if (_v != _last) {
|
|
414 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v];
|
|
415 |
if (__isByteArray(_patternBytes)) {
|
|
416 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset];
|
|
417 |
} else if (__isArray(_patternBytes)) {
|
|
418 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]);
|
|
419 |
}
|
|
420 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v];
|
|
421 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v];
|
|
422 |
_last = _v;
|
|
423 |
}
|
|
424 |
_outBits <<= 2;
|
|
425 |
if (_patternBits & _mask)
|
|
426 |
_outBits |= _p1;
|
|
427 |
else
|
|
428 |
_outBits |= _p0;
|
|
429 |
_mask >>= 1;
|
|
430 |
_outCount++;
|
|
431 |
if (_outCount == 4) {
|
|
432 |
*_dstP++ = _outBits;
|
|
433 |
_outCount = 0;
|
|
434 |
if (_mask == 0) {
|
|
435 |
_mask = 0x80;
|
|
436 |
}
|
|
437 |
}
|
|
438 |
}
|
|
439 |
if (_outCount) {
|
|
440 |
do {
|
|
441 |
_outBits <<= 2;
|
|
442 |
} while (++_outCount != 4);
|
|
443 |
*_dstP++ = _outBits;
|
|
444 |
}
|
|
445 |
_mask = 0x80;
|
|
446 |
_outBits = 0;
|
|
447 |
_patternOffset++;
|
|
448 |
if (_patternOffset == 8)
|
|
449 |
_patternOffset = 0;
|
1
|
450 |
}
|
|
451 |
%}
|
|
452 |
.
|
|
453 |
f := Form width:w height:h depth:2.
|
|
454 |
f initGC.
|
|
455 |
f device drawBits:formBytes depth:2 width:w height:h x:0 y:0
|
71
|
456 |
into:f id x:0 y:0 width:w height:h with:f gcId.
|
1
|
457 |
^ f
|
|
458 |
! !
|
|
459 |
|
|
460 |
!Depth8Image methodsFor:'converting palette images'!
|
|
461 |
|
|
462 |
paletteImageAsMonoFormOn:aDevice
|
|
463 |
"return a 1-bit monoForm from the palette picture -
|
|
464 |
the result is a thresholded form, with white for
|
|
465 |
brightness values above 50%, black below"
|
|
466 |
|
|
467 |
|monoBits f
|
|
468 |
map rMap gMap bMap
|
|
469 |
fast
|
|
470 |
r g b
|
|
471 |
v "{ Class: SmallInteger }"
|
|
472 |
bitCount "{ Class: SmallInteger }"
|
|
473 |
bits "{ Class: SmallInteger }"
|
|
474 |
w "{ Class: SmallInteger }"
|
|
475 |
h "{ Class: SmallInteger }"
|
|
476 |
mapSize "{ Class: SmallInteger }"
|
|
477 |
srcIndex "{ Class: SmallInteger }"
|
|
478 |
dstIndex "{ Class: SmallInteger }" |
|
|
479 |
|
|
480 |
w := width.
|
|
481 |
h := height.
|
|
482 |
monoBits := ByteArray uninitializedNew:(((w + 7) // 8) * h).
|
|
483 |
|
|
484 |
rMap := colorMap at:1.
|
|
485 |
gMap := colorMap at:2.
|
|
486 |
bMap := colorMap at:3.
|
|
487 |
map := ByteArray uninitializedNew:256.
|
|
488 |
|
|
489 |
mapSize := rMap size.
|
|
490 |
1 to:mapSize do:[:i |
|
71
|
491 |
r := rMap at:i.
|
|
492 |
r notNil ifTrue:[
|
|
493 |
g := gMap at:i.
|
|
494 |
b := bMap at:i.
|
|
495 |
v := ((3 * r) + (6 * g) + (1 * b)) // 10.
|
|
496 |
v := v bitShift:-7. "only keep hi-bit"
|
|
497 |
(v == 1) ifTrue:[
|
|
498 |
map at:i put:0 "was: 1"
|
|
499 |
] ifFalse:[
|
|
500 |
map at:i put:1 "was: 0"
|
|
501 |
]
|
|
502 |
]
|
1
|
503 |
].
|
|
504 |
|
|
505 |
fast := false.
|
|
506 |
%{
|
|
507 |
register unsigned char *srcPtr, *dstPtr, *mapPtr;
|
86
|
508 |
register __v, __bits, __bitCount;
|
1
|
509 |
register j;
|
|
510 |
register i;
|
|
511 |
extern OBJ ByteArray;
|
|
512 |
|
35
|
513 |
if (__isByteArray(_INST(bytes)) && __isByteArray(map) && __isByteArray(monoBits)) {
|
71
|
514 |
fast = true;
|
|
515 |
srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element;
|
|
516 |
dstPtr = _ByteArrayInstPtr(monoBits)->ba_element;
|
|
517 |
mapPtr = _ByteArrayInstPtr(map)->ba_element;
|
|
518 |
for (i=_intVal(h); i>0; i--) {
|
86
|
519 |
__bitCount = 0;
|
|
520 |
__bits = 0;
|
71
|
521 |
for (j=_intVal(w); j>0; j--) {
|
86
|
522 |
__v = mapPtr[*srcPtr++];
|
|
523 |
__bits = (__bits<<1) | __v;
|
|
524 |
__bitCount++;
|
|
525 |
if (__bitCount == 8) {
|
|
526 |
*dstPtr++ = __bits;
|
|
527 |
__bits = 0;
|
|
528 |
__bitCount = 0;
|
71
|
529 |
}
|
|
530 |
}
|
86
|
531 |
if (__bitCount != 0) {
|
|
532 |
*dstPtr++ = __bits;
|
71
|
533 |
}
|
|
534 |
}
|
1
|
535 |
}
|
|
536 |
%}
|
|
537 |
.
|
|
538 |
fast ifFalse:[
|
71
|
539 |
srcIndex := 1.
|
|
540 |
dstIndex := 1.
|
|
541 |
1 to:h do:[:row |
|
1
|
542 |
|
71
|
543 |
bitCount := 0.
|
|
544 |
bits := 0.
|
|
545 |
1 to:w do:[:col |
|
|
546 |
v := bytes at:srcIndex.
|
|
547 |
srcIndex := srcIndex + 1.
|
|
548 |
v := map at:(v + 1).
|
|
549 |
bits := (bits bitShift:1) bitOr:v.
|
|
550 |
bitCount := bitCount + 1.
|
|
551 |
(bitCount == 8) ifTrue:[
|
|
552 |
monoBits at:dstIndex put:bits.
|
|
553 |
dstIndex := dstIndex + 1.
|
|
554 |
bits := 0.
|
|
555 |
bitCount := 0
|
|
556 |
]
|
|
557 |
].
|
|
558 |
(bitCount ~~ 0) ifTrue:[
|
|
559 |
monoBits at:dstIndex put:bits.
|
|
560 |
dstIndex := dstIndex + 1
|
|
561 |
]
|
|
562 |
]
|
1
|
563 |
].
|
|
564 |
|
|
565 |
f := Form width:w height:h depth:1 on:aDevice.
|
|
566 |
f isNil ifTrue:[^ nil].
|
|
567 |
f initGC.
|
38
|
568 |
"/ (aDevice blackpixel == 0) ifFalse:[
|
|
569 |
"/ "have to invert bits"
|
|
570 |
"/ f function:#copyInverted
|
|
571 |
"/ ].
|
1
|
572 |
aDevice drawBits:monoBits depth:1 width:w height:h
|
71
|
573 |
x:0 y:0
|
|
574 |
into:(f id) x:0 y:0 width:w height:h with:(f gcId).
|
1
|
575 |
^ f
|
|
576 |
!
|
|
577 |
|
|
578 |
paletteImageAs2PlaneFormOn:aDevice
|
|
579 |
"return a 2-bit greyForm from the palette picture -
|
|
580 |
the result is a thresholded form, with white/lightGrey/darkGrey
|
|
581 |
and black for brightness values 100..75, 75..50, 50..25 and 25..0 %"
|
|
582 |
|
|
583 |
|twoPlaneBits f
|
|
584 |
map rMap gMap bMap fast
|
|
585 |
v "{ Class: SmallInteger }"
|
|
586 |
bitCount "{ Class: SmallInteger }"
|
|
587 |
bits "{ Class: SmallInteger }"
|
|
588 |
w "{ Class: SmallInteger }"
|
|
589 |
h "{ Class: SmallInteger }"
|
|
590 |
mapSize "{ Class: SmallInteger }"
|
|
591 |
srcIndex "{ Class: SmallInteger }"
|
|
592 |
dstIndex "{ Class: SmallInteger }" |
|
|
593 |
|
|
594 |
w := width.
|
|
595 |
h := height.
|
|
596 |
twoPlaneBits := ByteArray uninitializedNew:(((w * 2 + 7) // 8) * h).
|
|
597 |
|
|
598 |
rMap := colorMap at:1.
|
|
599 |
gMap := colorMap at:2.
|
|
600 |
bMap := colorMap at:3.
|
|
601 |
map := ByteArray uninitializedNew:256.
|
|
602 |
1 to:(rMap size) do:[:i |
|
71
|
603 |
|r g b v|
|
1
|
604 |
|
71
|
605 |
r := rMap at:i.
|
|
606 |
r notNil ifTrue:[
|
|
607 |
g := gMap at:i.
|
|
608 |
b := bMap at:i.
|
|
609 |
v := ((3 * r) + (6 * g) + (1 * b)) // 10.
|
|
610 |
v := v bitShift:-6. "only keep hi-2-bits"
|
|
611 |
map at:i put:v
|
|
612 |
]
|
1
|
613 |
].
|
|
614 |
fast := false.
|
|
615 |
%{
|
|
616 |
register unsigned char *srcPtr, *dstPtr, *mapPtr;
|
86
|
617 |
register __v, __bits, __bitCount;
|
1
|
618 |
register j;
|
|
619 |
register i;
|
|
620 |
extern OBJ ByteArray;
|
|
621 |
|
35
|
622 |
if ((__isByteArray(_INST(bytes))) && (__isByteArray(map)) && (__isByteArray(twoPlaneBits))) {
|
71
|
623 |
fast = true;
|
|
624 |
srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element;
|
|
625 |
dstPtr = _ByteArrayInstPtr(twoPlaneBits)->ba_element;
|
|
626 |
mapPtr = _ByteArrayInstPtr(map)->ba_element;
|
|
627 |
for (i=_intVal(h); i>0; i--) {
|
86
|
628 |
__bitCount = 0;
|
|
629 |
__bits = 0;
|
71
|
630 |
for (j=_intVal(w); j>0; j--) {
|
86
|
631 |
__v = mapPtr[*srcPtr++];
|
|
632 |
__bits = (__bits<<2) | __v;
|
|
633 |
__bitCount++;
|
|
634 |
if (__bitCount == 4) {
|
|
635 |
*dstPtr++ = __bits;
|
|
636 |
__bits = 0;
|
|
637 |
__bitCount = 0;
|
71
|
638 |
}
|
|
639 |
}
|
86
|
640 |
if (__bitCount != 0) {
|
|
641 |
*dstPtr++ = __bits;
|
71
|
642 |
}
|
|
643 |
}
|
1
|
644 |
}
|
|
645 |
%}
|
|
646 |
.
|
|
647 |
fast ifFalse:[
|
71
|
648 |
srcIndex := 1.
|
|
649 |
dstIndex := 1.
|
|
650 |
1 to:h do:[:row |
|
|
651 |
bitCount := 0.
|
|
652 |
bits := 0.
|
|
653 |
1 to:w do:[:col |
|
|
654 |
v := bytes at:srcIndex.
|
|
655 |
srcIndex := srcIndex + 1.
|
|
656 |
v := map at:(v + 1).
|
|
657 |
bits := (bits bitShift:2) bitOr:v.
|
|
658 |
bitCount := bitCount + 1.
|
|
659 |
(bitCount == 4) ifTrue:[
|
|
660 |
twoPlaneBits at:dstIndex put:bits.
|
|
661 |
dstIndex := dstIndex + 1.
|
|
662 |
bits := 0.
|
|
663 |
bitCount := 0
|
|
664 |
]
|
|
665 |
].
|
|
666 |
(bitCount ~~ 0) ifTrue:[
|
|
667 |
twoPlaneBits at:dstIndex put:bits.
|
|
668 |
dstIndex := dstIndex + 1
|
|
669 |
]
|
|
670 |
]
|
1
|
671 |
].
|
|
672 |
|
|
673 |
f := Form width:w height:h depth:2 on:aDevice.
|
|
674 |
f isNil ifTrue:[^ nil].
|
|
675 |
f initGC.
|
|
676 |
(aDevice blackpixel == 0) ifFalse:[
|
71
|
677 |
"have to invert bits"
|
|
678 |
f function:#copyInverted
|
1
|
679 |
].
|
|
680 |
aDevice drawBits:twoPlaneBits depth:2 width:w height:h
|
71
|
681 |
x:0 y:0
|
|
682 |
into:(f id) x:0 y:0 width:w height:h with:(f gcId).
|
1
|
683 |
^ f
|
|
684 |
!
|
|
685 |
|
|
686 |
paletteImageAsPseudoFormOn:aDevice
|
|
687 |
"return a pseudoForm from the palette picture. The main work is
|
|
688 |
in color reduction, when not all colors can be aquired."
|
|
689 |
|
46
|
690 |
|pseudoBits f gcRound has8BitImage deviceDepth
|
|
691 |
imgMap newImage pxl
|
16
|
692 |
usedColors usageCounts nUsed map mapIndex rMap gMap bMap
|
1
|
693 |
fit scale lastOK error
|
|
694 |
div
|
|
695 |
shift "{Class: SmallInteger }"
|
|
696 |
m "{Class: SmallInteger }" |
|
|
697 |
|
66
|
698 |
'D8Image: allocating colors ...' errorPrintNewline.
|
1
|
699 |
|
|
700 |
"find used colors"
|
|
701 |
|
|
702 |
usedColors := bytes usedValues. "gets us an array filled with used values"
|
71
|
703 |
"(could use bytes asBag)"
|
|
704 |
nUsed := usedColors max + 1.
|
1
|
705 |
|
|
706 |
"sort by usage"
|
|
707 |
usageCounts := bytes usageCounts.
|
|
708 |
usageCounts := usedColors asArray collect:[:clr | usageCounts at:(clr + 1)].
|
|
709 |
usageCounts sort:[:a :b | a > b] with:usedColors.
|
|
710 |
|
|
711 |
"allocate the colors (in order of usage count)"
|
|
712 |
|
|
713 |
rMap := colorMap at:1.
|
|
714 |
gMap := colorMap at:2.
|
|
715 |
bMap := colorMap at:3.
|
|
716 |
|
|
717 |
imgMap := Array new:nUsed.
|
|
718 |
|
|
719 |
"first, we try to get the exact colors"
|
|
720 |
|
|
721 |
shift := (8 - aDevice bitsPerRGB) negated.
|
|
722 |
m := (1 bitShift:(aDevice bitsPerRGB)) - 1.
|
|
723 |
div := m asFloat.
|
|
724 |
|
|
725 |
fit := true.
|
|
726 |
scale := 100.0 / div. "to scale 0..255 into 0.0 .. 100.0"
|
|
727 |
lastOK := 0.
|
|
728 |
usedColors do:[:aColorIndex |
|
71
|
729 |
|devColor color
|
|
730 |
r "{Class: SmallInteger }"
|
|
731 |
g "{Class: SmallInteger }"
|
|
732 |
b "{Class: SmallInteger }"
|
|
733 |
rMask "{Class: SmallInteger }"
|
|
734 |
gMask "{Class: SmallInteger }"
|
|
735 |
bMask "{Class: SmallInteger }"|
|
1
|
736 |
|
71
|
737 |
fit ifTrue:[
|
|
738 |
gMask := bMask := rMask := m.
|
1
|
739 |
|
71
|
740 |
mapIndex := aColorIndex + 1.
|
|
741 |
r := rMap at:mapIndex.
|
|
742 |
g := gMap at:mapIndex.
|
|
743 |
b := bMap at:mapIndex.
|
|
744 |
color := Color red:((r bitShift:shift) bitAnd:rMask) * scale
|
|
745 |
green:((g bitShift:shift) bitAnd:gMask) * scale
|
|
746 |
blue:((b bitShift:shift) bitAnd:bMask) * scale.
|
|
747 |
devColor := color exactOn:aDevice.
|
|
748 |
devColor isNil ifTrue:[
|
|
749 |
"no such color - on the first round, do a GC to flush unused
|
|
750 |
colors - this may help"
|
|
751 |
gcRound == 0 ifTrue:[
|
|
752 |
ObjectMemory scavenge.
|
|
753 |
devColor := color exactOn:aDevice.
|
|
754 |
gcRound := 1
|
|
755 |
].
|
|
756 |
devColor isNil ifTrue:[
|
|
757 |
gcRound == 1 ifTrue:[
|
75
|
758 |
'D8Image: force GC for possible color reclamation.' errorPrintNL.
|
71
|
759 |
ObjectMemory markAndSweep.
|
|
760 |
devColor := color exactOn:aDevice.
|
|
761 |
gcRound := 2
|
|
762 |
]
|
|
763 |
]
|
|
764 |
].
|
|
765 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[
|
|
766 |
imgMap at:mapIndex put:devColor.
|
|
767 |
lastOK := lastOK + 1.
|
|
768 |
] ifFalse:[
|
|
769 |
fit := false
|
|
770 |
]
|
|
771 |
]
|
1
|
772 |
].
|
|
773 |
|
16
|
774 |
"again, this time allow wrong colors (loop while increasing allowed error)"
|
1
|
775 |
|
|
776 |
fit ifFalse:[
|
71
|
777 |
gcRound := 0.
|
|
778 |
error := 10.
|
|
779 |
[fit] whileFalse:[
|
|
780 |
fit := true.
|
|
781 |
usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex |
|
|
782 |
|devColor color
|
|
783 |
r "{Class: SmallInteger }"
|
|
784 |
g "{Class: SmallInteger }"
|
|
785 |
b "{Class: SmallInteger }"
|
|
786 |
rMask "{Class: SmallInteger }"
|
|
787 |
gMask "{Class: SmallInteger }"
|
|
788 |
bMask "{Class: SmallInteger }"|
|
1
|
789 |
|
71
|
790 |
fit ifTrue:[
|
|
791 |
gMask := bMask := rMask := m.
|
1
|
792 |
|
71
|
793 |
mapIndex := aColorIndex + 1.
|
|
794 |
r := rMap at:mapIndex.
|
|
795 |
g := gMap at:mapIndex.
|
|
796 |
b := bMap at:mapIndex.
|
|
797 |
color := Color red:((r bitShift:shift) bitAnd:rMask) * scale
|
|
798 |
green:((g bitShift:shift) bitAnd:gMask) * scale
|
|
799 |
blue:((b bitShift:shift) bitAnd:bMask) * scale.
|
|
800 |
devColor := color nearestOn:aDevice error:error.
|
|
801 |
devColor isNil ifTrue:[
|
|
802 |
"no such color - on the first round, do a GC to flush unused
|
|
803 |
colors - this may help"
|
|
804 |
gcRound == 0 ifTrue:[
|
|
805 |
ObjectMemory scavenge.
|
|
806 |
devColor := color nearestOn:aDevice error:error.
|
|
807 |
gcRound := 1
|
|
808 |
].
|
|
809 |
devColor isNil ifTrue:[
|
|
810 |
gcRound == 1 ifTrue:[
|
81
|
811 |
'D8Image: force GC for possible color reclamation.' errorPrintNL.
|
71
|
812 |
ObjectMemory markAndSweep.
|
|
813 |
devColor := color nearestOn:aDevice error:error.
|
|
814 |
gcRound := 2
|
|
815 |
]
|
|
816 |
]
|
|
817 |
].
|
|
818 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[
|
|
819 |
imgMap at:mapIndex put:devColor.
|
|
820 |
lastOK := lastOK + 1.
|
|
821 |
] ifFalse:[
|
|
822 |
fit := false
|
|
823 |
]
|
|
824 |
].
|
|
825 |
].
|
|
826 |
error := error * 2
|
|
827 |
].
|
1
|
828 |
|
71
|
829 |
error > 100 ifTrue:[
|
|
830 |
'D8Image: not enough colors for a reasonable image' errorPrintNewline
|
|
831 |
] ifFalse:[
|
|
832 |
'D8Image: not enough colors for exact picture' errorPrintNewline.
|
|
833 |
]
|
1
|
834 |
].
|
|
835 |
|
|
836 |
"create translation map"
|
46
|
837 |
map := ByteArray new:256.
|
1
|
838 |
1 to:imgMap size do:[:i |
|
71
|
839 |
(imgMap at:i) notNil ifTrue:[
|
|
840 |
map at:i put:(imgMap at:i) colorId
|
|
841 |
]
|
1
|
842 |
].
|
|
843 |
|
46
|
844 |
deviceDepth := aDevice depth.
|
|
845 |
deviceDepth == 8 ifTrue:[
|
71
|
846 |
has8BitImage := true.
|
46
|
847 |
] ifFalse:[
|
71
|
848 |
has8BitImage := false.
|
|
849 |
aDevice supportedImageFormats do:[:fmt |
|
|
850 |
(fmt at:2) == 8 ifTrue:[
|
|
851 |
has8BitImage := true.
|
|
852 |
]
|
|
853 |
]
|
46
|
854 |
].
|
|
855 |
|
|
856 |
has8BitImage ifTrue:[
|
71
|
857 |
pseudoBits := ByteArray uninitializedNew:(width * height).
|
46
|
858 |
|
71
|
859 |
bytes expandPixels:8 "xlate only"
|
|
860 |
width:width
|
|
861 |
height:height
|
|
862 |
into:pseudoBits
|
|
863 |
mapping:map.
|
46
|
864 |
|
71
|
865 |
map := nil.
|
1
|
866 |
|
71
|
867 |
f := Form width:width height:height depth:deviceDepth on:aDevice.
|
|
868 |
f isNil ifTrue:[^ nil].
|
|
869 |
f colorMap:imgMap.
|
|
870 |
f initGC.
|
|
871 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:deviceDepth
|
|
872 |
width:width height:height
|
|
873 |
x:0 y:0
|
|
874 |
into:(f id) x:0 y:0
|
|
875 |
width:width height:height with:(f gcId).
|
|
876 |
^ f
|
46
|
877 |
].
|
1
|
878 |
|
46
|
879 |
"
|
|
880 |
slow fall back: convert into appropriate depth image,
|
|
881 |
by looping over each pixel individually
|
|
882 |
"
|
|
883 |
newImage := (Image implementorForDepth:deviceDepth) new.
|
|
884 |
newImage width:width.
|
|
885 |
newImage height:height.
|
|
886 |
newImage bits:(ByteArray uninitializedNew:(height * newImage bytesPerRow)).
|
|
887 |
|
|
888 |
0 to:height-1 do:[:row |
|
71
|
889 |
0 to:width-1 do:[:col |
|
|
890 |
pxl := self valueAtX:col y:row.
|
|
891 |
newImage atX:col y:row putValue:(map at:pxl)
|
|
892 |
]
|
46
|
893 |
].
|
|
894 |
|
|
895 |
f := Form width:width height:height depth:deviceDepth on:aDevice.
|
1
|
896 |
f isNil ifTrue:[^ nil].
|
|
897 |
f colorMap:imgMap.
|
|
898 |
f initGC.
|
46
|
899 |
|
|
900 |
aDevice drawBits:(newImage bits) depth:deviceDepth width:width height:height
|
71
|
901 |
x:0 y:0
|
|
902 |
into:(f id) x:0 y:0
|
|
903 |
width:width height:height with:(f gcId).
|
46
|
904 |
|
1
|
905 |
^ f
|
|
906 |
!
|
|
907 |
|
|
908 |
paletteImageAsGreyFormOn:aDevice
|
|
909 |
"return an 8-bit greyForm from the 8-bit palette picture;
|
|
910 |
only a translation has to be done"
|
|
911 |
|
|
912 |
|greyBits f v
|
|
913 |
nColors "{ Class: SmallInteger }"
|
|
914 |
r "{ Class: SmallInteger }"
|
|
915 |
g "{ Class: SmallInteger }"
|
|
916 |
b "{ Class: SmallInteger }"
|
|
917 |
map rMap gMap bMap|
|
|
918 |
|
|
919 |
greyBits := ByteArray uninitializedNew:(width * height).
|
|
920 |
|
|
921 |
rMap := colorMap at:1.
|
|
922 |
gMap := colorMap at:2.
|
|
923 |
bMap := colorMap at:3.
|
|
924 |
nColors := rMap size.
|
|
925 |
map := ByteArray uninitializedNew:256.
|
|
926 |
|
|
927 |
1 to:nColors do:[:i |
|
71
|
928 |
r := rMap at:i.
|
|
929 |
r notNil ifTrue:[
|
|
930 |
g := gMap at:i.
|
|
931 |
b := bMap at:i.
|
|
932 |
v := ((3 * r) + (6 * g) + (1 * b)) // 10.
|
|
933 |
map at:i put:v
|
|
934 |
]
|
1
|
935 |
].
|
|
936 |
bytes expandPixels:8 "xlate only"
|
71
|
937 |
width:width
|
|
938 |
height:height
|
|
939 |
into:greyBits
|
|
940 |
mapping:map.
|
1
|
941 |
|
|
942 |
f := Form width:width height:height depth:8 on:aDevice.
|
|
943 |
f isNil ifTrue:[^ nil].
|
|
944 |
f initGC.
|
|
945 |
aDevice drawBits:greyBits depth:8 width:width height:height
|
71
|
946 |
x:0 y:0
|
|
947 |
into:(f id) x:0 y:0
|
|
948 |
width:width height:height with:(f gcId).
|
1
|
949 |
^ f
|
|
950 |
!
|
|
951 |
|
|
952 |
paletteImageAsPatternDitheredGreyFormOn:aDevice
|
|
953 |
"return a dithered greyForm from the 8-bit palette picture.
|
|
954 |
works for any destination depth - but is very slow for some."
|
|
955 |
|
|
956 |
|f
|
|
957 |
r g b
|
|
958 |
map rMap gMap bMap
|
|
959 |
run last ditherColors first delta
|
|
960 |
clr depth
|
|
961 |
nDither "{Class: SmallInteger }"
|
|
962 |
nColors "{Class: SmallInteger }"
|
|
963 |
w "{Class: SmallInteger }"
|
|
964 |
h "{Class: SmallInteger }"
|
|
965 |
v "{Class: SmallInteger }"
|
|
966 |
srcIndex "{Class: SmallInteger }"|
|
|
967 |
|
|
968 |
|
|
969 |
Transcript showCr:'dithering ..'. Transcript endEntry.
|
|
970 |
|
|
971 |
nDither := NumberOfDitherColors.
|
|
972 |
ditherColors := Array new:nDither.
|
|
973 |
|
|
974 |
first := (100 / nDither / 2).
|
|
975 |
delta := 100 / nDither.
|
|
976 |
0 to:nDither-1 do:[:i |
|
71
|
977 |
ditherColors at:i+1 put:(Color grey:(i * delta + first)).
|
1
|
978 |
].
|
|
979 |
|
|
980 |
rMap := colorMap at:1.
|
|
981 |
gMap := colorMap at:2.
|
|
982 |
bMap := colorMap at:3.
|
|
983 |
nColors := rMap size.
|
|
984 |
map := Array new:nColors.
|
|
985 |
|
|
986 |
1 to:nColors do:[:i |
|
71
|
987 |
r := rMap at:i.
|
|
988 |
r notNil ifTrue:[
|
|
989 |
g := gMap at:i.
|
|
990 |
b := bMap at:i.
|
|
991 |
v := ((3 * r) + (6 * g) + (1 * b)) asInteger.
|
|
992 |
" v is now in the range 0 .. 2550 "
|
|
993 |
v := (v * (nDither - 1) // 2550) rounded.
|
|
994 |
" v is now 0 .. nDither-1 "
|
|
995 |
map at:i put:(ditherColors at:(v + 1))
|
|
996 |
]
|
1
|
997 |
].
|
|
998 |
|
|
999 |
"tuning - code below is so slooow"
|
|
1000 |
"get the patterns, fill form bytes here"
|
|
1001 |
|
|
1002 |
depth := aDevice depth.
|
|
1003 |
depth == 1 ifTrue:[
|
71
|
1004 |
^ self dither1PlaneUsingMap:map on:aDevice
|
1
|
1005 |
].
|
|
1006 |
depth == 2 ifTrue:[
|
71
|
1007 |
^ self dither2PlaneUsingMap:map on:aDevice
|
1
|
1008 |
].
|
|
1009 |
|
|
1010 |
"draw each pixel using dither color (let others do the dithering)
|
|
1011 |
although the code is simple, its very slow"
|
|
1012 |
|
|
1013 |
w := width.
|
|
1014 |
h := height.
|
|
1015 |
|
|
1016 |
f := Form width:w height:h depth:depth on:aDevice.
|
|
1017 |
f isNil ifTrue:[^ nil].
|
|
1018 |
f initGC.
|
|
1019 |
|
|
1020 |
srcIndex := 1.
|
|
1021 |
1 to:h do:[:dstY |
|
71
|
1022 |
run := 0.
|
|
1023 |
last := nil.
|
|
1024 |
1 to:w do:[:dstX |
|
|
1025 |
v := bytes at:srcIndex.
|
|
1026 |
srcIndex := srcIndex + 1.
|
|
1027 |
clr := map at:(v + 1).
|
1
|
1028 |
|
71
|
1029 |
clr == last ifTrue:[
|
|
1030 |
run := run + 1
|
|
1031 |
] ifFalse:[
|
|
1032 |
(run ~~ 0) ifTrue:[
|
|
1033 |
f fillRectangleX:dstX-run-1 y:dstY-1 width:run height:1.
|
|
1034 |
].
|
|
1035 |
run := 1.
|
|
1036 |
f paint:clr.
|
|
1037 |
last := clr
|
|
1038 |
].
|
|
1039 |
].
|
|
1040 |
f fillRectangleX:w-run y:dstY-1 width:run height:1.
|
1
|
1041 |
].
|
|
1042 |
^ f
|
|
1043 |
! !
|
|
1044 |
|
|
1045 |
!Depth8Image methodsFor:'converting greyscale images'!
|
|
1046 |
|
|
1047 |
greyImageAsMonoFormOn:aDevice
|
|
1048 |
"return a (thresholded) monochrome Form from the picture."
|
|
1049 |
|
|
1050 |
|bytesPerRow
|
|
1051 |
bytesPerMonoRow monoData
|
|
1052 |
pixel "{Class: SmallInteger }"
|
|
1053 |
byte "{Class: SmallInteger }"
|
|
1054 |
mask "{Class: SmallInteger }"
|
|
1055 |
srcIndex "{Class: SmallInteger }"
|
|
1056 |
dstIndex "{Class: SmallInteger }"
|
|
1057 |
nextSrc "{Class: SmallInteger }"
|
|
1058 |
nextDst "{Class: SmallInteger }"
|
|
1059 |
bitNumber "{Class: SmallInteger }"
|
|
1060 |
w "{Class: SmallInteger }"
|
|
1061 |
h "{Class: SmallInteger }" |
|
|
1062 |
|
|
1063 |
w := width.
|
|
1064 |
h := height.
|
|
1065 |
|
|
1066 |
bytesPerRow := self bytesPerRow.
|
|
1067 |
bytesPerMonoRow := w // 8.
|
|
1068 |
((w \\ 8) ~~ 0) ifTrue:[
|
71
|
1069 |
bytesPerMonoRow := bytesPerMonoRow + 1
|
1
|
1070 |
].
|
|
1071 |
monoData := ByteArray uninitializedNew:(bytesPerMonoRow * h).
|
|
1072 |
|
|
1073 |
"8 bit -> 1 bit extract; take most significant bit"
|
|
1074 |
|
|
1075 |
srcIndex := 1.
|
|
1076 |
dstIndex := 1.
|
|
1077 |
1 to:h do:[:count |
|
71
|
1078 |
nextSrc := srcIndex + bytesPerRow.
|
|
1079 |
nextDst := dstIndex + bytesPerMonoRow.
|
|
1080 |
bitNumber := 1.
|
|
1081 |
mask := 2r10000000.
|
|
1082 |
[bitNumber <= w] whileTrue:[
|
|
1083 |
byte := 0.
|
|
1084 |
pixel := bytes at:srcIndex.
|
|
1085 |
srcIndex := srcIndex + 1.
|
|
1086 |
((pixel bitAnd:2r10000000) ~~ 0) ifTrue:[
|
|
1087 |
byte := byte bitOr:mask
|
|
1088 |
].
|
|
1089 |
mask := mask bitShift: -1.
|
|
1090 |
(mask == 0) ifTrue:[
|
|
1091 |
monoData at:dstIndex put:byte.
|
|
1092 |
dstIndex := dstIndex + 1.
|
|
1093 |
byte := 0.
|
|
1094 |
mask := 2r10000000
|
|
1095 |
].
|
|
1096 |
bitNumber := bitNumber + 1
|
|
1097 |
].
|
|
1098 |
(mask == 2r10000000) ifFalse:[
|
|
1099 |
monoData at:dstIndex put:byte.
|
|
1100 |
dstIndex := dstIndex + 1
|
|
1101 |
].
|
|
1102 |
srcIndex := nextSrc.
|
|
1103 |
dstIndex := nextDst
|
1
|
1104 |
].
|
|
1105 |
|
|
1106 |
^ Form width:w height:h fromArray:monoData on:aDevice
|
|
1107 |
!
|
|
1108 |
|
|
1109 |
greyImageAsPatternDitheredGreyFormOn:aDevice
|
|
1110 |
"return a dithered greyForm from the grey picture.
|
|
1111 |
Works for any destination depth.
|
|
1112 |
Used to reduce the number of grey levels."
|
|
1113 |
|
|
1114 |
|f depth
|
|
1115 |
map pixel0bytes pixel1bytes clr ditherPattern
|
|
1116 |
last ditherColors nDither first delta patterns formBytes
|
|
1117 |
w "{Class: SmallInteger }"
|
|
1118 |
h "{Class: SmallInteger }"
|
|
1119 |
v "{Class: SmallInteger }"
|
|
1120 |
run "{Class: SmallInteger }"
|
|
1121 |
srcIndex "{Class: SmallInteger }"
|
|
1122 |
dstIndex "{Class: SmallInteger }"
|
|
1123 |
mask "{Class: SmallInteger }"
|
|
1124 |
outBits "{Class: SmallInteger }"
|
|
1125 |
patternOffset "{Class: SmallInteger }"
|
|
1126 |
patternBytes
|
|
1127 |
patternBits "{Class: SmallInteger }"
|
|
1128 |
index "{Class: SmallInteger }"
|
|
1129 |
p0 "{Class: SmallInteger }"
|
|
1130 |
p1 "{Class: SmallInteger }" |
|
|
1131 |
|
|
1132 |
Transcript showCr:'dithering ..'. Transcript endEntry.
|
|
1133 |
|
|
1134 |
nDither := NumberOfDitherColors.
|
|
1135 |
ditherColors := Array new:nDither.
|
|
1136 |
|
|
1137 |
first := (100 / nDither / 2).
|
|
1138 |
delta := 100 / nDither.
|
|
1139 |
0 to:nDither-1 do:[:i |
|
71
|
1140 |
ditherColors at:i+1 put:(Color grey:(i * delta + first)).
|
1
|
1141 |
].
|
|
1142 |
|
|
1143 |
map := Array new:256.
|
|
1144 |
1 to:256 do:[:i |
|
71
|
1145 |
v := i - 1.
|
|
1146 |
v := (v * (nDither - 1) // 255) rounded.
|
|
1147 |
" v is now 0 .. nDither-1 "
|
|
1148 |
map at:i put:(ditherColors at:(v + 1))
|
1
|
1149 |
].
|
|
1150 |
|
|
1151 |
depth := aDevice depth.
|
|
1152 |
|
|
1153 |
"tuning (general code is too slow)
|
|
1154 |
get the patterns, fill form bytes here"
|
|
1155 |
|
|
1156 |
w := width.
|
|
1157 |
h := height.
|
|
1158 |
|
|
1159 |
depth == 1 ifTrue:[
|
71
|
1160 |
^ self dither1PlaneUsingMap:map on:aDevice.
|
1
|
1161 |
|
|
1162 |
|
71
|
1163 |
formBytes := ByteArray uninitializedNew:(w + 7 // 8) * h.
|
|
1164 |
patterns := Array new:256.
|
|
1165 |
pixel0bytes := ByteArray uninitializedNew:256.
|
|
1166 |
pixel1bytes := ByteArray uninitializedNew:256.
|
1
|
1167 |
|
71
|
1168 |
"extract dither patterns and values to use for 1/0 bits
|
|
1169 |
in those from the dithercolors"
|
1
|
1170 |
|
71
|
1171 |
1 to:256 do:[:i |
|
|
1172 |
clr := (map at:i) on:aDevice.
|
|
1173 |
ditherPattern := clr ditherForm.
|
1
|
1174 |
|
71
|
1175 |
ditherPattern isNil ifTrue:[
|
|
1176 |
patterns at:i put:#[2r11111111
|
|
1177 |
2r11111111
|
|
1178 |
2r11111111
|
|
1179 |
2r11111111
|
|
1180 |
2r11111111
|
|
1181 |
2r11111111
|
|
1182 |
2r11111111
|
|
1183 |
2r11111111].
|
|
1184 |
pixel0bytes at:i put:clr colorId.
|
|
1185 |
pixel1bytes at:i put:clr colorId
|
|
1186 |
] ifFalse:[
|
|
1187 |
patterns at:i put:(ditherPattern bits).
|
|
1188 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId.
|
|
1189 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId.
|
|
1190 |
].
|
|
1191 |
].
|
1
|
1192 |
|
71
|
1193 |
srcIndex := 1.
|
|
1194 |
dstIndex := 1.
|
|
1195 |
mask := 16r80.
|
|
1196 |
outBits := 0.
|
|
1197 |
patternOffset := 1.
|
|
1198 |
1 to:h do:[:dstY |
|
|
1199 |
last := nil.
|
|
1200 |
1 to:w do:[:dstX |
|
|
1201 |
v := bytes at:srcIndex. "pixel value"
|
|
1202 |
srcIndex := srcIndex + 1.
|
1
|
1203 |
|
71
|
1204 |
v == last ifFalse:[
|
|
1205 |
index := v + 1. "index into map"
|
1
|
1206 |
|
71
|
1207 |
patternBytes := patterns at:index. "dither pattern for color"
|
|
1208 |
patternBits := patternBytes at:patternOffset. "dither row"
|
|
1209 |
p0 := pixel0bytes at:index. "value for 0-dither bit"
|
|
1210 |
p1 := pixel1bytes at:index. "value for 1-dither bit"
|
|
1211 |
last := v.
|
|
1212 |
].
|
|
1213 |
outBits := outBits bitShift:1.
|
|
1214 |
(patternBits bitAnd:mask) == 0 ifTrue:[
|
|
1215 |
outBits := outBits bitOr:p0.
|
|
1216 |
] ifFalse:[
|
|
1217 |
outBits := outBits bitOr:p1
|
|
1218 |
].
|
|
1219 |
mask := mask bitShift:-1.
|
|
1220 |
mask == 0 ifTrue:[
|
|
1221 |
mask := 16r80.
|
|
1222 |
formBytes at:dstIndex put:outBits.
|
|
1223 |
dstIndex := dstIndex + 1.
|
|
1224 |
outBits := 0
|
|
1225 |
]
|
|
1226 |
].
|
|
1227 |
mask == 16r80 ifFalse:[
|
|
1228 |
dstIndex := dstIndex + 1.
|
|
1229 |
mask := 16r80.
|
|
1230 |
outBits := 0
|
|
1231 |
].
|
|
1232 |
patternOffset := patternOffset + 1.
|
|
1233 |
patternOffset == 9 ifTrue:[
|
|
1234 |
patternOffset := 1
|
|
1235 |
]
|
|
1236 |
].
|
|
1237 |
f := Form width:w height:h fromArray:formBytes.
|
|
1238 |
^ f
|
1
|
1239 |
].
|
|
1240 |
|
|
1241 |
depth == 2 ifTrue:[
|
71
|
1242 |
^ self dither2PlaneUsingMap:map on:aDevice.
|
1
|
1243 |
|
71
|
1244 |
formBytes := ByteArray uninitializedNew:(w * 2 + 7 // 8) * h.
|
|
1245 |
patterns := Array new:256.
|
|
1246 |
pixel0bytes := ByteArray uninitializedNew:256.
|
|
1247 |
pixel1bytes := ByteArray uninitializedNew:256.
|
1
|
1248 |
|
71
|
1249 |
"extract dither patterns and values to use for 1/o bits
|
|
1250 |
in those from the dithercolors"
|
1
|
1251 |
|
71
|
1252 |
1 to:256 do:[:i |
|
|
1253 |
clr := (map at:i) on:aDevice.
|
|
1254 |
ditherPattern := clr ditherForm.
|
1
|
1255 |
|
71
|
1256 |
ditherPattern isNil ifTrue:[
|
|
1257 |
patterns at:i put:#[2r11111111
|
|
1258 |
2r11111111
|
|
1259 |
2r11111111
|
|
1260 |
2r11111111
|
|
1261 |
2r11111111
|
|
1262 |
2r11111111
|
|
1263 |
2r11111111
|
|
1264 |
2r11111111].
|
|
1265 |
pixel0bytes at:i put:clr colorId.
|
|
1266 |
pixel1bytes at:i put:clr colorId
|
|
1267 |
] ifFalse:[
|
|
1268 |
patterns at:i put:(ditherPattern bits).
|
|
1269 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId.
|
|
1270 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId.
|
|
1271 |
].
|
|
1272 |
].
|
1
|
1273 |
|
71
|
1274 |
srcIndex := 1.
|
|
1275 |
dstIndex := 1.
|
|
1276 |
mask := 16r80.
|
|
1277 |
outBits := 0.
|
|
1278 |
patternOffset := 1.
|
|
1279 |
1 to:h do:[:dstY |
|
|
1280 |
last := nil.
|
|
1281 |
1 to:w do:[:dstX |
|
|
1282 |
v := bytes at:srcIndex. "pixel value"
|
|
1283 |
srcIndex := srcIndex + 1.
|
1
|
1284 |
|
71
|
1285 |
v == last ifFalse:[
|
|
1286 |
index := v + 1. "index into map"
|
1
|
1287 |
|
71
|
1288 |
patternBytes := patterns at:index. "dither pattern for color"
|
|
1289 |
patternBits := patternBytes at:patternOffset. "dither row"
|
|
1290 |
p0 := pixel0bytes at:index. "value for 0-dither bit"
|
|
1291 |
p1 := pixel1bytes at:index. "value for 1-dither bit"
|
|
1292 |
last := v.
|
|
1293 |
].
|
|
1294 |
outBits := outBits bitShift:2.
|
1
|
1295 |
|
71
|
1296 |
(patternBits bitAnd:mask) == 0 ifTrue:[
|
|
1297 |
outBits := outBits bitOr:p0.
|
|
1298 |
] ifFalse:[
|
|
1299 |
outBits := outBits bitOr:p1
|
|
1300 |
].
|
|
1301 |
mask := mask bitShift:-1.
|
|
1302 |
mask == 16r08 ifTrue:[
|
|
1303 |
formBytes at:dstIndex put:outBits.
|
|
1304 |
dstIndex := dstIndex + 1.
|
|
1305 |
outBits := 0
|
|
1306 |
] ifFalse:[
|
|
1307 |
mask == 0 ifTrue:[
|
|
1308 |
mask := 16r80.
|
|
1309 |
formBytes at:dstIndex put:outBits.
|
|
1310 |
dstIndex := dstIndex + 1.
|
|
1311 |
outBits := 0
|
|
1312 |
]
|
|
1313 |
]
|
|
1314 |
].
|
|
1315 |
((mask == 16r80) or:[mask == 16r08]) ifFalse:[
|
|
1316 |
formBytes at:dstIndex put:outBits.
|
|
1317 |
dstIndex := dstIndex + 1.
|
|
1318 |
mask := 16r80.
|
|
1319 |
outBits := 0
|
|
1320 |
].
|
|
1321 |
patternOffset := patternOffset + 1.
|
|
1322 |
patternOffset == 9 ifTrue:[
|
|
1323 |
patternOffset := 1
|
|
1324 |
]
|
|
1325 |
].
|
1
|
1326 |
|
71
|
1327 |
f := Form width:w height:h depth:2.
|
|
1328 |
f initGC.
|
|
1329 |
f device drawBits:formBytes depth:2
|
|
1330 |
width:w height:h x:0 y:0
|
|
1331 |
into:f id x:0 y:0 width:w height:h with:f gcId.
|
|
1332 |
^ f
|
1
|
1333 |
].
|
|
1334 |
|
|
1335 |
"draw each pixel using dither color (let others do the dithering)
|
|
1336 |
although the code is simple, its very slow"
|
|
1337 |
|
|
1338 |
f := Form width:width height:height depth:depth on:aDevice.
|
|
1339 |
f isNil ifTrue:[^ nil].
|
|
1340 |
f initGC.
|
|
1341 |
|
|
1342 |
srcIndex := 1.
|
|
1343 |
1 to:h do:[:dstY |
|
71
|
1344 |
run := 0.
|
|
1345 |
last := nil.
|
|
1346 |
1 to:w do:[:dstX |
|
|
1347 |
|clr v|
|
|
1348 |
v := bytes at:srcIndex.
|
|
1349 |
srcIndex := srcIndex + 1.
|
|
1350 |
clr := map at:(v + 1).
|
1
|
1351 |
|
71
|
1352 |
clr == last ifTrue:[
|
|
1353 |
run := run + 1
|
|
1354 |
] ifFalse:[
|
|
1355 |
(run ~~ 0) ifTrue:[
|
|
1356 |
f fillRectangleX:dstX-run-1 y:dstY-1 width:run height:1.
|
|
1357 |
].
|
|
1358 |
run := 1.
|
|
1359 |
f paint:clr.
|
|
1360 |
last := clr
|
|
1361 |
].
|
|
1362 |
].
|
|
1363 |
f fillRectangleX:width-run y:dstY-1 width:run height:1.
|
1
|
1364 |
].
|
|
1365 |
^ f
|
|
1366 |
! !
|
|
1367 |
|
12
|
1368 |
!Depth8Image methodsFor:'image manipulations'!
|
|
1369 |
|
|
1370 |
flipHorizontal
|
|
1371 |
"inplace horizontal flip"
|
|
1372 |
|
|
1373 |
|index "{Class: SmallInteger }"
|
|
1374 |
h "{Class: SmallInteger }"
|
|
1375 |
w "{Class: SmallInteger }"
|
|
1376 |
buffer |
|
|
1377 |
|
|
1378 |
w := width - 1.
|
|
1379 |
h := height - 1.
|
|
1380 |
|
|
1381 |
buffer := ByteArray new:width.
|
|
1382 |
|
|
1383 |
index := 1.
|
|
1384 |
0 to:h do:[:row |
|
71
|
1385 |
buffer replaceFrom:1 to:width with:bytes startingAt:index.
|
|
1386 |
buffer reverse.
|
|
1387 |
bytes replaceFrom:index to:index+w with:buffer startingAt:1.
|
|
1388 |
index := index + w + 1.
|
12
|
1389 |
].
|
|
1390 |
"flush device info"
|
|
1391 |
self restored
|
|
1392 |
!
|
1
|
1393 |
|
|
1394 |
magnifyRowFrom:srcBytes offset:srcStart
|
71
|
1395 |
into:dstBytes offset:dstStart factor:mX
|
1
|
1396 |
|
81
|
1397 |
"magnify a single pixel row - can only magnify by integer factors.
|
|
1398 |
Especially tuned for factors 2,3 and 4."
|
1
|
1399 |
|
|
1400 |
%{
|
|
1401 |
REGISTER unsigned char *srcP, *dstP;
|
|
1402 |
REGISTER unsigned char _byte;
|
|
1403 |
int _mag;
|
|
1404 |
REGISTER int i;
|
|
1405 |
int _pixels;
|
|
1406 |
|
|
1407 |
if (_isSmallInteger(srcStart) && _isSmallInteger(dstStart)
|
|
1408 |
&& _isSmallInteger(_INST(width)) && _isSmallInteger(mX)
|
35
|
1409 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) {
|
71
|
1410 |
_mag = _intVal(mX);
|
|
1411 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
|
|
1412 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
|
|
1413 |
_pixels = _intVal(_INST(width));
|
1
|
1414 |
|
71
|
1415 |
switch (_mag) {
|
|
1416 |
case 1:
|
|
1417 |
break;
|
1
|
1418 |
|
71
|
1419 |
case 2:
|
|
1420 |
/* special code for common case */
|
|
1421 |
while (_pixels--) {
|
|
1422 |
_byte = *srcP++;
|
|
1423 |
*dstP++ = _byte;
|
|
1424 |
*dstP++ = _byte;
|
|
1425 |
}
|
|
1426 |
break;
|
1
|
1427 |
|
71
|
1428 |
case 3:
|
|
1429 |
/* special code for common case */
|
|
1430 |
while (_pixels--) {
|
|
1431 |
_byte = *srcP++;
|
|
1432 |
*dstP++ = _byte;
|
|
1433 |
*dstP++ = _byte;
|
|
1434 |
*dstP++ = _byte;
|
|
1435 |
}
|
|
1436 |
break;
|
1
|
1437 |
|
71
|
1438 |
case 4:
|
|
1439 |
/* special code for common case */
|
|
1440 |
while (_pixels--) {
|
|
1441 |
_byte = *srcP++;
|
|
1442 |
*dstP++ = _byte;
|
|
1443 |
*dstP++ = _byte;
|
|
1444 |
*dstP++ = _byte;
|
|
1445 |
*dstP++ = _byte;
|
|
1446 |
}
|
|
1447 |
break;
|
1
|
1448 |
|
71
|
1449 |
default:
|
|
1450 |
while (_pixels--) {
|
|
1451 |
_byte = *srcP++;
|
|
1452 |
for (i=_mag; i>0; i--)
|
|
1453 |
*dstP++ = _byte;
|
|
1454 |
}
|
|
1455 |
break;
|
|
1456 |
}
|
|
1457 |
RETURN (self);
|
1
|
1458 |
}
|
|
1459 |
%}
|
|
1460 |
.
|
|
1461 |
self primitiveFailed
|
|
1462 |
!
|
|
1463 |
|
|
1464 |
hardMagnifyBy:extent
|
|
1465 |
"return a new image magnified by extent, aPoint.
|
12
|
1466 |
This is the general magnification method, handling non-integral values"
|
1
|
1467 |
|
|
1468 |
|mX mY
|
|
1469 |
newWidth "{ Class: SmallInteger }"
|
|
1470 |
newHeight "{ Class: SmallInteger }"
|
|
1471 |
w "{ Class: SmallInteger }"
|
|
1472 |
h "{ Class: SmallInteger }"
|
|
1473 |
newImage newBytes
|
|
1474 |
value "{ Class: SmallInteger }"
|
|
1475 |
srcRowIdx "{ Class: SmallInteger }"
|
|
1476 |
srcIndex "{ Class: SmallInteger }"
|
|
1477 |
dstIndex "{ Class: SmallInteger }"|
|
|
1478 |
|
|
1479 |
mX := extent x.
|
|
1480 |
mY := extent y.
|
|
1481 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil].
|
|
1482 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self].
|
|
1483 |
|
|
1484 |
newWidth := (width * mX) truncated.
|
|
1485 |
newHeight := (height * mY) truncated.
|
|
1486 |
|
|
1487 |
newBytes := ByteArray uninitializedNew:(newWidth * newHeight).
|
|
1488 |
|
|
1489 |
newImage := self species new.
|
|
1490 |
newImage bits:newBytes.
|
|
1491 |
newImage width:newWidth.
|
|
1492 |
newImage height:newHeight.
|
|
1493 |
newImage photometric:photometric.
|
|
1494 |
newImage samplesPerPixel:samplesPerPixel.
|
|
1495 |
newImage bitsPerSample:#(8).
|
|
1496 |
newImage colorMap:colorMap copy.
|
|
1497 |
|
|
1498 |
"walk over destination image fetching pixels from source image"
|
|
1499 |
|
|
1500 |
mY := mY asFloat.
|
|
1501 |
mX := mX asFloat.
|
|
1502 |
%{
|
|
1503 |
unsigned char *_dstP = _ByteArrayInstPtr(newBytes)->ba_element;
|
|
1504 |
unsigned char *_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element;
|
|
1505 |
unsigned char *_srcRowP;
|
|
1506 |
int _width = _intVal(_INST(width));
|
|
1507 |
int _w = _intVal(newWidth) - 1;
|
|
1508 |
int _h = _intVal(newHeight) - 1;
|
|
1509 |
int _row, _col;
|
|
1510 |
double _mX = _floatVal(mX);
|
|
1511 |
double _mY = _floatVal(mY);
|
|
1512 |
|
|
1513 |
for (_row = 0; _row <= _h; _row++) {
|
71
|
1514 |
_srcRowP = _srcP + (_width * (int)((double)_row / _mY));
|
|
1515 |
for (_col = 0; _col <= _w; _col++) {
|
|
1516 |
*_dstP++ = _srcRowP[(int)((double)_col / _mX)];
|
|
1517 |
}
|
1
|
1518 |
}
|
|
1519 |
%}
|
|
1520 |
.
|
81
|
1521 |
" the above C-code is equivalent to:
|
|
1522 |
|
1
|
1523 |
dstIndex := 1.
|
|
1524 |
w := newWidth - 1.
|
|
1525 |
h := newHeight - 1.
|
|
1526 |
0 to:h do:[:row |
|
71
|
1527 |
srcRowIdx := (width * (row // mY)) + 1.
|
|
1528 |
0 to:w do:[:col |
|
|
1529 |
srcIndex := srcRowIdx + (col // mX).
|
|
1530 |
value := bytes at:srcIndex.
|
|
1531 |
newBytes at:dstIndex put:value.
|
|
1532 |
dstIndex := dstIndex + 1
|
|
1533 |
]
|
1
|
1534 |
].
|
|
1535 |
"
|
|
1536 |
|
|
1537 |
^ newImage
|
|
1538 |
! !
|