author | Claus Gittinger <cg@exept.de> |
Mon, 10 Jun 1996 19:25:04 +0200 | |
changeset 813 | 8bc17dba7a02 |
parent 810 | 9789d836dbc4 |
child 816 | 6576c8242e5d |
permissions | -rw-r--r-- |
5 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
71 | 3 |
All Rights Reserved |
5 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1 | 12 |
|
13 |
Image subclass:#Depth8Image |
|
579 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images' |
|
1 | 18 |
! |
19 |
||
46 | 20 |
!Depth8Image class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
71 | 25 |
All Rights Reserved |
5 | 26 |
|
46 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
5 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
38 |
this class represents 256-color (8 bit / pixel) images (palette, greyscale ...). |
|
39 |
It mainly consists of methods already implemented in Image, |
|
40 |
reimplemented here for more performance. |
|
611 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
44 |
||
45 |
[see also:] |
|
46 |
Depth1Image Depth2Image Depth4Image Depth16Image Depth24Image |
|
47 |
ImageReader |
|
46 | 48 |
" |
49 |
! ! |
|
5 | 50 |
|
89 | 51 |
!Depth8Image class methodsFor:'queries'! |
52 |
||
53 |
imageDepth |
|
579 | 54 |
"return the depth of images represented by instances of |
55 |
this class - here we return 8" |
|
1 | 56 |
|
57 |
^ 8 |
|
58 |
||
579 | 59 |
"Modified: 20.4.1996 / 23:40:22 / cg" |
81 | 60 |
! ! |
61 |
||
62 |
!Depth8Image methodsFor:'accessing'! |
|
1 | 63 |
|
64 |
atX:x y:y |
|
65 |
"retrieve a pixel at x/y; return a color. |
|
66 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
67 |
x = width-1, y=height-1 for lower right pixel" |
|
68 |
||
38 | 69 |
|value "{ Class: SmallInteger }" |
70 |
index "{ Class: SmallInteger }"| |
|
1 | 71 |
|
72 |
index := (width * y) + 1 + x. |
|
73 |
value := bytes at:index. |
|
74 |
||
75 |
photometric == #whiteIs0 ifTrue:[ |
|
746 | 76 |
^ Color gray:100 - (100 / 255 * value) |
1 | 77 |
]. |
78 |
photometric == #blackIs0 ifTrue:[ |
|
746 | 79 |
^ Color gray:(100 / 255 * value) |
1 | 80 |
]. |
81 |
photometric ~~ #palette ifTrue:[ |
|
802 | 82 |
^ self colorFromValue:value |
1 | 83 |
]. |
84 |
index := value + 1. |
|
89 | 85 |
^ colorMap at:index |
746 | 86 |
|
802 | 87 |
"Modified: 8.6.1996 / 10:52:48 / cg" |
1 | 88 |
! |
89 |
||
90 |
atX:x y:y putValue:aPixelValue |
|
91 |
"set the pixel at x/y to aPixelValue. |
|
92 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
93 |
x = width-1, y=height-1 for lower right pixel" |
|
94 |
||
95 |
|index "{ Class: SmallInteger }"| |
|
96 |
||
97 |
index := (width * y) + 1 + x. |
|
98 |
bytes at:index put:aPixelValue. |
|
99 |
! |
|
100 |
||
579 | 101 |
valueAtX:x y:y |
102 |
"retrieve a pixel at x/y; return a pixelValue. |
|
103 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
104 |
x = width-1, y=height-1 for lower right pixel" |
|
105 |
||
106 |
|index "{ Class: SmallInteger }"| |
|
107 |
||
108 |
index := (width * y) + 1 + x. |
|
109 |
^ bytes at:index. |
|
110 |
! ! |
|
111 |
||
1 | 112 |
!Depth8Image methodsFor:'converting palette images'! |
113 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
114 |
asGrayFormOn:aDevice |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
115 |
"get a gray device form. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
116 |
Redefined, since we can do it with simple translate, |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
117 |
if the depth matches my depth." |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
118 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
119 |
(aDevice visualType == #StaticGray) ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
120 |
(aDevice depth == 8) ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
121 |
^ self asGray8FormOn:aDevice |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
122 |
]. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
123 |
]. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
124 |
^ super asGrayFormOn:aDevice |
1 | 125 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
126 |
"Created: 10.6.1996 / 18:51:19 / cg" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
127 |
"Modified: 10.6.1996 / 18:54:36 / cg" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
128 |
! |
1 | 129 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
130 |
asGrey8FormOn:aDevice |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
131 |
"return an 8-bit greyForm from the 8-bit receiver image. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
132 |
Redefined, since only a translation has to be done here." |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
133 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
134 |
|greyBits map |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
135 |
mapSize "{ Class: SmallInteger }"| |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
136 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
137 |
greyBits := ByteArray uninitializedNew:(width * height). |
1 | 138 |
|
139 |
map := ByteArray uninitializedNew:256. |
|
140 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
141 |
1 to:256 do:[:i | |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
142 |
map at:i put:((self colorFromValue:(i-1)) brightness * 255) rounded |
1 | 143 |
]. |
89 | 144 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
145 |
"/ mapSize := colorMap size. |
89 | 146 |
"/ |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
147 |
"/ 1 to:mapSize do:[:i | |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
148 |
"/ map at:i put:((colorMap at:i) brightness * 255) rounded |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
149 |
"/ ]. |
1 | 150 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
151 |
bytes expandPixels:8 "xlate only" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
152 |
width:width |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
153 |
height:height |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
154 |
into:greyBits |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
155 |
mapping:map. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
156 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
157 |
^ self makeDevicePixmapOn:aDevice depth:8 fromArray:greyBits |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
158 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
159 |
"Created: 10.6.1996 / 18:51:50 / cg" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
160 |
"Modified: 10.6.1996 / 18:54:04 / cg" |
1 | 161 |
! |
162 |
||
89 | 163 |
paletteImageAsDitheredPseudoFormOn:aDevice |
164 |
"return a dithered pseudoForm from the palette picture. Depend |
|
165 |
on dither colors being preallocated (see Color>>getColors*)" |
|
166 |
||
167 |
^ self paletteImageAsDitheredPseudoFormOn:aDevice |
|
168 |
colors:Color fixColors |
|
169 |
nRed:Color numFixRed |
|
170 |
nGreen:Color numFixGreen |
|
171 |
nBlue:Color numFixBlue |
|
172 |
! |
|
173 |
||
174 |
paletteImageAsDitheredPseudoFormOn:aDevice colors:fixColors nRed:nRed nGreen:nGreen nBlue:nBlue |
|
175 |
"return a dithered pseudoForm from the palette picture. |
|
176 |
Use the colors in the fixColors array, which must be fixR x fixG x fixB |
|
177 |
colors assigned to aDevice, such as the preallocated colors of the |
|
178 |
Color class. |
|
179 |
By passing the ditherColors as extra array, this method can |
|
180 |
also be used to dither an 8bit image into a smaller number of colors, |
|
181 |
for example to create Depth4Images." |
|
182 |
||
183 |
"the code below is a q&d hack - it needs a rewrite to use a |
|
184 |
floyd-steinberg dither (currently, the error is only forwarded |
|
185 |
to the next pixel on the right, which seems ok for photograph-like |
|
186 |
images, but leads to more artifacts with equal colored areas). |
|
187 |
" |
|
188 |
||
189 |
|pseudoBits f has8BitImage deviceDepth |
|
156 | 190 |
rgbBytes |
89 | 191 |
w "{Class: SmallInteger }" |
192 |
h "{Class: SmallInteger }" |
|
193 |
index "{Class: SmallInteger }" |
|
194 |
fixR "{Class: SmallInteger }" |
|
195 |
fixG "{Class: SmallInteger }" |
|
196 |
fixB "{Class: SmallInteger }" |
|
156 | 197 |
fixIds failed map colorMapSize |
198 |
error| |
|
89 | 199 |
|
200 |
aDevice ~~ Display ifTrue:[^ nil]. |
|
201 |
||
202 |
fixR := nRed. |
|
203 |
fixR == 0 ifTrue:[ ^ nil]. |
|
204 |
fixG := nGreen. |
|
205 |
fixG == 0 ifTrue:[ ^ nil]. |
|
206 |
fixB := nBlue. |
|
207 |
fixB == 0 ifTrue:[ ^ nil]. |
|
208 |
"/ simple check |
|
209 |
(fixR * fixG * fixB) ~~ fixColors size ifTrue:[ |
|
210 |
self error:'invalid color array passed'. |
|
211 |
^ nil |
|
212 |
]. |
|
213 |
fixIds := (fixColors asArray collect:[:clr | clr colorId]) asByteArray. |
|
214 |
||
215 |
deviceDepth := aDevice depth. |
|
216 |
deviceDepth == 8 ifTrue:[ |
|
217 |
has8BitImage := true. |
|
218 |
] ifFalse:[ |
|
219 |
has8BitImage := false. |
|
220 |
aDevice supportedImageFormats do:[:fmt | |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
221 |
(fmt at:#bitsPerPixel) == 8 ifTrue:[ |
89 | 222 |
has8BitImage := true. |
223 |
] |
|
224 |
] |
|
225 |
]. |
|
226 |
has8BitImage ifFalse:[^ nil]. |
|
227 |
||
151 | 228 |
'D8IMAGE: dithering ...' infoPrintNL. |
89 | 229 |
|
230 |
" |
|
231 |
collect color components as integer values |
|
232 |
(code below uses components percent * 2.55 asInteger everywhere, to avoid |
|
233 |
float arithmetic, rounding etc. Thus, the range is 0..255 here) |
|
234 |
" |
|
156 | 235 |
colorMapSize := colorMap size. |
236 |
rgbBytes := ByteArray uninitializedNew:colorMapSize * 3. |
|
237 |
||
238 |
index := 1. |
|
239 |
1 to:colorMapSize do:[:i | |
|
89 | 240 |
|clr| |
156 | 241 |
|
89 | 242 |
clr := colorMap at:i. |
156 | 243 |
rgbBytes at:index put:(clr red * 2.55) asInteger. |
244 |
rgbBytes at:index+1 put:(clr green * 2.55) asInteger. |
|
245 |
rgbBytes at:index+2 put:(clr blue * 2.55) asInteger. |
|
246 |
index := index + 3. |
|
89 | 247 |
]. |
248 |
||
249 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
|
250 |
||
156 | 251 |
w := width + 2. |
252 |
error := ByteArray new:w*(3*2). |
|
253 |
||
89 | 254 |
w := width. |
255 |
h := height. |
|
256 |
||
257 |
failed := true. |
|
258 |
||
259 |
%{ |
|
156 | 260 |
/* |
261 |
* # define FLOYD_STEINBERG |
|
262 |
*/ |
|
89 | 263 |
int __x, __y; |
264 |
int __eR, __eG, __eB; |
|
265 |
unsigned char *srcP, *dstP; |
|
156 | 266 |
unsigned char *rgbP; |
89 | 267 |
unsigned char *idP; |
156 | 268 |
short *errP, *eP; |
89 | 269 |
int __fR, __fG, __fB; |
270 |
int iR, iG, iB; |
|
271 |
int idx; |
|
272 |
||
273 |
if (__isByteArray(_INST(bytes)) |
|
274 |
&& __isByteArray(pseudoBits) |
|
156 | 275 |
&& __isByteArray(rgbBytes) |
89 | 276 |
&& __isByteArray(fixIds) |
156 | 277 |
&& __isByteArray(error) |
97 | 278 |
&& __bothSmallInteger(fixR, fixG) |
279 |
&& __isSmallInteger(fixB)) { |
|
89 | 280 |
failed = false; |
281 |
||
282 |
srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
283 |
dstP = _ByteArrayInstPtr(pseudoBits)->ba_element; |
|
156 | 284 |
rgbP = _ByteArrayInstPtr(rgbBytes)->ba_element; |
89 | 285 |
idP = _ByteArrayInstPtr(fixIds)->ba_element; |
156 | 286 |
errP = (short *) _ByteArrayInstPtr(error)->ba_element; |
89 | 287 |
__fR = _intVal(fixR)-1; |
288 |
__fG = _intVal(fixG)-1; |
|
289 |
__fB = _intVal(fixB)-1; |
|
290 |
||
156 | 291 |
#ifdef FLOYD_STEINBERG |
292 |
/* |
|
293 |
* clear error accumulator |
|
294 |
*/ |
|
295 |
eP = errP; |
|
296 |
for (__x=_intVal(w)+1; __x>=0; __x--) { |
|
297 |
eP[0] = 0; eP[1] = 0; eP[2] = 0; |
|
298 |
eP += 3; |
|
299 |
} |
|
300 |
#endif |
|
89 | 301 |
for (__y=_intVal(h); __y>0; __y--) { |
302 |
__eR = __eG = __eB = 0; |
|
156 | 303 |
#ifdef FLOYD_STEINBERG |
304 |
eP = &(errP[3]); |
|
305 |
__eR += eP[0]; |
|
306 |
__eG += eP[1]; |
|
307 |
__eB += eP[2]; |
|
308 |
#endif |
|
309 |
||
89 | 310 |
for (__x=_intVal(w); __x>0; __x--) { |
311 |
int __want; |
|
155 | 312 |
int pix; |
313 |
int __wantR, __wantG, __wantB; |
|
156 | 314 |
int idx; |
315 |
int tR, tG, tB; |
|
316 |
int nR, nG, nB; |
|
89 | 317 |
|
318 |
pix = *srcP++; |
|
319 |
||
320 |
/* |
|
321 |
* wR, wG and wB is the wanted r/g/b value; |
|
322 |
* compute the index into the dId table .. |
|
323 |
* values: 0..255; scale to 0..fR-1, 0..fG-1, 0..fB-1 |
|
324 |
* |
|
325 |
* bad kludge: knows how to index into FixColor table |
|
326 |
*/ |
|
156 | 327 |
idx = pix+pix+pix; /* pix * 3 */ |
328 |
||
329 |
__wantR = rgbP[idx] + __eR; |
|
330 |
__wantG = rgbP[idx+1] + __eG; |
|
331 |
__wantB = rgbP[idx+2] + __eB; |
|
89 | 332 |
|
333 |
if (__wantR > 255) __want = 255; |
|
334 |
else if (__wantR < 0) __want = 0; |
|
335 |
else __want = __wantR; |
|
336 |
||
155 | 337 |
iR = (__want * __fR + 128) / 255; /* red index rounded */ |
89 | 338 |
idx = iR * (__fG+1); |
339 |
||
340 |
if (__wantG > 255) __want = 255; |
|
341 |
else if (__wantG < 0) __want = 0; |
|
342 |
else __want = __wantG; |
|
343 |
||
155 | 344 |
iG = (__want * __fG + 128) / 255; /* green index rounded */ |
89 | 345 |
idx = (idx + iG) * (__fB+1); |
346 |
||
347 |
if (__wantB > 255) __want = 255; |
|
348 |
else if (__wantB < 0) __want = 0; |
|
349 |
else __want = __wantB; |
|
350 |
||
155 | 351 |
iB = (__want * __fB + 128) / 255; /* blue index rounded */ |
89 | 352 |
idx = idx + iB; |
353 |
||
354 |
/* |
|
155 | 355 |
* store the corresponding dither colors colorId |
89 | 356 |
*/ |
357 |
*dstP++ = idP[idx]; |
|
358 |
||
359 |
/* |
|
360 |
* the new error: |
|
361 |
*/ |
|
156 | 362 |
__eR = __wantR - (iR * 255 / __fR); |
363 |
__eG = __wantG - (iG * 255 / __fG); |
|
364 |
__eB = __wantB - (iB * 255 / __fB); |
|
365 |
#ifdef FLOYD_STEINBERG |
|
366 |
/* |
|
367 |
* distribute the error |
|
368 |
*/ |
|
369 |
tR = __eR >> 4; /* 16th of error */ |
|
370 |
tG = __eG >> 4; |
|
371 |
tB = __eB >> 4; |
|
372 |
||
373 |
nR = eP[3] + (tR * 7);/* from accu: error for (x+1 / y) */ |
|
374 |
nG = eP[4] + (tG * 7);/* plus 7/16'th of this error */ |
|
375 |
nB = eP[5] + (tB * 7); |
|
376 |
||
377 |
eP[0] = tR*5; /* 5/16th for (x / y+1) */ |
|
378 |
eP[1] = tG*5; |
|
379 |
eP[2] = tB*5; |
|
380 |
||
381 |
eP[-3] = tR*3; /* 3/16th for (x-1 / y+1) */ |
|
382 |
eP[-2] = tG*3; |
|
383 |
eP[-1] = tB*3; |
|
384 |
||
385 |
eP[3] = __eR - (tR*15); /* 1/16th for (x+1 / y+1) */ |
|
386 |
eP[4] = __eG - (tG*15); |
|
387 |
eP[5] = __eB - (tB*15); |
|
388 |
||
389 |
__eR = nR; |
|
390 |
__eG = nG; |
|
391 |
__eB = nB; |
|
392 |
||
393 |
eP += 3; |
|
394 |
#endif |
|
89 | 395 |
} |
396 |
} |
|
397 |
} |
|
398 |
%}. |
|
399 |
failed ifTrue:[ |
|
400 |
self primitiveFailed. |
|
401 |
^ nil |
|
402 |
]. |
|
403 |
||
404 |
f := Form width:width height:height depth:deviceDepth on:aDevice. |
|
405 |
f isNil ifTrue:[^ nil]. |
|
155 | 406 |
"/ |
407 |
"/ have to create a funny colorMap, where |
|
408 |
"/ color at:index == color colorId:index |
|
409 |
"/ |
|
410 |
map := Array new:256. |
|
411 |
fixColors do:[:clr | |
|
412 |
map at:clr colorId + 1 put:clr |
|
413 |
]. |
|
414 |
f colorMap:map. |
|
89 | 415 |
f initGC. |
155 | 416 |
f bits:pseudoBits. |
89 | 417 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:deviceDepth |
418 |
width:width height:height |
|
419 |
x:0 y:0 |
|
420 |
into:(f id) x:0 y:0 |
|
421 |
width:width height:height with:(f gcId). |
|
422 |
^ f |
|
423 |
||
424 |
" |
|
155 | 425 |
example: |
426 |
color reduction from Depth8 to Depth4 (dithering) can be done by: |
|
89 | 427 |
|
155 | 428 |
|img8 reducedImg8 img4 map form| |
89 | 429 |
|
430 |
map := #( |
|
431 |
(0 0 0) |
|
432 |
(0 0 100) |
|
433 |
(0 50 0) |
|
434 |
(0 50 100) |
|
435 |
(0 100 0) |
|
436 |
(0 100 100) |
|
437 |
(100 0 0) |
|
438 |
(100 0 100) |
|
439 |
(100 50 0) |
|
440 |
(100 50 100) |
|
441 |
(100 100 0) |
|
442 |
(100 100 100)) collect:[:rgb | (Color red:(rgb at:1) |
|
443 |
green:(rgb at:2) |
|
444 |
blue:(rgb at:3)) on:Display]. |
|
445 |
||
446 |
img8 := Image fromFile:'bitmaps/bf.im8'. |
|
447 |
form := img8 paletteImageAsDitheredPseudoFormOn:Display |
|
448 |
colors:map |
|
449 |
nRed:2 |
|
450 |
nGreen:3 |
|
451 |
nBlue:2. |
|
155 | 452 |
img8 := Depth8Image fromForm:form. 'dithered version of original image'. |
89 | 453 |
img4 := Depth4Image fromImage:img8. |
454 |
" |
|
455 |
! |
|
456 |
||
1 | 457 |
paletteImageAsPseudoFormOn:aDevice |
458 |
"return a pseudoForm from the palette picture. The main work is |
|
459 |
in color reduction, when not all colors can be aquired." |
|
460 |
||
46 | 461 |
|pseudoBits f gcRound has8BitImage deviceDepth |
462 |
imgMap newImage pxl |
|
154 | 463 |
usedColors usageCounts maxIndex map |
89 | 464 |
fit scale lastOK error |
154 | 465 |
div |
466 |
bitsPerRGB "{Class: SmallInteger }" |
|
467 |
shift "{Class: SmallInteger }" |
|
468 |
m "{Class: SmallInteger }" | |
|
1 | 469 |
|
89 | 470 |
Color fixColors notNil ifTrue:[ |
471 |
f := self paletteImageAsDitheredPseudoFormOn:aDevice. |
|
472 |
f notNil ifTrue:[^ f]. |
|
473 |
]. |
|
474 |
||
1 | 475 |
"find used colors" |
476 |
||
154 | 477 |
usedColors := bytes usedValues. "gets us an array filled with used values" |
478 |
"(could use bytes asBag)" |
|
479 |
maxIndex := usedColors max + 1. |
|
480 |
||
180 | 481 |
usedColors size > 20 ifTrue:[ |
482 |
('D8IMAGE: allocating ' , usedColors size printString , ' colors ...') infoPrintNL. |
|
483 |
]. |
|
1 | 484 |
|
485 |
"sort by usage" |
|
486 |
usageCounts := bytes usageCounts. |
|
487 |
usageCounts := usedColors asArray collect:[:clr | usageCounts at:(clr + 1)]. |
|
488 |
usageCounts sort:[:a :b | a > b] with:usedColors. |
|
489 |
||
490 |
"allocate the colors (in order of usage count)" |
|
491 |
||
154 | 492 |
imgMap := Array new:maxIndex. |
1 | 493 |
|
89 | 494 |
" |
495 |
first, try to get the exact colors ... |
|
496 |
" |
|
154 | 497 |
bitsPerRGB := aDevice bitsPerRGB. |
498 |
shift := (8 - bitsPerRGB) negated. |
|
499 |
m := (1 bitShift:bitsPerRGB) - 1. |
|
1 | 500 |
div := m asFloat. |
501 |
||
502 |
fit := true. |
|
503 |
scale := 100.0 / div. "to scale 0..255 into 0.0 .. 100.0" |
|
504 |
lastOK := 0. |
|
89 | 505 |
gcRound := 0. |
506 |
||
1 | 507 |
usedColors do:[:aColorIndex | |
71 | 508 |
|devColor color |
89 | 509 |
r "{Class: SmallInteger }" |
510 |
g "{Class: SmallInteger }" |
|
511 |
b "{Class: SmallInteger }" |
|
512 |
mapIndex "{Class: SmallInteger }" |
|
513 |
rMask "{Class: SmallInteger }" |
|
514 |
gMask "{Class: SmallInteger }" |
|
515 |
bMask "{Class: SmallInteger }"| |
|
1 | 516 |
|
71 | 517 |
fit ifTrue:[ |
518 |
gMask := bMask := rMask := m. |
|
1 | 519 |
|
71 | 520 |
mapIndex := aColorIndex + 1. |
89 | 521 |
color := colorMap at:mapIndex. |
522 |
color colorId notNil ifTrue:[ |
|
523 |
"wow - an immediate hit" |
|
524 |
devColor := color |
|
525 |
] ifFalse:[ |
|
526 |
devColor := color exactOn:aDevice. |
|
527 |
devColor isNil ifTrue:[ |
|
528 |
" |
|
529 |
could not allocate color - on the first round, do a GC to flush |
|
530 |
unused colors - this may help if some colors where locked by |
|
531 |
already free images. |
|
532 |
" |
|
533 |
gcRound == 0 ifTrue:[ |
|
112 | 534 |
ObjectMemory scavenge; finalize. |
89 | 535 |
devColor := color exactOn:aDevice. |
536 |
gcRound := 1 |
|
537 |
]. |
|
538 |
devColor isNil ifTrue:[ |
|
539 |
gcRound == 1 ifTrue:[ |
|
158 | 540 |
CollectGarbageWhenRunningOutOfColors ifTrue:[ |
541 |
'D8IMAGE: force GC for possible color reclamation.' infoPrintNL. |
|
542 |
ObjectMemory incrementalGC; finalize. |
|
543 |
devColor := color exactOn:aDevice. |
|
544 |
]. |
|
89 | 545 |
gcRound := 2 |
546 |
] |
|
547 |
] |
|
71 | 548 |
]. |
549 |
]. |
|
550 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[ |
|
551 |
imgMap at:mapIndex put:devColor. |
|
552 |
lastOK := lastOK + 1. |
|
553 |
] ifFalse:[ |
|
554 |
fit := false |
|
555 |
] |
|
556 |
] |
|
1 | 557 |
]. |
558 |
||
559 |
fit ifFalse:[ |
|
157 | 560 |
"/ |tree| |
561 |
"/ |
|
562 |
"/ "/ first, create an octTree containing colors which we got ... |
|
563 |
"/ tree := ColorOctree new. |
|
564 |
"/ |
|
565 |
"/ usedColors from:1 to:lastOK do:[:aColorIndex | |
|
566 |
"/ tree insert:(colorMap at:aColorIndex + 1). |
|
567 |
"/ ]. |
|
568 |
"/ |
|
569 |
"/ "/ then, remap remaining, using nearest from those already allocated |
|
570 |
"/ |
|
571 |
"/ usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
572 |
"/ |mapIndex| |
|
573 |
"/ |
|
574 |
"/ mapIndex := aColorIndex + 1. |
|
575 |
"/ imgMap at:mapIndex put:(tree findBest:(colorMap at:mapIndex)) |
|
576 |
"/ ]. |
|
577 |
||
89 | 578 |
" |
579 |
again, this time allow wrong colors (loop while increasing allowed error) |
|
580 |
" |
|
71 | 581 |
error := 10. |
582 |
[fit] whileFalse:[ |
|
583 |
fit := true. |
|
584 |
usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
585 |
|devColor color |
|
89 | 586 |
r "{Class: SmallInteger }" |
587 |
g "{Class: SmallInteger }" |
|
588 |
b "{Class: SmallInteger }" |
|
589 |
mapIndex "{Class: SmallInteger }" |
|
590 |
rMask "{Class: SmallInteger }" |
|
591 |
gMask "{Class: SmallInteger }" |
|
592 |
bMask "{Class: SmallInteger }"| |
|
1 | 593 |
|
71 | 594 |
fit ifTrue:[ |
595 |
gMask := bMask := rMask := m. |
|
1 | 596 |
|
71 | 597 |
mapIndex := aColorIndex + 1. |
89 | 598 |
color := colorMap at:mapIndex. |
599 |
r := (color red * 255 / 100) rounded. |
|
600 |
g := (color green * 255 / 100) rounded. |
|
601 |
b := (color blue * 255 / 100) rounded. |
|
602 |
||
71 | 603 |
color := Color red:((r bitShift:shift) bitAnd:rMask) * scale |
604 |
green:((g bitShift:shift) bitAnd:gMask) * scale |
|
605 |
blue:((b bitShift:shift) bitAnd:bMask) * scale. |
|
89 | 606 |
|
607 |
color colorId notNil ifTrue:[ |
|
608 |
"wow - an immediate hit" |
|
609 |
devColor := color |
|
610 |
] ifFalse:[ |
|
611 |
devColor := color nearestOn:aDevice error:error. |
|
612 |
devColor isNil ifTrue:[ |
|
613 |
" |
|
614 |
no free color - on the first round, do a GC to flush unused |
|
615 |
colors - this may help if some colors where locked by already |
|
616 |
free images. |
|
617 |
" |
|
618 |
gcRound == 0 ifTrue:[ |
|
112 | 619 |
ObjectMemory scavenge; finalize. |
89 | 620 |
devColor := color nearestOn:aDevice error:error. |
621 |
gcRound := 1 |
|
622 |
]. |
|
623 |
devColor isNil ifTrue:[ |
|
624 |
gcRound == 1 ifTrue:[ |
|
230
2e35533f0af2
dont do a full GC if running out of colors
Claus Gittinger <cg@exept.de>
parents:
219
diff
changeset
|
625 |
CollectGarbageWhenRunningOutOfColors ifTrue:[ |
2e35533f0af2
dont do a full GC if running out of colors
Claus Gittinger <cg@exept.de>
parents:
219
diff
changeset
|
626 |
'D8IMAGE: force GC for possible color reclamation.' infoPrintNL. |
2e35533f0af2
dont do a full GC if running out of colors
Claus Gittinger <cg@exept.de>
parents:
219
diff
changeset
|
627 |
ObjectMemory incrementalGC; finalize. |
2e35533f0af2
dont do a full GC if running out of colors
Claus Gittinger <cg@exept.de>
parents:
219
diff
changeset
|
628 |
devColor := color nearestOn:aDevice error:error. |
2e35533f0af2
dont do a full GC if running out of colors
Claus Gittinger <cg@exept.de>
parents:
219
diff
changeset
|
629 |
]. |
89 | 630 |
gcRound := 2 |
631 |
] |
|
632 |
] |
|
71 | 633 |
]. |
634 |
]. |
|
635 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[ |
|
636 |
imgMap at:mapIndex put:devColor. |
|
637 |
lastOK := lastOK + 1. |
|
638 |
] ifFalse:[ |
|
639 |
fit := false |
|
640 |
] |
|
641 |
]. |
|
642 |
]. |
|
89 | 643 |
error := error * 2. |
644 |
error > 1000 ifTrue:[ |
|
645 |
" |
|
646 |
break out, if the error becomes too big. |
|
647 |
" |
|
151 | 648 |
'D8IMAGE: hard color allocation problem - revert to b&w' infoPrintNL. |
89 | 649 |
" |
650 |
map to b&w as a last fallback. |
|
651 |
(should really do a dither here) |
|
652 |
" |
|
653 |
usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
654 |
|color |
|
655 |
mapIndex "{ Class: SmallInteger }"| |
|
656 |
||
657 |
mapIndex := aColorIndex + 1. |
|
658 |
color := colorMap at:mapIndex. |
|
659 |
color brightness > 0.5 ifTrue:[ |
|
154 | 660 |
color := Color white. |
89 | 661 |
] ifFalse:[ |
154 | 662 |
color := Color black. |
663 |
]. |
|
664 |
imgMap at:mapIndex put:(color on:aDevice). |
|
89 | 665 |
]. |
666 |
fit := true. |
|
667 |
] |
|
71 | 668 |
]. |
1 | 669 |
|
71 | 670 |
error > 100 ifTrue:[ |
151 | 671 |
'D8IMAGE: not enough colors for a reasonable image' infoPrintNL |
71 | 672 |
] ifFalse:[ |
151 | 673 |
'D8IMAGE: not enough colors for exact picture' infoPrintNL. |
71 | 674 |
] |
1 | 675 |
]. |
676 |
||
89 | 677 |
" |
678 |
create translation map (from image colors to allocated colorIds) |
|
679 |
" |
|
46 | 680 |
map := ByteArray new:256. |
1 | 681 |
1 to:imgMap size do:[:i | |
154 | 682 |
|clr| |
683 |
||
684 |
(clr := imgMap at:i) notNil ifTrue:[ |
|
685 |
map at:i put:clr colorId |
|
71 | 686 |
] |
1 | 687 |
]. |
688 |
||
154 | 689 |
" |
690 |
does the device support 8-bit images ? |
|
691 |
" |
|
46 | 692 |
deviceDepth := aDevice depth. |
693 |
deviceDepth == 8 ifTrue:[ |
|
71 | 694 |
has8BitImage := true. |
46 | 695 |
] ifFalse:[ |
71 | 696 |
has8BitImage := false. |
697 |
aDevice supportedImageFormats do:[:fmt | |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
698 |
(fmt at:#bitsPerPixel) == 8 ifTrue:[ |
71 | 699 |
has8BitImage := true. |
700 |
] |
|
701 |
] |
|
46 | 702 |
]. |
703 |
||
89 | 704 |
" |
705 |
finally, create a form on the device and copy (& translate) |
|
706 |
the pixel values |
|
707 |
" |
|
46 | 708 |
has8BitImage ifTrue:[ |
71 | 709 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
46 | 710 |
|
71 | 711 |
bytes expandPixels:8 "xlate only" |
712 |
width:width |
|
713 |
height:height |
|
714 |
into:pseudoBits |
|
715 |
mapping:map. |
|
46 | 716 |
|
71 | 717 |
map := nil. |
1 | 718 |
|
71 | 719 |
f := Form width:width height:height depth:deviceDepth on:aDevice. |
720 |
f isNil ifTrue:[^ nil]. |
|
721 |
f colorMap:imgMap. |
|
722 |
f initGC. |
|
723 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:deviceDepth |
|
724 |
width:width height:height |
|
725 |
x:0 y:0 |
|
726 |
into:(f id) x:0 y:0 |
|
727 |
width:width height:height with:(f gcId). |
|
728 |
^ f |
|
46 | 729 |
]. |
1 | 730 |
|
46 | 731 |
" |
732 |
slow fall back: convert into appropriate depth image, |
|
733 |
by looping over each pixel individually |
|
734 |
" |
|
735 |
newImage := (Image implementorForDepth:deviceDepth) new. |
|
736 |
newImage width:width. |
|
737 |
newImage height:height. |
|
738 |
newImage bits:(ByteArray uninitializedNew:(height * newImage bytesPerRow)). |
|
739 |
||
740 |
0 to:height-1 do:[:row | |
|
71 | 741 |
0 to:width-1 do:[:col | |
742 |
pxl := self valueAtX:col y:row. |
|
743 |
newImage atX:col y:row putValue:(map at:pxl) |
|
744 |
] |
|
46 | 745 |
]. |
746 |
||
747 |
f := Form width:width height:height depth:deviceDepth on:aDevice. |
|
1 | 748 |
f isNil ifTrue:[^ nil]. |
749 |
f colorMap:imgMap. |
|
750 |
f initGC. |
|
46 | 751 |
|
752 |
aDevice drawBits:(newImage bits) depth:deviceDepth width:width height:height |
|
71 | 753 |
x:0 y:0 |
754 |
into:(f id) x:0 y:0 |
|
755 |
width:width height:height with:(f gcId). |
|
46 | 756 |
|
1 | 757 |
^ f |
758 |
! ! |
|
759 |
||
810 | 760 |
!Depth8Image methodsFor:'dither helpers'! |
761 |
||
762 |
orderedDitheredBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
|
763 |
"return the bitmap for a dithered depth-bitmap from the image; |
|
764 |
with a constant ditherMatrix, this can be used for thresholding. |
|
765 |
Redefined to make use of knowing that pixels are 8-bit values." |
|
766 |
||
767 |
|dH nDither |
|
768 |
greyLevels greyMap1 greyMap2 |
|
769 |
bytesPerRow "{Class: SmallInteger }" |
|
770 |
bytesPerOutRow "{Class: SmallInteger }" |
|
771 |
pixelsPerByte "{Class: SmallInteger }" |
|
772 |
outBits |
|
773 |
w "{Class: SmallInteger }" |
|
774 |
h "{Class: SmallInteger }" | |
|
775 |
||
776 |
nDither := ditherMatrix size. |
|
777 |
dH := nDither / dW. |
|
778 |
||
779 |
w := width. |
|
780 |
h := height. |
|
781 |
||
782 |
greyLevels := 1 bitShift:depth. |
|
783 |
pixelsPerByte := 8 / depth. |
|
784 |
||
785 |
bytesPerRow := self bytesPerRow. |
|
786 |
||
787 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
788 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
789 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
790 |
^ nil |
|
791 |
]. |
|
792 |
||
793 |
greyMap1 := self greyMapForRange:(greyLevels-1). "/ the pixels |
|
794 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
|
795 |
0 |
|
796 |
] ifFalse:[ |
|
797 |
b truncated |
|
798 |
] |
|
799 |
]) asByteArray. |
|
800 |
||
801 |
greyMap2 := self greyMapForRange:(greyLevels-1). |
|
802 |
greyMap2 := (greyMap2 collect:[:el | |
|
803 |
el isNil ifTrue:[ |
|
804 |
0 |
|
805 |
] ifFalse:[ |
|
806 |
((el - el truncated) "/ the error (0..1) |
|
807 |
* nDither) rounded |
|
808 |
]]) asByteArray. |
|
809 |
||
810 |
%{ |
|
811 |
int __dW = __intVal(dW); |
|
812 |
int __dH = __intVal(dH); |
|
813 |
int __byte; |
|
814 |
int __dT, __dO; |
|
815 |
int __depth = __intVal(depth); |
|
816 |
int __dstIdx = 0; |
|
817 |
int __srcIdx = 0; |
|
818 |
int __bitCnt; |
|
819 |
int __grey, __pixel; |
|
820 |
int __w = __intVal(w); |
|
821 |
int __h = __intVal(h); |
|
822 |
int __x; |
|
823 |
int __y; |
|
824 |
int __oX, __oY, __dY; |
|
825 |
int __nextDst; |
|
826 |
int __nextSrc; |
|
827 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
828 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
|
829 |
||
830 |
char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
|
831 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
832 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
833 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
|
834 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
|
835 |
||
836 |
__oY = __dY = 0; |
|
837 |
for (__y=0; __y<__h; __y++) { |
|
838 |
__nextDst = __dstIdx + __bytesPerOutRow; |
|
839 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
840 |
||
841 |
__byte = 0; |
|
842 |
__bitCnt = 8; |
|
843 |
||
844 |
__oX = 0; |
|
845 |
||
846 |
for (__x=0; __x<__w; __x++) { |
|
847 |
__grey = __bytes[__srcIdx++]; /* 0..255 */ |
|
848 |
||
849 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
|
850 |
__dO = __greyMap2[__grey]; /* 0.. nDither-1) */ |
|
851 |
__dT = __ditherMatrix[__dY + __oX]; |
|
852 |
||
853 |
if (__dO > __dT) /* dither says: next pixel */ |
|
854 |
__pixel++; |
|
855 |
||
856 |
__oX++; |
|
857 |
if (__oX == __dW) __oX = 0; |
|
858 |
||
859 |
__byte = (__byte << __depth) | __pixel; |
|
860 |
||
861 |
__bitCnt = __bitCnt - __depth; |
|
862 |
if (__bitCnt == 0) { |
|
863 |
__outBits[__dstIdx] = __byte; |
|
864 |
__dstIdx++; |
|
865 |
__byte = 0; |
|
866 |
__bitCnt = 8; |
|
867 |
} |
|
868 |
} |
|
869 |
||
870 |
if (__bitCnt != 8) { |
|
871 |
__byte = __byte << __bitCnt; |
|
872 |
__outBits[__dstIdx] = __byte; |
|
873 |
} |
|
874 |
||
875 |
__oY++; __dY += __dW; |
|
876 |
if (__oY == __dH) { |
|
877 |
__oY = 0; |
|
878 |
__dY = 0; |
|
879 |
} |
|
880 |
||
881 |
__srcIdx = __nextSrc; |
|
882 |
__dstIdx = __nextDst; |
|
883 |
} |
|
884 |
%}. |
|
885 |
^ outBits |
|
886 |
! |
|
887 |
||
888 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
|
889 |
"return the dithered monochrome bits for the receiver image; |
|
890 |
with a constant ditherMatrix, this can be used for thresholding. |
|
891 |
Redefined to make use of knowing that pixels are 8-bit values." |
|
892 |
||
893 |
|f dH nDither |
|
894 |
greyMap monoBits |
|
895 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
896 |
bytesPerRow "{Class: SmallInteger }" |
|
897 |
w "{Class: SmallInteger }" |
|
898 |
h "{Class: SmallInteger }"| |
|
899 |
||
900 |
nDither := ditherMatrix size. |
|
901 |
dH := nDither / dW. |
|
902 |
||
903 |
w := width. |
|
904 |
h := height. |
|
905 |
||
906 |
bytesPerRow := self bytesPerRow. |
|
907 |
||
908 |
bytesPerMonoRow := w + 7 // 8. |
|
909 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
910 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
911 |
^ nil |
|
912 |
]. |
|
913 |
||
914 |
greyMap := self greyByteMapForRange:nDither. |
|
915 |
||
916 |
%{ |
|
917 |
int __dW = __intVal(dW); |
|
918 |
int __dH = __intVal(dH); |
|
919 |
int __byte; |
|
920 |
int __dT; |
|
921 |
int __dstIdx = 0; |
|
922 |
int __srcIdx = 0; |
|
923 |
int __bitCnt; |
|
924 |
int __grey; |
|
925 |
int __w = __intVal(w); |
|
926 |
int __h = __intVal(h); |
|
927 |
int __x; |
|
928 |
int __y; |
|
929 |
int __oX, __oY, __dY; |
|
930 |
int __nextDst; |
|
931 |
int __nextSrc; |
|
932 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
933 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
934 |
||
935 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
936 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
937 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
938 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
939 |
||
940 |
__oY = __dY = 0; |
|
941 |
for (__y=0; __y<__h; __y++) { |
|
942 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
943 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
944 |
||
945 |
__byte = 0; |
|
946 |
__bitCnt = 8; |
|
947 |
||
948 |
__oX = 0; |
|
949 |
||
950 |
for (__x=__w; __x>0; __x--) { |
|
951 |
__grey = __bytes[__srcIdx]; /* 0..255 */ |
|
952 |
__srcIdx++; |
|
953 |
||
954 |
__grey = __greyMap[__grey]; |
|
955 |
__dT = __ditherMatrix[__dY + __oX]; |
|
956 |
||
957 |
__oX++; |
|
958 |
if (__oX == __dW) __oX = 0; |
|
959 |
||
960 |
__byte = __byte << 1; |
|
961 |
if (__grey > __dT) { |
|
962 |
__byte = __byte | 1; /* white */ |
|
963 |
} |
|
964 |
||
965 |
__bitCnt--; |
|
966 |
if (__bitCnt == 0) { |
|
967 |
__monoBits[__dstIdx] = __byte; |
|
968 |
__dstIdx++; |
|
969 |
__byte = 0; |
|
970 |
__bitCnt = 8; |
|
971 |
} |
|
972 |
} |
|
973 |
||
974 |
if (__bitCnt != 8) { |
|
975 |
__byte = __byte << __bitCnt; |
|
976 |
__monoBits[__dstIdx] = __byte; |
|
977 |
} |
|
978 |
||
979 |
__oY++; __dY += __dW; |
|
980 |
if (__oY == __dH) { |
|
981 |
__oY = 0; |
|
982 |
__dY = 0; |
|
983 |
} |
|
984 |
||
985 |
__srcIdx = __nextSrc; |
|
986 |
__dstIdx = __nextDst; |
|
987 |
} |
|
988 |
%}. |
|
989 |
||
990 |
^ monoBits |
|
991 |
||
992 |
" |
|
993 |
|i f| |
|
994 |
||
995 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
996 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
997 |
||
998 |
||
999 |
|i f| |
|
1000 |
||
1001 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1002 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1003 |
||
1004 |
||
1005 |
|i f| |
|
1006 |
||
1007 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1008 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1009 |
||
1010 |
||
1011 |
|i f| |
|
1012 |
||
1013 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
1014 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1015 |
" |
|
1016 |
||
1017 |
"Created: 7.6.1996 / 10:48:06 / cg" |
|
1018 |
"Modified: 7.6.1996 / 11:08:50 / cg" |
|
1019 |
! ! |
|
1020 |
||
579 | 1021 |
!Depth8Image methodsFor:'enumerating'! |
1 | 1022 |
|
798 | 1023 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
1024 |
"perform aBlock for each pixel from x1 to x2 in row y. |
|
1025 |
The block is passed the color at each pixel. |
|
1026 |
This method allows slighly faster processing of an |
|
1027 |
image than using atX:y:, since some processing can be |
|
1028 |
avoided when going from pixel to pixel. However, for |
|
1029 |
real image processing, specialized methods should be written." |
|
1030 |
||
1031 |
|srcIndex "{ Class: SmallInteger }" |
|
1032 |
value "{ Class: SmallInteger }" |
|
1033 |
x1 "{ Class: SmallInteger }" |
|
1034 |
x2 "{ Class: SmallInteger }" |
|
810 | 1035 |
color colorArray scale gr| |
798 | 1036 |
|
1037 |
x1 := xLow. |
|
1038 |
x2 := xHigh. |
|
1039 |
srcIndex := (width * y) + 1 + x1. |
|
1040 |
||
810 | 1041 |
colorArray := Array new:256. |
1042 |
||
798 | 1043 |
photometric == #blackIs0 ifTrue:[ |
810 | 1044 |
scale := 100.0 / 255.0. |
798 | 1045 |
|
1046 |
x1 to:x2 do:[:x | |
|
1047 |
value := bytes at:srcIndex. |
|
1048 |
srcIndex := srcIndex + 1. |
|
1049 |
||
810 | 1050 |
color := colorArray at:value+1. |
746 | 1051 |
color isNil ifTrue:[ |
810 | 1052 |
color := Color gray:(value * scale). |
1053 |
colorArray at:value+1 put:color |
|
798 | 1054 |
]. |
1055 |
aBlock value:x value:color |
|
746 | 1056 |
]. |
798 | 1057 |
^ self |
1058 |
]. |
|
1059 |
||
1060 |
photometric == #whiteIs0 ifTrue:[ |
|
810 | 1061 |
scale := 100.0 / 255.0. |
798 | 1062 |
|
1063 |
x1 to:x2 do:[:x | |
|
810 | 1064 |
value := bytes at:srcIndex. |
798 | 1065 |
srcIndex := srcIndex + 1. |
1066 |
||
810 | 1067 |
color := colorArray at:value+1. |
798 | 1068 |
color isNil ifTrue:[ |
810 | 1069 |
gr := 255 - value. |
1070 |
color := Color gray:(gr * scale). |
|
1071 |
colorArray at:value+1 put:color |
|
798 | 1072 |
]. |
1073 |
aBlock value:x value:color |
|
1074 |
]. |
|
1075 |
^ self |
|
1076 |
]. |
|
1077 |
||
1078 |
photometric ~~ #palette ifTrue:[ |
|
810 | 1079 |
x1 to:x2 do:[:x | |
1080 |
value := bytes at:srcIndex. |
|
1081 |
srcIndex := srcIndex + 1. |
|
1082 |
||
1083 |
color := colorArray at:value+1. |
|
1084 |
color isNil ifTrue:[ |
|
1085 |
color := self colorFromValue:value. |
|
1086 |
colorArray at:value+1 put:color |
|
1087 |
]. |
|
1088 |
aBlock value:x value:color |
|
1089 |
]. |
|
1090 |
^ self |
|
798 | 1091 |
]. |
1092 |
||
810 | 1093 |
colorArray := colorMap. |
802 | 1094 |
|
798 | 1095 |
x1 to:x2 do:[:x | |
1096 |
value := bytes at:srcIndex. |
|
1097 |
srcIndex := srcIndex + 1. |
|
1098 |
||
810 | 1099 |
color := colorArray at:(value+1). |
746 | 1100 |
aBlock value:x value:color |
579 | 1101 |
] |
746 | 1102 |
|
798 | 1103 |
"Created: 7.6.1996 / 19:12:35 / cg" |
810 | 1104 |
"Modified: 10.6.1996 / 10:32:07 / cg" |
579 | 1105 |
! |
1 | 1106 |
|
798 | 1107 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
579 | 1108 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
1109 |
The block is passed the pixelValue at each pixel. |
|
1110 |
This method allows slighly faster processing of an |
|
1111 |
image than using valueAtX:y:, since some processing can be |
|
1112 |
avoided when going from pixel to pixel. However, for |
|
1113 |
real image processing, specialized methods should be written." |
|
1 | 1114 |
|
579 | 1115 |
|srcIndex "{ Class: SmallInteger }" |
1116 |
pixelValue "{ Class: SmallInteger }" |
|
1117 |
x1 "{ Class: SmallInteger }" |
|
1118 |
x2 "{ Class: SmallInteger }"| |
|
1 | 1119 |
|
579 | 1120 |
x1 := xLow. |
1121 |
x2 := xHigh. |
|
1122 |
srcIndex := (width * y) + 1 + x1. |
|
1123 |
||
1124 |
x1 to:x2 do:[:x | |
|
798 | 1125 |
pixelValue := bytes at:srcIndex. |
1126 |
srcIndex := srcIndex + 1. |
|
1127 |
aBlock value:x value:pixelValue |
|
579 | 1128 |
] |
798 | 1129 |
|
1130 |
"Created: 7.6.1996 / 19:09:47 / cg" |
|
1 | 1131 |
! ! |
1132 |
||
12 | 1133 |
!Depth8Image methodsFor:'image manipulations'! |
1134 |
||
1135 |
flipHorizontal |
|
1136 |
"inplace horizontal flip" |
|
1137 |
||
1138 |
|index "{Class: SmallInteger }" |
|
1139 |
h "{Class: SmallInteger }" |
|
1140 |
w "{Class: SmallInteger }" |
|
1141 |
buffer | |
|
1142 |
||
1143 |
w := width - 1. |
|
1144 |
h := height - 1. |
|
1145 |
||
1146 |
buffer := ByteArray new:width. |
|
1147 |
||
1148 |
index := 1. |
|
1149 |
0 to:h do:[:row | |
|
71 | 1150 |
buffer replaceFrom:1 to:width with:bytes startingAt:index. |
1151 |
buffer reverse. |
|
1152 |
bytes replaceFrom:index to:index+w with:buffer startingAt:1. |
|
1153 |
index := index + w + 1. |
|
12 | 1154 |
]. |
1155 |
"flush device info" |
|
1156 |
self restored |
|
1157 |
! |
|
1 | 1158 |
|
134 | 1159 |
hardMagnifiedBy:extent |
1 | 1160 |
"return a new image magnified by extent, aPoint. |
12 | 1161 |
This is the general magnification method, handling non-integral values" |
1 | 1162 |
|
1163 |
|mX mY |
|
1164 |
newWidth "{ Class: SmallInteger }" |
|
1165 |
newHeight "{ Class: SmallInteger }" |
|
89 | 1166 |
w "{ Class: SmallInteger }" |
1 | 1167 |
h "{ Class: SmallInteger }" |
1168 |
newImage newBytes |
|
1169 |
value "{ Class: SmallInteger }" |
|
1170 |
srcRowIdx "{ Class: SmallInteger }" |
|
1171 |
srcIndex "{ Class: SmallInteger }" |
|
1172 |
dstIndex "{ Class: SmallInteger }"| |
|
1173 |
||
1174 |
mX := extent x. |
|
1175 |
mY := extent y. |
|
1176 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
1177 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
1178 |
||
1179 |
newWidth := (width * mX) truncated. |
|
1180 |
newHeight := (height * mY) truncated. |
|
1181 |
||
1182 |
newBytes := ByteArray uninitializedNew:(newWidth * newHeight). |
|
1183 |
||
1184 |
newImage := self species new. |
|
1185 |
newImage bits:newBytes. |
|
1186 |
newImage width:newWidth. |
|
1187 |
newImage height:newHeight. |
|
1188 |
newImage photometric:photometric. |
|
1189 |
newImage samplesPerPixel:samplesPerPixel. |
|
1190 |
newImage bitsPerSample:#(8). |
|
1191 |
newImage colorMap:colorMap copy. |
|
1192 |
||
1193 |
"walk over destination image fetching pixels from source image" |
|
1194 |
||
1195 |
mY := mY asFloat. |
|
1196 |
mX := mX asFloat. |
|
1197 |
%{ |
|
1198 |
unsigned char *_dstP = _ByteArrayInstPtr(newBytes)->ba_element; |
|
1199 |
unsigned char *_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1200 |
unsigned char *_srcRowP; |
|
1201 |
int _width = _intVal(_INST(width)); |
|
1202 |
int _w = _intVal(newWidth) - 1; |
|
1203 |
int _h = _intVal(newHeight) - 1; |
|
1204 |
int _row, _col; |
|
1205 |
double _mX = _floatVal(mX); |
|
1206 |
double _mY = _floatVal(mY); |
|
1207 |
||
1208 |
for (_row = 0; _row <= _h; _row++) { |
|
71 | 1209 |
_srcRowP = _srcP + (_width * (int)((double)_row / _mY)); |
1210 |
for (_col = 0; _col <= _w; _col++) { |
|
1211 |
*_dstP++ = _srcRowP[(int)((double)_col / _mX)]; |
|
1212 |
} |
|
1 | 1213 |
} |
1214 |
%} |
|
1215 |
. |
|
89 | 1216 |
"/ the above C-code is equivalent to: |
1217 |
"/ |
|
1218 |
"/ dstIndex := 1. |
|
1219 |
"/ w := newWidth - 1. |
|
1220 |
"/ h := newHeight - 1. |
|
1221 |
"/ 0 to:h do:[:row | |
|
1222 |
"/ srcRowIdx := (width * (row // mY)) + 1. |
|
1223 |
"/ 0 to:w do:[:col | |
|
1224 |
"/ srcIndex := srcRowIdx + (col // mX). |
|
1225 |
"/ value := bytes at:srcIndex. |
|
1226 |
"/ newBytes at:dstIndex put:value. |
|
1227 |
"/ dstIndex := dstIndex + 1 |
|
1228 |
"/ ] |
|
1229 |
"/ ]. |
|
1230 |
"/ |
|
1 | 1231 |
|
1232 |
^ newImage |
|
579 | 1233 |
! |
1234 |
||
1235 |
magnifyRowFrom:srcBytes offset:srcStart |
|
1236 |
into:dstBytes offset:dstStart factor:mX |
|
1237 |
||
1238 |
"magnify a single pixel row - can only magnify by integer factors. |
|
1239 |
Specially tuned for factors 2,3 and 4." |
|
1240 |
||
1241 |
%{ |
|
1242 |
REGISTER unsigned char *srcP, *dstP; |
|
1243 |
REGISTER unsigned char _byte; |
|
1244 |
int _mag; |
|
1245 |
REGISTER int i; |
|
1246 |
int _pixels; |
|
1247 |
OBJ w = _INST(width); |
|
1248 |
||
1249 |
if (__bothSmallInteger(srcStart, dstStart) |
|
1250 |
&& __bothSmallInteger(w, mX) |
|
1251 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
|
1252 |
_mag = _intVal(mX); |
|
1253 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
1254 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
1255 |
_pixels = _intVal(w); |
|
1256 |
||
1257 |
switch (_mag) { |
|
1258 |
case 1: |
|
1259 |
break; |
|
1260 |
||
1261 |
case 2: |
|
1262 |
/* special code for common case */ |
|
1263 |
while (_pixels--) { |
|
1264 |
_byte = *srcP++; |
|
1265 |
*dstP++ = _byte; |
|
1266 |
*dstP++ = _byte; |
|
1267 |
} |
|
1268 |
break; |
|
1269 |
||
1270 |
case 3: |
|
1271 |
/* special code for common case */ |
|
1272 |
while (_pixels--) { |
|
1273 |
_byte = *srcP++; |
|
1274 |
*dstP++ = _byte; |
|
1275 |
*dstP++ = _byte; |
|
1276 |
*dstP++ = _byte; |
|
1277 |
} |
|
1278 |
break; |
|
1279 |
||
1280 |
case 4: |
|
1281 |
/* special code for common case */ |
|
1282 |
while (_pixels--) { |
|
1283 |
_byte = *srcP++; |
|
1284 |
*dstP++ = _byte; |
|
1285 |
*dstP++ = _byte; |
|
1286 |
*dstP++ = _byte; |
|
1287 |
*dstP++ = _byte; |
|
1288 |
} |
|
1289 |
break; |
|
1290 |
||
1291 |
default: |
|
1292 |
while (_pixels--) { |
|
1293 |
_byte = *srcP++; |
|
1294 |
for (i=_mag; i>0; i--) |
|
1295 |
*dstP++ = _byte; |
|
1296 |
} |
|
1297 |
break; |
|
1298 |
} |
|
1299 |
RETURN (self); |
|
1300 |
} |
|
1301 |
%} |
|
1302 |
. |
|
1303 |
self primitiveFailed |
|
1 | 1304 |
! ! |
579 | 1305 |
|
1306 |
!Depth8Image methodsFor:'private'! |
|
1307 |
||
1308 |
dither1PlaneUsingMap:map on:aDevice |
|
1309 |
"a helper for dithering palette and greyscale images" |
|
1310 |
||
1311 |
|f |
|
1312 |
patterns formBytes |
|
1313 |
pixel0bytes pixel1bytes |
|
1314 |
clr ditherPattern |
|
1315 |
nColors "{Class: SmallInteger }" |
|
1316 |
w "{Class: SmallInteger }" |
|
1317 |
h "{Class: SmallInteger }"| |
|
1318 |
||
1319 |
nColors := map size. |
|
1320 |
w := width. |
|
1321 |
h := height. |
|
1322 |
||
1323 |
formBytes := ByteArray uninitializedNew:(w + 7 // 8) * h. |
|
1324 |
patterns := Array new:nColors. |
|
1325 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
1326 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1327 |
||
1328 |
" |
|
1329 |
extract dither patterns and values to use for 1/0 bits |
|
1330 |
in those from the dithercolors |
|
1331 |
" |
|
1332 |
1 to:nColors do:[:i | |
|
1333 |
clr := (map at:i) on:aDevice. |
|
1334 |
ditherPattern := clr ditherForm. |
|
1335 |
||
1336 |
ditherPattern isNil ifTrue:[ |
|
1337 |
patterns at:i put:#[2r11111111 |
|
1338 |
2r11111111 |
|
1339 |
2r11111111 |
|
1340 |
2r11111111 |
|
1341 |
2r11111111 |
|
1342 |
2r11111111 |
|
1343 |
2r11111111 |
|
1344 |
2r11111111]. |
|
1345 |
pixel0bytes at:i put:clr colorId. |
|
1346 |
pixel1bytes at:i put:clr colorId |
|
1347 |
] ifFalse:[ |
|
1348 |
patterns at:i put:(ditherPattern bits). |
|
1349 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
1350 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
1351 |
]. |
|
1352 |
]. |
|
1353 |
||
1354 |
%{ |
|
1355 |
unsigned char *_srcP, *_dstP; |
|
1356 |
OBJ _patternBytes; |
|
1357 |
unsigned char _mask = 0x80; |
|
1358 |
unsigned char _outBits = 0; |
|
1359 |
unsigned char _last, _v, _patternBits, _p0, _p1; |
|
1360 |
int _h, _w; |
|
1361 |
int _patternOffset = 0; |
|
1362 |
||
1363 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1364 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element; |
|
1365 |
for (_h = _intVal(h); _h; _h--) { |
|
1366 |
_last = -1; |
|
1367 |
for (_w = _intVal(w); _w; _w--) { |
|
1368 |
_v = *_srcP++; |
|
1369 |
if (_v != _last) { |
|
1370 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v]; |
|
1371 |
if (__isByteArray(_patternBytes)) { |
|
1372 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset]; |
|
1373 |
} else if (__isArray(_patternBytes)) { |
|
1374 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]); |
|
1375 |
} |
|
1376 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v]; |
|
1377 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v]; |
|
1378 |
_last = _v; |
|
1379 |
} |
|
1380 |
_outBits <<= 1; |
|
1381 |
if (_patternBits & _mask) |
|
1382 |
_outBits |= _p1; |
|
1383 |
else |
|
1384 |
_outBits |= _p0; |
|
1385 |
_mask >>= 1; |
|
1386 |
if (_mask == 0) { |
|
1387 |
_mask = 0x80; |
|
1388 |
*_dstP++ = _outBits; |
|
1389 |
_outBits = 0; |
|
1390 |
} |
|
1391 |
} |
|
1392 |
if (_mask != 0x80) { |
|
1393 |
while (_mask != 0) { |
|
1394 |
_outBits <<= 1; |
|
1395 |
_mask >>= 1; |
|
1396 |
} |
|
1397 |
*_dstP++ = _outBits; |
|
1398 |
_mask = 0x80; |
|
1399 |
_outBits = 0; |
|
1400 |
} |
|
1401 |
_patternOffset++; |
|
1402 |
if (_patternOffset == 8) |
|
1403 |
_patternOffset = 0; |
|
1404 |
} |
|
1405 |
%} |
|
1406 |
. |
|
1407 |
f := Form width:w height:h fromArray:formBytes. |
|
1408 |
^ f |
|
1409 |
! |
|
1410 |
||
1411 |
dither2PlaneUsingMap:map on:aDevice |
|
1412 |
"a helper for dithering palette and greyscale images" |
|
1413 |
||
1414 |
|f |
|
1415 |
patterns formBytes |
|
1416 |
pixel0bytes pixel1bytes |
|
1417 |
clr ditherPattern |
|
1418 |
nColors "{Class: SmallInteger }" |
|
1419 |
w "{Class: SmallInteger }" |
|
1420 |
h "{Class: SmallInteger }"| |
|
1421 |
||
1422 |
nColors := map size. |
|
1423 |
w := width. |
|
1424 |
h := height. |
|
1425 |
||
1426 |
formBytes := ByteArray uninitializedNew:(w * 2 + 7 // 8) * h. |
|
1427 |
patterns := Array new:nColors. |
|
1428 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
1429 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1430 |
||
1431 |
"extract dither patterns and values to use for 1/0 bits |
|
1432 |
in those from the dithercolors" |
|
1433 |
||
1434 |
1 to:nColors do:[:i | |
|
1435 |
clr := (map at:i) on:aDevice. |
|
1436 |
ditherPattern := clr ditherForm. |
|
1437 |
||
1438 |
ditherPattern isNil ifTrue:[ |
|
1439 |
patterns at:i put:#[2r11111111 |
|
1440 |
2r11111111 |
|
1441 |
2r11111111 |
|
1442 |
2r11111111 |
|
1443 |
2r11111111 |
|
1444 |
2r11111111 |
|
1445 |
2r11111111 |
|
1446 |
2r11111111]. |
|
1447 |
pixel0bytes at:i put:clr colorId. |
|
1448 |
pixel1bytes at:i put:clr colorId |
|
1449 |
] ifFalse:[ |
|
1450 |
patterns at:i put:(ditherPattern bits). |
|
1451 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
1452 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
1453 |
]. |
|
1454 |
]. |
|
1455 |
||
1456 |
%{ |
|
1457 |
unsigned char *_srcP, *_dstP; |
|
1458 |
OBJ _patternBytes; |
|
1459 |
unsigned char _mask = 0x80; |
|
1460 |
unsigned char _outBits = 0; |
|
1461 |
unsigned char _last, _v, _patternBits, _p0, _p1; |
|
1462 |
int _h, _w; |
|
1463 |
int _patternOffset = 0; |
|
1464 |
int _outCount; |
|
1465 |
||
1466 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1467 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element; |
|
1468 |
for (_h = _intVal(h); _h; _h--) { |
|
1469 |
_last = -1; |
|
1470 |
_outCount = 0; |
|
1471 |
for (_w = _intVal(w); _w; _w--) { |
|
1472 |
_v = *_srcP++; |
|
1473 |
if (_v != _last) { |
|
1474 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v]; |
|
1475 |
if (__isByteArray(_patternBytes)) { |
|
1476 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset]; |
|
1477 |
} else if (__isArray(_patternBytes)) { |
|
1478 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]); |
|
1479 |
} |
|
1480 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v]; |
|
1481 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v]; |
|
1482 |
_last = _v; |
|
1483 |
} |
|
1484 |
_outBits <<= 2; |
|
1485 |
if (_patternBits & _mask) |
|
1486 |
_outBits |= _p1; |
|
1487 |
else |
|
1488 |
_outBits |= _p0; |
|
1489 |
_mask >>= 1; |
|
1490 |
_outCount++; |
|
1491 |
if (_outCount == 4) { |
|
1492 |
*_dstP++ = _outBits; |
|
1493 |
_outCount = 0; |
|
1494 |
if (_mask == 0) { |
|
1495 |
_mask = 0x80; |
|
1496 |
} |
|
1497 |
} |
|
1498 |
} |
|
1499 |
if (_outCount) { |
|
1500 |
do { |
|
1501 |
_outBits <<= 2; |
|
1502 |
} while (++_outCount != 4); |
|
1503 |
*_dstP++ = _outBits; |
|
1504 |
} |
|
1505 |
_mask = 0x80; |
|
1506 |
_outBits = 0; |
|
1507 |
_patternOffset++; |
|
1508 |
if (_patternOffset == 8) |
|
1509 |
_patternOffset = 0; |
|
1510 |
} |
|
1511 |
%} |
|
1512 |
. |
|
1513 |
f := Form width:w height:h depth:2. |
|
1514 |
f initGC. |
|
1515 |
f device drawBits:formBytes depth:2 width:w height:h x:0 y:0 |
|
1516 |
into:f id x:0 y:0 width:w height:h with:f gcId. |
|
1517 |
^ f |
|
1518 |
! ! |
|
1519 |
||
1520 |
!Depth8Image methodsFor:'queries'! |
|
1521 |
||
1522 |
bitsPerPixel |
|
1523 |
"return the number of bits per pixel" |
|
1524 |
||
1525 |
^ 8 |
|
1526 |
! |
|
1527 |
||
1528 |
bitsPerRow |
|
1529 |
"return the number of bits in one scanline of the image" |
|
1530 |
||
1531 |
^ width * 8 |
|
1532 |
! |
|
1533 |
||
1534 |
bytesPerRow |
|
1535 |
"return the number of bytes in one scanline of the image" |
|
1536 |
||
1537 |
^ width |
|
1538 |
! |
|
1539 |
||
1540 |
usedValues |
|
1541 |
"return a collection of color values used in the receiver." |
|
1542 |
||
1543 |
^ bytes usedValues |
|
1544 |
! ! |
|
1545 |
||
1546 |
!Depth8Image class methodsFor:'documentation'! |
|
1547 |
||
1548 |
version |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1549 |
^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.46 1996-06-10 17:24:08 cg Exp $' |
579 | 1550 |
! ! |