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