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
|
1
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
155
|
17 |
category:'Collections-Arrayed'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
ByteArray comment:'
|
88
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
22 |
All Rights Reserved
|
92
|
23 |
|
249
|
24 |
$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.22 1995-02-15 10:21:21 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!ByteArray class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
249
|
45 |
$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.22 1995-02-15 10:21:21 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
1
|
48 |
|
88
|
49 |
documentation
|
|
50 |
"
|
|
51 |
ByteArrays store integers in the range 0..255.
|
129
|
52 |
In contrast to normal arrays (which store pointers to their elements),
|
|
53 |
byteArrays store the values in a dens & compact way. ByteArrays can
|
|
54 |
be used to hold the data for bitmaps, images and other bulk data.
|
|
55 |
ByteArrays are also used to store the bytecode-instructions of an
|
|
56 |
interpreted method and are used as superclass for Strings.
|
|
57 |
|
|
58 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants
|
|
59 |
as #[ element1 element2 .... elementN] and also use byteArray constants
|
|
60 |
as elements in a constant array.
|
|
61 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3])
|
|
62 |
|
|
63 |
If you have to communicate structure-data (in the C-sense) with external
|
|
64 |
programs/data-bases, see a companion class (Structure) in the goodies directory.
|
|
65 |
It allows the definition of subclasses of ByteArray, which transparently fetch
|
|
66 |
and store C-structure fields.
|
88
|
67 |
"
|
|
68 |
! !
|
1
|
69 |
|
3
|
70 |
!ByteArray class methodsFor:'queries'!
|
|
71 |
|
|
72 |
isBuiltInClass
|
|
73 |
"this class is known by the run-time-system"
|
|
74 |
|
|
75 |
^ self == ByteArray
|
|
76 |
! !
|
|
77 |
|
2
|
78 |
!ByteArray class methodsFor: 'binary storage'!
|
|
79 |
|
|
80 |
binaryDefinitionFrom: stream manager: manager
|
54
|
81 |
"get a ByteArray from the binary stream.
|
|
82 |
ByteArrays are stored as 4-byte size, followed by the bytes"
|
|
83 |
|
|
84 |
"take care of subclasses ..."
|
|
85 |
self == ByteArray ifTrue:[
|
155
|
86 |
^ (stream next: (stream nextNumber: 4))
|
54
|
87 |
].
|
|
88 |
^ super binaryDefinitionFrom: stream manager: manager
|
2
|
89 |
! !
|
|
90 |
|
|
91 |
!ByteArray methodsFor: 'binary storage'!
|
|
92 |
|
|
93 |
storeBinaryDefinitionOn: stream manager: manager
|
37
|
94 |
"append a binary representation of the receiver onto stream.
|
|
95 |
Redefined since ByteArrays are stored with a special type code and
|
|
96 |
in a more compact way."
|
|
97 |
|
|
98 |
"not, if I have named instance variables"
|
|
99 |
self class instSize ~~ 0 ifTrue:[
|
155
|
100 |
^ super storeBinaryDefinitionOn: stream manager: manager
|
37
|
101 |
].
|
2
|
102 |
manager putIdOf: self class on: stream.
|
|
103 |
stream nextNumber: 4 put: self basicSize.
|
37
|
104 |
stream nextPutAll: self asByteArray
|
2
|
105 |
! !
|
|
106 |
|
1
|
107 |
!ByteArray class methodsFor:'instance creation'!
|
|
108 |
|
|
109 |
uninitializedNew:anInteger
|
|
110 |
"return a new instance of the receiver with uninitialized
|
|
111 |
(i.e. undefined) contents. The indexed elements have any random
|
75
|
112 |
value. However, any named instance variables are still nilled.
|
|
113 |
For use, when contents will be set anyway shortly after - this
|
|
114 |
is a bit faster than the regular basicNew:, which clears the bytes.
|
|
115 |
Of course, it only makes a difference for very big ByteArrays, such
|
|
116 |
as used for images/bitmaps.
|
|
117 |
|
|
118 |
Notice: if you want to port code using uninitializedNew: to another
|
|
119 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling
|
|
120 |
method to the ByteArray class of the other smalltalk."
|
1
|
121 |
|
|
122 |
%{ /* NOCONTEXT */
|
|
123 |
OBJ newobj;
|
|
124 |
INT instsize, nInstVars, nindexedinstvars;
|
|
125 |
REGISTER OBJ *op;
|
|
126 |
extern OBJ new();
|
|
127 |
|
249
|
128 |
if (__isSmallInteger(anInteger)) {
|
155
|
129 |
nindexedinstvars = _intVal(anInteger);
|
|
130 |
if (nindexedinstvars >= 0) {
|
249
|
131 |
if (self == ByteArray) {
|
|
132 |
/*
|
|
133 |
* the most common case
|
|
134 |
*/
|
|
135 |
instsize = OHDR_SIZE + nindexedinstvars;
|
|
136 |
if (_CanDoQuickNew(instsize)) {
|
|
137 |
_qCheckedNew(newobj, instsize);
|
|
138 |
_InstPtr(newobj)->o_class = self;
|
|
139 |
RETURN (newobj );
|
155
|
140 |
}
|
249
|
141 |
}
|
|
142 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
|
|
143 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars;
|
|
144 |
PROTECT_CONTEXT
|
|
145 |
_qNew(newobj, instsize, SENDER);
|
|
146 |
UNPROTECT_CONTEXT
|
|
147 |
if (newobj) {
|
|
148 |
_InstPtr(newobj)->o_class = self;
|
|
149 |
if (nInstVars) {
|
|
150 |
/*
|
|
151 |
* still have to nil out named instvars ...
|
|
152 |
*/
|
|
153 |
#if defined(FAST_OBJECT_MEMSET4)
|
|
154 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
|
1
|
155 |
#else
|
249
|
156 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES)
|
|
157 |
/*
|
|
158 |
* knowing that nil is 0
|
|
159 |
*/
|
|
160 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
161 |
# else
|
|
162 |
op = _InstPtr(newobj)->i_instvars;
|
|
163 |
while (nInstVars--)
|
|
164 |
*op++ = nil;
|
|
165 |
# endif
|
1
|
166 |
#endif
|
155
|
167 |
}
|
249
|
168 |
RETURN ( newobj );
|
155
|
169 |
}
|
|
170 |
}
|
1
|
171 |
}
|
|
172 |
%}
|
|
173 |
.
|
12
|
174 |
(anInteger isMemberOf:SmallInteger) ifTrue:[
|
155
|
175 |
(anInteger < 0) ifTrue:[
|
|
176 |
^ self error:'bad (negative) argument to new'
|
|
177 |
].
|
|
178 |
"
|
|
179 |
memory allocation failed.
|
|
180 |
When we arrive here, there was no memory, even after
|
|
181 |
a garbage collect. This means, that the VM wanted to
|
|
182 |
get some more memory from the Operatingsystem, which
|
|
183 |
was not kind enough to give some.
|
|
184 |
"
|
|
185 |
^ ObjectMemory allocationFailureSignal raise.
|
12
|
186 |
].
|
1
|
187 |
^ self basicNew:anInteger
|
|
188 |
! !
|
|
189 |
|
|
190 |
!ByteArray methodsFor:'accessing'!
|
|
191 |
|
|
192 |
basicAt:index
|
|
193 |
"return the indexed instance variable with index, anInteger
|
2
|
194 |
- redefined here to be slighly faster than the default in Object"
|
1
|
195 |
|
|
196 |
%{ /* NOCONTEXT */
|
|
197 |
|
|
198 |
REGISTER int indx;
|
|
199 |
int nIndex;
|
54
|
200 |
OBJ cls;
|
1
|
201 |
|
249
|
202 |
if (__isSmallInteger(index)) {
|
155
|
203 |
indx = _intVal(index);
|
249
|
204 |
if (indx > 0) {
|
|
205 |
if ((cls = _qClass(self)) != ByteArray)
|
|
206 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
207 |
nIndex = __byteArraySize(self);
|
|
208 |
if (indx <= nIndex) {
|
|
209 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
|
|
210 |
}
|
155
|
211 |
}
|
1
|
212 |
}
|
|
213 |
%}
|
|
214 |
.
|
|
215 |
^ super basicAt:index
|
|
216 |
!
|
|
217 |
|
|
218 |
basicAt:index put:value
|
|
219 |
"set the indexed instance variable with index, anInteger to value
|
2
|
220 |
- redefined here to be slighly faster than the default in Object"
|
1
|
221 |
|
|
222 |
%{ /* NOCONTEXT */
|
|
223 |
|
|
224 |
REGISTER int indx;
|
|
225 |
int nIndex;
|
|
226 |
int val;
|
54
|
227 |
OBJ cls;
|
1
|
228 |
|
249
|
229 |
if (__bothSmallInteger(index, value)) {
|
155
|
230 |
val = _intVal(value);
|
216
|
231 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
|
155
|
232 |
indx = _intVal(index);
|
249
|
233 |
if (indx > 0) {
|
|
234 |
if ((cls = _qClass(self)) != ByteArray)
|
|
235 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
236 |
nIndex = __byteArraySize(self);
|
|
237 |
if (indx <= nIndex) {
|
|
238 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
|
|
239 |
RETURN ( value );
|
|
240 |
}
|
155
|
241 |
}
|
|
242 |
}
|
1
|
243 |
}
|
|
244 |
%}
|
|
245 |
.
|
|
246 |
^ super basicAt:index put:value
|
2
|
247 |
!
|
|
248 |
|
54
|
249 |
byteAt:index
|
249
|
250 |
"return the byte at index.
|
|
251 |
For ByteArray, this is the same as basicAt:;
|
|
252 |
however, for strings or symbols, this returns a numeric byteValue
|
|
253 |
instead of a character."
|
|
254 |
|
|
255 |
%{ /* NOCONTEXT */
|
|
256 |
|
|
257 |
REGISTER int indx;
|
|
258 |
int nIndex;
|
|
259 |
OBJ cls;
|
54
|
260 |
|
249
|
261 |
if (__isSmallInteger(index)) {
|
|
262 |
indx = _intVal(index);
|
|
263 |
if (indx > 0) {
|
|
264 |
if ((cls = _qClass(self)) != ByteArray)
|
|
265 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
266 |
nIndex = __byteArraySize(self);
|
|
267 |
if (indx <= nIndex) {
|
|
268 |
RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|
|
272 |
%}
|
|
273 |
.
|
|
274 |
^ (super basicAt:index) asInteger
|
54
|
275 |
!
|
|
276 |
|
249
|
277 |
byteAt:index put:value
|
|
278 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:.
|
|
279 |
However, for Strings, this expects a byteValue to be stored."
|
|
280 |
|
|
281 |
%{ /* NOCONTEXT */
|
|
282 |
|
|
283 |
REGISTER int indx;
|
|
284 |
int nIndex;
|
|
285 |
int val;
|
|
286 |
OBJ cls;
|
54
|
287 |
|
249
|
288 |
if (__bothSmallInteger(index, value)) {
|
|
289 |
val = _intVal(value);
|
|
290 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
|
|
291 |
indx = _intVal(index);
|
|
292 |
if (indx > 0) {
|
|
293 |
if ((cls = _qClass(self)) != ByteArray)
|
|
294 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
295 |
nIndex = __byteArraySize(self);
|
|
296 |
if (indx <= nIndex) {
|
|
297 |
_ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
|
|
298 |
RETURN ( value );
|
|
299 |
}
|
|
300 |
}
|
|
301 |
}
|
|
302 |
}
|
|
303 |
%}
|
|
304 |
.
|
|
305 |
^ super basicAt:index put:value
|
54
|
306 |
!
|
|
307 |
|
2
|
308 |
wordAt:index
|
|
309 |
"return the 2-bytes starting at index as an (unsigned) Integer.
|
129
|
310 |
The value is retrieved LSB-first (i.e. low 8-bits at lower byte index).
|
|
311 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
|
2
|
312 |
|
|
313 |
%{ /* NOCONTEXT */
|
|
314 |
|
|
315 |
REGISTER int indx;
|
|
316 |
int nIndex;
|
|
317 |
int val;
|
249
|
318 |
OBJ cls;
|
2
|
319 |
|
249
|
320 |
if (__isSmallInteger(index)) {
|
155
|
321 |
indx = _intVal(index);
|
249
|
322 |
if (indx > 0) {
|
|
323 |
if ((cls = _qClass(self)) != ByteArray)
|
|
324 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
325 |
nIndex = __byteArraySize(self);
|
|
326 |
if ((indx+1) <= nIndex) {
|
|
327 |
val = _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
328 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
|
329 |
RETURN ( _MKSMALLINT(val) );
|
|
330 |
}
|
155
|
331 |
}
|
2
|
332 |
}
|
|
333 |
%}
|
|
334 |
.
|
|
335 |
^ ((self at:index+1) * 256) + (self at:index)
|
|
336 |
!
|
|
337 |
|
|
338 |
wordAt:index put:value
|
|
339 |
"set the 2-bytes starting at index from the (unsigned) Integer value.
|
129
|
340 |
The stored value must be in the range 0 .. 16rFFFF.
|
|
341 |
The value is stored LSB-first - i.e. the low 8bits are stored at the
|
|
342 |
lower address.
|
|
343 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)"
|
2
|
344 |
|
|
345 |
%{ /* NOCONTEXT */
|
|
346 |
|
|
347 |
REGISTER int indx;
|
|
348 |
int nIndex;
|
|
349 |
int val;
|
249
|
350 |
OBJ cls;
|
2
|
351 |
|
249
|
352 |
if (__bothSmallInteger(index, value)) {
|
155
|
353 |
indx = _intVal(index);
|
249
|
354 |
if (indx > 0) {
|
|
355 |
if ((cls = _qClass(self)) != ByteArray)
|
|
356 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
357 |
nIndex = __byteArraySize(self);
|
|
358 |
if ((indx+1) <= nIndex) {
|
|
359 |
val = _intVal(value);
|
|
360 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
|
|
361 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
362 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = (val>>8) & 0xFF;
|
|
363 |
RETURN ( value );
|
|
364 |
}
|
155
|
365 |
}
|
|
366 |
}
|
2
|
367 |
}
|
|
368 |
%}
|
|
369 |
.
|
|
370 |
(value < 0) ifTrue:[
|
155
|
371 |
^ self elementBoundsError
|
2
|
372 |
].
|
|
373 |
self at:index put:(value \\ 256).
|
|
374 |
self at:index + 1 put:(value // 256).
|
|
375 |
^ value
|
129
|
376 |
|
|
377 |
"
|
|
378 |
|b|
|
|
379 |
b := ByteArray new:4.
|
|
380 |
b wordAt:1 put:16r0102.
|
|
381 |
b wordAt:3 put:16r0304.
|
|
382 |
b inspect
|
|
383 |
"
|
2
|
384 |
!
|
|
385 |
|
54
|
386 |
signedWordAt:index
|
129
|
387 |
"return the 2-bytes starting at index as a signed Integer.
|
|
388 |
The value is retrieved LSB-first (i.e. low 8-bits at lower byte index)"
|
54
|
389 |
|
|
390 |
|w|
|
|
391 |
|
|
392 |
w := self wordAt:index.
|
|
393 |
(w > 16r7FFF) ifTrue:[
|
155
|
394 |
^ w - 16r10000
|
54
|
395 |
].
|
|
396 |
^ w
|
|
397 |
|
|
398 |
"
|
129
|
399 |
|b|
|
|
400 |
b := ByteArray new:2.
|
|
401 |
b wordAt:1 put:16rFFFF.
|
|
402 |
b signedWordAt:1
|
54
|
403 |
"
|
|
404 |
!
|
|
405 |
|
|
406 |
signedWordAt:index put:value
|
129
|
407 |
"set the 2-bytes starting at index from the signed Integer value.
|
|
408 |
The stored value must be in the range -32768 .. +32676.
|
|
409 |
The value is stored LSB-first (i.e. low 8-bits at lower byte index)"
|
54
|
410 |
|
|
411 |
value > 0 ifTrue:[
|
155
|
412 |
^ self wordAt:index put:value
|
54
|
413 |
].
|
|
414 |
^ self wordAt:index put:(16r10000 + value)
|
129
|
415 |
|
|
416 |
"
|
|
417 |
|b|
|
|
418 |
b := ByteArray new:4.
|
|
419 |
b signedWordAt:1 put:-1.
|
|
420 |
b signedWordAt:3 put:-2.
|
|
421 |
b inspect
|
|
422 |
"
|
54
|
423 |
!
|
|
424 |
|
2
|
425 |
doubleWordAt:index
|
|
426 |
"return the 4-bytes starting at index as an (unsigned) Integer.
|
129
|
427 |
The value is retrieved LSB-first (i.e. low 8-bits at lowest byte-index).
|
|
428 |
question: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)"
|
2
|
429 |
|
|
430 |
%{ /* NOCONTEXT */
|
|
431 |
|
|
432 |
REGISTER int indx;
|
|
433 |
int nIndex;
|
|
434 |
int val;
|
249
|
435 |
OBJ cls;
|
2
|
436 |
|
249
|
437 |
if (__isSmallInteger(index)) {
|
155
|
438 |
indx = _intVal(index);
|
249
|
439 |
if (indx > 0) {
|
|
440 |
if ((cls = _qClass(self)) != ByteArray)
|
|
441 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
442 |
nIndex = __byteArraySize(self);
|
|
443 |
if ((indx+3) <= nIndex) {
|
|
444 |
val = _ByteArrayInstPtr(self)->ba_element[indx+3-1];
|
|
445 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1];
|
|
446 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1];
|
|
447 |
val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
|
|
448 |
if ((val >= 0) && (val <= _MAX_INT)) {
|
|
449 |
RETURN ( _MKSMALLINT(val) );
|
|
450 |
}
|
|
451 |
RETURN ( _MKULARGEINT(val) );
|
155
|
452 |
}
|
|
453 |
}
|
2
|
454 |
}
|
129
|
455 |
/*
|
|
456 |
* fall through in case of any error
|
|
457 |
* it will be handled in wordAt: finally
|
|
458 |
*/
|
2
|
459 |
%}
|
|
460 |
.
|
85
|
461 |
^ ((256 * 256) * (self wordAt:index+2)) + (self wordAt:index)
|
129
|
462 |
|
|
463 |
"
|
|
464 |
|b|
|
|
465 |
|
|
466 |
b := ByteArray withAll:#(1 2 3 4).
|
|
467 |
(b doubleWordAt:1) printStringRadix:16
|
|
468 |
"
|
2
|
469 |
!
|
|
470 |
|
|
471 |
doubleWordAt:index put:value
|
|
472 |
"set the 4-bytes starting at index from the (unsigned) Integer value.
|
129
|
473 |
The value must be in the range 0 to 16rFFFFFFFF.
|
|
474 |
The value is stored LSB-first (i.e. low 8-bits at lowest byte-index).
|
|
475 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)"
|
2
|
476 |
|
|
477 |
|t|
|
|
478 |
|
|
479 |
%{ /* NOCONTEXT */
|
|
480 |
|
|
481 |
REGISTER int indx;
|
|
482 |
int nIndex;
|
|
483 |
int val;
|
249
|
484 |
OBJ cls;
|
2
|
485 |
|
249
|
486 |
if (__bothSmallInteger(index, value)) {
|
155
|
487 |
val = _intVal(value);
|
|
488 |
if (val >= 0) {
|
|
489 |
indx = _intVal(index);
|
249
|
490 |
if (indx > 0) {
|
|
491 |
if ((cls = _qClass(self)) != ByteArray)
|
|
492 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
493 |
nIndex = _qSize(self) - OHDR_SIZE;
|
|
494 |
if ((indx+3) <= nIndex) {
|
|
495 |
_ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
|
|
496 |
val >>= 8;
|
|
497 |
_ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
|
|
498 |
val >>= 8;
|
|
499 |
_ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF;
|
|
500 |
val >>= 8;
|
|
501 |
_ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF;
|
|
502 |
RETURN ( value );
|
|
503 |
}
|
155
|
504 |
}
|
|
505 |
}
|
2
|
506 |
}
|
|
507 |
%}
|
|
508 |
.
|
|
509 |
(value < 0) ifTrue:[
|
155
|
510 |
^ self elementBoundsError
|
2
|
511 |
].
|
|
512 |
t := value // (256 * 256).
|
|
513 |
self wordAt:(index+2) put:t.
|
85
|
514 |
self wordAt:(index) put:(value - (256 * 256 * t)).
|
2
|
515 |
^ value
|
129
|
516 |
|
|
517 |
"
|
|
518 |
|b|
|
|
519 |
b := ByteArray new:4.
|
|
520 |
b doubleWordAt:1 put:16r04030201.
|
|
521 |
b inspect
|
|
522 |
"
|
54
|
523 |
!
|
|
524 |
|
|
525 |
signedDoubleWordAt:index
|
129
|
526 |
"return the 4-bytes starting at index as a signed Integer.
|
|
527 |
The value is retrieved LSB-first (i.e. low 8-bits at lowest byte-index)."
|
54
|
528 |
|
|
529 |
|w|
|
|
530 |
|
129
|
531 |
"
|
|
532 |
stupid: due to an STC bug, cannot currently have LargeInteger
|
|
533 |
constants - change to code below when fixed ...
|
|
534 |
"
|
54
|
535 |
w := self doubleWordAt:index.
|
|
536 |
(w > (16r7FFFFFF * 16r10 + 16rF)) ifTrue:[
|
155
|
537 |
^ w - (16r10000000 * 16r10)
|
54
|
538 |
].
|
|
539 |
^ w
|
|
540 |
"
|
|
541 |
w := self doubleWordAt:index.
|
|
542 |
(w > 16r7FFFFFFF) ifTrue:[
|
155
|
543 |
^ w - 16r100000000
|
54
|
544 |
].
|
|
545 |
^ w
|
|
546 |
"
|
|
547 |
|
|
548 |
"
|
129
|
549 |
|b|
|
|
550 |
b := ByteArray new:4.
|
|
551 |
b doubleWordAt:1 put:16rFFFFFFFF.
|
|
552 |
(b signedDoubleWordAt:1)
|
54
|
553 |
"
|
|
554 |
!
|
|
555 |
|
|
556 |
signedDoubleWordAt:index put:value
|
129
|
557 |
"set the 4-bytes starting at index from the signed Integer value.
|
|
558 |
The value is stored LSB-first (i.e. low 8-bits at lowest byte-index)."
|
54
|
559 |
|
|
560 |
value > 0 ifTrue:[
|
155
|
561 |
^ self doubleWordAt:index put:value
|
54
|
562 |
].
|
129
|
563 |
"
|
|
564 |
stupid: due to an STC bug, cannot currently have LargeInteger
|
|
565 |
constants - change to code below when fixed ...
|
|
566 |
"
|
54
|
567 |
^ self doubleWordAt:index put:((16r10000000 * 16r10) + value)
|
|
568 |
"
|
|
569 |
^ self doubleWordAt:index put:(16r100000000 + value)
|
|
570 |
"
|
129
|
571 |
"
|
|
572 |
|b|
|
|
573 |
b := ByteArray new:4.
|
|
574 |
b signedDoubleWordAt:1 put:-1.
|
|
575 |
(b doubleWordAt:1) printStringRadix:16
|
|
576 |
"
|
|
577 |
!
|
54
|
578 |
|
129
|
579 |
|
|
580 |
quadWordAt:index
|
|
581 |
"return the 8-bytes starting at index as an (unsigned) Integer.
|
|
582 |
The value is retrieved LSB-first (i.e. low 8-bits at lowest byte-index)."
|
|
583 |
|
|
584 |
|l bIdx "{ Class: SmallInteger }"|
|
|
585 |
|
|
586 |
l := LargeInteger basicNew numberOfDigits:8.
|
|
587 |
bIdx := index.
|
|
588 |
1 to:8 do:[:i |
|
155
|
589 |
l digitAt:i put:(self basicAt:bIdx).
|
|
590 |
bIdx := bIdx + 1
|
129
|
591 |
].
|
|
592 |
^ l normalize
|
|
593 |
|
|
594 |
"
|
|
595 |
|b|
|
|
596 |
|
|
597 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8).
|
|
598 |
(b quadWordAt:1) printStringRadix:16
|
|
599 |
"
|
|
600 |
!
|
|
601 |
|
|
602 |
quadWordAt:index put:anInteger
|
|
603 |
"set the 8-bytes starting at index from the (unsigned) Integer value.
|
|
604 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF.
|
|
605 |
The value is stored LSB-first (i.e. low 8-bits at lowest byte-index)."
|
|
606 |
|
|
607 |
|bIdx "{ Class: SmallInteger }"|
|
|
608 |
|
|
609 |
bIdx := index.
|
|
610 |
1 to:8 do:[:i |
|
155
|
611 |
self basicAt:bIdx put:(anInteger digitAt:i).
|
|
612 |
bIdx := bIdx + 1.
|
129
|
613 |
]
|
|
614 |
|
|
615 |
"
|
|
616 |
|b|
|
|
617 |
b := ByteArray new:8.
|
|
618 |
b quadWordAtIndex:1 put:16r0807060504030201.
|
|
619 |
b inspect
|
|
620 |
"
|
54
|
621 |
!
|
|
622 |
|
|
623 |
floatAt:index
|
|
624 |
"return the 4-bytes starting at index as a Float.
|
129
|
625 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80;
|
|
626 |
therefore this method reads a 4-byte float from the byteArray and returns
|
|
627 |
a float object which keeps an 8-byte double internally.
|
54
|
628 |
Notice also, that the bytes are expected to be in this machines
|
129
|
629 |
float representation and order - if the bytearray originated from another
|
54
|
630 |
machine, some conversion is usually needed."
|
|
631 |
|
|
632 |
|newFloat|
|
|
633 |
|
|
634 |
newFloat := Float basicNew.
|
77
|
635 |
UninterpretedBytes isBigEndian ifFalse:[
|
155
|
636 |
5 to:8 do:[:destIndex|
|
|
637 |
newFloat basicAt:destIndex put:(self at:index - 5 + destIndex)
|
|
638 |
].
|
77
|
639 |
] ifTrue:[
|
155
|
640 |
1 to:4 do:[:destIndex|
|
|
641 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
|
|
642 |
].
|
54
|
643 |
].
|
|
644 |
^ newFloat.
|
|
645 |
!
|
|
646 |
|
|
647 |
floatAt:index put:aFloat
|
|
648 |
"store the 4 bytes of value of the argument, aFloat into the receiver
|
|
649 |
starting at index.
|
|
650 |
Notice, that (currently) ST/X Floats are what DOubles are in ST-80.
|
|
651 |
Notice also, that the bytes are expected to be in this machines
|
|
652 |
float representation - if the bytearray originated from another
|
|
653 |
machine, some conversion is usually needed."
|
|
654 |
|
77
|
655 |
UninterpretedBytes isBigEndian ifFalse:[
|
155
|
656 |
5 to:8 do:[:srcIndex|
|
|
657 |
self at:index - 5 + srcIndex put:(aFloat basicAt:srcIndex)
|
|
658 |
].
|
77
|
659 |
] ifTrue:[
|
155
|
660 |
1 to:4 do:[:srcIndex|
|
|
661 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex)
|
|
662 |
].
|
54
|
663 |
].
|
|
664 |
^ aFloat
|
|
665 |
!
|
|
666 |
|
|
667 |
doubleAt:index
|
|
668 |
"return the 8-bytes starting at index as a Float.
|
|
669 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
|
|
670 |
Notice also, that the bytes are expected to be in this machines
|
|
671 |
float representation - if the bytearray originated from another
|
|
672 |
machine, some conversion is usually needed."
|
|
673 |
|
|
674 |
|newFloat|
|
|
675 |
|
|
676 |
newFloat := Float basicNew.
|
|
677 |
1 to:8 do:[:destIndex|
|
155
|
678 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
|
54
|
679 |
].
|
|
680 |
^ newFloat.
|
|
681 |
!
|
|
682 |
|
|
683 |
doubleAt:index put:aFloat
|
|
684 |
"store the value of the argument, aFloat into the receiver
|
|
685 |
starting at index.
|
|
686 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
|
|
687 |
Notice also, that the bytes are expected to be in this machines
|
|
688 |
float representation - if the bytearray originated from another
|
|
689 |
machine, some conversion is usually needed."
|
|
690 |
|
|
691 |
1 to:8 do:[:srcIndex|
|
155
|
692 |
self at:index - 1 + srcIndex put:(aFloat basicAt:srcIndex)
|
54
|
693 |
].
|
|
694 |
^ aFloat
|
129
|
695 |
!
|
|
696 |
|
|
697 |
ieeFloatAt:index
|
|
698 |
"retrieve the 4 bytes starting at index as a float.
|
|
699 |
The 4 bytes are assumed to be in IEE floating point single precision
|
|
700 |
number format."
|
|
701 |
|
|
702 |
"
|
|
703 |
currently, we assume that the machines native number format is already
|
|
704 |
IEE format - we need some more code here whenever ST/X is ported
|
|
705 |
to an IBM 370 or old VAX etc.
|
|
706 |
To date, all supported systems use IEE float numbers, so there should be
|
|
707 |
no problem.
|
|
708 |
"
|
|
709 |
^ self floatAt:index
|
|
710 |
!
|
|
711 |
|
|
712 |
ieeFloatAt:index put:aFloat
|
|
713 |
"store the value of the argument, aFloat into the receiver
|
|
714 |
starting at index. Storage is in IEE floating point single precision format.
|
|
715 |
(i.e. 4 bytes are stored). Since ST/X floats are really doubles, the low-
|
|
716 |
order 4 bytes of the precision is lost."
|
|
717 |
|
|
718 |
"
|
|
719 |
currently, we assume that the machines native number format is already
|
|
720 |
IEE format - we need some more code here whenever ST/X is ported
|
|
721 |
to an IBM 370 or old VAX etc.
|
|
722 |
To date, all supported systems use IEE float numbers, so there should be
|
|
723 |
no problem.
|
|
724 |
"
|
|
725 |
^ self floatAt:index put:aFloat
|
|
726 |
!
|
|
727 |
|
|
728 |
ieeDoubleAt:index
|
|
729 |
"retrieve the 8 bytes starting at index as a float.
|
|
730 |
The 8 bytes are assumed to be in IEE floating point single precision
|
|
731 |
number format."
|
|
732 |
|
|
733 |
"
|
|
734 |
currently, we assume that the machines native number format is already
|
|
735 |
IEE format - we need some more code here whenever ST/X is ported
|
|
736 |
to an IBM 370 or old VAX etc.
|
|
737 |
To date, all supported systems use IEE float numbers, so there should be
|
|
738 |
no problem.
|
|
739 |
"
|
|
740 |
^ self doubleAt:index
|
|
741 |
!
|
|
742 |
|
|
743 |
ieeDoubleAt:index put:aFloat
|
|
744 |
"store the value of the argument, aFloat into the receiver
|
|
745 |
starting at index. Storage is in IEE floating point double precision format.
|
|
746 |
(i.e. 8 bytes are stored)."
|
|
747 |
|
|
748 |
"
|
|
749 |
currently, we assume that the machines native number format is already
|
|
750 |
IEE format - we need some more code here whenever ST/X is ported
|
|
751 |
to an IBM 370 or old VAX etc.
|
|
752 |
To date, all supported systems use IEE float numbers, so there should be
|
|
753 |
no problem.
|
|
754 |
"
|
|
755 |
^ self doubleAt:index put:aFloat
|
1
|
756 |
! !
|
|
757 |
|
12
|
758 |
!ByteArray methodsFor:'converting'!
|
|
759 |
|
|
760 |
asByteArray
|
|
761 |
"return the receiver as a byteArray"
|
|
762 |
|
|
763 |
"could be an instance of a subclass..."
|
|
764 |
self class == ByteArray ifTrue:[
|
155
|
765 |
^ self
|
12
|
766 |
].
|
|
767 |
^ super asByteArray
|
129
|
768 |
|
|
769 |
"
|
|
770 |
'hello world' asByteArray
|
|
771 |
#(1 2 3 4 5 6 7) asByteArray
|
|
772 |
#(1 2 256 4 5 6 7) asByteArray
|
|
773 |
"
|
12
|
774 |
! !
|
|
775 |
|
|
776 |
!ByteArray methodsFor:'printing & storing'!
|
|
777 |
|
|
778 |
isLiteral
|
37
|
779 |
"return true, if the receiver can be used as a literal
|
|
780 |
(i.e. can be used in constant arrays)"
|
12
|
781 |
|
|
782 |
^ true
|
|
783 |
!
|
|
784 |
|
|
785 |
storeOn:aStream
|
54
|
786 |
"append a printed representation from which the receiver can be
|
|
787 |
reconstructed to aStream. (reimplemented to make it look better)"
|
|
788 |
|
|
789 |
|first|
|
|
790 |
|
|
791 |
self class == ByteArray ifTrue:[
|
155
|
792 |
aStream nextPutAll:'#['.
|
|
793 |
first := true.
|
|
794 |
self do:[:byte |
|
|
795 |
first ifFalse:[aStream space]
|
|
796 |
ifTrue:[first := false].
|
|
797 |
byte storeOn:aStream.
|
|
798 |
].
|
|
799 |
aStream nextPutAll:']'.
|
|
800 |
^ self
|
54
|
801 |
].
|
|
802 |
^ super storeOn:aStream
|
68
|
803 |
|
129
|
804 |
"
|
|
805 |
#[1 2 3 4 5] storeOn:Transcript
|
|
806 |
"
|
12
|
807 |
! !
|
|
808 |
|
1
|
809 |
!ByteArray methodsFor:'queries'!
|
|
810 |
|
|
811 |
indexOf:aByte startingAt:start
|
|
812 |
"return the index of the first occurrence of the argument, aByte
|
|
813 |
in the receiver starting at start, anInteger; return 0 if not found.
|
|
814 |
- reimplemented here for speed"
|
|
815 |
|
|
816 |
%{ /* NOCONTEXT */
|
|
817 |
|
|
818 |
REGISTER unsigned char *cp;
|
|
819 |
REGISTER int index, byteValue;
|
|
820 |
REGISTER int len;
|
249
|
821 |
OBJ cls;
|
1
|
822 |
|
249
|
823 |
if (! __isSmallInteger(aByte)) {
|
216
|
824 |
/*
|
|
825 |
* searching for something which cannot be found
|
|
826 |
*/
|
155
|
827 |
RETURN ( _MKSMALLINT(0) );
|
1
|
828 |
}
|
|
829 |
|
|
830 |
byteValue = _intVal(aByte);
|
|
831 |
|
216
|
832 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) {
|
|
833 |
/*
|
|
834 |
* searching for something which cannot be found
|
|
835 |
*/
|
155
|
836 |
RETURN ( _MKSMALLINT(0) );
|
1
|
837 |
}
|
|
838 |
|
249
|
839 |
if (__isSmallInteger(start)) {
|
155
|
840 |
index = _intVal(start);
|
249
|
841 |
len = __byteArraySize(self);
|
|
842 |
cp = _ByteArrayInstPtr(self)->ba_element;
|
|
843 |
if ((cls = _qClass(self)) != ByteArray) {
|
155
|
844 |
int nInst;
|
2
|
845 |
|
249
|
846 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
155
|
847 |
cp += nInst;
|
|
848 |
len -= nInst;
|
|
849 |
}
|
|
850 |
cp += index - 1;
|
|
851 |
while (index <= len) {
|
|
852 |
if (*cp == byteValue) {
|
|
853 |
RETURN ( _MKSMALLINT(index) );
|
|
854 |
}
|
|
855 |
index++;
|
|
856 |
cp++;
|
|
857 |
}
|
|
858 |
RETURN ( _MKSMALLINT(0) );
|
1
|
859 |
}
|
|
860 |
%}
|
|
861 |
.
|
|
862 |
^ super indexOf:aByte startingAt:start
|
129
|
863 |
|
|
864 |
"
|
|
865 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0
|
|
866 |
"
|
1
|
867 |
!
|
|
868 |
|
|
869 |
usedValues
|
|
870 |
"return a new ByteArray with all used values (actually a kind of Set);
|
129
|
871 |
This is needed specially in the bitmap/Imageclasses to find used colors
|
|
872 |
in an image."
|
1
|
873 |
|
|
874 |
|result l|
|
12
|
875 |
|
|
876 |
%{ /* STACK: 400 */
|
|
877 |
|
1
|
878 |
REGISTER unsigned char *cp;
|
|
879 |
REGISTER int len;
|
|
880 |
unsigned char flags[256];
|
|
881 |
static struct inlineCache nw = _ILC1;
|
213
|
882 |
extern OBJ ByteArray;
|
1
|
883 |
|
|
884 |
if (_qClass(self) == ByteArray) {
|
155
|
885 |
memset(flags, 0, sizeof(flags));
|
|
886 |
len = _qSize(self) - OHDR_SIZE;
|
|
887 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
1
|
888 |
|
155
|
889 |
/* for each used byte, set flag */
|
|
890 |
while (len > 0) {
|
|
891 |
flags[*cp] = 1;
|
|
892 |
cp++;
|
|
893 |
len--;
|
|
894 |
}
|
|
895 |
/* count 1's */
|
|
896 |
len = 0;
|
|
897 |
for (cp=flags+255; cp >= flags; cp--)
|
|
898 |
if (*cp) len++;
|
1
|
899 |
|
155
|
900 |
/* create ByteArray of used values */
|
|
901 |
result = (*nw.ilc_func)(ByteArray,
|
|
902 |
@symbol(new:),
|
|
903 |
CON_COMMA nil, &nw,
|
|
904 |
_MKSMALLINT(len));
|
|
905 |
if (_Class(result) == ByteArray) {
|
|
906 |
cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
|
|
907 |
for (len=0; len < 256; len++) {
|
|
908 |
if (flags[len])
|
|
909 |
*cp++ = len;
|
|
910 |
}
|
|
911 |
}
|
|
912 |
RETURN ( result );
|
1
|
913 |
}
|
|
914 |
%}
|
|
915 |
.
|
54
|
916 |
^ self asSet
|
129
|
917 |
|
|
918 |
"
|
|
919 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues
|
|
920 |
"
|
1
|
921 |
!
|
|
922 |
|
129
|
923 |
|
1
|
924 |
usageCounts
|
|
925 |
"return an array filled with value-counts -
|
129
|
926 |
This is needed in the bitmap/image classes to get info on color usage.
|
|
927 |
(i.e. to build up a histogram of color usage within an image)."
|
1
|
928 |
|
|
929 |
|counts|
|
|
930 |
|
|
931 |
counts := Array new:256.
|
12
|
932 |
|
|
933 |
%{ /* STACK: 2000 */
|
|
934 |
|
1
|
935 |
REGISTER unsigned char *cp;
|
|
936 |
REGISTER int nByte;
|
|
937 |
REGISTER int index;
|
|
938 |
int icounts[256];
|
|
939 |
|
54
|
940 |
if ((_qClass(self) == ByteArray) && __isArray(counts)) {
|
155
|
941 |
/*
|
|
942 |
* zero counts
|
|
943 |
*/
|
|
944 |
for (index=0; index<256; index++) {
|
|
945 |
icounts[index] = 0;
|
|
946 |
}
|
1
|
947 |
|
155
|
948 |
/*
|
|
949 |
* count
|
|
950 |
*/
|
|
951 |
nByte = _qSize(self) - OHDR_SIZE;
|
|
952 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
953 |
while (nByte--) {
|
|
954 |
icounts[*cp++]++;
|
|
955 |
}
|
1
|
956 |
|
155
|
957 |
/*
|
|
958 |
* make it real counts
|
|
959 |
*/
|
|
960 |
for (index=0; index<256; index++) {
|
|
961 |
_ArrayInstPtr(counts)->a_element[index] = _MKSMALLINT(icounts[index]);
|
|
962 |
}
|
|
963 |
RETURN ( counts );
|
1
|
964 |
}
|
|
965 |
%}
|
|
966 |
.
|
|
967 |
self primitiveFailed
|
129
|
968 |
|
|
969 |
"
|
|
970 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts
|
|
971 |
"
|
1
|
972 |
!
|
|
973 |
|
155
|
974 |
max
|
1
|
975 |
"return the maximum value in the receiver -
|
155
|
976 |
redefined to speedup image processing and sound-player
|
|
977 |
(which need a fast method for this on byteArrays)"
|
1
|
978 |
|
|
979 |
%{ /* NOCONTEXT */
|
|
980 |
|
|
981 |
REGISTER unsigned char *cp;
|
|
982 |
REGISTER int index, max;
|
|
983 |
int len;
|
|
984 |
|
|
985 |
if (_qClass(self) == ByteArray) {
|
155
|
986 |
max = 0;
|
|
987 |
index = 0;
|
|
988 |
len = _qSize(self) - OHDR_SIZE;
|
|
989 |
cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
|
|
990 |
while (++index <= len) {
|
|
991 |
if (*cp > max) max = *cp;
|
|
992 |
cp++;
|
|
993 |
}
|
|
994 |
RETURN ( _MKSMALLINT(max) );
|
1
|
995 |
}
|
155
|
996 |
%}.
|
|
997 |
^ super max
|
129
|
998 |
|
|
999 |
"
|
155
|
1000 |
#[1 2 3 1 2 3 1 2 19] max
|
129
|
1001 |
"
|
1
|
1002 |
! !
|
|
1003 |
|
92
|
1004 |
!ByteArray methodsFor:'copying'!
|
|
1005 |
|
|
1006 |
copyFrom:start to:stop
|
|
1007 |
"return the subcollection starting at index start, anInteger and ending
|
|
1008 |
at stop, anInteger.
|
|
1009 |
- reimplemented here for speed"
|
|
1010 |
|
|
1011 |
%{ /* NOCONTEXT */
|
|
1012 |
|
|
1013 |
#if !defined(FAST_MEMCPY)
|
|
1014 |
REGISTER unsigned char *srcp;
|
|
1015 |
#endif
|
|
1016 |
REGISTER unsigned char *dstp;
|
|
1017 |
REGISTER int count;
|
|
1018 |
int len, index1, index2, sz;
|
|
1019 |
OBJ newByteArray;
|
|
1020 |
|
|
1021 |
if (__isByteArray(self)
|
249
|
1022 |
&& __bothSmallInteger(start, stop)) {
|
|
1023 |
len = __byteArraySize(self);
|
155
|
1024 |
index1 = _intVal(start);
|
|
1025 |
index2 = _intVal(stop);
|
92
|
1026 |
|
155
|
1027 |
if ((index1 <= index2) && (index1 > 0)) {
|
|
1028 |
if (index2 <= len) {
|
|
1029 |
count = index2 - index1 + 1;
|
|
1030 |
PROTECT_CONTEXT
|
|
1031 |
sz = OHDR_SIZE + count;
|
|
1032 |
_qNew(newByteArray, sz, SENDER);
|
|
1033 |
UNPROTECT_CONTEXT
|
|
1034 |
if (newByteArray != nil) {
|
|
1035 |
_InstPtr(newByteArray)->o_class = ByteArray;
|
|
1036 |
dstp = _ByteArrayInstPtr(newByteArray)->ba_element;
|
92
|
1037 |
#ifdef FAST_MEMCPY
|
155
|
1038 |
bcopy(_ByteArrayInstPtr(self)->ba_element + index1 - 1, dstp, count);
|
92
|
1039 |
#else
|
155
|
1040 |
srcp = _ByteArrayInstPtr(self)->ba_element + index1 - 1;
|
|
1041 |
while (count--) {
|
|
1042 |
*dstp++ = *srcp++;
|
|
1043 |
}
|
92
|
1044 |
#endif
|
155
|
1045 |
RETURN ( newByteArray );
|
|
1046 |
}
|
|
1047 |
}
|
|
1048 |
}
|
92
|
1049 |
}
|
|
1050 |
%}.
|
|
1051 |
"
|
|
1052 |
fall back in case of non-integer index or out-of-bound index;
|
|
1053 |
will eventually lead to an out-of-bound signal raise
|
|
1054 |
"
|
|
1055 |
^ super copyFrom:start to:stop
|
|
1056 |
! !
|
|
1057 |
|
1
|
1058 |
!ByteArray methodsFor:'filling and replacing'!
|
|
1059 |
|
155
|
1060 |
from:start to:stop put:aNumber
|
|
1061 |
"fill part of the receiver with aNumber.
|
|
1062 |
- reimplemented here for speed"
|
|
1063 |
|
|
1064 |
%{ /* NOCONTEXT */
|
|
1065 |
|
|
1066 |
REGISTER unsigned char *dstp;
|
|
1067 |
REGISTER int count, value;
|
249
|
1068 |
int len, index1, index2;
|
|
1069 |
int nInst;
|
|
1070 |
OBJ cls;
|
155
|
1071 |
|
249
|
1072 |
if (__isSmallInteger(aNumber)
|
|
1073 |
&& __bothSmallInteger(start, stop)) {
|
|
1074 |
len = __byteArraySize(self);
|
155
|
1075 |
index1 = _intVal(start);
|
|
1076 |
index2 = _intVal(stop);
|
|
1077 |
|
249
|
1078 |
dstp = _ByteArrayInstPtr(self)->ba_element + index1 - 1;
|
|
1079 |
if ((cls = _qClass(self)) != ByteArray) {
|
|
1080 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
1081 |
dstp += nInst;
|
|
1082 |
len -= nInst;
|
|
1083 |
}
|
|
1084 |
|
155
|
1085 |
value = _intVal(aNumber);
|
216
|
1086 |
if ((value >= 0) && (value <= 255)
|
155
|
1087 |
&& (index1 <= index2)
|
|
1088 |
&& (index1 > 0)) {
|
|
1089 |
if (index2 <= len) {
|
|
1090 |
count = index2 - index1 + 1;
|
|
1091 |
#ifdef FAST_MEMSET
|
249
|
1092 |
memset(dstp, count, value);
|
155
|
1093 |
#else
|
|
1094 |
while (count--) {
|
|
1095 |
*dstp++ = value;
|
|
1096 |
}
|
|
1097 |
#endif
|
|
1098 |
RETURN (self);
|
|
1099 |
}
|
|
1100 |
}
|
|
1101 |
}
|
|
1102 |
%}.
|
|
1103 |
"
|
|
1104 |
fall back in case of non-integer index or out-of-bound index/value;
|
|
1105 |
will eventually lead to an out-of-bound signal raise
|
|
1106 |
"
|
|
1107 |
^ super from:start to:stop put:aNumber
|
|
1108 |
!
|
|
1109 |
|
1
|
1110 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
249
|
1111 |
"replace elements from another collection"
|
|
1112 |
|
|
1113 |
(aCollection class == self class) ifTrue:[
|
|
1114 |
^ self replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
|
|
1115 |
].
|
|
1116 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
1117 |
!
|
|
1118 |
|
|
1119 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
|
|
1120 |
"replace elements from another collection, which must be a ByteArray
|
|
1121 |
or String."
|
1
|
1122 |
|
54
|
1123 |
%{ /* NOCONTEXT */
|
1
|
1124 |
|
54
|
1125 |
int nIndex, repNIndex;
|
|
1126 |
int startIndex, stopIndex;
|
|
1127 |
REGISTER unsigned char *src;
|
|
1128 |
REGISTER int repStartIndex;
|
|
1129 |
int repStopIndex, count;
|
|
1130 |
REGISTER unsigned char *dst;
|
249
|
1131 |
OBJ cls;
|
|
1132 |
int nInst;
|
1
|
1133 |
|
249
|
1134 |
if (__isBytes(aCollection)
|
|
1135 |
&& __bothSmallInteger(start, stop)
|
|
1136 |
&& __isSmallInteger(repStart)) {
|
155
|
1137 |
startIndex = _intVal(start) - 1;
|
|
1138 |
if (startIndex >= 0) {
|
249
|
1139 |
dst = (_ByteArrayInstPtr(self)->ba_element) + startIndex;
|
|
1140 |
nIndex = __byteArraySize(self);
|
|
1141 |
|
|
1142 |
if ((cls = _qClass(self)) != ByteArray) {
|
|
1143 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
1144 |
dst += nInst;
|
|
1145 |
nIndex -= nInst;
|
|
1146 |
}
|
|
1147 |
|
155
|
1148 |
stopIndex = _intVal(stop) - 1;
|
|
1149 |
count = stopIndex - startIndex + 1;
|
|
1150 |
if (count == 0) {
|
|
1151 |
RETURN ( self );
|
|
1152 |
}
|
|
1153 |
if ((count > 0) && (stopIndex < nIndex)) {
|
|
1154 |
repStartIndex = _intVal(repStart) - 1;
|
|
1155 |
if (repStartIndex >= 0) {
|
|
1156 |
repNIndex = _qSize(aCollection) - OHDR_SIZE;
|
249
|
1157 |
src = (_ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex;
|
|
1158 |
if ((cls = _qClass(aCollection)) != ByteArray) {
|
|
1159 |
nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
1160 |
src += nInst;
|
|
1161 |
repNIndex -= nInst;
|
|
1162 |
}
|
|
1163 |
|
155
|
1164 |
repStopIndex = repStartIndex + (stopIndex - startIndex);
|
|
1165 |
if (repStopIndex < repNIndex) {
|
|
1166 |
if (aCollection == self) {
|
|
1167 |
/* take care of overlapping copy */
|
|
1168 |
if (src < dst) {
|
|
1169 |
/* must do a reverse copy */
|
|
1170 |
src += count;
|
|
1171 |
dst += count;
|
|
1172 |
while (count-- > 0) {
|
|
1173 |
*--dst = *--src;
|
|
1174 |
}
|
|
1175 |
RETURN ( self );
|
|
1176 |
}
|
|
1177 |
}
|
1
|
1178 |
#ifdef FAST_MEMCPY
|
166
|
1179 |
# ifdef bcopy4
|
|
1180 |
if (((src & 3) == 0)
|
|
1181 |
&& ((dst & 3) == 0)) {
|
|
1182 |
/* copy aligned part */
|
|
1183 |
bcopy4(src, dst, count//4);
|
|
1184 |
/* copy rest */
|
|
1185 |
if (count & 3) {
|
|
1186 |
src += (count & ~3);
|
|
1187 |
dst += (count & ~3);
|
|
1188 |
count = count & 3;
|
|
1189 |
while (count--) {
|
|
1190 |
*dst++ = *src++;
|
|
1191 |
}
|
|
1192 |
}
|
|
1193 |
} else
|
|
1194 |
# endif /* bcopy4 */
|
|
1195 |
bcopy(src, dst, count);
|
1
|
1196 |
#else
|
155
|
1197 |
while (count-- > 0) {
|
|
1198 |
*dst++ = *src++;
|
|
1199 |
}
|
1
|
1200 |
#endif
|
155
|
1201 |
RETURN ( self );
|
|
1202 |
}
|
|
1203 |
}
|
|
1204 |
}
|
|
1205 |
}
|
54
|
1206 |
}
|
1
|
1207 |
%}
|
|
1208 |
.
|
249
|
1209 |
^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
|
1
|
1210 |
! !
|
|
1211 |
|
129
|
1212 |
!ByteArray methodsFor:'image manipulation support'!
|
1
|
1213 |
|
|
1214 |
invert
|
|
1215 |
"invert all bytes - used with image manipulations
|
54
|
1216 |
written as a primitive for speed.
|
|
1217 |
Q: is this really needed ?"
|
1
|
1218 |
|
|
1219 |
%{ /* NOCONTEXT */
|
|
1220 |
|
|
1221 |
REGISTER unsigned char *dst;
|
|
1222 |
REGISTER unsigned long *ldst;
|
|
1223 |
REGISTER int cnt;
|
|
1224 |
|
|
1225 |
if (_qClass(self) == ByteArray) {
|
249
|
1226 |
cnt = __byteArraySize(self);
|
155
|
1227 |
dst = _ByteArrayInstPtr(self)->ba_element;
|
|
1228 |
if (! ((int)dst & (sizeof(long)-1))) {
|
|
1229 |
ldst = (unsigned long *)dst;
|
|
1230 |
while (cnt >= sizeof(long)) {
|
|
1231 |
*ldst = ~(*ldst);
|
|
1232 |
ldst++;
|
|
1233 |
cnt -= sizeof(long);
|
|
1234 |
}
|
|
1235 |
dst = (unsigned char *)ldst;
|
|
1236 |
}
|
|
1237 |
while (cnt--) {
|
|
1238 |
*dst = ~(*dst);
|
|
1239 |
dst++;
|
|
1240 |
}
|
|
1241 |
RETURN ( self );
|
1
|
1242 |
}
|
|
1243 |
%}
|
|
1244 |
.
|
|
1245 |
self primitiveFailed
|
|
1246 |
!
|
|
1247 |
|
12
|
1248 |
reverse
|
|
1249 |
"reverse order of elements inplace -
|
129
|
1250 |
written as a primitive for speed on image manipulations (mirror)"
|
12
|
1251 |
|
|
1252 |
%{ /* NOCONTEXT */
|
|
1253 |
|
|
1254 |
REGISTER unsigned char *p1, *p2;
|
|
1255 |
REGISTER int cnt;
|
|
1256 |
REGISTER unsigned t;
|
249
|
1257 |
OBJ cls;
|
12
|
1258 |
|
249
|
1259 |
cnt = __byteArraySize(self);
|
|
1260 |
p1 = _ByteArrayInstPtr(self)->ba_element;
|
|
1261 |
if ((cls = _qClass(self)) != ByteArray) {
|
|
1262 |
t = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
|
|
1263 |
p1 += t;
|
|
1264 |
cnt -= t;
|
12
|
1265 |
}
|
249
|
1266 |
p2 = p1 + cnt - 1;
|
|
1267 |
while (cnt > 0) {
|
|
1268 |
t = *p1;
|
|
1269 |
*p1++ = *p2;
|
|
1270 |
*p2-- = t;
|
|
1271 |
cnt-=2;
|
|
1272 |
}
|
|
1273 |
RETURN ( self );
|
12
|
1274 |
%}
|
|
1275 |
!
|
|
1276 |
|
1
|
1277 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray
|
155
|
1278 |
mapping:aMapByteArray
|
54
|
1279 |
|
1
|
1280 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into
|
|
1281 |
aByteArray with 8-bit pixels. The width/height-arguments are needed
|
|
1282 |
to skip any padded src-bits. On the fly, the destination pixels
|
129
|
1283 |
are translated using aMapByteArray (if non-nil).
|
|
1284 |
Input bits are read left-to right, i.e. the first byte in the output
|
|
1285 |
corresponds to the high bit(s) in the inputs first byte.
|
|
1286 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color
|
|
1287 |
machines. With nBitsPerPixel==8, this is a translate operation.
|
|
1288 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte
|
|
1289 |
value of n is found in map at:(n + 1).
|
|
1290 |
It can also be used to expand bit-arrays into byteArrays.
|
|
1291 |
This method is specialized for ByteArray arguments - it will not handle
|
|
1292 |
anything else."
|
1
|
1293 |
|
|
1294 |
%{ /* NOCONTEXT */
|
|
1295 |
|
54
|
1296 |
REGISTER unsigned char *src, *dst;
|
|
1297 |
REGISTER int wrun;
|
|
1298 |
unsigned char *srcNext;
|
|
1299 |
int bytesPerRow, mask, shift0, shift;
|
|
1300 |
int w, h, hrun;
|
|
1301 |
int srcBytes, dstBytes;
|
|
1302 |
int bitsPerPixel;
|
|
1303 |
int bits;
|
|
1304 |
int ncells;
|
|
1305 |
unsigned char *map;
|
1
|
1306 |
|
54
|
1307 |
if ((_qClass(self) == ByteArray)
|
|
1308 |
&& (_qClass(aByteArray) == ByteArray)
|
249
|
1309 |
&& __isSmallInteger(nBitsPerPixel)
|
|
1310 |
&& __bothSmallInteger(height, width)) {
|
155
|
1311 |
if ((aMapByteArray != nil)
|
|
1312 |
&& (_Class(aMapByteArray) == ByteArray)) {
|
|
1313 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
|
|
1314 |
} else {
|
|
1315 |
map = (unsigned char *)0;
|
|
1316 |
}
|
1
|
1317 |
|
155
|
1318 |
bitsPerPixel = _intVal(nBitsPerPixel);
|
|
1319 |
w = _intVal(width);
|
|
1320 |
h = _intVal(height);
|
|
1321 |
src = _ByteArrayInstPtr(self)->ba_element;
|
|
1322 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element;
|
|
1323 |
switch (bitsPerPixel) {
|
|
1324 |
case 1:
|
|
1325 |
mask = 0x01;
|
|
1326 |
break;
|
|
1327 |
case 2:
|
|
1328 |
mask = 0x03;
|
|
1329 |
break;
|
|
1330 |
case 4:
|
|
1331 |
mask = 0x0F;
|
|
1332 |
break;
|
|
1333 |
case 8:
|
|
1334 |
mask = 0xFF;
|
|
1335 |
break;
|
|
1336 |
default:
|
|
1337 |
goto fail;
|
|
1338 |
}
|
|
1339 |
ncells = mask + 1;
|
|
1340 |
if (map) {
|
|
1341 |
/*
|
|
1342 |
* if a map is present, it must have the correct size
|
|
1343 |
* (i.e. 2 raisedTo:nBitsPerPixel)
|
|
1344 |
*/
|
|
1345 |
if ((_qSize(aMapByteArray) - OHDR_SIZE) < ncells)
|
|
1346 |
goto fail;
|
|
1347 |
}
|
1
|
1348 |
|
155
|
1349 |
bytesPerRow = (w * bitsPerPixel + 7) / 8;
|
|
1350 |
shift0 = 8 - bitsPerPixel;
|
|
1351 |
srcBytes = bytesPerRow * h;
|
|
1352 |
dstBytes = w * h;
|
1
|
1353 |
|
249
|
1354 |
if ((__byteArraySize(self) >= srcBytes)
|
|
1355 |
&& (__byteArraySize(aByteArray) >= dstBytes)) {
|
155
|
1356 |
for (hrun=h; hrun; hrun--) {
|
|
1357 |
srcNext = src + bytesPerRow;
|
|
1358 |
shift = shift0;
|
|
1359 |
if (map) {
|
|
1360 |
for (wrun=w; wrun; wrun--) {
|
|
1361 |
if (shift == shift0) {
|
|
1362 |
bits = *src++;
|
|
1363 |
}
|
|
1364 |
*dst++ = map[(bits >> shift) & mask];
|
|
1365 |
shift -= bitsPerPixel;
|
|
1366 |
if (shift < 0) {
|
|
1367 |
shift = shift0;
|
|
1368 |
}
|
|
1369 |
}
|
|
1370 |
} else {
|
|
1371 |
for (wrun=w; wrun; wrun--) {
|
|
1372 |
if (shift == shift0) {
|
|
1373 |
bits = *src++;
|
|
1374 |
}
|
|
1375 |
*dst++ = (bits >> shift) & mask;
|
|
1376 |
shift -= bitsPerPixel;
|
|
1377 |
if (shift < 0) {
|
|
1378 |
shift = shift0;
|
|
1379 |
}
|
|
1380 |
}
|
|
1381 |
}
|
|
1382 |
src = srcNext;
|
|
1383 |
}
|
|
1384 |
RETURN ( self );
|
|
1385 |
}
|
1
|
1386 |
}
|
|
1387 |
fail: ;
|
|
1388 |
%}
|
|
1389 |
.
|
|
1390 |
self primitiveFailed
|
129
|
1391 |
|
|
1392 |
"Example1:
|
|
1393 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray
|
|
1394 |
"
|
|
1395 |
"
|
|
1396 |
|inBits outBits|
|
|
1397 |
|
|
1398 |
inBits := #[2r11110000
|
155
|
1399 |
2r11001100
|
|
1400 |
2r01010101
|
|
1401 |
2r00001111].
|
129
|
1402 |
outBits := ByteArray new:(8*4).
|
|
1403 |
inBits expandPixels:1 width:8 height:4
|
155
|
1404 |
into:outBits mapping:nil.
|
129
|
1405 |
outBits inspect
|
|
1406 |
"
|
|
1407 |
|
|
1408 |
"Example2:
|
|
1409 |
expand bit-array into a byteArray, translating 0-bits to 99,
|
|
1410 |
1-bits to 176. (just a stupid example)
|
|
1411 |
"
|
|
1412 |
"
|
|
1413 |
|inBits outBits|
|
|
1414 |
|
|
1415 |
inBits := #[2r11110000 2r11001100].
|
|
1416 |
outBits := ByteArray new:16.
|
|
1417 |
inBits expandPixels:1 width:16 height:1
|
155
|
1418 |
into:outBits mapping:#[99 176].
|
129
|
1419 |
outBits inspect
|
|
1420 |
"
|
|
1421 |
!
|
|
1422 |
|
|
1423 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray
|
155
|
1424 |
mapping:aMapByteArray
|
129
|
1425 |
|
|
1426 |
"given the receiver with 8-bit pixels, compress them into aByteArray
|
|
1427 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed
|
|
1428 |
to allow for any padding. On the fly, the source bytes are translated
|
|
1429 |
using aMapByteArray (if non-nil).
|
|
1430 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte
|
|
1431 |
value of n is found in map at:(n + 1).
|
|
1432 |
Output bits are filled left-to right, i.e. the first byte in the input
|
|
1433 |
corresponds to the high bit(s) if the first byte in the input.
|
|
1434 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit
|
|
1435 |
bitmaps.
|
|
1436 |
It can also be used to compress byte-arrays into bitArrays."
|
|
1437 |
|
|
1438 |
%{ /* NOCONTEXT */
|
|
1439 |
|
|
1440 |
REGISTER unsigned char *src, *dst;
|
|
1441 |
REGISTER int wrun;
|
|
1442 |
unsigned char *dstNext;
|
|
1443 |
int bytesPerRow, mask, shift0, shift;
|
|
1444 |
int w, h, hrun;
|
|
1445 |
int srcBytes, dstBytes;
|
|
1446 |
int bitsPerPixel;
|
|
1447 |
int bits;
|
|
1448 |
int ncells;
|
|
1449 |
unsigned char *map;
|
|
1450 |
|
|
1451 |
if ((_qClass(self) == ByteArray)
|
|
1452 |
&& (_qClass(aByteArray) == ByteArray)
|
249
|
1453 |
&& __isSmallInteger(nBitsPerPixel)
|
|
1454 |
&& __bothSmallInteger(height, width)) {
|
155
|
1455 |
if ((aMapByteArray != nil)
|
|
1456 |
&& (_Class(aMapByteArray) == ByteArray)) {
|
|
1457 |
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
|
|
1458 |
} else {
|
|
1459 |
map = (unsigned char *)0;
|
|
1460 |
}
|
129
|
1461 |
|
155
|
1462 |
bitsPerPixel = _intVal(nBitsPerPixel);
|
|
1463 |
w = _intVal(width);
|
|
1464 |
h = _intVal(height);
|
|
1465 |
src = _ByteArrayInstPtr(self)->ba_element;
|
|
1466 |
dst = _ByteArrayInstPtr(aByteArray)->ba_element;
|
|
1467 |
switch (bitsPerPixel) {
|
|
1468 |
case 1:
|
|
1469 |
mask = 0x01;
|
|
1470 |
break;
|
|
1471 |
case 2:
|
|
1472 |
mask = 0x03;
|
|
1473 |
break;
|
|
1474 |
case 4:
|
|
1475 |
mask = 0x0F;
|
|
1476 |
break;
|
|
1477 |
case 8:
|
|
1478 |
mask = 0xFF;
|
|
1479 |
break;
|
|
1480 |
default:
|
|
1481 |
goto fail;
|
|
1482 |
}
|
|
1483 |
if (map) {
|
|
1484 |
/*
|
|
1485 |
* if a map is present, it must have entries for
|
|
1486 |
* all possible byte-values (i.e. its size must be >= 256)
|
|
1487 |
*/
|
|
1488 |
if ((_qSize(aMapByteArray) - OHDR_SIZE) < 256)
|
|
1489 |
goto fail;
|
|
1490 |
}
|
129
|
1491 |
|
155
|
1492 |
bytesPerRow = (w * bitsPerPixel + 7) / 8;
|
|
1493 |
dstBytes = bytesPerRow * h;
|
|
1494 |
srcBytes = w * h;
|
129
|
1495 |
|
249
|
1496 |
if ((__byteArraySize(self) >= srcBytes)
|
|
1497 |
&& (__byteArraySize(aByteArray) >= dstBytes)) {
|
155
|
1498 |
for (hrun=h; hrun; hrun--) {
|
|
1499 |
dstNext = dst + bytesPerRow;
|
|
1500 |
bits = 0; shift = 8;
|
|
1501 |
if (map) {
|
|
1502 |
for (wrun=w; wrun; wrun--) {
|
|
1503 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask);
|
|
1504 |
shift -= bitsPerPixel;
|
|
1505 |
if (shift == 0) {
|
|
1506 |
*dst++ = bits;
|
|
1507 |
bits = 0; shift = 8;
|
|
1508 |
}
|
|
1509 |
}
|
|
1510 |
} else {
|
|
1511 |
for (wrun=w; wrun; wrun--) {
|
|
1512 |
bits = (bits << bitsPerPixel) | (*src++ & mask);
|
|
1513 |
shift -= bitsPerPixel;
|
|
1514 |
if (shift == 0) {
|
|
1515 |
*dst++ = bits;
|
|
1516 |
bits = 0; shift = 8;
|
|
1517 |
}
|
|
1518 |
}
|
|
1519 |
}
|
|
1520 |
if (shift != 8) {
|
|
1521 |
*dst = bits;
|
|
1522 |
}
|
|
1523 |
dst = dstNext;
|
|
1524 |
}
|
|
1525 |
RETURN ( self );
|
|
1526 |
}
|
129
|
1527 |
}
|
|
1528 |
fail: ;
|
|
1529 |
%}
|
|
1530 |
.
|
|
1531 |
self primitiveFailed
|
|
1532 |
|
|
1533 |
"Example1:
|
|
1534 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap
|
|
1535 |
"
|
|
1536 |
"
|
|
1537 |
|inBits outBits|
|
|
1538 |
|
|
1539 |
inBits := #[0 0 0 0 1 1 1 1
|
155
|
1540 |
0 0 1 1 0 0 1 1
|
|
1541 |
0 1 0 1 0 1 0 1
|
|
1542 |
1 1 1 1 0 0 0 0].
|
129
|
1543 |
outBits := ByteArray new:4.
|
|
1544 |
inBits compressPixels:1 width:8 height:4
|
155
|
1545 |
into:outBits mapping:nil.
|
129
|
1546 |
outBits inspect
|
|
1547 |
"
|
|
1548 |
|
|
1549 |
"Example2:
|
|
1550 |
compress byte-array into a bitArray, translating 99 to 0-bits,
|
|
1551 |
and 176 to 1-bits. (just a stupid example)
|
|
1552 |
"
|
|
1553 |
"
|
|
1554 |
|inBits outBits map|
|
|
1555 |
|
|
1556 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99].
|
|
1557 |
map := ByteArray new:256.
|
|
1558 |
map at:176+1 put:1.
|
|
1559 |
|
|
1560 |
outBits := ByteArray new:2.
|
|
1561 |
inBits compressPixels:1 width:16 height:1
|
155
|
1562 |
into:outBits mapping:map.
|
129
|
1563 |
outBits inspect
|
|
1564 |
"
|
|
1565 |
|
|
1566 |
"Example3:
|
|
1567 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits,
|
|
1568 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example)
|
|
1569 |
"
|
|
1570 |
"
|
|
1571 |
|inBits outBits map|
|
|
1572 |
|
|
1573 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99].
|
|
1574 |
map := ByteArray new:256.
|
|
1575 |
map atAll:(128+1 to:255+1) put:1.
|
|
1576 |
|
|
1577 |
outBits := ByteArray new:2.
|
|
1578 |
inBits compressPixels:1 width:16 height:1
|
155
|
1579 |
into:outBits mapping:map.
|
129
|
1580 |
outBits inspect
|
|
1581 |
"
|
1
|
1582 |
! !
|
129
|
1583 |
|
|
1584 |
|