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