1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
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 |
|
10
|
13 |
ArrayedCollection variableByteSubclass:#ByteArray
|
1
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Indexed'
|
|
18 |
!
|
|
19 |
|
|
20 |
ByteArray comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
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 |
|
54
|
28 |
$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.8 1994-02-25 12:54:53 claus Exp $
|
1
|
29 |
|
|
30 |
written spring 89 by claus
|
|
31 |
'!
|
|
32 |
|
3
|
33 |
!ByteArray class methodsFor:'queries'!
|
|
34 |
|
|
35 |
isBuiltInClass
|
|
36 |
"this class is known by the run-time-system"
|
|
37 |
|
|
38 |
^ self == ByteArray
|
|
39 |
! !
|
|
40 |
|
2
|
41 |
!ByteArray class methodsFor: 'binary storage'!
|
|
42 |
|
|
43 |
binaryDefinitionFrom: stream manager: manager
|
54
|
44 |
"get a ByteArray from the binary stream.
|
|
45 |
ByteArrays are stored as 4-byte size, followed by the bytes"
|
|
46 |
|
|
47 |
"take care of subclasses ..."
|
|
48 |
self == ByteArray ifTrue:[
|
|
49 |
^ (stream next: (stream nextNumber: 4))
|
|
50 |
].
|
|
51 |
^ super binaryDefinitionFrom: stream manager: manager
|
2
|
52 |
! !
|
|
53 |
|
10
|
54 |
!ByteArray methodsFor:'resizing'!
|
|
55 |
|
|
56 |
grow:sizeIncr
|
|
57 |
"report an error - ByteArrays cannot grow"
|
|
58 |
|
|
59 |
self fixedSizeError
|
|
60 |
! !
|
|
61 |
|
2
|
62 |
!ByteArray methodsFor: 'binary storage'!
|
|
63 |
|
|
64 |
storeBinaryDefinitionOn: stream manager: manager
|
37
|
65 |
"append a binary representation of the receiver onto stream.
|
|
66 |
Redefined since ByteArrays are stored with a special type code and
|
|
67 |
in a more compact way."
|
|
68 |
|
|
69 |
"not, if I have named instance variables"
|
|
70 |
self class instSize ~~ 0 ifTrue:[
|
|
71 |
^ super storeBinaryDefinitionOn: stream manager: manager
|
|
72 |
].
|
2
|
73 |
manager putIdOf: self class on: stream.
|
|
74 |
stream nextNumber: 4 put: self basicSize.
|
37
|
75 |
stream nextPutAll: self asByteArray
|
2
|
76 |
! !
|
|
77 |
|
1
|
78 |
!ByteArray class methodsFor:'instance creation'!
|
|
79 |
|
|
80 |
uninitializedNew:anInteger
|
|
81 |
"return a new instance of the receiver with uninitialized
|
|
82 |
(i.e. undefined) contents. The indexed elements have any random
|
|
83 |
value. use, when contents will be set anyway shortly after."
|
|
84 |
|
|
85 |
%{ /* NOCONTEXT */
|
|
86 |
OBJ newobj;
|
|
87 |
INT instsize, nInstVars, nindexedinstvars;
|
|
88 |
REGISTER OBJ *op;
|
|
89 |
extern OBJ new();
|
|
90 |
|
|
91 |
if (_isSmallInteger(anInteger)) {
|
|
92 |
nindexedinstvars = _intVal(anInteger);
|
|
93 |
if (nindexedinstvars >= 0) {
|
|
94 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
|
|
95 |
if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY) {
|
|
96 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
|
12
|
97 |
PROTECT_CONTEXT
|
1
|
98 |
_qNew(newobj, instsize, SENDER);
|
12
|
99 |
UNPROTECT_CONTEXT
|
|
100 |
if (newobj) {
|
|
101 |
_InstPtr(newobj)->o_class = self;
|
1
|
102 |
#if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
12
|
103 |
/*
|
|
104 |
* knowing that nil is 0
|
|
105 |
*/
|
|
106 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
1
|
107 |
#else
|
12
|
108 |
op = _InstPtr(newobj)->i_instvars;
|
|
109 |
while (nInstVars--)
|
|
110 |
*op++ = nil;
|
1
|
111 |
#endif
|
12
|
112 |
RETURN ( newobj );
|
|
113 |
}
|
10
|
114 |
}
|
|
115 |
}
|
1
|
116 |
}
|
|
117 |
%}
|
|
118 |
.
|
12
|
119 |
(anInteger isMemberOf:SmallInteger) ifTrue:[
|
|
120 |
(anInteger < 0) ifTrue:[
|
|
121 |
self error:'bad (negative) argument to new'
|
|
122 |
] ifFalse:[
|
|
123 |
ObjectMemory allocationFailureSignal raise.
|
|
124 |
].
|
|
125 |
^ nil
|
|
126 |
].
|
1
|
127 |
^ self basicNew:anInteger
|
|
128 |
|
|
129 |
! !
|
|
130 |
|
|
131 |
!ByteArray methodsFor:'accessing'!
|
|
132 |
|
|
133 |
basicAt:index
|
|
134 |
"return the indexed instance variable with index, anInteger
|
2
|
135 |
- redefined here to be slighly faster than the default in Object"
|
1
|
136 |
|
|
137 |
%{ /* NOCONTEXT */
|
|
138 |
|
|
139 |
REGISTER int indx;
|
|
140 |
int nIndex;
|
54
|
141 |
OBJ cls;
|
1
|
142 |
|
2
|
143 |
if (_isSmallInteger(index)) {
|
1
|
144 |
indx = _intVal(index);
|
2
|
145 |
|
54
|
146 |
if ((cls = _qClass(self)) != ByteArray)
|
|
147 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars) * sizeof(OBJ);
|
1
|
148 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
149 |
if ((indx > 0) && (indx <= nIndex)) {
|
|
150 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
|
|
151 |
}
|
|
152 |
}
|
|
153 |
%}
|
|
154 |
.
|
|
155 |
^ super basicAt:index
|
|
156 |
!
|
|
157 |
|
|
158 |
basicAt:index put:value
|
|
159 |
"set the indexed instance variable with index, anInteger to value
|
2
|
160 |
- redefined here to be slighly faster than the default in Object"
|
1
|
161 |
|
|
162 |
%{ /* NOCONTEXT */
|
|
163 |
|
|
164 |
REGISTER int indx;
|
|
165 |
int nIndex;
|
|
166 |
int val;
|
54
|
167 |
OBJ cls;
|
1
|
168 |
|
2
|
169 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
1
|
170 |
val = _intVal(value);
|
|
171 |
if ((val >= 0) && (val <= 255)) {
|
|
172 |
indx = _intVal(index);
|
54
|
173 |
if ((cls = _qClass(self)) != ByteArray)
|
|
174 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars) * sizeof(OBJ);
|
1
|
175 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
176 |
if ((indx > 0) && (indx <= nIndex)) {
|
|
177 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
|
|
178 |
RETURN ( value );
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
|
182 |
%}
|
|
183 |
.
|
|
184 |
^ super basicAt:index put:value
|
2
|
185 |
!
|
|
186 |
|
54
|
187 |
byteAt:index
|
|
188 |
"return the byte at index
|
|
189 |
- for ST-80 compatibility"
|
|
190 |
|
|
191 |
^ self at:index
|
|
192 |
!
|
|
193 |
|
|
194 |
byteAt:index put:aByteValuedInteger
|
|
195 |
"return the byte at index
|
|
196 |
- for ST-80 compatibility"
|
|
197 |
|
|
198 |
^ self at:index put:aByteValuedInteger
|
|
199 |
!
|
|
200 |
|
2
|
201 |
wordAt:index
|
|
202 |
"return the 2-bytes starting at index as an (unsigned) Integer.
|
54
|
203 |
Question: should it be signed ?"
|
2
|
204 |
|
|
205 |
%{ /* NOCONTEXT */
|
|
206 |
|
|
207 |
REGISTER int indx;
|
|
208 |
int nIndex;
|
|
209 |
int val;
|
|
210 |
extern OBJ _makeLarge();
|
|
211 |
|
|
212 |
if (_isSmallInteger(index)) {
|
|
213 |
indx = _intVal(index);
|
|
214 |
if (_qClass(self) != ByteArray)
|
|
215 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
216 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
217 |
if ((indx > 0) && ((indx+1) <= nIndex)) {
|
10
|
218 |
val = _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
219 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
2
|
220 |
RETURN ( _MKSMALLINT(val) );
|
|
221 |
}
|
|
222 |
}
|
|
223 |
%}
|
|
224 |
.
|
|
225 |
^ ((self at:index+1) * 256) + (self at:index)
|
|
226 |
!
|
|
227 |
|
|
228 |
wordAt:index put:value
|
|
229 |
"set the 2-bytes starting at index from the (unsigned) Integer value.
|
54
|
230 |
Question: should it be signed ?"
|
2
|
231 |
|
|
232 |
%{ /* NOCONTEXT */
|
|
233 |
|
|
234 |
REGISTER int indx;
|
|
235 |
int nIndex;
|
|
236 |
int val;
|
|
237 |
extern OBJ _makeLarge();
|
|
238 |
|
|
239 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
|
240 |
indx = _intVal(index);
|
|
241 |
if (_qClass(self) != ByteArray)
|
|
242 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
243 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
244 |
if ((indx > 0) && ((indx+1) <= nIndex)) {
|
10
|
245 |
val = _intVal(value);
|
|
246 |
if ((val >= 0) && (val <= 0xFFFF)) {
|
|
247 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
248 |
val >>= 8;
|
|
249 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
|
2
|
250 |
RETURN ( value );
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
%}
|
|
255 |
.
|
|
256 |
(value < 0) ifTrue:[
|
10
|
257 |
^ self elementBoundsError
|
2
|
258 |
].
|
|
259 |
self at:index put:(value \\ 256).
|
|
260 |
self at:index + 1 put:(value // 256).
|
|
261 |
^ value
|
|
262 |
!
|
|
263 |
|
54
|
264 |
signedWordAt:index
|
|
265 |
"return the 2-bytes starting at index as a signed Integer."
|
|
266 |
|
|
267 |
|w|
|
|
268 |
|
|
269 |
w := self wordAt:index.
|
|
270 |
(w > 16r7FFF) ifTrue:[
|
|
271 |
^ w - 16r10000
|
|
272 |
].
|
|
273 |
^ w
|
|
274 |
|
|
275 |
"
|
|
276 |
|b|
|
|
277 |
b := ByteArray new:4.
|
|
278 |
b signedWordAt:1 put:-1.
|
|
279 |
b wordAt:1
|
|
280 |
"
|
|
281 |
!
|
|
282 |
|
|
283 |
signedWordAt:index put:value
|
|
284 |
"set the 2-bytes starting at index from the signed Integer value."
|
|
285 |
|
|
286 |
value > 0 ifTrue:[
|
|
287 |
^ self wordAt:index put:value
|
|
288 |
].
|
|
289 |
^ self wordAt:index put:(16r10000 + value)
|
|
290 |
!
|
|
291 |
|
2
|
292 |
doubleWordAt:index
|
|
293 |
"return the 4-bytes starting at index as an (unsigned) Integer.
|
|
294 |
question: should it be signed ?"
|
|
295 |
|
|
296 |
%{ /* NOCONTEXT */
|
|
297 |
|
|
298 |
REGISTER int indx;
|
|
299 |
int nIndex;
|
|
300 |
int val;
|
|
301 |
extern OBJ _makeLarge();
|
|
302 |
|
|
303 |
if (_isSmallInteger(index)) {
|
|
304 |
indx = _intVal(index);
|
|
305 |
if (_qClass(self) != ByteArray)
|
|
306 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
307 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
308 |
if ((indx > 0) && ((indx+3) <= nIndex)) {
|
10
|
309 |
val = _ByteArrayInstPtr(self)->ba_element[indx+3-1];
|
|
310 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1];
|
|
311 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
312 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
|
313 |
if ((val >= 0) && (val <= _MAX_INT)) {
|
2
|
314 |
RETURN ( _MKSMALLINT(val) );
|
|
315 |
}
|
|
316 |
}
|
|
317 |
}
|
|
318 |
%}
|
|
319 |
.
|
|
320 |
^ ((self wordAt:index+2) * (256 *256)) + (self wordAt:index)
|
|
321 |
!
|
|
322 |
|
|
323 |
doubleWordAt:index put:value
|
|
324 |
"set the 4-bytes starting at index from the (unsigned) Integer value.
|
|
325 |
question: should it be signed ?"
|
|
326 |
|
|
327 |
|t|
|
|
328 |
|
|
329 |
%{ /* NOCONTEXT */
|
|
330 |
|
|
331 |
REGISTER int indx;
|
|
332 |
int nIndex;
|
|
333 |
int val;
|
|
334 |
extern OBJ _makeLarge();
|
|
335 |
|
|
336 |
if (_isSmallInteger(index) && _isSmallInteger(value)) {
|
10
|
337 |
val = _intVal(value);
|
|
338 |
if (val >= 0) {
|
2
|
339 |
indx = _intVal(index);
|
|
340 |
if (_qClass(self) != ByteArray)
|
|
341 |
indx += _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
342 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
343 |
if ((indx > 0) && ((indx+3) <= nIndex)) {
|
10
|
344 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
345 |
val >>= 8;
|
|
346 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
|
|
347 |
val >>= 8;
|
|
348 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF;
|
|
349 |
val >>= 8;
|
|
350 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF;
|
2
|
351 |
RETURN ( value );
|
|
352 |
}
|
|
353 |
}
|
|
354 |
}
|
|
355 |
%}
|
|
356 |
.
|
|
357 |
(value < 0) ifTrue:[
|
10
|
358 |
^ self elementBoundsError
|
2
|
359 |
].
|
|
360 |
t := value // (256 * 256).
|
|
361 |
self wordAt:(index+2) put:t.
|
|
362 |
self wordAt:(index) put:(value - (t * 256 * 256)).
|
|
363 |
^ value
|
54
|
364 |
!
|
|
365 |
|
|
366 |
signedDoubleWordAt:index
|
|
367 |
"return the 4-bytes starting at index as a signed Integer."
|
|
368 |
|
|
369 |
|w|
|
|
370 |
|
|
371 |
"stupid: due to an STC bug, cannot currently have LargeInteger
|
|
372 |
constants - change when fixed ..."
|
|
373 |
|
|
374 |
w := self doubleWordAt:index.
|
|
375 |
(w > (16r7FFFFFF * 16r10 + 16rF)) ifTrue:[
|
|
376 |
^ w - (16r10000000 * 16r10)
|
|
377 |
].
|
|
378 |
^ w
|
|
379 |
|
|
380 |
"
|
|
381 |
w := self doubleWordAt:index.
|
|
382 |
(w > 16r7FFFFFFF) ifTrue:[
|
|
383 |
^ w - 16r100000000
|
|
384 |
].
|
|
385 |
^ w
|
|
386 |
"
|
|
387 |
|
|
388 |
"
|
|
389 |
|b|
|
|
390 |
b := ByteArray new:4.
|
|
391 |
b signedDoubleWordAt:1 put:-1.
|
|
392 |
b doubleWordAt:1
|
|
393 |
"
|
|
394 |
!
|
|
395 |
|
|
396 |
signedDoubleWordAt:index put:value
|
|
397 |
"set the 4-bytes starting at index from the signed Integer value."
|
|
398 |
|
|
399 |
value > 0 ifTrue:[
|
|
400 |
^ self doubleWordAt:index put:value
|
|
401 |
].
|
|
402 |
"stupid: due to an STC bug, cannot currently have LargeInteger
|
|
403 |
constants - change when fixed ..."
|
|
404 |
^ self doubleWordAt:index put:((16r10000000 * 16r10) + value)
|
|
405 |
"
|
|
406 |
^ self doubleWordAt:index put:(16r100000000 + value)
|
|
407 |
"
|
|
408 |
|
|
409 |
!
|
|
410 |
|
|
411 |
floatAt:index
|
|
412 |
"return the 4-bytes starting at index as a Float.
|
|
413 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
|
|
414 |
Notice also, that the bytes are expected to be in this machines
|
|
415 |
float representation - if the bytearray originated from another
|
|
416 |
machine, some conversion is usually needed."
|
|
417 |
|
|
418 |
|newFloat|
|
|
419 |
|
|
420 |
newFloat := Float basicNew.
|
|
421 |
UninterpretedBytes isBigEndian ifTrue:[
|
|
422 |
5 to:8 do:[:destIndex|
|
|
423 |
newFloat basicAt:destIndex put:(self at:index - 5 + destIndex)
|
|
424 |
].
|
|
425 |
] ifFalse:[
|
|
426 |
1 to:4 do:[:destIndex|
|
|
427 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
|
|
428 |
].
|
|
429 |
].
|
|
430 |
^ newFloat.
|
|
431 |
!
|
|
432 |
|
|
433 |
floatAt:index put:aFloat
|
|
434 |
"store the 4 bytes of value of the argument, aFloat into the receiver
|
|
435 |
starting at index.
|
|
436 |
Notice, that (currently) ST/X Floats are what DOubles are in ST-80.
|
|
437 |
Notice also, that the bytes are expected to be in this machines
|
|
438 |
float representation - if the bytearray originated from another
|
|
439 |
machine, some conversion is usually needed."
|
|
440 |
|
|
441 |
UninterpretedBytes isBigEndian ifTrue:[
|
|
442 |
5 to:8 do:[:srcIndex|
|
|
443 |
self at:index - 5 + srcIndex put:(aFloat basicAt:srcIndex)
|
|
444 |
].
|
|
445 |
] ifFalse:[
|
|
446 |
1 to:4 do:[:srcIndex|
|
|
447 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex)
|
|
448 |
].
|
|
449 |
].
|
|
450 |
^ aFloat
|
|
451 |
!
|
|
452 |
|
|
453 |
doubleAt:index
|
|
454 |
"return the 8-bytes starting at index as a Float.
|
|
455 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
|
|
456 |
Notice also, that the bytes are expected to be in this machines
|
|
457 |
float representation - if the bytearray originated from another
|
|
458 |
machine, some conversion is usually needed."
|
|
459 |
|
|
460 |
|newFloat|
|
|
461 |
|
|
462 |
newFloat := Float basicNew.
|
|
463 |
1 to:8 do:[:destIndex|
|
|
464 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
|
|
465 |
].
|
|
466 |
^ newFloat.
|
|
467 |
!
|
|
468 |
|
|
469 |
doubleAt:index put:aFloat
|
|
470 |
"store the value of the argument, aFloat into the receiver
|
|
471 |
starting at index.
|
|
472 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
|
|
473 |
Notice also, that the bytes are expected to be in this machines
|
|
474 |
float representation - if the bytearray originated from another
|
|
475 |
machine, some conversion is usually needed."
|
|
476 |
|
|
477 |
1 to:8 do:[:srcIndex|
|
|
478 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex)
|
|
479 |
].
|
|
480 |
^ aFloat
|
1
|
481 |
! !
|
|
482 |
|
12
|
483 |
!ByteArray methodsFor:'converting'!
|
|
484 |
|
|
485 |
asByteArray
|
|
486 |
"return the receiver as a byteArray"
|
|
487 |
|
|
488 |
"could be an instance of a subclass..."
|
|
489 |
self class == ByteArray ifTrue:[
|
|
490 |
^ self
|
|
491 |
].
|
|
492 |
^ super asByteArray
|
|
493 |
! !
|
|
494 |
|
|
495 |
!ByteArray methodsFor:'printing & storing'!
|
|
496 |
|
|
497 |
isLiteral
|
37
|
498 |
"return true, if the receiver can be used as a literal
|
|
499 |
(i.e. can be used in constant arrays)"
|
12
|
500 |
|
|
501 |
^ true
|
|
502 |
!
|
|
503 |
|
|
504 |
storeOn:aStream
|
54
|
505 |
"append a printed representation from which the receiver can be
|
|
506 |
reconstructed to aStream. (reimplemented to make it look better)"
|
|
507 |
|
|
508 |
|first|
|
|
509 |
|
|
510 |
self class == ByteArray ifTrue:[
|
|
511 |
aStream nextPutAll:'#['.
|
|
512 |
first := true.
|
|
513 |
self do:[:byte |
|
|
514 |
first ifFalse:[aStream space]
|
|
515 |
ifTrue:[first := false].
|
|
516 |
byte storeOn:aStream.
|
|
517 |
].
|
|
518 |
aStream nextPutAll:']'.
|
|
519 |
^ self
|
|
520 |
].
|
|
521 |
^ super storeOn:aStream
|
12
|
522 |
! !
|
|
523 |
|
1
|
524 |
!ByteArray methodsFor:'queries'!
|
|
525 |
|
|
526 |
indexOf:aByte startingAt:start
|
|
527 |
"return the index of the first occurrence of the argument, aByte
|
|
528 |
in the receiver starting at start, anInteger; return 0 if not found.
|
|
529 |
- reimplemented here for speed"
|
|
530 |
|
|
531 |
%{ /* NOCONTEXT */
|
|
532 |
|
|
533 |
REGISTER unsigned char *cp;
|
|
534 |
REGISTER int index, byteValue;
|
|
535 |
REGISTER int len;
|
|
536 |
|
|
537 |
if (! _isSmallInteger(aByte)) {
|
|
538 |
RETURN ( _MKSMALLINT(0) );
|
|
539 |
}
|
|
540 |
|
|
541 |
byteValue = _intVal(aByte);
|
|
542 |
|
|
543 |
if ((byteValue < 0) || (byteValue > 255)) {
|
|
544 |
RETURN ( _MKSMALLINT(0) );
|
|
545 |
}
|
|
546 |
|
2
|
547 |
if (_isSmallInteger(start)) {
|
1
|
548 |
index = _intVal(start);
|
|
549 |
len = _qSize(self) - OHDR_SIZE;
|
|
550 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
2
|
551 |
if (_qClass(self) != ByteArray) {
|
10
|
552 |
int nInst;
|
2
|
553 |
|
|
554 |
nInst = _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
555 |
cp += nInst;
|
10
|
556 |
len -= nInst;
|
|
557 |
}
|
1
|
558 |
cp += index - 1;
|
|
559 |
while (index <= len) {
|
|
560 |
if (*cp == byteValue) {
|
|
561 |
RETURN ( _MKSMALLINT(index) );
|
|
562 |
}
|
|
563 |
index++;
|
|
564 |
cp++;
|
|
565 |
}
|
|
566 |
RETURN ( _MKSMALLINT(0) );
|
|
567 |
}
|
|
568 |
%}
|
|
569 |
.
|
|
570 |
^ super indexOf:aByte startingAt:start
|
|
571 |
!
|
|
572 |
|
|
573 |
usedValues
|
|
574 |
"return a new ByteArray with all used values (actually a kind of Set);
|
|
575 |
needed specially for Image class."
|
|
576 |
|
|
577 |
|result l|
|
12
|
578 |
|
|
579 |
%{ /* STACK: 400 */
|
|
580 |
|
1
|
581 |
REGISTER unsigned char *cp;
|
|
582 |
REGISTER int len;
|
|
583 |
unsigned char flags[256];
|
|
584 |
static struct inlineCache nw = _ILC1;
|
|
585 |
extern OBJ ByteArray, _new_;
|
|
586 |
|
|
587 |
if (_qClass(self) == ByteArray) {
|
|
588 |
memset(flags, 0, sizeof(flags));
|
|
589 |
len = _qSize(self) - OHDR_SIZE;
|
|
590 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
591 |
|
|
592 |
/* for each used byte, set flag */
|
|
593 |
while (len > 0) {
|
|
594 |
flags[*cp] = 1;
|
|
595 |
cp++;
|
|
596 |
len--;
|
|
597 |
}
|
|
598 |
/* count 1's */
|
|
599 |
len = 0;
|
2
|
600 |
for (cp=flags+255; cp >= flags; cp--)
|
1
|
601 |
if (*cp) len++;
|
|
602 |
|
|
603 |
/* create ByteArray of used values */
|
|
604 |
result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, _MKSMALLINT(len));
|
|
605 |
if (_Class(result) == ByteArray) {
|
|
606 |
cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
|
|
607 |
for (len=0; len < 256; len++) {
|
|
608 |
if (flags[len])
|
|
609 |
*cp++ = len;
|
|
610 |
}
|
|
611 |
}
|
|
612 |
RETURN ( result );
|
|
613 |
}
|
|
614 |
%}
|
|
615 |
.
|
54
|
616 |
^ self asSet
|
1
|
617 |
!
|
|
618 |
|
|
619 |
usageCounts
|
|
620 |
"return an array filled with value-counts -
|
|
621 |
added for Image handling"
|
|
622 |
|
|
623 |
|counts|
|
|
624 |
|
|
625 |
counts := Array new:256.
|
12
|
626 |
|
|
627 |
%{ /* STACK: 2000 */
|
|
628 |
|
1
|
629 |
REGISTER unsigned char *cp;
|
|
630 |
REGISTER int nByte;
|
|
631 |
REGISTER int index;
|
|
632 |
int icounts[256];
|
|
633 |
|
54
|
634 |
if ((_qClass(self) == ByteArray) && __isArray(counts)) {
|
1
|
635 |
/*
|
|
636 |
* zero counts
|
|
637 |
*/
|
|
638 |
for (index=0; index<256; index++) {
|
|
639 |
icounts[index] = 0;
|
|
640 |
}
|
|
641 |
|
|
642 |
/*
|
|
643 |
* count
|
|
644 |
*/
|
|
645 |
nByte = _qSize(self) - OHDR_SIZE;
|
|
646 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
647 |
while (nByte--) {
|
|
648 |
icounts[*cp++]++;
|
|
649 |
}
|
|
650 |
|
|
651 |
/*
|
|
652 |
* make it real counts
|
|
653 |
*/
|
|
654 |
for (index=0; index<256; index++) {
|
|
655 |
_ArrayInstPtr(counts)->a_element[index] = _MKSMALLINT(icounts[index]);
|
|
656 |
}
|
|
657 |
RETURN ( counts );
|
|
658 |
}
|
|
659 |
%}
|
|
660 |
.
|
|
661 |
self primitiveFailed
|
|
662 |
!
|
|
663 |
|
|
664 |
maximumValue
|
|
665 |
"return the maximum value in the receiver -
|
|
666 |
added for sound-player (which needs a fast method for this)"
|
|
667 |
|
|
668 |
%{ /* NOCONTEXT */
|
|
669 |
|
|
670 |
REGISTER unsigned char *cp;
|
|
671 |
REGISTER int index, max;
|
|
672 |
int len;
|
|
673 |
|
|
674 |
if (_qClass(self) == ByteArray) {
|
|
675 |
max = 0;
|
|
676 |
index = 0;
|
|
677 |
len = _qSize(self) - OHDR_SIZE;
|
|
678 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
679 |
while (++index <= len) {
|
|
680 |
if (*cp > max) max = *cp;
|
|
681 |
cp++;
|
|
682 |
}
|
|
683 |
RETURN ( _MKSMALLINT(max) );
|
|
684 |
}
|
|
685 |
%}
|
|
686 |
.
|
|
687 |
self primitiveFailed
|
|
688 |
! !
|
|
689 |
|
|
690 |
!ByteArray methodsFor:'filling and replacing'!
|
|
691 |
|
|
692 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
693 |
"reimplemented for speed"
|
|
694 |
|
54
|
695 |
%{ /* NOCONTEXT */
|
1
|
696 |
|
54
|
697 |
int nIndex, repNIndex;
|
|
698 |
int startIndex, stopIndex;
|
|
699 |
REGISTER unsigned char *src;
|
|
700 |
REGISTER int repStartIndex;
|
|
701 |
int repStopIndex, count;
|
|
702 |
REGISTER unsigned char *dst;
|
1
|
703 |
|
54
|
704 |
if ((_qClass(self) == ByteArray)
|
|
705 |
&& (_Class(aCollection) == ByteArray)
|
|
706 |
&& _isSmallInteger(start)
|
|
707 |
&& _isSmallInteger(stop)
|
|
708 |
&& _isSmallInteger(repStart)) {
|
|
709 |
startIndex = _intVal(start) - 1;
|
|
710 |
if (startIndex >= 0) {
|
|
711 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
712 |
stopIndex = _intVal(stop) - 1;
|
|
713 |
count = stopIndex - startIndex + 1;
|
|
714 |
if (count == 0) {
|
|
715 |
RETURN ( self );
|
|
716 |
}
|
|
717 |
if ((count > 0) && (stopIndex < nIndex)) {
|
|
718 |
repStartIndex = _intVal(repStart) - 1;
|
|
719 |
if (repStartIndex >= 0) {
|
|
720 |
repNIndex = _qSize(aCollection) - OHDR_SIZE;
|
|
721 |
repStopIndex = repStartIndex + (stopIndex - startIndex);
|
|
722 |
if (repStopIndex < repNIndex) {
|
|
723 |
src = &(_ByteArrayInstPtr(aCollection)->ba_element[repStartIndex]);
|
|
724 |
dst = &(_ByteArrayInstPtr(self)->ba_element[startIndex]);
|
1
|
725 |
|
54
|
726 |
if (aCollection == self) {
|
|
727 |
/* take care of overlapping copy */
|
|
728 |
if (src < dst) {
|
|
729 |
/* must do a reverse copy */
|
|
730 |
src += count;
|
|
731 |
dst += count;
|
|
732 |
while (count-- > 0) {
|
|
733 |
*--dst = *--src;
|
1
|
734 |
}
|
54
|
735 |
RETURN ( self );
|
1
|
736 |
}
|
54
|
737 |
}
|
1
|
738 |
#ifdef FAST_MEMCPY
|
54
|
739 |
bcopy(src, dst, count);
|
1
|
740 |
#else
|
54
|
741 |
while (count-- > 0) {
|
|
742 |
*dst++ = *src++;
|
|
743 |
}
|
1
|
744 |
#endif
|
54
|
745 |
RETURN ( self );
|
1
|
746 |
}
|
|
747 |
}
|
54
|
748 |
}
|
1
|
749 |
}
|
54
|
750 |
}
|
1
|
751 |
%}
|
|
752 |
.
|
|
753 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
754 |
! !
|
|
755 |
|
|
756 |
!ByteArray methodsFor:'image manipulation'!
|
|
757 |
|
|
758 |
invert
|
|
759 |
"invert all bytes - used with image manipulations
|
54
|
760 |
written as a primitive for speed.
|
|
761 |
Q: is this really needed ?"
|
1
|
762 |
|
|
763 |
%{ /* NOCONTEXT */
|
|
764 |
|
|
765 |
REGISTER unsigned char *dst;
|
|
766 |
REGISTER unsigned long *ldst;
|
|
767 |
REGISTER int cnt;
|
|
768 |
|
|
769 |
if (_qClass(self) == ByteArray) {
|
|
770 |
cnt = _qSize(self) - OHDR_SIZE;
|
|
771 |
dst = _ByteArrayInstPtr(self)->ba_element;
|
|
772 |
if (! ((int)dst & (sizeof(long)-1))) {
|
|
773 |
ldst = (unsigned long *)dst;
|
|
774 |
while (cnt >= sizeof(long)) {
|
|
775 |
*ldst = ~(*ldst);
|
|
776 |
ldst++;
|
|
777 |
cnt -= sizeof(long);
|
|
778 |
}
|
|
779 |
dst = (unsigned char *)ldst;
|
|
780 |
}
|
|
781 |
while (cnt--) {
|
|
782 |
*dst = ~(*dst);
|
|
783 |
dst++;
|
|
784 |
}
|
|
785 |
RETURN ( self );
|
|
786 |
}
|
|
787 |
%}
|
|
788 |
.
|
|
789 |
self primitiveFailed
|
|
790 |
!
|
|
791 |
|
12
|
792 |
reverse
|
|
793 |
"reverse order of elements inplace -
|
|
794 |
written as a primitive for speed on image manipulations"
|
|
795 |
|
|
796 |
%{ /* NOCONTEXT */
|
|
797 |
|
|
798 |
REGISTER unsigned char *p1, *p2;
|
|
799 |
REGISTER int cnt;
|
|
800 |
REGISTER unsigned t;
|
|
801 |
|
|
802 |
if (_qClass(self) == ByteArray) {
|
|
803 |
cnt = _qSize(self) - OHDR_SIZE;
|
|
804 |
p1 = _ByteArrayInstPtr(self)->ba_element;
|
|
805 |
p2 = _ByteArrayInstPtr(self)->ba_element + cnt - 1;
|
|
806 |
while (cnt > 0) {
|
|
807 |
t = *p1;
|
|
808 |
*p1++ = *p2;
|
|
809 |
*p2-- = t;
|
|
810 |
cnt-=2;
|
|
811 |
}
|
|
812 |
RETURN ( self );
|
|
813 |
}
|
|
814 |
%}
|
|
815 |
.
|
|
816 |
^ super reverse
|
|
817 |
!
|
|
818 |
|
1
|
819 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray
|
|
820 |
mapping:aMapByteArray
|
54
|
821 |
|
1
|
822 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into
|
|
823 |
aByteArray with 8-bit pixels. The width/height-arguments are needed
|
|
824 |
to skip any padded src-bits. On the fly, the destination pixels
|
54
|
825 |
are translated using aMapByteArray if non-nil.
|
1
|
826 |
- used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color
|
|
827 |
machines"
|
|
828 |
|
|
829 |
%{ /* NOCONTEXT */
|
|
830 |
|
54
|
831 |
REGISTER unsigned char *src, *dst;
|
|
832 |
REGISTER int wrun;
|
|
833 |
unsigned char *srcNext;
|
|
834 |
int bytesPerRow, mask, shift0, shift;
|
|
835 |
int w, h, hrun;
|
|
836 |
int srcBytes, dstBytes;
|
|
837 |
int bitsPerPixel;
|
|
838 |
int bits;
|
|
839 |
int ncells;
|
|
840 |
unsigned char *map;
|
1
|
841 |
|
54
|
842 |
if ((_qClass(self) == ByteArray)
|
|
843 |
&& (_qClass(aByteArray) == ByteArray)
|
|
844 |
&& _isSmallInteger(nBitsPerPixel)
|
|
845 |
&& _isSmallInteger(height)
|
|
846 |
&& _isSmallInteger(width)) {
|
|
847 |
if ((aMapByteArray != nil)
|
|
848 |
&& (_Class(aMapByteArray) == ByteArray)) {
|
|
849 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
|
|
850 |
} else {
|
|
851 |
map = (unsigned char *)0;
|
|
852 |
}
|
1
|
853 |
|
54
|
854 |
bitsPerPixel = _intVal(nBitsPerPixel);
|
|
855 |
w = _intVal(width);
|
|
856 |
h = _intVal(height);
|
|
857 |
src = _ByteArrayInstPtr(self)->ba_element;
|
|
858 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element;
|
|
859 |
switch (bitsPerPixel) {
|
|
860 |
case 1:
|
|
861 |
mask = 0x01;
|
|
862 |
break;
|
|
863 |
case 2:
|
|
864 |
mask = 0x03;
|
|
865 |
break;
|
|
866 |
case 4:
|
|
867 |
mask = 0x0F;
|
|
868 |
break;
|
|
869 |
case 8:
|
|
870 |
mask = 0xFF;
|
|
871 |
break;
|
|
872 |
default:
|
|
873 |
goto fail;
|
|
874 |
}
|
|
875 |
ncells = mask + 1;
|
|
876 |
if (map) {
|
|
877 |
if ((_qSize(aMapByteArray) - OHDR_SIZE) < ncells)
|
|
878 |
goto fail;
|
|
879 |
}
|
1
|
880 |
|
54
|
881 |
bytesPerRow = (w * bitsPerPixel + 7) / 8;
|
|
882 |
shift0 = 8 - bitsPerPixel;
|
|
883 |
srcBytes = bytesPerRow * h;
|
|
884 |
dstBytes = w * h;
|
1
|
885 |
|
54
|
886 |
if (((_qSize(self) - OHDR_SIZE) >= srcBytes)
|
|
887 |
&& ((_qSize(aByteArray) - OHDR_SIZE) >= dstBytes)) {
|
|
888 |
for (hrun=h; hrun; hrun--) {
|
|
889 |
srcNext = src + bytesPerRow;
|
|
890 |
shift = shift0;
|
|
891 |
if (map) {
|
|
892 |
for (wrun=w; wrun; wrun--) {
|
|
893 |
if (shift == shift0) {
|
|
894 |
bits = *src++;
|
1
|
895 |
}
|
54
|
896 |
*dst++ = map[(bits >> shift) & mask];
|
|
897 |
shift -= bitsPerPixel;
|
|
898 |
if (shift < 0) {
|
|
899 |
shift = shift0;
|
1
|
900 |
}
|
|
901 |
}
|
54
|
902 |
} else {
|
|
903 |
for (wrun=w; wrun; wrun--) {
|
|
904 |
if (shift == shift0) {
|
|
905 |
bits = *src++;
|
|
906 |
}
|
|
907 |
*dst++ = (bits >> shift) & mask;
|
|
908 |
shift -= bitsPerPixel;
|
|
909 |
if (shift < 0) {
|
|
910 |
shift = shift0;
|
|
911 |
}
|
|
912 |
}
|
1
|
913 |
}
|
54
|
914 |
src = srcNext;
|
1
|
915 |
}
|
54
|
916 |
RETURN ( self );
|
1
|
917 |
}
|
|
918 |
}
|
|
919 |
fail: ;
|
|
920 |
%}
|
|
921 |
.
|
|
922 |
self primitiveFailed
|
|
923 |
! !
|