author | Claus Gittinger <cg@exept.de> |
Sun, 28 Jul 2019 12:01:13 +0200 | |
changeset 24484 | ae344cc507a6 |
parent 24316 | 70cf9c5f8ae9 |
child 24485 | e0a875626c3e |
permissions | -rw-r--r-- |
23925 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5425 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17377 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
18 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
19 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
20 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
21 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
22 |
category:'Collections-Arrayed' |
1 | 23 |
! |
24 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
25 |
!ByteArray class methodsFor:'documentation'! |
88 | 26 |
|
563 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
88 | 41 |
documentation |
42 |
" |
|
43 |
ByteArrays store integers in the range 0..255. |
|
129 | 44 |
In contrast to normal arrays (which store pointers to their elements), |
20629 | 45 |
byteArrays store the values in a dense & compact way. |
24484 | 46 |
ByteArrays can be used to hold the data for bitmaps, images and other bulk data |
47 |
(instances are returned eg. when reading a binary file or socket). |
|
129 | 48 |
ByteArrays are also used to store the bytecode-instructions of an |
49 |
interpreted method and are used as superclass for Strings. |
|
50 |
||
51 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
52 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
53 |
as elements in a constant array. |
|
54 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
55 |
||
56 |
If you have to communicate structure-data (in the C-sense) with external |
|
20629 | 57 |
programs/data-bases, see a companion class (Structure). |
129 | 58 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
59 |
and store C-structure fields. |
|
1266 | 60 |
|
4062 | 61 |
[memory requirements:] |
20629 | 62 |
OBJ-HEADER + size |
4062 | 63 |
|
2145 | 64 |
[warning:] |
20629 | 65 |
read the warning about 'growing fixed size collection' |
66 |
in ArrayedCollection's documentation |
|
2145 | 67 |
|
1290 | 68 |
[author:] |
20629 | 69 |
Claus Gittinger |
1290 | 70 |
|
1266 | 71 |
[See also:] |
20629 | 72 |
Array CharacterArray String |
88 | 73 |
" |
2 | 74 |
! ! |
75 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
76 |
!ByteArray class methodsFor:'instance creation'! |
1 | 77 |
|
78 |
uninitializedNew:anInteger |
|
79 |
"return a new instance of the receiver with uninitialized |
|
80 |
(i.e. undefined) contents. The indexed elements have any random |
|
8908 | 81 |
value. However, any named instance variables are still nilled. |
75 | 82 |
For use, when contents will be set anyway shortly after - this |
83 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
84 |
Of course, it only makes a difference for very big ByteArrays, such |
|
85 |
as used for images/bitmaps. |
|
86 |
||
87 |
Notice: if you want to port code using uninitializedNew: to another |
|
8908 | 88 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
75 | 89 |
method to the ByteArray class of the other smalltalk." |
1 | 90 |
|
91 |
%{ /* NOCONTEXT */ |
|
92 |
OBJ newobj; |
|
93 |
INT instsize, nInstVars, nindexedinstvars; |
|
94 |
REGISTER OBJ *op; |
|
95 |
||
249 | 96 |
if (__isSmallInteger(anInteger)) { |
8908 | 97 |
nindexedinstvars = __intVal(anInteger); |
98 |
if (nindexedinstvars >= 0) { |
|
99 |
if (self == ByteArray) { |
|
100 |
/* |
|
101 |
* the most common case |
|
102 |
*/ |
|
103 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
104 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
105 |
__qCheckedNew(newobj, instsize); |
|
106 |
__InstPtr(newobj)->o_class = self; |
|
107 |
__qSTORE(newobj, self); |
|
108 |
RETURN (newobj ); |
|
109 |
} |
|
110 |
} else { |
|
111 |
/* |
|
112 |
* Take care for subclasses like TwoByteString |
|
113 |
*/ |
|
114 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
115 |
case BYTEARRAY: |
|
116 |
break; |
|
117 |
||
118 |
case WORDARRAY: |
|
119 |
case SWORDARRAY: |
|
120 |
nindexedinstvars *= 2; |
|
121 |
break; |
|
122 |
||
123 |
case LONGARRAY: |
|
124 |
case SLONGARRAY: |
|
125 |
nindexedinstvars *= 4; |
|
126 |
break; |
|
127 |
||
128 |
default: |
|
129 |
/* don't know about this array type, delegate to super */ |
|
130 |
goto out; |
|
131 |
} |
|
132 |
} |
|
133 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
134 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
135 |
__PROTECT_CONTEXT__ |
|
136 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
137 |
__UNPROTECT_CONTEXT__ |
|
138 |
if (newobj != nil) { |
|
139 |
__InstPtr(newobj)->o_class = self; |
|
140 |
__qSTORE(newobj, self); |
|
141 |
if (nInstVars) { |
|
142 |
/* |
|
143 |
* still have to nil out named instvars ... |
|
144 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
145 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
8908 | 146 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 147 |
#else |
249 | 148 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
8908 | 149 |
/* |
150 |
* knowing that nil is 0 |
|
151 |
*/ |
|
152 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
249 | 153 |
# else |
8908 | 154 |
op = __InstPtr(newobj)->i_instvars; |
155 |
while (nInstVars--) |
|
156 |
*op++ = nil; |
|
249 | 157 |
# endif |
1 | 158 |
#endif |
8908 | 159 |
} |
160 |
RETURN ( newobj ); |
|
161 |
} |
|
162 |
} |
|
1 | 163 |
} |
8159
ac6725a62e17
#uninitializedNew: - take care of WORDARRAYS etc.
Stefan Vogel <sv@exept.de>
parents:
8096
diff
changeset
|
164 |
out:; |
5425 | 165 |
%}. |
1 | 166 |
^ self basicNew:anInteger |
167 |
! ! |
|
168 |
||
11502 | 169 |
|
22032 | 170 |
|
24484 | 171 |
|
172 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
173 |
!ByteArray class methodsFor:'queries'! |
550 | 174 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
175 |
elementByteSize |
16851
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
176 |
"for bit-like containers, return the number of bytes stored per element. |
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
177 |
Here, 1 is returned" |
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
178 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
179 |
^ 1 |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
180 |
|
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
181 |
"Created: / 15-09-2011 / 14:12:53 / cg" |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
182 |
! |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
183 |
|
550 | 184 |
isBuiltInClass |
1266 | 185 |
"return true if this class is known by the run-time-system. |
186 |
Here, true is returned for myself, false for subclasses." |
|
550 | 187 |
|
188 |
^ self == ByteArray |
|
1266 | 189 |
|
190 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
19934 | 191 |
! |
192 |
||
193 |
maxVal |
|
19942 | 194 |
"the minimum value which can be stored in instances of me. |
195 |
For ByteArrays, this is 255" |
|
19934 | 196 |
|
197 |
^ 255 |
|
198 |
! |
|
199 |
||
200 |
minVal |
|
19942 | 201 |
"the minimum value which can be stored in instances of me. |
202 |
For ByteArrays, this is 0" |
|
19934 | 203 |
|
204 |
^ 0 |
|
550 | 205 |
! ! |
206 |
||
22032 | 207 |
|
24484 | 208 |
|
10884 | 209 |
!ByteArray methodsFor:'Compatibility-Squeak'! |
210 |
||
12144 | 211 |
bitXor:aByteArray |
212 |
"return a new byteArray containing the bitWise-xor of the receiver's and the |
|
213 |
argument's bytes" |
|
214 |
||
15625 | 215 |
|size size1| |
216 |
||
217 |
"size := self size min:aByteArray size" |
|
218 |
size := self size. |
|
219 |
size1 := aByteArray size. |
|
220 |
size1 < size ifTrue:[ |
|
19936 | 221 |
size := size1. |
12144 | 222 |
]. |
15625 | 223 |
|
224 |
^ self copy |
|
19936 | 225 |
bitXorBytesFrom:1 to:size with:aByteArray startingAt:1; |
226 |
yourself. |
|
15625 | 227 |
|
228 |
" |
|
19936 | 229 |
#[0 1 2 3 4] bitXor:#[0 1 2 3 4] |
230 |
#[0 1 2 3 4] bitXor:#[0 1 2 3] |
|
15625 | 231 |
" |
10884 | 232 |
! ! |
233 |
||
21426 | 234 |
|
550 | 235 |
!ByteArray methodsFor:'accessing'! |
1 | 236 |
|
563 | 237 |
basicAt:index |
238 |
"return the indexed instance variable with index, anInteger |
|
1019 | 239 |
- redefined here to be slighly faster than the default in Object. |
240 |
Q: is it worth the extra code ?" |
|
563 | 241 |
|
242 |
%{ /* NOCONTEXT */ |
|
243 |
||
244 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
245 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
246 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
247 |
REGISTER int nIndex; |
563 | 248 |
|
249 |
if (__isSmallInteger(index)) { |
|
4661 | 250 |
indx = __intVal(index) - 1; |
251 |
slf = self; |
|
252 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
253 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
254 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
255 |
goto fail; |
|
256 |
} |
|
257 |
if (indx < 0) goto fail; |
|
258 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
259 |
} |
|
260 |
nIndex = __byteArraySize(slf); |
|
261 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 262 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 263 |
} |
563 | 264 |
} |
1019 | 265 |
fail: ; |
266 |
%}. |
|
563 | 267 |
^ super basicAt:index |
268 |
! |
|
269 |
||
1 | 270 |
basicAt:index put:value |
1230 | 271 |
"set the indexed instance variable with index, anInteger to value. |
272 |
Returns value (sigh). |
|
1019 | 273 |
- redefined here to be slighly faster than the default in Object. |
274 |
Q: is it worth the extra code ?" |
|
1 | 275 |
|
276 |
%{ /* NOCONTEXT */ |
|
277 |
||
278 |
REGISTER int indx; |
|
279 |
int nIndex; |
|
280 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
281 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
282 |
REGISTER OBJ cls; |
1 | 283 |
|
249 | 284 |
if (__bothSmallInteger(index, value)) { |
4661 | 285 |
val = __intVal(value); |
286 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
287 |
indx = __intVal(index) - 1; |
|
288 |
slf = self; |
|
289 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
290 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
291 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
292 |
goto fail; |
|
293 |
} |
|
294 |
if (indx < 0) goto fail; |
|
295 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
296 |
} |
|
297 |
nIndex = __byteArraySize(slf); |
|
298 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
299 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
300 |
RETURN ( value ); |
|
301 |
} |
|
302 |
} |
|
1 | 303 |
} |
1019 | 304 |
fail: ; |
305 |
%}. |
|
1 | 306 |
^ super basicAt:index put:value |
1230 | 307 |
|
308 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 309 |
! |
310 |
||
4128 | 311 |
bitAt:index |
11502 | 312 |
"return the bit at index (1 based index) as 0 or 1" |
5439 | 313 |
|
314 |
|byteIndex bitIndex0 byte| |
|
4128 | 315 |
|
316 |
%{ /* NOCONTEXT */ |
|
317 |
||
318 |
REGISTER int indx; |
|
319 |
REGISTER int byte; |
|
320 |
int nIndex; |
|
321 |
REGISTER OBJ slf; |
|
322 |
REGISTER OBJ cls; |
|
323 |
||
324 |
if (__isSmallInteger(index)) { |
|
12879 | 325 |
indx = __intVal(index) - 1; |
326 |
slf = self; |
|
327 |
||
328 |
byte = indx / 8; |
|
329 |
indx = indx % 8; |
|
330 |
||
331 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
332 |
if (indx < 0) goto badIndex; |
|
333 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
334 |
} |
|
335 |
nIndex = __byteArraySize(slf); |
|
336 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
337 |
RETURN ( __mkSmallInteger(((__ByteArrayInstPtr(slf)->ba_element[byte] & (1 << indx)) != 0)) ); |
|
338 |
} |
|
4128 | 339 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
340 |
badIndex: ; |
4128 | 341 |
%}. |
5439 | 342 |
byteIndex := ((index-1) // 8) + 1. |
343 |
bitIndex0 := ((index-1) \\ 8). |
|
344 |
byte := self at:byteIndex. |
|
345 |
^ byte bitTest:(1 bitShift:bitIndex0). |
|
4128 | 346 |
|
347 |
" |
|
12879 | 348 |
#[ 1 1 1 1 ] bitAt:9 |
4128 | 349 |
#[ 1 1 1 1 ] bitAt:11 |
350 |
#[ 2 2 2 2 ] bitAt:10 |
|
351 |
" |
|
352 |
! |
|
353 |
||
5439 | 354 |
bitClearAt:index |
8908 | 355 |
"clear the bit at index (index starts with 1)" |
5439 | 356 |
|
357 |
|byteIndex bitIndex0 byte| |
|
4128 | 358 |
|
359 |
%{ /* NOCONTEXT */ |
|
360 |
||
361 |
REGISTER int indx; |
|
362 |
REGISTER int byte; |
|
363 |
int nIndex; |
|
364 |
REGISTER OBJ slf; |
|
365 |
REGISTER OBJ cls; |
|
366 |
||
5439 | 367 |
if (0 /* __isSmallInteger(index) */) { |
8908 | 368 |
indx = __intVal(index) - 1; |
369 |
slf = self; |
|
370 |
||
371 |
byte = indx / 8; |
|
372 |
indx = indx % 8; |
|
373 |
||
374 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
375 |
if (indx < 0) goto badIndex; |
|
376 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
377 |
} |
|
378 |
nIndex = __byteArraySize(slf); |
|
379 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
380 |
__ByteArrayInstPtr(slf)->ba_element[byte] &= ~(1 << indx); |
|
381 |
RETURN (slf); |
|
382 |
} |
|
4128 | 383 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
384 |
badIndex: ; |
4128 | 385 |
%}. |
5439 | 386 |
byteIndex := ((index-1) // 8) + 1. |
387 |
bitIndex0 := ((index-1) \\ 8). |
|
388 |
byte := self at:byteIndex. |
|
389 |
byte := byte bitClear:(1 bitShift:bitIndex0). |
|
390 |
self at:byteIndex put:byte. |
|
391 |
||
392 |
" |
|
8908 | 393 |
#[0 0 0 0] copy bitClearAt:1 |
394 |
#[0 0 0 0] copy bitClearAt:7 |
|
395 |
#[0 0 0 0] copy bitClearAt:8 |
|
396 |
#[0 0 0 0] copy bitClearAt:9 |
|
5439 | 397 |
" |
4128 | 398 |
" |
8908 | 399 |
#[ 0 0 0 0 ] bitSetAt:1 |
400 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
401 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
402 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
403 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
404 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
5439 | 405 |
" |
406 |
||
407 |
||
408 |
||
409 |
! |
|
410 |
||
411 |
bitSetAt:index |
|
8908 | 412 |
"set the bit at index (index starts with 1)" |
5439 | 413 |
|
414 |
|byteIndex bitIndex0 byte| |
|
415 |
||
416 |
%{ /* NOCONTEXT */ |
|
417 |
||
19134 | 418 |
if (__isSmallInteger(index) |
419 |
&& (__qClass(self) == @global(ByteArray))) { |
|
420 |
int indx; |
|
421 |
||
422 |
indx = __intVal(index) - 1; |
|
423 |
if (indx >= 0) { |
|
424 |
int byteIndex; |
|
425 |
int bitIndex; |
|
426 |
int nIndex; |
|
427 |
||
428 |
byteIndex = indx / 8; |
|
429 |
bitIndex = indx % 8; |
|
430 |
||
431 |
nIndex = __byteArraySize(self); |
|
432 |
if ((unsigned)byteIndex < (unsigned)nIndex) { |
|
433 |
__ByteArrayInstPtr(self)->ba_element[byteIndex] |= (1 << bitIndex); |
|
434 |
RETURN (self); |
|
435 |
} |
|
436 |
} |
|
5439 | 437 |
} |
438 |
%}. |
|
439 |
byteIndex := ((index-1) // 8) + 1. |
|
440 |
bitIndex0 := ((index-1) \\ 8). |
|
441 |
byte := self at:byteIndex. |
|
442 |
byte := byte bitOr:(1 bitShift:bitIndex0). |
|
443 |
self at:byteIndex put:byte. |
|
444 |
||
445 |
" |
|
8908 | 446 |
#[0 0 0 0] copy bitSetAt:1 |
447 |
#[0 0 0 0] copy bitSetAt:7 |
|
448 |
#[0 0 0 0] copy bitSetAt:8 |
|
449 |
#[0 0 0 0] copy bitSetAt:9 |
|
19134 | 450 |
#[0 0 0 0] copy bitSetAt:32 |
451 |
#[0 0 0 0] copy bitSetAt:33 |
|
452 |
#[0 0 0 0] copy bitSetAt:0 |
|
5439 | 453 |
" |
19134 | 454 |
" |
8908 | 455 |
#[ 0 0 0 0 ] bitSetAt:1 |
456 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
457 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
458 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
459 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
460 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
4128 | 461 |
" |
5554 | 462 |
! ! |
463 |
||
464 |
!ByteArray methodsFor:'accessing-bytes'! |
|
4128 | 465 |
|
54 | 466 |
byteAt:index |
8908 | 467 |
"return the byte at index. |
468 |
For ByteArray, this is the same as basicAt:; |
|
249 | 469 |
however, for strings or symbols, this returns a numeric byteValue |
470 |
instead of a character." |
|
471 |
||
472 |
%{ /* NOCONTEXT */ |
|
473 |
||
474 |
REGISTER int indx; |
|
475 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
476 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
477 |
REGISTER OBJ cls; |
54 | 478 |
|
249 | 479 |
if (__isSmallInteger(index)) { |
4661 | 480 |
indx = __intVal(index) - 1; |
481 |
slf = self; |
|
482 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
483 |
if (indx < 0) goto badIndex; |
|
484 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
485 |
} |
|
486 |
nIndex = __byteArraySize(slf); |
|
487 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 488 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 489 |
} |
249 | 490 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
491 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
492 |
%}. |
249 | 493 |
^ (super basicAt:index) asInteger |
54 | 494 |
! |
495 |
||
249 | 496 |
byteAt:index put:value |
497 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
498 |
However, for Strings, this expects a byteValue to be stored." |
|
499 |
||
500 |
%{ /* NOCONTEXT */ |
|
501 |
||
502 |
REGISTER int indx; |
|
503 |
int nIndex; |
|
504 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
505 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
506 |
REGISTER OBJ cls; |
54 | 507 |
|
249 | 508 |
if (__bothSmallInteger(index, value)) { |
19414 | 509 |
val = __intVal(value); |
510 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
511 |
indx = __intVal(index) - 1; |
|
512 |
slf = self; |
|
513 |
if ((cls = __qClass(slf)) != ByteArray) { |
|
514 |
if (indx < 0) goto badIndex; |
|
515 |
if (cls == ImmutableByteArray) goto badIndex; |
|
516 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
517 |
} |
|
518 |
nIndex = __byteArraySize(slf); |
|
519 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
520 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
521 |
RETURN ( value ); |
|
522 |
} |
|
523 |
} |
|
249 | 524 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
525 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
526 |
%}. |
249 | 527 |
^ super basicAt:index put:value |
5554 | 528 |
! ! |
529 |
||
530 |
!ByteArray methodsFor:'accessing-longs'! |
|
54 | 531 |
|
19350 | 532 |
unsignedInt32At:index put:value |
2 | 533 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
359 | 534 |
The value should be in the range 0 to 16rFFFFFFFF |
535 |
(for negative values, the stored value is not defined). |
|
19350 | 536 |
The value is stored in the machine's natural byte order. |
359 | 537 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
538 |
||
539 |
|t| |
|
540 |
||
541 |
%{ /* NOCONTEXT */ |
|
542 |
||
19414 | 543 |
REGISTER INT indx; |
359 | 544 |
int nIndex; |
545 |
union { |
|
19350 | 546 |
unsigned char u_char[4]; |
547 |
unsigned int u_uint; |
|
359 | 548 |
} val; |
549 |
OBJ cls; |
|
2950 | 550 |
unsigned char *byteP; |
359 | 551 |
|
552 |
if (__isSmallInteger(index)) { |
|
19350 | 553 |
if (__isSmallInteger(value)) { |
554 |
val.u_uint = __intVal(value); |
|
555 |
} else { |
|
556 |
val.u_uint = __longIntVal(value); |
|
557 |
if (val.u_uint == 0) goto error; |
|
558 |
} |
|
559 |
||
560 |
indx = __intVal(index); |
|
561 |
if (indx > 0) { |
|
19414 | 562 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
563 |
if (cls == ImmutableByteArray) goto error; |
|
19350 | 564 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
19414 | 565 |
} |
19350 | 566 |
nIndex = __qSize(self) - OHDR_SIZE; |
567 |
if ((indx+3) <= nIndex) { |
|
568 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
569 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
19350 | 570 |
((unsigned int *)byteP)[0] = val.u_uint; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
571 |
#else |
19350 | 572 |
if (((unsigned INT)byteP & 3) == 0) { |
573 |
((unsigned int *)byteP)[0] = val.u_uint; |
|
574 |
} else { |
|
575 |
byteP[0] = val.u_char[0]; |
|
576 |
byteP[1] = val.u_char[1]; |
|
577 |
byteP[2] = val.u_char[2]; |
|
578 |
byteP[3] = val.u_char[3]; |
|
579 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
580 |
#endif |
19350 | 581 |
RETURN ( value ); |
582 |
} |
|
583 |
} |
|
359 | 584 |
} |
585 |
error: ; |
|
586 |
%}. |
|
19350 | 587 |
^ super unsignedInt32At:index put:value. |
359 | 588 |
|
589 |
" |
|
590 |
|b| |
|
591 |
b := ByteArray new:4. |
|
19350 | 592 |
b unsignedInt32At:1 put:16r04030201. |
359 | 593 |
b inspect |
594 |
" |
|
595 |
! |
|
596 |
||
19350 | 597 |
unsignedInt32At:index put:value MSB:msb |
359 | 598 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
129 | 599 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 600 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 601 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 602 |
|
603 |
|t| |
|
604 |
||
605 |
%{ /* NOCONTEXT */ |
|
606 |
||
19414 | 607 |
REGISTER INT indx; |
2 | 608 |
int nIndex; |
609 |
int val; |
|
249 | 610 |
OBJ cls; |
2950 | 611 |
unsigned char *byteP; |
2 | 612 |
|
359 | 613 |
if (__isSmallInteger(index)) { |
19350 | 614 |
if (__isSmallInteger(value)) { |
615 |
val = __intVal(value); |
|
616 |
} else { |
|
617 |
val = __longIntVal(value); |
|
618 |
if (val == 0) goto error; |
|
619 |
} |
|
620 |
indx = __intVal(index); |
|
621 |
if (indx > 0) { |
|
19414 | 622 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
623 |
if (cls == ImmutableByteArray) goto error; |
|
19350 | 624 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
19414 | 625 |
} |
19350 | 626 |
nIndex = __qSize(self) - OHDR_SIZE; |
627 |
if ((indx+3) <= nIndex) { |
|
628 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
629 |
if (msb == true) { |
|
630 |
/* |
|
631 |
* most significant byte first (i.e sparc order) |
|
632 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
633 |
#if defined(__MSBFIRST__) |
19350 | 634 |
if (((INT)byteP & 3) == 0) { |
635 |
((int *)byteP)[0] = val; |
|
636 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
637 |
#endif |
19350 | 638 |
{ |
639 |
byteP[3] = val & 0xFF; |
|
640 |
val >>= 8; |
|
641 |
byteP[2] = val & 0xFF; |
|
642 |
val >>= 8; |
|
643 |
byteP[1] = val & 0xFF; |
|
644 |
val >>= 8; |
|
645 |
byteP[0] = val & 0xFF; |
|
646 |
} |
|
647 |
} else { |
|
648 |
/* |
|
649 |
* least significant byte first (i.e i386/alpha order) |
|
650 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
651 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
19350 | 652 |
((int *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
653 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
654 |
# if defined(__LSBFIRST__) |
19350 | 655 |
if (((unsigned INT)byteP & 3) == 0) { |
656 |
((int *)byteP)[0] = val; |
|
657 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
658 |
# endif |
19350 | 659 |
{ |
660 |
byteP[0] = val & 0xFF; |
|
661 |
val >>= 8; |
|
662 |
byteP[1] = val & 0xFF; |
|
663 |
val >>= 8; |
|
664 |
byteP[2] = val & 0xFF; |
|
665 |
val >>= 8; |
|
666 |
byteP[3] = val & 0xFF; |
|
667 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
668 |
#endif |
19350 | 669 |
} |
670 |
RETURN ( value ); |
|
671 |
} |
|
672 |
} |
|
2 | 673 |
} |
359 | 674 |
error: ; |
675 |
%}. |
|
19350 | 676 |
^ super unsignedInt32At:index put:value MSB:msb |
129 | 677 |
|
678 |
" |
|
679 |
|b| |
|
359 | 680 |
b := ByteArray new:8. |
19350 | 681 |
b unsignedInt32At:1 put:16r04030201 MSB:true. |
682 |
b unsignedInt32At:5 put:16r04030201 MSB:false. |
|
129 | 683 |
b inspect |
684 |
" |
|
5554 | 685 |
! ! |
686 |
||
687 |
!ByteArray methodsFor:'accessing-shorts'! |
|
54 | 688 |
|
19350 | 689 |
unsignedInt16At:index |
563 | 690 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
19350 | 691 |
The value is retrieved in the machine's natural byte order |
19048 | 692 |
Notice: |
693 |
the index is a byte index; thus, this allows for unaligned access to |
|
694 |
words on any boundary. |
|
563 | 695 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
696 |
||
697 |
%{ /* NOCONTEXT */ |
|
54 | 698 |
|
19414 | 699 |
REGISTER INT indx; |
563 | 700 |
int nIndex; |
701 |
union { |
|
19048 | 702 |
unsigned char u_char[2]; |
703 |
unsigned short u_ushort; |
|
563 | 704 |
} val; |
2950 | 705 |
unsigned char *byteP; |
54 | 706 |
|
563 | 707 |
if (__isSmallInteger(index)) { |
19048 | 708 |
indx = __intVal(index); |
709 |
if (indx > 0) { |
|
710 |
if (!__isByteArrayLike(self)) |
|
711 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
712 |
nIndex = __byteArraySize(self); |
|
713 |
if ((indx+1) <= nIndex) { |
|
714 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
715 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
19048 | 716 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
717 |
#else |
19350 | 718 |
val.u_char[0] = byteP[0]; |
719 |
val.u_char[1] = byteP[1]; |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
720 |
#endif |
19048 | 721 |
RETURN ( __mkSmallInteger((val.u_ushort)) ); |
722 |
} |
|
723 |
} |
|
563 | 724 |
} |
725 |
%}. |
|
19350 | 726 |
^ super unsignedInt16At:index |
54 | 727 |
! |
728 |
||
19350 | 729 |
unsignedInt16At:index MSB:msb |
563 | 730 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
731 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
21426 | 732 |
LSB-first (i.e. low 8-bits at lower byte index) if it's false. |
19048 | 733 |
Notice: |
734 |
the index is a byte index; thus, this allows for unaligned access to |
|
735 |
words on any boundary. |
|
563 | 736 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
737 |
||
738 |
%{ /* NOCONTEXT */ |
|
19414 | 739 |
REGISTER INT indx; |
563 | 740 |
int nIndex; |
741 |
int val; |
|
2950 | 742 |
unsigned char *byteP; |
54 | 743 |
|
563 | 744 |
if (__isSmallInteger(index)) { |
19048 | 745 |
indx = __intVal(index); |
746 |
if (indx > 0) { |
|
747 |
if (!__isByteArrayLike(self)) |
|
748 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
749 |
nIndex = __byteArraySize(self); |
|
750 |
if ((indx+1) <= nIndex) { |
|
751 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
752 |
if (msb == true) { |
|
753 |
/* |
|
754 |
* most significant byte first (i.e sparc order) |
|
755 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
756 |
#if defined(__MSBFIRST__) |
19048 | 757 |
/* |
758 |
* mhmh to be measured: |
|
759 |
* the if may hurt more than the additional |
|
760 |
* memory cycles on some machines ... |
|
761 |
*/ |
|
762 |
if (((INT)byteP & 1) == 0) { |
|
763 |
/* aligned */ |
|
764 |
val = ((unsigned short *)byteP)[0]; |
|
765 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
766 |
#endif |
19048 | 767 |
{ |
768 |
val = byteP[0]; |
|
769 |
val = (val << 8) + byteP[1]; |
|
770 |
} |
|
771 |
} else { |
|
772 |
/* |
|
773 |
* least significant byte first (i.e i386/alpha order) |
|
774 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
775 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
19048 | 776 |
val = ((unsigned short *)byteP)[0]; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
777 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
778 |
# if defined(__LSBFIRST__) |
19048 | 779 |
/* |
780 |
* mhmh to be measured: |
|
781 |
* the if may hurt more than the additional |
|
782 |
* memory cycles on some machines ... |
|
783 |
*/ |
|
784 |
if (((INT)byteP & 1) == 0) { |
|
785 |
/* aligned */ |
|
786 |
val = ((unsigned short *)byteP)[0]; |
|
787 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
788 |
# endif |
19048 | 789 |
{ |
790 |
val = byteP[1]; |
|
791 |
val = (val << 8) + byteP[0]; |
|
792 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
793 |
#endif |
19048 | 794 |
} |
795 |
RETURN ( __mkSmallInteger(val) ); |
|
796 |
} |
|
797 |
} |
|
563 | 798 |
} |
799 |
%}. |
|
19350 | 800 |
^ super unsignedInt16At:index MSB:msb |
21426 | 801 |
|
802 |
"Modified (comment): / 13-02-2017 / 19:56:53 / cg" |
|
54 | 803 |
! |
804 |
||
19350 | 805 |
unsignedInt16At:index put:value |
563 | 806 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
8908 | 807 |
The stored value must be in the range 0 .. 16rFFFF. |
19350 | 808 |
The value is stored in the machine's natural byteorder, |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
809 |
i.e. this method should only be used to fill byteArrays which are |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
810 |
used internally (not passed to other machines). |
19048 | 811 |
Notice: |
812 |
the index is a byte index; thus, this allows for unaligned access to |
|
813 |
words on any boundary. |
|
563 | 814 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
815 |
||
816 |
%{ /* NOCONTEXT */ |
|
54 | 817 |
|
19414 | 818 |
REGISTER INT indx; |
563 | 819 |
int nIndex; |
820 |
int v; |
|
821 |
union { |
|
19048 | 822 |
unsigned char u_char[2]; |
823 |
unsigned short u_ushort; |
|
563 | 824 |
} val; |
2950 | 825 |
unsigned char *byteP; |
129 | 826 |
|
563 | 827 |
if (__bothSmallInteger(index, value)) { |
19048 | 828 |
indx = __intVal(index); |
829 |
if (indx > 0) { |
|
19414 | 830 |
if (!__isByteArray(self)) { |
831 |
OBJ cls = __qClass(self); |
|
832 |
if (cls == ImmutableByteArray) goto immutable; |
|
833 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
834 |
} |
|
19048 | 835 |
nIndex = __byteArraySize(self); |
836 |
if ((indx+1) <= nIndex) { |
|
19482 | 837 |
v = __intVal(value); |
19048 | 838 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
839 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
840 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
19482 | 841 |
((unsigned short *)byteP)[0] = v; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
842 |
#else |
19048 | 843 |
/* |
844 |
* mhmh to be measured: |
|
845 |
* the if may hurt more than the additional |
|
846 |
* memory cycles on some machines ... |
|
847 |
*/ |
|
848 |
if (((INT)byteP & 1) == 0) { |
|
849 |
/* aligned */ |
|
19482 | 850 |
((unsigned short *)byteP)[0] = v; |
19048 | 851 |
} else { |
19482 | 852 |
val.u_ushort = v; |
19048 | 853 |
byteP[0] = val.u_char[0]; |
854 |
byteP[1] = val.u_char[1]; |
|
855 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
856 |
#endif |
19048 | 857 |
RETURN ( value ); |
858 |
} |
|
859 |
} |
|
860 |
} |
|
563 | 861 |
} |
19414 | 862 |
immutable: ; |
563 | 863 |
%}. |
19414 | 864 |
^ self unsignedInt16At:index put:value MSB:IsBigEndian |
129 | 865 |
|
866 |
" |
|
563 | 867 |
|b| |
868 |
b := ByteArray new:4. |
|
19350 | 869 |
b unsignedInt16At:1 put:16r0102. |
870 |
b unsignedInt16At:3 put:16r0304. |
|
8908 | 871 |
b inspect |
129 | 872 |
" |
873 |
! |
|
874 |
||
19350 | 875 |
unsignedInt16At:index put:value MSB:msb |
563 | 876 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
8908 | 877 |
The stored value must be in the range 0 .. 16rFFFF. |
563 | 878 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
879 |
lower index) if msb is false, MSB-first otherwise. |
|
19048 | 880 |
Notice: |
881 |
the index is a byte index; thus, this allows for unaligned access to |
|
882 |
words on any boundary. |
|
563 | 883 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
884 |
||
885 |
%{ /* NOCONTEXT */ |
|
886 |
||
19414 | 887 |
REGISTER INT indx; |
563 | 888 |
int nIndex; |
889 |
int val; |
|
890 |
OBJ cls; |
|
2950 | 891 |
unsigned char *byteP; |
563 | 892 |
|
893 |
if (__bothSmallInteger(index, value)) { |
|
19048 | 894 |
indx = __intVal(index); |
895 |
if (indx > 0) { |
|
19414 | 896 |
if (!__isByteArray(self)) { |
897 |
OBJ cls = __qClass(self); |
|
898 |
if (cls == ImmutableByteArray) goto immutable; |
|
899 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
900 |
} |
|
19048 | 901 |
nIndex = __byteArraySize(self); |
902 |
if ((indx+1) <= nIndex) { |
|
903 |
val = __intVal(value); |
|
904 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
905 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
906 |
if (msb == true) { |
|
907 |
/* |
|
908 |
* most significant byte first (i.e sparc order) |
|
909 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
910 |
#if defined(__MSBFIRST__) |
19048 | 911 |
/* |
912 |
* mhmh to be measured: |
|
913 |
* the if may hurt more than the additional |
|
914 |
* memory cycles on some machines ... |
|
915 |
*/ |
|
916 |
if (((INT)byteP & 1) == 0) { |
|
917 |
/* aligned */ |
|
918 |
((unsigned short *)byteP)[0] = val; |
|
919 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
920 |
#endif |
19048 | 921 |
{ |
922 |
byteP[1] = val & 0xFF; |
|
923 |
byteP[0] = (val>>8) & 0xFF; |
|
924 |
} |
|
925 |
} else { |
|
926 |
/* |
|
927 |
* least significant byte first (i.e i386/alpha order) |
|
928 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
929 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
19048 | 930 |
((unsigned short *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
931 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
932 |
# if defined(__LSBFIRST__) |
19048 | 933 |
/* |
934 |
* mhmh to be measured: |
|
935 |
* the if may hurt more than the additional |
|
936 |
* memory cycles on some machines ... |
|
937 |
*/ |
|
938 |
if (((INT)byteP & 1) == 0) { |
|
939 |
/* aligned */ |
|
940 |
((unsigned short *)byteP)[0] = val; |
|
941 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
942 |
# endif |
19048 | 943 |
{ |
944 |
byteP[0] = val & 0xFF; |
|
945 |
byteP[1] = (val>>8) & 0xFF; |
|
946 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
947 |
#endif |
19048 | 948 |
} |
949 |
RETURN ( value ); |
|
950 |
} |
|
951 |
} |
|
952 |
} |
|
563 | 953 |
} |
19414 | 954 |
immutable: ; |
563 | 955 |
%}. |
19350 | 956 |
^ super unsignedInt16At:index put:value MSB:msb |
129 | 957 |
|
958 |
" |
|
563 | 959 |
|b| |
960 |
b := ByteArray new:8. |
|
19350 | 961 |
b unsignedInt16At:1 put:16r0102 MSB:false. |
962 |
b unsignedInt16At:3 put:16r0304 MSB:false. |
|
963 |
b unsignedInt16At:5 put:16r0102 MSB:true. |
|
964 |
b unsignedInt16At:7 put:16r0304 MSB:true. |
|
8908 | 965 |
b inspect |
129 | 966 |
" |
1 | 967 |
! ! |
968 |
||
11502 | 969 |
|
3345 | 970 |
!ByteArray methodsFor:'comparing'! |
971 |
||
972 |
= aByteArray |
|
973 |
"Compare the receiver with the argument and return true if the |
|
974 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
975 |
Otherwise return false." |
|
976 |
||
977 |
%{ /* NOCONTEXT */ |
|
978 |
||
979 |
int l1, l2; |
|
980 |
REGISTER OBJ s = aByteArray; |
|
981 |
unsigned char *cp1, *cp2; |
|
982 |
OBJ cls; |
|
983 |
OBJ myCls; |
|
984 |
INT addrDelta; |
|
985 |
||
986 |
if (s == self) { |
|
12879 | 987 |
RETURN ( true ); |
3345 | 988 |
} |
989 |
if (! __isNonNilObject(s)) { |
|
12879 | 990 |
RETURN ( false ); |
3345 | 991 |
} |
992 |
||
993 |
cls = __qClass(s); |
|
994 |
myCls = __qClass(self); |
|
995 |
||
996 |
if (cls == myCls) { |
|
12879 | 997 |
l2 = __byteArraySize(s); |
998 |
l1 = __byteArraySize(self); |
|
999 |
if (l1 != l2) { |
|
1000 |
RETURN ( false ); |
|
1001 |
} |
|
1002 |
||
1003 |
cp1 = __byteArrayVal(self); |
|
1004 |
cp2 = __byteArrayVal(s); |
|
1005 |
||
1006 |
/* |
|
1007 |
* care for instances of subclasses ... |
|
1008 |
*/ |
|
1009 |
if (cls != ByteArray) { |
|
1010 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1011 |
||
1012 |
cp2 += n; |
|
1013 |
cp1 += n; |
|
1014 |
l1 -= n; |
|
1015 |
} |
|
1016 |
||
1017 |
addrDelta = cp2 - cp1; |
|
1018 |
while (l1 >= (sizeof(unsigned INT) * 4)) { |
|
1019 |
if (((unsigned INT *)cp1)[0] != ((unsigned INT *)(cp1+addrDelta))[0]) { |
|
1020 |
RETURN (false); |
|
1021 |
} |
|
1022 |
if (((unsigned INT *)cp1)[1] != ((unsigned INT *)(cp1+addrDelta))[1]) { |
|
1023 |
RETURN (false); |
|
1024 |
} |
|
1025 |
if (((unsigned INT *)cp1)[2] != ((unsigned INT *)(cp1+addrDelta))[2]) { |
|
1026 |
RETURN (false); |
|
1027 |
} |
|
1028 |
if (((unsigned INT *)cp1)[3] != ((unsigned INT *)(cp1+addrDelta))[3]) { |
|
1029 |
RETURN (false); |
|
1030 |
} |
|
1031 |
l1 -= sizeof(unsigned INT)*4; |
|
1032 |
cp1 += sizeof(unsigned INT)*4; |
|
1033 |
} |
|
1034 |
while (l1 >= sizeof(unsigned INT)) { |
|
1035 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1036 |
RETURN (false); |
|
1037 |
} |
|
1038 |
l1 -= sizeof(unsigned INT); |
|
1039 |
cp1 += sizeof(unsigned INT); |
|
1040 |
} |
|
1041 |
if (l1 >= sizeof(unsigned short)) { |
|
1042 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1043 |
RETURN (false); |
|
1044 |
} |
|
1045 |
l1 -= sizeof(unsigned short); |
|
1046 |
cp1 += sizeof(unsigned short); |
|
1047 |
} |
|
1048 |
while (l1) { |
|
1049 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1050 |
RETURN (false); |
|
1051 |
} |
|
1052 |
l1--; |
|
1053 |
cp1++; |
|
1054 |
} |
|
1055 |
||
1056 |
RETURN (true); |
|
3345 | 1057 |
} |
1058 |
%}. |
|
1059 |
^ super = aByteArray |
|
1060 |
! ! |
|
1061 |
||
12 | 1062 |
!ByteArray methodsFor:'converting'! |
1063 |
||
1064 |
asByteArray |
|
1065 |
"return the receiver as a byteArray" |
|
1066 |
||
1067 |
"could be an instance of a subclass..." |
|
1068 |
self class == ByteArray ifTrue:[ |
|
155 | 1069 |
^ self |
12 | 1070 |
]. |
1071 |
^ super asByteArray |
|
129 | 1072 |
|
1073 |
" |
|
8908 | 1074 |
'hello world' asByteArray |
1075 |
#(1 2 3 4 5 6 7) asByteArray |
|
1076 |
#(1 2 256 4 5 6 7) asByteArray |
|
129 | 1077 |
" |
4778 | 1078 |
! |
1079 |
||
12453
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1080 |
asImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1081 |
"return a write-protected copy of myself" |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1082 |
|
19178 | 1083 |
|copy| |
1084 |
||
1085 |
copy := self copy. |
|
1086 |
copy class == ByteArray ifTrue:[ |
|
1087 |
^ copy changeClassTo:ImmutableByteArray. |
|
1088 |
] ifFalse:[ |
|
1089 |
^ self shouldNotImplement. |
|
1090 |
]. |
|
12453
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1091 |
! |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1092 |
|
23925 | 1093 |
asImmutableCollection |
1094 |
"return a write-protected copy of myself" |
|
1095 |
||
1096 |
^ self asImmutableByteArray |
|
1097 |
||
1098 |
"Created: / 15-03-2019 / 13:48:05 / Stefan Vogel" |
|
1099 |
! |
|
1100 |
||
12276 | 1101 |
asInteger |
19397 | 1102 |
"convert myself to an unsigned integer - the first byte is most significant. |
12276 | 1103 |
This is also in Squeak." |
1104 |
||
16795 | 1105 |
^ self asIntegerMSB:true |
12276 | 1106 |
|
1107 |
" |
|
19397 | 1108 |
#[ 2 ] asInteger hexPrintString |
1109 |
#[ 16r1 16r2 ] asInteger hexPrintString |
|
1110 |
#[4 0 0 0 0 0 0 0] asInteger hexPrintString |
|
12276 | 1111 |
" |
1112 |
! |
|
1113 |
||
12621 | 1114 |
asIntegerMSB:isMSBFirst |
19397 | 1115 |
"convert myself to an unsigned integer - the first byte is most significant. |
12621 | 1116 |
This is also in Squeak." |
1117 |
||
14135 | 1118 |
^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed |
12621 | 1119 |
|
1120 |
" |
|
19178 | 1121 |
(#[ 2 ] asIntegerMSB:true) hexPrintString |
1122 |
(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString |
|
1123 |
(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString |
|
1124 |
(#[4 0 0 0 0 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString |
|
12621 | 1125 |
" |
1126 |
! |
|
1127 |
||
7117 | 1128 |
asPackedString |
1129 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1130 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1131 |
as a corresponding character (32+value) and the resulting string |
|
1132 |
is returned. The resulting string is always a multiple of 4 (since |
|
1133 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1134 |
encoded in the last character. |
|
16799 | 1135 |
ST-80 uses this encoding for Images... |
1136 |
This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421. |
|
1137 |
PS: I don't like it ;-) |
|
1138 |
See also: fromPackedString: - the reverse operation" |
|
7117 | 1139 |
|
8908 | 1140 |
|outStream |
7117 | 1141 |
index "{ Class:SmallInteger}" |
1142 |
nextIndex "{ Class:SmallInteger}" |
|
1143 |
stop "{ Class:SmallInteger}" |
|
1144 |
n "{ Class:SmallInteger}" |
|
1145 |
mod "{ Class:SmallInteger}" |
|
1146 |
cpl| |
|
1147 |
||
18584 | 1148 |
outStream := WriteStream on:(String new:((self size + 2) * 4 // 3)). |
8908 | 1149 |
index := 1. |
7117 | 1150 |
stop := self size. |
1151 |
||
1152 |
stop > 100 ifTrue:[ |
|
18655 | 1153 |
"/ cg: |
1154 |
"/ initial lineBreak |
|
1155 |
outStream cr. |
|
7117 | 1156 |
]. |
1157 |
cpl := 0. |
|
1158 |
||
1159 |
[index <= stop] whileTrue:[ |
|
18655 | 1160 |
"take 3 source bytes" |
1161 |
n := (self at:index) bitShift:16. |
|
1162 |
(index < stop) ifTrue:[ |
|
1163 |
nextIndex := index + 1. |
|
1164 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
|
1165 |
(nextIndex < stop) ifTrue:[ |
|
1166 |
n := n bitOr:(self at:(index + 2)). |
|
1167 |
]. |
|
1168 |
]. |
|
1169 |
index := index + 3. |
|
1170 |
||
1171 |
"took me a while to find that one out ..." |
|
1172 |
n := n bitXor:16r820820. |
|
1173 |
||
1174 |
outStream nextPut:(Character value:((n bitShift:-18) bitAnd:16r3F) + 32). |
|
1175 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1176 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1177 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1178 |
||
1179 |
"/ cg: |
|
1180 |
"/ lineBreak after every 120 characters |
|
1181 |
"/ fromPackedString will ignore those |
|
1182 |
cpl := cpl + 4. |
|
1183 |
cpl >= 120 ifTrue:[ |
|
1184 |
outStream cr. |
|
1185 |
cpl := 0. |
|
1186 |
]. |
|
7117 | 1187 |
]. |
1188 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
|
18655 | 1189 |
outStream backStep. |
1190 |
outStream nextPut:(Character value:(mod + 96)). |
|
7117 | 1191 |
]. |
1192 |
^ outStream contents |
|
1193 |
||
16799 | 1194 |
" |
1195 |
#[ 16r00 16r01 16r02 16r04 16r08 16r10 16r20 16r40 16r80 ] asPackedString |
|
1196 |
" |
|
1197 |
||
7117 | 1198 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
1199 |
! |
|
1200 |
||
19178 | 1201 |
asSignedByteArray |
1202 |
"return the receiver as a signed byteArray. |
|
1203 |
elements > 127 are converted to negative numbers." |
|
1204 |
||
1205 |
|cls| |
|
1206 |
||
1207 |
cls := self class. |
|
1208 |
"could be an instance of a subclass..." |
|
1209 |
(cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
1210 |
^ self copy changeClassTo:SignedByteArray. |
|
1211 |
]. |
|
1212 |
^ super asSignedByteArray |
|
1213 |
||
1214 |
" |
|
1215 |
'hello world' asSignedByteArray |
|
1216 |
#[1 2 3 4 5 6 7] asSignedByteArray |
|
1217 |
#[1 2 129 4 5 6 7] asSignedByteArray |
|
1218 |
" |
|
1219 |
! |
|
1220 |
||
20137 | 1221 |
asString |
1222 |
"speed up string conversions" |
|
1223 |
||
1224 |
|size cls| |
|
1225 |
||
1226 |
cls := self class. |
|
1227 |
||
1228 |
(cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
1229 |
size := self size. |
|
1230 |
^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1. |
|
1231 |
]. |
|
1232 |
^ super asString. |
|
1233 |
||
1234 |
" |
|
1235 |
#[16r41 16r42 16r43] asString |
|
1236 |
#[16r41 16r42 16r43] asImmutableByteArray asString |
|
1237 |
" |
|
1238 |
! |
|
1239 |
||
17377 | 1240 |
beImmutable |
1241 |
"make myself write-protected" |
|
1242 |
||
20519 | 1243 |
super beImmutable. |
19178 | 1244 |
self class == ByteArray ifTrue:[ |
1245 |
self changeClassTo:ImmutableByteArray. |
|
1246 |
]. |
|
24316 | 1247 |
|
1248 |
"Modified: / 09-06-2019 / 15:14:59 / Claus Gittinger" |
|
19178 | 1249 |
! |
1250 |
||
1251 |
beSigned |
|
24314 | 1252 |
"destructively make mayself signed. |
1253 |
elements > 127 are converted to negative numbers. |
|
1254 |
WARNING: this changes the receiver itself |
|
1255 |
- use this only for initialization of new instances" |
|
19178 | 1256 |
|
1257 |
self class == ByteArray ifTrue:[ |
|
1258 |
self changeClassTo:SignedByteArray. |
|
1259 |
] ifFalse:[ |
|
1260 |
self shouldNotImplement. |
|
1261 |
]. |
|
1262 |
||
1263 |
" |
|
24314 | 1264 |
#[ 1 2 3 128 255 ] copy beSigned |
1265 |
#[ 1 2 3 128 255 ] beImmutable beSigned |
|
19178 | 1266 |
" |
24314 | 1267 |
|
1268 |
"Modified: / 09-06-2019 / 14:57:56 / Claus Gittinger" |
|
19178 | 1269 |
! |
1270 |
||
1271 |
beUnsigned |
|
24315 | 1272 |
"that's what I am (but I don't know if this is true for subclasses)." |
19178 | 1273 |
|
1274 |
self class == ByteArray ifTrue:[ |
|
1275 |
^ self. |
|
1276 |
]. |
|
19414 | 1277 |
self shouldNotImplement. |
19178 | 1278 |
|
1279 |
" |
|
24314 | 1280 |
#[ 1 2 3 128 255 ] copy beUnsigned |
19178 | 1281 |
" |
24314 | 1282 |
|
24315 | 1283 |
"Modified (comment): / 09-06-2019 / 14:58:22 / Claus Gittinger" |
17377 | 1284 |
! |
1285 |
||
4807 | 1286 |
decodeAsLiteralArray |
1287 |
"given a literalEncoding in the receiver, |
|
1288 |
create & return the corresponding object. |
|
1289 |
The inverse operation to #literalArrayEncoding." |
|
1290 |
||
1291 |
^ self |
|
1292 |
||
1293 |
! |
|
1294 |
||
4778 | 1295 |
literalArrayEncoding |
1296 |
"encode myself as an array literal, from which a copy of the receiver |
|
1297 |
can be reconstructed with #decodeAsLiteralArray." |
|
1298 |
||
12487 | 1299 |
|cls| |
1300 |
||
1301 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 1302 |
^ self |
11063
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1303 |
]. |
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1304 |
^ super literalArrayEncoding |
4778 | 1305 |
|
1306 |
" |
|
8908 | 1307 |
#[1 2 3] literalArrayEncoding |
4778 | 1308 |
" |
12 | 1309 |
! ! |
1310 |
||
92 | 1311 |
!ByteArray methodsFor:'copying'! |
1312 |
||
5024 | 1313 |
copy |
1314 |
"redefined for a bit more speed" |
|
1315 |
||
1316 |
self class == ByteArray ifTrue:[ |
|
8908 | 1317 |
^ self copyFrom:1 to:(self size) |
5024 | 1318 |
]. |
1319 |
^ super copy |
|
1320 |
! |
|
1321 |
||
92 | 1322 |
copyFrom:start to:stop |
1323 |
"return the subcollection starting at index start, anInteger and ending |
|
1324 |
at stop, anInteger. |
|
1325 |
- reimplemented here for speed" |
|
1326 |
||
1327 |
%{ /* NOCONTEXT */ |
|
1328 |
||
1329 |
REGISTER unsigned char *srcp; |
|
1330 |
REGISTER unsigned char *dstp; |
|
1331 |
REGISTER int count; |
|
1332 |
int len, index1, index2, sz; |
|
1333 |
OBJ newByteArray; |
|
1334 |
||
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1335 |
if (__isByteArrayLike(self) |
249 | 1336 |
&& __bothSmallInteger(start, stop)) { |
12879 | 1337 |
len = __byteArraySize(self); |
1338 |
index1 = __intVal(start); |
|
1339 |
index2 = __intVal(stop); |
|
1340 |
||
1341 |
if ((index1 <= index2) && (index1 > 0)) { |
|
1342 |
if (index2 <= len) { |
|
1343 |
count = index2 - index1 + 1; |
|
1344 |
__PROTECT_CONTEXT__ |
|
1345 |
sz = OHDR_SIZE + count; |
|
1346 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
|
1347 |
__UNPROTECT_CONTEXT__ |
|
1348 |
if (newByteArray != nil) { |
|
1349 |
__InstPtr(newByteArray)->o_class = ByteArray; |
|
1350 |
__qSTORE(newByteArray, ByteArray); |
|
1351 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
1352 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
2648 | 1353 |
|
1354 |
#ifdef bcopy4 |
|
12879 | 1355 |
if (((unsigned INT)srcp & 3) == ((unsigned INT)dstp & 3)) { |
1356 |
int nW; |
|
1357 |
||
1358 |
/* copy unaligned part */ |
|
1359 |
while (count && (((unsigned INT)srcp & 3) != 0)) { |
|
1360 |
*dstp++ = *srcp++; |
|
1361 |
count--; |
|
1362 |
} |
|
1363 |
if (count) { |
|
1364 |
/* copy aligned part */ |
|
1365 |
nW = count >> 2; |
|
1366 |
if (count & 3) { |
|
1367 |
nW++; |
|
1368 |
} |
|
1369 |
bcopy4(srcp, dstp, nW); |
|
1370 |
} |
|
1371 |
RETURN ( newByteArray ); |
|
1372 |
} |
|
2648 | 1373 |
#endif /* bcopy4 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
1374 |
#if __POINTER_SIZE__ == 8 |
12879 | 1375 |
if (((unsigned INT)srcp & 7) == ((unsigned INT)dstp & 7)) { |
1376 |
int nW; |
|
1377 |
||
1378 |
/* copy unaligned part */ |
|
1379 |
while (count && (((unsigned INT)srcp & 7) != 0)) { |
|
1380 |
*dstp++ = *srcp++; |
|
1381 |
count--; |
|
1382 |
} |
|
1383 |
/* copy aligned part */ |
|
1384 |
while (count >= 8) { |
|
1385 |
((unsigned INT *)dstp)[0] = ((unsigned INT *)srcp)[0]; |
|
1386 |
dstp += 8; |
|
1387 |
srcp += 8; |
|
1388 |
count -= 8; |
|
1389 |
} |
|
1390 |
/* copy remaining part */ |
|
1391 |
while (count) { |
|
1392 |
*dstp++ = *srcp++; |
|
1393 |
count--; |
|
1394 |
} |
|
1395 |
RETURN ( newByteArray ); |
|
1396 |
} |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1397 |
#endif /* bcopy4 */ |
2648 | 1398 |
|
92 | 1399 |
#ifdef FAST_MEMCPY |
12879 | 1400 |
bcopy(srcp, dstp, count); |
92 | 1401 |
#else |
12879 | 1402 |
while (count--) { |
1403 |
*dstp++ = *srcp++; |
|
1404 |
} |
|
92 | 1405 |
#endif |
12879 | 1406 |
RETURN ( newByteArray ); |
1407 |
} |
|
1408 |
} |
|
1409 |
} |
|
92 | 1410 |
} |
1411 |
%}. |
|
1412 |
" |
|
1413 |
fall back in case of non-integer index or out-of-bound index; |
|
1414 |
will eventually lead to an out-of-bound signal raise |
|
1415 |
" |
|
1416 |
^ super copyFrom:start to:stop |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1417 |
|
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1418 |
" |
8908 | 1419 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:1 to:10 |
1420 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:7 |
|
1421 |
||
1422 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:11 |
|
1423 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:10 |
|
1424 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:9 |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1425 |
" |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1426 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1427 |
|
5024 | 1428 |
shallowCopy |
1429 |
"redefined for a bit more speed" |
|
1430 |
||
1431 |
self class == ByteArray ifTrue:[ |
|
8908 | 1432 |
^ self copyFrom:1 to:(self size) |
5024 | 1433 |
]. |
1434 |
^ super shallowCopy |
|
1435 |
! |
|
1436 |
||
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1437 |
symbolFrom:start to:stop |
8908 | 1438 |
"make a symbol from the characters of the subcollection starting |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1439 |
at index start, anInteger and ending at stop, anInteger. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1440 |
This saves us garbage and character copying." |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1441 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1442 |
|sym| |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1443 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1444 |
%{ /* STACK:1024 */ |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1445 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1446 |
REGISTER unsigned char *srcp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1447 |
REGISTER unsigned char *endp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1448 |
REGISTER int count; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1449 |
int len, index1, index2; |
4086 | 1450 |
unsigned char scratchBuffer[1024], savec; |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1451 |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1452 |
if (__isByteArrayLike(self) && __bothSmallInteger(start, stop)) { |
12879 | 1453 |
len = __byteArraySize(self); |
1454 |
index1 = __intVal(start); |
|
1455 |
index2 = __intVal(stop); |
|
1456 |
||
1457 |
if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) { |
|
1458 |
count = index2 - index1 + 1; |
|
1459 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1460 |
if (index2 < len) { |
|
1461 |
/* temporarily stuff in a '\0' */ |
|
1462 |
endp = srcp + count + 1; |
|
1463 |
savec = *endp; |
|
1464 |
*endp = '\0'; |
|
1465 |
sym = __MKSYMBOL(srcp, 0); |
|
1466 |
/* must refetch endp (in case of a GC */ |
|
1467 |
endp = __ByteArrayInstPtr(self)->ba_element + index1 + count; |
|
1468 |
*endp = savec; |
|
1469 |
} else { |
|
1470 |
/* not enough space for '\0', copy the bytes */ |
|
1471 |
if (count < sizeof(scratchBuffer)) { |
|
1472 |
bcopy(srcp, scratchBuffer, count); |
|
1473 |
scratchBuffer[count] = '\0'; |
|
1474 |
sym = __MKSYMBOL(scratchBuffer, 0); |
|
1475 |
} |
|
1476 |
} |
|
1477 |
} |
|
1478 |
if (sym != nil) |
|
1479 |
RETURN(sym); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1480 |
} |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1481 |
%}. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1482 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1483 |
fall back in case of non-integer index or out-of-bound index |
4086 | 1484 |
or not enough stack-memory available; |
1485 |
may eventually lead to an out-of-bound signal raise |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1486 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1487 |
^ (super copyFrom:start to:stop) asString asSymbol |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1488 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1489 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1490 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1491 |
'abcdefghijklmnop' symbolFrom:1 to:3 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1492 |
'abcdefghijklmnop' symbolFrom:3 to:16 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1493 |
'abcdefghijklmnop' symbolFrom:3 to:17 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1494 |
" |
92 | 1495 |
! ! |
1496 |
||
4154 | 1497 |
!ByteArray methodsFor:'filling & replacing'! |
1 | 1498 |
|
155 | 1499 |
from:start to:stop put:aNumber |
1500 |
"fill part of the receiver with aNumber. |
|
1501 |
- reimplemented here for speed" |
|
1502 |
||
1503 |
%{ /* NOCONTEXT */ |
|
1504 |
||
1505 |
REGISTER unsigned char *dstp; |
|
1506 |
REGISTER int count, value; |
|
249 | 1507 |
int len, index1, index2; |
1508 |
OBJ cls; |
|
155 | 1509 |
|
249 | 1510 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1511 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1512 |
&& __isBytes(self)) { |
4969 | 1513 |
len = __byteArraySize(self); |
1514 |
index1 = __intVal(start); |
|
1515 |
index2 = __intVal(stop); |
|
1516 |
||
1517 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1518 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1519 |
int nInst; |
|
1520 |
||
1521 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1522 |
dstp += nInst; |
|
1523 |
len -= nInst; |
|
1524 |
} |
|
1525 |
||
1526 |
value = __intVal(aNumber); |
|
1527 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
8908 | 1528 |
&& (index1 <= index2) |
4969 | 1529 |
&& (index1 > 0)) { |
1530 |
if (index2 <= len) { |
|
1531 |
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
|
1532 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1533 |
#ifdef memset4 |
4969 | 1534 |
if (count > 20) { |
1535 |
/* fill unaligned part */ |
|
1536 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1537 |
*dstp++ = value; |
|
1538 |
count--; |
|
1539 |
} |
|
1540 |
/* fill aligned part */ |
|
1541 |
{ |
|
1542 |
int n4 = count & ~3; |
|
1543 |
int v4, nW; |
|
1544 |
||
1545 |
v4 = (value << 8) | value; |
|
1546 |
v4 = (v4 << 16) | v4; |
|
1547 |
nW = n4>>2; |
|
1548 |
memset4(dstp, v4, nW); |
|
1549 |
count -= n4; |
|
1550 |
dstp += n4; |
|
1551 |
} |
|
1552 |
while (count--) { |
|
1553 |
*dstp++ = value; |
|
1554 |
} |
|
1555 |
RETURN (self); |
|
1556 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1557 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1558 |
|
8908 | 1559 |
#if (__POINTER_SIZE__ == 8) |
4969 | 1560 |
{ |
1561 |
INT v8; |
|
1562 |
||
1563 |
v8 = (value << 8) | value; |
|
1564 |
v8 = (v8 << 16) | v8; |
|
1565 |
v8 = (v8 << 32) | v8; |
|
1566 |
||
1567 |
/* fill unaligned part */ |
|
1568 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) { |
|
1569 |
*dstp++ = value; |
|
1570 |
count--; |
|
1571 |
} |
|
1572 |
||
1573 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) { |
|
1574 |
((unsigned int *)dstp)[0] = v8; |
|
1575 |
dstp += 4; |
|
1576 |
count -= 4; |
|
1577 |
} |
|
1578 |
||
1579 |
/* fill aligned part */ |
|
1580 |
while (count >= 8) { |
|
1581 |
((unsigned INT *)dstp)[0] = v8; |
|
1582 |
dstp += 8; |
|
1583 |
count -= 8; |
|
1584 |
} |
|
1585 |
||
1586 |
/* fill rest */ |
|
1587 |
if (count >= 4) { |
|
1588 |
((unsigned int *)dstp)[0] = v8; |
|
1589 |
dstp += 4; |
|
1590 |
count -= 4; |
|
1591 |
} |
|
1592 |
if (count >= 2) { |
|
1593 |
((unsigned short *)dstp)[0] = v8; |
|
1594 |
dstp += 2; |
|
1595 |
count -= 2; |
|
1596 |
} |
|
1597 |
if (count) { |
|
1598 |
*dstp = value; |
|
1599 |
} |
|
1600 |
RETURN (self); |
|
1601 |
} |
|
8908 | 1602 |
#endif /* 64bit */ |
4945 | 1603 |
|
155 | 1604 |
#ifdef FAST_MEMSET |
4969 | 1605 |
memset(dstp, value, count); |
155 | 1606 |
#else |
8919 | 1607 |
# ifdef __UNROLL_LOOPS__ |
4969 | 1608 |
while (count >= 8) { |
1609 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1610 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1611 |
dstp += 8; |
|
1612 |
count -= 8; |
|
1613 |
} |
|
8919 | 1614 |
# endif /* __UNROLL_LOOPS__ */ |
4969 | 1615 |
while (count--) { |
1616 |
*dstp++ = value; |
|
1617 |
} |
|
155 | 1618 |
#endif |
4969 | 1619 |
RETURN (self); |
1620 |
} |
|
1621 |
} |
|
155 | 1622 |
} |
1623 |
%}. |
|
1624 |
" |
|
1625 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1626 |
will eventually lead to an out-of-bound signal raise |
|
1627 |
" |
|
1628 |
^ super from:start to:stop put:aNumber |
|
4945 | 1629 |
|
1630 |
" |
|
8908 | 1631 |
(ByteArray new:10) from:1 to:10 put:1 |
1632 |
(ByteArray new:20) from:10 to:20 put:1 |
|
1633 |
(ByteArray new:20) from:1 to:10 put:1 |
|
4945 | 1634 |
" |
1 | 1635 |
! ! |
1636 |
||
129 | 1637 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1638 |
|
8908 | 1639 |
bitAndBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1640 |
"replace bytes in the receiver with the result of a bitAnd operation. |
1641 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1642 |
||
1643 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitAnd: |
|
1644 |
||
1645 |
" |
|
1646 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1647 |
bitAndBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1648 |
#[1 2 3 4 5 6 7 8] |
4969 | 1649 |
bitAndBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1650 |
" |
1651 |
||
1652 |
! |
|
1653 |
||
1654 |
bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:ruleSymbol |
|
1655 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1656 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
4919 | 1657 |
of some logical operation, as specified by the ruleSymbol. |
1658 |
SourceBytes are fetched starting at sourceOffset. |
|
1659 |
Valid rule symbols are: |
|
18655 | 1660 |
#copy - trivial; same as replaceBytesFrom:to:with:startingAt: |
1661 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:(srcByte[sI]) |
|
1662 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:(srcByte[sI]) |
|
1663 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:(srcByte[sI]) |
|
1664 |
#+ - adding; byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256 |
|
1665 |
#- - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256 |
|
4919 | 1666 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1667 |
" |
|
1668 |
||
1669 |
|srcIdx| |
|
1670 |
||
1671 |
%{ |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1672 |
if ((__isByteArrayLike(sourceBytes)) |
4919 | 1673 |
&& (__qClass(self) == ByteArray) |
1674 |
&& __isSmallInteger(dstStart) |
|
1675 |
&& __isSmallInteger(dstEnd) |
|
1676 |
&& __isSmallInteger(sourceStart)) { |
|
18655 | 1677 |
unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element; |
1678 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1679 |
int srcLen = __byteArraySize(sourceBytes); |
|
1680 |
int dstLen = __byteArraySize(self); |
|
1681 |
int __srcStart = __intVal(sourceStart); |
|
1682 |
int __dstStart = __intVal(dstStart); |
|
1683 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1684 |
||
1685 |
if ((__dstStart >= 1) |
|
1686 |
&& (__srcStart >= 1) |
|
1687 |
&& ((__dstStart + count - 1) <= dstLen) |
|
1688 |
&& ((__srcStart + count - 1) <= srcLen)) { |
|
1689 |
srcP += __srcStart - 1; |
|
1690 |
dstP += __dstStart - 1; |
|
5439 | 1691 |
|
1692 |
#define OP_LOOP_BYTES(OP) \ |
|
16621 | 1693 |
while (count >= 4) { \ |
18655 | 1694 |
(dstP[0]) OP (srcP[0]); \ |
1695 |
(dstP[1]) OP (srcP[1]); \ |
|
1696 |
(dstP[2]) OP (srcP[2]); \ |
|
1697 |
(dstP[3]) OP (srcP[3]); \ |
|
1698 |
srcP += 4; \ |
|
1699 |
dstP += 4; \ |
|
1700 |
count -= 4; \ |
|
16621 | 1701 |
} \ |
5439 | 1702 |
while (count > 0) { \ |
18655 | 1703 |
*dstP OP (*srcP); \ |
1704 |
srcP++; \ |
|
1705 |
dstP++; \ |
|
1706 |
count--; \ |
|
8908 | 1707 |
} |
4919 | 1708 |
|
1709 |
#define OP_LOOP(OP) \ |
|
5439 | 1710 |
while (count >= 16) { \ |
18655 | 1711 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1712 |
((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]); \ |
|
1713 |
((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]); \ |
|
1714 |
((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]); \ |
|
1715 |
srcP += 16; \ |
|
1716 |
dstP += 16; \ |
|
1717 |
count -= 16; \ |
|
4919 | 1718 |
} \ |
5439 | 1719 |
while (count >= 4) { \ |
18655 | 1720 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1721 |
srcP += 4; \ |
|
1722 |
dstP += 4; \ |
|
1723 |
count -= 4; \ |
|
4919 | 1724 |
} \ |
1725 |
while (count > 0) { \ |
|
18655 | 1726 |
*dstP OP (*srcP); \ |
1727 |
srcP++; \ |
|
1728 |
dstP++; \ |
|
1729 |
count--; \ |
|
8908 | 1730 |
} |
1731 |
||
1732 |
||
18655 | 1733 |
if (ruleSymbol == @symbol(bitXor:)) { |
1734 |
OP_LOOP( ^= ) |
|
1735 |
RETURN (self); |
|
1736 |
} |
|
1737 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1738 |
OP_LOOP( ^=~ ) |
|
1739 |
RETURN (self); |
|
1740 |
} |
|
1741 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1742 |
OP_LOOP( &= ) |
|
1743 |
RETURN (self); |
|
1744 |
} |
|
1745 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1746 |
OP_LOOP( &=~ ) |
|
1747 |
RETURN (self); |
|
1748 |
} |
|
1749 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1750 |
OP_LOOP( |= ) |
|
1751 |
RETURN (self); |
|
1752 |
} |
|
1753 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1754 |
OP_LOOP( |=~ ) |
|
1755 |
RETURN (self); |
|
1756 |
} |
|
1757 |
if (ruleSymbol == @symbol(copy)) { |
|
1758 |
OP_LOOP( = ) |
|
1759 |
RETURN (self); |
|
1760 |
} |
|
1761 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1762 |
OP_LOOP( =~ ) |
|
1763 |
RETURN (self); |
|
1764 |
} |
|
1765 |
if (ruleSymbol == @symbol(+)) { |
|
1766 |
OP_LOOP_BYTES( += ) |
|
1767 |
RETURN (self); |
|
1768 |
} |
|
1769 |
if (ruleSymbol == @symbol(-)) { |
|
1770 |
OP_LOOP_BYTES( -= ) |
|
1771 |
RETURN (self); |
|
1772 |
} |
|
1773 |
} |
|
4919 | 1774 |
} |
5549 | 1775 |
#undef OP_LOOP_BYTES |
1776 |
#undef OP_LOOP |
|
1777 |
||
4919 | 1778 |
%}. |
1779 |
ruleSymbol == #copy ifTrue:[ |
|
18655 | 1780 |
self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart. |
1781 |
^ self |
|
4919 | 1782 |
]. |
1783 |
||
1784 |
srcIdx := sourceStart. |
|
1785 |
dstStart to:dstEnd do:[:dstIdx | |
|
18655 | 1786 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)). |
1787 |
srcIdx := srcIdx + 1. |
|
4919 | 1788 |
]. |
1789 |
||
1790 |
" |
|
1791 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1792 |
bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1793 |
#[1 2 3 4 5 6 7 8] |
18655 | 1794 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1795 |
#[1 2 3 4 5 6 7 8] |
18655 | 1796 |
bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd: |
4919 | 1797 |
#[1 2 3 4 5 6 7 8] |
18655 | 1798 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
5439 | 1799 |
#[255 0 0 0 0 0 0 0] |
18655 | 1800 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
4919 | 1801 |
#[1 2 3 4 5 6 7 8] |
18655 | 1802 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+ |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1803 |
#[1 2 3 4 5 6 7 8] |
18655 | 1804 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+ |
5439 | 1805 |
#[1 2 3 4 5 6 7 8] |
18655 | 1806 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot |
4919 | 1807 |
|
1808 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1809 |
bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+ |
5439 | 1810 |
" |
1811 |
! |
|
1812 |
||
1813 |
bitBlitBytesFrom:dstStart to:dstEnd withConstant:sourceByte rule:ruleSymbol |
|
1814 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1815 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
5439 | 1816 |
of some logical operation, as specified by the ruleSymbol. |
1817 |
Valid rule symbols are: |
|
18655 | 1818 |
#copy - trivial; same as from:to:put: |
1819 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:sourceConst |
|
1820 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:sourceConst |
|
1821 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:sourceConst |
|
1822 |
#+ - adding; byte[dI] = (byte[dI] + sourceConst) mod: 256 |
|
1823 |
#- - subtract; byte[dI] = (byte[dI] - sourceConst) mod: 256 |
|
5439 | 1824 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1825 |
" |
|
1826 |
||
1827 |
|srcIdx| |
|
1828 |
||
1829 |
%{ |
|
1830 |
if ((__qClass(self) == ByteArray) |
|
1831 |
&& __isSmallInteger(dstStart) |
|
1832 |
&& __isSmallInteger(dstEnd) |
|
1833 |
&& __isSmallInteger(sourceByte)) { |
|
18655 | 1834 |
unsigned char srcByte = __intVal(sourceByte); |
1835 |
unsigned srcWord; |
|
1836 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1837 |
int dstLen = __byteArraySize(self); |
|
1838 |
int __dstStart = __intVal(dstStart); |
|
1839 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1840 |
||
1841 |
srcWord = (srcByte << 8) | srcByte; |
|
1842 |
srcWord = (srcWord << 16) | srcWord; |
|
1843 |
if ((__dstStart >= 1) |
|
1844 |
&& ((__dstStart + count - 1) <= dstLen)) { |
|
1845 |
dstP += __dstStart - 1; |
|
5439 | 1846 |
|
1847 |
#define OP_LOOP_BYTES(OP) \ |
|
16620 | 1848 |
while (count >= 4) { \ |
18655 | 1849 |
dstP[0] OP srcByte; \ |
1850 |
dstP[1] OP srcByte; \ |
|
1851 |
dstP[2] OP srcByte; \ |
|
1852 |
dstP[3] OP srcByte; \ |
|
1853 |
dstP += 4; \ |
|
1854 |
count -= 4; \ |
|
16620 | 1855 |
} \ |
5439 | 1856 |
while (count > 0) { \ |
18655 | 1857 |
*dstP OP srcByte; \ |
1858 |
dstP++; \ |
|
1859 |
count--; \ |
|
8908 | 1860 |
} |
5439 | 1861 |
|
16620 | 1862 |
#define OP_LOOP_INT32(OP) \ |
1863 |
while (count >= 16) { \ |
|
18655 | 1864 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1865 |
((unsigned int *)dstP)[1] OP srcWord; \ |
|
1866 |
((unsigned int *)dstP)[2] OP srcWord; \ |
|
1867 |
((unsigned int *)dstP)[3] OP srcWord; \ |
|
1868 |
dstP += 16; \ |
|
1869 |
count -= 16; \ |
|
16620 | 1870 |
} \ |
1871 |
||
5439 | 1872 |
#define OP_LOOP(OP) \ |
16620 | 1873 |
OP_LOOP_INT32(OP) \ |
5439 | 1874 |
while (count >= 4) { \ |
18655 | 1875 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1876 |
dstP += 4; \ |
|
1877 |
count -= 4; \ |
|
5439 | 1878 |
} \ |
1879 |
while (count > 0) { \ |
|
18655 | 1880 |
*dstP OP srcByte; \ |
1881 |
dstP++; \ |
|
1882 |
count--; \ |
|
8908 | 1883 |
} |
1884 |
||
1885 |
||
18655 | 1886 |
if (ruleSymbol == @symbol(bitXor:)) { |
1887 |
OP_LOOP( ^= ) |
|
1888 |
RETURN (self); |
|
1889 |
} |
|
1890 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1891 |
OP_LOOP( ^=~ ) |
|
1892 |
RETURN (self); |
|
1893 |
} |
|
1894 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1895 |
OP_LOOP( &= ) |
|
1896 |
RETURN (self); |
|
1897 |
} |
|
1898 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1899 |
OP_LOOP( &=~ ) |
|
1900 |
RETURN (self); |
|
1901 |
} |
|
1902 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1903 |
OP_LOOP( |= ) |
|
1904 |
RETURN (self); |
|
1905 |
} |
|
1906 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1907 |
OP_LOOP( |=~ ) |
|
1908 |
RETURN (self); |
|
1909 |
} |
|
1910 |
if (ruleSymbol == @symbol(copy)) { |
|
1911 |
OP_LOOP( = ) |
|
1912 |
RETURN (self); |
|
1913 |
} |
|
1914 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1915 |
OP_LOOP( =~ ) |
|
1916 |
RETURN (self); |
|
1917 |
} |
|
1918 |
if (ruleSymbol == @symbol(+)) { |
|
1919 |
OP_LOOP_BYTES( += ) |
|
1920 |
RETURN (self); |
|
1921 |
} |
|
1922 |
if (ruleSymbol == @symbol(-)) { |
|
1923 |
OP_LOOP_BYTES( -= ) |
|
1924 |
RETURN (self); |
|
1925 |
} |
|
1926 |
} |
|
5439 | 1927 |
} |
5549 | 1928 |
#undef OP_LOOP_BYTES |
1929 |
#undef OP_LOOP |
|
5439 | 1930 |
%}. |
1931 |
ruleSymbol == #copy ifTrue:[ |
|
18655 | 1932 |
self from:dstStart to:dstEnd put:sourceByte. |
1933 |
^ self |
|
5439 | 1934 |
]. |
1935 |
||
1936 |
dstStart to:dstEnd do:[:dstIdx | |
|
18655 | 1937 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:sourceByte). |
5439 | 1938 |
]. |
1939 |
||
1940 |
" |
|
1941 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1942 |
bitBlitBytesFrom:1 to:3 withConstant:1 rule:#bitXor: |
5439 | 1943 |
#[1 2 3 4 5 6 7 8] |
18655 | 1944 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitXor: |
5439 | 1945 |
#[1 2 3 4 5 6 7 8] |
18655 | 1946 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitAnd: |
5439 | 1947 |
#[1 2 3 4 5 6 7 8] |
18655 | 1948 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1949 |
#[255 0 0 0 0 0 0 0] |
18655 | 1950 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1951 |
#[1 2 3 4 5 6 7 8] |
18655 | 1952 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#+ |
5439 | 1953 |
#[1 2 3 4 5 6 7 8] |
18655 | 1954 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#- |
5439 | 1955 |
#[1 2 3 4 5 6 7 8] |
18655 | 1956 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#copyNot |
5439 | 1957 |
|
1958 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1959 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
4919 | 1960 |
" |
1961 |
! |
|
1962 |
||
8908 | 1963 |
bitOrBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1964 |
"replace bytes in the receiver with the result of a bitOr operation. |
1965 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1966 |
||
1967 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitOr: |
|
1968 |
||
1969 |
" |
|
1970 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1971 |
bitOrBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1972 |
#[1 2 3 4 5 6 7 8] |
4969 | 1973 |
bitOrBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1974 |
" |
1975 |
||
1976 |
! |
|
1977 |
||
8908 | 1978 |
bitXorBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1979 |
"replace bytes in the receiver with the result of an bitXor operation. |
1980 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1981 |
||
1982 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitXor: |
|
1983 |
||
1984 |
" |
|
1985 |
#[1 2 3 4 5 6 7 8] |
|
4969 | 1986 |
bitXorBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1987 |
#[1 2 3 4 5 6 7 8] |
8908 | 1988 |
bitXorBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1989 |
#[1 2 3 4 5 6 7 8] |
4969 | 1990 |
bitXorBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1991 |
" |
1992 |
||
1993 |
! |
|
1994 |
||
2866 | 1995 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 1996 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
1997 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1998 |
to allow for any padding. On the fly, the source bytes are translated |
|
1999 |
using aMapByteArray (if non-nil). |
|
2000 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2001 |
value of n is found in map at:(n + 1). |
|
2002 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
2003 |
corresponds to the high bit(s) if the first byte in the input. |
|
8908 | 2004 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
129 | 2005 |
bitmaps. |
2006 |
It can also be used to compress byte-arrays into bitArrays." |
|
2007 |
||
2008 |
%{ /* NOCONTEXT */ |
|
2009 |
||
2010 |
REGISTER unsigned char *src, *dst; |
|
2011 |
REGISTER int wrun; |
|
2012 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
2013 |
unsigned char *dstEnd; |
129 | 2014 |
int bytesPerRow, mask, shift0, shift; |
2015 |
int w, h, hrun; |
|
2016 |
int srcBytes, dstBytes; |
|
2017 |
int bitsPerPixel; |
|
2018 |
int bits; |
|
2019 |
int ncells; |
|
2020 |
unsigned char *map; |
|
2021 |
||
8908 | 2022 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2023 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
249 | 2024 |
&& __isSmallInteger(nBitsPerPixel) |
2025 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 2026 |
if ((aMapByteArray != nil) |
2027 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2028 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2029 |
} else { |
|
2030 |
map = (unsigned char *)0; |
|
2031 |
} |
|
129 | 2032 |
|
2915 | 2033 |
bitsPerPixel = __intVal(nBitsPerPixel); |
2034 |
w = __intVal(width); |
|
2035 |
h = __intVal(height); |
|
2036 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2037 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2038 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
2039 |
switch (bitsPerPixel) { |
|
2040 |
case 1: |
|
2041 |
mask = 0x01; |
|
2042 |
break; |
|
2043 |
case 2: |
|
2044 |
mask = 0x03; |
|
2045 |
break; |
|
2046 |
case 4: |
|
2047 |
mask = 0x0F; |
|
2048 |
break; |
|
2049 |
case 8: |
|
2050 |
mask = 0xFF; |
|
2051 |
break; |
|
2052 |
default: |
|
13703 | 2053 |
console_printf("invalid depth in compressPixels\n"); |
2915 | 2054 |
goto fail; |
2055 |
} |
|
2056 |
if (map) { |
|
2057 |
/* |
|
2058 |
* if a map is present, it must have entries for |
|
2059 |
* all possible byte-values (i.e. its size must be >= 256) |
|
2060 |
*/ |
|
2061 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
13703 | 2062 |
console_printf("invalid map in compressPixels\n"); |
2915 | 2063 |
goto fail; |
2064 |
} |
|
2065 |
} |
|
129 | 2066 |
|
2915 | 2067 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
2068 |
dstBytes = bytesPerRow * h; |
|
2069 |
srcBytes = w * h; |
|
2866 | 2070 |
|
2915 | 2071 |
if ((__byteArraySize(self) >= srcBytes) |
2072 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2073 |
for (hrun=h; hrun; hrun--) { |
|
2074 |
dstNext = dst + bytesPerRow; |
|
2075 |
bits = 0; shift = 8; |
|
2076 |
if (map) { |
|
2077 |
for (wrun=w; wrun; wrun--) { |
|
2078 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
2079 |
shift -= bitsPerPixel; |
|
2080 |
if (shift == 0) { |
|
2081 |
if (dst == dstEnd) goto fail; |
|
2082 |
*dst++ = bits; |
|
2083 |
bits = 0; shift = 8; |
|
2084 |
} |
|
2085 |
} |
|
2086 |
} else { |
|
2087 |
for (wrun=w; wrun; wrun--) { |
|
2088 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
2089 |
shift -= bitsPerPixel; |
|
2090 |
if (shift == 0) { |
|
2091 |
if (dst == dstEnd) goto fail; |
|
2092 |
*dst++ = bits; |
|
2093 |
bits = 0; shift = 8; |
|
2094 |
} |
|
2095 |
} |
|
2096 |
} |
|
2097 |
if (shift != 8) { |
|
2098 |
if (dst == dstEnd) goto fail; |
|
2099 |
*dst = bits; |
|
2100 |
} |
|
2101 |
dst = dstNext; |
|
2102 |
} |
|
2103 |
RETURN ( self ); |
|
2104 |
} |
|
129 | 2105 |
} |
2106 |
fail: ; |
|
2866 | 2107 |
%}. |
129 | 2108 |
self primitiveFailed |
2109 |
||
2110 |
"Example1: |
|
2111 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
2112 |
" |
|
2113 |
" |
|
2114 |
|inBits outBits| |
|
2115 |
||
2116 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 2117 |
0 0 1 1 0 0 1 1 |
2118 |
0 1 0 1 0 1 0 1 |
|
2119 |
1 1 1 1 0 0 0 0]. |
|
129 | 2120 |
outBits := ByteArray new:4. |
2121 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 2122 |
into:outBits mapping:nil. |
129 | 2123 |
outBits inspect |
2124 |
" |
|
2125 |
||
2126 |
"Example2: |
|
2127 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
2128 |
and 176 to 1-bits. (just a stupid example) |
|
2129 |
" |
|
2130 |
" |
|
2131 |
|inBits outBits map| |
|
2132 |
||
2133 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2134 |
map := ByteArray new:256. |
|
2135 |
map at:176+1 put:1. |
|
2136 |
||
2137 |
outBits := ByteArray new:2. |
|
8908 | 2138 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2139 |
into:outBits mapping:map. |
129 | 2140 |
outBits inspect |
2141 |
" |
|
2142 |
||
2143 |
"Example3: |
|
2144 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
2145 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
2146 |
" |
|
2147 |
" |
|
2148 |
|inBits outBits map| |
|
2149 |
||
2150 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2151 |
map := ByteArray new:256. |
|
2152 |
map atAll:(128+1 to:255+1) put:1. |
|
2153 |
||
2154 |
outBits := ByteArray new:2. |
|
8908 | 2155 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2156 |
into:outBits mapping:map. |
129 | 2157 |
outBits inspect |
2158 |
" |
|
563 | 2159 |
! |
2160 |
||
2866 | 2161 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 2162 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
2163 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
2164 |
to skip any padded src-bits. On the fly, the destination pixels |
|
2165 |
are translated using aMapByteArray (if non-nil). |
|
2166 |
Input bits are read left-to right, i.e. the first byte in the output |
|
2167 |
corresponds to the high bit(s) in the inputs first byte. |
|
2168 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
2169 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
2170 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2171 |
value of n is found in map at:(n + 1). |
|
2172 |
It can also be used to expand bit-arrays into byteArrays. |
|
2173 |
This method is specialized for ByteArray arguments - it will not handle |
|
2174 |
anything else." |
|
2175 |
||
2176 |
%{ /* NOCONTEXT */ |
|
2177 |
||
2178 |
REGISTER unsigned char *src, *dst; |
|
2179 |
REGISTER int wrun; |
|
2180 |
unsigned char *srcNext; |
|
2181 |
int bytesPerRow, mask, shift0, shift; |
|
2182 |
int w, h, hrun; |
|
2183 |
int srcBytes, dstBytes; |
|
2184 |
int bitsPerPixel; |
|
2185 |
int bits; |
|
2186 |
int ncells; |
|
2187 |
unsigned char *map; |
|
2188 |
||
8908 | 2189 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2190 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
563 | 2191 |
&& __isSmallInteger(nBitsPerPixel) |
2192 |
&& __bothSmallInteger(height, width)) { |
|
8908 | 2193 |
if ((aMapByteArray != nil) |
2194 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2195 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2196 |
} else { |
|
2197 |
map = (unsigned char *)0; |
|
2198 |
} |
|
2199 |
||
2200 |
bitsPerPixel = __intVal(nBitsPerPixel); |
|
2201 |
w = __intVal(width); |
|
2202 |
h = __intVal(height); |
|
2203 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2204 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2205 |
switch (bitsPerPixel) { |
|
2206 |
case 1: |
|
2207 |
mask = 0x01; |
|
2208 |
break; |
|
2209 |
case 2: |
|
2210 |
mask = 0x03; |
|
2211 |
break; |
|
2212 |
case 4: |
|
2213 |
mask = 0x0F; |
|
2214 |
break; |
|
2215 |
case 8: |
|
2216 |
mask = 0xFF; |
|
2217 |
break; |
|
2218 |
default: |
|
13703 | 2219 |
console_printf("expandPixels: invalid depth\n"); |
8908 | 2220 |
goto fail; |
2221 |
} |
|
2222 |
ncells = mask + 1; |
|
2223 |
if (map) { |
|
2224 |
/* |
|
2225 |
* if a map is present, it must have the correct size |
|
2226 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
2227 |
*/ |
|
2228 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
13703 | 2229 |
console_printf("expandPixels: invalid map\n"); |
8908 | 2230 |
goto fail; |
2231 |
} |
|
2232 |
} |
|
2233 |
||
2234 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
2235 |
shift0 = 8 - bitsPerPixel; |
|
2236 |
srcBytes = bytesPerRow * h; |
|
2237 |
dstBytes = w * h; |
|
2238 |
||
2239 |
if ((__byteArraySize(self) >= srcBytes) |
|
2240 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2241 |
for (hrun=h; hrun; hrun--) { |
|
2242 |
srcNext = src + bytesPerRow; |
|
2243 |
shift = shift0; |
|
2244 |
if (map) { |
|
2245 |
if (shift0 == 0) { |
|
2246 |
/* translate only */ |
|
2247 |
for (wrun=w; wrun; wrun--) { |
|
2248 |
bits = *src++; |
|
2249 |
*dst++ = map[bits]; |
|
2250 |
} |
|
2251 |
} else { |
|
2252 |
for (wrun=w; wrun; wrun--) { |
|
2253 |
if (shift == shift0) { |
|
2254 |
bits = *src++; |
|
2255 |
} |
|
2256 |
*dst++ = map[(bits >> shift) & mask]; |
|
2257 |
shift -= bitsPerPixel; |
|
2258 |
if (shift < 0) { |
|
2259 |
shift = shift0; |
|
2260 |
} |
|
2261 |
} |
|
2262 |
} |
|
2263 |
} else { |
|
2264 |
for (wrun=w; wrun; wrun--) { |
|
2265 |
if (shift == shift0) { |
|
2266 |
bits = *src++; |
|
2267 |
} |
|
2268 |
*dst++ = (bits >> shift) & mask; |
|
2269 |
shift -= bitsPerPixel; |
|
2270 |
if (shift < 0) { |
|
2271 |
shift = shift0; |
|
2272 |
} |
|
2273 |
} |
|
2274 |
} |
|
2275 |
src = srcNext; |
|
2276 |
} |
|
2277 |
RETURN ( self ); |
|
2278 |
} |
|
18655 | 2279 |
console_printf("expandPixels: buffer size: self:%"_ld_" expect at least:%"_ld_"\n", |
2280 |
(INT)(__byteArraySize(self)), (INT)srcBytes); |
|
2281 |
console_printf("expandPixels: buffer size: arg:%"_ld_" expect at least:%"_ld_"\n", |
|
2282 |
(INT)(__byteArraySize(aByteArray)), (INT)dstBytes); |
|
563 | 2283 |
} |
13703 | 2284 |
console_printf("expandPixels: invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
2285 |
|
563 | 2286 |
fail: ; |
2866 | 2287 |
%}. |
563 | 2288 |
self primitiveFailed |
2289 |
||
2290 |
"Example1: |
|
2291 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
2292 |
" |
|
2293 |
" |
|
2294 |
|inBits outBits| |
|
2295 |
||
8908 | 2296 |
inBits := #[2r11110000 |
2297 |
2r11001100 |
|
2298 |
2r01010101 |
|
2299 |
2r00001111]. |
|
563 | 2300 |
outBits := ByteArray new:(8*4). |
2301 |
inBits expandPixels:1 width:8 height:4 |
|
8908 | 2302 |
into:outBits mapping:nil. |
563 | 2303 |
outBits inspect |
2304 |
" |
|
2305 |
||
2306 |
"Example2: |
|
2307 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
2308 |
1-bits to 176. (just a stupid example) |
|
2309 |
" |
|
2310 |
" |
|
2311 |
|inBits outBits| |
|
2312 |
||
2313 |
inBits := #[2r11110000 2r11001100]. |
|
2314 |
outBits := ByteArray new:16. |
|
8908 | 2315 |
inBits expandPixels:1 width:16 height:1 |
2316 |
into:outBits mapping:#[99 176]. |
|
563 | 2317 |
outBits inspect |
2318 |
" |
|
4978 | 2319 |
|
2320 |
"This Can also be used to extract nibbles ..." |
|
2321 |
" |
|
2322 |
|inBits outBits| |
|
2323 |
||
8908 | 2324 |
inBits := #[ 16r12 16r34 16r56 16r78 16r9A 16rBC 16rDE 16rF0 ]. |
4978 | 2325 |
outBits := ByteArray new:(inBits size * 2). |
2326 |
inBits expandPixels:4 width:outBits size height:1 into:outBits mapping:nil. |
|
2327 |
outBits inspect |
|
2328 |
" |
|
2329 |
||
563 | 2330 |
! |
2331 |
||
2332 |
invert |
|
24246 | 2333 |
"invert all bytes inplace - used with image manipulations |
563 | 2334 |
written as a primitive for speed. |
24246 | 2335 |
Warning: destructive; modifies the receiver - not a copy. |
563 | 2336 |
Q: is this really needed ?" |
2337 |
||
2338 |
%{ /* NOCONTEXT */ |
|
2339 |
REGISTER unsigned char *dst; |
|
2340 |
REGISTER unsigned long *ldst; |
|
2341 |
REGISTER int cnt; |
|
2342 |
||
1133 | 2343 |
if (__qClass(self) == @global(ByteArray)) { |
24246 | 2344 |
cnt = __byteArraySize(self); |
2345 |
dst = __ByteArrayInstPtr(self)->ba_element; |
|
2346 |
if (((INT)dst & (sizeof(long)-1)) == 0) { // aligned |
|
2347 |
ldst = (unsigned long *)dst; |
|
2348 |
while (cnt >= (sizeof(long))*4) { |
|
2349 |
ldst[0] = ~(ldst[0]); |
|
2350 |
ldst[1] = ~(ldst[1]); |
|
2351 |
ldst[2] = ~(ldst[2]); |
|
2352 |
ldst[3] = ~(ldst[3]); |
|
2353 |
ldst += 4; |
|
2354 |
cnt -= (sizeof(long))*4; |
|
2355 |
} |
|
2356 |
while (cnt >= sizeof(long)) { |
|
2357 |
*ldst = ~(*ldst); |
|
2358 |
ldst++; |
|
2359 |
cnt -= sizeof(long); |
|
2360 |
} |
|
2361 |
dst = (unsigned char *)ldst; |
|
2362 |
} |
|
2363 |
while (cnt--) { |
|
2364 |
*dst = ~(*dst); |
|
2365 |
dst++; |
|
2366 |
} |
|
2367 |
RETURN ( self ); |
|
563 | 2368 |
} |
4927 | 2369 |
%}. |
5439 | 2370 |
self bitBlitBytesFrom:1 to:self size withConstant:16rFF rule:#bitXor: |
4927 | 2371 |
|
2372 |
" |
|
2373 |
#[1 2 3 4 5 6 7 8 9 10] copy invert |
|
8908 | 2374 |
#[1 2 3 4 5 6 7 8 9 10] copy |
24246 | 2375 |
bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor: |
16621 | 2376 |
|
2377 |
|l| |
|
18655 | 2378 |
l := ByteArray fromHexString:'0102030405060708090a0b0c0d0e0f1112131415161718191a1b1c1d1e1f'. |
16621 | 2379 |
Time millisecondsToRun:[ |
24246 | 2380 |
1000000 timesRepeat:[ l invert ]. |
18655 | 2381 |
] |
4927 | 2382 |
" |
24246 | 2383 |
|
2384 |
"Modified (comment): / 03-06-2019 / 18:08:47 / Claus Gittinger" |
|
563 | 2385 |
! |
2386 |
||
2387 |
reverse |
|
8908 | 2388 |
"reverse the order of my elements inplace - |
7338 | 2389 |
WARNING: this is a destructive operation, which modifies the receiver. |
19619 | 2390 |
Please use reversed (with a 'd' at the end) for a functional version. |
7338 | 2391 |
Written as a primitive for speed on image manipulations (mirror)" |
563 | 2392 |
|
2393 |
%{ /* NOCONTEXT */ |
|
2394 |
||
2395 |
REGISTER unsigned char *p1, *p2; |
|
2396 |
REGISTER int cnt; |
|
2397 |
REGISTER unsigned t; |
|
2398 |
OBJ cls; |
|
2399 |
||
1133 | 2400 |
if (__qClass(self) == @global(ByteArray)) { |
19619 | 2401 |
cnt = __byteArraySize(self); |
2402 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
|
2403 |
p2 = p1 + cnt - 1; |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2404 |
|
4927 | 2405 |
#if defined(__BSWAP) |
19619 | 2406 |
/* |
2407 |
* can we use the bswap instruction ? |
|
2408 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2409 |
*/ |
|
2410 |
if (__HAS_BSWAP() |
|
2411 |
&& ((cnt & 3) == 0)) { |
|
2412 |
unsigned int *ip1, *ip2; |
|
2413 |
||
2414 |
ip1 = (unsigned int *)p1; |
|
2415 |
ip2 = (unsigned int *)(p2 - 3); |
|
2416 |
||
2417 |
ip2 -= 7; |
|
2418 |
while (ip1 <= ip2) { |
|
2419 |
int t1, t2; |
|
2420 |
||
2421 |
t1 = ip1[0]; |
|
2422 |
t2 = ip2[7]; |
|
2423 |
ip2[7] = __BSWAP(t1); |
|
2424 |
ip1[0] = __BSWAP(t2); |
|
2425 |
||
2426 |
t1 = ip1[1]; |
|
2427 |
t2 = ip2[6]; |
|
2428 |
ip2[6] = __BSWAP(t1); |
|
2429 |
ip1[1] = __BSWAP(t2); |
|
2430 |
||
2431 |
t1 = ip1[2]; |
|
2432 |
t2 = ip2[5]; |
|
2433 |
ip2[5] = __BSWAP(t1); |
|
2434 |
ip1[2] = __BSWAP(t2); |
|
2435 |
||
2436 |
t1 = ip1[3]; |
|
2437 |
t2 = ip2[4]; |
|
2438 |
ip2[4] = __BSWAP(t1); |
|
2439 |
ip1[3] = __BSWAP(t2); |
|
2440 |
||
2441 |
ip1 += 4; |
|
2442 |
ip2 -= 4; |
|
2443 |
} |
|
2444 |
ip2 += 7; |
|
2445 |
||
2446 |
while (ip1 < ip2) { |
|
2447 |
int t; |
|
2448 |
||
2449 |
t = __BSWAP(*ip1); |
|
2450 |
*ip1++ = __BSWAP(*ip2); |
|
2451 |
*ip2-- = t; |
|
2452 |
} |
|
2453 |
||
2454 |
if (ip1 == ip2) { |
|
2455 |
int t; |
|
2456 |
t = *ip1; |
|
2457 |
t = __BSWAP(t); |
|
2458 |
*ip1 = t; |
|
2459 |
} |
|
2460 |
RETURN ( self ); |
|
2461 |
} |
|
2462 |
#endif /* __BSWAP (i.e. __i386__ && __GNUC__) */ |
|
2463 |
||
2464 |
p2 -= 7; |
|
2465 |
while (p1 <= p2) { |
|
2466 |
t = p1[0]; |
|
2467 |
p1[0] = p2[7]; |
|
2468 |
p2[7] = t; |
|
2469 |
||
2470 |
t = p1[1]; |
|
2471 |
p1[1] = p2[6]; |
|
2472 |
p2[6] = t; |
|
2473 |
||
2474 |
t = p1[2]; |
|
2475 |
p1[2] = p2[5]; |
|
2476 |
p2[5] = t; |
|
2477 |
||
2478 |
t = p1[3]; |
|
2479 |
p1[3] = p2[4]; |
|
2480 |
p2[4] = t; |
|
2481 |
||
2482 |
p1 += 4; |
|
2483 |
p2 -= 4; |
|
2484 |
} |
|
2485 |
p2 += 7; |
|
2486 |
||
2487 |
while (p1 < p2) { |
|
2488 |
t = *p1; |
|
2489 |
*p1++ = *p2; |
|
2490 |
*p2-- = t; |
|
2491 |
} |
|
2492 |
RETURN ( self ); |
|
563 | 2493 |
} |
2494 |
%}. |
|
2495 |
^ super reverse |
|
2585 | 2496 |
|
2497 |
" |
|
21736 | 2498 |
#[1 2 3 4 5] copy reverse |
2499 |
#[] copy reverse |
|
2500 |
#[1] copy reverse |
|
2501 |
#[1 2] copy reverse |
|
2502 |
#[1 2 3] copy reverse |
|
2503 |
#[1 2 3 4] copy reverse |
|
2504 |
#[1 2 3 4 5] copy reverse |
|
2505 |
#[1 2 3 4 5 6] copy reverse |
|
2506 |
#[1 2 3 4 5 6 7] copy reverse |
|
2507 |
#[1 2 3 4 5 6 7 8] copy reverse |
|
2508 |
#[1 2 3 4 5 6 7 8] copy reverseFrom:2 to:5 |
|
2509 |
#[1 2 3 4 5 6 7 8 9] copy reverse |
|
2510 |
#[1 2 3 4 5 6 7 8 9 10] copy reverse |
|
2511 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy reverse |
|
2512 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] copy reverse |
|
2513 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] copy reverse |
|
8908 | 2514 |
(1 to:255) asByteArray reverse |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2515 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2516 |
1 to:1024 do:[:i| |
19619 | 2517 |
|bytes test rBytes| |
2518 |
||
2519 |
bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2520 |
test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2521 |
rBytes := bytes copy. |
|
2522 |
rBytes reverse ~= test ifTrue:[ |
|
2523 |
self halt |
|
2524 |
]. |
|
2525 |
rBytes := bytes copy. |
|
2526 |
rBytes reverse reverse ~= bytes ifTrue:[ |
|
2527 |
self halt |
|
2528 |
] |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2529 |
]. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2530 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2531 |
Time millisecondsToRun:[ |
19619 | 2532 |
10000000 timesRepeat:[ |
2533 |
#[1 2 3 4 5 6 7 8] reverse |
|
2534 |
] |
|
8908 | 2535 |
] |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2536 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2537 |
|b| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2538 |
b := (0 to:255) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2539 |
Time millisecondsToRun:[ |
19619 | 2540 |
10000000 timesRepeat:[ |
2541 |
b reverse |
|
2542 |
] |
|
8908 | 2543 |
] |
2585 | 2544 |
" |
21736 | 2545 |
|
2546 |
"Modified (comment): / 01-05-2017 / 12:56:13 / cg" |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2547 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2548 |
|
22032 | 2549 |
swap:i1 with:i2 |
2550 |
"spap the bytes at i1 and i2" |
|
2551 |
||
2552 |
%{ /* NOCONTEXT */ |
|
2553 |
||
2554 |
REGISTER unsigned char *p; |
|
2555 |
unsigned int __i1, __i2; |
|
2556 |
int sz; |
|
2557 |
unsigned int t; |
|
2558 |
||
2559 |
if (__qClass(self) == @global(ByteArray) && __bothSmallInteger(i1, i2)) { |
|
2560 |
__i1 = __intVal(i1) - 1; |
|
2561 |
__i2 = __intVal(i2) - 1; |
|
2562 |
sz = __byteArraySize(self); |
|
2563 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2564 |
if ((__i1 < sz) && (__i2 < sz)) { |
|
2565 |
t = p[__i1]; |
|
2566 |
p[__i1] = p[__i2]; |
|
2567 |
p[__i2] = t; |
|
2568 |
RETURN ( self ); |
|
2569 |
} |
|
2570 |
} |
|
2571 |
%}. |
|
2572 |
^ super swap:i1 with:i2 |
|
2573 |
||
2574 |
" |
|
2575 |
#[1 2 3 4 5 6 7 8 9 10] copy swapIndex:1 and:10 |
|
2576 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapIndex:5 and:6 |
|
2577 |
" |
|
2578 |
! |
|
2579 |
||
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2580 |
swapBytes |
19414 | 2581 |
"swap bytes (of int16s) inplace - |
18655 | 2582 |
Expects that the receiver has an even number of bytes; |
15274 | 2583 |
if not, only the pairs excluding the last byte are swapped. |
2584 |
written as a primitive for speed on image grabbing (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
|
2585 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2586 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2587 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2588 |
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
|
2589 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2590 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2591 |
|
1133 | 2592 |
if (__qClass(self) == @global(ByteArray)) { |
19414 | 2593 |
cnt = __byteArraySize(self); |
2594 |
cnt = cnt & ~1; /* make it even */ |
|
2595 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2596 |
||
2597 |
while (cnt >= sizeof(INT)) { |
|
2598 |
unsigned INT i = ((unsigned INT *)p)[0]; |
|
15274 | 2599 |
|
2600 |
#if __POINTER_SIZE__ == 8 |
|
19414 | 2601 |
i = ((i>>8) & 0x00FF00FF00FF00FF) | ((i & 0x00FF00FF00FF00FF) << 8); |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2602 |
#else |
19414 | 2603 |
i = ((i>>8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8); |
15274 | 2604 |
#endif /* __POINTER_SIZE__ */ |
19414 | 2605 |
((unsigned INT *)p)[0] = i; |
2606 |
p += sizeof(INT); |
|
2607 |
cnt -= sizeof(INT); |
|
2608 |
} |
|
2609 |
while (cnt > 0) { |
|
2610 |
unsigned short s; |
|
2611 |
||
2612 |
s = ((unsigned short *)p)[0]; |
|
2613 |
s = (s >> 8) | (s << 8); |
|
2614 |
((unsigned short *)p)[0] = s; |
|
2615 |
p += 2; |
|
2616 |
cnt -= 2; |
|
2617 |
} |
|
2618 |
RETURN ( self ); |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2619 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2620 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2621 |
^ 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
|
2622 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2623 |
" |
15274 | 2624 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9] |
2625 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9 11] |
|
18655 | 2626 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18] copy swapBytes |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2627 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2628 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2629 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2630 |
swapLongs |
8908 | 2631 |
"swap long bytes inplace |
4927 | 2632 |
- any partial longs at the end are not swapped." |
2633 |
||
2634 |
self swapLongsFrom:1 to:self size |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2635 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2636 |
" |
8908 | 2637 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
2638 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
|
2639 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
|
2640 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongs |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2641 |
" |
4365 | 2642 |
! |
2643 |
||
2644 |
swapLongsFrom:startIndex to:endIndex |
|
19414 | 2645 |
"swap longs (int32s) inplace |
8908 | 2646 |
- any partial longs at the end are not swapped. |
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2647 |
Swapping is from startIndex to (exclusiv) endIndex; |
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2648 |
indexing starts at 1." |
4365 | 2649 |
|
2650 |
%{ /* NOCONTEXT */ |
|
2651 |
||
2652 |
REGISTER unsigned char *p; |
|
2653 |
REGISTER int limit; |
|
2654 |
REGISTER unsigned t; |
|
2655 |
||
2656 |
if ((__qClass(self) == @global(ByteArray)) |
|
2657 |
&& __isSmallInteger(startIndex) |
|
2658 |
&& __isSmallInteger(endIndex)) { |
|
19414 | 2659 |
int __idx = __intVal(startIndex); |
2660 |
int __endIdx = __intVal(endIndex); |
|
2661 |
||
2662 |
limit = __byteArraySize(self); |
|
2663 |
if (__endIdx < limit) { |
|
2664 |
limit = __endIdx; |
|
2665 |
} |
|
2666 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2667 |
p = p + __idx - 1; |
|
2668 |
||
2669 |
limit = limit - 4 + 1; |
|
4927 | 2670 |
|
2671 |
#if defined(__BSWAP) |
|
19414 | 2672 |
/* |
2673 |
* can we use the bswap instruction ? |
|
2674 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2675 |
*/ |
|
2676 |
if (__HAS_BSWAP() |
|
2677 |
&& (((unsigned int)p & 3) == 0)) { |
|
2678 |
unsigned int *ip; |
|
2679 |
ip = (unsigned int *)p; |
|
2680 |
||
2681 |
while (__idx <= limit) { |
|
2682 |
*ip = __BSWAP(*ip); |
|
2683 |
ip++; |
|
2684 |
__idx += 4; |
|
2685 |
} |
|
2686 |
RETURN (self); |
|
2687 |
} |
|
4927 | 2688 |
#endif /* __BSWAP */ |
2689 |
||
19414 | 2690 |
while (__idx <= limit) { |
2691 |
t = p[0]; |
|
2692 |
p[0] = p[3]; |
|
2693 |
p[3] = t; |
|
2694 |
t = p[1]; |
|
2695 |
p[1] = p[2]; |
|
2696 |
p[2] = t; |
|
2697 |
p += 4; |
|
2698 |
__idx += 4; |
|
2699 |
} |
|
2700 |
RETURN ( self ); |
|
4365 | 2701 |
} |
2702 |
%}. |
|
2703 |
^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly |
|
2704 |
||
2705 |
" |
|
8908 | 2706 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:3 |
2707 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:4 |
|
2708 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:5 |
|
2709 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:6 |
|
2710 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:7 |
|
2711 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:8 |
|
2712 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongsFrom:1 to:11 |
|
2713 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongsFrom:1 to:12 |
|
2714 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongsFrom:1 to:13 |
|
2715 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:5 to:10 |
|
4365 | 2716 |
" |
1 | 2717 |
! ! |
550 | 2718 |
|
10028 | 2719 |
|
550 | 2720 |
!ByteArray methodsFor:'printing & storing'! |
2721 |
||
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2722 |
displayOn:aGCOrStream |
1244 | 2723 |
"return a printed representation of the receiver for displaying" |
2724 |
||
21539 | 2725 |
|cls| |
2726 |
||
17535 | 2727 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21539 | 2728 |
"/ old ST80 means: draw-yourself on a GC. |
18596 | 2729 |
cls := self class. |
2730 |
((cls == ByteArray or:[cls == ImmutableByteArray]) and:[aGCOrStream isStream]) ifTrue:[ |
|
21539 | 2731 |
self storeOn:aGCOrStream. |
2732 |
^ self |
|
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2733 |
]. |
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2734 |
^ super displayOn:aGCOrStream |
563 | 2735 |
|
21539 | 2736 |
"Created: / 25-10-1995 / 13:33:26 / cg" |
2737 |
"Modified: / 22-04-1996 / 12:54:06 / cg" |
|
2738 |
"Modified (comment): / 22-02-2017 / 16:54:27 / cg" |
|
563 | 2739 |
! |
2740 |
||
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2741 |
printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2742 |
"append a printed representation to aStream" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2743 |
|
12487 | 2744 |
|cls| |
2745 |
||
2746 |
"/ care for subclasses |
|
12879 | 2747 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2748 |
aStream nextPutAll:'#['. |
|
2749 |
self |
|
2750 |
do:[:byte | byte printOn:aStream] |
|
2751 |
separatedBy:[aStream space]. |
|
2752 |
aStream nextPut:$]. |
|
2753 |
^ self |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2754 |
]. |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2755 |
^ super printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2756 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2757 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2758 |
#[1 2 3 4 5] printOn:Transcript |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2759 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2760 |
|
8908 | 2761 |
#[1 2 3 4 5] storeString |
2762 |
#[1 2 3 4 5] displayString |
|
2763 |
#[1 2 3 4 5] printString |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2764 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2765 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2766 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2767 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2768 |
! |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2769 |
|
4050 | 2770 |
printOn:aStream base:radix |
2771 |
"append a printed representation to aStream in the given number base." |
|
2772 |
||
6125 | 2773 |
^ self printOn:aStream base:radix showRadix:false |
2774 |
||
2775 |
" |
|
2776 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2777 |
'Hello World' printOn:Transcript base:2 |
|
2778 |
||
8908 | 2779 |
#[1 2 3 4 5] storeString |
2780 |
#[1 2 3 4 5] displayString |
|
2781 |
#[1 2 3 4 5] printString |
|
6125 | 2782 |
" |
2783 |
||
2784 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2785 |
"Modified: / 31.10.2001 / 09:43:56 / cg" |
|
2786 |
! |
|
2787 |
||
2788 |
printOn:aStream base:radix showRadix:showRadix |
|
2789 |
"append a printed representation to aStream in the given number base." |
|
2790 |
||
12487 | 2791 |
|cls| |
2792 |
||
2793 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 2794 |
"/ care for subclasses |
2795 |
aStream nextPutAll:'#['. |
|
2796 |
self |
|
2797 |
do:[:byte | byte printOn:aStream base:radix showRadix:showRadix] |
|
2798 |
separatedBy:[aStream space]. |
|
2799 |
aStream nextPut:$]. |
|
2800 |
^ self |
|
4050 | 2801 |
]. |
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2802 |
^ self printOn:aStream |
4050 | 2803 |
|
2804 |
" |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2805 |
#[1 2 3 4 5] printOn:Transcript base:2 |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2806 |
'Hello World' printOn:Transcript base:2 |
4050 | 2807 |
|
8908 | 2808 |
#[1 2 3 4 5] storeString |
2809 |
#[1 2 3 4 5] displayString |
|
2810 |
#[1 2 3 4 5] printString |
|
4050 | 2811 |
" |
2812 |
||
2813 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2814 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
6125 | 2815 |
"Created: / 31.10.2001 / 09:43:41 / cg" |
4050 | 2816 |
! |
2817 |
||
563 | 2818 |
storeOn:aStream |
2819 |
"append a printed representation from which the receiver can be |
|
2820 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2821 |
||
12487 | 2822 |
|cls| |
2823 |
||
21483 | 2824 |
cls := self class. |
2825 |
(cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
2826 |
"/ care for subclasses |
|
2827 |
aStream nextPutAll:'#['. |
|
2828 |
self |
|
2829 |
do:[:byte | byte storeOn:aStream] |
|
2830 |
separatedBy:[aStream space]. |
|
2831 |
aStream nextPut:$]. |
|
2832 |
^ self |
|
563 | 2833 |
]. |
2834 |
^ super storeOn:aStream |
|
2835 |
||
2836 |
" |
|
8908 | 2837 |
#[1 2 3 4 5] storeOn:Transcript |
2838 |
||
2839 |
#[1 2 3 4 5] storeString |
|
2840 |
#[1 2 3 4 5] displayString |
|
2841 |
#[1 2 3 4 5] printString |
|
563 | 2842 |
" |
1082 | 2843 |
|
21483 | 2844 |
"Modified: / 12-09-1997 / 22:11:33 / cg" |
2845 |
"Modified: / 17-02-2017 / 10:50:37 / stefan" |
|
550 | 2846 |
! ! |
2847 |
||
7822 | 2848 |
!ByteArray methodsFor:'queries'! |
1082 | 2849 |
|
22933 | 2850 |
characterSize |
2851 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32). |
|
2852 |
Needed in case someone writes bytes to a CharacterWriteStream (comanche response)" |
|
2853 |
||
2854 |
^ 8 |
|
2855 |
! |
|
2856 |
||
16804 | 2857 |
containsNon7BitAscii |
2858 |
"return true, if any byte in the receiver has the 7th bit on. |
|
2859 |
This my look as a too specific operation to be put here, |
|
2860 |
put it is very helpful for UTF8 string reading (Java class reader), |
|
2861 |
to quickly determine, if UTF8 decoding is needed or not. |
|
18596 | 2862 |
As most strings in a class file are in fact only containing 7bit ascii, |
16804 | 2863 |
this should speedup class file reading considerably" |
2864 |
||
16805 | 2865 |
%{ /* NOCONTEXT */ |
16804 | 2866 |
REGISTER unsigned char *cp; |
2867 |
REGISTER unsigned char *endP; |
|
2868 |
||
18611 | 2869 |
if (__isByteArrayLike(self)) { |
18655 | 2870 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
2871 |
endP = cp + __byteArraySize(self); |
|
16806 | 2872 |
#if __POINTER_SIZE__ == 8 |
18655 | 2873 |
while (cp+8 < endP) { |
2874 |
if ( ((unsigned INT *)cp)[0] & 0x8080808080808080) RETURN( true ); |
|
2875 |
cp += 8; |
|
2876 |
} |
|
16806 | 2877 |
#endif |
18655 | 2878 |
while (cp+4 < endP) { |
2879 |
if ( ((unsigned int *)cp)[0] & 0x80808080) RETURN( true ); |
|
2880 |
cp += 4; |
|
2881 |
} |
|
2882 |
while (cp < endP) { |
|
2883 |
if (*cp++ & 0x80) RETURN( true ); |
|
2884 |
} |
|
2885 |
RETURN ( false ); |
|
16804 | 2886 |
} |
2887 |
%} |
|
2888 |
. |
|
2889 |
^ self contains:[:b | b bitTest:16r80]. |
|
2890 |
||
2891 |
" |
|
2892 |
#[1 2 3 1 2 3 1 2 127 ] containsNon7BitAscii |
|
2893 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] containsNon7BitAscii |
|
2894 |
" |
|
2895 |
! |
|
2896 |
||
18966 | 2897 |
containsNon8BitElements |
2898 |
"return true, if one of my elements is larger than a single byte. |
|
2899 |
Per definition not." |
|
2900 |
||
2901 |
^ false. |
|
2902 |
! |
|
2903 |
||
23710 | 2904 |
isEmpty |
2905 |
"return true if the receiver is empty (i.e. if size == 0) |
|
2906 |
Redefined here for performance" |
|
2907 |
||
2908 |
%{ /* NOCONTEXT */ |
|
2909 |
#ifndef __SCHTEAM__ |
|
2910 |
if (__isByteArrayLike(self)) { |
|
2911 |
RETURN ( (__byteArraySize(self) == 0) ? true : false); |
|
2912 |
} |
|
2913 |
#endif /* ! __SCHTEAM__ */ |
|
2914 |
%}. |
|
2915 |
^ self size == 0 |
|
2916 |
||
2917 |
"Created: / 16-02-2017 / 15:02:03 / stefan" |
|
2918 |
! |
|
2919 |
||
20306 | 2920 |
isValidElement:anObject |
2921 |
"return true, if I can hold this kind of object" |
|
2922 |
||
2923 |
^ anObject isInteger |
|
2924 |
and:[ (anObject >= self class minVal) |
|
2925 |
and:[ (anObject <= self class maxVal) ]] |
|
2926 |
! |
|
2927 |
||
563 | 2928 |
max |
2929 |
"return the maximum value in the receiver - |
|
8908 | 2930 |
redefined to speedup image processing and sound-player |
563 | 2931 |
(which need a fast method for this on byteArrays)" |
550 | 2932 |
|
563 | 2933 |
%{ /* NOCONTEXT */ |
550 | 2934 |
|
2935 |
REGISTER unsigned char *cp; |
|
563 | 2936 |
REGISTER int index, max; |
2937 |
int len; |
|
550 | 2938 |
|
14135 | 2939 |
if (__isByteArrayLike(self)) { |
19414 | 2940 |
max = 0; |
2941 |
index = 0; |
|
2942 |
len = __qSize(self) - OHDR_SIZE; |
|
2943 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2944 |
||
2945 |
if (len > 0) { |
|
2946 |
while (++index <= len) { |
|
2947 |
unsigned int byte; |
|
2948 |
||
2949 |
byte = *cp; |
|
2950 |
cp++; |
|
2951 |
if (byte > max) { |
|
2952 |
max = byte; |
|
2953 |
if (byte == 255) break; |
|
2954 |
} |
|
2955 |
} |
|
2956 |
RETURN ( __mkSmallInteger(max) ); |
|
2957 |
} |
|
550 | 2958 |
} |
563 | 2959 |
%}. |
2960 |
^ super max |
|
550 | 2961 |
|
2962 |
" |
|
8908 | 2963 |
#[1 2 3 1 2 3 1 2 19] max |
19414 | 2964 |
#[] max |
550 | 2965 |
" |
5554 | 2966 |
! |
550 | 2967 |
|
23710 | 2968 |
notEmpty |
2969 |
"return true if the receiver is not empty (i.e. if size ~~ 0) |
|
2970 |
Redefined here for performance" |
|
2971 |
||
2972 |
%{ /* NOCONTEXT */ |
|
2973 |
#ifndef __SCHTEAM__ |
|
2974 |
if (__isByteArrayLike(self)) { |
|
2975 |
RETURN ( (__byteArraySize(self) != 0) ? true : false); |
|
2976 |
} |
|
2977 |
#endif /* ! __SCHTEAM__ */ |
|
2978 |
%}. |
|
2979 |
^ self size ~~ 0 |
|
2980 |
||
2981 |
"Created: / 16-02-2017 / 15:02:36 / stefan" |
|
2982 |
! |
|
2983 |
||
19645 | 2984 |
startsWith:aByteOrByteArray |
2985 |
"return true, if the receiver starts with something, aStringOrChar. |
|
2986 |
If the argument is empty, true is returned. |
|
2987 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
2988 |
which are both inconsistent w.r.t. an empty argument." |
|
2989 |
||
2990 |
%{ /* NOCONTEXT */ |
|
2991 |
int len1, len2; |
|
2992 |
REGISTER unsigned char *src1, *src2; |
|
2993 |
unsigned char c; |
|
2994 |
REGISTER OBJ slf = self; |
|
2995 |
||
2996 |
if (__qIsByteArrayLike(slf) &&__isByteArrayLike(aByteOrByteArray)) { |
|
2997 |
src1 = __byteArrayVal(slf); |
|
2998 |
src2 = __byteArrayVal(aByteOrByteArray); |
|
2999 |
||
3000 |
if (src1[0] != src2[0]) { |
|
3001 |
if (__qSize(aByteOrByteArray) == OHDR_SIZE) { |
|
3002 |
RETURN (true); |
|
3003 |
} |
|
3004 |
RETURN ( false ); |
|
3005 |
} |
|
3006 |
||
3007 |
len1 = __qSize(slf); |
|
3008 |
len2 = __qSize(aByteOrByteArray); |
|
3009 |
if (len1 < len2) { |
|
3010 |
RETURN ( false ); |
|
3011 |
} |
|
3012 |
||
3013 |
# ifdef UINT64 |
|
3014 |
while (len2 > (OHDR_SIZE+sizeof(UINT64))) { |
|
3015 |
if ( ((UINT64 *)src1)[0] != ((UINT64 *)src2)[0] ) { |
|
3016 |
RETURN (false); |
|
3017 |
} |
|
3018 |
len2 -= sizeof(UINT64); |
|
3019 |
src1 += sizeof(UINT64); |
|
3020 |
src2 += sizeof(UINT64); |
|
3021 |
} |
|
3022 |
# else |
|
3023 |
# ifdef __UNROLL_LOOPS__ |
|
3024 |
while (len2 > (OHDR_SIZE+sizeof(INT)*4)) { |
|
3025 |
if ( ((unsigned INT *)src1)[0] != ((unsigned INT *)src2)[0]) { |
|
3026 |
RETURN (false); |
|
3027 |
} |
|
3028 |
if ( ((unsigned INT *)src1)[1] != ((unsigned INT *)src2)[1]) { |
|
3029 |
RETURN (false); |
|
3030 |
} |
|
3031 |
if ( ((unsigned INT *)src1)[2] != ((unsigned INT *)src2)[2]) { |
|
3032 |
RETURN (false); |
|
3033 |
} |
|
3034 |
if ( ((unsigned INT *)src1)[3] != ((unsigned INT *)src2)[3]) { |
|
3035 |
RETURN (false); |
|
3036 |
} |
|
3037 |
len2 -= sizeof(INT)*4; |
|
3038 |
src1 += sizeof(INT)*4; |
|
3039 |
src2 += sizeof(INT)*4; |
|
3040 |
} |
|
3041 |
# endif /* __UNROLL_LOOPS__ */ |
|
3042 |
# endif /* UINT64 */ |
|
3043 |
||
3044 |
while (len2 > (OHDR_SIZE+sizeof(INT))) { |
|
3045 |
if ( ((unsigned INT *)src1)[0] != ((unsigned INT *)src2)[0]) { |
|
3046 |
RETURN (false); |
|
3047 |
} |
|
3048 |
len2 -= sizeof(INT); |
|
3049 |
src1 += sizeof(INT); |
|
3050 |
src2 += sizeof(INT); |
|
3051 |
} |
|
3052 |
||
19764 | 3053 |
for ( ; len2 > OHDR_SIZE; len2--) { |
19645 | 3054 |
if (*src1++ != *src2++) { |
3055 |
RETURN (false); |
|
3056 |
} |
|
3057 |
} |
|
3058 |
RETURN (true); |
|
3059 |
} |
|
3060 |
if (__isSmallInteger(aByteOrByteArray)) { |
|
3061 |
int val = __intVal(aByteOrByteArray); |
|
3062 |
if (__byteArraySize(slf) > 0) { |
|
3063 |
RETURN ( (__byteArrayVal(slf)[0] == val) ? true : false); |
|
3064 |
} |
|
3065 |
RETURN ( false ); |
|
3066 |
} |
|
3067 |
%}. |
|
3068 |
^ super startsWith:aByteOrByteArray |
|
3069 |
||
3070 |
" |
|
3071 |
#[1 2 3 4 5 6 7 8 9 10] startsWith:#[ 1 2 3 4 5] |
|
19764 | 3072 |
#[1 2 3 4] startsWith:#[ 1 3 4 5] |
19645 | 3073 |
#[1 2 3 4 5 6 7 8 9 10] startsWith:#[ 0 1 2 3 4 5] |
3074 |
#[1 2 3 4 5 6 7 8 9 10] startsWith:#(1 2 3 4 5) |
|
3075 |
#[1 2 3 4 5 6 7 8 9 10] startsWith:1 |
|
3076 |
#[1 2 3 4 5 6 7 8 9 10] startsWith:2 |
|
3077 |
" |
|
3078 |
! |
|
3079 |
||
550 | 3080 |
usageCounts |
3081 |
"return an array filled with value-counts - |
|
3082 |
This is needed in the bitmap/image classes to get info on color usage. |
|
3083 |
(i.e. to build up a histogram of color usage within an image)." |
|
3084 |
||
3085 |
|counts| |
|
3086 |
||
3087 |
counts := Array basicNew:256. |
|
3088 |
||
3089 |
%{ /* STACK: 2000 */ |
|
3090 |
||
3091 |
REGISTER unsigned char *cp; |
|
3092 |
REGISTER int nByte; |
|
3093 |
REGISTER int index; |
|
8913 | 3094 |
INT icounts[256]; |
550 | 3095 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
3096 |
if (__isByteArrayLike(self) && __isArray(counts)) { |
12879 | 3097 |
/* |
3098 |
* zero counts |
|
3099 |
*/ |
|
3100 |
for (index=0; index<256; index++) { |
|
3101 |
icounts[index] = 0; |
|
3102 |
} |
|
3103 |
||
3104 |
/* |
|
3105 |
* count |
|
3106 |
*/ |
|
3107 |
nByte = __qSize(self) - OHDR_SIZE; |
|
3108 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
3109 |
while (nByte--) { |
|
3110 |
icounts[*cp++]++; |
|
3111 |
} |
|
3112 |
||
3113 |
/* |
|
3114 |
* make it real counts |
|
3115 |
*/ |
|
3116 |
for (index=0; index<256; index++) { |
|
3117 |
__ArrayInstPtr(counts)->a_element[index] = __mkSmallInteger(icounts[index]); |
|
3118 |
} |
|
3119 |
RETURN ( counts ); |
|
550 | 3120 |
} |
3121 |
%} |
|
3122 |
. |
|
3123 |
self primitiveFailed |
|
3124 |
||
3125 |
" |
|
8908 | 3126 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
550 | 3127 |
" |
3128 |
! |
|
3129 |
||
563 | 3130 |
usedValues |
3131 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
8908 | 3132 |
This is needed specially in the bitmap/Imageclasses to find used colors |
2866 | 3133 |
of an image." |
550 | 3134 |
|
563 | 3135 |
%{ /* STACK: 400 */ |
550 | 3136 |
|
3137 |
REGISTER unsigned char *cp; |
|
2866 | 3138 |
unsigned char *endp; |
563 | 3139 |
REGISTER int len; |
2890 | 3140 |
OBJ result; |
2866 | 3141 |
union { |
12879 | 3142 |
unsigned char flags[256]; |
3143 |
int toForceAlignmentOfFlags; |
|
2866 | 3144 |
} f; |
3145 |
||
3146 |
#ifdef TO_BE_MEASURED |
|
3147 |
int coverage = 0; |
|
3148 |
#endif |
|
550 | 3149 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
3150 |
if (__isByteArrayLike(self)) { |
12879 | 3151 |
memset(f.flags, 0, sizeof(f.flags)); |
3152 |
len = __qSize(self) - OHDR_SIZE; |
|
3153 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
3154 |
||
3155 |
/* for each used byte, set flag */ |
|
3156 |
while (len > 0) { |
|
2866 | 3157 |
#ifdef TO_BE_MEASURED |
12879 | 3158 |
unsigned byte; |
3159 |
||
3160 |
byte = *cp; |
|
3161 |
if (! f.flags[byte]) { |
|
3162 |
f.flags[byte] = 1; |
|
3163 |
coverage++; |
|
3164 |
if (coverage == 256) { |
|
3165 |
/* no need to scan rest */ |
|
3166 |
break; |
|
3167 |
} |
|
3168 |
} |
|
2866 | 3169 |
#else |
12879 | 3170 |
f.flags[*cp] = 1; |
2866 | 3171 |
#endif |
12879 | 3172 |
cp++; |
3173 |
len--; |
|
3174 |
} |
|
3175 |
||
3176 |
/* count 1's */ |
|
3177 |
len = 0; |
|
3178 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
3179 |
if ( *((unsigned int *)cp)) { |
|
3180 |
if (cp[0]) len++; |
|
3181 |
if (cp[1]) len++; |
|
3182 |
if (cp[2]) len++; |
|
3183 |
if (cp[3]) len++; |
|
3184 |
} |
|
3185 |
cp += 4; |
|
3186 |
} |
|
3187 |
||
3188 |
/* create ByteArray of used values */ |
|
3189 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
3190 |
if (result) { |
|
3191 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
3192 |
for (len=0; len < 256; len++) { |
|
3193 |
if (f.flags[len]) |
|
3194 |
*cp++ = len; |
|
3195 |
} |
|
3196 |
RETURN ( result ); |
|
3197 |
} |
|
550 | 3198 |
} |
2866 | 3199 |
%}. |
3200 |
^ self asIdentitySet asByteArray |
|
550 | 3201 |
|
3202 |
" |
|
8908 | 3203 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 3204 |
" |
3205 |
! ! |
|
3206 |
||
22032 | 3207 |
|
7822 | 3208 |
!ByteArray methodsFor:'searching'! |
3209 |
||
3210 |
indexOf:aByte startingAt:start |
|
3211 |
"return the index of the first occurrence of the argument, aByte |
|
3212 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
3213 |
- reimplemented here for speed" |
|
3214 |
||
3215 |
%{ /* NOCONTEXT */ |
|
3216 |
||
3217 |
REGISTER unsigned char *cp; |
|
8913 | 3218 |
REGISTER INT index; |
3219 |
REGISTER int byteValue; |
|
7822 | 3220 |
REGISTER int len; |
3221 |
OBJ cls; |
|
3222 |
||
3223 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
18655 | 3224 |
byteValue = __intVal(aByte); |
3225 |
||
3226 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
3227 |
/* |
|
3228 |
* searching for something which cannot be found |
|
3229 |
*/ |
|
3230 |
RETURN ( __mkSmallInteger(0) ); |
|
3231 |
} |
|
3232 |
||
3233 |
if (__isSmallInteger(start)) { |
|
3234 |
index = __intVal(start); |
|
3235 |
len = __byteArraySize(self); |
|
3236 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
3237 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
3238 |
int nInst; |
|
3239 |
||
3240 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
3241 |
cp += nInst; |
|
3242 |
len -= nInst; |
|
3243 |
} |
|
3244 |
cp += index - 1; |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3245 |
#ifdef __UNROLL_LOOPS__ |
18655 | 3246 |
while ((index+4) < len) { |
3247 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); } |
|
3248 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); } |
|
3249 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); } |
|
3250 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); } |
|
3251 |
index += 4; |
|
3252 |
cp += 4; |
|
3253 |
} |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3254 |
#endif |
18655 | 3255 |
while (index <= len) { |
3256 |
if (*cp == byteValue) { |
|
3257 |
RETURN ( __mkSmallInteger(index) ); |
|
3258 |
} |
|
3259 |
index++; |
|
3260 |
cp++; |
|
3261 |
} |
|
3262 |
RETURN ( __mkSmallInteger(0) ); |
|
3263 |
} |
|
7822 | 3264 |
} |
3265 |
%}. |
|
3266 |
^ super indexOf:aByte startingAt:start |
|
3267 |
||
3268 |
" |
|
14135 | 3269 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1 |
7822 | 3270 |
" |
3271 |
! ! |
|
3272 |
||
22032 | 3273 |
|
5554 | 3274 |
!ByteArray methodsFor:'testing'! |
3275 |
||
3276 |
isByteArray |
|
3277 |
"return true, if the receiver is some kind of bytearray; |
|
3278 |
true is returned here - the method is redefined from Object." |
|
3279 |
||
3280 |
^ true |
|
3281 |
||
3282 |
||
3283 |
! |
|
3284 |
||
23800 | 3285 |
isIntegerArray |
3286 |
"return true if the receiver has integer elements. |
|
3287 |
These are Byte- and Integer arrays; both signed and unsigned" |
|
3288 |
||
3289 |
^ true |
|
3290 |
||
3291 |
"Created: / 02-03-2019 / 23:11:00 / Claus Gittinger" |
|
3292 |
! |
|
3293 |
||
5554 | 3294 |
isLiteral |
3295 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
3296 |
(i.e. can be used in constant arrays)" |
|
3297 |
||
3298 |
"/ no, simply returning true here is a mistake: |
|
8908 | 3299 |
"/ it could be a subclass of ByteArray |
5554 | 3300 |
"/ (of which the compiler does not know at all ...) |
3301 |
||
3302 |
^ self class == ByteArray |
|
3303 |
||
3304 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
3305 |
! ! |
|
3306 |
||
24484 | 3307 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3308 |
!ByteArray class methodsFor:'documentation'! |
628 | 3309 |
|
3310 |
version |
|
18584 | 3311 |
^ '$Header$' |
12091 | 3312 |
! |
3313 |
||
3314 |
version_CVS |
|
18584 | 3315 |
^ '$Header$' |
628 | 3316 |
! ! |
15274 | 3317 |