author | Claus Gittinger <cg@exept.de> |
Thu, 04 Jan 1996 02:24:09 +0100 | |
changeset 829 | fc386319f41c |
parent 788 | e80f1c42b87b |
child 834 | c68ed1088b42 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 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 |
550 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Arrayed' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!ByteArray class methodsFor:'documentation'! |
21 |
||
563 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
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 |
! |
|
35 |
||
88 | 36 |
documentation |
37 |
" |
|
38 |
ByteArrays store integers in the range 0..255. |
|
129 | 39 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 40 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 41 |
be used to hold the data for bitmaps, images and other bulk data. |
42 |
ByteArrays are also used to store the bytecode-instructions of an |
|
43 |
interpreted method and are used as superclass for Strings. |
|
44 |
||
45 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
46 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
47 |
as elements in a constant array. |
|
48 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
49 |
||
50 |
If you have to communicate structure-data (in the C-sense) with external |
|
51 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
52 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
53 |
and store C-structure fields. |
|
88 | 54 |
" |
2 | 55 |
! ! |
56 |
||
1 | 57 |
!ByteArray class methodsFor:'instance creation'! |
58 |
||
563 | 59 |
fromPackedString:aString |
60 |
"ST-80 compatibility: decode a byteArray from a packed string in which |
|
61 |
6bits are encoded per character. The argument, aString must be a multiple |
|
62 |
of 4 in size (since 24 is the lcm of 6 and 8). This is somewhat like |
|
63 |
the radix-encoding used in good old PDP11 times ;-) |
|
64 |
ST-80 uses this encoding for Images ... |
|
65 |
PS: It took a while to figure that one out ... I dont like it ;-)" |
|
66 |
||
67 |
|index "{ Class: SmallInteger }" |
|
68 |
dstIndex "{ Class: SmallInteger }" |
|
69 |
stop "{ Class: SmallInteger }" |
|
70 |
n "{ Class: SmallInteger }" |
|
71 |
sz "{ Class: SmallInteger }" |
|
72 |
lastCharacter bytes| |
|
73 |
||
74 |
sz := aString size. |
|
75 |
sz == 0 ifTrue:[^ self new]. |
|
76 |
stop := sz // 4 * 3. |
|
77 |
"the size modulu 3 is encoded in the last character, if its in the |
|
78 |
range 97 .. otherwise, its exact." |
|
79 |
||
80 |
lastCharacter := aString last. |
|
81 |
lastCharacter asciiValue > 96 ifTrue:[ |
|
82 |
stop := stop - 3 + lastCharacter asciiValue - 96 |
|
83 |
]. |
|
84 |
bytes := self new:stop. |
|
85 |
index := 1. dstIndex := 1. |
|
86 |
[dstIndex <= stop] whileTrue:[ |
|
87 |
"take 4 characters ..." |
|
88 |
n := (aString at:index) asciiValue - 32. |
|
89 |
n := (n bitShift:6) + ((aString at:index+1) asciiValue - 32). |
|
90 |
n := (n bitShift:6) + ((aString at:index+2) asciiValue - 32). |
|
91 |
n := (n bitShift:6) + ((aString at:index+3) asciiValue - 32). |
|
92 |
n := n bitXor:16r820820. |
|
93 |
index := index + 4. |
|
94 |
bytes at:dstIndex put:(n bitShift:-16). |
|
95 |
dstIndex < stop ifTrue:[ |
|
96 |
bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF). |
|
97 |
dstIndex+2 <= stop ifTrue:[ |
|
98 |
bytes at:dstIndex+2 put:(n bitAnd:16rFF). |
|
99 |
] |
|
100 |
]. |
|
101 |
dstIndex := dstIndex + 3. |
|
102 |
]. |
|
103 |
^ bytes |
|
104 |
||
105 |
" |
|
106 |
ByteArray fromPackedString:(#[1 1 1 1] asPackedString) |
|
107 |
ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString) |
|
108 |
ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString) |
|
109 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString) |
|
110 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString) |
|
111 |
||
112 |
" |
|
113 |
! |
|
114 |
||
1 | 115 |
uninitializedNew:anInteger |
116 |
"return a new instance of the receiver with uninitialized |
|
117 |
(i.e. undefined) contents. The indexed elements have any random |
|
75 | 118 |
value. However, any named instance variables are still nilled. |
119 |
For use, when contents will be set anyway shortly after - this |
|
120 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
121 |
Of course, it only makes a difference for very big ByteArrays, such |
|
122 |
as used for images/bitmaps. |
|
123 |
||
124 |
Notice: if you want to port code using uninitializedNew: to another |
|
125 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
|
126 |
method to the ByteArray class of the other smalltalk." |
|
1 | 127 |
|
128 |
%{ /* NOCONTEXT */ |
|
129 |
OBJ newobj; |
|
130 |
INT instsize, nInstVars, nindexedinstvars; |
|
131 |
REGISTER OBJ *op; |
|
132 |
||
249 | 133 |
if (__isSmallInteger(anInteger)) { |
155 | 134 |
nindexedinstvars = _intVal(anInteger); |
135 |
if (nindexedinstvars >= 0) { |
|
249 | 136 |
if (self == ByteArray) { |
137 |
/* |
|
138 |
* the most common case |
|
139 |
*/ |
|
140 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
141 |
if (_CanDoQuickNew(instsize)) { |
|
142 |
_qCheckedNew(newobj, instsize); |
|
143 |
_InstPtr(newobj)->o_class = self; |
|
144 |
RETURN (newobj ); |
|
155 | 145 |
} |
249 | 146 |
} |
147 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars); |
|
148 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
149 |
PROTECT_CONTEXT |
|
150 |
_qNew(newobj, instsize, SENDER); |
|
151 |
UNPROTECT_CONTEXT |
|
281 | 152 |
if (newobj != nil) { |
249 | 153 |
_InstPtr(newobj)->o_class = self; |
367 | 154 |
__qSTORE(newobj, self); |
249 | 155 |
if (nInstVars) { |
156 |
/* |
|
157 |
* still have to nil out named instvars ... |
|
158 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
159 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
249 | 160 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 161 |
#else |
249 | 162 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
163 |
/* |
|
164 |
* knowing that nil is 0 |
|
165 |
*/ |
|
166 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
167 |
# else |
|
168 |
op = _InstPtr(newobj)->i_instvars; |
|
169 |
while (nInstVars--) |
|
170 |
*op++ = nil; |
|
171 |
# endif |
|
1 | 172 |
#endif |
155 | 173 |
} |
249 | 174 |
RETURN ( newobj ); |
155 | 175 |
} |
176 |
} |
|
1 | 177 |
} |
178 |
%} |
|
179 |
. |
|
12 | 180 |
(anInteger isMemberOf:SmallInteger) ifTrue:[ |
155 | 181 |
(anInteger < 0) ifTrue:[ |
182 |
^ self error:'bad (negative) argument to new' |
|
183 |
]. |
|
184 |
" |
|
185 |
memory allocation failed. |
|
186 |
When we arrive here, there was no memory, even after |
|
187 |
a garbage collect. This means, that the VM wanted to |
|
188 |
get some more memory from the Operatingsystem, which |
|
189 |
was not kind enough to give some. |
|
190 |
" |
|
191 |
^ ObjectMemory allocationFailureSignal raise. |
|
12 | 192 |
]. |
1 | 193 |
^ self basicNew:anInteger |
194 |
! ! |
|
195 |
||
550 | 196 |
!ByteArray class methodsFor:'binary storage'! |
1 | 197 |
|
550 | 198 |
binaryDefinitionFrom: stream manager: manager |
199 |
"get a ByteArray from the binary stream. |
|
200 |
ByteArrays are stored as 4-byte size, followed by the bytes" |
|
1 | 201 |
|
550 | 202 |
"take care of subclasses ..." |
203 |
self == ByteArray ifTrue:[ |
|
204 |
^ (stream next: (stream nextNumber: 4)) |
|
205 |
]. |
|
206 |
^ super binaryDefinitionFrom: stream manager: manager |
|
207 |
! ! |
|
208 |
||
209 |
!ByteArray class methodsFor:'queries'! |
|
210 |
||
211 |
isBuiltInClass |
|
212 |
"this class is known by the run-time-system" |
|
213 |
||
214 |
^ self == ByteArray |
|
215 |
! ! |
|
216 |
||
217 |
!ByteArray methodsFor:'accessing'! |
|
1 | 218 |
|
563 | 219 |
basicAt:index |
220 |
"return the indexed instance variable with index, anInteger |
|
221 |
- redefined here to be slighly faster than the default in Object" |
|
222 |
||
223 |
%{ /* NOCONTEXT */ |
|
224 |
||
225 |
REGISTER int indx; |
|
226 |
int nIndex; |
|
227 |
OBJ cls; |
|
228 |
||
229 |
if (__isSmallInteger(index)) { |
|
230 |
indx = _intVal(index); |
|
231 |
if (indx > 0) { |
|
232 |
if ((cls = __qClass(self)) != ByteArray) |
|
233 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
234 |
nIndex = __byteArraySize(self); |
|
235 |
if (indx <= nIndex) { |
|
236 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) ); |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
%} |
|
241 |
. |
|
242 |
^ super basicAt:index |
|
243 |
! |
|
244 |
||
1 | 245 |
basicAt:index put:value |
246 |
"set the indexed instance variable with index, anInteger to value |
|
2 | 247 |
- redefined here to be slighly faster than the default in Object" |
1 | 248 |
|
249 |
%{ /* NOCONTEXT */ |
|
250 |
||
251 |
REGISTER int indx; |
|
252 |
int nIndex; |
|
253 |
int val; |
|
54 | 254 |
OBJ cls; |
1 | 255 |
|
249 | 256 |
if (__bothSmallInteger(index, value)) { |
155 | 257 |
val = _intVal(value); |
216 | 258 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
155 | 259 |
indx = _intVal(index); |
249 | 260 |
if (indx > 0) { |
328 | 261 |
if ((cls = __qClass(self)) != ByteArray) |
249 | 262 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
263 |
nIndex = __byteArraySize(self); |
|
264 |
if (indx <= nIndex) { |
|
265 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val; |
|
266 |
RETURN ( value ); |
|
267 |
} |
|
155 | 268 |
} |
269 |
} |
|
1 | 270 |
} |
271 |
%} |
|
272 |
. |
|
273 |
^ super basicAt:index put:value |
|
2 | 274 |
! |
275 |
||
54 | 276 |
byteAt:index |
249 | 277 |
"return the byte at index. |
278 |
For ByteArray, this is the same as basicAt:; |
|
279 |
however, for strings or symbols, this returns a numeric byteValue |
|
280 |
instead of a character." |
|
281 |
||
282 |
%{ /* NOCONTEXT */ |
|
283 |
||
284 |
REGISTER int indx; |
|
285 |
int nIndex; |
|
286 |
OBJ cls; |
|
54 | 287 |
|
249 | 288 |
if (__isSmallInteger(index)) { |
289 |
indx = _intVal(index); |
|
290 |
if (indx > 0) { |
|
328 | 291 |
if ((cls = __qClass(self)) != ByteArray) |
249 | 292 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
293 |
nIndex = __byteArraySize(self); |
|
294 |
if (indx <= nIndex) { |
|
295 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) ); |
|
296 |
} |
|
297 |
} |
|
298 |
} |
|
299 |
%} |
|
300 |
. |
|
301 |
^ (super basicAt:index) asInteger |
|
54 | 302 |
! |
303 |
||
249 | 304 |
byteAt:index put:value |
305 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
306 |
However, for Strings, this expects a byteValue to be stored." |
|
307 |
||
308 |
%{ /* NOCONTEXT */ |
|
309 |
||
310 |
REGISTER int indx; |
|
311 |
int nIndex; |
|
312 |
int val; |
|
313 |
OBJ cls; |
|
54 | 314 |
|
249 | 315 |
if (__bothSmallInteger(index, value)) { |
316 |
val = _intVal(value); |
|
317 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
|
318 |
indx = _intVal(index); |
|
319 |
if (indx > 0) { |
|
328 | 320 |
if ((cls = __qClass(self)) != ByteArray) |
249 | 321 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
322 |
nIndex = __byteArraySize(self); |
|
323 |
if (indx <= nIndex) { |
|
324 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val; |
|
325 |
RETURN ( value ); |
|
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
%} |
|
331 |
. |
|
332 |
^ super basicAt:index put:value |
|
54 | 333 |
! |
334 |
||
563 | 335 |
doubleAt:index |
336 |
"return the 8-bytes starting at index as a Float. |
|
337 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
|
338 |
Notice also, that the bytes are expected to be in this machines |
|
339 |
float representation - if the bytearray originated from another |
|
340 |
machine, some conversion is usually needed." |
|
2 | 341 |
|
563 | 342 |
|newFloat| |
359 | 343 |
|
563 | 344 |
newFloat := Float basicNew. |
345 |
1 to:8 do:[:destIndex| |
|
346 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex) |
|
359 | 347 |
]. |
563 | 348 |
^ newFloat. |
359 | 349 |
! |
350 |
||
563 | 351 |
doubleAt:index put:aFloat |
352 |
"store the value of the argument, aFloat into the receiver |
|
353 |
starting at index. |
|
354 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
|
355 |
Notice also, that the bytes are expected to be in this machines |
|
356 |
float representation - if the bytearray originated from another |
|
357 |
machine, some conversion is usually needed." |
|
54 | 358 |
|
563 | 359 |
1 to:8 do:[:srcIndex| |
360 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex) |
|
359 | 361 |
]. |
563 | 362 |
^ aFloat |
54 | 363 |
! |
364 |
||
2 | 365 |
doubleWordAt:index |
366 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
359 | 367 |
The value is retrieved in the machines natural byte order. |
368 |
Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)" |
|
369 |
||
370 |
%{ /* NOCONTEXT */ |
|
371 |
||
372 |
REGISTER int indx; |
|
373 |
int nIndex; |
|
374 |
union { |
|
375 |
char u_char[4]; |
|
376 |
unsigned u_uint; |
|
377 |
} val; |
|
378 |
OBJ cls; |
|
379 |
||
380 |
if (__isSmallInteger(index)) { |
|
381 |
indx = _intVal(index); |
|
382 |
if (indx > 0) { |
|
383 |
if ((cls = __qClass(self)) != ByteArray) |
|
384 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
385 |
nIndex = __byteArraySize(self); |
|
386 |
if ((indx+3) <= nIndex) { |
|
387 |
val.u_char[0] = _ByteArrayInstPtr(self)->ba_element[indx+0-1]; |
|
388 |
val.u_char[1] = _ByteArrayInstPtr(self)->ba_element[indx+1-1]; |
|
389 |
val.u_char[2] = _ByteArrayInstPtr(self)->ba_element[indx+2-1]; |
|
390 |
val.u_char[3] = _ByteArrayInstPtr(self)->ba_element[indx+3-1]; |
|
391 |
if ((val.u_uint >= 0) && (val.u_uint <= _MAX_INT)) { |
|
392 |
RETURN ( _MKSMALLINT(val.u_uint) ); |
|
393 |
} |
|
394 |
RETURN ( _MKULARGEINT(val.u_uint) ); |
|
395 |
} |
|
396 |
} |
|
397 |
} |
|
398 |
%}. |
|
399 |
^ SubscriptOutOfBoundsSignal raise. |
|
400 |
||
401 |
" |
|
402 |
|b| |
|
403 |
||
404 |
b := ByteArray withAll:#(1 2 3 4). |
|
405 |
(b doubleWordAt:1) printStringRadix:16 |
|
406 |
" |
|
407 |
! |
|
408 |
||
409 |
doubleWordAt:index MSB:msb |
|
410 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
411 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
412 |
LSB-first otherwise. |
|
413 |
Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)" |
|
2 | 414 |
|
415 |
%{ /* NOCONTEXT */ |
|
416 |
||
417 |
REGISTER int indx; |
|
418 |
int nIndex; |
|
419 |
int val; |
|
249 | 420 |
OBJ cls; |
2 | 421 |
|
249 | 422 |
if (__isSmallInteger(index)) { |
155 | 423 |
indx = _intVal(index); |
249 | 424 |
if (indx > 0) { |
328 | 425 |
if ((cls = __qClass(self)) != ByteArray) |
249 | 426 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
427 |
nIndex = __byteArraySize(self); |
|
428 |
if ((indx+3) <= nIndex) { |
|
359 | 429 |
if (msb == true) { |
430 |
val = _ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
431 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1]; |
|
432 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1]; |
|
433 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+3-1]; |
|
434 |
} else { |
|
435 |
val = _ByteArrayInstPtr(self)->ba_element[indx+3-1]; |
|
436 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1]; |
|
437 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1]; |
|
438 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
439 |
} |
|
249 | 440 |
if ((val >= 0) && (val <= _MAX_INT)) { |
441 |
RETURN ( _MKSMALLINT(val) ); |
|
442 |
} |
|
443 |
RETURN ( _MKULARGEINT(val) ); |
|
155 | 444 |
} |
445 |
} |
|
2 | 446 |
} |
359 | 447 |
%}. |
448 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 449 |
|
450 |
" |
|
451 |
|b| |
|
452 |
||
453 |
b := ByteArray withAll:#(1 2 3 4). |
|
359 | 454 |
(b doubleWordAt:1 MSB:true) printStringRadix:16. |
455 |
(b doubleWordAt:1 MSB:false) printStringRadix:16 |
|
129 | 456 |
" |
2 | 457 |
! |
458 |
||
459 |
doubleWordAt:index put:value |
|
460 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 461 |
The value should be in the range 0 to 16rFFFFFFFF |
462 |
(for negative values, the stored value is not defined). |
|
463 |
The value is stored in the machines natural byte order. |
|
464 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
465 |
||
466 |
|t| |
|
467 |
||
468 |
%{ /* NOCONTEXT */ |
|
469 |
||
470 |
REGISTER int indx; |
|
471 |
int nIndex; |
|
472 |
union { |
|
473 |
char u_char[4]; |
|
474 |
unsigned u_uint; |
|
475 |
} val; |
|
476 |
OBJ cls; |
|
477 |
||
478 |
if (__isSmallInteger(index)) { |
|
479 |
if (__isSmallInteger(value)) { |
|
480 |
val.u_uint = _intVal(value); |
|
481 |
} else { |
|
482 |
val.u_uint = __longIntVal(value); |
|
483 |
if (val.u_uint == 0) goto error; |
|
484 |
} |
|
485 |
||
486 |
indx = _intVal(index); |
|
487 |
if (indx > 0) { |
|
488 |
if ((cls = __qClass(self)) != ByteArray) |
|
489 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
490 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
491 |
if ((indx+3) <= nIndex) { |
|
492 |
_ByteArrayInstPtr(self)->ba_element[indx+0-1] = val.u_char[0]; |
|
493 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val.u_char[1]; |
|
494 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val.u_char[2]; |
|
495 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val.u_char[3]; |
|
496 |
RETURN ( value ); |
|
497 |
} |
|
498 |
} |
|
499 |
} |
|
500 |
error: ; |
|
501 |
%}. |
|
502 |
^ SubscriptOutOfBoundsSignal raise. |
|
503 |
||
504 |
" |
|
505 |
|b| |
|
506 |
b := ByteArray new:4. |
|
507 |
b doubleWordAt:1 put:16r04030201. |
|
508 |
b inspect |
|
509 |
" |
|
510 |
! |
|
511 |
||
512 |
doubleWordAt:index put:value MSB:msb |
|
513 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 514 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 515 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 516 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 517 |
|
518 |
|t| |
|
519 |
||
520 |
%{ /* NOCONTEXT */ |
|
521 |
||
522 |
REGISTER int indx; |
|
523 |
int nIndex; |
|
524 |
int val; |
|
249 | 525 |
OBJ cls; |
2 | 526 |
|
359 | 527 |
if (__isSmallInteger(index)) { |
528 |
if (__isSmallInteger(value)) { |
|
529 |
val = _intVal(value); |
|
530 |
} else { |
|
531 |
val = __longIntVal(value); |
|
532 |
if (val == 0) goto error; |
|
533 |
} |
|
534 |
indx = _intVal(index); |
|
535 |
if (indx > 0) { |
|
536 |
if ((cls = __qClass(self)) != ByteArray) |
|
537 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
538 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
539 |
if ((indx+3) <= nIndex) { |
|
540 |
if (msb == true) { |
|
541 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF; |
|
542 |
val >>= 8; |
|
543 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF; |
|
249 | 544 |
val >>= 8; |
545 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF; |
|
546 |
val >>= 8; |
|
359 | 547 |
_ByteArrayInstPtr(self)->ba_element[indx+0-1] = val & 0xFF; |
548 |
} else { |
|
549 |
_ByteArrayInstPtr(self)->ba_element[indx+0-1] = val & 0xFF; |
|
550 |
val >>= 8; |
|
551 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF; |
|
552 |
val >>= 8; |
|
249 | 553 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF; |
554 |
val >>= 8; |
|
555 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF; |
|
556 |
} |
|
359 | 557 |
RETURN ( value ); |
155 | 558 |
} |
559 |
} |
|
2 | 560 |
} |
359 | 561 |
error: ; |
562 |
%}. |
|
563 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 564 |
|
565 |
" |
|
566 |
|b| |
|
359 | 567 |
b := ByteArray new:8. |
568 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
569 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 570 |
b inspect |
571 |
" |
|
54 | 572 |
! |
573 |
||
563 | 574 |
floatAt:index |
575 |
"return the 4-bytes starting at index as a Float. |
|
576 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80; |
|
577 |
therefore this method reads a 4-byte float from the byteArray and returns |
|
578 |
a float object which keeps an 8-byte double internally. |
|
579 |
Notice also, that the bytes are expected to be in this machines |
|
580 |
float representation and order - if the bytearray originated from another |
|
581 |
machine, some conversion is usually needed." |
|
582 |
||
583 |
|newFloat| |
|
584 |
||
585 |
newFloat := Float basicNew. |
|
586 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
587 |
5 to:8 do:[:destIndex| |
|
588 |
newFloat basicAt:destIndex put:(self at:index - 5 + destIndex) |
|
589 |
]. |
|
590 |
] ifTrue:[ |
|
591 |
1 to:4 do:[:destIndex| |
|
592 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex) |
|
593 |
]. |
|
594 |
]. |
|
595 |
^ newFloat. |
|
596 |
! |
|
597 |
||
598 |
floatAt:index put:aFloat |
|
599 |
"store the 4 bytes of value of the argument, aFloat into the receiver |
|
600 |
starting at index. |
|
601 |
Notice, that (currently) ST/X Floats are what DOubles are in ST-80. |
|
602 |
Notice also, that the bytes are expected to be in this machines |
|
603 |
float representation - if the bytearray originated from another |
|
604 |
machine, some conversion is usually needed." |
|
605 |
||
606 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
607 |
5 to:8 do:[:srcIndex| |
|
608 |
self at:index - 5 + srcIndex put:(aFloat basicAt:srcIndex) |
|
609 |
]. |
|
610 |
] ifTrue:[ |
|
611 |
1 to:4 do:[:srcIndex| |
|
612 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex) |
|
613 |
]. |
|
614 |
]. |
|
615 |
^ aFloat |
|
616 |
! |
|
617 |
||
618 |
ieeDoubleAt:index |
|
619 |
"retrieve the 8 bytes starting at index as a float. |
|
620 |
The 8 bytes are assumed to be in IEE floating point single precision |
|
621 |
number format." |
|
622 |
||
623 |
" |
|
624 |
currently, we assume that the machines native number format is already |
|
625 |
IEE format - we need some more code here whenever ST/X is ported |
|
626 |
to an IBM 370 or old VAX etc. |
|
627 |
To date, all supported systems use IEE float numbers, so there should be |
|
628 |
no problem. |
|
629 |
" |
|
630 |
^ self doubleAt:index |
|
631 |
! |
|
632 |
||
633 |
ieeDoubleAt:index put:aFloat |
|
634 |
"store the value of the argument, aFloat into the receiver |
|
635 |
starting at index. Storage is in IEE floating point double precision format. |
|
636 |
(i.e. 8 bytes are stored)." |
|
637 |
||
638 |
" |
|
639 |
currently, we assume that the machines native number format is already |
|
640 |
IEE format - we need some more code here whenever ST/X is ported |
|
641 |
to an IBM 370 or old VAX etc. |
|
642 |
To date, all supported systems use IEE float numbers, so there should be |
|
643 |
no problem. |
|
644 |
" |
|
645 |
^ self doubleAt:index put:aFloat |
|
646 |
! |
|
647 |
||
648 |
ieeFloatAt:index |
|
649 |
"retrieve the 4 bytes starting at index as a float. |
|
650 |
The 4 bytes are assumed to be in IEE floating point single precision |
|
651 |
number format." |
|
652 |
||
653 |
" |
|
654 |
currently, we assume that the machines native number format is already |
|
655 |
IEE format - we need some more code here whenever ST/X is ported |
|
656 |
to an IBM 370 or old VAX etc. |
|
657 |
To date, all supported systems use IEE float numbers, so there should be |
|
658 |
no problem. |
|
659 |
" |
|
660 |
^ self floatAt:index |
|
661 |
! |
|
662 |
||
663 |
ieeFloatAt:index put:aFloat |
|
664 |
"store the value of the argument, aFloat into the receiver |
|
665 |
starting at index. Storage is in IEE floating point single precision format. |
|
666 |
(i.e. 4 bytes are stored). Since ST/X floats are really doubles, the low- |
|
667 |
order 4 bytes of the precision is lost." |
|
668 |
||
669 |
" |
|
670 |
currently, we assume that the machines native number format is already |
|
671 |
IEE format - we need some more code here whenever ST/X is ported |
|
672 |
to an IBM 370 or old VAX etc. |
|
673 |
To date, all supported systems use IEE float numbers, so there should be |
|
674 |
no problem. |
|
675 |
" |
|
676 |
^ self floatAt:index put:aFloat |
|
677 |
! |
|
678 |
||
679 |
quadWordAt:index MSB:msb |
|
680 |
"return the 8-bytes starting at index as an (unsigned) Integer. |
|
681 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
682 |
||
683 |
|l |
|
684 |
bIdx "{ Class: SmallInteger }" |
|
685 |
delta "{ Class: SmallInteger }"| |
|
686 |
||
687 |
l := LargeInteger basicNew numberOfDigits:8. |
|
688 |
msb ifTrue:[ |
|
689 |
bIdx := index + 7. |
|
690 |
delta := -1 |
|
691 |
] ifFalse:[ |
|
692 |
bIdx := index. |
|
693 |
delta := 1 |
|
694 |
]. |
|
695 |
1 to:8 do:[:i | |
|
696 |
l digitAt:i put:(self basicAt:bIdx). |
|
697 |
bIdx := bIdx + delta |
|
698 |
]. |
|
699 |
^ l normalize |
|
700 |
||
701 |
" |
|
702 |
|b| |
|
703 |
||
704 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
705 |
(b quadWordAt:1 MSB:false) printStringRadix:16 |
|
706 |
" |
|
707 |
! |
|
708 |
||
709 |
quadWordAt:index put:anInteger MSB:msb |
|
710 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
|
711 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
712 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
713 |
||
714 |
|bIdx "{ Class: SmallInteger }" |
|
715 |
delta "{ Class: SmallInteger }"| |
|
716 |
||
717 |
msb ifTrue:[ |
|
718 |
bIdx := index + 7. |
|
719 |
delta := -1 |
|
720 |
] ifFalse:[ |
|
721 |
bIdx := index. |
|
722 |
delta := 1 |
|
723 |
]. |
|
724 |
1 to:8 do:[:i | |
|
725 |
self basicAt:bIdx put:(anInteger digitAt:i). |
|
726 |
bIdx := bIdx + delta. |
|
727 |
]. |
|
728 |
^ anInteger |
|
729 |
||
730 |
" |
|
731 |
|b| |
|
732 |
b := ByteArray new:8. |
|
733 |
b quadWordAtIndex:1 put:16r0807060504030201 MSB:false. |
|
734 |
b inspect |
|
735 |
" |
|
736 |
! |
|
737 |
||
54 | 738 |
signedDoubleWordAt:index |
129 | 739 |
"return the 4-bytes starting at index as a signed Integer. |
359 | 740 |
The value is retrieved in the machines natural byte order." |
54 | 741 |
|
742 |
|w| |
|
743 |
||
129 | 744 |
" |
745 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
746 |
constants - change to code below when fixed ... |
|
747 |
" |
|
54 | 748 |
w := self doubleWordAt:index. |
749 |
(w > (16r7FFFFFF * 16r10 + 16rF)) ifTrue:[ |
|
155 | 750 |
^ w - (16r10000000 * 16r10) |
54 | 751 |
]. |
752 |
^ w |
|
753 |
" |
|
754 |
w := self doubleWordAt:index. |
|
755 |
(w > 16r7FFFFFFF) ifTrue:[ |
|
155 | 756 |
^ w - 16r100000000 |
54 | 757 |
]. |
758 |
^ w |
|
759 |
" |
|
760 |
||
761 |
" |
|
129 | 762 |
|b| |
763 |
b := ByteArray new:4. |
|
764 |
b doubleWordAt:1 put:16rFFFFFFFF. |
|
765 |
(b signedDoubleWordAt:1) |
|
54 | 766 |
" |
767 |
! |
|
768 |
||
359 | 769 |
signedDoubleWordAt:index MSB:msb |
770 |
"return the 4-bytes starting at index as a signed Integer. |
|
771 |
Depending on msb, the value is retrieved MSB-first or LSB-first." |
|
54 | 772 |
|
359 | 773 |
|w| |
774 |
||
129 | 775 |
" |
776 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
777 |
constants - change to code below when fixed ... |
|
778 |
" |
|
359 | 779 |
w := self doubleWordAt:index MSB:msb. |
780 |
(w > (16r7FFFFFF * 16r10 + 16rF)) ifTrue:[ |
|
781 |
^ w - (16r10000000 * 16r10) |
|
782 |
]. |
|
783 |
^ w |
|
784 |
" |
|
785 |
w := self doubleWordAt:index. |
|
786 |
(w > 16r7FFFFFFF) ifTrue:[ |
|
787 |
^ w - 16r100000000 |
|
788 |
]. |
|
789 |
^ w |
|
54 | 790 |
" |
359 | 791 |
|
792 |
" |
|
793 |
|b| |
|
794 |
b := ByteArray new:4. |
|
795 |
b doubleWordAt:1 put:16rFFFFFFFF. |
|
796 |
(b signedDoubleWordAt:1) |
|
797 |
" |
|
798 |
! |
|
799 |
||
800 |
signedDoubleWordAt:index put:value |
|
801 |
"set the 4-bytes starting at index from the signed Integer value. |
|
802 |
The value is stored in the machines natural byte order." |
|
803 |
||
804 |
|v| |
|
805 |
||
806 |
value > 0 ifTrue:[ |
|
807 |
v := value |
|
808 |
] ifFalse:[ |
|
809 |
" |
|
810 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
811 |
constants - change to code below when fixed ... |
|
812 |
" |
|
813 |
v := value + (16r10000000 * 16r10) |
|
814 |
"//// v := value + 16r100000000 |
|
815 |
]. |
|
816 |
self doubleWordAt:index put:v. |
|
817 |
^ value |
|
818 |
||
819 |
" |
|
820 |
|b| |
|
821 |
b := ByteArray new:4. |
|
822 |
b signedDoubleWordAt:1 put:-1. |
|
823 |
(b doubleWordAt:1) printStringRadix:16 |
|
824 |
" |
|
825 |
! |
|
826 |
||
827 |
signedDoubleWordAt:index put:value MSB:msb |
|
828 |
"set the 4-bytes starting at index from the signed Integer value. |
|
829 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
830 |
||
831 |
|v| |
|
832 |
||
833 |
value > 0 ifTrue:[ |
|
834 |
v := value |
|
835 |
] ifFalse:[ |
|
836 |
" |
|
837 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
838 |
constants - change to code below when fixed ... |
|
839 |
" |
|
840 |
v := value + (16r10000000 * 16r10) |
|
841 |
"//// v := value + 16r100000000 |
|
842 |
]. |
|
843 |
self doubleWordAt:index put:v MSB:msb. |
|
844 |
^ value |
|
845 |
||
129 | 846 |
" |
847 |
|b| |
|
848 |
b := ByteArray new:4. |
|
849 |
b signedDoubleWordAt:1 put:-1. |
|
850 |
(b doubleWordAt:1) printStringRadix:16 |
|
851 |
" |
|
852 |
! |
|
54 | 853 |
|
563 | 854 |
signedWordAt:index |
855 |
"return the 2-bytes starting at index as a signed Integer. |
|
856 |
The value is retrieved in the machines natural byte order." |
|
857 |
||
858 |
|w "{ Class: SmallInteger }"| |
|
129 | 859 |
|
563 | 860 |
w := self wordAt:index. |
861 |
(w > 16r7FFF) ifTrue:[ |
|
862 |
^ w - 16r10000 |
|
863 |
]. |
|
864 |
^ w |
|
129 | 865 |
|
563 | 866 |
" |
867 |
|b| |
|
868 |
b := ByteArray new:2. |
|
869 |
b wordAt:1 put:16rFFFF. |
|
870 |
b signedWordAt:1 |
|
871 |
" |
|
872 |
! |
|
873 |
||
874 |
signedWordAt:index MSB:msb |
|
875 |
"return the 2-bytes starting at index as a signed Integer. |
|
876 |
The value is retrieved MSB-first if the msb-arg is true, |
|
877 |
LSB-first otherwise" |
|
878 |
||
879 |
|w "{ Class: SmallInteger }"| |
|
880 |
||
881 |
w := self wordAt:index MSB:msb. |
|
882 |
(w > 16r7FFF) ifTrue:[ |
|
883 |
^ w - 16r10000 |
|
395 | 884 |
]. |
563 | 885 |
^ w |
129 | 886 |
|
887 |
" |
|
888 |
|b| |
|
563 | 889 |
b := ByteArray new:2. |
890 |
b wordAt:1 put:16rFFFF. |
|
891 |
b signedWordAt:1 |
|
129 | 892 |
" |
893 |
! |
|
894 |
||
563 | 895 |
signedWordAt:index put:value |
896 |
"set the 2-bytes starting at index from the signed Integer value. |
|
897 |
The stored value must be in the range -32768 .. +32676. |
|
898 |
The value is stored in the machines natural byteorder." |
|
129 | 899 |
|
563 | 900 |
|v| |
129 | 901 |
|
563 | 902 |
value > 0 ifTrue:[ |
903 |
v := value |
|
359 | 904 |
] ifFalse:[ |
563 | 905 |
v := 16r10000 + value |
395 | 906 |
]. |
563 | 907 |
self wordAt:index put:v. |
908 |
^ value |
|
129 | 909 |
|
910 |
" |
|
911 |
|b| |
|
563 | 912 |
b := ByteArray new:4. |
913 |
b signedWordAt:1 put:-1. |
|
914 |
b signedWordAt:3 put:-2. |
|
915 |
b inspect |
|
916 |
" |
|
917 |
! |
|
918 |
||
919 |
signedWordAt:index put:value MSB:msb |
|
920 |
"set the 2-bytes starting at index from the signed Integer value. |
|
921 |
The stored value must be in the range -32768 .. +32676. |
|
922 |
The value is stored MSB-first, if the msb-arg is true; |
|
923 |
LSB-first otherwise" |
|
924 |
||
925 |
|v| |
|
926 |
||
927 |
value > 0 ifTrue:[ |
|
928 |
v := value |
|
929 |
] ifFalse:[ |
|
930 |
v := 16r10000 + value |
|
931 |
]. |
|
932 |
self wordAt:index put:v MSB:msb. |
|
933 |
^ value |
|
934 |
||
935 |
" |
|
936 |
|b| |
|
937 |
b := ByteArray new:4. |
|
938 |
b signedWordAt:1 put:-1. |
|
939 |
b signedWordAt:3 put:-2. |
|
129 | 940 |
b inspect |
941 |
" |
|
54 | 942 |
! |
943 |
||
563 | 944 |
wordAt:index |
945 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
946 |
The value is retrieved in the machines natural byte order |
|
947 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
948 |
||
949 |
%{ /* NOCONTEXT */ |
|
54 | 950 |
|
563 | 951 |
REGISTER int indx; |
952 |
int nIndex; |
|
953 |
union { |
|
954 |
char u_char[2]; |
|
955 |
unsigned short u_ushort; |
|
956 |
} val; |
|
957 |
OBJ cls; |
|
54 | 958 |
|
563 | 959 |
if (__isSmallInteger(index)) { |
960 |
indx = _intVal(index); |
|
961 |
if (indx > 0) { |
|
962 |
if ((cls = __qClass(self)) != ByteArray) |
|
963 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
964 |
nIndex = __byteArraySize(self); |
|
965 |
if ((indx+1) <= nIndex) { |
|
966 |
val.u_char[0] = _ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
967 |
val.u_char[1] = _ByteArrayInstPtr(self)->ba_element[indx-1+1]; |
|
968 |
RETURN ( _MKSMALLINT(val.u_ushort) ); |
|
969 |
} |
|
970 |
} |
|
971 |
} |
|
972 |
%}. |
|
973 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 974 |
! |
975 |
||
563 | 976 |
wordAt:index MSB:msb |
977 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
978 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
979 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
980 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
981 |
||
982 |
%{ /* NOCONTEXT */ |
|
983 |
||
984 |
REGISTER int indx; |
|
985 |
int nIndex; |
|
986 |
int val; |
|
987 |
OBJ cls; |
|
54 | 988 |
|
563 | 989 |
if (__isSmallInteger(index)) { |
990 |
indx = _intVal(index); |
|
991 |
if (indx > 0) { |
|
992 |
if ((cls = __qClass(self)) != ByteArray) |
|
993 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
994 |
nIndex = __byteArraySize(self); |
|
995 |
if ((indx+1) <= nIndex) { |
|
996 |
if (msb == true) { |
|
997 |
val = _ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
998 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1+1]; |
|
999 |
} else { |
|
1000 |
val = _ByteArrayInstPtr(self)->ba_element[indx+1-1]; |
|
1001 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
1002 |
} |
|
1003 |
RETURN ( _MKSMALLINT(val) ); |
|
1004 |
} |
|
1005 |
} |
|
1006 |
} |
|
1007 |
%}. |
|
1008 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 1009 |
! |
1010 |
||
563 | 1011 |
wordAt:index put:value |
1012 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1013 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1014 |
The value is stored in the machines natural byteorder. |
|
1015 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1016 |
||
1017 |
%{ /* NOCONTEXT */ |
|
54 | 1018 |
|
563 | 1019 |
REGISTER int indx; |
1020 |
int nIndex; |
|
1021 |
int v; |
|
1022 |
union { |
|
1023 |
char u_char[2]; |
|
1024 |
unsigned short u_ushort; |
|
1025 |
} val; |
|
1026 |
OBJ cls; |
|
129 | 1027 |
|
563 | 1028 |
if (__bothSmallInteger(index, value)) { |
1029 |
indx = _intVal(index); |
|
1030 |
if (indx > 0) { |
|
1031 |
if ((cls = __qClass(self)) != ByteArray) |
|
1032 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
1033 |
nIndex = __byteArraySize(self); |
|
1034 |
if ((indx+1) <= nIndex) { |
|
1035 |
val.u_ushort = v = _intVal(value); |
|
1036 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
1037 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val.u_char[0]; |
|
1038 |
_ByteArrayInstPtr(self)->ba_element[indx-1+1] = val.u_char[1]; |
|
1039 |
RETURN ( value ); |
|
1040 |
} |
|
1041 |
} |
|
1042 |
} |
|
1043 |
} |
|
1044 |
%}. |
|
1045 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1046 |
^ self elementBoundsError |
|
1047 |
]. |
|
1048 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1049 |
|
1050 |
" |
|
563 | 1051 |
|b| |
1052 |
b := ByteArray new:4. |
|
1053 |
b wordAt:1 put:16r0102. |
|
1054 |
b wordAt:3 put:16r0304. |
|
1055 |
b inspect |
|
129 | 1056 |
" |
1057 |
! |
|
1058 |
||
563 | 1059 |
wordAt:index put:value MSB:msb |
1060 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1061 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1062 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
1063 |
lower index) if msb is false, MSB-first otherwise. |
|
1064 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1065 |
||
1066 |
%{ /* NOCONTEXT */ |
|
1067 |
||
1068 |
REGISTER int indx; |
|
1069 |
int nIndex; |
|
1070 |
int val; |
|
1071 |
OBJ cls; |
|
1072 |
||
1073 |
if (__bothSmallInteger(index, value)) { |
|
1074 |
indx = _intVal(index); |
|
1075 |
if (indx > 0) { |
|
1076 |
if ((cls = __qClass(self)) != ByteArray) |
|
1077 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
1078 |
nIndex = __byteArraySize(self); |
|
1079 |
if ((indx+1) <= nIndex) { |
|
1080 |
val = _intVal(value); |
|
1081 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
1082 |
if (msb == true) { |
|
1083 |
_ByteArrayInstPtr(self)->ba_element[indx-1+1] = val & 0xFF; |
|
1084 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = (val>>8) & 0xFF; |
|
1085 |
} else { |
|
1086 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF; |
|
1087 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = (val>>8) & 0xFF; |
|
1088 |
} |
|
1089 |
RETURN ( value ); |
|
1090 |
} |
|
1091 |
} |
|
1092 |
} |
|
1093 |
} |
|
1094 |
%}. |
|
1095 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1096 |
^ self elementBoundsError |
|
1097 |
]. |
|
1098 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1099 |
|
1100 |
" |
|
563 | 1101 |
|b| |
1102 |
b := ByteArray new:8. |
|
1103 |
b wordAt:1 put:16r0102 MSB:false. |
|
1104 |
b wordAt:3 put:16r0304 MSB:false. |
|
1105 |
b wordAt:5 put:16r0102 MSB:true. |
|
1106 |
b wordAt:7 put:16r0304 MSB:true. |
|
1107 |
b inspect |
|
129 | 1108 |
" |
1 | 1109 |
! ! |
1110 |
||
550 | 1111 |
!ByteArray methodsFor:'binary storage'! |
1112 |
||
1113 |
storeBinaryDefinitionOn:stream manager:manager |
|
1114 |
"append a binary representation of the receiver onto stream. |
|
1115 |
Redefined since ByteArrays are stored with a special type code and |
|
1116 |
in a more compact way." |
|
1117 |
||
1118 |
"not, if I have named instance variables" |
|
1119 |
self class instSize ~~ 0 ifTrue:[ |
|
1120 |
^ super storeBinaryDefinitionOn:stream manager:manager |
|
1121 |
]. |
|
1122 |
manager putIdOfClass:(self class) on: stream. |
|
1123 |
stream nextNumber:4 put:(self basicSize). |
|
1124 |
stream nextPutAll:(self asByteArray) |
|
1125 |
! ! |
|
1126 |
||
12 | 1127 |
!ByteArray methodsFor:'converting'! |
1128 |
||
1129 |
asByteArray |
|
1130 |
"return the receiver as a byteArray" |
|
1131 |
||
1132 |
"could be an instance of a subclass..." |
|
1133 |
self class == ByteArray ifTrue:[ |
|
155 | 1134 |
^ self |
12 | 1135 |
]. |
1136 |
^ super asByteArray |
|
129 | 1137 |
|
1138 |
" |
|
1139 |
'hello world' asByteArray |
|
1140 |
#(1 2 3 4 5 6 7) asByteArray |
|
1141 |
#(1 2 256 4 5 6 7) asByteArray |
|
1142 |
" |
|
12 | 1143 |
! ! |
1144 |
||
92 | 1145 |
!ByteArray methodsFor:'copying'! |
1146 |
||
1147 |
copyFrom:start to:stop |
|
1148 |
"return the subcollection starting at index start, anInteger and ending |
|
1149 |
at stop, anInteger. |
|
1150 |
- reimplemented here for speed" |
|
1151 |
||
1152 |
%{ /* NOCONTEXT */ |
|
1153 |
||
1154 |
#if !defined(FAST_MEMCPY) |
|
1155 |
REGISTER unsigned char *srcp; |
|
1156 |
#endif |
|
1157 |
REGISTER unsigned char *dstp; |
|
1158 |
REGISTER int count; |
|
1159 |
int len, index1, index2, sz; |
|
1160 |
OBJ newByteArray; |
|
1161 |
||
1162 |
if (__isByteArray(self) |
|
249 | 1163 |
&& __bothSmallInteger(start, stop)) { |
1164 |
len = __byteArraySize(self); |
|
155 | 1165 |
index1 = _intVal(start); |
1166 |
index2 = _intVal(stop); |
|
92 | 1167 |
|
155 | 1168 |
if ((index1 <= index2) && (index1 > 0)) { |
1169 |
if (index2 <= len) { |
|
1170 |
count = index2 - index1 + 1; |
|
1171 |
PROTECT_CONTEXT |
|
1172 |
sz = OHDR_SIZE + count; |
|
1173 |
_qNew(newByteArray, sz, SENDER); |
|
1174 |
UNPROTECT_CONTEXT |
|
1175 |
if (newByteArray != nil) { |
|
1176 |
_InstPtr(newByteArray)->o_class = ByteArray; |
|
1177 |
dstp = _ByteArrayInstPtr(newByteArray)->ba_element; |
|
92 | 1178 |
#ifdef FAST_MEMCPY |
155 | 1179 |
bcopy(_ByteArrayInstPtr(self)->ba_element + index1 - 1, dstp, count); |
92 | 1180 |
#else |
155 | 1181 |
srcp = _ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1182 |
while (count--) { |
|
1183 |
*dstp++ = *srcp++; |
|
1184 |
} |
|
92 | 1185 |
#endif |
155 | 1186 |
RETURN ( newByteArray ); |
1187 |
} |
|
1188 |
} |
|
1189 |
} |
|
92 | 1190 |
} |
1191 |
%}. |
|
1192 |
" |
|
1193 |
fall back in case of non-integer index or out-of-bound index; |
|
1194 |
will eventually lead to an out-of-bound signal raise |
|
1195 |
" |
|
1196 |
^ super copyFrom:start to:stop |
|
1197 |
! ! |
|
1198 |
||
1 | 1199 |
!ByteArray methodsFor:'filling and replacing'! |
1200 |
||
155 | 1201 |
from:start to:stop put:aNumber |
1202 |
"fill part of the receiver with aNumber. |
|
1203 |
- reimplemented here for speed" |
|
1204 |
||
1205 |
%{ /* NOCONTEXT */ |
|
1206 |
||
1207 |
REGISTER unsigned char *dstp; |
|
1208 |
REGISTER int count, value; |
|
249 | 1209 |
int len, index1, index2; |
1210 |
int nInst; |
|
1211 |
OBJ cls; |
|
155 | 1212 |
|
249 | 1213 |
if (__isSmallInteger(aNumber) |
1214 |
&& __bothSmallInteger(start, stop)) { |
|
1215 |
len = __byteArraySize(self); |
|
155 | 1216 |
index1 = _intVal(start); |
1217 |
index2 = _intVal(stop); |
|
1218 |
||
249 | 1219 |
dstp = _ByteArrayInstPtr(self)->ba_element + index1 - 1; |
328 | 1220 |
if ((cls = __qClass(self)) != ByteArray) { |
249 | 1221 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1222 |
dstp += nInst; |
|
1223 |
len -= nInst; |
|
1224 |
} |
|
1225 |
||
155 | 1226 |
value = _intVal(aNumber); |
422 | 1227 |
if (((value & ~0xFF) == 0) /* i.e. (value >= 0) && (value <= 255) */ |
155 | 1228 |
&& (index1 <= index2) |
1229 |
&& (index1 > 0)) { |
|
1230 |
if (index2 <= len) { |
|
1231 |
count = index2 - index1 + 1; |
|
1232 |
#ifdef FAST_MEMSET |
|
249 | 1233 |
memset(dstp, count, value); |
155 | 1234 |
#else |
422 | 1235 |
# ifdef UNROLL_LOOPS |
1236 |
while (count >= 8) { |
|
1237 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1238 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1239 |
dstp += 8; |
|
1240 |
count -= 8; |
|
1241 |
} |
|
1242 |
# endif |
|
155 | 1243 |
while (count--) { |
1244 |
*dstp++ = value; |
|
1245 |
} |
|
1246 |
#endif |
|
1247 |
RETURN (self); |
|
1248 |
} |
|
1249 |
} |
|
1250 |
} |
|
1251 |
%}. |
|
1252 |
" |
|
1253 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1254 |
will eventually lead to an out-of-bound signal raise |
|
1255 |
" |
|
1256 |
^ super from:start to:stop put:aNumber |
|
1257 |
! |
|
1258 |
||
249 | 1259 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
1260 |
"replace elements from another collection, which must be a ByteArray |
|
1261 |
or String." |
|
1 | 1262 |
|
54 | 1263 |
%{ /* NOCONTEXT */ |
1 | 1264 |
|
54 | 1265 |
int nIndex, repNIndex; |
1266 |
int startIndex, stopIndex; |
|
1267 |
REGISTER unsigned char *src; |
|
1268 |
REGISTER int repStartIndex; |
|
1269 |
int repStopIndex, count; |
|
1270 |
REGISTER unsigned char *dst; |
|
249 | 1271 |
OBJ cls; |
1272 |
int nInst; |
|
1 | 1273 |
|
249 | 1274 |
if (__isBytes(aCollection) |
1275 |
&& __bothSmallInteger(start, stop) |
|
1276 |
&& __isSmallInteger(repStart)) { |
|
155 | 1277 |
startIndex = _intVal(start) - 1; |
1278 |
if (startIndex >= 0) { |
|
249 | 1279 |
dst = (_ByteArrayInstPtr(self)->ba_element) + startIndex; |
1280 |
nIndex = __byteArraySize(self); |
|
1281 |
||
328 | 1282 |
if ((cls = __qClass(self)) != ByteArray) { |
249 | 1283 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1284 |
dst += nInst; |
|
1285 |
nIndex -= nInst; |
|
1286 |
} |
|
1287 |
||
155 | 1288 |
stopIndex = _intVal(stop) - 1; |
1289 |
count = stopIndex - startIndex + 1; |
|
1290 |
if (count == 0) { |
|
1291 |
RETURN ( self ); |
|
1292 |
} |
|
1293 |
if ((count > 0) && (stopIndex < nIndex)) { |
|
1294 |
repStartIndex = _intVal(repStart) - 1; |
|
1295 |
if (repStartIndex >= 0) { |
|
328 | 1296 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
249 | 1297 |
src = (_ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
328 | 1298 |
if ((cls = __qClass(aCollection)) != ByteArray) { |
249 | 1299 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1300 |
src += nInst; |
|
1301 |
repNIndex -= nInst; |
|
1302 |
} |
|
1303 |
||
155 | 1304 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
1305 |
if (repStopIndex < repNIndex) { |
|
1306 |
if (aCollection == self) { |
|
1307 |
/* take care of overlapping copy */ |
|
1308 |
if (src < dst) { |
|
1309 |
/* must do a reverse copy */ |
|
1310 |
src += count; |
|
1311 |
dst += count; |
|
1312 |
while (count-- > 0) { |
|
1313 |
*--dst = *--src; |
|
1314 |
} |
|
1315 |
RETURN ( self ); |
|
1316 |
} |
|
1317 |
} |
|
1 | 1318 |
#ifdef FAST_MEMCPY |
166 | 1319 |
# ifdef bcopy4 |
1320 |
if (((src & 3) == 0) |
|
1321 |
&& ((dst & 3) == 0)) { |
|
1322 |
/* copy aligned part */ |
|
1323 |
bcopy4(src, dst, count//4); |
|
1324 |
/* copy rest */ |
|
1325 |
if (count & 3) { |
|
1326 |
src += (count & ~3); |
|
1327 |
dst += (count & ~3); |
|
1328 |
count = count & 3; |
|
1329 |
while (count--) { |
|
1330 |
*dst++ = *src++; |
|
1331 |
} |
|
1332 |
} |
|
1333 |
} else |
|
1334 |
# endif /* bcopy4 */ |
|
1335 |
bcopy(src, dst, count); |
|
1 | 1336 |
#else |
155 | 1337 |
while (count-- > 0) { |
1338 |
*dst++ = *src++; |
|
1339 |
} |
|
1 | 1340 |
#endif |
155 | 1341 |
RETURN ( self ); |
1342 |
} |
|
1343 |
} |
|
1344 |
} |
|
1345 |
} |
|
54 | 1346 |
} |
437 | 1347 |
%}. |
1348 |
" |
|
1349 |
fall back in case of non-ByteArray argument, |
|
1350 |
or for the error report if any index is invalid |
|
1351 |
" |
|
1352 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
563 | 1353 |
! |
1354 |
||
1355 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1356 |
"replace elements from another collection" |
|
1357 |
||
1358 |
(aCollection class == self class) ifTrue:[ |
|
1359 |
^ self replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
|
1360 |
]. |
|
1361 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1 | 1362 |
! ! |
1363 |
||
129 | 1364 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1365 |
|
129 | 1366 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray |
155 | 1367 |
mapping:aMapByteArray |
129 | 1368 |
|
1369 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
|
1370 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1371 |
to allow for any padding. On the fly, the source bytes are translated |
|
1372 |
using aMapByteArray (if non-nil). |
|
1373 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1374 |
value of n is found in map at:(n + 1). |
|
1375 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1376 |
corresponds to the high bit(s) if the first byte in the input. |
|
1377 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
|
1378 |
bitmaps. |
|
1379 |
It can also be used to compress byte-arrays into bitArrays." |
|
1380 |
||
1381 |
%{ /* NOCONTEXT */ |
|
1382 |
||
1383 |
REGISTER unsigned char *src, *dst; |
|
1384 |
REGISTER int wrun; |
|
1385 |
unsigned char *dstNext; |
|
1386 |
int bytesPerRow, mask, shift0, shift; |
|
1387 |
int w, h, hrun; |
|
1388 |
int srcBytes, dstBytes; |
|
1389 |
int bitsPerPixel; |
|
1390 |
int bits; |
|
1391 |
int ncells; |
|
1392 |
unsigned char *map; |
|
1393 |
||
328 | 1394 |
if ((__qClass(self) == ByteArray) |
1395 |
&& (__qClass(aByteArray) == ByteArray) |
|
249 | 1396 |
&& __isSmallInteger(nBitsPerPixel) |
1397 |
&& __bothSmallInteger(height, width)) { |
|
155 | 1398 |
if ((aMapByteArray != nil) |
328 | 1399 |
&& (__Class(aMapByteArray) == ByteArray)) { |
155 | 1400 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element; |
1401 |
} else { |
|
1402 |
map = (unsigned char *)0; |
|
1403 |
} |
|
129 | 1404 |
|
155 | 1405 |
bitsPerPixel = _intVal(nBitsPerPixel); |
1406 |
w = _intVal(width); |
|
1407 |
h = _intVal(height); |
|
1408 |
src = _ByteArrayInstPtr(self)->ba_element; |
|
1409 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element; |
|
1410 |
switch (bitsPerPixel) { |
|
1411 |
case 1: |
|
1412 |
mask = 0x01; |
|
1413 |
break; |
|
1414 |
case 2: |
|
1415 |
mask = 0x03; |
|
1416 |
break; |
|
1417 |
case 4: |
|
1418 |
mask = 0x0F; |
|
1419 |
break; |
|
1420 |
case 8: |
|
1421 |
mask = 0xFF; |
|
1422 |
break; |
|
1423 |
default: |
|
1424 |
goto fail; |
|
1425 |
} |
|
1426 |
if (map) { |
|
1427 |
/* |
|
1428 |
* if a map is present, it must have entries for |
|
1429 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1430 |
*/ |
|
328 | 1431 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) |
155 | 1432 |
goto fail; |
1433 |
} |
|
129 | 1434 |
|
155 | 1435 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1436 |
dstBytes = bytesPerRow * h; |
|
1437 |
srcBytes = w * h; |
|
129 | 1438 |
|
249 | 1439 |
if ((__byteArraySize(self) >= srcBytes) |
1440 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
155 | 1441 |
for (hrun=h; hrun; hrun--) { |
1442 |
dstNext = dst + bytesPerRow; |
|
1443 |
bits = 0; shift = 8; |
|
1444 |
if (map) { |
|
1445 |
for (wrun=w; wrun; wrun--) { |
|
1446 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1447 |
shift -= bitsPerPixel; |
|
1448 |
if (shift == 0) { |
|
1449 |
*dst++ = bits; |
|
1450 |
bits = 0; shift = 8; |
|
1451 |
} |
|
1452 |
} |
|
1453 |
} else { |
|
1454 |
for (wrun=w; wrun; wrun--) { |
|
1455 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1456 |
shift -= bitsPerPixel; |
|
1457 |
if (shift == 0) { |
|
1458 |
*dst++ = bits; |
|
1459 |
bits = 0; shift = 8; |
|
1460 |
} |
|
1461 |
} |
|
1462 |
} |
|
1463 |
if (shift != 8) { |
|
1464 |
*dst = bits; |
|
1465 |
} |
|
1466 |
dst = dstNext; |
|
1467 |
} |
|
1468 |
RETURN ( self ); |
|
1469 |
} |
|
129 | 1470 |
} |
1471 |
fail: ; |
|
1472 |
%} |
|
1473 |
. |
|
1474 |
self primitiveFailed |
|
1475 |
||
1476 |
"Example1: |
|
1477 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1478 |
" |
|
1479 |
" |
|
1480 |
|inBits outBits| |
|
1481 |
||
1482 |
inBits := #[0 0 0 0 1 1 1 1 |
|
155 | 1483 |
0 0 1 1 0 0 1 1 |
1484 |
0 1 0 1 0 1 0 1 |
|
1485 |
1 1 1 1 0 0 0 0]. |
|
129 | 1486 |
outBits := ByteArray new:4. |
1487 |
inBits compressPixels:1 width:8 height:4 |
|
155 | 1488 |
into:outBits mapping:nil. |
129 | 1489 |
outBits inspect |
1490 |
" |
|
1491 |
||
1492 |
"Example2: |
|
1493 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
1494 |
and 176 to 1-bits. (just a stupid example) |
|
1495 |
" |
|
1496 |
" |
|
1497 |
|inBits outBits map| |
|
1498 |
||
1499 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1500 |
map := ByteArray new:256. |
|
1501 |
map at:176+1 put:1. |
|
1502 |
||
1503 |
outBits := ByteArray new:2. |
|
1504 |
inBits compressPixels:1 width:16 height:1 |
|
155 | 1505 |
into:outBits mapping:map. |
129 | 1506 |
outBits inspect |
1507 |
" |
|
1508 |
||
1509 |
"Example3: |
|
1510 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
1511 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
1512 |
" |
|
1513 |
" |
|
1514 |
|inBits outBits map| |
|
1515 |
||
1516 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1517 |
map := ByteArray new:256. |
|
1518 |
map atAll:(128+1 to:255+1) put:1. |
|
1519 |
||
1520 |
outBits := ByteArray new:2. |
|
1521 |
inBits compressPixels:1 width:16 height:1 |
|
155 | 1522 |
into:outBits mapping:map. |
129 | 1523 |
outBits inspect |
1524 |
" |
|
563 | 1525 |
! |
1526 |
||
1527 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray |
|
1528 |
mapping:aMapByteArray |
|
1529 |
||
1530 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
|
1531 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
1532 |
to skip any padded src-bits. On the fly, the destination pixels |
|
1533 |
are translated using aMapByteArray (if non-nil). |
|
1534 |
Input bits are read left-to right, i.e. the first byte in the output |
|
1535 |
corresponds to the high bit(s) in the inputs first byte. |
|
1536 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
1537 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
1538 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1539 |
value of n is found in map at:(n + 1). |
|
1540 |
It can also be used to expand bit-arrays into byteArrays. |
|
1541 |
This method is specialized for ByteArray arguments - it will not handle |
|
1542 |
anything else." |
|
1543 |
||
1544 |
%{ /* NOCONTEXT */ |
|
1545 |
||
1546 |
REGISTER unsigned char *src, *dst; |
|
1547 |
REGISTER int wrun; |
|
1548 |
unsigned char *srcNext; |
|
1549 |
int bytesPerRow, mask, shift0, shift; |
|
1550 |
int w, h, hrun; |
|
1551 |
int srcBytes, dstBytes; |
|
1552 |
int bitsPerPixel; |
|
1553 |
int bits; |
|
1554 |
int ncells; |
|
1555 |
unsigned char *map; |
|
1556 |
||
1557 |
if ((__qClass(self) == ByteArray) |
|
1558 |
&& (__qClass(aByteArray) == ByteArray) |
|
1559 |
&& __isSmallInteger(nBitsPerPixel) |
|
1560 |
&& __bothSmallInteger(height, width)) { |
|
1561 |
if ((aMapByteArray != nil) |
|
1562 |
&& (__Class(aMapByteArray) == ByteArray)) { |
|
1563 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1564 |
} else { |
|
1565 |
map = (unsigned char *)0; |
|
1566 |
} |
|
1567 |
||
1568 |
bitsPerPixel = _intVal(nBitsPerPixel); |
|
1569 |
w = _intVal(width); |
|
1570 |
h = _intVal(height); |
|
1571 |
src = _ByteArrayInstPtr(self)->ba_element; |
|
1572 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element; |
|
1573 |
switch (bitsPerPixel) { |
|
1574 |
case 1: |
|
1575 |
mask = 0x01; |
|
1576 |
break; |
|
1577 |
case 2: |
|
1578 |
mask = 0x03; |
|
1579 |
break; |
|
1580 |
case 4: |
|
1581 |
mask = 0x0F; |
|
1582 |
break; |
|
1583 |
case 8: |
|
1584 |
mask = 0xFF; |
|
1585 |
break; |
|
1586 |
default: |
|
1587 |
goto fail; |
|
1588 |
} |
|
1589 |
ncells = mask + 1; |
|
1590 |
if (map) { |
|
1591 |
/* |
|
1592 |
* if a map is present, it must have the correct size |
|
1593 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
1594 |
*/ |
|
1595 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) |
|
1596 |
goto fail; |
|
1597 |
} |
|
1598 |
||
1599 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
1600 |
shift0 = 8 - bitsPerPixel; |
|
1601 |
srcBytes = bytesPerRow * h; |
|
1602 |
dstBytes = w * h; |
|
1603 |
||
1604 |
if ((__byteArraySize(self) >= srcBytes) |
|
1605 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1606 |
for (hrun=h; hrun; hrun--) { |
|
1607 |
srcNext = src + bytesPerRow; |
|
1608 |
shift = shift0; |
|
1609 |
if (map) { |
|
1610 |
for (wrun=w; wrun; wrun--) { |
|
1611 |
if (shift == shift0) { |
|
1612 |
bits = *src++; |
|
1613 |
} |
|
1614 |
*dst++ = map[(bits >> shift) & mask]; |
|
1615 |
shift -= bitsPerPixel; |
|
1616 |
if (shift < 0) { |
|
1617 |
shift = shift0; |
|
1618 |
} |
|
1619 |
} |
|
1620 |
} else { |
|
1621 |
for (wrun=w; wrun; wrun--) { |
|
1622 |
if (shift == shift0) { |
|
1623 |
bits = *src++; |
|
1624 |
} |
|
1625 |
*dst++ = (bits >> shift) & mask; |
|
1626 |
shift -= bitsPerPixel; |
|
1627 |
if (shift < 0) { |
|
1628 |
shift = shift0; |
|
1629 |
} |
|
1630 |
} |
|
1631 |
} |
|
1632 |
src = srcNext; |
|
1633 |
} |
|
1634 |
RETURN ( self ); |
|
1635 |
} |
|
1636 |
} |
|
1637 |
fail: ; |
|
1638 |
%} |
|
1639 |
. |
|
1640 |
self primitiveFailed |
|
1641 |
||
1642 |
"Example1: |
|
1643 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
1644 |
" |
|
1645 |
" |
|
1646 |
|inBits outBits| |
|
1647 |
||
1648 |
inBits := #[2r11110000 |
|
1649 |
2r11001100 |
|
1650 |
2r01010101 |
|
1651 |
2r00001111]. |
|
1652 |
outBits := ByteArray new:(8*4). |
|
1653 |
inBits expandPixels:1 width:8 height:4 |
|
1654 |
into:outBits mapping:nil. |
|
1655 |
outBits inspect |
|
1656 |
" |
|
1657 |
||
1658 |
"Example2: |
|
1659 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
1660 |
1-bits to 176. (just a stupid example) |
|
1661 |
" |
|
1662 |
" |
|
1663 |
|inBits outBits| |
|
1664 |
||
1665 |
inBits := #[2r11110000 2r11001100]. |
|
1666 |
outBits := ByteArray new:16. |
|
1667 |
inBits expandPixels:1 width:16 height:1 |
|
1668 |
into:outBits mapping:#[99 176]. |
|
1669 |
outBits inspect |
|
1670 |
" |
|
1671 |
! |
|
1672 |
||
1673 |
invert |
|
1674 |
"invert all bytes - used with image manipulations |
|
1675 |
written as a primitive for speed. |
|
1676 |
Q: is this really needed ?" |
|
1677 |
||
1678 |
%{ /* NOCONTEXT */ |
|
1679 |
||
1680 |
REGISTER unsigned char *dst; |
|
1681 |
REGISTER unsigned long *ldst; |
|
1682 |
REGISTER int cnt; |
|
1683 |
||
1684 |
if (__qClass(self) == ByteArray) { |
|
1685 |
cnt = __byteArraySize(self); |
|
1686 |
dst = _ByteArrayInstPtr(self)->ba_element; |
|
1687 |
if (! ((int)dst & (sizeof(long)-1))) { |
|
1688 |
ldst = (unsigned long *)dst; |
|
1689 |
while (cnt >= sizeof(long)) { |
|
1690 |
*ldst = ~(*ldst); |
|
1691 |
ldst++; |
|
1692 |
cnt -= sizeof(long); |
|
1693 |
} |
|
1694 |
dst = (unsigned char *)ldst; |
|
1695 |
} |
|
1696 |
while (cnt--) { |
|
1697 |
*dst = ~(*dst); |
|
1698 |
dst++; |
|
1699 |
} |
|
1700 |
RETURN ( self ); |
|
1701 |
} |
|
1702 |
%} |
|
1703 |
. |
|
1704 |
self primitiveFailed |
|
1705 |
! |
|
1706 |
||
1707 |
reverse |
|
1708 |
"reverse order of elements inplace - |
|
1709 |
written as a primitive for speed on image manipulations (mirror)" |
|
1710 |
||
1711 |
%{ /* NOCONTEXT */ |
|
1712 |
||
1713 |
REGISTER unsigned char *p1, *p2; |
|
1714 |
REGISTER int cnt; |
|
1715 |
REGISTER unsigned t; |
|
1716 |
OBJ cls; |
|
1717 |
||
1718 |
if (__qClass(self) == ByteArray) { |
|
1719 |
cnt = __byteArraySize(self); |
|
1720 |
p1 = _ByteArrayInstPtr(self)->ba_element; |
|
1721 |
p2 = p1 + cnt - 1; |
|
1722 |
while (cnt > 0) { |
|
1723 |
t = *p1; |
|
1724 |
*p1++ = *p2; |
|
1725 |
*p2-- = t; |
|
1726 |
cnt-=2; |
|
1727 |
} |
|
1728 |
RETURN ( self ); |
|
1729 |
} |
|
1730 |
%}. |
|
1731 |
^ super reverse |
|
1 | 1732 |
! ! |
550 | 1733 |
|
1734 |
!ByteArray methodsFor:'printing & storing'! |
|
1735 |
||
1736 |
asPackedString |
|
1737 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1738 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1739 |
as a corresponding character (32+value) and the resulting string |
|
1740 |
is returned. The resulting string is always a multiple of 4 (since |
|
1741 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1742 |
encoded in the last character. |
|
1743 |
ST-80 uses this encoding for Images ... |
|
1744 |
PS: I dont like it ;-)" |
|
1745 |
||
1746 |
|outStream |
|
1747 |
index "{ Class:SmallInteger}" |
|
1748 |
stop "{ Class:SmallInteger}" |
|
1749 |
n "{ Class:SmallInteger}"| |
|
1750 |
||
1751 |
outStream := WriteStream on:String new. |
|
1752 |
index := 1. |
|
1753 |
stop := self size. |
|
1754 |
[index <= stop] whileTrue:[ |
|
1755 |
"take 3 source bytes" |
|
1756 |
n := (self at:index) bitShift:16. |
|
1757 |
(index < stop) ifTrue:[ |
|
1758 |
n := n bitOr:((self at:(index + 1)) bitShift:8). |
|
1759 |
(index + 1 < stop) ifTrue:[ |
|
1760 |
n := n bitOr:(self at:(index + 2)). |
|
1761 |
]. |
|
1762 |
]. |
|
1763 |
||
1764 |
"took me a while to find that one out ..." |
|
1765 |
n := n bitXor:16r820820. |
|
1766 |
||
1767 |
outStream nextPut:(Character value:(n bitShift:-18) + 32). |
|
1768 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1769 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1770 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1771 |
index := index + 3. |
|
1772 |
]. |
|
1773 |
stop \\ 3 ~~ 0 ifTrue:[ |
|
1774 |
outStream position:(outStream position - 1). |
|
1775 |
outStream nextPut:(Character value:(stop \\ 3 + 96)). |
|
1776 |
]. |
|
1777 |
^ outStream contents |
|
563 | 1778 |
! |
1779 |
||
1780 |
displayString |
|
1781 |
^ self storeString |
|
1782 |
||
1783 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1784 |
! |
|
1785 |
||
1786 |
isLiteral |
|
1787 |
"return true, if the receiver can be used as a literal |
|
1788 |
(i.e. can be used in constant arrays)" |
|
1789 |
||
1790 |
"no, simply returning true here is a mistake: |
|
1791 |
it could be a subclass of ByteArray |
|
1792 |
(of which the compiler does not know at all ...)" |
|
1793 |
||
1794 |
^ self class == ByteArray |
|
1795 |
! |
|
1796 |
||
1797 |
storeOn:aStream |
|
1798 |
"append a printed representation from which the receiver can be |
|
1799 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
1800 |
||
1801 |
|first| |
|
1802 |
||
1803 |
self class == ByteArray ifTrue:[ |
|
1804 |
aStream nextPutAll:'#['. |
|
1805 |
first := true. |
|
1806 |
self do:[:byte | |
|
1807 |
first ifFalse:[aStream space] |
|
1808 |
ifTrue:[first := false]. |
|
1809 |
byte storeOn:aStream. |
|
1810 |
]. |
|
1811 |
aStream nextPutAll:']'. |
|
1812 |
^ self |
|
1813 |
]. |
|
1814 |
^ super storeOn:aStream |
|
1815 |
||
1816 |
" |
|
1817 |
#[1 2 3 4 5] storeOn:Transcript |
|
1818 |
" |
|
550 | 1819 |
! ! |
1820 |
||
1821 |
!ByteArray methodsFor:'queries'! |
|
1822 |
||
1823 |
indexOf:aByte startingAt:start |
|
1824 |
"return the index of the first occurrence of the argument, aByte |
|
1825 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
1826 |
- reimplemented here for speed" |
|
1827 |
||
1828 |
%{ /* NOCONTEXT */ |
|
1829 |
||
1830 |
REGISTER unsigned char *cp; |
|
1831 |
REGISTER int index, byteValue; |
|
1832 |
REGISTER int len; |
|
1833 |
OBJ cls; |
|
1834 |
||
1835 |
if (__isSmallInteger(aByte)) { |
|
1836 |
byteValue = _intVal(aByte); |
|
1837 |
||
1838 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
1839 |
/* |
|
1840 |
* searching for something which cannot be found |
|
1841 |
*/ |
|
1842 |
RETURN ( _MKSMALLINT(0) ); |
|
1843 |
} |
|
1844 |
||
1845 |
if (__isSmallInteger(start)) { |
|
1846 |
index = _intVal(start); |
|
1847 |
len = __byteArraySize(self); |
|
1848 |
cp = _ByteArrayInstPtr(self)->ba_element; |
|
1849 |
if ((cls = __qClass(self)) != ByteArray) { |
|
1850 |
int nInst; |
|
1851 |
||
1852 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
1853 |
cp += nInst; |
|
1854 |
len -= nInst; |
|
1855 |
} |
|
1856 |
cp += index - 1; |
|
1857 |
while (index <= len) { |
|
1858 |
if (*cp == byteValue) { |
|
1859 |
RETURN ( _MKSMALLINT(index) ); |
|
1860 |
} |
|
1861 |
index++; |
|
1862 |
cp++; |
|
1863 |
} |
|
1864 |
RETURN ( _MKSMALLINT(0) ); |
|
1865 |
} |
|
1866 |
} |
|
1867 |
%}. |
|
1868 |
^ super indexOf:aByte startingAt:start |
|
1869 |
||
1870 |
" |
|
1871 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 |
|
1872 |
" |
|
1873 |
! |
|
1874 |
||
563 | 1875 |
max |
1876 |
"return the maximum value in the receiver - |
|
1877 |
redefined to speedup image processing and sound-player |
|
1878 |
(which need a fast method for this on byteArrays)" |
|
550 | 1879 |
|
563 | 1880 |
%{ /* NOCONTEXT */ |
550 | 1881 |
|
1882 |
REGISTER unsigned char *cp; |
|
563 | 1883 |
REGISTER int index, max; |
1884 |
int len; |
|
550 | 1885 |
|
1886 |
if (__qClass(self) == ByteArray) { |
|
563 | 1887 |
max = 0; |
1888 |
index = 0; |
|
550 | 1889 |
len = __qSize(self) - OHDR_SIZE; |
1890 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]); |
|
563 | 1891 |
while (++index <= len) { |
1892 |
if (*cp > max) max = *cp; |
|
550 | 1893 |
cp++; |
1894 |
} |
|
563 | 1895 |
RETURN ( _MKSMALLINT(max) ); |
550 | 1896 |
} |
563 | 1897 |
%}. |
1898 |
^ super max |
|
550 | 1899 |
|
1900 |
" |
|
563 | 1901 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 1902 |
" |
1903 |
! |
|
1904 |
||
1905 |
usageCounts |
|
1906 |
"return an array filled with value-counts - |
|
1907 |
This is needed in the bitmap/image classes to get info on color usage. |
|
1908 |
(i.e. to build up a histogram of color usage within an image)." |
|
1909 |
||
1910 |
|counts| |
|
1911 |
||
1912 |
counts := Array basicNew:256. |
|
1913 |
||
1914 |
%{ /* STACK: 2000 */ |
|
1915 |
||
1916 |
REGISTER unsigned char *cp; |
|
1917 |
REGISTER int nByte; |
|
1918 |
REGISTER int index; |
|
1919 |
int icounts[256]; |
|
1920 |
||
1921 |
if ((__qClass(self) == ByteArray) && __isArray(counts)) { |
|
1922 |
/* |
|
1923 |
* zero counts |
|
1924 |
*/ |
|
1925 |
for (index=0; index<256; index++) { |
|
1926 |
icounts[index] = 0; |
|
1927 |
} |
|
1928 |
||
1929 |
/* |
|
1930 |
* count |
|
1931 |
*/ |
|
1932 |
nByte = __qSize(self) - OHDR_SIZE; |
|
1933 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]); |
|
1934 |
while (nByte--) { |
|
1935 |
icounts[*cp++]++; |
|
1936 |
} |
|
1937 |
||
1938 |
/* |
|
1939 |
* make it real counts |
|
1940 |
*/ |
|
1941 |
for (index=0; index<256; index++) { |
|
1942 |
_ArrayInstPtr(counts)->a_element[index] = _MKSMALLINT(icounts[index]); |
|
1943 |
} |
|
1944 |
RETURN ( counts ); |
|
1945 |
} |
|
1946 |
%} |
|
1947 |
. |
|
1948 |
self primitiveFailed |
|
1949 |
||
1950 |
" |
|
1951 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
|
1952 |
" |
|
1953 |
! |
|
1954 |
||
563 | 1955 |
usedValues |
1956 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
1957 |
This is needed specially in the bitmap/Imageclasses to find used colors |
|
1958 |
in an image." |
|
550 | 1959 |
|
563 | 1960 |
|result l| |
1961 |
||
1962 |
%{ /* STACK: 400 */ |
|
550 | 1963 |
|
1964 |
REGISTER unsigned char *cp; |
|
563 | 1965 |
REGISTER int len; |
1966 |
unsigned char flags[256]; |
|
1967 |
static struct inlineCache nw = _ILC1; |
|
550 | 1968 |
|
1969 |
if (__qClass(self) == ByteArray) { |
|
563 | 1970 |
memset(flags, 0, sizeof(flags)); |
550 | 1971 |
len = __qSize(self) - OHDR_SIZE; |
1972 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]); |
|
563 | 1973 |
|
1974 |
/* for each used byte, set flag */ |
|
1975 |
while (len > 0) { |
|
1976 |
flags[*cp] = 1; |
|
550 | 1977 |
cp++; |
563 | 1978 |
len--; |
550 | 1979 |
} |
563 | 1980 |
/* count 1's */ |
1981 |
len = 0; |
|
1982 |
for (cp=flags+255; cp >= flags; cp--) |
|
1983 |
if (*cp) len++; |
|
1984 |
||
1985 |
/* create ByteArray of used values */ |
|
1986 |
result = (*nw.ilc_func)(ByteArray, |
|
1987 |
@symbol(new:), |
|
1988 |
CON_COMMA nil, &nw, |
|
1989 |
_MKSMALLINT(len)); |
|
1990 |
if (__Class(result) == ByteArray) { |
|
1991 |
cp = &(_ByteArrayInstPtr(result)->ba_element[0]); |
|
1992 |
for (len=0; len < 256; len++) { |
|
1993 |
if (flags[len]) |
|
1994 |
*cp++ = len; |
|
1995 |
} |
|
1996 |
} |
|
1997 |
RETURN ( result ); |
|
550 | 1998 |
} |
563 | 1999 |
%} |
2000 |
. |
|
2001 |
^ self asSet |
|
550 | 2002 |
|
2003 |
" |
|
563 | 2004 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2005 |
" |
2006 |
! ! |
|
2007 |
||
628 | 2008 |
!ByteArray class methodsFor:'documentation'! |
2009 |
||
2010 |
version |
|
829
fc386319f41c
removed external function defs (soon no longer allowed)
Claus Gittinger <cg@exept.de>
parents:
788
diff
changeset
|
2011 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.41 1996-01-04 01:22:53 cg Exp $' |
628 | 2012 |
! ! |