Gtk based imshow of: reading and displaying an image with gtk stb_image

In front of one has been able to based on gtk and displays the read image . More foregoing one: GDI-based imshow: stb_image read image using drawing and correcting and displaying an image by reading the image by the GDI stb_image, implements a imshow. Benpian at both based on the use stb_image read image, and using gtk display, the initial realization of a gtk imshow based.

The first is to find a code that creates image gtk from the specified buffer and displays (Reference 1). Then stb image reading, I had previously package, get fc image opencv and is compatible bgr format. However, the order is needed to find gtk rgb, so they made a conversion step: BGR to RGB, and then pass the corresponding buffer gtk to generate its image.

Code

The complete code requires GDI-based imshow: Use stb_image read and corrected image rendering this blog post code, and add the code for this article gtk_show_image_v3.c:

#include <gtk/gtk.h>
#include <stdlib.h>
#include <assert.h>

#include "fc_image.h"

void fc_bgr_to_rgb(FcImage* im) {
    if (im==NULL) return;
    if (im->c<=0 || im->h<=0 || im->w==0) return;
    assert(im->c==3);

    int num_pixel = im->c * im->h * im->w;
    unsigned char t;
    for(int i=0; i<num_pixel; i+=3) {
        t = im->data[i];
        im->data[i] = im->data[i+2];
        im->data[i+2] = t;
    }
}

int main (int argc, char *argv[])
{
    const char* im_pth = "/home/zz/work/libfc/imgs/fruits.jpg";
    FcImage im = fc_load_image(im_pth);
    fc_bgr_to_rgb(&im);


    GtkWidget *window;
    GtkWidget* image;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data (im.data, GDK_COLORSPACE_RGB,
        FALSE, 8, im.w, im.h, im.w*3, NULL, NULL);

    gtk_window_set_title (GTK_WINDOW (window), "Image Viewer");

    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

    image = gtk_image_new_from_pixbuf (pixbuf);
    gtk_container_add(GTK_CONTAINER (window), image);

    gtk_widget_show_all (window);

    gtk_main ();

    return 0;
}

Simple package

Taking into account the existing bgr order to modify the image buffer rgb, place modification is definitely a problem, affecting subsequent use of the algorithm. It thus generates a new copy of the image data. And also you need to im as parameters, and title, of the API package, for later calls. The modified code as follows:

#include <gtk/gtk.h>
#include <stdlib.h>
#include <assert.h>

#include "fc_image.h"

void fc_copy_bgr_to_rgb(const FcImage* src, FcImage* dst) {
    if (src==NULL) return;
    if (dst==NULL) return;
    assert(src!=dst);
    assert(src->data!=NULL);
    assert(dst->data!=NULL);
    assert(src->data!=dst->data);
    assert(src->c>=0 && src->h>=0 && src->c==3);
    assert(src->c>=0 && src->h>=0 && src->c==3);
    assert(src->c==dst->c && src->h==dst->h && src->w==dst->w);

    int num_pixel = src->c * src->h * src->w;
    for(int i=0; i<num_pixel; i+=3) {
        dst->data[i] = src->data[i+2];
        dst->data[i+1] = src->data[i+1];
        dst->data[i+2] = src->data[i];
    }
}

FcImage fc_make_image(int w, int h, int c)
{
    FcImage out;
    out.w = w;
    out.h = h;
    out.c = c;
    out.data = (unsigned char*)calloc(h*w*c, sizeof(float));
    return out;
}

void gtk_show_image_v3(const FcImage* im, const char* title)
{
    FcImage im_rgb = fc_make_image(im->w, im->h, im->c); //?? check this dimensions
    fc_copy_bgr_to_rgb(im, &im_rgb);

    GtkWidget *window;
    GtkWidget* image;

    gtk_init (NULL, NULL);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data (im_rgb.data, GDK_COLORSPACE_RGB,
        FALSE, 8, im_rgb.w, im_rgb.h, im_rgb.w*3, NULL, NULL);

    gtk_window_set_title (GTK_WINDOW (window), title);

    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

    image = gtk_image_new_from_pixbuf (pixbuf);
    gtk_container_add(GTK_CONTAINER (window), image);

    gtk_widget_show_all (window);

    gtk_main ();
}


int main (int argc, char *argv[])
{
    const char* im_pth = "/home/zz/work/libfc/imgs/fruits.jpg";
    FcImage im = fc_load_image(im_pth);
    const char* title = "fruits";
    gtk_show_image_v3(&im, title);

    return 0;
}

reference

Display a sequence of images using gtk in Linux

Guess you like

Origin www.cnblogs.com/zjutzz/p/10960235.html