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