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