Gdk_pixbuf_new_from_bytes() auto-detect image info

I am trying to read an image from an internet URL. Wouldn’t know what it’s type is, so cannot know any metadata about it. Tried reading the image using "soup_session_send_and_read_{async,finish}" which returns a GBytes*. However this cannot be directly used in "gdk_pixbuf_new_from_bytes()", as "gdk_pixbuf_new_from_bytes()" needs lot of image specific metadata to be passed to it.

Why cannot gdk_pixbuf_new_from_bytes() detect all metadata from the "Bytes*" in-memory buffer, when "gdk_pixbuf_new_from_stream()" can do it ?

I currently use "g_memory_input_stream_new_from_bytes()" to build a stream out of the "Bytes*" buffer and use "gdk_pixbuf_new_from_stream()", so it works.

But still want to understand what’s the actual use of "gdk_pixbuf_new_from_bytes()" ?

The new_from_bytes() constructor is convenience around the new_from_data() constructor; both constructors take a buffer of “image data in 8-bit/sample packed format”, as the documentation says. In other words: the new_from_bytes() constructor expects the actual RGBA pixel data, not an encoded image data format.

The naming is what is confusing here.

In GIO, generally there are a lot of constructors of the form "Foo.new_from_sourceX()", "Foo.new_from_sourceY()" etc, which normally gives the impression that the Foo data-sink object supports reading data bytes from different sources.

But, "new_from_bytes()" / "new_from_data()" names are generic but they’re not. They are totally specialized functions which expect raw uncompressed / un-encoded RGBA pixel data. The point that these are grouped with generic "new_from_stream()" / "new_from_file()" is what is confusing here.

Ideally, "new_from_bytes()" name should be given to a constructor which does

   stream = g_memory_input_stream_new_from_bytes (bytes)
   return gdk_pixbuf_new_from_stream (stream)

That’s not really any more clear or confusing than the current naming. It’s also entirely too late to change GdkPixbuf.

I wasn’t thinking about changing any API.

But, the API documentation could be updated to address issues raised in the above posts.

The documentation groups by type, so all constructors go together.

Additionally, as I linked above, the documentation specifies that new_from_data/new_from_bytes take packed RGB(A) pixel data.

If you have ideas for better wording, feel free to open a merge request; I’m happy to review contributions to the documentation.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.