author | Claus Gittinger <cg@exept.de> |
Tue, 24 Jun 1997 22:25:38 +0200 | |
changeset 1782 | a1d67a9456ea |
parent 1656 | ebcad6911ddb |
child 1812 | beeaf75b245b |
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" |
81 | 140 |
! ! |
141 |
||
810 | 142 |
!Depth4Image methodsFor:'dither helpers'! |
798 | 143 |
|
1782 | 144 |
orderedDitheredGrayBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
806 | 145 |
"return the bitmap for a dithered depth-bitmap from the image; |
146 |
with a constant ditherMatrix, this can be used for thresholding. |
|
147 |
Redefined to make use of knowing that pixels are 4-bit values." |
|
148 |
||
149 |
|dH nDither |
|
150 |
greyLevels greyMap1 greyMap2 |
|
151 |
bytesPerRow "{Class: SmallInteger }" |
|
152 |
bytesPerOutRow "{Class: SmallInteger }" |
|
153 |
pixelsPerByte "{Class: SmallInteger }" |
|
154 |
outBits |
|
155 |
w "{Class: SmallInteger }" |
|
156 |
h "{Class: SmallInteger }" | |
|
157 |
||
158 |
nDither := ditherMatrix size. |
|
159 |
dH := nDither / dW. |
|
160 |
||
161 |
w := width. |
|
162 |
h := height. |
|
163 |
||
164 |
greyLevels := 1 bitShift:depth. |
|
165 |
pixelsPerByte := 8 / depth. |
|
166 |
||
167 |
bytesPerRow := self bytesPerRow. |
|
168 |
||
169 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
170 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
171 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
172 |
^ nil |
|
173 |
]. |
|
174 |
||
175 |
greyMap1 := self greyMapForRange:(greyLevels-1). "/ the pixels |
|
176 |
greyMap1 := (greyMap1 collect:[:b | b isNil ifTrue:[ |
|
177 |
0 |
|
178 |
] ifFalse:[ |
|
179 |
b truncated |
|
180 |
] |
|
181 |
]) asByteArray. |
|
182 |
||
183 |
greyMap2 := self greyMapForRange:(greyLevels-1). |
|
184 |
greyMap2 := (greyMap2 collect:[:el | |
|
185 |
el isNil ifTrue:[ |
|
186 |
0 |
|
187 |
] ifFalse:[ |
|
188 |
((el - el truncated) "/ the error (0..1) |
|
189 |
* nDither) rounded |
|
190 |
]]) asByteArray. |
|
191 |
||
192 |
%{ |
|
193 |
int __dW = __intVal(dW); |
|
194 |
int __dH = __intVal(dH); |
|
195 |
int __byte; |
|
196 |
int __dT, __dO; |
|
197 |
int __depth = __intVal(depth); |
|
198 |
int __dstIdx = 0; |
|
199 |
int __srcIdx = 0; |
|
200 |
int __bitCnt; |
|
201 |
int __inByte; |
|
202 |
int __grey, __pixel; |
|
203 |
int __w = __intVal(w); |
|
204 |
int __h = __intVal(h); |
|
205 |
int __x; |
|
206 |
int __y; |
|
207 |
int __oX, __oY, __dY; |
|
208 |
int __nextDst; |
|
209 |
int __nextSrc; |
|
210 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
211 |
int __bytesPerOutRow = __intVal(bytesPerOutRow); |
|
212 |
||
213 |
char *__outBits = __ByteArrayInstPtr(outBits)->ba_element; |
|
808 | 214 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
806 | 215 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
216 |
unsigned char *__greyMap1 = __ByteArrayInstPtr(greyMap1)->ba_element; |
|
217 |
unsigned char *__greyMap2 = __ByteArrayInstPtr(greyMap2)->ba_element; |
|
218 |
||
219 |
__oY = __dY = 0; |
|
220 |
for (__y=0; __y<__h; __y++) { |
|
221 |
__nextDst = __dstIdx + __bytesPerOutRow; |
|
222 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
223 |
||
224 |
__byte = 0; |
|
225 |
__bitCnt = 8; |
|
226 |
||
227 |
__oX = 0; |
|
228 |
||
229 |
for (__x=0; __x<__w; __x++) { |
|
230 |
if (__x & 1) { |
|
231 |
/* odd */ |
|
232 |
__grey = __inByte & 0xF; |
|
233 |
__srcIdx++; |
|
234 |
} else { |
|
235 |
__inByte = __bytes[__srcIdx]; |
|
236 |
__grey = (__inByte >> 4) & 0xF; |
|
237 |
} |
|
238 |
||
239 |
__pixel = __greyMap1[__grey]; /* 0..(greyLevels-1) */ |
|
240 |
__dO = __greyMap2[__grey]; /* 0.. nDither-1) */ |
|
241 |
__dT = __ditherMatrix[__dY + __oX]; |
|
242 |
||
243 |
if (__dO > __dT) /* dither says: next pixel */ |
|
244 |
__pixel++; |
|
245 |
||
246 |
__oX++; |
|
247 |
if (__oX == __dW) __oX = 0; |
|
248 |
||
249 |
__byte = (__byte << __depth) | __pixel; |
|
250 |
||
251 |
__bitCnt = __bitCnt - __depth; |
|
252 |
if (__bitCnt == 0) { |
|
253 |
__outBits[__dstIdx] = __byte; |
|
254 |
__dstIdx++; |
|
255 |
__byte = 0; |
|
256 |
__bitCnt = 8; |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
if (__bitCnt != 8) { |
|
261 |
__byte = __byte << __bitCnt; |
|
262 |
__outBits[__dstIdx] = __byte; |
|
263 |
} |
|
264 |
||
265 |
__oY++; __dY += __dW; |
|
266 |
if (__oY == __dH) { |
|
267 |
__oY = 0; |
|
268 |
__dY = 0; |
|
269 |
} |
|
270 |
||
271 |
__srcIdx = __nextSrc; |
|
272 |
__dstIdx = __nextDst; |
|
273 |
} |
|
274 |
%}. |
|
275 |
^ outBits |
|
276 |
||
277 |
! |
|
278 |
||
798 | 279 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
280 |
"return the dithered monochrome bits for the receiver image; |
|
281 |
with a constant ditherMatrix, this can be used for thresholding. |
|
282 |
Redefined to make use of knowing that pixels are 4-bit values." |
|
283 |
||
284 |
|f dH nDither |
|
285 |
greyMap monoBits |
|
286 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
287 |
bytesPerRow "{Class: SmallInteger }" |
|
288 |
w "{Class: SmallInteger }" |
|
289 |
h "{Class: SmallInteger }"| |
|
290 |
||
291 |
nDither := ditherMatrix size. |
|
292 |
dH := nDither / dW. |
|
293 |
||
294 |
w := width. |
|
295 |
h := height. |
|
296 |
||
297 |
bytesPerRow := self bytesPerRow. |
|
298 |
||
299 |
bytesPerMonoRow := w + 7 // 8. |
|
300 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
301 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
302 |
^ nil |
|
303 |
]. |
|
304 |
||
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
305 |
greyMap := self greyByteMapForRange:nDither. |
798 | 306 |
|
307 |
%{ |
|
308 |
int __dW = __intVal(dW); |
|
309 |
int __dH = __intVal(dH); |
|
310 |
int __byte; |
|
311 |
int __dT; |
|
312 |
int __dstIdx = 0; |
|
313 |
int __srcIdx = 0; |
|
314 |
int __bitCnt; |
|
315 |
int __inByte; |
|
316 |
int __grey; |
|
317 |
int __w = __intVal(w); |
|
318 |
int __h = __intVal(h); |
|
319 |
int __x; |
|
320 |
int __y; |
|
321 |
int __oX, __oY, __dY; |
|
322 |
int __nextDst; |
|
323 |
int __nextSrc; |
|
324 |
int __bytesPerRow = __intVal(bytesPerRow); |
|
325 |
int __bytesPerMonoRow = __intVal(bytesPerMonoRow); |
|
326 |
||
327 |
char *__monoBits = __ByteArrayInstPtr(monoBits)->ba_element; |
|
808 | 328 |
unsigned char *__ditherMatrix = __ByteArrayInstPtr(ditherMatrix)->ba_element; |
798 | 329 |
unsigned char *__bytes = __ByteArrayInstPtr(__INST(bytes))->ba_element; |
330 |
unsigned char *__greyMap = __ByteArrayInstPtr(greyMap)->ba_element; |
|
331 |
||
332 |
__oY = __dY = 0; |
|
333 |
for (__y=0; __y<__h; __y++) { |
|
334 |
__nextDst = __dstIdx + __bytesPerMonoRow; |
|
335 |
__nextSrc = __srcIdx + __bytesPerRow; |
|
336 |
||
337 |
__byte = 0; |
|
338 |
__bitCnt = 8; |
|
339 |
||
340 |
__oX = 0; |
|
341 |
||
342 |
for (__x=0; __x<__w; __x++) { |
|
343 |
if (__x & 1) { |
|
344 |
/* odd */ |
|
345 |
__grey = __inByte & 0xF; |
|
346 |
__srcIdx++; |
|
347 |
} else { |
|
348 |
__inByte = __bytes[__srcIdx]; /* 0..255 */ |
|
349 |
__grey = (__inByte >> 4) & 0xF; |
|
350 |
} |
|
351 |
||
352 |
__grey = __greyMap[__grey]; |
|
353 |
__dT = __ditherMatrix[__dY + __oX]; |
|
354 |
||
355 |
__oX++; |
|
356 |
if (__oX == __dW) __oX = 0; |
|
357 |
||
358 |
__byte = __byte << 1; |
|
359 |
if (__grey > __dT) { |
|
360 |
__byte = __byte | 1; /* white */ |
|
361 |
} |
|
362 |
||
363 |
__bitCnt--; |
|
364 |
if (__bitCnt == 0) { |
|
365 |
__monoBits[__dstIdx] = __byte; |
|
366 |
__dstIdx++; |
|
367 |
__byte = 0; |
|
368 |
__bitCnt = 8; |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
if (__bitCnt != 8) { |
|
373 |
__byte = __byte << __bitCnt; |
|
374 |
__monoBits[__dstIdx] = __byte; |
|
375 |
} |
|
376 |
||
377 |
__oY++; __dY += __dW; |
|
378 |
if (__oY == __dH) { |
|
379 |
__oY = 0; |
|
380 |
__dY = 0; |
|
381 |
} |
|
382 |
||
383 |
__srcIdx = __nextSrc; |
|
384 |
__dstIdx = __nextDst; |
|
385 |
} |
|
386 |
%}. |
|
387 |
||
388 |
^ monoBits |
|
389 |
! ! |
|
390 |
||
89 | 391 |
!Depth4Image methodsFor:'enumerating'! |
81 | 392 |
|
798 | 393 |
colorsAtY:y from:xLow to:xHigh do:aBlock |
1 | 394 |
"perform aBlock for each pixel from x1 to x2 in row y. |
395 |
The block is passed the color at each pixel. |
|
396 |
This method allows slighly faster processing of an |
|
397 |
image than using atX:y:, since some processing can be |
|
398 |
avoided when going from pixel to pixel. However, for |
|
399 |
real image processing, specialized methods should be written." |
|
400 |
||
401 |
|srcIndex "{ Class: SmallInteger }" |
|
402 |
byte "{ Class: SmallInteger }" |
|
403 |
shift "{ Class: SmallInteger }" |
|
404 |
value "{ Class: SmallInteger }" |
|
405 |
x1 "{ Class: SmallInteger }" |
|
406 |
x2 "{ Class: SmallInteger }" |
|
810 | 407 |
colorArray p | |
1 | 408 |
|
798 | 409 |
(p := photometric) == #palette ifTrue:[ |
810 | 410 |
colorArray := colorMap. |
798 | 411 |
] ifFalse:[ |
810 | 412 |
colorArray := Array new:16. |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
413 |
(p == #whiteIs0 or:[p == #blackIs0]) ifTrue:[ |
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
414 |
0 to:15 do:[:i | |
810 | 415 |
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
|
416 |
]. |
798 | 417 |
p == #blackIs0 ifTrue:[ |
810 | 418 |
colorArray reverse |
746 | 419 |
] |
803
899f7eb32ef2
support #rgb - specialized code relaxed
Claus Gittinger <cg@exept.de>
parents:
798
diff
changeset
|
420 |
] ifFalse:[ |
810 | 421 |
0 to:15 do:[:i | |
422 |
colorArray at:(i+1) put:(self colorFromValue:i). |
|
423 |
]. |
|
746 | 424 |
] |
1 | 425 |
]. |
426 |
||
427 |
x1 := xLow. |
|
428 |
x2 := xHigh. |
|
810 | 429 |
|
1 | 430 |
srcIndex := (self bytesPerRow * y) + 1. |
431 |
srcIndex := srcIndex + (x1 // 2). |
|
12 | 432 |
x1 even ifTrue:[ |
746 | 433 |
shift := -4 |
12 | 434 |
] ifFalse:[ |
746 | 435 |
shift := 0 |
12 | 436 |
]. |
1 | 437 |
|
438 |
x1 to:x2 do:[:x | |
|
746 | 439 |
shift == 0 ifTrue:[ |
440 |
byte := bytes at:srcIndex. |
|
441 |
value := byte bitAnd:16rF. |
|
442 |
shift := -4. |
|
443 |
srcIndex := srcIndex + 1. |
|
444 |
] ifFalse:[ |
|
445 |
byte := bytes at:srcIndex. |
|
446 |
value := (byte bitShift:-4) bitAnd:16rF. |
|
447 |
shift := 0 |
|
448 |
]. |
|
810 | 449 |
aBlock value:x value:(colorArray at:(value + 1)). |
1 | 450 |
] |
746 | 451 |
|
798 | 452 |
"Created: 7.6.1996 / 19:12:33 / cg" |
810 | 453 |
"Modified: 10.6.1996 / 10:29:39 / cg" |
280 | 454 |
! |
455 |
||
798 | 456 |
valuesAtY:y from:xLow to:xHigh do:aBlock |
280 | 457 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
458 |
The block is passed the pixelValue at each pixel. |
|
459 |
This method allows slighly faster processing of an |
|
460 |
image than using valueAtX:y:, since some processing can be |
|
461 |
avoided when going from pixel to pixel. However, for |
|
462 |
real image processing, specialized methods should be written." |
|
463 |
||
464 |
|srcIndex "{ Class: SmallInteger }" |
|
465 |
byte "{ Class: SmallInteger }" |
|
466 |
shift "{ Class: SmallInteger }" |
|
467 |
pixelValue "{ Class: SmallInteger }" |
|
468 |
x1 "{ Class: SmallInteger }" |
|
469 |
x2 "{ Class: SmallInteger }" |
|
470 |
| |
|
471 |
||
472 |
x1 := xLow. |
|
473 |
x2 := xHigh. |
|
474 |
srcIndex := (self bytesPerRow * y) + 1. |
|
475 |
srcIndex := srcIndex + (x1 // 2). |
|
476 |
x1 even ifTrue:[ |
|
798 | 477 |
shift := -4 |
280 | 478 |
] ifFalse:[ |
798 | 479 |
shift := 0 |
280 | 480 |
]. |
481 |
||
482 |
x1 to:x2 do:[:x | |
|
798 | 483 |
shift == 0 ifTrue:[ |
484 |
byte := bytes at:srcIndex. |
|
485 |
pixelValue := byte bitAnd:16rF. |
|
486 |
shift := -4. |
|
487 |
srcIndex := srcIndex + 1. |
|
488 |
] ifFalse:[ |
|
489 |
byte := bytes at:srcIndex. |
|
490 |
pixelValue := (byte bitShift:-4) bitAnd:16rF. |
|
491 |
shift := 0 |
|
492 |
]. |
|
493 |
aBlock value:x value:pixelValue. |
|
280 | 494 |
] |
798 | 495 |
|
496 |
"Created: 7.6.1996 / 19:09:45 / cg" |
|
1 | 497 |
! ! |
498 |
||
499 |
!Depth4Image methodsFor:'magnification'! |
|
500 |
||
501 |
magnifyRowFrom:srcBytes offset:srcStart |
|
81 | 502 |
into:dstBytes offset:dstStart factor:mX |
1 | 503 |
|
89 | 504 |
"magnify a single pixel row - can only magnify by integer factors. |
505 |
Specially tuned for factor 2." |
|
1 | 506 |
|
507 |
%{ |
|
508 |
unsigned char *srcP, *dstP; |
|
509 |
int _mag; |
|
510 |
REGISTER int i; |
|
511 |
REGISTER unsigned char _byte; |
|
512 |
int _pixels; |
|
513 |
REGISTER int outcnt, bits, bit; |
|
97 | 514 |
OBJ w = _INST(width); |
1 | 515 |
|
97 | 516 |
if (__bothSmallInteger(srcStart, dstStart) |
517 |
&& __bothSmallInteger(w, mX) |
|
35 | 518 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) { |
81 | 519 |
_mag = _intVal(mX); |
520 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart); |
|
521 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart); |
|
97 | 522 |
_pixels = _intVal(w); |
1 | 523 |
|
81 | 524 |
switch (_mag) { |
525 |
case 1: |
|
526 |
break; |
|
1 | 527 |
|
81 | 528 |
case 2: |
529 |
_byte = *srcP++; |
|
530 |
while (_pixels) { |
|
531 |
bit = (_byte >> 4) & 0xF; |
|
532 |
bits = (bit << 4) | bit; |
|
533 |
*dstP++ = bits; |
|
1 | 534 |
|
81 | 535 |
if (--_pixels) { |
536 |
bit = _byte & 0xF; |
|
537 |
bits = (bit << 4) | bit; |
|
538 |
*dstP++ = bits; |
|
539 |
_byte = *srcP++; |
|
540 |
_pixels--; |
|
541 |
} |
|
542 |
} |
|
543 |
break; |
|
1 | 544 |
|
81 | 545 |
default: |
546 |
bits = 0, outcnt = 0; |
|
547 |
_byte = *srcP++; |
|
548 |
while (_pixels) { |
|
549 |
bit = (_byte >> 4) & 0xF; |
|
1 | 550 |
|
81 | 551 |
for (i=_mag; i>0; i--) { |
552 |
bits = (bits << 4) | bit; |
|
553 |
outcnt++; |
|
554 |
if (outcnt == 2) { |
|
555 |
*dstP++ = bits; |
|
556 |
bits = 0; |
|
557 |
outcnt = 0; |
|
558 |
} |
|
559 |
} |
|
1 | 560 |
|
81 | 561 |
if (--_pixels) { |
562 |
bit = _byte & 0xF; |
|
563 |
for (i=_mag; i>0; i--) { |
|
564 |
bits = (bits << 4) | bit; |
|
565 |
outcnt++; |
|
566 |
if (outcnt == 2) { |
|
567 |
*dstP++ = bits; |
|
568 |
bits = 0; |
|
569 |
outcnt = 0; |
|
570 |
} |
|
571 |
} |
|
572 |
_byte = *srcP++; |
|
573 |
_pixels--; |
|
574 |
} |
|
575 |
} |
|
1 | 576 |
|
81 | 577 |
break; |
578 |
} |
|
579 |
RETURN (self); |
|
1 | 580 |
} |
581 |
%} |
|
582 |
. |
|
583 |
self primitiveFailed |
|
584 |
! ! |
|
280 | 585 |
|
586 |
!Depth4Image methodsFor:'queries'! |
|
587 |
||
588 |
bitsPerPixel |
|
589 |
"return the number of bits per pixel" |
|
590 |
||
591 |
^ 4 |
|
592 |
! |
|
593 |
||
594 |
bitsPerRow |
|
595 |
"return the number of bits in one scanline of the image" |
|
596 |
||
597 |
^ width * 4 |
|
598 |
! |
|
599 |
||
600 |
bytesPerRow |
|
601 |
"return the number of bytes in one scanline of the image" |
|
602 |
||
603 |
|nbytes| |
|
604 |
||
605 |
nbytes := width // 2. |
|
606 |
width odd ifTrue:[ |
|
607 |
^ nbytes + 1 |
|
608 |
]. |
|
609 |
^ nbytes |
|
610 |
! |
|
611 |
||
612 |
usedValues |
|
613 |
"return a collection of color values used in the receiver." |
|
614 |
||
615 |
|useFlags usedValues| |
|
616 |
||
617 |
useFlags := Array new:16 withAll:false. |
|
618 |
width even ifFalse:[ |
|
798 | 619 |
0 to:self height - 1 do:[:y | |
620 |
self valuesAtY:y from:0 to:self width - 1 do:[:x :pixel | |
|
621 |
useFlags at:(pixel + 1) put:true |
|
622 |
] |
|
623 |
]. |
|
280 | 624 |
] ifTrue:[ |
798 | 625 |
bytes usedValues do:[:byte | |
626 |
useFlags at:(byte bitShift:-4)+1 put:true. |
|
627 |
useFlags at:(byte bitAnd:2r1111)+1 put:true. |
|
628 |
]. |
|
280 | 629 |
]. |
630 |
usedValues := OrderedCollection new. |
|
631 |
1 to:16 do:[:i | (useFlags at:i) ifTrue:[usedValues add:(i-1)]]. |
|
632 |
^ usedValues |
|
798 | 633 |
|
634 |
"Modified: 7.6.1996 / 19:09:14 / cg" |
|
280 | 635 |
! ! |
636 |
||
637 |
!Depth4Image class methodsFor:'documentation'! |
|
638 |
||
639 |
version |
|
1782 | 640 |
^ '$Header: /cvs/stx/stx/libview/Depth4Image.st,v 1.24 1997-06-24 20:25:02 cg Exp $' |
280 | 641 |
! ! |