author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:20:46 +0200 | |
changeset 1290 | 15ba3221b89b |
parent 1266 | cef9b3cd49df |
child 1330 | 41c850b3b550 |
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)) { |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
244 |
if (((INT)__ClassInstPtr(cls)->c_flags & _MASKSMALLINT(ARRAYMASK)) |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
245 |
!= _MASKSMALLINT(BYTEARRAY)) { |
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)) { |
|
280 |
if (((INT)__ClassInstPtr(cls)->c_flags & _MASKSMALLINT(ARRAYMASK)) |
|
281 |
!= _MASKSMALLINT(BYTEARRAY)) { |
|
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 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
788 |
aSignedByteValue > 0 ifTrue:[ |
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 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
803 |
"Modified: 18.4.1996 / 16:37:40 / cg" |
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 |
||
874 |
value > 0 ifTrue:[ |
|
875 |
v := value |
|
876 |
] ifFalse:[ |
|
877 |
" |
|
878 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
879 |
constants - change to code below when fixed ... |
|
880 |
" |
|
881 |
v := value + (16r10000000 * 16r10) |
|
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 |
" |
|
893 |
! |
|
894 |
||
895 |
signedDoubleWordAt:index put:value MSB:msb |
|
896 |
"set the 4-bytes starting at index from the signed Integer value. |
|
897 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
898 |
||
899 |
|v| |
|
900 |
||
901 |
value > 0 ifTrue:[ |
|
902 |
v := value |
|
903 |
] ifFalse:[ |
|
904 |
" |
|
905 |
stupid: due to an STC bug, cannot currently have LargeInteger |
|
906 |
constants - change to code below when fixed ... |
|
907 |
" |
|
908 |
v := value + (16r10000000 * 16r10) |
|
909 |
"//// v := value + 16r100000000 |
|
910 |
]. |
|
911 |
self doubleWordAt:index put:v MSB:msb. |
|
912 |
^ value |
|
913 |
||
129 | 914 |
" |
915 |
|b| |
|
916 |
b := ByteArray new:4. |
|
917 |
b signedDoubleWordAt:1 put:-1. |
|
918 |
(b doubleWordAt:1) printStringRadix:16 |
|
919 |
" |
|
920 |
! |
|
54 | 921 |
|
563 | 922 |
signedWordAt:index |
923 |
"return the 2-bytes starting at index as a signed Integer. |
|
924 |
The value is retrieved in the machines natural byte order." |
|
925 |
||
926 |
|w "{ Class: SmallInteger }"| |
|
129 | 927 |
|
563 | 928 |
w := self wordAt:index. |
929 |
(w > 16r7FFF) ifTrue:[ |
|
930 |
^ w - 16r10000 |
|
931 |
]. |
|
932 |
^ w |
|
129 | 933 |
|
563 | 934 |
" |
935 |
|b| |
|
936 |
b := ByteArray new:2. |
|
937 |
b wordAt:1 put:16rFFFF. |
|
938 |
b signedWordAt:1 |
|
939 |
" |
|
940 |
! |
|
941 |
||
942 |
signedWordAt:index MSB:msb |
|
943 |
"return the 2-bytes starting at index as a signed Integer. |
|
944 |
The value is retrieved MSB-first if the msb-arg is true, |
|
945 |
LSB-first otherwise" |
|
946 |
||
947 |
|w "{ Class: SmallInteger }"| |
|
948 |
||
949 |
w := self wordAt:index MSB:msb. |
|
950 |
(w > 16r7FFF) ifTrue:[ |
|
951 |
^ w - 16r10000 |
|
395 | 952 |
]. |
563 | 953 |
^ w |
129 | 954 |
|
955 |
" |
|
956 |
|b| |
|
563 | 957 |
b := ByteArray new:2. |
958 |
b wordAt:1 put:16rFFFF. |
|
959 |
b signedWordAt:1 |
|
129 | 960 |
" |
961 |
! |
|
962 |
||
563 | 963 |
signedWordAt:index put:value |
964 |
"set the 2-bytes starting at index from the signed Integer value. |
|
965 |
The stored value must be in the range -32768 .. +32676. |
|
966 |
The value is stored in the machines natural byteorder." |
|
129 | 967 |
|
563 | 968 |
|v| |
129 | 969 |
|
563 | 970 |
value > 0 ifTrue:[ |
971 |
v := value |
|
359 | 972 |
] ifFalse:[ |
563 | 973 |
v := 16r10000 + value |
395 | 974 |
]. |
563 | 975 |
self wordAt:index put:v. |
976 |
^ value |
|
129 | 977 |
|
978 |
" |
|
979 |
|b| |
|
563 | 980 |
b := ByteArray new:4. |
981 |
b signedWordAt:1 put:-1. |
|
982 |
b signedWordAt:3 put:-2. |
|
983 |
b inspect |
|
984 |
" |
|
985 |
! |
|
986 |
||
987 |
signedWordAt:index put:value MSB:msb |
|
988 |
"set the 2-bytes starting at index from the signed Integer value. |
|
989 |
The stored value must be in the range -32768 .. +32676. |
|
990 |
The value is stored MSB-first, if the msb-arg is true; |
|
991 |
LSB-first otherwise" |
|
992 |
||
993 |
|v| |
|
994 |
||
995 |
value > 0 ifTrue:[ |
|
996 |
v := value |
|
997 |
] ifFalse:[ |
|
998 |
v := 16r10000 + value |
|
999 |
]. |
|
1000 |
self wordAt:index put:v MSB:msb. |
|
1001 |
^ value |
|
1002 |
||
1003 |
" |
|
1004 |
|b| |
|
1005 |
b := ByteArray new:4. |
|
1006 |
b signedWordAt:1 put:-1. |
|
1007 |
b signedWordAt:3 put:-2. |
|
129 | 1008 |
b inspect |
1009 |
" |
|
54 | 1010 |
! |
1011 |
||
563 | 1012 |
wordAt:index |
1013 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1014 |
The value is retrieved in the machines natural byte order |
|
1015 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
1016 |
||
1017 |
%{ /* NOCONTEXT */ |
|
54 | 1018 |
|
563 | 1019 |
REGISTER int indx; |
1020 |
int nIndex; |
|
1021 |
union { |
|
1022 |
char u_char[2]; |
|
1023 |
unsigned short u_ushort; |
|
1024 |
} val; |
|
1025 |
OBJ cls; |
|
54 | 1026 |
|
563 | 1027 |
if (__isSmallInteger(index)) { |
1133 | 1028 |
indx = __intVal(index); |
563 | 1029 |
if (indx > 0) { |
1133 | 1030 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1031 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1032 |
nIndex = __byteArraySize(self); |
1033 |
if ((indx+1) <= nIndex) { |
|
1133 | 1034 |
val.u_char[0] = __ByteArrayInstPtr(self)->ba_element[indx-1]; |
1035 |
val.u_char[1] = __ByteArrayInstPtr(self)->ba_element[indx-1+1]; |
|
1036 |
RETURN ( __MKSMALLINT(val.u_ushort) ); |
|
563 | 1037 |
} |
1038 |
} |
|
1039 |
} |
|
1040 |
%}. |
|
1041 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 1042 |
! |
1043 |
||
563 | 1044 |
wordAt:index MSB:msb |
1045 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1046 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
1047 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
1048 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
1049 |
||
1050 |
%{ /* NOCONTEXT */ |
|
1051 |
||
1052 |
REGISTER int indx; |
|
1053 |
int nIndex; |
|
1054 |
int val; |
|
1055 |
OBJ cls; |
|
54 | 1056 |
|
563 | 1057 |
if (__isSmallInteger(index)) { |
1133 | 1058 |
indx = __intVal(index); |
563 | 1059 |
if (indx > 0) { |
1133 | 1060 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1061 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1062 |
nIndex = __byteArraySize(self); |
1063 |
if ((indx+1) <= nIndex) { |
|
1064 |
if (msb == true) { |
|
1133 | 1065 |
val = __ByteArrayInstPtr(self)->ba_element[indx-1]; |
1066 |
val = (val << 8) + __ByteArrayInstPtr(self)->ba_element[indx-1+1]; |
|
563 | 1067 |
} else { |
1133 | 1068 |
val = __ByteArrayInstPtr(self)->ba_element[indx+1-1]; |
1069 |
val = (val << 8) + __ByteArrayInstPtr(self)->ba_element[indx-1]; |
|
563 | 1070 |
} |
1133 | 1071 |
RETURN ( __MKSMALLINT(val) ); |
563 | 1072 |
} |
1073 |
} |
|
1074 |
} |
|
1075 |
%}. |
|
1076 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 1077 |
! |
1078 |
||
563 | 1079 |
wordAt:index put:value |
1080 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1081 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1082 |
The value is stored in the machines natural byteorder. |
|
1083 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1084 |
||
1085 |
%{ /* NOCONTEXT */ |
|
54 | 1086 |
|
563 | 1087 |
REGISTER int indx; |
1088 |
int nIndex; |
|
1089 |
int v; |
|
1090 |
union { |
|
1091 |
char u_char[2]; |
|
1092 |
unsigned short u_ushort; |
|
1093 |
} val; |
|
1094 |
OBJ cls; |
|
129 | 1095 |
|
563 | 1096 |
if (__bothSmallInteger(index, value)) { |
1133 | 1097 |
indx = __intVal(index); |
563 | 1098 |
if (indx > 0) { |
1133 | 1099 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1100 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1101 |
nIndex = __byteArraySize(self); |
1102 |
if ((indx+1) <= nIndex) { |
|
1133 | 1103 |
val.u_ushort = v = __intVal(value); |
563 | 1104 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
1133 | 1105 |
__ByteArrayInstPtr(self)->ba_element[indx-1] = val.u_char[0]; |
1106 |
__ByteArrayInstPtr(self)->ba_element[indx-1+1] = val.u_char[1]; |
|
563 | 1107 |
RETURN ( value ); |
1108 |
} |
|
1109 |
} |
|
1110 |
} |
|
1111 |
} |
|
1112 |
%}. |
|
1113 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1114 |
^ self elementBoundsError |
|
1115 |
]. |
|
1116 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1117 |
|
1118 |
" |
|
563 | 1119 |
|b| |
1120 |
b := ByteArray new:4. |
|
1121 |
b wordAt:1 put:16r0102. |
|
1122 |
b wordAt:3 put:16r0304. |
|
1123 |
b inspect |
|
129 | 1124 |
" |
1125 |
! |
|
1126 |
||
563 | 1127 |
wordAt:index put:value MSB:msb |
1128 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1129 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1130 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
1131 |
lower index) if msb is false, MSB-first otherwise. |
|
1132 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1133 |
||
1134 |
%{ /* NOCONTEXT */ |
|
1135 |
||
1136 |
REGISTER int indx; |
|
1137 |
int nIndex; |
|
1138 |
int val; |
|
1139 |
OBJ cls; |
|
1140 |
||
1141 |
if (__bothSmallInteger(index, value)) { |
|
1133 | 1142 |
indx = __intVal(index); |
563 | 1143 |
if (indx > 0) { |
1133 | 1144 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1145 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1146 |
nIndex = __byteArraySize(self); |
1147 |
if ((indx+1) <= nIndex) { |
|
1133 | 1148 |
val = __intVal(value); |
563 | 1149 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
1150 |
if (msb == true) { |
|
1133 | 1151 |
__ByteArrayInstPtr(self)->ba_element[indx-1+1] = val & 0xFF; |
1152 |
__ByteArrayInstPtr(self)->ba_element[indx-1] = (val>>8) & 0xFF; |
|
563 | 1153 |
} else { |
1133 | 1154 |
__ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF; |
1155 |
__ByteArrayInstPtr(self)->ba_element[indx+1-1] = (val>>8) & 0xFF; |
|
563 | 1156 |
} |
1157 |
RETURN ( value ); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
} |
|
1161 |
} |
|
1162 |
%}. |
|
1163 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1164 |
^ self elementBoundsError |
|
1165 |
]. |
|
1166 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1167 |
|
1168 |
" |
|
563 | 1169 |
|b| |
1170 |
b := ByteArray new:8. |
|
1171 |
b wordAt:1 put:16r0102 MSB:false. |
|
1172 |
b wordAt:3 put:16r0304 MSB:false. |
|
1173 |
b wordAt:5 put:16r0102 MSB:true. |
|
1174 |
b wordAt:7 put:16r0304 MSB:true. |
|
1175 |
b inspect |
|
129 | 1176 |
" |
1 | 1177 |
! ! |
1178 |
||
550 | 1179 |
!ByteArray methodsFor:'binary storage'! |
1180 |
||
1181 |
storeBinaryDefinitionOn:stream manager:manager |
|
1182 |
"append a binary representation of the receiver onto stream. |
|
1183 |
Redefined since ByteArrays are stored with a special type code and |
|
1261 | 1184 |
in a more compact way. |
1185 |
This is an internal interface for binary storage mechanism." |
|
550 | 1186 |
|
1187 |
"not, if I have named instance variables" |
|
1188 |
self class instSize ~~ 0 ifTrue:[ |
|
1261 | 1189 |
^ super storeBinaryDefinitionOn:stream manager:manager |
550 | 1190 |
]. |
1191 |
manager putIdOfClass:(self class) on: stream. |
|
1192 |
stream nextNumber:4 put:(self basicSize). |
|
1193 |
stream nextPutAll:(self asByteArray) |
|
1261 | 1194 |
|
1195 |
"Modified: 23.4.1996 / 09:30:02 / cg" |
|
550 | 1196 |
! ! |
1197 |
||
12 | 1198 |
!ByteArray methodsFor:'converting'! |
1199 |
||
1200 |
asByteArray |
|
1201 |
"return the receiver as a byteArray" |
|
1202 |
||
1203 |
"could be an instance of a subclass..." |
|
1204 |
self class == ByteArray ifTrue:[ |
|
155 | 1205 |
^ self |
12 | 1206 |
]. |
1207 |
^ super asByteArray |
|
129 | 1208 |
|
1209 |
" |
|
1210 |
'hello world' asByteArray |
|
1211 |
#(1 2 3 4 5 6 7) asByteArray |
|
1212 |
#(1 2 256 4 5 6 7) asByteArray |
|
1213 |
" |
|
12 | 1214 |
! ! |
1215 |
||
92 | 1216 |
!ByteArray methodsFor:'copying'! |
1217 |
||
1218 |
copyFrom:start to:stop |
|
1219 |
"return the subcollection starting at index start, anInteger and ending |
|
1220 |
at stop, anInteger. |
|
1221 |
- reimplemented here for speed" |
|
1222 |
||
1223 |
%{ /* NOCONTEXT */ |
|
1224 |
||
1225 |
#if !defined(FAST_MEMCPY) |
|
1226 |
REGISTER unsigned char *srcp; |
|
1227 |
#endif |
|
1228 |
REGISTER unsigned char *dstp; |
|
1229 |
REGISTER int count; |
|
1230 |
int len, index1, index2, sz; |
|
1231 |
OBJ newByteArray; |
|
1232 |
||
1233 |
if (__isByteArray(self) |
|
249 | 1234 |
&& __bothSmallInteger(start, stop)) { |
1235 |
len = __byteArraySize(self); |
|
1133 | 1236 |
index1 = __intVal(start); |
1237 |
index2 = __intVal(stop); |
|
92 | 1238 |
|
155 | 1239 |
if ((index1 <= index2) && (index1 > 0)) { |
1240 |
if (index2 <= len) { |
|
1241 |
count = index2 - index1 + 1; |
|
834 | 1242 |
__PROTECT_CONTEXT__ |
155 | 1243 |
sz = OHDR_SIZE + count; |
835 | 1244 |
__qNew(newByteArray, sz, SENDER); |
834 | 1245 |
__UNPROTECT_CONTEXT__ |
155 | 1246 |
if (newByteArray != nil) { |
1133 | 1247 |
__InstPtr(newByteArray)->o_class = ByteArray; |
1248 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
92 | 1249 |
#ifdef FAST_MEMCPY |
1133 | 1250 |
bcopy(__ByteArrayInstPtr(self)->ba_element + index1 - 1, dstp, count); |
92 | 1251 |
#else |
1133 | 1252 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
155 | 1253 |
while (count--) { |
1254 |
*dstp++ = *srcp++; |
|
1255 |
} |
|
92 | 1256 |
#endif |
155 | 1257 |
RETURN ( newByteArray ); |
1258 |
} |
|
1259 |
} |
|
1260 |
} |
|
92 | 1261 |
} |
1262 |
%}. |
|
1263 |
" |
|
1264 |
fall back in case of non-integer index or out-of-bound index; |
|
1265 |
will eventually lead to an out-of-bound signal raise |
|
1266 |
" |
|
1267 |
^ super copyFrom:start to:stop |
|
1268 |
! ! |
|
1269 |
||
1 | 1270 |
!ByteArray methodsFor:'filling and replacing'! |
1271 |
||
155 | 1272 |
from:start to:stop put:aNumber |
1273 |
"fill part of the receiver with aNumber. |
|
1274 |
- reimplemented here for speed" |
|
1275 |
||
1276 |
%{ /* NOCONTEXT */ |
|
1277 |
||
1278 |
REGISTER unsigned char *dstp; |
|
1279 |
REGISTER int count, value; |
|
249 | 1280 |
int len, index1, index2; |
1281 |
int nInst; |
|
1282 |
OBJ cls; |
|
155 | 1283 |
|
249 | 1284 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1285 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1286 |
&& __isBytes(self)) { |
1019 | 1287 |
len = __byteArraySize(self); |
1133 | 1288 |
index1 = __intVal(start); |
1289 |
index2 = __intVal(stop); |
|
155 | 1290 |
|
1133 | 1291 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1019 | 1292 |
if ((cls = __qClass(self)) != ByteArray) { |
1133 | 1293 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 1294 |
dstp += nInst; |
1295 |
len -= nInst; |
|
1296 |
} |
|
249 | 1297 |
|
1133 | 1298 |
value = __intVal(aNumber); |
1019 | 1299 |
if (((value & ~0xFF) == 0) /* i.e. (value >= 0) && (value <= 255) */ |
1300 |
&& (index1 <= index2) |
|
1301 |
&& (index1 > 0)) { |
|
1302 |
if (index2 <= len) { |
|
1303 |
count = index2 - index1 + 1; |
|
155 | 1304 |
#ifdef FAST_MEMSET |
1019 | 1305 |
memset(dstp, count, value); |
155 | 1306 |
#else |
422 | 1307 |
# ifdef UNROLL_LOOPS |
1019 | 1308 |
while (count >= 8) { |
1309 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1310 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1311 |
dstp += 8; |
|
1312 |
count -= 8; |
|
1313 |
} |
|
422 | 1314 |
# endif |
1019 | 1315 |
while (count--) { |
1316 |
*dstp++ = value; |
|
1317 |
} |
|
155 | 1318 |
#endif |
1019 | 1319 |
RETURN (self); |
1320 |
} |
|
1321 |
} |
|
155 | 1322 |
} |
1323 |
%}. |
|
1324 |
" |
|
1325 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1326 |
will eventually lead to an out-of-bound signal raise |
|
1327 |
" |
|
1328 |
^ super from:start to:stop put:aNumber |
|
1329 |
! |
|
1330 |
||
249 | 1331 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
1332 |
"replace elements from another collection, which must be a ByteArray |
|
1333 |
or String." |
|
1 | 1334 |
|
54 | 1335 |
%{ /* NOCONTEXT */ |
1 | 1336 |
|
54 | 1337 |
int nIndex, repNIndex; |
1338 |
int startIndex, stopIndex; |
|
1339 |
REGISTER unsigned char *src; |
|
1340 |
REGISTER int repStartIndex; |
|
1341 |
int repStopIndex, count; |
|
1342 |
REGISTER unsigned char *dst; |
|
249 | 1343 |
OBJ cls; |
1344 |
int nInst; |
|
1 | 1345 |
|
249 | 1346 |
if (__isBytes(aCollection) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1347 |
&& __isBytes(self) |
249 | 1348 |
&& __bothSmallInteger(start, stop) |
1349 |
&& __isSmallInteger(repStart)) { |
|
1133 | 1350 |
startIndex = __intVal(start) - 1; |
1019 | 1351 |
if (startIndex >= 0) { |
1133 | 1352 |
dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex; |
1019 | 1353 |
nIndex = __byteArraySize(self); |
249 | 1354 |
|
1019 | 1355 |
if ((cls = __qClass(self)) != ByteArray) { |
1133 | 1356 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 1357 |
dst += nInst; |
1358 |
nIndex -= nInst; |
|
1359 |
} |
|
249 | 1360 |
|
1133 | 1361 |
stopIndex = __intVal(stop) - 1; |
1019 | 1362 |
count = stopIndex - startIndex + 1; |
1363 |
if (count == 0) { |
|
1364 |
RETURN ( self ); |
|
1365 |
} |
|
1366 |
if ((count > 0) && (stopIndex < nIndex)) { |
|
1133 | 1367 |
repStartIndex = __intVal(repStart) - 1; |
1019 | 1368 |
if (repStartIndex >= 0) { |
1369 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
1133 | 1370 |
src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
1019 | 1371 |
if ((cls = __qClass(aCollection)) != ByteArray) { |
1133 | 1372 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 1373 |
src += nInst; |
1374 |
repNIndex -= nInst; |
|
1375 |
} |
|
249 | 1376 |
|
1019 | 1377 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
1378 |
if (repStopIndex < repNIndex) { |
|
1379 |
if (aCollection == self) { |
|
1380 |
/* take care of overlapping copy */ |
|
1381 |
if (src < dst) { |
|
1382 |
/* must do a reverse copy */ |
|
1383 |
src += count; |
|
1384 |
dst += count; |
|
1385 |
while (count-- > 0) { |
|
1386 |
*--dst = *--src; |
|
1387 |
} |
|
1388 |
RETURN ( self ); |
|
1389 |
} |
|
1390 |
} |
|
1 | 1391 |
#ifdef FAST_MEMCPY |
166 | 1392 |
# ifdef bcopy4 |
1019 | 1393 |
if (((src & 3) == 0) |
1394 |
&& ((dst & 3) == 0)) { |
|
1395 |
/* copy aligned part */ |
|
1396 |
bcopy4(src, dst, count//4); |
|
1397 |
/* copy rest */ |
|
1398 |
if (count & 3) { |
|
1399 |
src += (count & ~3); |
|
1400 |
dst += (count & ~3); |
|
1401 |
count = count & 3; |
|
1402 |
while (count--) { |
|
1403 |
*dst++ = *src++; |
|
1404 |
} |
|
1405 |
} |
|
1406 |
} else |
|
166 | 1407 |
# endif /* bcopy4 */ |
1019 | 1408 |
bcopy(src, dst, count); |
1 | 1409 |
#else |
1019 | 1410 |
while (count-- > 0) { |
1411 |
*dst++ = *src++; |
|
1412 |
} |
|
1 | 1413 |
#endif |
1019 | 1414 |
RETURN ( self ); |
1415 |
} |
|
1416 |
} |
|
1417 |
} |
|
1418 |
} |
|
54 | 1419 |
} |
437 | 1420 |
%}. |
1421 |
" |
|
1422 |
fall back in case of non-ByteArray argument, |
|
1423 |
or for the error report if any index is invalid |
|
1424 |
" |
|
1425 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
563 | 1426 |
! |
1427 |
||
1428 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1169 | 1429 |
"replace elements in the receiver between index start and stop, |
1430 |
with elements taken from replacementCollection starting at repStart." |
|
563 | 1431 |
|
1432 |
(aCollection class == self class) ifTrue:[ |
|
1169 | 1433 |
^ self replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
563 | 1434 |
]. |
1435 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1169 | 1436 |
|
1437 |
"Modified: 13.4.1996 / 12:16:46 / cg" |
|
1 | 1438 |
! ! |
1439 |
||
129 | 1440 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1441 |
|
129 | 1442 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray |
155 | 1443 |
mapping:aMapByteArray |
129 | 1444 |
|
1445 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
|
1446 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1447 |
to allow for any padding. On the fly, the source bytes are translated |
|
1448 |
using aMapByteArray (if non-nil). |
|
1449 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1450 |
value of n is found in map at:(n + 1). |
|
1451 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1452 |
corresponds to the high bit(s) if the first byte in the input. |
|
1453 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
|
1454 |
bitmaps. |
|
1455 |
It can also be used to compress byte-arrays into bitArrays." |
|
1456 |
||
1457 |
%{ /* NOCONTEXT */ |
|
1458 |
||
1459 |
REGISTER unsigned char *src, *dst; |
|
1460 |
REGISTER int wrun; |
|
1461 |
unsigned char *dstNext; |
|
1462 |
int bytesPerRow, mask, shift0, shift; |
|
1463 |
int w, h, hrun; |
|
1464 |
int srcBytes, dstBytes; |
|
1465 |
int bitsPerPixel; |
|
1466 |
int bits; |
|
1467 |
int ncells; |
|
1468 |
unsigned char *map; |
|
1469 |
||
1133 | 1470 |
if ((__qClass(self) == @global(ByteArray)) |
1471 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
249 | 1472 |
&& __isSmallInteger(nBitsPerPixel) |
1473 |
&& __bothSmallInteger(height, width)) { |
|
155 | 1474 |
if ((aMapByteArray != nil) |
1133 | 1475 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
1476 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
155 | 1477 |
} else { |
1478 |
map = (unsigned char *)0; |
|
1479 |
} |
|
129 | 1480 |
|
1133 | 1481 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1482 |
w = __intVal(width); |
|
1483 |
h = __intVal(height); |
|
1484 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1485 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
155 | 1486 |
switch (bitsPerPixel) { |
1487 |
case 1: |
|
1488 |
mask = 0x01; |
|
1489 |
break; |
|
1490 |
case 2: |
|
1491 |
mask = 0x03; |
|
1492 |
break; |
|
1493 |
case 4: |
|
1494 |
mask = 0x0F; |
|
1495 |
break; |
|
1496 |
case 8: |
|
1497 |
mask = 0xFF; |
|
1498 |
break; |
|
1499 |
default: |
|
1500 |
goto fail; |
|
1501 |
} |
|
1502 |
if (map) { |
|
1503 |
/* |
|
1504 |
* if a map is present, it must have entries for |
|
1505 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1506 |
*/ |
|
328 | 1507 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) |
155 | 1508 |
goto fail; |
1509 |
} |
|
129 | 1510 |
|
155 | 1511 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1512 |
dstBytes = bytesPerRow * h; |
|
1513 |
srcBytes = w * h; |
|
129 | 1514 |
|
249 | 1515 |
if ((__byteArraySize(self) >= srcBytes) |
1516 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
155 | 1517 |
for (hrun=h; hrun; hrun--) { |
1518 |
dstNext = dst + bytesPerRow; |
|
1519 |
bits = 0; shift = 8; |
|
1520 |
if (map) { |
|
1521 |
for (wrun=w; wrun; wrun--) { |
|
1522 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1523 |
shift -= bitsPerPixel; |
|
1524 |
if (shift == 0) { |
|
1525 |
*dst++ = bits; |
|
1526 |
bits = 0; shift = 8; |
|
1527 |
} |
|
1528 |
} |
|
1529 |
} else { |
|
1530 |
for (wrun=w; wrun; wrun--) { |
|
1531 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1532 |
shift -= bitsPerPixel; |
|
1533 |
if (shift == 0) { |
|
1534 |
*dst++ = bits; |
|
1535 |
bits = 0; shift = 8; |
|
1536 |
} |
|
1537 |
} |
|
1538 |
} |
|
1539 |
if (shift != 8) { |
|
1540 |
*dst = bits; |
|
1541 |
} |
|
1542 |
dst = dstNext; |
|
1543 |
} |
|
1544 |
RETURN ( self ); |
|
1545 |
} |
|
129 | 1546 |
} |
1547 |
fail: ; |
|
1548 |
%} |
|
1549 |
. |
|
1550 |
self primitiveFailed |
|
1551 |
||
1552 |
"Example1: |
|
1553 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1554 |
" |
|
1555 |
" |
|
1556 |
|inBits outBits| |
|
1557 |
||
1558 |
inBits := #[0 0 0 0 1 1 1 1 |
|
155 | 1559 |
0 0 1 1 0 0 1 1 |
1560 |
0 1 0 1 0 1 0 1 |
|
1561 |
1 1 1 1 0 0 0 0]. |
|
129 | 1562 |
outBits := ByteArray new:4. |
1563 |
inBits compressPixels:1 width:8 height:4 |
|
155 | 1564 |
into:outBits mapping:nil. |
129 | 1565 |
outBits inspect |
1566 |
" |
|
1567 |
||
1568 |
"Example2: |
|
1569 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
1570 |
and 176 to 1-bits. (just a stupid example) |
|
1571 |
" |
|
1572 |
" |
|
1573 |
|inBits outBits map| |
|
1574 |
||
1575 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1576 |
map := ByteArray new:256. |
|
1577 |
map at:176+1 put:1. |
|
1578 |
||
1579 |
outBits := ByteArray new:2. |
|
1580 |
inBits compressPixels:1 width:16 height:1 |
|
155 | 1581 |
into:outBits mapping:map. |
129 | 1582 |
outBits inspect |
1583 |
" |
|
1584 |
||
1585 |
"Example3: |
|
1586 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
1587 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
1588 |
" |
|
1589 |
" |
|
1590 |
|inBits outBits map| |
|
1591 |
||
1592 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1593 |
map := ByteArray new:256. |
|
1594 |
map atAll:(128+1 to:255+1) put:1. |
|
1595 |
||
1596 |
outBits := ByteArray new:2. |
|
1597 |
inBits compressPixels:1 width:16 height:1 |
|
155 | 1598 |
into:outBits mapping:map. |
129 | 1599 |
outBits inspect |
1600 |
" |
|
563 | 1601 |
! |
1602 |
||
1603 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray |
|
1604 |
mapping:aMapByteArray |
|
1605 |
||
1606 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
|
1607 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
1608 |
to skip any padded src-bits. On the fly, the destination pixels |
|
1609 |
are translated using aMapByteArray (if non-nil). |
|
1610 |
Input bits are read left-to right, i.e. the first byte in the output |
|
1611 |
corresponds to the high bit(s) in the inputs first byte. |
|
1612 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
1613 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
1614 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1615 |
value of n is found in map at:(n + 1). |
|
1616 |
It can also be used to expand bit-arrays into byteArrays. |
|
1617 |
This method is specialized for ByteArray arguments - it will not handle |
|
1618 |
anything else." |
|
1619 |
||
1620 |
%{ /* NOCONTEXT */ |
|
1621 |
||
1622 |
REGISTER unsigned char *src, *dst; |
|
1623 |
REGISTER int wrun; |
|
1624 |
unsigned char *srcNext; |
|
1625 |
int bytesPerRow, mask, shift0, shift; |
|
1626 |
int w, h, hrun; |
|
1627 |
int srcBytes, dstBytes; |
|
1628 |
int bitsPerPixel; |
|
1629 |
int bits; |
|
1630 |
int ncells; |
|
1631 |
unsigned char *map; |
|
1632 |
||
1133 | 1633 |
if ((__qClass(self) == @global(ByteArray)) |
1634 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
563 | 1635 |
&& __isSmallInteger(nBitsPerPixel) |
1636 |
&& __bothSmallInteger(height, width)) { |
|
1637 |
if ((aMapByteArray != nil) |
|
1133 | 1638 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
1639 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
563 | 1640 |
} else { |
1641 |
map = (unsigned char *)0; |
|
1642 |
} |
|
1643 |
||
1133 | 1644 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1645 |
w = __intVal(width); |
|
1646 |
h = __intVal(height); |
|
1647 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1648 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
563 | 1649 |
switch (bitsPerPixel) { |
1650 |
case 1: |
|
1651 |
mask = 0x01; |
|
1652 |
break; |
|
1653 |
case 2: |
|
1654 |
mask = 0x03; |
|
1655 |
break; |
|
1656 |
case 4: |
|
1657 |
mask = 0x0F; |
|
1658 |
break; |
|
1659 |
case 8: |
|
1660 |
mask = 0xFF; |
|
1661 |
break; |
|
1662 |
default: |
|
1663 |
goto fail; |
|
1664 |
} |
|
1665 |
ncells = mask + 1; |
|
1666 |
if (map) { |
|
1667 |
/* |
|
1668 |
* if a map is present, it must have the correct size |
|
1669 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
1670 |
*/ |
|
1671 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) |
|
1672 |
goto fail; |
|
1673 |
} |
|
1674 |
||
1675 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
1676 |
shift0 = 8 - bitsPerPixel; |
|
1677 |
srcBytes = bytesPerRow * h; |
|
1678 |
dstBytes = w * h; |
|
1679 |
||
1680 |
if ((__byteArraySize(self) >= srcBytes) |
|
1681 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1682 |
for (hrun=h; hrun; hrun--) { |
|
1683 |
srcNext = src + bytesPerRow; |
|
1684 |
shift = shift0; |
|
1685 |
if (map) { |
|
1686 |
for (wrun=w; wrun; wrun--) { |
|
1687 |
if (shift == shift0) { |
|
1688 |
bits = *src++; |
|
1689 |
} |
|
1690 |
*dst++ = map[(bits >> shift) & mask]; |
|
1691 |
shift -= bitsPerPixel; |
|
1692 |
if (shift < 0) { |
|
1693 |
shift = shift0; |
|
1694 |
} |
|
1695 |
} |
|
1696 |
} else { |
|
1697 |
for (wrun=w; wrun; wrun--) { |
|
1698 |
if (shift == shift0) { |
|
1699 |
bits = *src++; |
|
1700 |
} |
|
1701 |
*dst++ = (bits >> shift) & mask; |
|
1702 |
shift -= bitsPerPixel; |
|
1703 |
if (shift < 0) { |
|
1704 |
shift = shift0; |
|
1705 |
} |
|
1706 |
} |
|
1707 |
} |
|
1708 |
src = srcNext; |
|
1709 |
} |
|
1710 |
RETURN ( self ); |
|
1711 |
} |
|
1712 |
} |
|
1713 |
fail: ; |
|
1714 |
%} |
|
1715 |
. |
|
1716 |
self primitiveFailed |
|
1717 |
||
1718 |
"Example1: |
|
1719 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
1720 |
" |
|
1721 |
" |
|
1722 |
|inBits outBits| |
|
1723 |
||
1724 |
inBits := #[2r11110000 |
|
1725 |
2r11001100 |
|
1726 |
2r01010101 |
|
1727 |
2r00001111]. |
|
1728 |
outBits := ByteArray new:(8*4). |
|
1729 |
inBits expandPixels:1 width:8 height:4 |
|
1730 |
into:outBits mapping:nil. |
|
1731 |
outBits inspect |
|
1732 |
" |
|
1733 |
||
1734 |
"Example2: |
|
1735 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
1736 |
1-bits to 176. (just a stupid example) |
|
1737 |
" |
|
1738 |
" |
|
1739 |
|inBits outBits| |
|
1740 |
||
1741 |
inBits := #[2r11110000 2r11001100]. |
|
1742 |
outBits := ByteArray new:16. |
|
1743 |
inBits expandPixels:1 width:16 height:1 |
|
1744 |
into:outBits mapping:#[99 176]. |
|
1745 |
outBits inspect |
|
1746 |
" |
|
1747 |
! |
|
1748 |
||
1749 |
invert |
|
1750 |
"invert all bytes - used with image manipulations |
|
1751 |
written as a primitive for speed. |
|
1752 |
Q: is this really needed ?" |
|
1753 |
||
1754 |
%{ /* NOCONTEXT */ |
|
1755 |
||
1756 |
REGISTER unsigned char *dst; |
|
1757 |
REGISTER unsigned long *ldst; |
|
1758 |
REGISTER int cnt; |
|
1759 |
||
1133 | 1760 |
if (__qClass(self) == @global(ByteArray)) { |
563 | 1761 |
cnt = __byteArraySize(self); |
1133 | 1762 |
dst = __ByteArrayInstPtr(self)->ba_element; |
563 | 1763 |
if (! ((int)dst & (sizeof(long)-1))) { |
1764 |
ldst = (unsigned long *)dst; |
|
1765 |
while (cnt >= sizeof(long)) { |
|
1766 |
*ldst = ~(*ldst); |
|
1767 |
ldst++; |
|
1768 |
cnt -= sizeof(long); |
|
1769 |
} |
|
1770 |
dst = (unsigned char *)ldst; |
|
1771 |
} |
|
1772 |
while (cnt--) { |
|
1773 |
*dst = ~(*dst); |
|
1774 |
dst++; |
|
1775 |
} |
|
1776 |
RETURN ( self ); |
|
1777 |
} |
|
1778 |
%} |
|
1779 |
. |
|
1780 |
self primitiveFailed |
|
1781 |
! |
|
1782 |
||
1783 |
reverse |
|
1784 |
"reverse order of elements inplace - |
|
1785 |
written as a primitive for speed on image manipulations (mirror)" |
|
1786 |
||
1787 |
%{ /* NOCONTEXT */ |
|
1788 |
||
1789 |
REGISTER unsigned char *p1, *p2; |
|
1790 |
REGISTER int cnt; |
|
1791 |
REGISTER unsigned t; |
|
1792 |
OBJ cls; |
|
1793 |
||
1133 | 1794 |
if (__qClass(self) == @global(ByteArray)) { |
563 | 1795 |
cnt = __byteArraySize(self); |
1133 | 1796 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
563 | 1797 |
p2 = p1 + cnt - 1; |
1798 |
while (cnt > 0) { |
|
1799 |
t = *p1; |
|
1800 |
*p1++ = *p2; |
|
1801 |
*p2-- = t; |
|
1802 |
cnt-=2; |
|
1803 |
} |
|
1804 |
RETURN ( self ); |
|
1805 |
} |
|
1806 |
%}. |
|
1807 |
^ super reverse |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1808 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1809 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1810 |
swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1811 |
"swap bytes inplace - |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1812 |
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
|
1813 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1814 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1815 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1816 |
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
|
1817 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1818 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1819 |
|
1133 | 1820 |
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
|
1821 |
cnt = __byteArraySize(self); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1822 |
cnt = cnt & ~1; /* make it even */ |
1133 | 1823 |
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
|
1824 |
while (cnt > 0) { |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1825 |
t = p[0]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1826 |
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
|
1827 |
p[1] = t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1828 |
p += 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1829 |
cnt -= 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1830 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1831 |
RETURN ( self ); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1832 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1833 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1834 |
^ 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
|
1835 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1836 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1837 |
#[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
|
1838 |
#[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
|
1839 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1840 |
! |
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 |
swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1843 |
"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
|
1844 |
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
|
1845 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1846 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1847 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1848 |
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
|
1849 |
REGISTER int cnt; |
931 | 1850 |
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
|
1851 |
|
1133 | 1852 |
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
|
1853 |
cnt = __byteArraySize(self); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1854 |
cnt = cnt & ~3; /* make it even */ |
1133 | 1855 |
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
|
1856 |
while (cnt > 0) { |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1857 |
t = p[0]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1858 |
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
|
1859 |
p[3] = t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1860 |
t = p[1]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1861 |
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
|
1862 |
p[2] = t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1863 |
p += 4; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1864 |
cnt -= 4; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1865 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1866 |
RETURN ( self ); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1867 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1868 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1869 |
^ 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
|
1870 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1871 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1872 |
#[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
|
1873 |
#[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
|
1874 |
#[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
|
1875 |
#[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
|
1876 |
" |
1 | 1877 |
! ! |
550 | 1878 |
|
1879 |
!ByteArray methodsFor:'printing & storing'! |
|
1880 |
||
1881 |
asPackedString |
|
1882 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1883 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1884 |
as a corresponding character (32+value) and the resulting string |
|
1885 |
is returned. The resulting string is always a multiple of 4 (since |
|
1886 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1887 |
encoded in the last character. |
|
1888 |
ST-80 uses this encoding for Images ... |
|
1889 |
PS: I dont like it ;-)" |
|
1890 |
||
1891 |
|outStream |
|
1892 |
index "{ Class:SmallInteger}" |
|
1893 |
stop "{ Class:SmallInteger}" |
|
1894 |
n "{ Class:SmallInteger}"| |
|
1895 |
||
1896 |
outStream := WriteStream on:String new. |
|
1897 |
index := 1. |
|
1898 |
stop := self size. |
|
1899 |
[index <= stop] whileTrue:[ |
|
1900 |
"take 3 source bytes" |
|
1901 |
n := (self at:index) bitShift:16. |
|
1902 |
(index < stop) ifTrue:[ |
|
1903 |
n := n bitOr:((self at:(index + 1)) bitShift:8). |
|
1904 |
(index + 1 < stop) ifTrue:[ |
|
1905 |
n := n bitOr:(self at:(index + 2)). |
|
1906 |
]. |
|
1907 |
]. |
|
1908 |
||
1909 |
"took me a while to find that one out ..." |
|
1910 |
n := n bitXor:16r820820. |
|
1911 |
||
1912 |
outStream nextPut:(Character value:(n bitShift:-18) + 32). |
|
1913 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1914 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1915 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1916 |
index := index + 3. |
|
1917 |
]. |
|
1918 |
stop \\ 3 ~~ 0 ifTrue:[ |
|
1919 |
outStream position:(outStream position - 1). |
|
1920 |
outStream nextPut:(Character value:(stop \\ 3 + 96)). |
|
1921 |
]. |
|
1922 |
^ outStream contents |
|
563 | 1923 |
! |
1924 |
||
1925 |
displayString |
|
1244 | 1926 |
"return a printed representation of the receiver for displaying" |
1927 |
||
563 | 1928 |
^ self storeString |
1929 |
||
1930 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 1931 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 1932 |
! |
1933 |
||
1934 |
storeOn:aStream |
|
1935 |
"append a printed representation from which the receiver can be |
|
1936 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
1937 |
||
1938 |
|first| |
|
1939 |
||
1082 | 1940 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
1941 |
aStream nextPutAll:'#['. |
|
1942 |
first := true. |
|
1943 |
self do:[:byte | |
|
1944 |
first ifFalse:[aStream space] |
|
1945 |
ifTrue:[first := false]. |
|
1946 |
byte storeOn:aStream. |
|
1947 |
]. |
|
1948 |
aStream nextPutAll:']'. |
|
1949 |
^ self |
|
563 | 1950 |
]. |
1951 |
^ super storeOn:aStream |
|
1952 |
||
1953 |
" |
|
1082 | 1954 |
#[1 2 3 4 5] storeOn:Transcript |
1955 |
||
1956 |
#[1 2 3 4 5] storeString |
|
1957 |
#[1 2 3 4 5] displayString |
|
1958 |
#[1 2 3 4 5] printString |
|
563 | 1959 |
" |
1082 | 1960 |
|
1961 |
"Modified: 7.3.1996 / 15:11:44 / cg" |
|
550 | 1962 |
! ! |
1963 |
||
1964 |
!ByteArray methodsFor:'queries'! |
|
1965 |
||
1966 |
indexOf:aByte startingAt:start |
|
1967 |
"return the index of the first occurrence of the argument, aByte |
|
1968 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
1969 |
- reimplemented here for speed" |
|
1970 |
||
1971 |
%{ /* NOCONTEXT */ |
|
1972 |
||
1973 |
REGISTER unsigned char *cp; |
|
1974 |
REGISTER int index, byteValue; |
|
1975 |
REGISTER int len; |
|
1976 |
OBJ cls; |
|
1977 |
||
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1978 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
1133 | 1979 |
byteValue = __intVal(aByte); |
550 | 1980 |
|
1019 | 1981 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
1982 |
/* |
|
1983 |
* searching for something which cannot be found |
|
1984 |
*/ |
|
1133 | 1985 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 1986 |
} |
550 | 1987 |
|
1019 | 1988 |
if (__isSmallInteger(start)) { |
1133 | 1989 |
index = __intVal(start); |
1019 | 1990 |
len = __byteArraySize(self); |
1133 | 1991 |
cp = __ByteArrayInstPtr(self)->ba_element; |
1992 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1019 | 1993 |
int nInst; |
550 | 1994 |
|
1133 | 1995 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 1996 |
cp += nInst; |
1997 |
len -= nInst; |
|
1998 |
} |
|
1999 |
cp += index - 1; |
|
2000 |
while (index <= len) { |
|
2001 |
if (*cp == byteValue) { |
|
1133 | 2002 |
RETURN ( __MKSMALLINT(index) ); |
1019 | 2003 |
} |
2004 |
index++; |
|
2005 |
cp++; |
|
2006 |
} |
|
1133 | 2007 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 2008 |
} |
550 | 2009 |
} |
2010 |
%}. |
|
2011 |
^ super indexOf:aByte startingAt:start |
|
2012 |
||
2013 |
" |
|
2014 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 |
|
2015 |
" |
|
2016 |
! |
|
2017 |
||
1082 | 2018 |
isLiteral |
2019 |
"return true, if the receiver can be used as a literal |
|
2020 |
(i.e. can be used in constant arrays)" |
|
2021 |
||
1244 | 2022 |
"/ no, simply returning true here is a mistake: |
2023 |
"/ it could be a subclass of ByteArray |
|
2024 |
"/ (of which the compiler does not know at all ...) |
|
1082 | 2025 |
|
2026 |
^ self class == ByteArray |
|
1244 | 2027 |
|
2028 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
1082 | 2029 |
! |
2030 |
||
563 | 2031 |
max |
2032 |
"return the maximum value in the receiver - |
|
2033 |
redefined to speedup image processing and sound-player |
|
2034 |
(which need a fast method for this on byteArrays)" |
|
550 | 2035 |
|
563 | 2036 |
%{ /* NOCONTEXT */ |
550 | 2037 |
|
2038 |
REGISTER unsigned char *cp; |
|
563 | 2039 |
REGISTER int index, max; |
2040 |
int len; |
|
550 | 2041 |
|
1133 | 2042 |
if (__qClass(self) == @global(ByteArray)) { |
563 | 2043 |
max = 0; |
2044 |
index = 0; |
|
550 | 2045 |
len = __qSize(self) - OHDR_SIZE; |
1133 | 2046 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
563 | 2047 |
while (++index <= len) { |
2048 |
if (*cp > max) max = *cp; |
|
550 | 2049 |
cp++; |
2050 |
} |
|
1133 | 2051 |
RETURN ( __MKSMALLINT(max) ); |
550 | 2052 |
} |
563 | 2053 |
%}. |
2054 |
^ super max |
|
550 | 2055 |
|
2056 |
" |
|
563 | 2057 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2058 |
" |
1082 | 2059 |
! ! |
2060 |
||
2061 |
!ByteArray methodsFor:'special queries'! |
|
550 | 2062 |
|
2063 |
usageCounts |
|
2064 |
"return an array filled with value-counts - |
|
2065 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2066 |
(i.e. to build up a histogram of color usage within an image)." |
|
2067 |
||
2068 |
|counts| |
|
2069 |
||
2070 |
counts := Array basicNew:256. |
|
2071 |
||
2072 |
%{ /* STACK: 2000 */ |
|
2073 |
||
2074 |
REGISTER unsigned char *cp; |
|
2075 |
REGISTER int nByte; |
|
2076 |
REGISTER int index; |
|
2077 |
int icounts[256]; |
|
2078 |
||
1133 | 2079 |
if ((__qClass(self) == @global(ByteArray)) && __isArray(counts)) { |
550 | 2080 |
/* |
2081 |
* zero counts |
|
2082 |
*/ |
|
2083 |
for (index=0; index<256; index++) { |
|
2084 |
icounts[index] = 0; |
|
2085 |
} |
|
2086 |
||
2087 |
/* |
|
2088 |
* count |
|
2089 |
*/ |
|
2090 |
nByte = __qSize(self) - OHDR_SIZE; |
|
1133 | 2091 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
550 | 2092 |
while (nByte--) { |
2093 |
icounts[*cp++]++; |
|
2094 |
} |
|
2095 |
||
2096 |
/* |
|
2097 |
* make it real counts |
|
2098 |
*/ |
|
2099 |
for (index=0; index<256; index++) { |
|
1133 | 2100 |
__ArrayInstPtr(counts)->a_element[index] = __MKSMALLINT(icounts[index]); |
550 | 2101 |
} |
2102 |
RETURN ( counts ); |
|
2103 |
} |
|
2104 |
%} |
|
2105 |
. |
|
2106 |
self primitiveFailed |
|
2107 |
||
2108 |
" |
|
2109 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
|
2110 |
" |
|
2111 |
! |
|
2112 |
||
563 | 2113 |
usedValues |
2114 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
2115 |
This is needed specially in the bitmap/Imageclasses to find used colors |
|
2116 |
in an image." |
|
550 | 2117 |
|
563 | 2118 |
|result l| |
2119 |
||
2120 |
%{ /* STACK: 400 */ |
|
550 | 2121 |
|
2122 |
REGISTER unsigned char *cp; |
|
563 | 2123 |
REGISTER int len; |
2124 |
unsigned char flags[256]; |
|
2125 |
static struct inlineCache nw = _ILC1; |
|
550 | 2126 |
|
1133 | 2127 |
if (__qClass(self) == @global(ByteArray)) { |
563 | 2128 |
memset(flags, 0, sizeof(flags)); |
550 | 2129 |
len = __qSize(self) - OHDR_SIZE; |
1133 | 2130 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
563 | 2131 |
|
2132 |
/* for each used byte, set flag */ |
|
2133 |
while (len > 0) { |
|
2134 |
flags[*cp] = 1; |
|
550 | 2135 |
cp++; |
563 | 2136 |
len--; |
550 | 2137 |
} |
563 | 2138 |
/* count 1's */ |
2139 |
len = 0; |
|
2140 |
for (cp=flags+255; cp >= flags; cp--) |
|
2141 |
if (*cp) len++; |
|
2142 |
||
2143 |
/* create ByteArray of used values */ |
|
1133 | 2144 |
result = (*nw.ilc_func)(@global(ByteArray), |
563 | 2145 |
@symbol(new:), |
2146 |
CON_COMMA nil, &nw, |
|
1133 | 2147 |
__MKSMALLINT(len)); |
2148 |
||
2149 |
if (__Class(result) == @global(ByteArray)) { |
|
2150 |
cp = &(__ByteArrayInstPtr(result)->ba_element[0]); |
|
563 | 2151 |
for (len=0; len < 256; len++) { |
2152 |
if (flags[len]) |
|
2153 |
*cp++ = len; |
|
2154 |
} |
|
2155 |
} |
|
2156 |
RETURN ( result ); |
|
550 | 2157 |
} |
563 | 2158 |
%} |
2159 |
. |
|
2160 |
^ self asSet |
|
550 | 2161 |
|
2162 |
" |
|
563 | 2163 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2164 |
" |
2165 |
! ! |
|
2166 |
||
628 | 2167 |
!ByteArray class methodsFor:'documentation'! |
2168 |
||
2169 |
version |
|
1290 | 2170 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.57 1996-04-25 16:14:40 cg Exp $' |
628 | 2171 |
! ! |