Save brush with transparency

Find quick help here to get you started with Hollywood
Post Reply
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Save brush with transparency

Post by Lerio69 »

I have create a brush (not from file) with BLACK color background and draw in it.
Now I want to save it in PNG with transparency, so i try to use command:

Code: Select all

SaveBrush(1, image$, #BLACK, #IMGFMT_PNG)
But the image saved not have transparency.

If I try to use AlphaChannel

Code: Select all

CreateBrush(1, wX, hY, #BLACK, {AlphaChannel = True, Clear = True})
The image saved is all black.

How can create and save brush with transparency?
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Re: Save brush with transparency

Post by Lerio69 »

Saving to GIF format work, image saved have transparency. So this is a bug?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Save brush with transparency

Post by airsoftsoftwair »

It doesn't really make sense to use the "transcol" argument if your brush already has an alpha channel. The "transcol" argument is meant to mask out areas of an opaque brush. If your brush isn't opaque, don't use the "transcol" argument at all, i.e. do this:

Code: Select all

CreateBrush(1, 640, 480, #BLACK, {AlphaChannel = True, Clear = True})
SaveBrush(1, "test.png", #NOTRANSPARENCY, #IMGFMT_PNG)
This works fine here.
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Re: Save brush with transparency

Post by Lerio69 »

Yes, this save a PNG without transparency,
the question is that I want a PNG with transparency, but I cant.

I try to create two function.
One that read a waypoints from a file and draw its in a brush.
Second that save brush in all image format.

Code: Select all

Function DrawAIW(wX, hY)
...
       CreateBrush(1, wX, hY)
...
EndFunction

Function Save_Brush()
	fname$ = UnleftStr(FilePart(file$), 3).."png"
	pname$ = PathPart(file$)
	image$ = FileRequest("Save Image", "png|jpg|gif|bmp", #REQ_SAVEMODE, pname$, fname$)
	var$ = RightStr(image$, 3)
	If image$<>""	
		Switch var$
		Case "png":
			fmt = #IMGFMT_PNG
		Case "jpg":
			fmt = #IMGFMT_JPEG
		Case "bmp":
			fmt = #IMGFMT_BMP
		Case "gif":
			fmt = #IMGFMT_GIF
		EndSwitch
		SaveBrush(brush, image$, #BLACK, fmt, {dither=True})
	EndIf
EndFunction
With this code, when I select to save brush as PNG, the image file not have transparency.
But if I select to save in GIF format, image file have transparency (this is ok and what I want)
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Save brush with transparency

Post by Allanon »

mmm...

Could you try to create you brush with:

Code: Select all

   CreateBrush(1, wX, hY, #BLACK, { AlphaChannel = True, Clear = True })
This way at creation time you enable the transparency channel and you also clear all the brush contents, making it full transparent.
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Re: Save brush with transparency

Post by Lerio69 »

I made this app for testing whats happen.

Code: Select all

@REQUIRE "RapaGUI"
@DISPLAY {Width = 600, Height = 400, color = #BLACK}
CreateBrush(1, 600, 400)

Function p_Save_Brush()
	image$ = FileRequest("Save Image", "png|jpg|gif|bmp", #REQ_SAVEMODE)
	var$ = RightStr(image$, 3)
	If image$<>""	
		Switch var$
		Case "png":
			fmt = #IMGFMT_PNG
		Case "jpg":
			fmt = #IMGFMT_JPEG
		Case "bmp":
			fmt = #IMGFMT_BMP
		Case "gif":
			fmt = #IMGFMT_GIF
		EndSwitch
		SaveBrush(1, image$, #BLACK, fmt, {dither=True})
	EndIf
EndFunction

Function p_Draw(W,H)
	FreeBrush(1)
	CreateBrush(1, W, H)
	SelectBrush(1)
	SetFillStyle(#FILLCOLOR)
	Box(W/2, H/2, 2+Rnd(200), 2+Rnd(100), #WHITE)
	SetFillStyle(#FILLNONE)
	Circle(100, 100, 2+Rnd(80), #RED)
	DisplayBrush(1, #CENTER, #CENTER)
EndFunction

Function p_EventFunc(msg)
	Switch msg.Class
	Case "Button":
		Switch msg.attribute
		Case "Pressed":
			Switch msg.ID
			Case "draw":
				p_Draw(600, 400)
			Case "save":
				p_Save_Brush
			EndSwitch
		EndSwitch
	EndSwitch	
EndFunction

AppStr$=[[<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="win" title="AIW Converter" sizegadget="False">
		<hgroup>
			<vgroup>
				<hgroup frame="true">
					<hollywood id="hwmcc1" display="1"/>
					<rectangle/>
				</hgroup>
			</vgroup>
			<vgroup>
				<button fixwidth="true" width="135" id="draw">Draw</button>
				<rectangle />
				<button fixwidth="true" width="135" id="save">Save</button>
			</vgroup>
		</hgroup>
	</window>
</application>]]

InstallEventHandler({RapaGUI = p_EventFunc})

moai.CreateApp(AppStr$)

Repeat
	WaitEvent
Forever
In my main App, p_Draw is called after Loading a File or change graphics option (colors and/or line width and/or image dimensions)

If in p_Draw I change the line

Code: Select all

CreateBrush(1, W, H)
in

Code: Select all

CreateBrush(1, W, H, #BLACK, { AlphaChannel = True, Clear = True })
I have another Issue: the brush result completly Black and not displayed.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Save brush with transparency

Post by Allanon »

Hi Lerio69,
I've tried your script on my machine (Windows10) and every time I press [draw] a box and a circle are drawn...
Image
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Re: Save brush with transparency

Post by Lerio69 »

Yes, this is only a simulation of my App.
Have you tried to save the image in PNG and GIF formats to see the differences?
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Save brush with transparency

Post by Allanon »

Here is the fixed version :)
Have a look at SelectBrush() documentation to check varius #SELMODE_COMBO modes

Code: Select all

Function p_Draw(W,H)
  ; Ad ogni redraw devi cancellare lo schermo
  SetFillStyle(#FILLCOLOR)
  Box(0, 0, w, h, #BLUE)
  
  FreeBrush(1)
  CreateBrush(1, W, H, #BLACK, { AlphaChannel = True, Clear = True })
  
  ; Il brush va selezionato in modalità combo perchè così
  ; puoi disegnare anche nell'alpha channel
  SelectBrush(1, #SELMODE_COMBO, 1)
    Box(W/2, H/2, 2+Rnd(200), 2+Rnd(100), #WHITE)
    SetFillStyle(#FILLNONE)
    Circle(100, 100, 2+Rnd(80), #RED)
    
  ; Con EndSelect() riporti il display come dispositivo di
  ; output
  EndSelect()
   
  DisplayBrush(1, #CENTER, #CENTER)
EndFunction
User avatar
Lerio69
Posts: 29
Joined: Sat Sep 24, 2016 11:47 pm
Location: Italy

Re: Save brush with transparency

Post by Lerio69 »

Thanks a lot, I have to learn many things.
Post Reply