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