Convert PIXBUF to PB_Image

Linux specific forum
User avatar
makke
New User
New User
Posts: 6
Joined: Sun Nov 27, 2016 9:23 pm
Location: Germany
Contact:

Convert PIXBUF to PB_Image

Post by makke »

Hello,

I am am testing some functions of GTK and GDK, during the use of the PIXBUF and the PB internal image handling I come to some limitations. Is there any other way than StartDrawing()-StopDrawing() to convert a PIXBUF image to a PB image ?

Any tips ?
---
Xubuntu 16.04 LTS (64 bit)
Debian 8 (64 bit)
Windows 7 (64 bit)

and, I apologize for my bad english.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Convert PIXBUF to PB_Image

Post by Fred »

You should be able to use DrawImage() directly with your pixbuf pointer, as ImageID() is a pixbuf on Linux
User avatar
makke
New User
New User
Posts: 6
Joined: Sun Nov 27, 2016 9:23 pm
Location: Germany
Contact:

Re: Convert PIXBUF to PB_Image

Post by makke »

Sure, thank you, my first post was a bit inaccurate. I know that all functions the need the ImageID() function, can handle the pixbuf pointer (e.g. SetGadgetState(#ImageGadget, PIXBUF_ptr)). I thought about more/other options.
---
Xubuntu 16.04 LTS (64 bit)
Debian 8 (64 bit)
Windows 7 (64 bit)

and, I apologize for my bad english.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Convert PIXBUF to PB_Image

Post by wilbert »

You could try CreateImage and use gdk_pixbuf_copy_area to copy the pixbuf to the PB created image.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
makke
New User
New User
Posts: 6
Joined: Sun Nov 27, 2016 9:23 pm
Location: Germany
Contact:

Re: Convert PIXBUF to PB_Image

Post by makke »

wilbert wrote:You could try CreateImage and use gdk_pixbuf_copy_area to copy the pixbuf to the PB created image.
It's like Drawimage(). But thanks for the suggestion.

Here ist my procedure for this:

Code: Select all

    Procedure.i Pixbuf_ConvertToImage(Pixbuf.i)
      Protected.i image, w, h, a
      w = gdk_pixbuf_get_width(Pixbuf)
      h = gdk_pixbuf_get_height(Pixbuf)
      a = gdk_pixbuf_get_has_alpha(Pixbuf)
      If a
        image = CreateImage(#PB_Any, w, h, 32, #PB_Image_Transparent)
      Else
        image = CreateImage(#PB_Any, w, h, 24)
      EndIf
      If IsImage(image)
        StartDrawing(ImageOutput(image))
        If a
          DrawingMode(#PB_2DDrawing_AllChannels)
        Else
          DrawingMode(#PB_2DDrawing_Default)
        EndIf
        DrawImage(Pixbuf, 0, 0, w, h)
        StopDrawing()
      Else
        ProcedureReturn -1
      EndIf
      ProcedureReturn image
    EndProcedure
---
Xubuntu 16.04 LTS (64 bit)
Debian 8 (64 bit)
Windows 7 (64 bit)

and, I apologize for my bad english.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Convert PIXBUF to PB_Image

Post by wilbert »

makke wrote:It's like Drawimage(). But thanks for the suggestion.
You asked for a way without StartDrawing()-StopDrawing() and with gdk_pixbuf_copy_area you should be able to copy the pixbuf without those PB commands.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
makke
New User
New User
Posts: 6
Joined: Sun Nov 27, 2016 9:23 pm
Location: Germany
Contact:

Re: Convert PIXBUF to PB_Image

Post by makke »

Ah, ok, you think the the destination Pixbuf can be the Purebasic image ? I'll try it out and post the results here.

[EDIT:]
It works, look:

Code: Select all

EnableExplicit

ImportC ""
  g_error_free (*error.GError)
  gdk_pixbuf_get_width  (*pixbuf)
  gdk_pixbuf_get_height (*pixbuf)
  gdk_pixbuf_new_from_file (filename.p-utf8, *error.GError)
  gdk_pixbuf_new_from_file_at_scale (filename.p-utf8, width.i, height.i, preserve_aspect_ratio.b, *error.GError)
  gdk_pixbuf_save  (*pixbuf, filename.p-utf8, type.p-utf8, *error.GError = #Null)
  gdk_pixbuf_savev (*pixbuf, filename.p-utf8, type.p-utf8, *option_keys, *option_values, *error.GError)
  gdk_pixbuf_copy_area (*src_pixbuf, src_x.i, src_y.i, src_width.i, src_height.i, *dest_pixbuf, dest_x.i, dest_y.i)
EndImport

Define.i       newpix, result, w, h
Define.GError  *error

Dim keys.s(3)
Dim values.s(3)

Macro toUTF8(Text, Var)
  Var = Space(Len(Text))+1
  PokeS(@Var, Text, Len(Text), #PB_UTF8)
EndMacro

toUTF8("quality", keys(0))
toUTF8("10", values(0))


If OpenWindow(0, 0, 0, 800, 600, "PIXBUF Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  
  If ImageGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0)
    
    newpix = gdk_pixbuf_new_from_file_at_scale (#PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp", GadgetWidth(0), GadgetHeight(0), #True, @*error)
    
    If newpix = 0 Or *error <> 0
      Debug *error\domain
      Debug *error\code
      Debug PeekS(*error\message, -1, #PB_Ascii)
      g_error_free(*error)
    Else
      ;SetGadgetState(0, newpix)
      ;result = gdk_pixbuf_savev(newpix, "PureBasic.jpg", "jpeg", keys(), values(), @*error)
      ;If result = 0 Or *error <> 0
      ;  Debug "Error: " + PeekS(*error\message, -1, #PB_Ascii)
      ;EndIf
      w = gdk_pixbuf_get_width(newpix)
      h = gdk_pixbuf_get_height(newpix)
      CreateImage(0, w, h)
      gdk_pixbuf_copy_area (newpix, 0, 0, w, h, ImageID(0), 0, 0)
      SetGadgetState(0, ImageID(0))
    EndIf
    
  EndIf
  
EndIf

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

End
Thank You!
---
Xubuntu 16.04 LTS (64 bit)
Debian 8 (64 bit)
Windows 7 (64 bit)

and, I apologize for my bad english.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Convert PIXBUF to PB_Image

Post by wilbert »

makke wrote:Ah, ok, you think the the destination Pixbuf can be the Purebasic image ? I'll try it out and post the results here.
That was indeed the idea :)
Glad to hear it works as expected.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply