the minimum rect of a paint image

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

the minimum rect of a paint image

Post by jmburnod » Mon Jun 22, 2009 6:14 pm

Hi All,

The follow up of same posts

A exemple stack is available in RevOnLine with name "minrectofpaint"

Thank to Jan and Bernd for support

RectOfPaintNoTransparent function return the minimum rectangle of a paint image (the first pixel no transparent at TopLeft,TopRight,BottomRight and BottomLeft

Code: Select all

function RectOfPaintNoTransparent pImage
   put the Topleft of Image pImage into LeTLim
   put TopOfPaintAlpha(pImage) into rTop
   put BotOfPaintAlpha(pImage) into rBot
   put LeftOfPaintAlpha(pImage) into rLeft
   put RightOfPaintAlpha(pImage) into rRight
   put rLeft,rTop,rRight,rBot into LaR
   add item 1 of LeTLim to item 1 of LaR
   add item 2 of LeTLim to item 2 of LaR
   add item 1 of LeTLim to item 3 of LaR
   add item 2 of LeTLim to item 4 of LaR
   return LaR
end RectOfPaintNoTransparent

function TopOfPaintAlpha pImage -- return the top of a paint image (the first pixel no transparent at top)
 put the alphadata of image pImage into tAlphaData
   put the width of image pImage into tWidth
   put the height of image pImage into tHeight
   put empty into tTopBuffer
   put 1 into tByteIndex
   repeat with tRow = 1 to tHeight
      repeat with tColumn = 1 to tWidth
         put charToNum(char tByteIndex of  tAlphaData) into tPixelData
         if tPixelData is not 0 then
           put tRow-1  into tTopBuffer
            return tTopBuffer
         end if
         add 1 to tByteIndex
      end repeat
   end repeat
   return empty
end TopOfPaintAlpha

function RightOfPaintAlpha pImage -- return the right of a paint image (the last pixel no transparent at right)
   put the alphadata of image pImage into tAlphaData
   put the width of image pImage into tWidth
   put the height of image pImage into tHeight
   put empty into tTopBuffer
  put the left of image pImage into laLeftDep
  put 1 into tByteIndex
   put 1 into tColumn
   repeat with tColumn =  tWidth down to 1
      repeat with tRow = 1 to tHeight
      put (tRow*tWidth)-tWidth+tColumn into tByteIndex
         put charToNum(char tByteIndex of  tAlphaData) into tPixelData
        if tPixelData is not 0 then
           put tColumn+1 into tTopBuffer
            return tTopBuffer
         end if
      end repeat
   end repeat
   return empty
end RightOfPaintAlpha

function BotOfPaintAlpha pImage -- return the bot of a paint image (the last pixel no transparent at bot)
 put the alphadata of image pImage into tAlphaData
   put the width of image pImage into tWidth
   put the height of image pImage into tHeight
   put empty into tTopBuffer
   put tWidth*tHeight into tByteIndex
   repeat with tRow = tHeight down to 1
      repeat with tColumn =  tWidth down to 1
         put charToNum(char tByteIndex of  tAlphaData) into tPixelData
         if tPixelData is not 0 then
           put tRow+1 into tTopBuffer
            return tTopBuffer
         end if
         subtract 1 from tByteIndex
      end repeat
   end repeat
   return empty
end BotOfPaintAlpha

function LeftOfPaintAlpha pImage -- return the left of a paint image (the first pixel no transparent at left)
   put the alphadata of image pImage into tAlphaData
   put the width of image pImage into tWidth
   put the height of image pImage into tHeight
   put empty into tTopBuffer
   put the left of image pImage into laLeftDep
   put 1 into tByteIndex
   put 1 into tColumn
   repeat with tColumn =  1 to tWidth
      repeat with tRow = 1 to tHeight
         put (tRow*tWidth)-tWidth+tColumn into tByteIndex
         put charToNum(char tByteIndex of  tAlphaData) into tPixelData
         if tPixelData is not 0 then
            put tColumn-1 into tTopBuffer
            return tTopBuffer
         end if
      end repeat
   end repeat
   return empty
end LeftOfPaintAlpha

Al the best

Jean-Marc

Post Reply