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