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