New topic: 

How to save a picture with alpha channel?

<http://forums.realsoftware.com/viewtopic.php?t=36301>

         Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic          Author  
Message        relaht          Post subject: How to save a picture with alpha 
channel?Posted: Fri Nov 12, 2010 7:28 pm                         
Joined: Fri Dec 01, 2006 9:39 pm
Posts: 119                I know SaveAsPNGMBS but this works only with a 
picture that have a mask.
My picture doesn't have a mask but "transparent = 1"

Anybody knows how to save it? Creating a mask with the transparency would also 
help.   
                             Top                DaveS          Post subject: 
Re: How to save a picture with alpha channel?Posted: Fri Nov 12, 2010 8:19 pm   
                              
Joined: Sun Aug 05, 2007 10:46 am
Posts: 2723
Location: San Diego, CA                use f.saveaspicture

create a mask if you need/want to.... [only for PNG, GIF not for JPG]


off the cuff.. not tested.... create a mask

Code:  dim p_rgb as RGBSurface
  dim m_rgb as RGBSurface
  
  p_RGB=p.RGBSurface
  m_rgb=p.Mask.RGBSurface
  
  for y=0 to p.width
  for x=0 to p.height
  if p_rgb.Pixel(x,y)=&cffffff then 
    m_rgb.Pixel(x,y)=&cffffff
  else
    m_rgb.Pixel(x,y)=&c000000
  end if
  next x
  next y

      
_________________
Dave Sisemore
MacPro, OSX 10.6.4 RB2009r5.1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top                relaht          Post subject: 
Re: How to save a picture with alpha channel?Posted: Fri Nov 12, 2010 8:33 pm   
                      
Joined: Fri Dec 01, 2006 9:39 pm
Posts: 119                DaveS wrote:use f.saveaspicture[/code]
You are right, I can use this as well...
I didn't look at this methode for a long time and and in the "good old days" RB 
could only save a pict.

Thanks for the 'create mask' methode, but it might be a little to slow. My 
picture has HD resolution 1920 x 1080 pixel.   
                             Top                DaveS          Post subject: 
Re: How to save a picture with alpha channel?Posted: Fri Nov 12, 2010 8:59 pm   
                              
Joined: Sun Aug 05, 2007 10:46 am
Posts: 2723
Location: San Diego, CA                I bet you will surprised at how fast 
that will go.... using RGBSURFACE is the trick.... setting pixels on the 
GRAPHICS object is dead slow

I use this type of method in a Paint program I wrote, and you barely notice the 
delay


this might even be faster

Code:dim p_rgb as RGBSurface
  dim m_rgb as RGBSurface
  
  p.graphics.mask.forecolor=&c00000
  p.graphics.mask.fillrect 0,0,p.width,p.height

  p_RGB=p.RGBSurface
  m_rgb=p.Mask.RGBSurface
  
  for y=0 to p.width
  for x=0 to p.height
  if p_rgb.Pixel(x,y)=&cffffff then  m_rgb.Pixel(x,y)=&cffffff
  next x
  next y
      
_________________
Dave Sisemore
MacPro, OSX 10.6.4 RB2009r5.1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top                relaht          Post subject: 
Re: How to save a picture with alpha channel?Posted: Fri Nov 12, 2010 10:00 pm  
                       
Joined: Fri Dec 01, 2006 9:39 pm
Posts: 119                You are right. This is very fast! For the HD picture 
it takes 0.08 seconds. This good enough for me too 

The methode that you posted, is not full correctly, but I got the idea.
Here is what I came up with:
Code:Sub CreateMask(p As Picture)
  dim x, y As Integer
  dim p_rgb as RGBSurface
  dim m_rgb as RGBSurface
  
  p.Mask.Graphics.ForeColor = &cffffff
  p.Mask.Graphics.FillRect 0, 0, p.Width, p.Height
  
  p_RGB=p.RGBSurface
  m_rgb=p.Mask.RGBSurface
  
  for x = 0 to p.width
  for y = 0 to p.height
  
  if p_rgb.Pixel(x,y)<>&cffffff then m_rgb.Pixel(x,y)=&c000000
  
  next y
  next x
  
End Sub

This works perfectly for me.


But I couldn't save the picture as PNG including the mask with:
Code:f.SaveAsPicture p, 150
The mask get lost with this code.
Did I something wrong?

Anyway it works with SaveAsPNGMBS and this is good enough for me.

Thanks again for your help.     

    Last edited by relaht on Fri Nov 12, 2010 10:18 pm, edited 1 time in total. 
  
                             Top                DaveS          Post subject: 
Re: How to save a picture with alpha channel?Posted: Fri Nov 12, 2010 10:11 pm  
                               
Joined: Sun Aug 05, 2007 10:46 am
Posts: 2723
Location: San Diego, CA                interesting.... my paint program uses 
150 as the format as it seems to work just fine.... and the add_mask routine I 
have is almost 100% identical to what you modified by off the cuff code to.

but in any case.. glad I could help      
_________________
Dave Sisemore
MacPro, OSX 10.6.4 RB2009r5.1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to