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