1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989-92 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
"
|
|
12 |
|
|
13 |
IntegerArray subclass:#ByteArray
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Indexed'
|
|
18 |
!
|
|
19 |
|
|
20 |
ByteArray comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1989-92 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
ByteArrays store integers in the range 0..255
|
|
26 |
unlike Smalltalk/80, my ByteArrays have fixed size - may change
|
|
27 |
|
|
28 |
%W% %E%
|
|
29 |
|
|
30 |
written spring 89 by claus
|
|
31 |
'!
|
|
32 |
|
2
|
33 |
!ByteArray class methodsFor: 'binary storage'!
|
|
34 |
|
|
35 |
binaryDefinitionFrom: stream manager: manager
|
|
36 |
^ (stream next: (stream nextNumber: 4))
|
|
37 |
! !
|
|
38 |
|
|
39 |
!ByteArray methodsFor: 'binary storage'!
|
|
40 |
|
|
41 |
storeBinaryDefinitionOn: stream manager: manager
|
|
42 |
manager putIdOf: self class on: stream.
|
|
43 |
stream nextNumber: 4 put: self basicSize.
|
|
44 |
stream nextPutAll: self
|
|
45 |
! !
|
|
46 |
|
1
|
47 |
!ByteArray class methodsFor:'instance creation'!
|
|
48 |
|
|
49 |
uninitializedNew:anInteger
|
|
50 |
"return a new instance of the receiver with uninitialized
|
|
51 |
(i.e. undefined) contents. The indexed elements have any random
|
|
52 |
value. use, when contents will be set anyway shortly after."
|
|
53 |
|
|
54 |
%{ /* NOCONTEXT */
|
|
55 |
|
|
56 |
OBJ newobj;
|
|
57 |
INT instsize, nInstVars, nindexedinstvars;
|
|
58 |
REGISTER OBJ *op;
|
|
59 |
extern OBJ new();
|
|
60 |
|
|
61 |
if (_isSmallInteger(anInteger)) {
|
|
62 |
nindexedinstvars = _intVal(anInteger);
|
|
63 |
if (nindexedinstvars >= 0) {
|
|
64 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
|
|
65 |
if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY) {
|
|
66 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
|
|
67 |
PROTECT(self);
|
|
68 |
_qNew(newobj, instsize, SENDER);
|
|
69 |
UNPROTECT(self);
|
|
70 |
_InstPtr(newobj)->o_class = self;
|
|
71 |
#if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
72 |
/*
|
|
73 |
* knowing that nil is 0
|
|
74 |
*/
|
|
75 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
76 |
#else
|
|
77 |
op = _InstPtr(newobj)->i_instvars;
|
|
78 |
while (nInstVars--)
|
|
79 |
*op++ = nil;
|
|
80 |
#endif
|
|
81 |
RETURN ( newobj );
|
|
82 |
}
|
|
83 |
}
|
|
84 |
}
|
|
85 |
%}
|
|
86 |
.
|
|
87 |
^ self basicNew:anInteger
|
|
88 |
|
|
89 |
! !
|
|
90 |
|
|
91 |
!ByteArray methodsFor:'accessing'!
|
|
92 |
|
|
93 |
basicAt:index
|
|
94 |
"return the indexed instance variable with index, anInteger
|
2
|
95 |
- redefined here to be slighly faster than the default in Object"
|
1
|
96 |
|
|
97 |
%{ /* NOCONTEXT */
|
|
98 |
|
|
99 |
REGISTER int indx;
|
|
100 |
int nIndex;
|
|
101 |
|
2
|
102 |
if (_isSmallInteger(index)) {
|
1
|
103 |
indx = _intVal(index);
|
2
|
104 |
|
|
105 |
if (_qClass(self) != ByteArray)
|
|
106 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
1
|
107 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
108 |
if ((indx > 0) && (indx <= nIndex)) {
|
|
109 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
|
|
110 |
}
|
|
111 |
}
|
|
112 |
%}
|
|
113 |
.
|
|
114 |
^ super basicAt:index
|
|
115 |
!
|
|
116 |
|
|
117 |
basicAt:index put:value
|
|
118 |
"set the indexed instance variable with index, anInteger to value
|
2
|
119 |
- redefined here to be slighly faster than the default in Object"
|
1
|
120 |
|
|
121 |
%{ /* NOCONTEXT */
|
|
122 |
|
|
123 |
REGISTER int indx;
|
|
124 |
int nIndex;
|
|
125 |
int val;
|
|
126 |
|
2
|
127 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
1
|
128 |
val = _intVal(value);
|
|
129 |
if ((val >= 0) && (val <= 255)) {
|
|
130 |
indx = _intVal(index);
|
2
|
131 |
if (_qClass(self) != ByteArray)
|
|
132 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
1
|
133 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
134 |
if ((indx > 0) && (indx <= nIndex)) {
|
|
135 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
|
|
136 |
RETURN ( value );
|
|
137 |
}
|
|
138 |
}
|
|
139 |
}
|
|
140 |
%}
|
|
141 |
.
|
|
142 |
^ super basicAt:index put:value
|
2
|
143 |
!
|
|
144 |
|
|
145 |
wordAt:index
|
|
146 |
"return the 2-bytes starting at index as an (unsigned) Integer.
|
|
147 |
question: should it be signed ?"
|
|
148 |
|
|
149 |
%{ /* NOCONTEXT */
|
|
150 |
|
|
151 |
REGISTER int indx;
|
|
152 |
int nIndex;
|
|
153 |
int val;
|
|
154 |
extern OBJ _makeLarge();
|
|
155 |
|
|
156 |
if (_isSmallInteger(index)) {
|
|
157 |
indx = _intVal(index);
|
|
158 |
if (_qClass(self) != ByteArray)
|
|
159 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
160 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
161 |
if ((indx > 0) && ((indx+1) <= nIndex)) {
|
|
162 |
val = _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
163 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
|
164 |
RETURN ( _MKSMALLINT(val) );
|
|
165 |
}
|
|
166 |
}
|
|
167 |
%}
|
|
168 |
.
|
|
169 |
^ ((self at:index+1) * 256) + (self at:index)
|
|
170 |
!
|
|
171 |
|
|
172 |
wordAt:index put:value
|
|
173 |
"set the 2-bytes starting at index from the (unsigned) Integer value.
|
|
174 |
question: should it be signed ?"
|
|
175 |
|
|
176 |
%{ /* NOCONTEXT */
|
|
177 |
|
|
178 |
REGISTER int indx;
|
|
179 |
int nIndex;
|
|
180 |
int val;
|
|
181 |
extern OBJ _makeLarge();
|
|
182 |
|
|
183 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
|
184 |
indx = _intVal(index);
|
|
185 |
if (_qClass(self) != ByteArray)
|
|
186 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
187 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
188 |
if ((indx > 0) && ((indx+1) <= nIndex)) {
|
|
189 |
val = _intVal(value);
|
|
190 |
if ((val >= 0) && (val <= 0xFFFF)) {
|
|
191 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
192 |
val >>= 8;
|
|
193 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
|
|
194 |
RETURN ( value );
|
|
195 |
}
|
|
196 |
}
|
|
197 |
}
|
|
198 |
%}
|
|
199 |
.
|
|
200 |
(value < 0) ifTrue:[
|
|
201 |
^ self elementBoundsError
|
|
202 |
].
|
|
203 |
self at:index put:(value \\ 256).
|
|
204 |
self at:index + 1 put:(value // 256).
|
|
205 |
^ value
|
|
206 |
!
|
|
207 |
|
|
208 |
doubleWordAt:index
|
|
209 |
"return the 4-bytes starting at index as an (unsigned) Integer.
|
|
210 |
question: should it be signed ?"
|
|
211 |
|
|
212 |
%{ /* NOCONTEXT */
|
|
213 |
|
|
214 |
REGISTER int indx;
|
|
215 |
int nIndex;
|
|
216 |
int val;
|
|
217 |
extern OBJ _makeLarge();
|
|
218 |
|
|
219 |
if (_isSmallInteger(index)) {
|
|
220 |
indx = _intVal(index);
|
|
221 |
if (_qClass(self) != ByteArray)
|
|
222 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
223 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
224 |
if ((indx > 0) && ((indx+3) <= nIndex)) {
|
|
225 |
val = _ByteArrayInstPtr(self)->ba_element[indx+3-1];
|
|
226 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1];
|
|
227 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
228 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
|
229 |
if ((val >= 0) && (val <= _MAX_INT)) {
|
|
230 |
RETURN ( _MKSMALLINT(val) );
|
|
231 |
}
|
|
232 |
}
|
|
233 |
}
|
|
234 |
%}
|
|
235 |
.
|
|
236 |
^ ((self wordAt:index+2) * (256 *256)) + (self wordAt:index)
|
|
237 |
!
|
|
238 |
|
|
239 |
doubleWordAt:index put:value
|
|
240 |
"set the 4-bytes starting at index from the (unsigned) Integer value.
|
|
241 |
question: should it be signed ?"
|
|
242 |
|
|
243 |
|t|
|
|
244 |
|
|
245 |
%{ /* NOCONTEXT */
|
|
246 |
|
|
247 |
REGISTER int indx;
|
|
248 |
int nIndex;
|
|
249 |
int val;
|
|
250 |
extern OBJ _makeLarge();
|
|
251 |
|
|
252 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
|
253 |
val = _intVal(value);
|
|
254 |
if (val >= 0) {
|
|
255 |
indx = _intVal(index);
|
|
256 |
if (_qClass(self) != ByteArray)
|
|
257 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
258 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
259 |
if ((indx > 0) && ((indx+3) <= nIndex)) {
|
|
260 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
261 |
val >>= 8;
|
|
262 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
|
|
263 |
val >>= 8;
|
|
264 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF;
|
|
265 |
val >>= 8;
|
|
266 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF;
|
|
267 |
RETURN ( value );
|
|
268 |
}
|
|
269 |
}
|
|
270 |
}
|
|
271 |
%}
|
|
272 |
.
|
|
273 |
(value < 0) ifTrue:[
|
|
274 |
^ self elementBoundsError
|
|
275 |
].
|
|
276 |
t := value // (256 * 256).
|
|
277 |
self wordAt:(index+2) put:t.
|
|
278 |
self wordAt:(index) put:(value - (t * 256 * 256)).
|
|
279 |
^ value
|
1
|
280 |
! !
|
|
281 |
|
|
282 |
!ByteArray methodsFor:'queries'!
|
|
283 |
|
|
284 |
indexOf:aByte startingAt:start
|
|
285 |
"return the index of the first occurrence of the argument, aByte
|
|
286 |
in the receiver starting at start, anInteger; return 0 if not found.
|
|
287 |
- reimplemented here for speed"
|
|
288 |
|
|
289 |
%{ /* NOCONTEXT */
|
|
290 |
|
|
291 |
REGISTER unsigned char *cp;
|
|
292 |
REGISTER int index, byteValue;
|
|
293 |
REGISTER int len;
|
|
294 |
|
|
295 |
if (! _isSmallInteger(aByte)) {
|
|
296 |
RETURN ( _MKSMALLINT(0) );
|
|
297 |
}
|
|
298 |
|
|
299 |
byteValue = _intVal(aByte);
|
|
300 |
|
|
301 |
if ((byteValue < 0) || (byteValue > 255)) {
|
|
302 |
RETURN ( _MKSMALLINT(0) );
|
|
303 |
}
|
|
304 |
|
2
|
305 |
if (_isSmallInteger(start)) {
|
1
|
306 |
index = _intVal(start);
|
|
307 |
len = _qSize(self) - OHDR_SIZE;
|
|
308 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
2
|
309 |
if (_qClass(self) != ByteArray) {
|
|
310 |
int nInst;
|
|
311 |
|
|
312 |
nInst = _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
313 |
cp += nInst;
|
|
314 |
len -= nInst;
|
|
315 |
}
|
1
|
316 |
cp += index - 1;
|
|
317 |
while (index <= len) {
|
|
318 |
if (*cp == byteValue) {
|
|
319 |
RETURN ( _MKSMALLINT(index) );
|
|
320 |
}
|
|
321 |
index++;
|
|
322 |
cp++;
|
|
323 |
}
|
|
324 |
RETURN ( _MKSMALLINT(0) );
|
|
325 |
}
|
|
326 |
%}
|
|
327 |
.
|
|
328 |
^ super indexOf:aByte startingAt:start
|
|
329 |
!
|
|
330 |
|
|
331 |
usedValues
|
|
332 |
"return a new ByteArray with all used values (actually a kind of Set);
|
|
333 |
needed specially for Image class."
|
|
334 |
|
|
335 |
|result l|
|
|
336 |
%{
|
|
337 |
REGISTER unsigned char *cp;
|
|
338 |
REGISTER int len;
|
|
339 |
unsigned char flags[256];
|
|
340 |
static struct inlineCache nw = _ILC1;
|
|
341 |
extern OBJ ByteArray, _new_;
|
|
342 |
|
|
343 |
if (_qClass(self) == ByteArray) {
|
|
344 |
memset(flags, 0, sizeof(flags));
|
|
345 |
len = _qSize(self) - OHDR_SIZE;
|
|
346 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
347 |
|
|
348 |
/* for each used byte, set flag */
|
|
349 |
while (len > 0) {
|
|
350 |
flags[*cp] = 1;
|
|
351 |
cp++;
|
|
352 |
len--;
|
|
353 |
}
|
|
354 |
/* count 1's */
|
|
355 |
len = 0;
|
2
|
356 |
for (cp=flags+255; cp >= flags; cp--)
|
1
|
357 |
if (*cp) len++;
|
|
358 |
|
|
359 |
/* create ByteArray of used values */
|
|
360 |
#ifdef PASS_ARG_REF
|
|
361 |
l = _MKSMALLINT(len);
|
|
362 |
result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, &l);
|
|
363 |
#else
|
|
364 |
result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, _MKSMALLINT(len));
|
|
365 |
#endif
|
|
366 |
if (_Class(result) == ByteArray) {
|
|
367 |
cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
|
|
368 |
for (len=0; len < 256; len++) {
|
|
369 |
if (flags[len])
|
|
370 |
*cp++ = len;
|
|
371 |
}
|
|
372 |
}
|
|
373 |
RETURN ( result );
|
|
374 |
}
|
|
375 |
%}
|
|
376 |
.
|
|
377 |
self primitiveFailed
|
|
378 |
!
|
|
379 |
|
|
380 |
usageCounts
|
|
381 |
"return an array filled with value-counts -
|
|
382 |
added for Image handling"
|
|
383 |
|
|
384 |
|counts|
|
|
385 |
|
|
386 |
counts := Array new:256.
|
|
387 |
%{
|
|
388 |
REGISTER unsigned char *cp;
|
|
389 |
REGISTER int nByte;
|
|
390 |
REGISTER int index;
|
|
391 |
int icounts[256];
|
|
392 |
|
|
393 |
if ((_qClass(self) == ByteArray) && _isArray(counts)) {
|
|
394 |
/*
|
|
395 |
* zero counts
|
|
396 |
*/
|
|
397 |
for (index=0; index<256; index++) {
|
|
398 |
icounts[index] = 0;
|
|
399 |
}
|
|
400 |
|
|
401 |
/*
|
|
402 |
* count
|
|
403 |
*/
|
|
404 |
nByte = _qSize(self) - OHDR_SIZE;
|
|
405 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
406 |
while (nByte--) {
|
|
407 |
icounts[*cp++]++;
|
|
408 |
}
|
|
409 |
|
|
410 |
/*
|
|
411 |
* make it real counts
|
|
412 |
*/
|
|
413 |
for (index=0; index<256; index++) {
|
|
414 |
_ArrayInstPtr(counts)->a_element[index] = _MKSMALLINT(icounts[index]);
|
|
415 |
}
|
|
416 |
RETURN ( counts );
|
|
417 |
}
|
|
418 |
%}
|
|
419 |
.
|
|
420 |
self primitiveFailed
|
|
421 |
!
|
|
422 |
|
|
423 |
maximumValue
|
|
424 |
"return the maximum value in the receiver -
|
|
425 |
added for sound-player (which needs a fast method for this)"
|
|
426 |
|
|
427 |
%{ /* NOCONTEXT */
|
|
428 |
|
|
429 |
REGISTER unsigned char *cp;
|
|
430 |
REGISTER int index, max;
|
|
431 |
int len;
|
|
432 |
|
|
433 |
if (_qClass(self) == ByteArray) {
|
|
434 |
max = 0;
|
|
435 |
index = 0;
|
|
436 |
len = _qSize(self) - OHDR_SIZE;
|
|
437 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
438 |
while (++index <= len) {
|
|
439 |
if (*cp > max) max = *cp;
|
|
440 |
cp++;
|
|
441 |
}
|
|
442 |
RETURN ( _MKSMALLINT(max) );
|
|
443 |
}
|
|
444 |
%}
|
|
445 |
.
|
|
446 |
self primitiveFailed
|
|
447 |
! !
|
|
448 |
|
|
449 |
!ByteArray methodsFor:'filling and replacing'!
|
|
450 |
|
|
451 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
452 |
"reimplemented for speed"
|
|
453 |
|
|
454 |
%{ /* NOCONTEXT */
|
|
455 |
|
|
456 |
int nIndex, repNIndex;
|
|
457 |
int startIndex, stopIndex;
|
|
458 |
REGISTER unsigned char *src;
|
|
459 |
REGISTER int repStartIndex;
|
|
460 |
int repStopIndex, count;
|
|
461 |
REGISTER unsigned char *dst;
|
|
462 |
|
|
463 |
if ((_qClass(self) == ByteArray)
|
|
464 |
&& (_Class(aCollection) == ByteArray)
|
|
465 |
&& _isSmallInteger(start)
|
|
466 |
&& _isSmallInteger(stop)
|
|
467 |
&& _isSmallInteger(repStart)) {
|
|
468 |
startIndex = _intVal(start) - 1;
|
|
469 |
if (startIndex >= 0) {
|
|
470 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
471 |
stopIndex = _intVal(stop) - 1;
|
|
472 |
count = stopIndex - startIndex + 1;
|
|
473 |
if ((count > 0) && (stopIndex < nIndex)) {
|
|
474 |
repStartIndex = _intVal(repStart) - 1;
|
|
475 |
if (repStartIndex >= 0) {
|
|
476 |
repNIndex = _qSize(aCollection) - OHDR_SIZE;
|
|
477 |
repStopIndex = repStartIndex + (stopIndex - startIndex);
|
|
478 |
if (repStopIndex < repNIndex) {
|
|
479 |
src = &(_ByteArrayInstPtr(aCollection)->ba_element[repStartIndex]);
|
|
480 |
dst = &(_ByteArrayInstPtr(self)->ba_element[startIndex]);
|
|
481 |
|
|
482 |
if (aCollection == self) {
|
|
483 |
/* take care of overlapping copy */
|
|
484 |
if (src < dst) {
|
|
485 |
/* must do a reverse copy */
|
|
486 |
src += count;
|
|
487 |
dst += count;
|
|
488 |
while (count-- > 0) {
|
|
489 |
*--dst = *--src;
|
|
490 |
}
|
|
491 |
RETURN ( self );
|
|
492 |
}
|
|
493 |
}
|
|
494 |
#ifdef FAST_MEMCPY
|
|
495 |
bcopy(src, dst, count);
|
|
496 |
#else
|
|
497 |
while (count-- > 0) {
|
|
498 |
*dst++ = *src++;
|
|
499 |
}
|
|
500 |
#endif
|
|
501 |
RETURN ( self );
|
|
502 |
}
|
|
503 |
}
|
|
504 |
}
|
|
505 |
}
|
|
506 |
}
|
|
507 |
%}
|
|
508 |
.
|
|
509 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
510 |
! !
|
|
511 |
|
|
512 |
!ByteArray methodsFor:'image manipulation'!
|
|
513 |
|
|
514 |
invert
|
|
515 |
"invert all bytes - used with image manipulations
|
|
516 |
written as a primitive for speed"
|
|
517 |
|
|
518 |
%{ /* NOCONTEXT */
|
|
519 |
|
|
520 |
REGISTER unsigned char *dst;
|
|
521 |
REGISTER unsigned long *ldst;
|
|
522 |
REGISTER int cnt;
|
|
523 |
|
|
524 |
if (_qClass(self) == ByteArray) {
|
|
525 |
cnt = _qSize(self) - OHDR_SIZE;
|
|
526 |
dst = _ByteArrayInstPtr(self)->ba_element;
|
|
527 |
if (! ((int)dst & (sizeof(long)-1))) {
|
|
528 |
ldst = (unsigned long *)dst;
|
|
529 |
while (cnt >= sizeof(long)) {
|
|
530 |
*ldst = ~(*ldst);
|
|
531 |
ldst++;
|
|
532 |
cnt -= sizeof(long);
|
|
533 |
}
|
|
534 |
dst = (unsigned char *)ldst;
|
|
535 |
}
|
|
536 |
while (cnt--) {
|
|
537 |
*dst = ~(*dst);
|
|
538 |
dst++;
|
|
539 |
}
|
|
540 |
RETURN ( self );
|
|
541 |
}
|
|
542 |
%}
|
|
543 |
.
|
|
544 |
self primitiveFailed
|
|
545 |
!
|
|
546 |
|
|
547 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray
|
|
548 |
mapping:aMapByteArray
|
|
549 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into
|
|
550 |
aByteArray with 8-bit pixels. The width/height-arguments are needed
|
|
551 |
to skip any padded src-bits. On the fly, the destination pixels
|
|
552 |
are translated using aMapByteArray if nonnil.
|
|
553 |
- used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color
|
|
554 |
machines"
|
|
555 |
|
|
556 |
%{ /* NOCONTEXT */
|
|
557 |
|
|
558 |
{
|
|
559 |
REGISTER unsigned char *src, *dst;
|
|
560 |
REGISTER int wrun;
|
|
561 |
unsigned char *srcNext;
|
|
562 |
int bytesPerRow, mask, shift0, shift;
|
|
563 |
int w, h, hrun;
|
|
564 |
int srcBytes, dstBytes;
|
|
565 |
int bitsPerPixel;
|
|
566 |
int bits;
|
|
567 |
int ncells;
|
|
568 |
unsigned char *map;
|
|
569 |
|
|
570 |
if ((_qClass(self) == ByteArray)
|
|
571 |
&& (_qClass(aByteArray) == ByteArray)
|
|
572 |
&& _isSmallInteger(nBitsPerPixel)
|
|
573 |
&& _isSmallInteger(height)
|
|
574 |
&& _isSmallInteger(width)) {
|
|
575 |
if ((aMapByteArray != nil)
|
|
576 |
&& (_Class(aMapByteArray) == ByteArray)) {
|
|
577 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
|
|
578 |
} else {
|
|
579 |
map = (unsigned char *)0;
|
|
580 |
}
|
|
581 |
|
|
582 |
bitsPerPixel = _intVal(nBitsPerPixel);
|
|
583 |
w = _intVal(width);
|
|
584 |
h = _intVal(height);
|
|
585 |
src = _ByteArrayInstPtr(self)->ba_element;
|
|
586 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element;
|
|
587 |
switch (bitsPerPixel) {
|
|
588 |
case 1:
|
|
589 |
mask = 0x01;
|
|
590 |
break;
|
|
591 |
case 2:
|
|
592 |
mask = 0x03;
|
|
593 |
break;
|
|
594 |
case 4:
|
|
595 |
mask = 0x0F;
|
|
596 |
break;
|
|
597 |
case 8:
|
|
598 |
mask = 0xFF;
|
|
599 |
break;
|
|
600 |
default:
|
|
601 |
goto fail;
|
|
602 |
}
|
|
603 |
ncells = mask + 1;
|
|
604 |
if (map) {
|
|
605 |
if ((_qSize(aMapByteArray) - OHDR_SIZE) < ncells)
|
|
606 |
goto fail;
|
|
607 |
}
|
|
608 |
|
|
609 |
bytesPerRow = (w * bitsPerPixel + 7) / 8;
|
|
610 |
shift0 = 8 - bitsPerPixel;
|
|
611 |
srcBytes = bytesPerRow * h;
|
|
612 |
dstBytes = w * h;
|
|
613 |
|
|
614 |
if (((_qSize(self) - OHDR_SIZE) >= srcBytes)
|
|
615 |
&& ((_qSize(aByteArray) - OHDR_SIZE) >= dstBytes)) {
|
|
616 |
for (hrun=h; hrun; hrun--) {
|
|
617 |
srcNext = src + bytesPerRow;
|
|
618 |
shift = shift0;
|
|
619 |
if (map) {
|
|
620 |
for (wrun=w; wrun; wrun--) {
|
|
621 |
if (shift == shift0) {
|
|
622 |
bits = *src++;
|
|
623 |
}
|
|
624 |
*dst++ = map[(bits >> shift) & mask];
|
|
625 |
shift -= bitsPerPixel;
|
|
626 |
if (shift < 0) {
|
|
627 |
shift = shift0;
|
|
628 |
}
|
|
629 |
}
|
|
630 |
} else {
|
|
631 |
for (wrun=w; wrun; wrun--) {
|
|
632 |
if (shift == shift0) {
|
|
633 |
bits = *src++;
|
|
634 |
}
|
|
635 |
*dst++ = (bits >> shift) & mask;
|
|
636 |
shift -= bitsPerPixel;
|
|
637 |
if (shift < 0) {
|
|
638 |
shift = shift0;
|
|
639 |
}
|
|
640 |
}
|
|
641 |
}
|
|
642 |
src = srcNext;
|
|
643 |
}
|
|
644 |
RETURN ( self );
|
|
645 |
}
|
|
646 |
}
|
|
647 |
}
|
|
648 |
fail: ;
|
|
649 |
%}
|
|
650 |
.
|
|
651 |
self primitiveFailed
|
|
652 |
! !
|