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