Question about DrawingBuffers (with example)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Question about DrawingBuffers (with example)

Post by Mijikai »

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:

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
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question about DrawingBuffers (with example)

Post by infratec »

mestnyi wrote: Sat Feb 04, 2023 2:53 pm after running your codes what should happen? :)
The choosen color should be converted to transparent.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question about DrawingBuffers (with example)

Post by infratec »

I changed my CustomFilter code abvove.

Now loading also the provided image.
But ... this image was loaded as 24bit image, since it has no alpha channel.
A conversion to 32 bit was needed.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Question about DrawingBuffers (with example)

Post by netmaestro »

I played with this for fun. Probably doesn't add anything to the discourse but this is what I did:

Code: Select all

Global transparentcolor.q

Macro CheckerBoard(image,bs) ; #image, blocksize
  w=ImageWidth(image):h=ImageHeight(image)
  StartDrawing(ImageOutput(image))
    For j=0 To h Step 2*bs
      For i=0 To w Step 2*bs
        Box(i,j,bs,bs,#White) : Box(i+bs,j,bs,bs,RGB(200,200,200))
        Box(i,j+bs,bs,bs,RGB(200,200,200)) : Box(i+bs,j+bs,bs,bs,#White)
      Next
    Next  
  StopDrawing()
EndMacro

UsePNGImageDecoder()
img0 = CatchImage(#PB_Any, ?PicPak, 838)

DataSection
  PicPak:
  Data.q $0A1A0A0D474E5089,$524448490D000000,$2800000040000000,$6BACC10000000208,$59487009000000BE,$0B0000130B000073,$0000189C9A000113,$855854414449F802
  Data.q $A60C30E36F3B59D5,$6410E8676081CB6C,$43B43AABFFFFE0E9,$51C8E7C0DA05A1D1,$252587A863A0606E,$494914C8C4DFA467,$BB622C33808A887C,$12DA4178FEF8901B
  Data.q $817F2C72208B2F48,$386EED80BE221704,$91AD6A7F1294A38C,$71214A7448D204B6,$6C0008E30FA44718,$96CAA53A12A7E58A,$C95533CE3FADC4D1,$1AD73A6976640668
  Data.q $0A1BBB79E71D44CD,$DC73D3CD813C10D6,$488C94A44CEC175C,$80046BA6BE7D3E99,$94936A1A6DA1F42A,$0B187594A75E6E92,$042A7AC867F61BE6,$6D084C2C2CD2A0A7
  Data.q $606822B62243D7A1,$DE647187529D2158,$F43EFD5710588E95,$6DD831878871ED89,$6B51EF3F30606BB6,$0FFFC11BC02E083D,$DF818C1C71B96D76,$A0C15D4F42175482
  Data.q $189B5B013D4A2F5C,$BB68598232B24CDC,$8799BC062AF28E1B,$D05088354B27220E,$880B990317FD83DB,$B3647D54CACB009D,$90E65EB24A1CF38F,$2E960A51A61444BA
  Data.q $062DDF30BC6C3317,$AD1ACCD3E89F7EAA,$D08596BE86F33C75,$AE8AC59716AF1042,$6B612F12E05D885C,$600597762D043302,$959AD4A74753416F,$E020FF6CC0361138
  Data.q $422590AD586B749C,$3CD867010EF7894A,$1AE2EB6BCAAEB33D,$0FF7A5C1D639716E,$5646755C35EFD54D,$B2B9BDB39A163B22,$1BD8E5177AB990A0,$0C0551534DB4003E
  Data.q $2DCB215641164291,$AB578EAA2D636E88,$0395CB2157B84F5C,$4D4580B3DDE7E600,$F5AC3C5E42EE008F,$EE6E0047A7F72514,$0316323BFBFAF9F4,$EFC2EF1C9B903AA1
  Data.q $19BC98C343FDBB8E,$E44F58DC2F04D6D0,$7B68B50FF6EBD02F,$EA0006318C64E210,$4E34A53BA40006BA,$4CC814838D6B4210,$1B4B5E817F1678AB,$17176F5F4F3CF463
  Data.q $7F0BBD1A48CC66F2,$1F1E07562D971C81,$71C4157191EC850B,$2AF22097FC158C3E,$3DB45A9CBB7FC658,$15276A52B54A7400,$DA8C64E6327D75D4,$08C36108134CFE6C
  Data.q $6BD5CFE5CC4D8D43,$DEBE998C6318DBE3,$BA52966D7F10272E,$B9FCB989B08E0F1D,$422F04E86873DD7A,$817F2F05C7DCDE37,$751B150DDDB5FA5E,$73E80DED0933E1A7
  Data.q $97AE9FCB9288FBA2,$9431F442EEC53F01,$B9FC599C2A15FFFD,$FEFC7187D6B4297A,$47179B6E5CF58978,$DFBA94A57A8DFF2E,$001ADFE4FC6105FF,$4549000000008727
  Data.b $4E,$44,$AE,$42,$60,$82
  PicPakend:
EndDataSection

Procedure FilterCallback(x, y, SourceColor, TargetColor)
  If SourceColor<>transparentcolor
    ResultColor = Sourcecolor
  Else
    ResultColor = TargetColor
  EndIf
  ProcedureReturn ResultColor
EndProcedure

StartDrawing(ImageOutput(img0))
  transparentcolor = $FF<<24|Point(0,0)
StopDrawing()

CreateImage(1, ImageWidth(img0), ImageHeight(img0), 32)
CheckerBoard(1,4)

StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_CustomFilter)      
  CustomFilterCallback(@FilterCallback())
  DrawImage(ImageID(img0),0,0)
StopDrawing()

ShowLibraryViewer("image",1)
CallDebugger
BERESHEIT
Post Reply