Re: Question about DrawingBuffers (with example)
Posted: Sat Feb 04, 2023 3:42 pm
Another way/example to make a color transparent.
Non 32bit images will be converted to 32bit images.
Colors that match the specified ColorKey will be transparent.
I also added some optimisations.
Code:
Non 32bit images will be converted to 32bit images.
Colors that match the specified ColorKey will be transparent.
I also added some optimisations.
Code:
Code: Select all
EnableExplicit
UsePNGImageDecoder()
UsePNGImageEncoder()
Procedure.i ImageMakeColorTransparent(*Image.Integer,ColorKey.l)
Protected *pixel.Long,img.i,offset.i,pixel.i
If ImageDepth(*Image\i,#PB_Image_InternalDepth) = 32
img = StartDrawing(ImageOutput(*Image\i))
Else
img = CreateImage(#PB_Any,ImageWidth(*Image\i),ImageHeight(*Image\i),32)
If img
If StartDrawing(ImageOutput(img))
DrawImage(ImageID(*Image\i),0,0)
FreeImage(*Image\i)
*Image\i = img
Else
FreeImage(img)
img = #Null
EndIf
EndIf
EndIf
If img
offset = DrawingBuffer()
pixel = offset + ((OutputWidth() * OutputHeight()) << 2) - 4
For *pixel = offset To pixel Step 4
*pixel\l % ColorKey
Next
StopDrawing()
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.i Main()
Protected img.i
img = LoadImage(#PB_Any,"test.png");<- i used the image provided by the OP as example
Debug img
If ImageMakeColorTransparent(@img,$FF00FF00);<- green color should be transparent
Debug SaveImage(img,"test_changed.png",#PB_ImagePlugin_PNG,#Null,32)
EndIf
FreeImage(img)
EndProcedure
Main()
End