Starting at square one with image processing

James Hurley jhurley0305 at sbcglobal.net
Sat Jul 7 21:03:30 EDT 2007


>
> Message: 7
> Date: Sat, 07 Jul 2007 11:40:58 +0200
> From: Wilhelm Sanke <sanke at hrz.uni-kassel.de>
> Subject: Re: Starting at square one with image processing
> To: use-revolution at lists.runrev.com
> Message-ID: <468F5FAA.9010605 at hrz.uni-kassel.de>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>
>   James Hurley jhurley0305 at sbcglobal.net
>   <mailto:use-revolution%40lists.runrev.com?Subject=Starting%20at% 
> 20square%20one%20with%20image%20processing&In-Reply- 
> To=20070706134327.98049488F6E%40mail.runrev.com>
>
> wrote:
>
>> I've discovered that I have no idea what's going on with the
>> imagedata function.
>>
>> I thought that the imagedata was a pixel by pixel map of the image. I
>> tried the following handler to look at each pixel of my image:
>
>
>
> Allow me to add some information to the recommendations you already  
> got from this list.
>
> The basic format for imagedata manipulation I use in my  
> applications is like shown below.
> Treating the image as a square enables you also to access and  
> change pixels according to
> their positions, i.e. you could select single pixels or rectangular  
> parts of the picture
>  and manipulate only them. Changing the imagedata for an entire  
> image would look like here:
>
>
>  "put the imageData of image "x" into iData
>  put the height of img "x" into theight
>  put the width of img "x" into twidth
>  put 4* twidth into ro  # "ro" for "row": There are 4 chars to each  
> pixel in a row.
>
>   repeat with i = 0 to theight - 1
>      repeat with j = 0 to twidth - 1
> #get the color values
>       put (chartonum(char (i*ro + j* 4 + 2) of idata)) into tRed
>       put (chartonum(char (i*ro + j* 4 + 3) of idata)) into tGreen
>       put (chartonum(char (i*ro + j* 4 + 4) of idata)) into tBlue
> #=============================
> # now do whatever you want to change the values of tRed, TGreen,  
> and tBlue
> # and then put them back into the imagedata
> #==================================
>      put numtochar(tRed) into char  (i*ro + j* 4 + 2) of idata
>      put numtochar(tGreen) into char (i*ro + j* 4 + 3) of idata
>      put numtochar(tBlue) into char  (i*ro + j* 4 + 4) of idata
>     end repeat
>   end repeat
>   set the imageData of image "x" to iData"
>
> I also use the "for each" format in my scripts, but this is not  
> necessarily faster.
> I have even found cases where "for each" is slower than the double  
> repeat loop.
>
> To speed up script execution it is advisable to simplify the  
> computation inside
> the loops - especially for larger images. Such a simplified script  
> is not as
> readable as the basic one, but it is indeed faster.
> Thus, the repeat loop from above could be changed to:
>
> "repeat with i = 0 to theight - 1
>     put i*ro into ti
>      repeat with j = 0 to twidth - 1
>       put ti + j*4 into tij
> #get the color values
>       put (chartonum(char tij+ 2) of idata)) into tRed
>       put (chartonum(char (tij+ 3) of idata)) into tGreen
>       put (chartonum(char (tij+ 4) of idata)) into tBlue
> #=============================
> # now do whatever you want to change the values of tRed, TGreen,  
> and tBlue
> # and then put them back into the imagedata
> #==================================
>      put numtochar(tRed) into char  (tij+ 2) of idata
>      put numtochar(tGreen) into char (tij+ 3) of idata
>      put numtochar(tBlue) into char  (tij+ 4) of idata
>     end repeat
>   end repeat"
>
> To produce a gray-scale image based on the red values (this looks  
> much better
>  than computing the average value of tRed, tGreen, tBlue) the inner  
> part of
> the script examples above would be
>
> " put (chartonum(char tij+ 2) of idata)) into tRed
>   put numtochar(tRed) into char (tij+ 3) of idata
>   put numtochar(tRed) into char  (tij+ 4) of idata"
>
> In my various "imagedata" stacks there are quite a number of  
> accessible scripts
>  that you could change and experiment with - about hundred in
> stack "Imagedata toolkit 2 preview".
>
> <http://www.sanke.org/Software/ImagedataToolkitPreview3.zip>
> <http://www.sanke.org/Software/SeamlessTiles.zip>
>
> A new imagedata stack that can handle images of any size will be  
> released soon.
>
> Regards,
>
> Wilhelm Sanke
> <http://www.sanke.org/MetaMedia>

Wilhelm,

Whew! This is impressive. It will take me quite a while to digest the  
content of your toolkit. Thanks for sharing it.

A good part of my problem was using an image created with the Run Rev  
paint tools. They give very erratic results for the imageData.

Using you suggestions and some fussing around, I built a poor man's  
pencil (a graphic square with the script below), a tool that paints a  
white line onto a my image. (My image is a black square called "mine".)

Suppose I wanted to use this "pencil" as an eraser instead; what  
would be the imagedata I would insert in the neighborhood of the eraser?

Jim

-----------------------------------
local MyName, tData, tImgWidth, tImgHeight, tBlankPixel, tMax, x0, y0

on mouseDown
   put the name of me into myName
   put  the  imageData of img  "mine" into tData
   put the rect of image "mine" into tImgRect
   put item 1 of tImgRect into x0
   put item 2 of tImgRect into y0
   put the width of img "mine" into tImgWidth
   put the height of img "mine" into tImgHeight
end mouseDown

on MouseUp
   put empty into myName
end MouseUp

on mouseLeave
   put empty into myName
end mouseLeave

on MouseMove x,y
   if myName is empty then exit mouseMove
   set the loc of me to x,y
   put x-x0 + 1 into u
   put y-y0 +1 into v
   if within(img "mine", the  loc of me) then
     put (v-1)*tImgWidth + u into tPixelNumber
     put 1+ (tPixelNumber-1)*4 into tImageDataCharStart
     put u & space & v & space & tPixelNumber& space &  
tImageDataCharStart into msg box
     put NumtoChar(0)& numtochar(255) & numtochar(255) & numtochar 
(255)  into char tImageDataCharStart to tImageDataCharStart+3 of tData
     set the imageData of img "mine" to tData
   end if
end MouseMove








More information about the use-livecode mailing list