-1

I am trying to display an image based on a string of hex pixel data. The string object being transmitted to me is similar to the following:

std::string image_bytes = "0x00, 0x01, 0x02, etc...";

I am trying to process this data using the following code:

GtkWidget *image;
GdkPixbufLoader* loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, (const guchar*)image_bytes.data(), image_bytes.size(), nullptr);
gdk_image_set_from_pixbuf(GTK_IMAGE(image), loader);

This is giving me a headache of errors, any ideas?

Thanks

MamaShark
  • 19
  • 2
  • Please add a minimal and reproducible example as well as the error message. This is way too broad. – BobMorane Apr 18 '22 at 15:21
  • Please provide a [mre] of the problem, along with the specific errors that you're getting. "A headache of errors" isn't enough information to diagnose the problem. Please read [ask] for more information on asking good questions. – Sylvester Kruin Apr 18 '22 at 20:20

1 Answers1

0

This is in C, but it should work in C++.
In your code, the hex data is stored as an array of strings (char *str[]), not a string (char *str); by doing so, 0x21 will be "0x21", not !.

It also appears that you did not initialise image. This will cause a segmentation fault.

loader as the second argument of gtk_image_set_from_pixbuf() is an incompatible pointer to GdkPixbufLoader * from GdkPixbuf *.

/* GtkPixbufLoader demo program */
#include <gtk/gtk.h>
#include <string.h>

int main(int argc, char **argv) {
        GtkWidget *window, *image;
        GdkPixbufLoader *loader;

        // This is just random data from the beginning of a JPEG file.
        unsigned char data[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52};

        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        image = gtk_image_new();
        loader = gdk_pixbuf_loader_new();

        /* Create pixbuf from data and set the image from `loader` */
        gdk_pixbuf_loader_write(loader, data, strlen((char *) data), NULL);
        gtk_image_set_from_pixbuf(GTK_IMAGE(image), gdk_pixbuf_loader_get_pixbuf(loader));

        gtk_container_add(GTK_CONTAINER(window), image);
        gtk_widget_show_all(GTK_WIDGET(window));

        gtk_main();
        return 0;
}
  • [`gtk_image_set_from_pixbuf`](https://docs.gtk.org/gtk4/method.Image.set_from_pixbuf.html) is deprecated since 4.12. Is there a way to do this using [`gtk_image_new_from_paintable`](https://docs.gtk.org/gtk4/ctor.Image.new_from_paintable.html) instead? – earboxer Aug 26 '23 at 14:08