author | Claus Gittinger <cg@exept.de> |
Mon, 21 Jul 1997 18:07:37 +0200 | |
changeset 1816 | 88716b7cc777 |
parent 1812 | beeaf75b245b |
child 2042 | 58c0228331bb |
permissions | -rw-r--r-- |
4 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 3 |
All Rights Reserved |
4 | 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:#Depth4Image |
|
579 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Images' |
|
1 | 18 |
! |
19 |
||
46 | 20 |
!Depth4Image class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
81 | 25 |
All Rights Reserved |
4 | 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 |
! |
|
4 | 35 |
|
46 | 36 |
documentation |
37 |
" |
|
38 |
this class represents 16-color (4 bit / pixel) images. |
|
39 |
Most images coming from the windows world are represented as Depth4Images. |
|
40 |
It mainly consists of methods already implemented in Image, |
|
41 |
reimplemented here for more performance. |
|
611 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
45 |
||
46 |
[see also:] |
|
47 |
Depth1Image Depth2Image Depth8Image Depth16Image Depth24Image |
|
48 |
ImageReader |
|
46 | 49 |
" |
50 |
! ! |
|
4 | 51 |
|
89 | 52 |
!Depth4Image class methodsFor:'queries'! |
53 |
||
54 |
imageDepth |
|
579 | 55 |
"return the depth of images represented by instances of |
56 |
this class - here we return 4" |
|
57 |
||
89 | 58 |
^ 4 |
579 | 59 |
|
60 |
"Modified: 20.4.1996 / 23:40:18 / cg" |
|
89 | 61 |
! ! |
62 |
||
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
63 |
!Depth4Image methodsFor:'accessing - pixels'! |
1 | 64 |
|
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
65 |
colorAtX:x y:y |
1 | 66 |
"retrieve a pixel at x/y; return a color. |
67 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
68 |
x = width-1, y=height-1 for lower right pixel" |
|
69 |
||
46 | 70 |
|lineIndex "{ Class: SmallInteger }" |
71 |
byte "{ Class: SmallInteger }" |
|
72 |
value "{ Class: SmallInteger }" | |
|
1 | 73 |
|
74 |
lineIndex := (self bytesPerRow * y) + 1. |
|
75 |
||
76 |
"left pixel in high bits" |
|
77 |
byte := bytes at:(lineIndex + (x // 2)). |
|
78 |
x even ifTrue:[ |
|
746 | 79 |
value := (byte bitShift:-4) bitAnd:16rF. |
1 | 80 |
] ifFalse:[ |
746 | 81 |
value := byte bitAnd:16rF. |
1 | 82 |
]. |
83 |
photometric == #whiteIs0 ifTrue:[ |
|
746 | 84 |
^ Color gray:100 - (100 / 15 * value) |
1 | 85 |
]. |
86 |
photometric == #blackIs0 ifTrue:[ |
|
746 | 87 |
^ Color gray:(100 / 15 * value) |
1 | 88 |
]. |
89 |
photometric ~~ #palette ifTrue:[ |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
90 |
^ self colorFromValue:value |
1 | 91 |
]. |
89 | 92 |
^ colorMap at:(value + 1) |
746 | 93 |
|
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
94 |
"Modified: 8.6.1996 / 10:52:43 / cg" |
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
95 |
"Created: 24.4.1997 / 17:33:51 / cg" |
1 | 96 |
! |
97 |
||
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
98 |
pixelAtX:x y:y |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
99 |
"retrieve a pixel at x/y; return a pixelValue. |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
100 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
101 |
x = width-1, y=height-1 for lower right pixel" |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
102 |
|
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
103 |
|lineIndex "{ Class: SmallInteger }" |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
104 |
byte "{ Class: SmallInteger }" | |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
105 |
|
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
106 |
lineIndex := (self bytesPerRow * y) + 1. |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
107 |
|
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
108 |
"left pixel in high bits" |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
109 |
byte := bytes at:(lineIndex + (x // 2)). |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
110 |
x even ifTrue:[ |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
111 |
^ (byte bitShift:-4) bitAnd:16rF. |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
112 |
]. |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
113 |
^ byte bitAnd:16rF. |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
114 |
|
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
115 |
"Created: 24.4.1997 / 16:06:43 / cg" |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
116 |
! |
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
117 |
|
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
118 |
pixelAtX:x y:y put:aPixelValue |
1 | 119 |
"set the pixel at x/y to aPixelValue. |
120 |
Pixels start at x=0 , y=0 for upper left pixel, end at |
|
121 |
x = width-1, y=height-1 for lower right pixel" |
|
122 |
||
46 | 123 |
|lineIndex "{ Class: SmallInteger }" |
124 |
index "{ Class: SmallInteger }" |
|
125 |
byte "{ Class: SmallInteger }" | |
|
1 | 126 |
|
127 |
lineIndex := (self bytesPerRow * y) + 1. |
|
128 |
||
129 |
"left pixel is in high bits" |
|
130 |
index := lineIndex + (x // 2). |
|
131 |
byte := bytes at:index. |
|
132 |
x even ifTrue:[ |
|
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
133 |
byte := (byte bitAnd:16rF) bitOr:(aPixelValue bitShift:4) |
1 | 134 |
] ifFalse:[ |
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
135 |
byte := (byte bitAnd:16rF0) bitOr:aPixelValue |
1 | 136 |
]. |
137 |
bytes at:index put:byte |
|
280 | 138 |
|
1656
ebcad6911ddb
pixel accessing methods cleaned up
Claus Gittinger <cg@exept.de>
parents:
810
diff
changeset
|
139 |
"Created: 24.4.1997 / 17:06:39 / cg" |
1816 | 140 |
! |
141 |
||
142 |
rowAt:rowIndex into:aPixelBuffer |
|
143 |
"fill aBuffer with pixel values retrieved from a single row. |
|
144 |
Notice: rowIndex is 0-based." |
|
145 |
||
146 |
|lineIndex "{ Class: SmallInteger }" |
|
147 |
byte "{ Class: SmallInteger }" |
|
148 |
w "{ Class: SmallInteger }" |
|
149 |
pixel dstIdx| |
|
150 |
||
151 |
dstIdx := 1. |
|
152 |
w := width - 1. |
|
153 |
lineIndex := (self bytesPerRow * rowIndex). |
|
154 |
0 to:w do:[:x | |
|
155 |
x even ifTrue:[ |
|
156 |
lineIndex := lineIndex + 1. |
|
157 |
byte := bytes at:lineIndex. |
|
158 |
pixel := (byte bitShift:-4) bitAnd:16rF. |
|
159 |
] ifFalse:[ |
|
160 |
pixel := byte bitAnd:16rF. |
|
161 |
]. |
|
162 |
aPixelBuffer at:dstIdx put:pixel. |
|
163 |
dstIdx := dstIdx + 1 |
|
164 |
]. |
|
165 |
||
166 |
"Created: 21.7.1997 / 18:04:00 / cg" |
|
167 |
"Modified: 21.7.1997 / 18:05:47 / cg" |
|
81 | 168 |
! ! |
169 |
||
810 | 170 |
!Depth4Image methodsFor:'dither helpers'! |
798 | 171 |
|
1782 | 172 |
orderedDitheredGrayBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
806 | 173 |
"return the bitmap for a dithered depth-bitmap from the image; |
174 |
with a constant ditherMatrix, this can be used for thresholding. |
|
175 |
Redefined to make use of knowing that pixels are 4-bit values." |
|
176 |
||
177 |
|dH nDither |
|
178 |
greyLevels greyMap1 greyMap2 |
|
179 |
bytesPerRow "{Class: SmallInteger }" |
|
180 |
bytesPerOutRow "{Class: SmallInteger }" |
|
181 |
pixelsPerByte "{Class: SmallInteger }" |
|
182 |
outBits |
|
183 |
w "{Class: SmallInteger }" |
|
184 |
h "{Class: SmallInteger }" | |
|
185 |
||
186 |
nDither := ditherMatrix size. |
|
187 |
dH := nDither / dW. |
|
188 |
||
189 |
w := width. |
|
190 |
h := height. |
|
191 |
||
192 |
greyLevels := 1 bitShift:depth. |
|
193 |
pixelsPerByte := 8 / depth. |
|
194 |
||
195 |
bytesPerRow := self bytesPerRow. |
|
196 |
||
197 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
198 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
199 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
200 |
^ nil |
|
201 |
]. |
|
202 |
||
203 |
greyMap1 := self greyMapForRange:(greyLevels-1). "/ the pixels |
|
204 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
|
205 |
0 |
|
206 |
] ifFalse:[ |
|
207 |
b truncated |
|
208 |
] |
|
209 |
]) asByteArray. |
|
210 |
||
211 |
greyMap2 := self greyMapForRange:(greyLevels-1). |
|
212 |
greyMap2 := (greyMap2 collect:[:el | |
|
213 |
el isNil ifTrue:[ |
|
214 |
0 |
|
215 |
] ifFalse:[ |
|
216 |
((el - el truncated) "/ the error (0..1) |
|
217 |
* nDither) rounded |
|
218 |
]]) asByteArray. |
|
219 |
||
220 |
%{ |
|
221 |
int __dW = __intVal(dW); |
|
222 |
int __dH = __intVal(dH); |
|
223 |
int __byte; |
|
224 |
int __dT, __dO; |
|
225 |
int __depth = __intVal(depth); |
|
226 |
int __dstIdx = 0; |
|
227 |
int __srcIdx = 0; |
|
228 |
int __bitCnt; |
|
229 |
int __inByte; |
|
230 |
int __grey, __pixel; |
|
231 |
int __w = __intVal(w); |
|
232 |
int __h = __intVal(h); |
|
233 |
int __x; |
|
234 |
int __y; |
|
235 |
int __oX, __oY, __dY; |
|
236 |
int __nextDst; |
|
237 |
int __nextSrc; |
|
238 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
239 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
|
240 |
||
1812
beeaf75b245b
avoid type warnings (char * vs. unsigned char *)
Claus Gittinger <cg@exept.de>
parents:
1782
diff
changeset
|
241 |
unsigned char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
808 | 242 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
806 | 243 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
244 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
|
245 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
|
246 |
||
247 |
__oY = __dY = 0; |
|
248 |
for (__y=0; __y<__h; __y++) { |
|
249 |
__nextDst = __dstIdx + __bytesPerOutRow; |
|
250 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
251 |
||
252 |
__byte = 0; |
|
253 |
__bitCnt = 8; |
|
254 |
||
255 |
__oX = 0; |
|
256 |
||
257 |
for (__x=0; __x<__w; __x++) { |
|
258 |
if (__x & 1) { |
|
259 |
/* odd */ |
|
260 |
__grey = __inByte & 0xF; |
|
261 |
__srcIdx++; |
|
262 |
} else { |
|
263 |
__inByte = __bytes[__srcIdx]; |
|
264 |
__grey = (__inByte >> 4) & 0xF; |
|
265 |
} |
|
266 |
||
267 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
|
268 |
__dO = __greyMap2[__grey]; /* 0.. nDither-1) */ |
|
269 |
__dT = __ditherMatrix[__dY + __oX]; |
|
270 |
||
271 |
if (__dO > __dT) /* dither says: next pixel */ |
|
272 |
__pixel++; |
|
273 |
||
274 |
__oX++; |
|
275 |
if (__oX == __dW) __oX = 0; |
|
276 |
||
277 |
__byte = (__byte << __depth) | __pixel; |
|
278 |
||
279 |
__bitCnt = __bitCnt - __depth; |
|
280 |
if (__bitCnt == 0) { |
|
281 |
__outBits[__dstIdx] = __byte; |
|
282 |
__dstIdx++; |
|
283 |
__byte = 0; |
|
284 |
__bitCnt = 8; |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
if (__bitCnt != 8) { |
|
289 |
__byte = __byte << __bitCnt; |
|
290 |
__outBits[__dstIdx] = __byte; |
|
291 |
} |
|
292 |
||
293 |
__oY++; __dY += __dW; |
|
294 |
if (__oY == __dH) { |
|
295 |
__oY = 0; |
|
296 |
__dY = 0; |
|
297 |
} |
|
298 |
||
299 |
__srcIdx = __nextSrc; |
|
300 |
__dstIdx = __nextDst; |
|
301 |
} |
|
302 |
%}. |
|
303 |
^ outBits |
|
304 |
||
305 |
! |
|
306 |
||
798 | 307 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
308 |
"return the dithered monochrome bits for the receiver image; |
|
309 |
with a constant ditherMatrix, this can be used for thresholding. |
|
310 |
Redefined to make use of knowing that pixels are 4-bit values." |
|
311 |
||
312 |
|f dH nDither |
|
313 |
greyMap monoBits |
|
314 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
315 |
bytesPerRow "{Class: SmallInteger }" |
|
316 |
w "{Class: SmallInteger }" |
|
317 |
h "{Class: SmallInteger }"| |
|
318 |
||
319 |
nDither := ditherMatrix size. |
|
320 |
dH := nDither / dW. |
|
321 |
||
322 |
w := width. |
|
323 |
h := height. |
|
324 |
||
325 |
bytesPerRow := self bytesPerRow. |
|
326 |
||
327 |
bytesPerMonoRow := w + 7 // 8. |
|
328 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
329 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
330 |
^ nil |
|
331 |
]. |
|
332 |
||
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
333 |
greyMap := self greyByteMapForRange:nDither. |
798 | 334 |
|
335 |
%{ |
|
336 |
int __dW = __intVal(dW); |
|
337 |
int __dH = __intVal(dH); |
|
338 |
int __byte; |
|
339 |
int __dT; |
|
340 |
int __dstIdx = 0; |
|
341 |
int __srcIdx = 0; |
|
342 |
int __bitCnt; |
|
343 |
int __inByte; |
|
344 |
int __grey; |
|
345 |
int __w = __intVal(w); |
|
346 |
int __h = __intVal(h); |
|
347 |
int __x; |
|
348 |
int __y; |
|
349 |
int __oX, __oY, __dY; |
|
350 |
int __nextDst; |
|
351 |
int __nextSrc; |
|
352 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
353 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
354 |
||
1812
beeaf75b245b
avoid type warnings (char * vs. unsigned char *)
Claus Gittinger <cg@exept.de>
parents:
1782
diff
changeset
|
355 |
unsigned char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
808 | 356 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
798 | 357 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
358 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
359 |
||
360 |
__oY = __dY = 0; |
|
361 |
for (__y=0; __y<__h; __y++) { |
|
362 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
363 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
364 |
||
365 |
__byte = 0; |
|
366 |
__bitCnt = 8; |
|
367 |
||
368 |
__oX = 0; |
|
369 |
||
370 |
for (__x=0; __x<__w; __x++) { |
|
371 |
if (__x & 1) { |
|
372 |
/* odd */ |
|
373 |
__grey = __inByte & 0xF; |
|
374 |
__srcIdx++; |
|
375 |
} else { |
|
376 |
__inByte = __bytes[__srcIdx]; /* 0..255 */ |
|
377 |
__grey = (__inByte >> 4) & 0xF; |
|
378 |
} |
|
379 |
||
380 |
__grey = __greyMap[__grey]; |
|
381 |
__dT = __ditherMatrix[__dY + __oX]; |
|
382 |
||
383 |
__oX++; |
|
384 |
if (__oX == __dW) __oX = 0; |
|
385 |
||
386 |
__byte = __byte << 1; |
|
387 |
if (__grey > __dT) { |
|
388 |
__byte = __byte | 1; /* white */ |
|
389 |
} |
|
390 |
||
391 |
__bitCnt--; |
|
392 |
if (__bitCnt == 0) { |
|
393 |
__monoBits[__dstIdx] = __byte; |
|
394 |
__dstIdx++; |
|
395 |
__byte = 0; |
|
396 |
__bitCnt = 8; |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
if (__bitCnt != 8) { |
|
401 |
__byte = __byte << __bitCnt; |
|
402 |
__monoBits[__dstIdx] = __byte; |
|
403 |
} |
|
404 |
||
405 |
__oY++; __dY += __dW; |
|
406 |
if (__oY == __dH) { |
|
407 |
__oY = 0; |
|
408 |
__dY = 0; |
|
409 |
} |
|
410 |
||
411 |
__srcIdx = __nextSrc; |
|
412 |
__dstIdx = __nextDst; |
|
413 |
} |
|
414 |
%}. |
|
415 |
||
416 |
^ monoBits |
|
417 |
! ! |
|
418 |
||
89 | 419 |
!Depth4Image methodsFor:'enumerating'! |
81 | 420 |
|
798 | 421 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
1 | 422 |
"perform aBlock for each pixel from x1 to x2 in row y. |
423 |
The block is passed the color at each pixel. |
|
424 |
This method allows slighly faster processing of an |
|
425 |
image than using atX:y:, since some processing can be |
|
426 |
avoided when going from pixel to pixel. However, for |
|
427 |
real image processing, specialized methods should be written." |
|
428 |
||
429 |
|srcIndex "{ Class: SmallInteger }" |
|
430 |
byte "{ Class: SmallInteger }" |
|
431 |
shift "{ Class: SmallInteger }" |
|
432 |
value "{ Class: SmallInteger }" |
|
433 |
x1 "{ Class: SmallInteger }" |
|
434 |
x2 "{ Class: SmallInteger }" |
|
810 | 435 |
colorArray p | |
1 | 436 |
|
798 | 437 |
(p := photometric) == #palette ifTrue:[ |
810 | 438 |
colorArray := colorMap. |
798 | 439 |
] ifFalse:[ |
810 | 440 |
colorArray := Array new:16. |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
441 |
(p == #whiteIs0 or:[p == #blackIs0]) ifTrue:[ |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
442 |
0 to:15 do:[:i | |
810 | 443 |
colorArray at:(i+1) put:(Color gray:100 - (100 / 15 * i)) |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
444 |
]. |
798 | 445 |
p == #blackIs0 ifTrue:[ |
810 | 446 |
colorArray reverse |
746 | 447 |
] |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
448 |
] ifFalse:[ |
810 | 449 |
0 to:15 do:[:i | |
450 |
colorArray at:(i+1) put:(self colorFromValue:i). |
|
451 |
]. |
|
746 | 452 |
] |
1 | 453 |
]. |
454 |
||
455 |
x1 := xLow. |
|
456 |
x2 := xHigh. |
|
810 | 457 |
|
1 | 458 |
srcIndex := (self bytesPerRow * y) + 1. |
459 |
srcIndex := srcIndex + (x1 // 2). |
|
12 | 460 |
x1 even ifTrue:[ |
746 | 461 |
shift := -4 |
12 | 462 |
] ifFalse:[ |
746 | 463 |
shift := 0 |
12 | 464 |
]. |
1 | 465 |
|
466 |
x1 to:x2 do:[:x | |
|
746 | 467 |
shift == 0 ifTrue:[ |
468 |
byte := bytes at:srcIndex. |
|
469 |
value := byte bitAnd:16rF. |
|
470 |
shift := -4. |
|
471 |
srcIndex := srcIndex + 1. |
|
472 |
] ifFalse:[ |
|
473 |
byte := bytes at:srcIndex. |
|
474 |
value := (byte bitShift:-4) bitAnd:16rF. |
|
475 |
shift := 0 |
|
476 |
]. |
|
810 | 477 |
aBlock value:x value:(colorArray at:(value + 1)). |
1 | 478 |
] |
746 | 479 |
|
798 | 480 |
"Created: 7.6.1996 / 19:12:33 / cg" |
810 | 481 |
"Modified: 10.6.1996 / 10:29:39 / cg" |
280 | 482 |
! |
483 |
||
798 | 484 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
280 | 485 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
486 |
The block is passed the pixelValue at each pixel. |
|
487 |
This method allows slighly faster processing of an |
|
488 |
image than using valueAtX:y:, since some processing can be |
|
489 |
avoided when going from pixel to pixel. However, for |
|
490 |
real image processing, specialized methods should be written." |
|
491 |
||
492 |
|srcIndex "{ Class: SmallInteger }" |
|
493 |
byte "{ Class: SmallInteger }" |
|
494 |
shift "{ Class: SmallInteger }" |
|
495 |
pixelValue "{ Class: SmallInteger }" |
|
496 |
x1 "{ Class: SmallInteger }" |
|
497 |
x2 "{ Class: SmallInteger }" |
|
498 |
| |
|
499 |
||
500 |
x1 := xLow. |
|
501 |
x2 := xHigh. |
|
502 |
srcIndex := (self bytesPerRow * y) + 1. |
|
503 |
srcIndex := srcIndex + (x1 // 2). |
|
504 |
x1 even ifTrue:[ |
|
798 | 505 |
shift := -4 |
280 | 506 |
] ifFalse:[ |
798 | 507 |
shift := 0 |
280 | 508 |
]. |
509 |
||
510 |
x1 to:x2 do:[:x | |
|
798 | 511 |
shift == 0 ifTrue:[ |
512 |
byte := bytes at:srcIndex. |
|
513 |
pixelValue := byte bitAnd:16rF. |
|
514 |
shift := -4. |
|
515 |
srcIndex := srcIndex + 1. |
|
516 |
] ifFalse:[ |
|
517 |
byte := bytes at:srcIndex. |
|
518 |
pixelValue := (byte bitShift:-4) bitAnd:16rF. |
|
519 |
shift := 0 |
|
520 |
]. |
|
521 |
aBlock value:x value:pixelValue. |
|
280 | 522 |
] |
798 | 523 |
|
524 |
"Created: 7.6.1996 / 19:09:45 / cg" |
|
1 | 525 |
! ! |
526 |
||
527 |
!Depth4Image methodsFor:'magnification'! |
|
528 |
||
529 |
magnifyRowFrom:srcBytes offset:srcStart |
|
81 | 530 |
into:dstBytes offset:dstStart factor:mX |
1 | 531 |
|
89 | 532 |
"magnify a single pixel row - can only magnify by integer factors. |
533 |
Specially tuned for factor 2." |
|
1 | 534 |
|
535 |
%{ |
|
536 |
unsigned char *srcP, *dstP; |
|
537 |
int _mag; |
|
538 |
REGISTER int i; |
|
539 |
REGISTER unsigned char _byte; |
|
540 |
int _pixels; |
|
541 |
REGISTER int outcnt, bits, bit; |
|
97 | 542 |
OBJ w = _INST(width); |
1 | 543 |
|
97 | 544 |
if (__bothSmallInteger(srcStart, dstStart) |
545 |
&& __bothSmallInteger(w, mX) |
|
35 | 546 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
81 | 547 |
_mag = _intVal(mX); |
548 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
549 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
97 | 550 |
_pixels = _intVal(w); |
1 | 551 |
|
81 | 552 |
switch (_mag) { |
553 |
case 1: |
|
554 |
break; |
|
1 | 555 |
|
81 | 556 |
case 2: |
557 |
_byte = *srcP++; |
|
558 |
while (_pixels) { |
|
559 |
bit = (_byte >> 4) & 0xF; |
|
560 |
bits = (bit << 4) | bit; |
|
561 |
*dstP++ = bits; |
|
1 | 562 |
|
81 | 563 |
if (--_pixels) { |
564 |
bit = _byte & 0xF; |
|
565 |
bits = (bit << 4) | bit; |
|
566 |
*dstP++ = bits; |
|
567 |
_byte = *srcP++; |
|
568 |
_pixels--; |
|
569 |
} |
|
570 |
} |
|
571 |
break; |
|
1 | 572 |
|
81 | 573 |
default: |
574 |
bits = 0, outcnt = 0; |
|
575 |
_byte = *srcP++; |
|
576 |
while (_pixels) { |
|
577 |
bit = (_byte >> 4) & 0xF; |
|
1 | 578 |
|
81 | 579 |
for (i=_mag; i>0; i--) { |
580 |
bits = (bits << 4) | bit; |
|
581 |
outcnt++; |
|
582 |
if (outcnt == 2) { |
|
583 |
*dstP++ = bits; |
|
584 |
bits = 0; |
|
585 |
outcnt = 0; |
|
586 |
} |
|
587 |
} |
|
1 | 588 |
|
81 | 589 |
if (--_pixels) { |
590 |
bit = _byte & 0xF; |
|
591 |
for (i=_mag; i>0; i--) { |
|
592 |
bits = (bits << 4) | bit; |
|
593 |
outcnt++; |
|
594 |
if (outcnt == 2) { |
|
595 |
*dstP++ = bits; |
|
596 |
bits = 0; |
|
597 |
outcnt = 0; |
|
598 |
} |
|
599 |
} |
|
600 |
_byte = *srcP++; |
|
601 |
_pixels--; |
|
602 |
} |
|
603 |
} |
|
1 | 604 |
|
81 | 605 |
break; |
606 |
} |
|
607 |
RETURN (self); |
|
1 | 608 |
} |
609 |
%} |
|
610 |
. |
|
611 |
self primitiveFailed |
|
612 |
! ! |
|
280 | 613 |
|
614 |
!Depth4Image methodsFor:'queries'! |
|
615 |
||
616 |
bitsPerPixel |
|
617 |
"return the number of bits per pixel" |
|
618 |
||
619 |
^ 4 |
|
620 |
! |
|
621 |
||
622 |
bitsPerRow |
|
623 |
"return the number of bits in one scanline of the image" |
|
624 |
||
625 |
^ width * 4 |
|
626 |
! |
|
627 |
||
628 |
bytesPerRow |
|
629 |
"return the number of bytes in one scanline of the image" |
|
630 |
||
631 |
|nbytes| |
|
632 |
||
633 |
nbytes := width // 2. |
|
634 |
width odd ifTrue:[ |
|
635 |
^ nbytes + 1 |
|
636 |
]. |
|
637 |
^ nbytes |
|
638 |
! |
|
639 |
||
640 |
usedValues |
|
641 |
"return a collection of color values used in the receiver." |
|
642 |
||
643 |
|useFlags usedValues| |
|
644 |
||
645 |
useFlags := Array new:16 withAll:false. |
|
646 |
width even ifFalse:[ |
|
798 | 647 |
0 to:self height - 1 do:[:y | |
648 |
self valuesAtY:y from:0 to:self width - 1 do:[:x :pixel | |
|
649 |
useFlags at:(pixel + 1) put:true |
|
650 |
] |
|
651 |
]. |
|
280 | 652 |
] ifTrue:[ |
798 | 653 |
bytes usedValues do:[:byte | |
654 |
useFlags at:(byte bitShift:-4)+1 put:true. |
|
655 |
useFlags at:(byte bitAnd:2r1111)+1 put:true. |
|
656 |
]. |
|
280 | 657 |
]. |
658 |
usedValues := OrderedCollection new. |
|
659 |
1 to:16 do:[:i | (useFlags at:i) ifTrue:[usedValues add:(i-1)]]. |
|
660 |
^ usedValues |
|
798 | 661 |
|
662 |
"Modified: 7.6.1996 / 19:09:14 / cg" |
|
280 | 663 |
! ! |
664 |
||
665 |
!Depth4Image class methodsFor:'documentation'! |
|
666 |
||
667 |
version |
|
1816 | 668 |
^ '$Header: /cvs/stx/stx/libview/Depth4Image.st,v 1.26 1997-07-21 16:07:37 cg Exp $' |
280 | 669 |
! ! |