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