1
|
1 |
'From Smalltalk/X, Version:2.7.1 on 9-Aug-1993 at 20:32:37'!
|
|
2 |
|
|
3 |
Image subclass:#Depth4Image
|
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Graphics-Display Objects'
|
|
8 |
!
|
|
9 |
|
|
10 |
!Depth4Image methodsFor:'accessing'!
|
|
11 |
|
|
12 |
bitsPerPixel
|
|
13 |
"return the number of bits per pixel"
|
|
14 |
|
|
15 |
^ 4
|
|
16 |
!
|
|
17 |
|
|
18 |
bitsPerRow
|
|
19 |
"return the number of bits in one scanline of the image"
|
|
20 |
|
|
21 |
^ width * 4
|
|
22 |
!
|
|
23 |
|
|
24 |
bitsPerSample
|
|
25 |
"return the number of bits per sample.
|
|
26 |
The return value is an array of bits-per-plane."
|
|
27 |
|
|
28 |
^ #(4)
|
|
29 |
!
|
|
30 |
|
|
31 |
bytesPerRow
|
|
32 |
"return the number of bytes in one scanline of the image"
|
|
33 |
|
|
34 |
|nbytes|
|
|
35 |
|
|
36 |
nbytes := width // 2.
|
|
37 |
width odd ifTrue:[
|
|
38 |
^ nbytes + 1
|
|
39 |
].
|
|
40 |
^ nbytes
|
|
41 |
!
|
|
42 |
|
|
43 |
samplesPerPixel
|
|
44 |
"return the number of samples per pixel in the image."
|
|
45 |
|
|
46 |
^ 1
|
|
47 |
!
|
|
48 |
|
|
49 |
valueAtX:x y:y
|
|
50 |
"retrieve a pixel at x/y; return a pixelValue.
|
|
51 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
52 |
x = width-1, y=height-1 for lower right pixel"
|
|
53 |
|
|
54 |
|lineIndex byte value|
|
|
55 |
|
|
56 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
57 |
|
|
58 |
"left pixel in high bits"
|
|
59 |
byte := bytes at:(lineIndex + (x // 2)).
|
|
60 |
x even ifTrue:[
|
|
61 |
^ (byte bitShift:-4) bitAnd:16rF.
|
|
62 |
].
|
|
63 |
^ byte bitAnd:16rF.
|
|
64 |
!
|
|
65 |
|
|
66 |
atX:x y:y
|
|
67 |
"retrieve a pixel at x/y; return a color.
|
|
68 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
69 |
x = width-1, y=height-1 for lower right pixel"
|
|
70 |
|
|
71 |
|lineIndex byte value|
|
|
72 |
|
|
73 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
74 |
|
|
75 |
"left pixel in high bits"
|
|
76 |
byte := bytes at:(lineIndex + (x // 2)).
|
|
77 |
x even ifTrue:[
|
|
78 |
value := (byte bitShift:-4) bitAnd:16rF.
|
|
79 |
] ifFalse:[
|
|
80 |
value := byte bitAnd:16rF.
|
|
81 |
].
|
|
82 |
photometric == #whiteIs0 ifTrue:[
|
|
83 |
^ Color grey:100 - (100 / 15 * value)
|
|
84 |
].
|
|
85 |
photometric == #blackIs0 ifTrue:[
|
|
86 |
^ Color grey:(100 / 15 * value)
|
|
87 |
].
|
|
88 |
photometric ~~ #palette ifTrue:[
|
|
89 |
self error:'format not supported'.
|
|
90 |
^ nil
|
|
91 |
].
|
|
92 |
^ Color red:(((colorMap at:1) at:(value + 1)) * 100 / 255)
|
|
93 |
green:(((colorMap at:2) at:(value + 1)) * 100 / 255)
|
|
94 |
blue:(((colorMap at:3) at:(value + 1)) * 100 / 255)
|
|
95 |
!
|
|
96 |
|
|
97 |
atX:x y:y putValue:aPixelValue
|
|
98 |
"set the pixel at x/y to aPixelValue.
|
|
99 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
100 |
x = width-1, y=height-1 for lower right pixel"
|
|
101 |
|
|
102 |
|lineIndex index byte|
|
|
103 |
|
|
104 |
lineIndex := (self bytesPerRow * y) + 1.
|
|
105 |
|
|
106 |
"left pixel is in high bits"
|
|
107 |
index := lineIndex + (x // 2).
|
|
108 |
byte := bytes at:index.
|
|
109 |
x even ifTrue:[
|
|
110 |
byte := (byte bitAnd:16rF) bitOr:(aPixelValue bitShift:4)
|
|
111 |
] ifFalse:[
|
|
112 |
byte := (byte bitAnd:16rF0) bitOr:aPixelValue
|
|
113 |
].
|
|
114 |
bytes at:index put:byte
|
|
115 |
!
|
|
116 |
|
|
117 |
atY:y from:xLow to:xHigh do:aBlock
|
|
118 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
119 |
The block is passed the color at each pixel.
|
|
120 |
This method allows slighly faster processing of an
|
|
121 |
image than using atX:y:, since some processing can be
|
|
122 |
avoided when going from pixel to pixel. However, for
|
|
123 |
real image processing, specialized methods should be written."
|
|
124 |
|
|
125 |
|srcIndex "{ Class: SmallInteger }"
|
|
126 |
byte "{ Class: SmallInteger }"
|
|
127 |
shift "{ Class: SmallInteger }"
|
|
128 |
value "{ Class: SmallInteger }"
|
|
129 |
x1 "{ Class: SmallInteger }"
|
|
130 |
x2 "{ Class: SmallInteger }"
|
|
131 |
colors|
|
|
132 |
|
|
133 |
colors := Array new:16.
|
|
134 |
photometric == #whiteIs0 ifTrue:[
|
|
135 |
0 to:15 do:[:i |
|
|
136 |
colors at:(i+1) put:(Color grey:100 - (100 / 15 * i))
|
|
137 |
]
|
|
138 |
] ifFalse:[
|
|
139 |
photometric == #blackIs0 ifTrue:[
|
|
140 |
0 to:15 do:[:i |
|
|
141 |
colors at:(i+1) put:(Color grey:(100 / 15 * i))
|
|
142 |
]
|
|
143 |
] ifFalse:[
|
|
144 |
photometric == #palette ifTrue:[
|
|
145 |
1 to:16 do:[:i |
|
|
146 |
colors at:i
|
|
147 |
put:(Color red:(((colorMap at:1) at:i) * 100 / 255)
|
|
148 |
green:(((colorMap at:2) at:i) * 100 / 255)
|
|
149 |
blue:(((colorMap at:3) at:i) * 100 / 255))
|
|
150 |
]
|
|
151 |
] ifFalse:[
|
|
152 |
self error:'format not supported'.
|
|
153 |
^ nil
|
|
154 |
]
|
|
155 |
]
|
|
156 |
].
|
|
157 |
|
|
158 |
x1 := xLow.
|
|
159 |
x2 := xHigh.
|
|
160 |
srcIndex := (self bytesPerRow * y) + 1.
|
|
161 |
srcIndex := srcIndex + (x1 // 2).
|
|
162 |
shift := #(-4 0) at:((x1 \\ 2) + 1).
|
|
163 |
|
|
164 |
x1 to:x2 do:[:x |
|
|
165 |
byte := bytes at:srcIndex.
|
|
166 |
value := (byte bitShift:shift) bitAnd:16rF.
|
|
167 |
|
|
168 |
aBlock value:x value:(colors at:(value + 1)).
|
|
169 |
shift == 0 ifTrue:[
|
|
170 |
shift := -4.
|
|
171 |
srcIndex := srcIndex + 1.
|
|
172 |
] ifFalse:[
|
|
173 |
shift := 0
|
|
174 |
]
|
|
175 |
]
|
|
176 |
! !
|
|
177 |
|
|
178 |
!Depth4Image methodsFor:'magnification'!
|
|
179 |
|
|
180 |
magnifyRowFrom:srcBytes offset:srcStart
|
|
181 |
into:dstBytes offset:dstStart factor:mX
|
|
182 |
|
|
183 |
"magnify a single pixel row - can only magnify by integer factors"
|
|
184 |
|
|
185 |
%{
|
|
186 |
unsigned char *srcP, *dstP;
|
|
187 |
int _mag;
|
|
188 |
REGISTER int i;
|
|
189 |
REGISTER unsigned char _byte;
|
|
190 |
int _pixels;
|
|
191 |
REGISTER int outcnt, bits, bit;
|
|
192 |
|
|
193 |
if (_isSmallInteger(srcStart) && _isSmallInteger(dstStart)
|
|
194 |
&& _isSmallInteger(_INST(width)) && _isSmallInteger(mX)
|
|
195 |
&& _isByteArray(srcBytes) && _isByteArray(dstBytes)) {
|
|
196 |
_mag = _intVal(mX);
|
|
197 |
srcP = _ByteArrayInstPtr(srcBytes)->ba_element - 1 + _intVal(srcStart);
|
|
198 |
dstP = _ByteArrayInstPtr(dstBytes)->ba_element - 1 + _intVal(dstStart);
|
|
199 |
_pixels = _intVal(_INST(width));
|
|
200 |
|
|
201 |
switch (_mag) {
|
|
202 |
case 1:
|
|
203 |
break;
|
|
204 |
|
|
205 |
case 2:
|
|
206 |
_byte = *srcP++;
|
|
207 |
while (_pixels) {
|
|
208 |
bit = (_byte >> 4) & 0xF;
|
|
209 |
bits = (bit << 4) | bit;
|
|
210 |
*dstP++ = bits;
|
|
211 |
|
|
212 |
if (--_pixels) {
|
|
213 |
bit = _byte & 0xF;
|
|
214 |
bits = (bit << 4) | bit;
|
|
215 |
*dstP++ = bits;
|
|
216 |
_byte = *srcP++;
|
|
217 |
_pixels--;
|
|
218 |
}
|
|
219 |
}
|
|
220 |
break;
|
|
221 |
|
|
222 |
default:
|
|
223 |
bits = 0, outcnt = 0;
|
|
224 |
_byte = *srcP++;
|
|
225 |
while (_pixels) {
|
|
226 |
bit = (_byte >> 4) & 0xF;
|
|
227 |
|
|
228 |
for (i=_mag; i>0; i--) {
|
|
229 |
bits = (bits << 4) | bit;
|
|
230 |
outcnt++;
|
|
231 |
if (outcnt == 2) {
|
|
232 |
*dstP++ = bits;
|
|
233 |
bits = 0;
|
|
234 |
outcnt = 0;
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
if (--_pixels) {
|
|
239 |
bit = _byte & 0xF;
|
|
240 |
for (i=_mag; i>0; i--) {
|
|
241 |
bits = (bits << 4) | bit;
|
|
242 |
outcnt++;
|
|
243 |
if (outcnt == 2) {
|
|
244 |
*dstP++ = bits;
|
|
245 |
bits = 0;
|
|
246 |
outcnt = 0;
|
|
247 |
}
|
|
248 |
}
|
|
249 |
_byte = *srcP++;
|
|
250 |
_pixels--;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
break;
|
|
255 |
}
|
|
256 |
RETURN (self);
|
|
257 |
}
|
|
258 |
%}
|
|
259 |
.
|
|
260 |
self primitiveFailed
|
|
261 |
! !
|