Turning imageData into alphaData (quickly)?

Ian Wood revlist at azurevision.co.uk
Mon Mar 13 15:51:50 EST 2006


On 13 Mar 2006, at 16:56, Wilhelm Sanke wrote:

> Hi Ian,
>
> I modified your script to the solution below, i.e. I took the  
> script of my "gray-scale" button of my ImageData Toolkit and  
> adapted it to your needs.
> The script is for a 640 X 480 image, meaning that more than about  
> one million of chars have to be processed (640 * 480 * 3)
> Running the script takes *one* second on my Windows XP computer (2  
> GHz):
>
> on mouseUp
>  set the cursor to watch
>  put the milliseconds into Start
>  put the imageData of image 2 into iData
>  put empty into tmaskdata
>  put 2560 into re # i.e. 640 * 4
>  repeat with i = 0 to 479
>    repeat with j = 0 to 639
>      put  chartonum(char (i*re + (j*4+2)) of idata) into tC1
>      put chartonum(char (i*re + (j*4+3)) of idata) into tC2
>      put  chartonum(char (i*re + (j*4+4)) of idata) into tC3
>      put the round of ((tc1 + tc2 + tc3)/3) into tM
>      put numToChar(tM) after tMaskData
>    end repeat
>  end repeat
>  put the milliseconds - Start into fld "test"
> end mouseUp
>
> Best regards,
>
> Wilhelm Sanke
> <http://www.sanke.org/MetaMedia>
> <http://www.sanke.org/ImageDataArt>

Wow! The 200x200 image processed in 200ms.

And making a universal version to work with any image dimensions is  
trivial, just change a few lines and make it a function, just pass  
the name of the img - or to be safer, edit to make it run off the  
long ID...

function makeMask tMaskImg
   set the cursor to watch
   put width of tMaskImg into tW
   put height of tMaskImg into tH
   put the milliseconds into Start
   put the imageData of tMaskImg into iData
   put empty into tmaskdata
   put tW * 4 into re
   repeat with i = 0 to (tH - 1)
     repeat with j = 0 to (tW - 1)
       put  chartonum(char (i*re + (j*4+2)) of idata) into tC1
       put chartonum(char (i*re + (j*4+3)) of idata) into tC2
       put  chartonum(char (i*re + (j*4+4)) of idata) into tC3
       put the round of ((tc1 + tc2 + tc3)/3) into tM
       put numToChar(tM) after tMaskData
     end repeat
   end repeat
   return tMaskData
end makeMask

Thanks a lot,

Ian



More information about the use-livecode mailing list