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
|
81
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
118
|
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.
|
|
42 |
"
|
|
43 |
! !
|
4
|
44 |
|
89
|
45 |
!Depth4Image class methodsFor:'queries'!
|
|
46 |
|
|
47 |
imageDepth
|
|
48 |
^ 4
|
|
49 |
! !
|
|
50 |
|
81
|
51 |
!Depth4Image methodsFor:'accessing'!
|
1
|
52 |
|
|
53 |
atX:x y:y
|
|
54 |
"retrieve a pixel at x/y; return a color.
|
|
55 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
56 |
x = width-1, y=height-1 for lower right pixel"
|
|
57 |
|
46
|
58 |
|lineIndex "{ Class: SmallInteger }"
|
|
59 |
byte "{ Class: SmallInteger }"
|
|
60 |
value "{ Class: SmallInteger }" |
|
1
|
61 |
|
|
62 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
63 |
|
|
64 |
"left pixel in high bits"
|
|
65 |
byte := bytes at:(lineIndex + (x // 2)).
|
|
66 |
x even ifTrue:[
|
81
|
67 |
value := (byte bitShift:-4) bitAnd:16rF.
|
1
|
68 |
] ifFalse:[
|
81
|
69 |
value := byte bitAnd:16rF.
|
1
|
70 |
].
|
|
71 |
photometric == #whiteIs0 ifTrue:[
|
81
|
72 |
^ Color grey:100 - (100 / 15 * value)
|
1
|
73 |
].
|
|
74 |
photometric == #blackIs0 ifTrue:[
|
81
|
75 |
^ Color grey:(100 / 15 * value)
|
1
|
76 |
].
|
|
77 |
photometric ~~ #palette ifTrue:[
|
81
|
78 |
self error:'format not supported'.
|
|
79 |
^ nil
|
1
|
80 |
].
|
89
|
81 |
^ colorMap at:(value + 1)
|
1
|
82 |
!
|
|
83 |
|
|
84 |
atX:x y:y putValue:aPixelValue
|
|
85 |
"set the pixel at x/y to aPixelValue.
|
|
86 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
87 |
x = width-1, y=height-1 for lower right pixel"
|
|
88 |
|
46
|
89 |
|lineIndex "{ Class: SmallInteger }"
|
|
90 |
index "{ Class: SmallInteger }"
|
|
91 |
byte "{ Class: SmallInteger }" |
|
1
|
92 |
|
|
93 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
94 |
|
|
95 |
"left pixel is in high bits"
|
|
96 |
index := lineIndex + (x // 2).
|
|
97 |
byte := bytes at:index.
|
|
98 |
x even ifTrue:[
|
81
|
99 |
byte := (byte bitAnd:16rF) bitOr:(aPixelValue bitShift:4)
|
1
|
100 |
] ifFalse:[
|
81
|
101 |
byte := (byte bitAnd:16rF0) bitOr:aPixelValue
|
1
|
102 |
].
|
|
103 |
bytes at:index put:byte
|
280
|
104 |
!
|
|
105 |
|
|
106 |
valueAtX:x y:y
|
|
107 |
"retrieve a pixel at x/y; return a pixelValue.
|
|
108 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
109 |
x = width-1, y=height-1 for lower right pixel"
|
|
110 |
|
|
111 |
|lineIndex "{ Class: SmallInteger }"
|
|
112 |
byte "{ Class: SmallInteger }" |
|
|
113 |
|
|
114 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
115 |
|
|
116 |
"left pixel in high bits"
|
|
117 |
byte := bytes at:(lineIndex + (x // 2)).
|
|
118 |
x even ifTrue:[
|
|
119 |
^ (byte bitShift:-4) bitAnd:16rF.
|
|
120 |
].
|
|
121 |
^ byte bitAnd:16rF.
|
81
|
122 |
! !
|
|
123 |
|
89
|
124 |
!Depth4Image methodsFor:'enumerating'!
|
81
|
125 |
|
1
|
126 |
atY:y from:xLow to:xHigh do:aBlock
|
|
127 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
128 |
The block is passed the color at each pixel.
|
|
129 |
This method allows slighly faster processing of an
|
|
130 |
image than using atX:y:, since some processing can be
|
|
131 |
avoided when going from pixel to pixel. However, for
|
|
132 |
real image processing, specialized methods should be written."
|
|
133 |
|
|
134 |
|srcIndex "{ Class: SmallInteger }"
|
|
135 |
byte "{ Class: SmallInteger }"
|
|
136 |
shift "{ Class: SmallInteger }"
|
|
137 |
value "{ Class: SmallInteger }"
|
|
138 |
x1 "{ Class: SmallInteger }"
|
|
139 |
x2 "{ Class: SmallInteger }"
|
89
|
140 |
colors p |
|
1
|
141 |
|
89
|
142 |
(p := photometric) == #whiteIs0 ifTrue:[
|
|
143 |
colors := Array new:16.
|
81
|
144 |
0 to:15 do:[:i |
|
|
145 |
colors at:(i+1) put:(Color grey:100 - (100 / 15 * i))
|
|
146 |
]
|
1
|
147 |
] ifFalse:[
|
89
|
148 |
p == #blackIs0 ifTrue:[
|
|
149 |
colors := Array new:16.
|
81
|
150 |
0 to:15 do:[:i |
|
|
151 |
colors at:(i+1) put:(Color grey:(100 / 15 * i))
|
|
152 |
]
|
|
153 |
] ifFalse:[
|
89
|
154 |
p == #palette ifTrue:[
|
|
155 |
colors := colorMap.
|
81
|
156 |
] ifFalse:[
|
|
157 |
self error:'format not supported'.
|
|
158 |
^ nil
|
89
|
159 |
]
|
81
|
160 |
]
|
1
|
161 |
].
|
|
162 |
|
|
163 |
x1 := xLow.
|
|
164 |
x2 := xHigh.
|
|
165 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
166 |
srcIndex := srcIndex + (x1 // 2).
|
12
|
167 |
x1 even ifTrue:[
|
|
168 |
shift := -4
|
|
169 |
] ifFalse:[
|
|
170 |
shift := 0
|
|
171 |
].
|
1
|
172 |
|
|
173 |
x1 to:x2 do:[:x |
|
12
|
174 |
shift == 0 ifTrue:[
|
81
|
175 |
byte := bytes at:srcIndex.
|
|
176 |
value := byte bitAnd:16rF.
|
|
177 |
shift := -4.
|
|
178 |
srcIndex := srcIndex + 1.
|
|
179 |
] ifFalse:[
|
|
180 |
byte := bytes at:srcIndex.
|
|
181 |
value := (byte bitShift:-4) bitAnd:16rF.
|
|
182 |
shift := 0
|
12
|
183 |
].
|
81
|
184 |
aBlock value:x value:(colors at:(value + 1)).
|
1
|
185 |
]
|
280
|
186 |
!
|
|
187 |
|
|
188 |
valueAtY:y from:xLow to:xHigh do:aBlock
|
|
189 |
"perform aBlock for each pixelValue from x1 to x2 in row y.
|
|
190 |
The block is passed the pixelValue at each pixel.
|
|
191 |
This method allows slighly faster processing of an
|
|
192 |
image than using valueAtX:y:, since some processing can be
|
|
193 |
avoided when going from pixel to pixel. However, for
|
|
194 |
real image processing, specialized methods should be written."
|
|
195 |
|
|
196 |
|srcIndex "{ Class: SmallInteger }"
|
|
197 |
byte "{ Class: SmallInteger }"
|
|
198 |
shift "{ Class: SmallInteger }"
|
|
199 |
pixelValue "{ Class: SmallInteger }"
|
|
200 |
x1 "{ Class: SmallInteger }"
|
|
201 |
x2 "{ Class: SmallInteger }"
|
|
202 |
|
|
|
203 |
|
|
204 |
x1 := xLow.
|
|
205 |
x2 := xHigh.
|
|
206 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
207 |
srcIndex := srcIndex + (x1 // 2).
|
|
208 |
x1 even ifTrue:[
|
|
209 |
shift := -4
|
|
210 |
] ifFalse:[
|
|
211 |
shift := 0
|
|
212 |
].
|
|
213 |
|
|
214 |
x1 to:x2 do:[:x |
|
|
215 |
shift == 0 ifTrue:[
|
|
216 |
byte := bytes at:srcIndex.
|
|
217 |
pixelValue := byte bitAnd:16rF.
|
|
218 |
shift := -4.
|
|
219 |
srcIndex := srcIndex + 1.
|
|
220 |
] ifFalse:[
|
|
221 |
byte := bytes at:srcIndex.
|
|
222 |
pixelValue := (byte bitShift:-4) bitAnd:16rF.
|
|
223 |
shift := 0
|
|
224 |
].
|
|
225 |
aBlock value:x value:pixelValue.
|
|
226 |
]
|
1
|
227 |
! !
|
|
228 |
|
|
229 |
!Depth4Image methodsFor:'magnification'!
|
|
230 |
|
|
231 |
magnifyRowFrom:srcBytes offset:srcStart
|
81
|
232 |
into:dstBytes offset:dstStart factor:mX
|
1
|
233 |
|
89
|
234 |
"magnify a single pixel row - can only magnify by integer factors.
|
|
235 |
Specially tuned for factor 2."
|
1
|
236 |
|
|
237 |
%{
|
|
238 |
unsigned char *srcP, *dstP;
|
|
239 |
int _mag;
|
|
240 |
REGISTER int i;
|
|
241 |
REGISTER unsigned char _byte;
|
|
242 |
int _pixels;
|
|
243 |
REGISTER int outcnt, bits, bit;
|
97
|
244 |
OBJ w = _INST(width);
|
1
|
245 |
|
97
|
246 |
if (__bothSmallInteger(srcStart, dstStart)
|
|
247 |
&& __bothSmallInteger(w, mX)
|
35
|
248 |
&& __isByteArray(srcBytes) && __isByteArray(dstBytes)) {
|
81
|
249 |
_mag = _intVal(mX);
|
|
250 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
|
|
251 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
|
97
|
252 |
_pixels = _intVal(w);
|
1
|
253 |
|
81
|
254 |
switch (_mag) {
|
|
255 |
case 1:
|
|
256 |
break;
|
1
|
257 |
|
81
|
258 |
case 2:
|
|
259 |
_byte = *srcP++;
|
|
260 |
while (_pixels) {
|
|
261 |
bit = (_byte >> 4) & 0xF;
|
|
262 |
bits = (bit << 4) | bit;
|
|
263 |
*dstP++ = bits;
|
1
|
264 |
|
81
|
265 |
if (--_pixels) {
|
|
266 |
bit = _byte & 0xF;
|
|
267 |
bits = (bit << 4) | bit;
|
|
268 |
*dstP++ = bits;
|
|
269 |
_byte = *srcP++;
|
|
270 |
_pixels--;
|
|
271 |
}
|
|
272 |
}
|
|
273 |
break;
|
1
|
274 |
|
81
|
275 |
default:
|
|
276 |
bits = 0, outcnt = 0;
|
|
277 |
_byte = *srcP++;
|
|
278 |
while (_pixels) {
|
|
279 |
bit = (_byte >> 4) & 0xF;
|
1
|
280 |
|
81
|
281 |
for (i=_mag; i>0; i--) {
|
|
282 |
bits = (bits << 4) | bit;
|
|
283 |
outcnt++;
|
|
284 |
if (outcnt == 2) {
|
|
285 |
*dstP++ = bits;
|
|
286 |
bits = 0;
|
|
287 |
outcnt = 0;
|
|
288 |
}
|
|
289 |
}
|
1
|
290 |
|
81
|
291 |
if (--_pixels) {
|
|
292 |
bit = _byte & 0xF;
|
|
293 |
for (i=_mag; i>0; i--) {
|
|
294 |
bits = (bits << 4) | bit;
|
|
295 |
outcnt++;
|
|
296 |
if (outcnt == 2) {
|
|
297 |
*dstP++ = bits;
|
|
298 |
bits = 0;
|
|
299 |
outcnt = 0;
|
|
300 |
}
|
|
301 |
}
|
|
302 |
_byte = *srcP++;
|
|
303 |
_pixels--;
|
|
304 |
}
|
|
305 |
}
|
1
|
306 |
|
81
|
307 |
break;
|
|
308 |
}
|
|
309 |
RETURN (self);
|
1
|
310 |
}
|
|
311 |
%}
|
|
312 |
.
|
|
313 |
self primitiveFailed
|
|
314 |
! !
|
280
|
315 |
|
|
316 |
!Depth4Image methodsFor:'queries'!
|
|
317 |
|
|
318 |
bitsPerPixel
|
|
319 |
"return the number of bits per pixel"
|
|
320 |
|
|
321 |
^ 4
|
|
322 |
!
|
|
323 |
|
|
324 |
bitsPerRow
|
|
325 |
"return the number of bits in one scanline of the image"
|
|
326 |
|
|
327 |
^ width * 4
|
|
328 |
!
|
|
329 |
|
|
330 |
bytesPerRow
|
|
331 |
"return the number of bytes in one scanline of the image"
|
|
332 |
|
|
333 |
|nbytes|
|
|
334 |
|
|
335 |
nbytes := width // 2.
|
|
336 |
width odd ifTrue:[
|
|
337 |
^ nbytes + 1
|
|
338 |
].
|
|
339 |
^ nbytes
|
|
340 |
!
|
|
341 |
|
|
342 |
usedValues
|
|
343 |
"return a collection of color values used in the receiver."
|
|
344 |
|
|
345 |
|useFlags usedValues|
|
|
346 |
|
|
347 |
useFlags := Array new:16 withAll:false.
|
|
348 |
width even ifFalse:[
|
|
349 |
0 to:self height - 1 do:[:y |
|
|
350 |
self valueAtY:y from:0 to:self width - 1 do:[:x :pixel |
|
|
351 |
useFlags at:(pixel + 1) put:true
|
|
352 |
]
|
|
353 |
].
|
|
354 |
] ifTrue:[
|
|
355 |
bytes usedValues do:[:byte |
|
|
356 |
useFlags at:(byte bitShift:-4)+1 put:true.
|
|
357 |
useFlags at:(byte bitAnd:2r1111)+1 put:true.
|
|
358 |
].
|
|
359 |
].
|
|
360 |
usedValues := OrderedCollection new.
|
|
361 |
1 to:16 do:[:i | (useFlags at:i) ifTrue:[usedValues add:(i-1)]].
|
|
362 |
^ usedValues
|
|
363 |
! !
|
|
364 |
|
|
365 |
!Depth4Image class methodsFor:'documentation'!
|
|
366 |
|
|
367 |
version
|
|
368 |
^ '$Header: /cvs/stx/stx/libview/Depth4Image.st,v 1.14 1995-12-07 11:17:07 cg Exp $'
|
|
369 |
! !
|