Page 1 of 1

[5.42] vector drawing & saveImage()

Posted: Mon Mar 28, 2016 3:10 pm
by [blendman]
Hi

There is a bug with SaveImage() and the vector drawing library, with alpha chanel.

On the result, we can see a black border :
Image


Code to test :

Code: Select all


Procedure DrawVector()
    
    AddPathCircle(75, 100, 60)      
    VectorSourceColor(RGBA(255, 240, 200, 255))
    FillPath()      
    AddPathCircle(125, 100, 60)
    VectorSourceColor(RGBA(210, 250, 255, 207))
    FillPath()    
    
    ; Dessin opaque sur une couche semi-transparente
    BeginVectorLayer(240)
    AddPathCircle(275, 100, 60)    
    VectorSourceColor(RGBA(255, 240, 240, 255))
    FillPath()        
    AddPathCircle(325, 100, 60)
    VectorSourceColor(RGBA(200,255, 240, 255))
    FillPath()    
    EndVectorLayer()
    
EndProcedure


If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 200)
    
    If StartVectorDrawing(CanvasVectorOutput(0))
        
        DrawVector()
        StopVectorDrawing()
    EndIf
    
    UsePNGImageEncoder()
    
    File$ = SaveFileRequester("Save Image...", File$, "png Images|*.png|All Files|*.*", 0)
    If File$ <> ""
        If CreateImage(0, GadgetWidth(0), GadgetHeight(0), 32,#PB_Image_Transparent) 
            
            If StartDrawing(ImageOutput(0))
                DrawingMode(#PB_2DDrawing_AllChannels)
                Box(0,0,GadgetWidth(0),GadgetHeight(0),RGBA(0,0,0,0))
                StopDrawing()
            EndIf
            
            If StartVectorDrawing(ImageVectorOutput(0))
                DrawVector()
                StopVectorDrawing()
            EndIf
            If SaveImage(0, File$, #PB_ImagePlugin_PNG) = 0
                MessageRequester("CanvasGadget", "Cannot save image: " + File$)
            EndIf
            
            FreeImage(0)
        EndIf            
    EndIf
        
    Repeat
        Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf

I m' not sur, but, perhap's for transparent pixels, the color to erase shouldn't be the black, but the color of the pixel (premul mode ?).

Fr :
Il y a un bug lorsqu'on sauvegarde une image avec canal alpha (png). On voit un liseret (bordure) noire autour des formes avec transparence.
Je ne suis pas certain, mais peut-être que lors de l'enregistrement, les pixels transparents (<255 et >0) devraient être effacés avec la couleur du pixel et non la couleur noire ;).

Re: [5.42] vector drawing & saveImage()

Posted: Sun May 25, 2025 9:56 am
by Fred
When putting a black transparent image, all the aliasing is done with a black background, so when it is saved, you see the black artifact. To solve it, you need
to put a white based transparent background:

Code: Select all

         If StartDrawing(ImageOutput(#IMAGE))
              DrawingMode(#PB_2DDrawing_AllChannels)
              Box(0,0,OutputWidth(),OutputHeight(),RGBA(255,255,255,0))
              StopDrawing()
          EndIf

I will add a new #PB_Image_TransparentBlack constant for the next major version to easily solve this and #PB_Image_Transparent will default to white transparent image.

Re: [5.42] vector drawing & saveImage()

Posted: Sun May 25, 2025 11:35 pm
by Michael Vogel
Hm, what about...

Code: Select all

CreateImage(....,#PB_Image_Transparent | #White)
...this would allow to use any color not only white and black. Existing code would work as before as well.

But the constant #PB_Image_Transparent would have to be changed - if it is set to $FF000000 a simple XOR would be enough before initializing the image memory.

Re: [5.42] vector drawing & saveImage()

Posted: Mon May 26, 2025 4:34 am
by #NULL
Fred wrote: Sun May 25, 2025 9:56 am When putting a black transparent image, all the aliasing is done with a black background, so when it is saved, you see the black artifact. To solve it, you need
to put a white based transparent background:
I don't have a Windows to test, but doesn't this just create white artifacts instead of black ones, visible when the image is displayed on a dark background? I think the vector library needs a way to control the blend mode, like 2d drawing has with #PB_2DDrawing_AllChannels that just 'puts' pixels instead of blending with already existing values. Or, if the existing color has an alpha of 0 then it shouldn't influence the resulting color.