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