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