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