author | Claus Gittinger <cg@exept.de> |
Fri, 14 Jun 1996 21:34:11 +0200 | |
changeset 859 | 34268959162d |
parent 855 | 9b5aa569679f |
child 875 | b23541f72152 |
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 |
||
853 | 114 |
asGray8FormOn:aDevice |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
115 |
"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
|
116 |
Redefined, since only a translation has to be done here." |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
117 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
118 |
|greyBits map |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
119 |
mapSize "{ Class: SmallInteger }"| |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
120 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
121 |
greyBits := ByteArray uninitializedNew:(width * height). |
1 | 122 |
|
123 |
map := ByteArray uninitializedNew:256. |
|
124 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
125 |
1 to:256 do:[:i | |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
126 |
map at:i put:((self colorFromValue:(i-1)) brightness * 255) rounded |
1 | 127 |
]. |
89 | 128 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
129 |
"/ mapSize := colorMap size. |
89 | 130 |
"/ |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
131 |
"/ 1 to:mapSize do:[:i | |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
132 |
"/ map at:i put:((colorMap at:i) brightness * 255) rounded |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
133 |
"/ ]. |
1 | 134 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
135 |
bytes expandPixels:8 "xlate only" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
136 |
width:width |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
137 |
height:height |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
138 |
into:greyBits |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
139 |
mapping:map. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
140 |
|
816 | 141 |
^ self makeDeviceGrayPixmapOn:aDevice depth:8 fromArray:greyBits |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
142 |
|
816 | 143 |
"Modified: 10.6.1996 / 20:10:14 / cg" |
853 | 144 |
"Created: 14.6.1996 / 15:23:09 / cg" |
145 |
! |
|
146 |
||
147 |
asGrayFormOn:aDevice |
|
148 |
"get a gray device form. |
|
149 |
Redefined, since we can do it with simple translate, |
|
150 |
if the depth matches my depth." |
|
151 |
||
152 |
(aDevice visualType == #StaticGray) ifTrue:[ |
|
153 |
(aDevice depth == 8) ifTrue:[ |
|
154 |
^ self asGray8FormOn:aDevice |
|
155 |
]. |
|
156 |
]. |
|
157 |
^ super asGrayFormOn:aDevice |
|
158 |
||
159 |
"Created: 10.6.1996 / 18:51:19 / cg" |
|
160 |
"Modified: 10.6.1996 / 18:54:36 / cg" |
|
1 | 161 |
! |
162 |
||
163 |
paletteImageAsPseudoFormOn:aDevice |
|
164 |
"return a pseudoForm from the palette picture. The main work is |
|
165 |
in color reduction, when not all colors can be aquired." |
|
166 |
||
46 | 167 |
|pseudoBits f gcRound has8BitImage deviceDepth |
168 |
imgMap newImage pxl |
|
154 | 169 |
usedColors usageCounts maxIndex map |
89 | 170 |
fit scale lastOK error |
154 | 171 |
div |
172 |
bitsPerRGB "{Class: SmallInteger }" |
|
173 |
shift "{Class: SmallInteger }" |
|
174 |
m "{Class: SmallInteger }" | |
|
1 | 175 |
|
89 | 176 |
Color fixColors notNil ifTrue:[ |
855 | 177 |
f := self |
178 |
asFloydSteinbergDitheredDepth8FormOn:aDevice |
|
179 |
colors:Color fixColors |
|
180 |
nRed:Color numFixRed |
|
181 |
nGreen:Color numFixGreen |
|
182 |
nBlue:Color numFixBlue. |
|
183 |
f notNil ifTrue:[^ f]. |
|
89 | 184 |
]. |
185 |
||
1 | 186 |
"find used colors" |
187 |
||
154 | 188 |
usedColors := bytes usedValues. "gets us an array filled with used values" |
855 | 189 |
"(could use bytes asBag)" |
154 | 190 |
maxIndex := usedColors max + 1. |
191 |
||
180 | 192 |
usedColors size > 20 ifTrue:[ |
855 | 193 |
('D8IMAGE: allocating ' , usedColors size printString , ' colors ...') infoPrintNL. |
180 | 194 |
]. |
1 | 195 |
|
196 |
"sort by usage" |
|
197 |
usageCounts := bytes usageCounts. |
|
198 |
usageCounts := usedColors asArray collect:[:clr | usageCounts at:(clr + 1)]. |
|
199 |
usageCounts sort:[:a :b | a > b] with:usedColors. |
|
200 |
||
201 |
"allocate the colors (in order of usage count)" |
|
202 |
||
154 | 203 |
imgMap := Array new:maxIndex. |
1 | 204 |
|
89 | 205 |
" |
206 |
first, try to get the exact colors ... |
|
207 |
" |
|
154 | 208 |
bitsPerRGB := aDevice bitsPerRGB. |
209 |
shift := (8 - bitsPerRGB) negated. |
|
210 |
m := (1 bitShift:bitsPerRGB) - 1. |
|
1 | 211 |
div := m asFloat. |
212 |
||
213 |
fit := true. |
|
214 |
scale := 100.0 / div. "to scale 0..255 into 0.0 .. 100.0" |
|
215 |
lastOK := 0. |
|
89 | 216 |
gcRound := 0. |
217 |
||
1 | 218 |
usedColors do:[:aColorIndex | |
855 | 219 |
|devColor color |
220 |
r "{Class: SmallInteger }" |
|
221 |
g "{Class: SmallInteger }" |
|
222 |
b "{Class: SmallInteger }" |
|
859 | 223 |
mapIndex "{Class: SmallInteger }"| |
1 | 224 |
|
855 | 225 |
fit ifTrue:[ |
226 |
mapIndex := aColorIndex + 1. |
|
227 |
color := colorMap at:mapIndex. |
|
859 | 228 |
(color colorId notNil |
229 |
and:[color device == aDevice]) ifTrue:[ |
|
855 | 230 |
"wow - an immediate hit" |
231 |
devColor := color |
|
232 |
] ifFalse:[ |
|
233 |
devColor := color exactOn:aDevice. |
|
234 |
devColor isNil ifTrue:[ |
|
235 |
" |
|
236 |
could not allocate color - on the first round, do a GC to flush |
|
237 |
unused colors - this may help if some colors where locked by |
|
238 |
already free images. |
|
239 |
" |
|
240 |
gcRound == 0 ifTrue:[ |
|
241 |
ObjectMemory scavenge; finalize. |
|
242 |
devColor := color exactOn:aDevice. |
|
243 |
gcRound := 1 |
|
244 |
]. |
|
245 |
devColor isNil ifTrue:[ |
|
246 |
gcRound == 1 ifTrue:[ |
|
247 |
CollectGarbageWhenRunningOutOfColors ifTrue:[ |
|
248 |
'D8IMAGE: force GC for possible color reclamation.' infoPrintNL. |
|
249 |
ObjectMemory incrementalGC; finalize. |
|
250 |
devColor := color exactOn:aDevice. |
|
251 |
]. |
|
252 |
gcRound := 2 |
|
253 |
] |
|
254 |
] |
|
255 |
]. |
|
256 |
]. |
|
257 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[ |
|
258 |
imgMap at:mapIndex put:devColor. |
|
259 |
lastOK := lastOK + 1. |
|
260 |
] ifFalse:[ |
|
261 |
fit := false |
|
262 |
] |
|
263 |
] |
|
1 | 264 |
]. |
265 |
||
266 |
fit ifFalse:[ |
|
157 | 267 |
"/ |tree| |
268 |
"/ |
|
269 |
"/ "/ first, create an octTree containing colors which we got ... |
|
270 |
"/ tree := ColorOctree new. |
|
271 |
"/ |
|
272 |
"/ usedColors from:1 to:lastOK do:[:aColorIndex | |
|
273 |
"/ tree insert:(colorMap at:aColorIndex + 1). |
|
274 |
"/ ]. |
|
275 |
"/ |
|
276 |
"/ "/ then, remap remaining, using nearest from those already allocated |
|
277 |
"/ |
|
278 |
"/ usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
279 |
"/ |mapIndex| |
|
280 |
"/ |
|
281 |
"/ mapIndex := aColorIndex + 1. |
|
282 |
"/ imgMap at:mapIndex put:(tree findBest:(colorMap at:mapIndex)) |
|
283 |
"/ ]. |
|
284 |
||
859 | 285 |
fit ifFalse:[ |
286 |
('D8IMAGE: got %1 exact colors (out of %2)' bindWith:lastOK with:usedColors size) infoPrintCR. |
|
287 |
]. |
|
288 |
||
855 | 289 |
" |
290 |
again, this time allow wrong colors (loop while increasing allowed error) |
|
291 |
" |
|
859 | 292 |
error := 1. |
855 | 293 |
[fit] whileFalse:[ |
294 |
fit := true. |
|
295 |
usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
296 |
|devColor color |
|
297 |
r "{Class: SmallInteger }" |
|
298 |
g "{Class: SmallInteger }" |
|
299 |
b "{Class: SmallInteger }" |
|
300 |
mapIndex "{Class: SmallInteger }" |
|
301 |
rMask "{Class: SmallInteger }" |
|
302 |
gMask "{Class: SmallInteger }" |
|
303 |
bMask "{Class: SmallInteger }"| |
|
1 | 304 |
|
855 | 305 |
fit ifTrue:[ |
306 |
gMask := bMask := rMask := m. |
|
1 | 307 |
|
855 | 308 |
mapIndex := aColorIndex + 1. |
309 |
color := colorMap at:mapIndex. |
|
859 | 310 |
r := (color red * 255 / 100.0) rounded. |
311 |
g := (color green * 255 / 100.0) rounded. |
|
312 |
b := (color blue * 255 / 100.0) rounded. |
|
89 | 313 |
|
855 | 314 |
color := Color red:((r bitShift:shift) bitAnd:rMask) * scale |
315 |
green:((g bitShift:shift) bitAnd:gMask) * scale |
|
316 |
blue:((b bitShift:shift) bitAnd:bMask) * scale. |
|
89 | 317 |
|
859 | 318 |
(color colorId notNil |
319 |
and:[color device == aDevice]) ifTrue:[ |
|
855 | 320 |
"wow - an immediate hit" |
859 | 321 |
devColor := color. |
855 | 322 |
] ifFalse:[ |
859 | 323 |
devColor := color nearestOn:aDevice. |
324 |
(devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ |
|
325 |
devColor := nil |
|
326 |
]. |
|
855 | 327 |
devColor isNil ifTrue:[ |
328 |
" |
|
329 |
no free color - on the first round, do a GC to flush unused |
|
330 |
colors - this may help if some colors where locked by already |
|
331 |
free images. |
|
332 |
" |
|
333 |
gcRound == 0 ifTrue:[ |
|
334 |
ObjectMemory scavenge; finalize. |
|
859 | 335 |
devColor := color nearestOn:aDevice. |
336 |
(devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ |
|
337 |
devColor := nil |
|
338 |
]. |
|
855 | 339 |
gcRound := 1 |
340 |
]. |
|
341 |
devColor isNil ifTrue:[ |
|
342 |
gcRound == 1 ifTrue:[ |
|
343 |
CollectGarbageWhenRunningOutOfColors ifTrue:[ |
|
344 |
'D8IMAGE: force GC for possible color reclamation.' infoPrintNL. |
|
345 |
ObjectMemory incrementalGC; finalize. |
|
859 | 346 |
devColor := color nearestOn:aDevice. |
347 |
(devColor notNil and:[(devColor deltaFrom:color) > error]) ifTrue:[ |
|
348 |
devColor := nil |
|
349 |
]. |
|
855 | 350 |
]. |
351 |
gcRound := 2 |
|
352 |
] |
|
353 |
] |
|
354 |
]. |
|
355 |
]. |
|
356 |
(devColor notNil and:[devColor colorId notNil]) ifTrue:[ |
|
357 |
imgMap at:mapIndex put:devColor. |
|
358 |
lastOK := lastOK + 1. |
|
359 |
] ifFalse:[ |
|
360 |
fit := false |
|
361 |
] |
|
362 |
]. |
|
363 |
]. |
|
859 | 364 |
|
365 |
fit ifTrue:[ |
|
366 |
('D8IMAGE: remaining colors with error <= %1' bindWith:error) infoPrintCR. |
|
367 |
]. |
|
368 |
||
855 | 369 |
error := error * 2. |
859 | 370 |
error > 100 ifTrue:[ |
855 | 371 |
" |
372 |
break out, if the error becomes too big. |
|
373 |
" |
|
374 |
'D8IMAGE: hard color allocation problem - revert to b&w' infoPrintNL. |
|
375 |
" |
|
376 |
map to b&w as a last fallback. |
|
377 |
(should really do a dither here) |
|
378 |
" |
|
379 |
usedColors from:(lastOK+1) to:(usedColors size) do:[:aColorIndex | |
|
380 |
|color |
|
381 |
mapIndex "{ Class: SmallInteger }"| |
|
89 | 382 |
|
855 | 383 |
mapIndex := aColorIndex + 1. |
384 |
color := colorMap at:mapIndex. |
|
859 | 385 |
color brightness > 0.4 ifTrue:[ |
855 | 386 |
color := Color white. |
387 |
] ifFalse:[ |
|
388 |
color := Color black. |
|
389 |
]. |
|
390 |
imgMap at:mapIndex put:(color on:aDevice). |
|
391 |
]. |
|
392 |
fit := true. |
|
393 |
] |
|
394 |
]. |
|
1 | 395 |
|
859 | 396 |
error > 10 ifTrue:[ |
855 | 397 |
'D8IMAGE: not enough colors for a reasonable image' infoPrintNL |
398 |
] ifFalse:[ |
|
399 |
'D8IMAGE: not enough colors for exact picture' infoPrintNL. |
|
400 |
] |
|
1 | 401 |
]. |
402 |
||
89 | 403 |
" |
404 |
create translation map (from image colors to allocated colorIds) |
|
405 |
" |
|
46 | 406 |
map := ByteArray new:256. |
1 | 407 |
1 to:imgMap size do:[:i | |
855 | 408 |
|clr| |
154 | 409 |
|
855 | 410 |
(clr := imgMap at:i) notNil ifTrue:[ |
411 |
map at:i put:clr colorId |
|
412 |
] |
|
1 | 413 |
]. |
414 |
||
154 | 415 |
" |
416 |
does the device support 8-bit images ? |
|
417 |
" |
|
46 | 418 |
deviceDepth := aDevice depth. |
419 |
deviceDepth == 8 ifTrue:[ |
|
855 | 420 |
has8BitImage := true. |
46 | 421 |
] ifFalse:[ |
855 | 422 |
has8BitImage := false. |
423 |
aDevice supportedImageFormats do:[:fmt | |
|
424 |
(fmt at:#bitsPerPixel) == 8 ifTrue:[ |
|
425 |
has8BitImage := true. |
|
426 |
] |
|
427 |
] |
|
46 | 428 |
]. |
429 |
||
89 | 430 |
" |
431 |
finally, create a form on the device and copy (& translate) |
|
432 |
the pixel values |
|
433 |
" |
|
46 | 434 |
has8BitImage ifTrue:[ |
855 | 435 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
46 | 436 |
|
855 | 437 |
bytes expandPixels:8 "xlate only" |
438 |
width:width |
|
439 |
height:height |
|
440 |
into:pseudoBits |
|
441 |
mapping:map. |
|
46 | 442 |
|
855 | 443 |
map := nil. |
1 | 444 |
|
855 | 445 |
f := Form width:width height:height depth:deviceDepth on:aDevice. |
446 |
f isNil ifTrue:[^ nil]. |
|
447 |
f colorMap:imgMap. |
|
448 |
f initGC. |
|
449 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:deviceDepth |
|
450 |
width:width height:height |
|
451 |
x:0 y:0 |
|
452 |
into:(f id) x:0 y:0 |
|
453 |
width:width height:height with:(f gcId). |
|
454 |
^ f |
|
46 | 455 |
]. |
1 | 456 |
|
46 | 457 |
" |
458 |
slow fall back: convert into appropriate depth image, |
|
459 |
by looping over each pixel individually |
|
460 |
" |
|
461 |
newImage := (Image implementorForDepth:deviceDepth) new. |
|
462 |
newImage width:width. |
|
463 |
newImage height:height. |
|
464 |
newImage bits:(ByteArray uninitializedNew:(height * newImage bytesPerRow)). |
|
465 |
||
466 |
0 to:height-1 do:[:row | |
|
855 | 467 |
0 to:width-1 do:[:col | |
468 |
pxl := self valueAtX:col y:row. |
|
469 |
newImage atX:col y:row putValue:(map at:pxl) |
|
470 |
] |
|
46 | 471 |
]. |
472 |
||
473 |
f := Form width:width height:height depth:deviceDepth on:aDevice. |
|
1 | 474 |
f isNil ifTrue:[^ nil]. |
475 |
f colorMap:imgMap. |
|
476 |
f initGC. |
|
46 | 477 |
|
478 |
aDevice drawBits:(newImage bits) depth:deviceDepth width:width height:height |
|
855 | 479 |
x:0 y:0 |
480 |
into:(f id) x:0 y:0 |
|
481 |
width:width height:height with:(f gcId). |
|
46 | 482 |
|
1 | 483 |
^ f |
855 | 484 |
|
859 | 485 |
"Modified: 14.6.1996 / 20:47:29 / cg" |
1 | 486 |
! ! |
487 |
||
810 | 488 |
!Depth8Image methodsFor:'dither helpers'! |
489 |
||
490 |
orderedDitheredBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
|
491 |
"return the bitmap for a dithered depth-bitmap from the image; |
|
492 |
with a constant ditherMatrix, this can be used for thresholding. |
|
493 |
Redefined to make use of knowing that pixels are 8-bit values." |
|
494 |
||
495 |
|dH nDither |
|
496 |
greyLevels greyMap1 greyMap2 |
|
497 |
bytesPerRow "{Class: SmallInteger }" |
|
498 |
bytesPerOutRow "{Class: SmallInteger }" |
|
499 |
pixelsPerByte "{Class: SmallInteger }" |
|
500 |
outBits |
|
501 |
w "{Class: SmallInteger }" |
|
502 |
h "{Class: SmallInteger }" | |
|
503 |
||
504 |
nDither := ditherMatrix size. |
|
505 |
dH := nDither / dW. |
|
506 |
||
507 |
w := width. |
|
508 |
h := height. |
|
509 |
||
510 |
greyLevels := 1 bitShift:depth. |
|
511 |
pixelsPerByte := 8 / depth. |
|
512 |
||
513 |
bytesPerRow := self bytesPerRow. |
|
514 |
||
515 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
516 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
517 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
518 |
^ nil |
|
519 |
]. |
|
520 |
||
521 |
greyMap1 := self greyMapForRange:(greyLevels-1). "/ the pixels |
|
522 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
|
523 |
0 |
|
524 |
] ifFalse:[ |
|
525 |
b truncated |
|
526 |
] |
|
527 |
]) asByteArray. |
|
528 |
||
529 |
greyMap2 := self greyMapForRange:(greyLevels-1). |
|
530 |
greyMap2 := (greyMap2 collect:[:el | |
|
531 |
el isNil ifTrue:[ |
|
532 |
0 |
|
533 |
] ifFalse:[ |
|
534 |
((el - el truncated) "/ the error (0..1) |
|
535 |
* nDither) rounded |
|
536 |
]]) asByteArray. |
|
537 |
||
538 |
%{ |
|
539 |
int __dW = __intVal(dW); |
|
540 |
int __dH = __intVal(dH); |
|
541 |
int __byte; |
|
542 |
int __dT, __dO; |
|
543 |
int __depth = __intVal(depth); |
|
544 |
int __dstIdx = 0; |
|
545 |
int __srcIdx = 0; |
|
546 |
int __bitCnt; |
|
547 |
int __grey, __pixel; |
|
548 |
int __w = __intVal(w); |
|
549 |
int __h = __intVal(h); |
|
550 |
int __x; |
|
551 |
int __y; |
|
552 |
int __oX, __oY, __dY; |
|
553 |
int __nextDst; |
|
554 |
int __nextSrc; |
|
555 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
556 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
|
557 |
||
558 |
char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
|
559 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
560 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
561 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
|
562 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
|
563 |
||
564 |
__oY = __dY = 0; |
|
565 |
for (__y=0; __y<__h; __y++) { |
|
566 |
__nextDst = __dstIdx + __bytesPerOutRow; |
|
567 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
568 |
||
569 |
__byte = 0; |
|
570 |
__bitCnt = 8; |
|
571 |
||
572 |
__oX = 0; |
|
573 |
||
574 |
for (__x=0; __x<__w; __x++) { |
|
575 |
__grey = __bytes[__srcIdx++]; /* 0..255 */ |
|
576 |
||
577 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
|
578 |
__dO = __greyMap2[__grey]; /* 0.. nDither-1) */ |
|
579 |
__dT = __ditherMatrix[__dY + __oX]; |
|
580 |
||
581 |
if (__dO > __dT) /* dither says: next pixel */ |
|
582 |
__pixel++; |
|
583 |
||
584 |
__oX++; |
|
585 |
if (__oX == __dW) __oX = 0; |
|
586 |
||
587 |
__byte = (__byte << __depth) | __pixel; |
|
588 |
||
589 |
__bitCnt = __bitCnt - __depth; |
|
590 |
if (__bitCnt == 0) { |
|
591 |
__outBits[__dstIdx] = __byte; |
|
592 |
__dstIdx++; |
|
593 |
__byte = 0; |
|
594 |
__bitCnt = 8; |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
if (__bitCnt != 8) { |
|
599 |
__byte = __byte << __bitCnt; |
|
600 |
__outBits[__dstIdx] = __byte; |
|
601 |
} |
|
602 |
||
603 |
__oY++; __dY += __dW; |
|
604 |
if (__oY == __dH) { |
|
605 |
__oY = 0; |
|
606 |
__dY = 0; |
|
607 |
} |
|
608 |
||
609 |
__srcIdx = __nextSrc; |
|
610 |
__dstIdx = __nextDst; |
|
611 |
} |
|
612 |
%}. |
|
613 |
^ outBits |
|
614 |
! |
|
615 |
||
616 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
|
617 |
"return the dithered monochrome bits for the receiver image; |
|
618 |
with a constant ditherMatrix, this can be used for thresholding. |
|
619 |
Redefined to make use of knowing that pixels are 8-bit values." |
|
620 |
||
621 |
|f dH nDither |
|
622 |
greyMap monoBits |
|
623 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
624 |
bytesPerRow "{Class: SmallInteger }" |
|
625 |
w "{Class: SmallInteger }" |
|
626 |
h "{Class: SmallInteger }"| |
|
627 |
||
628 |
nDither := ditherMatrix size. |
|
629 |
dH := nDither / dW. |
|
630 |
||
631 |
w := width. |
|
632 |
h := height. |
|
633 |
||
634 |
bytesPerRow := self bytesPerRow. |
|
635 |
||
636 |
bytesPerMonoRow := w + 7 // 8. |
|
637 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
638 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
639 |
^ nil |
|
640 |
]. |
|
641 |
||
642 |
greyMap := self greyByteMapForRange:nDither. |
|
643 |
||
644 |
%{ |
|
645 |
int __dW = __intVal(dW); |
|
646 |
int __dH = __intVal(dH); |
|
647 |
int __byte; |
|
648 |
int __dT; |
|
649 |
int __dstIdx = 0; |
|
650 |
int __srcIdx = 0; |
|
651 |
int __bitCnt; |
|
652 |
int __grey; |
|
653 |
int __w = __intVal(w); |
|
654 |
int __h = __intVal(h); |
|
655 |
int __x; |
|
656 |
int __y; |
|
657 |
int __oX, __oY, __dY; |
|
658 |
int __nextDst; |
|
659 |
int __nextSrc; |
|
660 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
661 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
662 |
||
663 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
664 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
665 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
666 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
667 |
||
668 |
__oY = __dY = 0; |
|
669 |
for (__y=0; __y<__h; __y++) { |
|
670 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
671 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
672 |
||
673 |
__byte = 0; |
|
674 |
__bitCnt = 8; |
|
675 |
||
676 |
__oX = 0; |
|
677 |
||
678 |
for (__x=__w; __x>0; __x--) { |
|
679 |
__grey = __bytes[__srcIdx]; /* 0..255 */ |
|
680 |
__srcIdx++; |
|
681 |
||
682 |
__grey = __greyMap[__grey]; |
|
683 |
__dT = __ditherMatrix[__dY + __oX]; |
|
684 |
||
685 |
__oX++; |
|
686 |
if (__oX == __dW) __oX = 0; |
|
687 |
||
688 |
__byte = __byte << 1; |
|
689 |
if (__grey > __dT) { |
|
690 |
__byte = __byte | 1; /* white */ |
|
691 |
} |
|
692 |
||
693 |
__bitCnt--; |
|
694 |
if (__bitCnt == 0) { |
|
695 |
__monoBits[__dstIdx] = __byte; |
|
696 |
__dstIdx++; |
|
697 |
__byte = 0; |
|
698 |
__bitCnt = 8; |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
if (__bitCnt != 8) { |
|
703 |
__byte = __byte << __bitCnt; |
|
704 |
__monoBits[__dstIdx] = __byte; |
|
705 |
} |
|
706 |
||
707 |
__oY++; __dY += __dW; |
|
708 |
if (__oY == __dH) { |
|
709 |
__oY = 0; |
|
710 |
__dY = 0; |
|
711 |
} |
|
712 |
||
713 |
__srcIdx = __nextSrc; |
|
714 |
__dstIdx = __nextDst; |
|
715 |
} |
|
716 |
%}. |
|
717 |
||
718 |
^ monoBits |
|
719 |
||
720 |
" |
|
721 |
|i f| |
|
722 |
||
723 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
724 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
725 |
||
726 |
||
727 |
|i f| |
|
728 |
||
729 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
730 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
731 |
||
732 |
||
733 |
|i f| |
|
734 |
||
735 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
736 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
737 |
||
738 |
||
739 |
|i f| |
|
740 |
||
741 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
742 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
743 |
" |
|
744 |
||
745 |
"Created: 7.6.1996 / 10:48:06 / cg" |
|
746 |
"Modified: 7.6.1996 / 11:08:50 / cg" |
|
747 |
! ! |
|
748 |
||
579 | 749 |
!Depth8Image methodsFor:'enumerating'! |
1 | 750 |
|
798 | 751 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
752 |
"perform aBlock for each pixel from x1 to x2 in row y. |
|
753 |
The block is passed the color at each pixel. |
|
754 |
This method allows slighly faster processing of an |
|
755 |
image than using atX:y:, since some processing can be |
|
756 |
avoided when going from pixel to pixel. However, for |
|
757 |
real image processing, specialized methods should be written." |
|
758 |
||
759 |
|srcIndex "{ Class: SmallInteger }" |
|
760 |
value "{ Class: SmallInteger }" |
|
761 |
x1 "{ Class: SmallInteger }" |
|
762 |
x2 "{ Class: SmallInteger }" |
|
810 | 763 |
color colorArray scale gr| |
798 | 764 |
|
765 |
x1 := xLow. |
|
766 |
x2 := xHigh. |
|
767 |
srcIndex := (width * y) + 1 + x1. |
|
768 |
||
810 | 769 |
colorArray := Array new:256. |
770 |
||
798 | 771 |
photometric == #blackIs0 ifTrue:[ |
810 | 772 |
scale := 100.0 / 255.0. |
798 | 773 |
|
774 |
x1 to:x2 do:[:x | |
|
775 |
value := bytes at:srcIndex. |
|
776 |
srcIndex := srcIndex + 1. |
|
777 |
||
810 | 778 |
color := colorArray at:value+1. |
746 | 779 |
color isNil ifTrue:[ |
810 | 780 |
color := Color gray:(value * scale). |
781 |
colorArray at:value+1 put:color |
|
798 | 782 |
]. |
783 |
aBlock value:x value:color |
|
746 | 784 |
]. |
798 | 785 |
^ self |
786 |
]. |
|
787 |
||
788 |
photometric == #whiteIs0 ifTrue:[ |
|
810 | 789 |
scale := 100.0 / 255.0. |
798 | 790 |
|
791 |
x1 to:x2 do:[:x | |
|
810 | 792 |
value := bytes at:srcIndex. |
798 | 793 |
srcIndex := srcIndex + 1. |
794 |
||
810 | 795 |
color := colorArray at:value+1. |
798 | 796 |
color isNil ifTrue:[ |
810 | 797 |
gr := 255 - value. |
798 |
color := Color gray:(gr * scale). |
|
799 |
colorArray at:value+1 put:color |
|
798 | 800 |
]. |
801 |
aBlock value:x value:color |
|
802 |
]. |
|
803 |
^ self |
|
804 |
]. |
|
805 |
||
806 |
photometric ~~ #palette ifTrue:[ |
|
810 | 807 |
x1 to:x2 do:[:x | |
808 |
value := bytes at:srcIndex. |
|
809 |
srcIndex := srcIndex + 1. |
|
810 |
||
811 |
color := colorArray at:value+1. |
|
812 |
color isNil ifTrue:[ |
|
813 |
color := self colorFromValue:value. |
|
814 |
colorArray at:value+1 put:color |
|
815 |
]. |
|
816 |
aBlock value:x value:color |
|
817 |
]. |
|
818 |
^ self |
|
798 | 819 |
]. |
820 |
||
810 | 821 |
colorArray := colorMap. |
802 | 822 |
|
798 | 823 |
x1 to:x2 do:[:x | |
824 |
value := bytes at:srcIndex. |
|
825 |
srcIndex := srcIndex + 1. |
|
826 |
||
810 | 827 |
color := colorArray at:(value+1). |
746 | 828 |
aBlock value:x value:color |
579 | 829 |
] |
746 | 830 |
|
798 | 831 |
"Created: 7.6.1996 / 19:12:35 / cg" |
810 | 832 |
"Modified: 10.6.1996 / 10:32:07 / cg" |
579 | 833 |
! |
1 | 834 |
|
798 | 835 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
579 | 836 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
837 |
The block is passed the pixelValue at each pixel. |
|
838 |
This method allows slighly faster processing of an |
|
839 |
image than using valueAtX:y:, since some processing can be |
|
840 |
avoided when going from pixel to pixel. However, for |
|
841 |
real image processing, specialized methods should be written." |
|
1 | 842 |
|
579 | 843 |
|srcIndex "{ Class: SmallInteger }" |
844 |
pixelValue "{ Class: SmallInteger }" |
|
845 |
x1 "{ Class: SmallInteger }" |
|
846 |
x2 "{ Class: SmallInteger }"| |
|
1 | 847 |
|
579 | 848 |
x1 := xLow. |
849 |
x2 := xHigh. |
|
850 |
srcIndex := (width * y) + 1 + x1. |
|
851 |
||
852 |
x1 to:x2 do:[:x | |
|
798 | 853 |
pixelValue := bytes at:srcIndex. |
854 |
srcIndex := srcIndex + 1. |
|
855 |
aBlock value:x value:pixelValue |
|
579 | 856 |
] |
798 | 857 |
|
858 |
"Created: 7.6.1996 / 19:09:47 / cg" |
|
1 | 859 |
! ! |
860 |
||
12 | 861 |
!Depth8Image methodsFor:'image manipulations'! |
862 |
||
863 |
flipHorizontal |
|
864 |
"inplace horizontal flip" |
|
865 |
||
866 |
|index "{Class: SmallInteger }" |
|
867 |
h "{Class: SmallInteger }" |
|
868 |
w "{Class: SmallInteger }" |
|
869 |
buffer | |
|
870 |
||
871 |
w := width - 1. |
|
872 |
h := height - 1. |
|
873 |
||
874 |
buffer := ByteArray new:width. |
|
875 |
||
876 |
index := 1. |
|
877 |
0 to:h do:[:row | |
|
71 | 878 |
buffer replaceFrom:1 to:width with:bytes startingAt:index. |
879 |
buffer reverse. |
|
880 |
bytes replaceFrom:index to:index+w with:buffer startingAt:1. |
|
881 |
index := index + w + 1. |
|
12 | 882 |
]. |
883 |
"flush device info" |
|
884 |
self restored |
|
885 |
! |
|
1 | 886 |
|
134 | 887 |
hardMagnifiedBy:extent |
1 | 888 |
"return a new image magnified by extent, aPoint. |
12 | 889 |
This is the general magnification method, handling non-integral values" |
1 | 890 |
|
891 |
|mX mY |
|
892 |
newWidth "{ Class: SmallInteger }" |
|
893 |
newHeight "{ Class: SmallInteger }" |
|
89 | 894 |
w "{ Class: SmallInteger }" |
1 | 895 |
h "{ Class: SmallInteger }" |
896 |
newImage newBytes |
|
897 |
value "{ Class: SmallInteger }" |
|
898 |
srcRowIdx "{ Class: SmallInteger }" |
|
899 |
srcIndex "{ Class: SmallInteger }" |
|
900 |
dstIndex "{ Class: SmallInteger }"| |
|
901 |
||
902 |
mX := extent x. |
|
903 |
mY := extent y. |
|
904 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
905 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
906 |
||
907 |
newWidth := (width * mX) truncated. |
|
908 |
newHeight := (height * mY) truncated. |
|
909 |
||
910 |
newBytes := ByteArray uninitializedNew:(newWidth * newHeight). |
|
911 |
||
912 |
newImage := self species new. |
|
913 |
newImage bits:newBytes. |
|
914 |
newImage width:newWidth. |
|
915 |
newImage height:newHeight. |
|
916 |
newImage photometric:photometric. |
|
917 |
newImage samplesPerPixel:samplesPerPixel. |
|
918 |
newImage bitsPerSample:#(8). |
|
919 |
newImage colorMap:colorMap copy. |
|
920 |
||
921 |
"walk over destination image fetching pixels from source image" |
|
922 |
||
923 |
mY := mY asFloat. |
|
924 |
mX := mX asFloat. |
|
925 |
%{ |
|
926 |
unsigned char *_dstP = _ByteArrayInstPtr(newBytes)->ba_element; |
|
927 |
unsigned char *_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
928 |
unsigned char *_srcRowP; |
|
929 |
int _width = _intVal(_INST(width)); |
|
930 |
int _w = _intVal(newWidth) - 1; |
|
931 |
int _h = _intVal(newHeight) - 1; |
|
932 |
int _row, _col; |
|
933 |
double _mX = _floatVal(mX); |
|
934 |
double _mY = _floatVal(mY); |
|
935 |
||
936 |
for (_row = 0; _row <= _h; _row++) { |
|
71 | 937 |
_srcRowP = _srcP + (_width * (int)((double)_row / _mY)); |
938 |
for (_col = 0; _col <= _w; _col++) { |
|
939 |
*_dstP++ = _srcRowP[(int)((double)_col / _mX)]; |
|
940 |
} |
|
1 | 941 |
} |
942 |
%} |
|
943 |
. |
|
89 | 944 |
"/ the above C-code is equivalent to: |
945 |
"/ |
|
946 |
"/ dstIndex := 1. |
|
947 |
"/ w := newWidth - 1. |
|
948 |
"/ h := newHeight - 1. |
|
949 |
"/ 0 to:h do:[:row | |
|
950 |
"/ srcRowIdx := (width * (row // mY)) + 1. |
|
951 |
"/ 0 to:w do:[:col | |
|
952 |
"/ srcIndex := srcRowIdx + (col // mX). |
|
953 |
"/ value := bytes at:srcIndex. |
|
954 |
"/ newBytes at:dstIndex put:value. |
|
955 |
"/ dstIndex := dstIndex + 1 |
|
956 |
"/ ] |
|
957 |
"/ ]. |
|
958 |
"/ |
|
1 | 959 |
|
960 |
^ newImage |
|
579 | 961 |
! |
962 |
||
963 |
magnifyRowFrom:srcBytes offset:srcStart |
|
964 |
into:dstBytes offset:dstStart factor:mX |
|
965 |
||
966 |
"magnify a single pixel row - can only magnify by integer factors. |
|
967 |
Specially tuned for factors 2,3 and 4." |
|
968 |
||
969 |
%{ |
|
970 |
REGISTER unsigned char *srcP, *dstP; |
|
971 |
REGISTER unsigned char _byte; |
|
972 |
int _mag; |
|
973 |
REGISTER int i; |
|
974 |
int _pixels; |
|
975 |
OBJ w = _INST(width); |
|
976 |
||
977 |
if (__bothSmallInteger(srcStart, dstStart) |
|
978 |
&& __bothSmallInteger(w, mX) |
|
979 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
|
980 |
_mag = _intVal(mX); |
|
981 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
982 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
983 |
_pixels = _intVal(w); |
|
984 |
||
985 |
switch (_mag) { |
|
986 |
case 1: |
|
987 |
break; |
|
988 |
||
989 |
case 2: |
|
990 |
/* special code for common case */ |
|
991 |
while (_pixels--) { |
|
992 |
_byte = *srcP++; |
|
993 |
*dstP++ = _byte; |
|
994 |
*dstP++ = _byte; |
|
995 |
} |
|
996 |
break; |
|
997 |
||
998 |
case 3: |
|
999 |
/* special code for common case */ |
|
1000 |
while (_pixels--) { |
|
1001 |
_byte = *srcP++; |
|
1002 |
*dstP++ = _byte; |
|
1003 |
*dstP++ = _byte; |
|
1004 |
*dstP++ = _byte; |
|
1005 |
} |
|
1006 |
break; |
|
1007 |
||
1008 |
case 4: |
|
1009 |
/* special code for common case */ |
|
1010 |
while (_pixels--) { |
|
1011 |
_byte = *srcP++; |
|
1012 |
*dstP++ = _byte; |
|
1013 |
*dstP++ = _byte; |
|
1014 |
*dstP++ = _byte; |
|
1015 |
*dstP++ = _byte; |
|
1016 |
} |
|
1017 |
break; |
|
1018 |
||
1019 |
default: |
|
1020 |
while (_pixels--) { |
|
1021 |
_byte = *srcP++; |
|
1022 |
for (i=_mag; i>0; i--) |
|
1023 |
*dstP++ = _byte; |
|
1024 |
} |
|
1025 |
break; |
|
1026 |
} |
|
1027 |
RETURN (self); |
|
1028 |
} |
|
1029 |
%} |
|
1030 |
. |
|
1031 |
self primitiveFailed |
|
1 | 1032 |
! ! |
579 | 1033 |
|
1034 |
!Depth8Image methodsFor:'private'! |
|
1035 |
||
1036 |
dither1PlaneUsingMap:map on:aDevice |
|
1037 |
"a helper for dithering palette and greyscale images" |
|
1038 |
||
1039 |
|f |
|
1040 |
patterns formBytes |
|
1041 |
pixel0bytes pixel1bytes |
|
1042 |
clr ditherPattern |
|
1043 |
nColors "{Class: SmallInteger }" |
|
1044 |
w "{Class: SmallInteger }" |
|
1045 |
h "{Class: SmallInteger }"| |
|
1046 |
||
1047 |
nColors := map size. |
|
1048 |
w := width. |
|
1049 |
h := height. |
|
1050 |
||
1051 |
formBytes := ByteArray uninitializedNew:(w + 7 // 8) * h. |
|
1052 |
patterns := Array new:nColors. |
|
1053 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
1054 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1055 |
||
1056 |
" |
|
1057 |
extract dither patterns and values to use for 1/0 bits |
|
1058 |
in those from the dithercolors |
|
1059 |
" |
|
1060 |
1 to:nColors do:[:i | |
|
1061 |
clr := (map at:i) on:aDevice. |
|
1062 |
ditherPattern := clr ditherForm. |
|
1063 |
||
1064 |
ditherPattern isNil ifTrue:[ |
|
1065 |
patterns at:i put:#[2r11111111 |
|
1066 |
2r11111111 |
|
1067 |
2r11111111 |
|
1068 |
2r11111111 |
|
1069 |
2r11111111 |
|
1070 |
2r11111111 |
|
1071 |
2r11111111 |
|
1072 |
2r11111111]. |
|
1073 |
pixel0bytes at:i put:clr colorId. |
|
1074 |
pixel1bytes at:i put:clr colorId |
|
1075 |
] ifFalse:[ |
|
1076 |
patterns at:i put:(ditherPattern bits). |
|
1077 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
1078 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
1079 |
]. |
|
1080 |
]. |
|
1081 |
||
1082 |
%{ |
|
1083 |
unsigned char *_srcP, *_dstP; |
|
1084 |
OBJ _patternBytes; |
|
1085 |
unsigned char _mask = 0x80; |
|
1086 |
unsigned char _outBits = 0; |
|
1087 |
unsigned char _last, _v, _patternBits, _p0, _p1; |
|
1088 |
int _h, _w; |
|
1089 |
int _patternOffset = 0; |
|
1090 |
||
1091 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1092 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element; |
|
1093 |
for (_h = _intVal(h); _h; _h--) { |
|
1094 |
_last = -1; |
|
1095 |
for (_w = _intVal(w); _w; _w--) { |
|
1096 |
_v = *_srcP++; |
|
1097 |
if (_v != _last) { |
|
1098 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v]; |
|
1099 |
if (__isByteArray(_patternBytes)) { |
|
1100 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset]; |
|
1101 |
} else if (__isArray(_patternBytes)) { |
|
1102 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]); |
|
1103 |
} |
|
1104 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v]; |
|
1105 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v]; |
|
1106 |
_last = _v; |
|
1107 |
} |
|
1108 |
_outBits <<= 1; |
|
1109 |
if (_patternBits & _mask) |
|
1110 |
_outBits |= _p1; |
|
1111 |
else |
|
1112 |
_outBits |= _p0; |
|
1113 |
_mask >>= 1; |
|
1114 |
if (_mask == 0) { |
|
1115 |
_mask = 0x80; |
|
1116 |
*_dstP++ = _outBits; |
|
1117 |
_outBits = 0; |
|
1118 |
} |
|
1119 |
} |
|
1120 |
if (_mask != 0x80) { |
|
1121 |
while (_mask != 0) { |
|
1122 |
_outBits <<= 1; |
|
1123 |
_mask >>= 1; |
|
1124 |
} |
|
1125 |
*_dstP++ = _outBits; |
|
1126 |
_mask = 0x80; |
|
1127 |
_outBits = 0; |
|
1128 |
} |
|
1129 |
_patternOffset++; |
|
1130 |
if (_patternOffset == 8) |
|
1131 |
_patternOffset = 0; |
|
1132 |
} |
|
1133 |
%} |
|
1134 |
. |
|
1135 |
f := Form width:w height:h fromArray:formBytes. |
|
1136 |
^ f |
|
1137 |
! |
|
1138 |
||
1139 |
dither2PlaneUsingMap:map on:aDevice |
|
1140 |
"a helper for dithering palette and greyscale images" |
|
1141 |
||
1142 |
|f |
|
1143 |
patterns formBytes |
|
1144 |
pixel0bytes pixel1bytes |
|
1145 |
clr ditherPattern |
|
1146 |
nColors "{Class: SmallInteger }" |
|
1147 |
w "{Class: SmallInteger }" |
|
1148 |
h "{Class: SmallInteger }"| |
|
1149 |
||
1150 |
nColors := map size. |
|
1151 |
w := width. |
|
1152 |
h := height. |
|
1153 |
||
1154 |
formBytes := ByteArray uninitializedNew:(w * 2 + 7 // 8) * h. |
|
1155 |
patterns := Array new:nColors. |
|
1156 |
pixel0bytes := ByteArray uninitializedNew:nColors. |
|
1157 |
pixel1bytes := ByteArray uninitializedNew:nColors. |
|
1158 |
||
1159 |
"extract dither patterns and values to use for 1/0 bits |
|
1160 |
in those from the dithercolors" |
|
1161 |
||
1162 |
1 to:nColors do:[:i | |
|
1163 |
clr := (map at:i) on:aDevice. |
|
1164 |
ditherPattern := clr ditherForm. |
|
1165 |
||
1166 |
ditherPattern isNil ifTrue:[ |
|
1167 |
patterns at:i put:#[2r11111111 |
|
1168 |
2r11111111 |
|
1169 |
2r11111111 |
|
1170 |
2r11111111 |
|
1171 |
2r11111111 |
|
1172 |
2r11111111 |
|
1173 |
2r11111111 |
|
1174 |
2r11111111]. |
|
1175 |
pixel0bytes at:i put:clr colorId. |
|
1176 |
pixel1bytes at:i put:clr colorId |
|
1177 |
] ifFalse:[ |
|
1178 |
patterns at:i put:(ditherPattern bits). |
|
1179 |
pixel0bytes at:i put:(ditherPattern colorMap at:1) colorId. |
|
1180 |
pixel1bytes at:i put:(ditherPattern colorMap at:2) colorId. |
|
1181 |
]. |
|
1182 |
]. |
|
1183 |
||
1184 |
%{ |
|
1185 |
unsigned char *_srcP, *_dstP; |
|
1186 |
OBJ _patternBytes; |
|
1187 |
unsigned char _mask = 0x80; |
|
1188 |
unsigned char _outBits = 0; |
|
1189 |
unsigned char _last, _v, _patternBits, _p0, _p1; |
|
1190 |
int _h, _w; |
|
1191 |
int _patternOffset = 0; |
|
1192 |
int _outCount; |
|
1193 |
||
1194 |
_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1195 |
_dstP = _ByteArrayInstPtr(formBytes)->ba_element; |
|
1196 |
for (_h = _intVal(h); _h; _h--) { |
|
1197 |
_last = -1; |
|
1198 |
_outCount = 0; |
|
1199 |
for (_w = _intVal(w); _w; _w--) { |
|
1200 |
_v = *_srcP++; |
|
1201 |
if (_v != _last) { |
|
1202 |
_patternBytes = _ArrayInstPtr(patterns)->a_element[_v]; |
|
1203 |
if (__isByteArray(_patternBytes)) { |
|
1204 |
_patternBits = _ByteArrayInstPtr(_patternBytes)->ba_element[_patternOffset]; |
|
1205 |
} else if (__isArray(_patternBytes)) { |
|
1206 |
_patternBits = _intVal(_ArrayInstPtr(_patternBytes)->a_element[_patternOffset]); |
|
1207 |
} |
|
1208 |
_p0 = _ByteArrayInstPtr(pixel0bytes)->ba_element[_v]; |
|
1209 |
_p1 = _ByteArrayInstPtr(pixel1bytes)->ba_element[_v]; |
|
1210 |
_last = _v; |
|
1211 |
} |
|
1212 |
_outBits <<= 2; |
|
1213 |
if (_patternBits & _mask) |
|
1214 |
_outBits |= _p1; |
|
1215 |
else |
|
1216 |
_outBits |= _p0; |
|
1217 |
_mask >>= 1; |
|
1218 |
_outCount++; |
|
1219 |
if (_outCount == 4) { |
|
1220 |
*_dstP++ = _outBits; |
|
1221 |
_outCount = 0; |
|
1222 |
if (_mask == 0) { |
|
1223 |
_mask = 0x80; |
|
1224 |
} |
|
1225 |
} |
|
1226 |
} |
|
1227 |
if (_outCount) { |
|
1228 |
do { |
|
1229 |
_outBits <<= 2; |
|
1230 |
} while (++_outCount != 4); |
|
1231 |
*_dstP++ = _outBits; |
|
1232 |
} |
|
1233 |
_mask = 0x80; |
|
1234 |
_outBits = 0; |
|
1235 |
_patternOffset++; |
|
1236 |
if (_patternOffset == 8) |
|
1237 |
_patternOffset = 0; |
|
1238 |
} |
|
1239 |
%} |
|
1240 |
. |
|
1241 |
f := Form width:w height:h depth:2. |
|
1242 |
f initGC. |
|
1243 |
f device drawBits:formBytes depth:2 width:w height:h x:0 y:0 |
|
1244 |
into:f id x:0 y:0 width:w height:h with:f gcId. |
|
1245 |
^ f |
|
1246 |
! ! |
|
1247 |
||
1248 |
!Depth8Image methodsFor:'queries'! |
|
1249 |
||
1250 |
bitsPerPixel |
|
1251 |
"return the number of bits per pixel" |
|
1252 |
||
1253 |
^ 8 |
|
1254 |
! |
|
1255 |
||
1256 |
bitsPerRow |
|
1257 |
"return the number of bits in one scanline of the image" |
|
1258 |
||
1259 |
^ width * 8 |
|
1260 |
! |
|
1261 |
||
1262 |
bytesPerRow |
|
1263 |
"return the number of bytes in one scanline of the image" |
|
1264 |
||
1265 |
^ width |
|
1266 |
! |
|
1267 |
||
1268 |
usedValues |
|
1269 |
"return a collection of color values used in the receiver." |
|
1270 |
||
1271 |
^ bytes usedValues |
|
1272 |
! ! |
|
1273 |
||
1274 |
!Depth8Image class methodsFor:'documentation'! |
|
1275 |
||
1276 |
version |
|
859 | 1277 |
^ '$Header: /cvs/stx/stx/libview/Depth8Image.st,v 1.51 1996-06-14 19:34:11 cg Exp $' |
579 | 1278 |
! ! |