Arrays

Alex Tweedly alex at tweedly.net
Mon Feb 7 21:53:01 EST 2005


Ben Fisher wrote:

>I'm working on image processing. Let's say I wanted to lighten every shade in the image by 2. The way I do this now is by getting the chartonum of each char in the imagedata (except for every fourth char which is always 0), adding 2, getting the numtochar, and putting this after a variable. Then I set the imagedata of the image to that variable. It works great, but it's a little slow. Too slow for a smooth animation which is what I'm trying to accomplish.
>
>Is there a way to directly add something to the imagedata in binary? For example, adding 2 to all the red in an image in one step rather than going through all the data manually. Or is converting binary>decimal>binary necessary?
>
Frank's suggestion of blendLevel (with perhaps a transparent and/or 
primary-coloured overlay) is probably your best hope for a quick solution.

Part of the solution SHOULD be binaryEncode/binaryDecode - but 
unfortunately they are currently limited in what they can do (without 
any obvious reason why they need to be). I'm planning to submit an 
enhancement request in this area - and thought I'd run a draft by the 
list to see if there are any suggestions.

Enhancement suggestion for binaryEncode/binaryDecode

1. Provide explicit "big-endian" and "little-endian" integer codes.
binaryEncode/Decode provide codes for host order ('s', 'S', 'i', 'I') 
and network-order ('n', 'N'). However, some file formats (notably EXIF 
metadata in JPG and other photo formats) specify the byte-order as 
big-endian or little-endian, so can only be decoded by building in 
knowledge of the endian-ness of the host (and in the case of big-endian 
hosts such as PPC, there is no way to do a little-endian integer 
(en)decode).

2. Bulk conversion.
binaryDecode could provide a mechanism to easily convert a block of 
similar integers; the essential part of it is already there (for example 
a code value such as "12N" to extract the next 12 (network-order) 4-byte 
integers. However, this can only be used in conjunction with an explicit 
variable list, such as
   put binaryDecode("12N", buffer, i1, i2, i3, i4, i5, .... i12) into 
tResult
which is very cumbersome to deal with.

This should be enhanced to allow one of the following:

2a. array container instead of variable list.
    put binaryDecode("12N", buffer, myArray) into tResult
    repeat with i= 1 to tResult
        dealWith(myArray[i])
    end repeat

2b. single variable, with each decoded value going into a separate item 
(or word, or line)
     put binaryDecode("12N", buffer, myVar) into tResult
    repeat for each item thisOne in myVar
        dealWith(thisOne)
    end repeat

And of course, a corresponding change for binaryEncode.

3. Binary and array manipulations

3a. The arithmetic operators can all handle the case where the container 
is an array, and implement either array-to-array or scalar-to-array 
operations. It would be useful to extend this mechanism to include the 
binary operators (bitAnd, bitOr, etc.)

3b. It would also be useful if there were the option of "cycling" the 
shorter array, rather than treating the non-existent entries as zero.
(see below for example)

3c. This style of array manipulations could be extended to binary 
buffers, with both bit and arithmetic operations happening to each byte 
of the buffer.  
(extend to buffers of 2-bye and 4-byte integer ???? probably not).

example of 3b and 3c: adding value to a single colour channel in 
imagedata; you would supply one operand as 0200 (as bytes, not as a 
character string), and add that to the binary buffer or array.

4. Add a new chunk type "byte"
(I don't really expect this one to happen :-)

Thus one could do
    add 2 to byte 4 of myVar
    bitAnd x'7f' to bytes 12 to 44 of myVar


- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.5 - Release Date: 03/02/2005



More information about the use-livecode mailing list