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