Manipulating imagedata - quickest technique?
Wilhelm Sanke
sanke at hrz.uni-kassel.de
Mon Apr 3 09:43:28 EDT 2006
On Mon Apr 3 2006 David Bovill david at openpartnership.net wrote:
> What's the quickest technique? This script is based on some of Chipps
> work... baiscally it loops through the whole imageData 4 chars at a time
> and manipulates each chunk before adding it to the new data.
>
> A few questions:
>
> 1) Is this the fastest way?
From my very recent experience I think the looping approach in your
example Chipp uses is indeed very fast and could hardly be optimized.
But much depends on the amount of math - and possibly if-control
structures - you need in that loop to produce new imagedata or to change
them.
Looping, however, only works when you want to change *all* imagedata in
one single aspect. If position of the pixels matters, for example, when
you want to mirror parts of an image, you need to apply a double repeat
loop determining the vertical and horizontal positions, which is almost
identical in speed compared with a one-dimensional loop (see some sample
scripts below).
> 2) Can someone explain the use of binaryEncode - is this faster or
> slower?
IMHO the use of binaryEncode is *not* faster than addressing the "chars"
of the imagedata. Again, the needed math in between is what matters most.
> 3) Is there a way to cash some intermediary form of data that is
> faster to manipulate?
> (snip) So what sort of format would make the most sense to store
> this data in - some sort of table / array?
As I understand it, the imagedata is already an *array* of a special kind.
I find that manipulating the color information in an 160 x 120 array
(used in my "Colorpattern Toolkit" for the colored chars of a field) is
basically slower than changing the imagedata of an 640 X 480 image
(which I use in my soon to be released "ImageData Toolkit").
Regards,
Wilhelm Sanke
<http://www.sanke.org/MetaMedia>
<http://www.sanke.org/ImageDataArt>
<http://www.sanke.org/Software/VanishingImage.zip>
=================================
P.S.: Three sample scripts for an image size of 640 X 480 pixels - with
execution speed (WindowsXP, 2 GHz)
1. "Create horizontal lines of different color" - 3 seconds
on mouseUp
put the milliseconds into Start
put empty into iData
put random(255) into R
put random(255) into G
put random(255) into B
put random(255) into x
put random(255) into y
put random(255) into z
repeat with i = 1 to 480
add x to R
add y to G
add z to B
repeat with j = 1 to 640
put binaryEncode("CCCC",0,R,G,B) into newPixel
put newPixel after iData
end repeat
end repeat
set the imageData of img 2 to iData
put the milliseconds - Start into fld "test"
end mouseUp
2. "duplicate the color values" - 1.7 seconds
on mouseUp
set the cursor to watch
put the milliseconds into Start
put the imageData of image 2 into iData
put idata into idata2
put 0 into counter
repeat for each char C in idata
add 1 to counter
if counter mod 4 <> 1 then # the "open" zero item before R,G,B
put chartonum(C) into tC
put 2* tC into tC
if tC > 255 then put tC - 255 into tC
put numtochar(tC) into char counter of idata2
end if
end repeat
set the imageData of image 2 to iData2
put the milliseconds - Start into fld "test"
end mouseUp
3. "produce complementary colors" - 1.4 seconds (the two-dimensional
repeat structure is not needed here, but is nevertheless faster than the
above "repeat for each" structure)
on mouseUp
set the cursor to watch
put the milliseconds into Start
put the imageData of image 2 into iData
put 2560 into re
repeat with i = 0 to 479
repeat with j = 0 to 639
put 255 - chartonum(char (i*re + (j*4+2)) of idata) into tC
put numtochar(tC) into char (i*re + (j*4+2)) of idata
put 255 - chartonum(char (i*re + (j*4+3)) of idata) into tC
put numtochar(tC) into char (i*re + (j*4+3)) of idata
put 255 - chartonum(char (i*re + (j*4+4)) of idata) into tC
put numtochar(tC) into char (i*re + (j*4+4)) of idata
end repeat
end repeat
set the imageData of image 2 to iData
put the milliseconds - Start into fld "test"
end mouseUp
More information about the use-livecode
mailing list