author | Claus Gittinger <cg@exept.de> |
Fri, 14 Jun 1996 19:51:16 +0200 | |
changeset 858 | 5eb598185858 |
parent 856 | 9f802d4d0c1a |
child 886 | 04b88b61c640 |
permissions | -rw-r--r-- |
3 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 3 |
All Rights Reserved |
3 | 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:#Depth24Image |
|
579 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images' |
|
1 | 18 |
! |
19 |
||
46 | 20 |
!Depth24Image class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 25 |
All Rights Reserved |
3 | 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 |
! |
|
3 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
38 |
this class represents true-color (24 bit / pixel) images. |
|
39 |
It mainly consists of methods already implemented in Image, |
|
40 |
reimplemented here for more performance. |
|
611 | 41 |
|
798 | 42 |
Only the #rgb format is supported here. |
43 |
||
611 | 44 |
[author:] |
45 |
Claus Gittinger |
|
46 |
||
47 |
[see also:] |
|
48 |
Depth1Image Depth2Image Depth4Image Depth8Image Depth16Image |
|
49 |
ImageReader |
|
46 | 50 |
" |
51 |
! ! |
|
3 | 52 |
|
89 | 53 |
!Depth24Image class methodsFor:'queries'! |
54 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
55 |
defaultPhotometric |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
56 |
"return the default photometric pixel interpretation" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
57 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
58 |
^ #rgb |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
59 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
60 |
"Created: 10.6.1996 / 18:08:25 / cg" |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
61 |
! |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
62 |
|
89 | 63 |
imageDepth |
579 | 64 |
"return the depth of images represented by instances of |
65 |
this class - here we return 24" |
|
66 |
||
89 | 67 |
^ 24 |
579 | 68 |
|
69 |
"Modified: 20.4.1996 / 23:39:50 / cg" |
|
89 | 70 |
! ! |
71 |
||
81 | 72 |
!Depth24Image methodsFor:'accessing'! |
1 | 73 |
|
74 |
atX:x y:y |
|
75 |
"retrieve a pixel at x/y; return a color. |
|
76 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
77 |
x = width-1, y=height-1 for lower right pixel" |
|
78 |
||
38 | 79 |
|index "{ Class: SmallInteger }" |
80 |
rVal gVal bVal| |
|
1 | 81 |
|
82 |
index := 1 + (((width * y) + x) * 3). |
|
83 |
rVal := bytes at:(index). |
|
84 |
gVal := bytes at:(index + 1). |
|
85 |
bVal := bytes at:(index + 2). |
|
86 |
||
87 |
photometric ~~ #rgb ifTrue:[ |
|
805 | 88 |
^ super atX:x y:y |
1 | 89 |
]. |
90 |
^ Color red:rVal * 100 / 255 |
|
805 | 91 |
green:gVal * 100 / 255 |
92 |
blue:bVal * 100 / 255 |
|
93 |
||
94 |
"Modified: 8.6.1996 / 10:45:05 / cg" |
|
1 | 95 |
! |
96 |
||
89 | 97 |
atX:x y:y put:aColor |
98 |
"set the pixel at x/y to aColor. |
|
99 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
100 |
x = width-1, y=height-1 for lower right pixel." |
|
101 |
||
102 |
|index "{ Class: SmallInteger }" |
|
103 |
val "{ Class: SmallInteger }" | |
|
104 |
||
105 |
index := 1 + (((width * y) + x) * 3). |
|
106 |
val := (aColor red / 100 * 255) rounded. |
|
107 |
bytes at:(index) put:val. |
|
108 |
val := (aColor green / 100 * 255) rounded. |
|
109 |
bytes at:(index + 1) put:val. |
|
110 |
val := (aColor blue / 100 * 255) rounded. |
|
111 |
bytes at:(index + 2) put:val. |
|
112 |
! |
|
113 |
||
1 | 114 |
atX:x y:y putValue:aPixelValue |
115 |
"set a pixel at x/y to aPixelValue, which is 24 bits RGB. |
|
116 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
117 |
x = width-1, y=height-1 for lower right pixel" |
|
118 |
||
119 |
|index "{ Class: SmallInteger }" |
|
120 |
val "{ Class: SmallInteger }" | |
|
121 |
||
122 |
index := 1 + (((width * y) + x) * 3). |
|
123 |
val := aPixelValue. |
|
124 |
bytes at:(index + 2) put:(val bitAnd:16rFF). |
|
125 |
val := val bitShift:-8. |
|
126 |
bytes at:(index + 1) put:(val bitAnd:16rFF). |
|
127 |
val := val bitShift:-8. |
|
128 |
bytes at:(index) put:val. |
|
129 |
! |
|
130 |
||
282 | 131 |
valueAtX:x y:y |
132 |
"retrieve a pixel at x/y; return a color. |
|
133 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
134 |
x = width-1, y=height-1 for lower right pixel" |
|
1 | 135 |
|
282 | 136 |
|index "{ Class: SmallInteger }" |
137 |
rVal "{ Class: SmallInteger }" |
|
138 |
gVal "{ Class: SmallInteger }" |
|
139 |
bVal "{ Class: SmallInteger }"| |
|
1 | 140 |
|
282 | 141 |
index := 1 + (((width * y) + x) * 3). |
142 |
rVal := bytes at:(index). |
|
143 |
gVal := bytes at:(index + 1). |
|
144 |
bVal := bytes at:(index + 2). |
|
145 |
^ (((rVal bitShift:8) bitOr:gVal) bitShift:8) bitOr:bVal |
|
1 | 146 |
! ! |
147 |
||
148 |
!Depth24Image methodsFor:'converting rgb images'! |
|
149 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
150 |
asGrayFormOn:aDevice |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
151 |
"return a grey form from the receiver. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
152 |
Redefined to use special code when converting to 8-bit |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
153 |
greyScale displays." |
1 | 154 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
155 |
(aDevice visualType == #StaticGray) ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
156 |
aDevice depth == 8 ifTrue:[ |
816 | 157 |
^ self makeDeviceGrayPixmapOn:aDevice depth:aDevice depth fromArray:(self threshold8BitGrayBits) |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
158 |
]. |
1 | 159 |
]. |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
160 |
^ super asGrayFormOn:aDevice |
1 | 161 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
162 |
"Created: 10.6.1996 / 19:00:45 / cg" |
816 | 163 |
"Modified: 10.6.1996 / 20:10:19 / cg" |
1 | 164 |
! |
165 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
166 |
asThresholdGrayImageDepth:depth |
808 | 167 |
"return an 8-bit grey image from the rgb picture. |
168 |
Pixel values are reduced to a 0..255 grey level." |
|
1 | 169 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
170 |
depth == 8 ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
171 |
photometric == #rgb ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
172 |
^ Depth8Image |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
173 |
width:width |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
174 |
height:height |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
175 |
fromArray:(self threshold8BitGrayBits) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
176 |
] |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
177 |
]. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
178 |
^ super asThresholdGrayImageDepth:depth |
808 | 179 |
|
180 |
" |
|
181 |
|i| |
|
182 |
||
183 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
184 |
(i asThresholdGrayImageDepth:8) inspect. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
185 |
(i asThresholdGrayImageDepth:4) inspect. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
186 |
(i asThresholdGrayImageDepth:2) inspect. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
187 |
(i asThresholdGrayImageDepth:1) inspect. |
808 | 188 |
" |
1 | 189 |
|
808 | 190 |
" |
191 |
|i| |
|
192 |
||
193 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
194 |
((i asThresholdGrayImageDepth:8) asOrderedDitheredGrayImageDepth:2) inspect |
808 | 195 |
" |
196 |
||
197 |
" |
|
198 |
|i| |
|
1 | 199 |
|
808 | 200 |
i := Image fromFile:'bitmaps/granite.tiff'. |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
201 |
((i asThresholdGrayImageDepth:8) asOrderedDitheredGrayImageDepth:4) inspect |
808 | 202 |
" |
203 |
||
204 |
" |
|
205 |
|i| |
|
206 |
||
207 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
208 |
(i asThresholdGrayImageDepth:8) asOrderedDitheredMonochromeImage inspect |
808 | 209 |
" |
210 |
||
211 |
"Created: 8.6.1996 / 13:58:46 / cg" |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
212 |
"Modified: 10.6.1996 / 19:11:18 / cg" |
1 | 213 |
! |
214 |
||
282 | 215 |
rgbImageAsDitheredPseudoFormOn:aDevice |
216 |
"return a dithered pseudocolor form from the rgb-picture. |
|
217 |
This method depends on fixColors being allocated (see Color>>getColors*)" |
|
218 |
||
219 |
^ self rgbImageAsDitheredPseudoFormOn:aDevice |
|
220 |
colors:Color fixColors |
|
221 |
nRed:Color numFixRed |
|
222 |
nGreen:Color numFixGreen |
|
223 |
nBlue:Color numFixBlue |
|
224 |
! |
|
225 |
||
226 |
rgbImageAsDitheredPseudoFormOn:aDevice colors:fixColors nRed:nRed nGreen:nGreen nBlue:nBlue |
|
227 |
"return a dithered pseudocolor form from the rgb-picture" |
|
228 |
||
229 |
|pseudoBits f |
|
230 |
h "{ Class: SmallInteger }" |
|
231 |
w "{ Class: SmallInteger }" |
|
232 |
"/ eR "{Class: SmallInteger }" |
|
233 |
"/ eG "{Class: SmallInteger }" |
|
234 |
"/ eB "{Class: SmallInteger }" |
|
235 |
"/ wantR "{Class: SmallInteger }" |
|
236 |
"/ wantG "{Class: SmallInteger }" |
|
237 |
"/ wantB "{Class: SmallInteger }" |
|
238 |
fixR "{Class: SmallInteger }" |
|
239 |
fixG "{Class: SmallInteger }" |
|
240 |
fixB "{Class: SmallInteger }" |
|
241 |
srcIndex "{ Class: SmallInteger }" |
|
242 |
dstIndex "{ Class: SmallInteger }" |
|
243 |
deviceDepth has8BitImage |
|
244 |
fixIds failed| |
|
245 |
||
246 |
aDevice ~~ Display ifTrue:[^ nil]. |
|
247 |
||
248 |
fixR := nRed. |
|
249 |
fixR == 0 ifTrue:[ ^ nil]. |
|
250 |
fixG := nGreen. |
|
251 |
fixG == 0 ifTrue:[ ^ nil]. |
|
252 |
fixB := nBlue. |
|
253 |
fixB == 0 ifTrue:[ ^ nil]. |
|
254 |
"/ simple check |
|
255 |
(fixR * fixG * fixB) ~~ fixColors size ifTrue:[ |
|
858 | 256 |
self error:'invalid color array passed'. |
257 |
^ nil |
|
282 | 258 |
]. |
259 |
fixIds := (fixColors asArray collect:[:clr | clr colorId]) asByteArray. |
|
260 |
||
261 |
deviceDepth := aDevice depth. |
|
262 |
deviceDepth == 8 ifTrue:[ |
|
858 | 263 |
has8BitImage := true. |
282 | 264 |
] ifFalse:[ |
858 | 265 |
has8BitImage := false. |
266 |
aDevice supportedImageFormats do:[:fmt | |
|
267 |
(fmt at:#bitsPerPixel) == 8 ifTrue:[ |
|
268 |
has8BitImage := true. |
|
269 |
] |
|
270 |
] |
|
282 | 271 |
]. |
272 |
has8BitImage ifFalse:[^ nil]. |
|
273 |
||
274 |
'D24IMAGE: dithering ...' infoPrintNL. |
|
275 |
||
276 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
|
277 |
||
278 |
h := height. |
|
279 |
w := width. |
|
280 |
||
281 |
%{ |
|
282 |
int __x, __y; |
|
283 |
int __eR, __eG, __eB; |
|
284 |
int __wantR, __wantG, __wantB; |
|
285 |
unsigned char *srcP, *dstP; |
|
286 |
unsigned char *redP, *greenP, *blueP; |
|
287 |
int pix; |
|
288 |
unsigned char *idP; |
|
289 |
int __fR, __fG, __fB; |
|
290 |
int iR, iG, iB; |
|
291 |
int idx; |
|
858 | 292 |
int __w = __intVal(w); |
282 | 293 |
|
858 | 294 |
if (__isByteArray(__INST(bytes)) |
282 | 295 |
&& __isByteArray(pseudoBits) |
296 |
&& __isByteArray(fixIds) |
|
297 |
&& __bothSmallInteger(fixR, fixG) |
|
298 |
&& __isSmallInteger(fixB)) { |
|
858 | 299 |
failed = false; |
282 | 300 |
|
858 | 301 |
srcP = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
302 |
dstP = __ByteArrayInstPtr(pseudoBits)->ba_element; |
|
303 |
idP = __ByteArrayInstPtr(fixIds)->ba_element; |
|
304 |
__fR = __intVal(fixR)-1; |
|
305 |
__fG = __intVal(fixG)-1; |
|
306 |
__fB = __intVal(fixB)-1; |
|
282 | 307 |
|
858 | 308 |
for (__y=__intVal(h); __y>0; __y--) { |
309 |
__eR = __eG = __eB = 0; |
|
310 |
for (__x=__w; __x>0; __x--) { |
|
311 |
int __want; |
|
282 | 312 |
|
858 | 313 |
/* |
314 |
* wR, wG and wB is the wanted r/g/b value; |
|
315 |
* compute the index into the dId table .. |
|
316 |
* values: 0..255; scale to 0..fR-1, 0..fG-1, 0..fB-1 |
|
317 |
* |
|
318 |
* bad kludge: knows how to index into FixColor table |
|
319 |
*/ |
|
320 |
__wantR = __want = srcP[0] + __eR; |
|
282 | 321 |
|
858 | 322 |
if (__want > 255) __want = 255; |
323 |
else if (__want < 0) __want = 0; |
|
324 |
||
325 |
iR = __want * __fR / 128; |
|
326 |
iR = (iR / 2) + (iR & 1); |
|
282 | 327 |
|
858 | 328 |
__wantG = __want = srcP[1] + __eG; |
329 |
if (__want > 255) __want = 255; |
|
330 |
else if (__want < 0) __want = 0; |
|
282 | 331 |
|
858 | 332 |
iG = __want * __fG / 128; |
333 |
iG = (iG / 2) + (iG & 1); |
|
282 | 334 |
|
858 | 335 |
__wantB = __want = srcP[2] + __eB; |
336 |
if (__want > 255) __want = 255; |
|
337 |
else if (__want < 0) __want = 0; |
|
282 | 338 |
|
858 | 339 |
iB = __want * __fB / 128; |
340 |
iB = (iB / 2) + (iB & 1); |
|
282 | 341 |
|
858 | 342 |
idx = iR * (__fG+1); |
343 |
idx = (idx + iG) * (__fB+1); |
|
344 |
idx = idx + iB; |
|
282 | 345 |
|
858 | 346 |
/* |
347 |
* store the corresponding dither colorId |
|
348 |
*/ |
|
349 |
*dstP++ = idP[idx]; |
|
282 | 350 |
|
858 | 351 |
srcP += 3; |
352 |
||
353 |
/* |
|
354 |
* the new error: |
|
355 |
*/ |
|
356 |
__eR = __wantR - (iR * 256 / __fR); |
|
357 |
__eG = __wantG - (iG * 256 / __fG); |
|
358 |
__eB = __wantB - (iB * 256 / __fB); |
|
359 |
} |
|
360 |
} |
|
282 | 361 |
} |
362 |
%}. |
|
363 |
failed ifTrue:[ |
|
858 | 364 |
self primitiveFailed. |
365 |
^ nil |
|
282 | 366 |
|
367 |
"/ for non-C programmers: |
|
368 |
"/ the above code is (roughly) equivalent to: |
|
369 |
"/ srcIndex := 1. |
|
370 |
"/ dstIndex := 1. |
|
371 |
"/ 1 to:h do:[:y | |
|
372 |
"/ eR := eG := eB := 0. |
|
373 |
"/ 1 to:w do:[:x | |
|
374 |
"/ |pixel "{ Class: SmallInteger }" |
|
375 |
"/ clr |
|
376 |
"/ idx "{ Class: SmallInteger }" |
|
377 |
"/ iR "{ Class: SmallInteger }" |
|
378 |
"/ iG "{ Class: SmallInteger }" |
|
379 |
"/ iB "{ Class: SmallInteger }" |
|
380 |
"/ wR "{ Class: SmallInteger }" |
|
381 |
"/ wG "{ Class: SmallInteger }" |
|
382 |
"/ wB "{ Class: SmallInteger }" | |
|
383 |
"/ |
|
384 |
"/ wantR := ((bytes at:srcIndex) + eR). srcIndex := srcIndex + 1. |
|
385 |
"/ wantG := ((bytes at:srcIndex) + eG). srcIndex := srcIndex + 1. |
|
386 |
"/ wantB := ((bytes at:srcIndex) + eB). srcIndex := srcIndex + 1. |
|
387 |
"/ wR := wantR. |
|
388 |
"/ wR > 255 ifTrue:[wR := 255] ifFalse:[wR < 0 ifTrue:[wR := 0]]. |
|
389 |
"/ wG := wantG. |
|
390 |
"/ wG > 255 ifTrue:[wG := 255] ifFalse:[wG < 0 ifTrue:[wG := 0]]. |
|
391 |
"/ wB := wantB. |
|
392 |
"/ wB > 255 ifTrue:[wB := 255] ifFalse:[wB < 0 ifTrue:[wB := 0]]. |
|
393 |
"/ |
|
394 |
"/ iR := wR * (fixR-1) // 128. |
|
395 |
"/ iR := (iR // 2) + (iR bitAnd:1). |
|
396 |
"/ iG := wG * (fixG-1) // 128. |
|
397 |
"/ iG := (iG // 2) + (iG bitAnd:1). |
|
398 |
"/ iB := wB * (fixB-1) // 128. |
|
399 |
"/ iB := (iB // 2) + (iB bitAnd:1). |
|
400 |
"/ idx := (iR * fixR + iG) * fixB + iB + 1. |
|
401 |
"/ |
|
402 |
"/ clr := fixColors at:idx. |
|
403 |
"/ |
|
404 |
"/ eR := wantR - (clr red * 2) asInteger. |
|
405 |
"/ eG := wantG - (clr green * 2) asInteger. |
|
406 |
"/ eB := wantB - (clr blue * 2) asInteger. |
|
407 |
"/ |
|
408 |
"/ pixel := clr colorId. |
|
409 |
"/ pseudoBits at:dstIndex put:pixel. |
|
410 |
"/ |
|
411 |
"/ dstIndex := dstIndex + 1 |
|
412 |
"/ ]. |
|
413 |
]. |
|
414 |
||
415 |
f := Form width:width height:height depth:aDevice depth on:aDevice. |
|
416 |
f isNil ifTrue:[^ nil]. |
|
417 |
f colorMap:fixColors. |
|
418 |
f initGC. |
|
419 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:aDevice depth |
|
858 | 420 |
width:width height:height |
421 |
x:0 y:0 |
|
422 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
282 | 423 |
^ f |
424 |
! |
|
425 |
||
1 | 426 |
rgbImageAsPseudoFormOn:aDevice |
427 |
"return a pseudocolor form from the rgb-picture" |
|
428 |
||
429 |
|pseudoBits f |
|
430 |
r "{ Class: SmallInteger }" |
|
431 |
g "{ Class: SmallInteger }" |
|
432 |
b "{ Class: SmallInteger }" |
|
433 |
srcIndex "{ Class: SmallInteger }" |
|
434 |
dstIndex "{ Class: SmallInteger }" |
|
435 |
rMask "{ Class: SmallInteger }" |
|
436 |
gMask "{ Class: SmallInteger }" |
|
437 |
bMask "{ Class: SmallInteger }" |
|
438 |
redArray greenArray blueArray |
|
439 |
dataSize "{ Class: SmallInteger }" |
|
440 |
nColors "{ Class: SmallInteger }" |
|
441 |
fit fitMap colors color |
|
442 |
fast |
|
443 |
colorIndex "{ Class: SmallInteger }" |
|
89 | 444 |
depth nColorCells deep| |
445 |
||
446 |
Color fixColors notNil ifTrue:[ |
|
447 |
f := self rgbImageAsDitheredPseudoFormOn:aDevice. |
|
448 |
f notNil ifTrue:[^ f]. |
|
449 |
]. |
|
1 | 450 |
|
451 |
"find used colors; build color-tree" |
|
452 |
||
453 |
fit := false. |
|
454 |
fitMap := false. |
|
455 |
depth := aDevice depth. |
|
456 |
nColorCells := aDevice ncells. |
|
457 |
||
89 | 458 |
deep := (depth > 8). |
459 |
||
1 | 460 |
rMask := 2r11111111. |
461 |
gMask := 2r11111111. |
|
462 |
bMask := 2r11111111. |
|
463 |
||
151 | 464 |
'D24IMAGE: allocating colors ...' infoPrintNL. |
66 | 465 |
|
1 | 466 |
[fit] whileFalse:[ |
81 | 467 |
[fitMap] whileFalse:[ |
468 |
srcIndex := 1. |
|
469 |
redArray := Array new:256. |
|
1 | 470 |
|
89 | 471 |
" |
472 |
find used colors, build [r][g][b] 3-dimensional array |
|
473 |
containing true for used colors |
|
474 |
" |
|
81 | 475 |
nColors := 0. |
476 |
srcIndex := 1. |
|
477 |
dataSize := bytes size. |
|
478 |
[srcIndex < dataSize] whileTrue:[ |
|
1 | 479 |
%{ |
81 | 480 |
if (__isByteArray(_INST(bytes))) { |
481 |
int sI = _intVal(srcIndex); |
|
482 |
unsigned char *cp = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1 | 483 |
|
315
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
484 |
r = __MKSMALLINT((cp[sI - 1] & _intVal(rMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
485 |
g = __MKSMALLINT((cp[sI] & _intVal(gMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
486 |
b = __MKSMALLINT((cp[sI + 1] & _intVal(bMask)) + 1); |
2abc494f0c45
_-macros replaced by __-macros
Claus Gittinger <cg@exept.de>
parents:
282
diff
changeset
|
487 |
srcIndex = __MKSMALLINT(sI + 3); |
81 | 488 |
fast = true; |
489 |
} else { |
|
490 |
fast = false; |
|
491 |
} |
|
1 | 492 |
%} |
493 |
. |
|
81 | 494 |
fast ifFalse:[ |
495 |
r := bytes at:srcIndex. |
|
496 |
r := (r bitAnd:rMask) + 1. |
|
497 |
srcIndex := srcIndex + 1. |
|
498 |
g := bytes at:srcIndex. |
|
499 |
g := (g bitAnd:gMask) + 1. |
|
500 |
srcIndex := srcIndex + 1. |
|
501 |
b := bytes at:srcIndex. |
|
502 |
b := (b bitAnd:bMask) + 1. |
|
503 |
srcIndex := srcIndex + 1 |
|
504 |
]. |
|
1 | 505 |
|
81 | 506 |
greenArray := redArray at:r. |
507 |
greenArray isNil ifTrue:[ |
|
508 |
greenArray := Array new:256. |
|
509 |
redArray at:r put:greenArray |
|
510 |
]. |
|
511 |
blueArray := greenArray at:g. |
|
512 |
blueArray isNil ifTrue:[ |
|
89 | 513 |
deep ifTrue:[blueArray := Array new:256] |
514 |
ifFalse:[blueArray := ByteArray new:256]. |
|
81 | 515 |
greenArray at:g put:blueArray |
516 |
]. |
|
89 | 517 |
(blueArray at:b) == 0 ifTrue:[ |
518 |
blueArray at:b put:1. |
|
81 | 519 |
nColors := nColors + 1. |
520 |
(nColors > nColorCells) ifTrue:[ |
|
151 | 521 |
'D24IMAGE: more than ' infoPrint. nColorCells infoPrint. ' colors' infoPrintNL. |
81 | 522 |
srcIndex := dataSize + 1 |
523 |
] |
|
524 |
] |
|
525 |
]. |
|
1 | 526 |
|
81 | 527 |
"again with less color bits if it does not fit colormap" |
1 | 528 |
|
81 | 529 |
(nColors <= nColorCells) ifTrue:[ |
530 |
fitMap := true |
|
531 |
] ifFalse:[ |
|
532 |
"must try again - cutting off some bits" |
|
533 |
(bMask == 2r11111111) ifTrue:[ |
|
534 |
bMask := 2r11111110 |
|
535 |
] ifFalse:[ |
|
89 | 536 |
(bMask == 2r11111110) ifTrue:[ |
537 |
bMask := 2r11111100 |
|
538 |
] ifFalse:[ |
|
539 |
rMask := (rMask bitShift:1) bitAnd:2r11111111. |
|
540 |
gMask := (gMask bitShift:1) bitAnd:2r11111111. |
|
541 |
bMask := (bMask bitShift:1) bitAnd:2r11111111 |
|
542 |
] |
|
81 | 543 |
]. |
151 | 544 |
'D24IMAGE: too many colors; retry with less color resolution' infoPrintNL. |
38 | 545 |
" |
1 | 546 |
'masks:' print. rMask print. ' ' print. gMask print. ' ' print. |
547 |
bMask printNewline |
|
38 | 548 |
" |
81 | 549 |
] |
550 |
]. |
|
1 | 551 |
|
151 | 552 |
'D24IMAGE: ' infoPrint. nColors infoPrint. ' colors used' infoPrintNL. |
89 | 553 |
|
81 | 554 |
colors := Array new:nColors. |
555 |
colorIndex := 1. |
|
1 | 556 |
|
89 | 557 |
" |
558 |
now, we have reduced things to the number of colors |
|
559 |
which are theoretically supported by the devices colormap. |
|
560 |
allocate all used colors in walking over true entries in |
|
561 |
the [r][g][b] table - this may still fail |
|
562 |
" |
|
81 | 563 |
fit := true. |
1 | 564 |
|
81 | 565 |
r := 0. |
566 |
redArray do:[:greenArray | |
|
567 |
(fit and:[greenArray notNil]) ifTrue:[ |
|
568 |
g := 0. |
|
569 |
greenArray do:[:blueArray | |
|
570 |
(fit and:[blueArray notNil]) ifTrue:[ |
|
571 |
b := 0. |
|
89 | 572 |
blueArray do:[:present | |
573 |
|id| |
|
574 |
||
575 |
(fit and:[present ~~ 0]) ifTrue:[ |
|
81 | 576 |
color := Color red:(r * 100.0 / 255.0) |
577 |
green:(g * 100.0 / 255.0) |
|
578 |
blue:(b * 100.0 / 255.0). |
|
579 |
color := color on:aDevice. |
|
89 | 580 |
(id := color colorId) isNil ifTrue:[ |
81 | 581 |
fit := false |
582 |
] ifFalse:[ |
|
583 |
colors at:colorIndex put:color. |
|
584 |
colorIndex := colorIndex + 1. |
|
89 | 585 |
blueArray at:(b + 1) put:id |
81 | 586 |
] |
587 |
]. |
|
588 |
b := b + 1 |
|
589 |
] |
|
590 |
]. |
|
591 |
g := g + 1 |
|
592 |
] |
|
593 |
]. |
|
594 |
r := r + 1 |
|
595 |
]. |
|
1 | 596 |
|
81 | 597 |
"again with less color bits if we did not get all colors" |
1 | 598 |
|
81 | 599 |
fit ifFalse:[ |
151 | 600 |
'D24IMAGE: could not allocate color(s)' infoPrintNL. |
1 | 601 |
|
81 | 602 |
"free the allocated colors" |
603 |
colors atAllPut:nil. |
|
604 |
"a kludge - force immediate freeing of colors" |
|
112 | 605 |
ObjectMemory scavenge; finalize. |
1 | 606 |
|
81 | 607 |
"cut off one more color-bit - cut off blue first" |
608 |
(bMask == 2r11111111) ifTrue:[ |
|
609 |
bMask := 2r11111110 |
|
610 |
] ifFalse:[ |
|
611 |
(bMask == 2r11111110) ifTrue:[ |
|
612 |
bMask := 2r11111100 |
|
613 |
] ifFalse:[ |
|
614 |
rMask := (rMask bitShift:1) bitAnd:2r11111111. |
|
615 |
gMask := (gMask bitShift:1) bitAnd:2r11111111. |
|
616 |
bMask := (bMask bitShift:1) bitAnd:2r11111111 |
|
617 |
] |
|
618 |
]. |
|
619 |
fitMap := false. |
|
620 |
redArray := nil |
|
621 |
] |
|
1 | 622 |
]. |
623 |
||
624 |
"create pseudocolor bits and translate" |
|
625 |
||
626 |
pseudoBits := ByteArray uninitializedNew:(width * height). |
|
627 |
||
628 |
srcIndex := 1. |
|
629 |
dstIndex := 1. |
|
630 |
||
89 | 631 |
(rMask == 2r11111111 |
632 |
and:[gMask == 2r11111111 |
|
633 |
and:[bMask == 2r11111111]]) ifTrue:[ |
|
634 |
[srcIndex < dataSize] whileTrue:[ |
|
635 |
r := bytes at:srcIndex. |
|
636 |
srcIndex := srcIndex + 1. |
|
637 |
g := bytes at:srcIndex. |
|
638 |
srcIndex := srcIndex + 1. |
|
639 |
b := bytes at:srcIndex. |
|
640 |
srcIndex := srcIndex + 1. |
|
641 |
greenArray := redArray at:(r + 1). |
|
642 |
blueArray := greenArray at:(g + 1). |
|
643 |
pseudoBits at:dstIndex put:(blueArray at:(b + 1)). |
|
644 |
dstIndex := dstIndex + 1 |
|
645 |
] |
|
646 |
] ifFalse:[ |
|
647 |
[srcIndex < dataSize] whileTrue:[ |
|
648 |
r := bytes at:srcIndex. |
|
649 |
r := r bitAnd:rMask. |
|
650 |
srcIndex := srcIndex + 1. |
|
651 |
g := bytes at:srcIndex. |
|
652 |
g := g bitAnd:gMask. |
|
653 |
srcIndex := srcIndex + 1. |
|
654 |
b := bytes at:srcIndex. |
|
655 |
b := b bitAnd:bMask. |
|
656 |
srcIndex := srcIndex + 1. |
|
657 |
greenArray := redArray at:(r + 1). |
|
658 |
blueArray := greenArray at:(g + 1). |
|
659 |
pseudoBits at:dstIndex put:(blueArray at:(b + 1)). |
|
660 |
dstIndex := dstIndex + 1 |
|
661 |
] |
|
1 | 662 |
]. |
663 |
||
89 | 664 |
f := Form width:width height:height depth:depth on:aDevice. |
1 | 665 |
f isNil ifTrue:[^ nil]. |
666 |
f colorMap:colors. |
|
667 |
f initGC. |
|
89 | 668 |
aDevice drawBits:pseudoBits bitsPerPixel:8 depth:depth |
81 | 669 |
width:width height:height |
670 |
x:0 y:0 |
|
671 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
1 | 672 |
^ f |
194 | 673 |
! |
674 |
||
675 |
rgbImageAsTrueColorFormOn:aDevice |
|
676 |
"return a truecolor form from the rgb-picture." |
|
677 |
||
678 |
|bestFormat usedDeviceDepth usedDeviceBitsPerPixel depth |
|
679 |
myDepth form imageBits destIndex srcIndex |
|
856 | 680 |
rightShiftR rightShiftG rightShiftB shiftRed shiftGreen shiftBlue| |
194 | 681 |
|
682 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
236 | 683 |
usedDeviceDepth := bestFormat at:#depth. |
684 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
|
194 | 685 |
|
686 |
rightShiftR := (8 - aDevice bitsRed). |
|
687 |
rightShiftG := (8 - aDevice bitsGreen). |
|
688 |
rightShiftB := (8 - aDevice bitsBlue). |
|
689 |
||
690 |
shiftRed := aDevice shiftRed. |
|
691 |
shiftGreen := aDevice shiftGreen. |
|
692 |
shiftBlue := aDevice shiftBlue. |
|
693 |
||
694 |
"/ |
|
695 |
"/ for now, only a few formats are supported |
|
696 |
"/ |
|
697 |
myDepth := self bitsPerPixel. |
|
698 |
myDepth == usedDeviceBitsPerPixel ifTrue:[ |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
699 |
"/ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
700 |
"/ first, the trivial case, where the depths match |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
701 |
"/ 24 bit/pixel |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
702 |
"/ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
703 |
imageBits := bytes. |
194 | 704 |
] ifFalse:[ |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
705 |
"/ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
706 |
"/ 16 bit/pixel ... |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
707 |
"/ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
708 |
(usedDeviceBitsPerPixel == 16) ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
709 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
194 | 710 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
711 |
"/ now, walk over the image and compose 16bit values from the r/g/b triples |
194 | 712 |
|
856 | 713 |
%{ |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
714 |
if (__bothSmallInteger(_INST(height),_INST(width)) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
715 |
&& __bothSmallInteger(rightShiftR, shiftRed) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
716 |
&& __bothSmallInteger(rightShiftG, shiftGreen) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
717 |
&& __bothSmallInteger(rightShiftB, shiftBlue) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
718 |
&& __isByteArray(_INST(bytes)) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
719 |
&& __isByteArray(imageBits)) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
720 |
int rShRed = __intVal(rightShiftR), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
721 |
rShGreen = __intVal(rightShiftG), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
722 |
rShBlue = __intVal(rightShiftB), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
723 |
lShRed = __intVal(shiftRed), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
724 |
lShGreen = __intVal(shiftGreen), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
725 |
lShBlue = __intVal(shiftBlue); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
726 |
int x, y, w; |
194 | 727 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
728 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
729 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
194 | 730 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
731 |
w = __intVal(_INST(width)); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
732 |
if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
733 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
734 |
for (x=w; x > 0; x--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
735 |
unsigned v; |
194 | 736 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
737 |
v = srcPtr[0] << lShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
738 |
v |= (srcPtr[1] << lShGreen); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
739 |
v |= (srcPtr[2] << lShBlue); |
194 | 740 |
# ifdef MSBFIRST |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
741 |
((short *)dstPtr)[0] = v; |
194 | 742 |
# else |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
743 |
dstPtr[0] = (v>>8) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
744 |
dstPtr[1] = (v) & 0xFF; |
194 | 745 |
# endif |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
746 |
dstPtr += 2; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
747 |
srcPtr += 3; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
748 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
749 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
750 |
} else { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
751 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
752 |
for (x=w; x > 0; x--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
753 |
unsigned r, g, b, v; |
194 | 754 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
755 |
r = srcPtr[0] >> rShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
756 |
g = srcPtr[1] >> rShGreen; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
757 |
b = srcPtr[2] >> rShBlue; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
758 |
v = r << lShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
759 |
v |= (g << lShGreen); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
760 |
v |= (b << lShBlue); |
194 | 761 |
# ifdef MSBFIRST |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
762 |
((short *)dstPtr)[0] = v; |
194 | 763 |
# else |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
764 |
dstPtr[0] = (v>>8) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
765 |
dstPtr[1] = (v) & 0xFF; |
194 | 766 |
# endif |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
767 |
dstPtr += 2; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
768 |
srcPtr += 3; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
769 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
770 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
771 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
772 |
} |
194 | 773 |
%}. |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
774 |
] ifFalse:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
775 |
"/ |
856 | 776 |
"/ 32 bits/pixel ... |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
777 |
"/ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
778 |
(usedDeviceBitsPerPixel == 32) ifTrue:[ |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
779 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
194 | 780 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
781 |
"/ now, walk over the image and compose 32bit values from the r/g/b triples |
194 | 782 |
|
856 | 783 |
%{ |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
784 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
785 |
&& __bothSmallInteger(rightShiftR, shiftRed) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
786 |
&& __bothSmallInteger(rightShiftG, shiftGreen) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
787 |
&& __bothSmallInteger(rightShiftB, shiftBlue) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
788 |
&& __isByteArray(_INST(bytes)) |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
789 |
&& __isByteArray(imageBits)) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
790 |
int rShRed = __intVal(rightShiftR), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
791 |
rShGreen = __intVal(rightShiftG), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
792 |
rShBlue = __intVal(rightShiftB), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
793 |
lShRed = __intVal(shiftRed), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
794 |
lShGreen = __intVal(shiftGreen), |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
795 |
lShBlue = __intVal(shiftBlue); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
796 |
int x, y, w; |
194 | 797 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
798 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
799 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
194 | 800 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
801 |
w = __intVal(_INST(width)); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
802 |
if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
803 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
804 |
for (x=w; x > 0; x--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
805 |
unsigned v; |
194 | 806 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
807 |
v = srcPtr[0] << lShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
808 |
v |= (srcPtr[1] << lShGreen); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
809 |
v |= (srcPtr[2] << lShBlue); |
194 | 810 |
# ifdef MSBFIRST |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
811 |
((int *)dstPtr)[0] = v; |
194 | 812 |
# else |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
813 |
dstPtr[0] = (v>>24) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
814 |
dstPtr[1] = (v>>16) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
815 |
dstPtr[2] = (v>>8) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
816 |
dstPtr[3] = (v) & 0xFF; |
194 | 817 |
# endif |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
818 |
dstPtr += 4; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
819 |
srcPtr += 3; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
820 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
821 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
822 |
} else { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
823 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
824 |
for (x=w; x > 0; x--) { |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
825 |
unsigned r, g, b, v; |
194 | 826 |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
827 |
r = srcPtr[0] >> rShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
828 |
g = srcPtr[1] >> rShGreen; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
829 |
b = srcPtr[2] >> rShBlue; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
830 |
v = r << lShRed; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
831 |
v |= (g << lShGreen); |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
832 |
v |= (b << lShBlue); |
194 | 833 |
# ifdef MSBFIRST |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
834 |
((int *)dstPtr)[0] = v; |
194 | 835 |
# else |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
836 |
dstPtr[0] = (v>>24) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
837 |
dstPtr[1] = (v>>16) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
838 |
dstPtr[2] = (v>>8) & 0xFF; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
839 |
dstPtr[3] = (v) & 0xFF; |
194 | 840 |
# endif |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
841 |
dstPtr += 4; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
842 |
srcPtr += 3; |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
843 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
844 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
845 |
} |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
846 |
} |
194 | 847 |
%}. |
856 | 848 |
] ifFalse:[ |
849 |
"/ |
|
850 |
"/ 8 bits/pixel ... |
|
851 |
"/ |
|
852 |
(usedDeviceBitsPerPixel == 8) ifTrue:[ |
|
853 |
imageBits := ByteArray uninitializedNew:(width * height). |
|
854 |
||
855 |
"/ now, walk over the image and compose 8bit values from the r/g/b triples |
|
194 | 856 |
|
856 | 857 |
%{ |
858 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
859 |
&& __bothSmallInteger(rightShiftR, shiftRed) |
|
860 |
&& __bothSmallInteger(rightShiftG, shiftGreen) |
|
861 |
&& __bothSmallInteger(rightShiftB, shiftBlue) |
|
862 |
&& __isByteArray(_INST(bytes)) |
|
863 |
&& __isByteArray(imageBits)) { |
|
864 |
int rShRed = __intVal(rightShiftR), |
|
865 |
rShGreen = __intVal(rightShiftG), |
|
866 |
rShBlue = __intVal(rightShiftB), |
|
867 |
lShRed = __intVal(shiftRed), |
|
868 |
lShGreen = __intVal(shiftGreen), |
|
869 |
lShBlue = __intVal(shiftBlue); |
|
870 |
int x, y, w; |
|
194 | 871 |
|
856 | 872 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
873 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
194 | 874 |
|
856 | 875 |
w = __intVal(_INST(width)); |
876 |
if ((rShRed == 0) && (rShGreen == 0) && (rShBlue == 0)) { |
|
877 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
878 |
for (x=w; x > 0; x--) { |
|
879 |
unsigned v; |
|
194 | 880 |
|
856 | 881 |
v = srcPtr[0] << lShRed; |
882 |
v |= (srcPtr[1] << lShGreen); |
|
883 |
v |= (srcPtr[2] << lShBlue); |
|
884 |
dstPtr[0] = v; |
|
194 | 885 |
|
856 | 886 |
dstPtr += 1; |
887 |
srcPtr += 3; |
|
888 |
} |
|
889 |
} |
|
890 |
} else { |
|
891 |
for (y=__intVal(_INST(height)); y > 0; y--) { |
|
892 |
for (x=w; x > 0; x--) { |
|
893 |
unsigned r, g, b, v; |
|
194 | 894 |
|
856 | 895 |
r = srcPtr[0] >> rShRed; |
896 |
g = srcPtr[1] >> rShGreen; |
|
897 |
b = srcPtr[2] >> rShBlue; |
|
898 |
v = r << lShRed; |
|
899 |
v |= (g << lShGreen); |
|
900 |
v |= (b << lShBlue); |
|
901 |
||
902 |
dstPtr[0] = v; |
|
903 |
||
904 |
dstPtr += 1; |
|
905 |
srcPtr += 3; |
|
906 |
} |
|
907 |
} |
|
908 |
} |
|
909 |
} |
|
910 |
%}. |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
911 |
] |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
912 |
]. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
913 |
] |
194 | 914 |
]. |
915 |
||
916 |
imageBits isNil ifTrue:[ |
|
856 | 917 |
'IMAGE: unimplemented trueColor depth in #rgbImageAsTrueColorFormOn: ' errorPrint. |
918 |
usedDeviceBitsPerPixel errorPrintCR. |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
919 |
^ self asMonochromeFormOn:aDevice |
194 | 920 |
]. |
921 |
||
922 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
|
923 |
form isNil ifTrue:[ |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
924 |
'IMAGE: display bitmap creation failed' errorPrintNL. |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
925 |
^ nil |
194 | 926 |
]. |
927 |
form initGC. |
|
928 |
||
929 |
form |
|
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
930 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
931 |
width:width height:height |
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
932 |
x:0 y:0 toX:0 y:0. |
194 | 933 |
|
934 |
^ form |
|
935 |
||
936 |
"Modified: 21.10.1995 / 19:30:11 / cg" |
|
810 | 937 |
! ! |
938 |
||
939 |
!Depth24Image methodsFor:'dither helpers'! |
|
940 |
||
842
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
941 |
floydSteinbergDitheredBitsDepth:depth |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
942 |
"return the bitmap for a dithered depth bitmap from the image. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
943 |
Redefined to make use of knowing that pixels are 24 bit r/g/b" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
944 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
945 |
|dstIndex "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
946 |
nextDst "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
947 |
bytesPerRow "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
948 |
bytesPerOutRow "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
949 |
outBits greyMap1 greyMap2 greyLevels |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
950 |
errorArray |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
951 |
errorArray1 |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
952 |
e t v |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
953 |
w "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
954 |
h "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
955 |
bitCnt "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
956 |
byte "{Class: SmallInteger }" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
957 |
grey dT |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
958 |
eR eRB eB eLB | |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
959 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
960 |
(samplesPerPixel ~~ 3 |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
961 |
or:[bitsPerSample ~= #(8 8 8)]) ifTrue:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
962 |
^ super floydSteinbergDitheredBitsDepth:depth |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
963 |
]. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
964 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
965 |
w := width. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
966 |
h := height. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
967 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
968 |
bytesPerRow := self bytesPerRow. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
969 |
bytesPerOutRow := ((w * depth) + 7) // 8. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
970 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
971 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
972 |
^ nil |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
973 |
]. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
974 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
975 |
greyLevels := (1 bitShift:depth) - 1. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
976 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
977 |
greyMap1 := Array new:256. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
978 |
greyMap2 := Array new:256. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
979 |
1 to:256 do:[:i | |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
980 |
v := (greyLevels / 255 * (i-1)). |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
981 |
greyMap1 at:i put:v. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
982 |
greyMap2 at:i put:v. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
983 |
]. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
984 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
985 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
986 |
0 |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
987 |
] ifFalse:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
988 |
b truncated |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
989 |
] |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
990 |
]) asByteArray. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
991 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
992 |
greyMap2 := (greyMap2 collect:[:el | |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
993 |
el isNil ifTrue:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
994 |
0 |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
995 |
] ifFalse:[ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
996 |
((el - el truncated) "/ the error (0..1) |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
997 |
* 255) rounded |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
998 |
]]) asByteArray. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
999 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1000 |
errorArray := ByteArray new:(w + 2) * 2. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1001 |
errorArray1 := ByteArray new:(w + 2) * 2. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1002 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1003 |
%{ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1004 |
int __byte; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1005 |
int __dT, __err, __e16, __eR, __eB, __eRB, __eLB; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1006 |
int __depth = __intVal(depth); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1007 |
int __dstIdx = 0; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1008 |
int __srcIdx = 0; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1009 |
int __bitCnt; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1010 |
int __grey; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1011 |
int __w = __intVal(w); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1012 |
int __h = __intVal(h); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1013 |
int __x; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1014 |
int __y; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1015 |
int __nextDst; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1016 |
int __nextSrc; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1017 |
int __bytesPerRow = __intVal(bytesPerRow); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1018 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1019 |
int __pixel; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1020 |
int __greyLevels = __intVal(greyLevels); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1021 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1022 |
char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1023 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1024 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1025 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1026 |
short *__errorArray = (short *)(__ByteArrayInstPtr(errorArray)->ba_element); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1027 |
short *__errorArray1 = (short *)(__ByteArrayInstPtr(errorArray1)->ba_element); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1028 |
short *__t; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1029 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1030 |
bzero(errorArray1, (__w+2) * 2); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1031 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1032 |
__bitCnt = 8; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1033 |
for (__y=0; __y<__h; __y++) { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1034 |
__nextDst = __dstIdx + __bytesPerOutRow; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1035 |
__nextSrc = __srcIdx + __bytesPerRow; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1036 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1037 |
__byte = 0; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1038 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1039 |
__t = __errorArray; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1040 |
__errorArray = __errorArray1; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1041 |
__errorArray1 = __t; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1042 |
bzero(errorArray1, (__w+2) * 2); |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1043 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1044 |
for (__x=0; __x<__w; __x++) { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1045 |
__grey = (__bytes[__srcIdx] * 3) /* 0.3*r + 0.6*g + b -> 0..2550 */ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1046 |
+ (__bytes[__srcIdx+1] * 6) |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1047 |
+ __bytes[__srcIdx+2]; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1048 |
__grey = __grey / 10; /* 0 .. 255 */ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1049 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1050 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1051 |
__err = __greyMap2[__grey]; /* 0.. 255 - error) */ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1052 |
__err += __errorArray[__x+1]; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1053 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1054 |
if (__err > 127) { /* dither says: next pixel */ |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1055 |
if ( __pixel != __greyLevels) |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1056 |
__pixel++; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1057 |
__err = __err - 255; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1058 |
} else { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1059 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1060 |
if (__err) { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1061 |
__e16 = __err >> 4; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1062 |
__eR = __e16 * 7; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1063 |
__eRB = __e16 * 1; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1064 |
__eB = __e16 * 5; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1065 |
__eLB = __err - __eR - __eRB - __eB; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1066 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1067 |
__errorArray [__x+1+1] += __eR; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1068 |
__errorArray1[__x+1+1] += __eRB; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1069 |
__errorArray1[__x+1 ] += __eB; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1070 |
__errorArray1[__x+1-1] += __eLB; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1071 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1072 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1073 |
__srcIdx += 3; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1074 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1075 |
__byte = (__byte << __depth) | __pixel; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1076 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1077 |
__bitCnt = __bitCnt - __depth; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1078 |
if (__bitCnt == 0) { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1079 |
__outBits[__dstIdx] = __byte; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1080 |
__dstIdx++; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1081 |
__byte = 0; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1082 |
__bitCnt = 8; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1083 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1084 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1085 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1086 |
if (__bitCnt != 8) { |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1087 |
__byte = __byte << __bitCnt; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1088 |
__outBits[__dstIdx] = __byte; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1089 |
__bitCnt = 8; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1090 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1091 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1092 |
__srcIdx = __nextSrc; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1093 |
__dstIdx = __nextDst; |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1094 |
} |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1095 |
%}. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1096 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1097 |
^ outBits |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1098 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1099 |
"Created: 10.6.1996 / 13:28:22 / cg" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1100 |
"Modified: 10.6.1996 / 15:09:07 / cg" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1101 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1102 |
! |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1103 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1104 |
floydSteinbergDitheredMonochromeBits |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1105 |
"return the bitmap for a dithered monochrome bitmap from the image. |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1106 |
Redefined to make use of knowing that pixels are 24 bit r/g/b" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1107 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1108 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1109 |
^ self floydSteinbergDitheredBitsDepth:1 |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1110 |
|
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1111 |
"Created: 10.6.1996 / 22:55:59 / cg" |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1112 |
! |
dc8aa9127b49
added conversion for rgb -> greyScale using floyd-steinberg dither
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
1113 |
|
810 | 1114 |
orderedDitheredBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
1115 |
"return the bitmap for a dithered depth-bitmap from the image; |
|
1116 |
with a constant ditherMatrix, this can be used for thresholding. |
|
1117 |
Redefined to make use of knowing that pixels are 24-bit values." |
|
1118 |
||
1119 |
|f dH nDither v range |
|
1120 |
greyMap1 greyMap2 greyLevels outBits |
|
1121 |
bytesPerOutRow "{Class: SmallInteger }" |
|
1122 |
bytesPerRow "{Class: SmallInteger }" |
|
1123 |
w "{Class: SmallInteger }" |
|
1124 |
h "{Class: SmallInteger }"| |
|
1125 |
||
1126 |
photometric ~~ #rgb ifTrue:[ |
|
1127 |
self error:'invalid format'. |
|
1128 |
^ nil |
|
1129 |
]. |
|
1130 |
||
1131 |
nDither := ditherMatrix size. |
|
1132 |
dH := nDither / dW. |
|
1133 |
||
1134 |
w := width. |
|
1135 |
h := height. |
|
1136 |
||
1137 |
greyLevels := 1 bitShift:depth. |
|
1138 |
bytesPerRow := self bytesPerRow. |
|
1139 |
||
1140 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
1141 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
1142 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
1143 |
^ nil |
|
1144 |
]. |
|
1145 |
||
1146 |
greyMap1 := Array new:256. |
|
1147 |
greyMap2 := Array new:256. |
|
1148 |
range := greyLevels-1. |
|
1149 |
1 to:256 do:[:i | |
|
1150 |
v := (range / 255 * (i-1)). |
|
1151 |
greyMap1 at:i put:v. |
|
1152 |
greyMap2 at:i put:v. |
|
1153 |
]. |
|
1154 |
||
1155 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
|
1156 |
0 |
|
1157 |
] ifFalse:[ |
|
1158 |
b truncated |
|
1159 |
] |
|
1160 |
]) asByteArray. |
|
1161 |
||
1162 |
greyMap2 := (greyMap2 collect:[:el | |
|
1163 |
el isNil ifTrue:[ |
|
1164 |
0 |
|
1165 |
] ifFalse:[ |
|
1166 |
((el - el truncated) "/ the error (0..1) |
|
1167 |
* nDither) rounded |
|
1168 |
]]) asByteArray. |
|
1169 |
%{ |
|
1170 |
int __dW = __intVal(dW); |
|
1171 |
int __dH = __intVal(dH); |
|
1172 |
int __byte; |
|
1173 |
int __nDither = __intVal(nDither); |
|
1174 |
int __dT, __dO; |
|
1175 |
int __depth = __intVal(depth); |
|
1176 |
int __dstIdx = 0; |
|
1177 |
int __srcIdx = 0; |
|
1178 |
int __bitCnt; |
|
1179 |
int __grey; |
|
1180 |
int __w = __intVal(w); |
|
1181 |
int __h = __intVal(h); |
|
1182 |
int __x; |
|
1183 |
int __y; |
|
1184 |
int __oX, __oY, __dY; |
|
1185 |
int __nextDst; |
|
1186 |
int __nextSrc; |
|
1187 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
1188 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
|
1189 |
int __pixel; |
|
1190 |
||
1191 |
char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
|
1192 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
1193 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
1194 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
|
1195 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
|
1196 |
||
1197 |
__oY = __dY = 0; |
|
1198 |
for (__y=0; __y<__h; __y++) { |
|
1199 |
__nextDst = __dstIdx + __bytesPerOutRow; |
|
1200 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
1201 |
||
1202 |
__byte = 0; |
|
1203 |
__bitCnt = 8; |
|
1204 |
||
1205 |
__oX = 0; |
|
1206 |
||
1207 |
for (__x=0; __x<__w; __x++) { |
|
1208 |
__grey = (__bytes[__srcIdx] * 3) /* 0.3*r + 0.6*g + b -> 0..2550 */ |
|
1209 |
+ (__bytes[__srcIdx+1] * 6) |
|
1210 |
+ __bytes[__srcIdx+2]; |
|
1211 |
__grey = __grey / 10; /* 0 .. 255 */ |
|
1212 |
||
1213 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
|
1214 |
__dO = __greyMap2[__grey]; /* 0.. nDither-1) */ |
|
1215 |
__dT = __ditherMatrix[__dY + __oX]; |
|
1216 |
||
1217 |
if (__dO > __dT) /* dither says: next pixel */ |
|
1218 |
__pixel++; |
|
1219 |
||
1220 |
__srcIdx += 3; |
|
1221 |
||
1222 |
__oX++; |
|
1223 |
if (__oX == __dW) __oX = 0; |
|
1224 |
||
1225 |
__byte = (__byte << __depth) | __pixel; |
|
1226 |
||
1227 |
__bitCnt = __bitCnt - __depth; |
|
1228 |
if (__bitCnt == 0) { |
|
1229 |
__outBits[__dstIdx] = __byte; |
|
1230 |
__dstIdx++; |
|
1231 |
__byte = 0; |
|
1232 |
__bitCnt = 8; |
|
1233 |
} |
|
1234 |
} |
|
1235 |
||
1236 |
if (__bitCnt != 8) { |
|
1237 |
__byte = __byte << __bitCnt; |
|
1238 |
__outBits[__dstIdx] = __byte; |
|
1239 |
} |
|
1240 |
||
1241 |
__oY++; __dY += __dW; |
|
1242 |
if (__oY == __dH) { |
|
1243 |
__oY = 0; |
|
1244 |
__dY = 0; |
|
1245 |
} |
|
1246 |
||
1247 |
__srcIdx = __nextSrc; |
|
1248 |
__dstIdx = __nextDst; |
|
1249 |
} |
|
1250 |
%}. |
|
1251 |
||
1252 |
^ outBits |
|
1253 |
||
1254 |
" |
|
1255 |
|i| |
|
1256 |
||
1257 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1258 |
i asOrderedDitheredMonochromeImage inspect. |
|
1259 |
i asOrderedDitheredDepth2GrayImage inspect. |
|
1260 |
i asOrderedDitheredDepth4GrayImage inspect. |
|
1261 |
i asOrderedDitheredDepth8GrayImage inspect. |
|
1262 |
" |
|
1263 |
||
1264 |
"Created: 7.6.1996 / 10:48:06 / cg" |
|
1265 |
"Modified: 7.6.1996 / 11:08:50 / cg" |
|
808 | 1266 |
! |
1267 |
||
810 | 1268 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
1269 |
"return the dithered monochrome bits for the receiver image; |
|
1270 |
with a constant ditherMatrix, this can be used for thresholding. |
|
1271 |
Redefined to make use of knowing that pixels are 24-bit values." |
|
1272 |
||
1273 |
|f dH nDither |
|
1274 |
greyMap monoBits |
|
1275 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
1276 |
bytesPerRow "{Class: SmallInteger }" |
|
1277 |
w "{Class: SmallInteger }" |
|
1278 |
h "{Class: SmallInteger }"| |
|
1279 |
||
1280 |
photometric ~~ #rgb ifTrue:[ |
|
1281 |
self error:'invalid format'. |
|
1282 |
^ nil |
|
1283 |
]. |
|
1284 |
||
1285 |
nDither := ditherMatrix size. |
|
1286 |
dH := nDither / dW. |
|
1287 |
||
1288 |
w := width. |
|
1289 |
h := height. |
|
1290 |
||
1291 |
bytesPerRow := self bytesPerRow. |
|
1292 |
||
1293 |
bytesPerMonoRow := w + 7 // 8. |
|
1294 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
1295 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
1296 |
^ nil |
|
1297 |
]. |
|
1298 |
||
1299 |
%{ |
|
1300 |
int __dW = __intVal(dW); |
|
1301 |
int __dH = __intVal(dH); |
|
1302 |
int __byte; |
|
1303 |
int __nDither = __intVal(nDither); |
|
1304 |
int __dT; |
|
1305 |
int __dstIdx = 0; |
|
1306 |
int __srcIdx = 0; |
|
1307 |
int __bitCnt; |
|
1308 |
int __grey; |
|
1309 |
int __w = __intVal(w); |
|
1310 |
int __h = __intVal(h); |
|
1311 |
int __x; |
|
1312 |
int __y; |
|
1313 |
int __oX, __oY, __dY; |
|
1314 |
int __nextDst; |
|
1315 |
int __nextSrc; |
|
1316 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
1317 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
1318 |
||
1319 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
1320 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
|
1321 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
1322 |
||
1323 |
__oY = __dY = 0; |
|
1324 |
for (__y=0; __y<__h; __y++) { |
|
1325 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
1326 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
1327 |
||
1328 |
__byte = 0; |
|
1329 |
__bitCnt = 8; |
|
1330 |
||
1331 |
__oX = 0; |
|
1332 |
||
1333 |
for (__x=0; __x<__w; __x++) { |
|
1334 |
__grey = (__bytes[__srcIdx] * 3) /* 0.3*r + 0.6*g + b -> 0..2550 */ |
|
1335 |
+ (__bytes[__srcIdx+1] * 6) |
|
1336 |
+ __bytes[__srcIdx+2]; |
|
1337 |
__grey = __grey * (__nDither+1) / 2550; /* 0 .. nDither+1 */ |
|
1338 |
__srcIdx += 3; |
|
1339 |
||
1340 |
__dT = __ditherMatrix[__dY + __oX]; |
|
1341 |
||
1342 |
__oX++; |
|
1343 |
if (__oX == __dW) __oX = 0; |
|
1344 |
||
1345 |
__byte = __byte << 1; |
|
1346 |
if (__grey > __dT) { |
|
1347 |
__byte = __byte | 1; /* white */ |
|
1348 |
} |
|
1349 |
||
1350 |
__bitCnt--; |
|
1351 |
if (__bitCnt == 0) { |
|
1352 |
__monoBits[__dstIdx] = __byte; |
|
1353 |
__dstIdx++; |
|
1354 |
__byte = 0; |
|
1355 |
__bitCnt = 8; |
|
1356 |
} |
|
1357 |
} |
|
1358 |
||
1359 |
if (__bitCnt != 8) { |
|
1360 |
__byte = __byte << __bitCnt; |
|
1361 |
__monoBits[__dstIdx] = __byte; |
|
1362 |
} |
|
1363 |
||
1364 |
__oY++; __dY += __dW; |
|
1365 |
if (__oY == __dH) { |
|
1366 |
__oY = 0; |
|
1367 |
__dY = 0; |
|
1368 |
} |
|
1369 |
||
1370 |
__srcIdx = __nextSrc; |
|
1371 |
__dstIdx = __nextDst; |
|
1372 |
} |
|
1373 |
%}. |
|
1374 |
||
1375 |
^ monoBits |
|
1376 |
||
1377 |
" |
|
1378 |
|i f| |
|
1379 |
||
1380 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1381 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1382 |
" |
|
1383 |
||
1384 |
"Created: 7.6.1996 / 10:48:06 / cg" |
|
1385 |
"Modified: 7.6.1996 / 11:08:50 / cg" |
|
1386 |
! |
|
1387 |
||
1388 |
threshold8BitGrayBits |
|
808 | 1389 |
"return the bits for an 8-bit grey image from the receiver. |
1390 |
Special code, since this is a common case." |
|
1391 |
||
1392 |
|greyBits |
|
1393 |
srcIndex "{ Class: SmallInteger }" |
|
1394 |
dstIndex "{ Class: SmallInteger }"| |
|
1395 |
||
1396 |
greyBits := ByteArray uninitializedNew:(width * height). |
|
1397 |
%{ |
|
1398 |
register unsigned char *srcPtr, *dstPtr; |
|
1399 |
register __v; |
|
1400 |
register i; |
|
1401 |
||
1402 |
if ((__Class(__INST(bytes)) == ByteArray) |
|
1403 |
&& (__Class(greyBits) == ByteArray)) { |
|
1404 |
srcPtr = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
|
1405 |
dstPtr = __ByteArrayInstPtr(greyBits)->ba_element; |
|
1406 |
||
1407 |
i = __intVal(__INST(height)) * __intVal(__INST(width)); |
|
1408 |
||
1409 |
for (; i > 0; i--) { |
|
1410 |
__v = (srcPtr[0] * 3); /* 3*r + 6*g + b ; 0 .. 2550 */ |
|
1411 |
__v += (srcPtr[1] * 6); |
|
1412 |
__v += srcPtr[2]; |
|
1413 |
srcPtr += 3; |
|
1414 |
__v /= 10; /* 0..255 */ |
|
1415 |
*dstPtr++ = __v ; |
|
1416 |
} |
|
1417 |
} |
|
1418 |
%}. |
|
1419 |
^ greyBits |
|
1420 |
||
1 | 1421 |
! ! |
1422 |
||
282 | 1423 |
!Depth24Image methodsFor:'enumerating'! |
1 | 1424 |
|
798 | 1425 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
282 | 1426 |
"perform aBlock for each pixel from x1 to x2 in row y. |
1427 |
The block is passed the color at each pixel. |
|
1428 |
This method allows slighly faster processing of an |
|
1429 |
image than using atX:y:, since some processing can be |
|
1430 |
avoided when going from pixel to pixel. However, for |
|
1431 |
real image processing, specialized methods should be written." |
|
1 | 1432 |
|
282 | 1433 |
|srcIndex "{ Class: SmallInteger }" |
1434 |
x1 "{ Class: SmallInteger }" |
|
1435 |
x2 "{ Class: SmallInteger }" |
|
1436 |
rVal "{ Class: SmallInteger }" |
|
1437 |
gVal "{ Class: SmallInteger }" |
|
1438 |
bVal "{ Class: SmallInteger }" |
|
1439 |
lastR lastG lastB lastColor| |
|
1440 |
||
1441 |
photometric ~~ #rgb ifTrue:[ |
|
805 | 1442 |
^ super colorsAtY:y from:xLow to:xHigh do:aBlock. |
282 | 1443 |
]. |
1444 |
||
1445 |
x1 := xLow. |
|
1446 |
x2 := xHigh. |
|
1447 |
||
1448 |
srcIndex := 1 + (((width * y) + x1) * 3). |
|
1 | 1449 |
|
282 | 1450 |
x1 to:x2 do:[:x | |
798 | 1451 |
rVal := bytes at:(srcIndex). |
1452 |
gVal := bytes at:(srcIndex + 1). |
|
1453 |
bVal := bytes at:(srcIndex + 2). |
|
1454 |
srcIndex := srcIndex + 3. |
|
1455 |
||
1456 |
(rVal == lastR and:[gVal == lastG and:[bVal == lastB]]) ifFalse:[ |
|
1457 |
lastColor := Color red:rVal * 100 / 255 |
|
1458 |
green:gVal * 100 / 255 |
|
1459 |
blue:bVal * 100 / 255. |
|
1460 |
lastR := rVal. |
|
1461 |
lastG := gVal. |
|
1462 |
lastB := bVal. |
|
1463 |
]. |
|
1464 |
aBlock value:x value:lastColor |
|
282 | 1465 |
] |
798 | 1466 |
|
1467 |
"Created: 7.6.1996 / 19:12:28 / cg" |
|
805 | 1468 |
"Modified: 8.6.1996 / 10:16:51 / cg" |
282 | 1469 |
! |
1 | 1470 |
|
798 | 1471 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
282 | 1472 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
1473 |
The block is passed the pixelValue at each pixel. |
|
1474 |
This method allows slighly faster processing of an |
|
1475 |
image than using valueAtX:y:, since some processing can be |
|
1476 |
avoided when going from pixel to pixel. However, for |
|
1477 |
real image processing, specialized methods should be written. |
|
1478 |
Notice that the pixelValue is the r/g/b value packed into an integer |
|
1479 |
(r bitShift:16) bitOr:(g bitSHift:8) bitOr:b" |
|
1480 |
||
1481 |
|srcIndex "{ Class: SmallInteger }" |
|
1482 |
x1 "{ Class: SmallInteger }" |
|
1483 |
x2 "{ Class: SmallInteger }" |
|
1484 |
r "{ Class: SmallInteger }" |
|
1485 |
g "{ Class: SmallInteger }" |
|
1486 |
b "{ Class: SmallInteger }"| |
|
1487 |
||
1488 |
x1 := xLow. |
|
1489 |
x2 := xHigh. |
|
1490 |
||
1491 |
srcIndex := 1 + (((width * y) + x1) * 3). |
|
1492 |
||
1493 |
x1 to:x2 do:[:x | |
|
798 | 1494 |
r := bytes at:(srcIndex). |
1495 |
g := bytes at:(srcIndex + 1). |
|
1496 |
b := bytes at:(srcIndex + 2). |
|
1497 |
srcIndex := srcIndex + 3. |
|
1498 |
aBlock value:x value:(((r bitShift:16) bitOr:(g bitShift:8)) bitOr:b) |
|
282 | 1499 |
] |
798 | 1500 |
|
1501 |
"Created: 7.6.1996 / 19:09:40 / cg" |
|
282 | 1502 |
! ! |
1503 |
||
1504 |
!Depth24Image methodsFor:'magnification'! |
|
1 | 1505 |
|
134 | 1506 |
hardMagnifiedBy:extent |
1 | 1507 |
"return a new image magnified by extent, aPoint. |
1508 |
This is the general magnification method, handling non-integral values" |
|
1509 |
||
1510 |
|mX mY |
|
1511 |
newWidth "{ Class: SmallInteger }" |
|
1512 |
newHeight "{ Class: SmallInteger }" |
|
1513 |
w "{ Class: SmallInteger }" |
|
1514 |
h "{ Class: SmallInteger }" |
|
1515 |
newImage newBytes |
|
1516 |
value "{ Class: SmallInteger }" |
|
1517 |
srcRowIdx "{ Class: SmallInteger }" |
|
1518 |
srcIndex "{ Class: SmallInteger }" |
|
1519 |
dstIndex "{ Class: SmallInteger }"| |
|
1520 |
||
1521 |
mX := extent x. |
|
1522 |
mY := extent y. |
|
1523 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
1524 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
1525 |
||
1526 |
newWidth := (width * mX) truncated. |
|
1527 |
newHeight := (height * mY) truncated. |
|
1528 |
newBytes := ByteArray uninitializedNew:(newWidth * 3 * newHeight). |
|
1529 |
||
1530 |
newImage := self species new. |
|
1531 |
newImage bits:newBytes. |
|
1532 |
newImage width:newWidth. |
|
1533 |
newImage height:newHeight. |
|
1534 |
newImage photometric:photometric. |
|
1535 |
newImage samplesPerPixel:samplesPerPixel. |
|
1536 |
newImage bitsPerSample:#(8 8 8). |
|
1537 |
||
1538 |
"walk over destination image fetching pixels from source image" |
|
1539 |
||
1540 |
mY := mY asFloat. |
|
1541 |
mX := mX asFloat. |
|
1542 |
%{ |
|
1543 |
REGISTER unsigned char *_dstP = _ByteArrayInstPtr(newBytes)->ba_element; |
|
1544 |
unsigned char *_srcP = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
1545 |
unsigned char *_srcRowP, *sP; |
|
1546 |
int _width3 = _intVal(_INST(width)) * 3; |
|
1547 |
int _w = _intVal(newWidth) - 1; |
|
1548 |
int _h = _intVal(newHeight) - 1; |
|
1549 |
int _row, _col; |
|
1550 |
double _mX = _floatVal(mX); |
|
1551 |
double _mY = _floatVal(mY); |
|
1552 |
||
1553 |
for (_row = 0; _row <= _h; _row++) { |
|
81 | 1554 |
_srcRowP = _srcP + (_width3 * (int)((double)_row / _mY)); |
1555 |
for (_col = 0; _col <= _w; _col++) { |
|
1556 |
sP = _srcRowP + (((int)((double)_col / _mX)) * 3); |
|
1557 |
_dstP[0] = sP[0]; |
|
1558 |
_dstP[1] = sP[1]; |
|
1559 |
_dstP[2] = sP[2]; |
|
1560 |
_dstP += 3; |
|
1561 |
} |
|
1 | 1562 |
} |
1563 |
%} |
|
1564 |
. |
|
81 | 1565 |
" the above C-code is equivalent to: |
1566 |
||
1 | 1567 |
dstIndex := 1. |
1568 |
w := newWidth - 1. |
|
1569 |
h := newHeight - 1. |
|
1570 |
0 to:h do:[:row | |
|
81 | 1571 |
srcRowIdx := (width * 3 * (row // mY)) + 1. |
1572 |
0 to:w do:[:col | |
|
1573 |
srcIndex := srcRowIdx + ((col // mX) * 3). |
|
1574 |
value := bytes at:srcIndex. |
|
1575 |
newBytes at:dstIndex put:value. |
|
1576 |
value := bytes at:(srcIndex + 1). |
|
1577 |
newBytes at:(dstIndex + 1) put:value. |
|
1578 |
value := bytes at:(srcIndex + 2). |
|
1579 |
newBytes at:(dstIndex + 2) put:value. |
|
1580 |
dstIndex := dstIndex + 3 |
|
1581 |
] |
|
1 | 1582 |
]. |
1583 |
" |
|
1584 |
^ newImage |
|
282 | 1585 |
! |
1586 |
||
1587 |
magnifyRowFrom:srcBytes offset:srcStart |
|
1588 |
into:dstBytes offset:dstStart factor:mX |
|
1589 |
||
1590 |
"magnify a single pixel row - can only magnify by integer factors" |
|
1591 |
||
1592 |
%{ |
|
1593 |
unsigned char *srcP, *dstP; |
|
1594 |
int _mag; |
|
1595 |
REGISTER int i; |
|
1596 |
REGISTER unsigned char byte1, byte2, byte3; |
|
1597 |
int _pixels; |
|
1598 |
OBJ w = _INST(width); |
|
1599 |
||
1600 |
if (__bothSmallInteger(srcStart, dstStart) |
|
1601 |
&& __bothSmallInteger(w, mX) |
|
1602 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
|
1603 |
_mag = _intVal(mX); |
|
1604 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
1605 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
1606 |
_pixels = _intVal(w); |
|
1607 |
||
1608 |
while (_pixels--) { |
|
1609 |
byte1 = *srcP; |
|
1610 |
byte2 = *(srcP+1); |
|
1611 |
byte3 = *(srcP+2); |
|
1612 |
srcP += 3; |
|
1613 |
for (i=_mag; i>0; i--) { |
|
1614 |
*dstP = byte1; |
|
1615 |
*(dstP+1) = byte2; |
|
1616 |
*(dstP+2) = byte3; |
|
1617 |
dstP += 3; |
|
1618 |
} |
|
1619 |
} |
|
1620 |
RETURN (self); |
|
1621 |
} |
|
1622 |
%} |
|
1623 |
. |
|
1624 |
self primitiveFailed |
|
1 | 1625 |
! ! |
282 | 1626 |
|
1627 |
!Depth24Image methodsFor:'queries'! |
|
1628 |
||
1629 |
bitsPerPixel |
|
1630 |
"return the number of bits per pixel" |
|
1631 |
||
1632 |
^ 24 |
|
1633 |
! |
|
1634 |
||
1635 |
bitsPerRow |
|
1636 |
"return the number of bits in one scanline of the image" |
|
1637 |
||
1638 |
^ width * 24 |
|
1639 |
! |
|
1640 |
||
1641 |
bitsPerSample |
|
1642 |
"return the number of bits per sample. |
|
1643 |
The return value is an array of bits-per-plane." |
|
1644 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1645 |
bitsPerSample notNil ifTrue:[^ bitsPerSample]. |
282 | 1646 |
^ #(8 8 8) |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1647 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1648 |
"Modified: 10.6.1996 / 18:02:33 / cg" |
282 | 1649 |
! |
1650 |
||
805 | 1651 |
blueBitsOf:pixel |
1652 |
^ pixel bitAnd:16rFF. |
|
1653 |
||
1654 |
"Created: 8.6.1996 / 09:56:20 / cg" |
|
1655 |
! |
|
1656 |
||
1657 |
blueComponentOf:pixel |
|
1658 |
^ (100.0 / 255.0) * (pixel bitAnd:16rFF) |
|
1659 |
||
1660 |
"Created: 8.6.1996 / 08:42:44 / cg" |
|
1661 |
"Modified: 8.6.1996 / 09:57:41 / cg" |
|
1662 |
! |
|
1663 |
||
282 | 1664 |
bytesPerRow |
1665 |
"return the number of bytes in one scanline of the image" |
|
1666 |
||
1667 |
^ width * 3 |
|
1668 |
! |
|
1669 |
||
805 | 1670 |
greenBitsOf:pixel |
1671 |
^ (pixel bitShift:-8) bitAnd:16rFF. |
|
1672 |
||
1673 |
"Modified: 8.6.1996 / 08:56:28 / cg" |
|
1674 |
"Created: 8.6.1996 / 09:56:30 / cg" |
|
1675 |
! |
|
1676 |
||
1677 |
greenComponentOf:pixel |
|
1678 |
^ (100.0 / 255.0) * (pixel bitShift:-8) bitAnd:16rFF. |
|
1679 |
||
1680 |
"Created: 8.6.1996 / 08:42:37 / cg" |
|
1681 |
"Modified: 8.6.1996 / 09:57:32 / cg" |
|
1682 |
! |
|
1683 |
||
1684 |
redBitsOf:pixel |
|
1685 |
^ (pixel bitShift:-16) bitAnd:16rFF. |
|
1686 |
||
1687 |
"Modified: 8.6.1996 / 08:56:31 / cg" |
|
1688 |
"Created: 8.6.1996 / 09:56:39 / cg" |
|
1689 |
! |
|
1690 |
||
1691 |
redComponentOf:pixel |
|
1692 |
^ (100.0 / 255.0) * (pixel bitShift:-16) bitAnd:16rFF. |
|
1693 |
||
1694 |
"Created: 8.6.1996 / 08:42:25 / cg" |
|
1695 |
"Modified: 8.6.1996 / 09:57:23 / cg" |
|
1696 |
! |
|
1697 |
||
282 | 1698 |
samplesPerPixel |
1699 |
"return the number of samples per pixel in the image." |
|
1700 |
||
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1701 |
samplesPerPixel notNil ifTrue:[^ samplesPerPixel]. |
282 | 1702 |
^ 3 |
813
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1703 |
|
8bc17dba7a02
removed obsolete comversion stuff
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
1704 |
"Modified: 10.6.1996 / 18:03:09 / cg" |
282 | 1705 |
! ! |
1706 |
||
1707 |
!Depth24Image class methodsFor:'documentation'! |
|
1708 |
||
1709 |
version |
|
858 | 1710 |
^ '$Header: /cvs/stx/stx/libview/Depth24Image.st,v 1.35 1996-06-14 17:51:16 cg Exp $' |
282 | 1711 |
! ! |