【问题标题】:GTKMM :: Drawing multiple images using single Gdk::PixBuf in GTk::Drawing areaGTKMM :: 在 GTk::Drawing 区域中使用单个 Gdk::PixBuf 绘制多个图像
【发布时间】:2014-07-03 14:04:19
【问题描述】:

大家好, 我正在尝试在 Gtk::DrawingArea 中绘制实时图像,其方式与它们在火车站、机场、地铁等的监视器中显示的方式相同。

为了当前的测试目的,我已经硬编码了我的代码。

在这里

Gtk::绘图区宽度=720,高度=640。 我有 first.pgm 和 second.pgm 图像,每个图像都有 360*640 尺寸。(是的,我为了测试目的旋转了它。) 这些图像每个像素包含一个字节。

1) 绘制单个图像时绘制良好。 我只是将图像读入大小为 360*640*3 的 unsigned char* 缓冲区。 我是这样读的

int bufferIndex=0; 
int fileIndex=0; //just assume 
for (int i=0; i<height; i++)
{
     for (int j=0; j<width; j++)
     {
         //below three lines are for RGB purpose required in Gdk::Pixbuf
         buffer[bufferIndex++] = //File[fileIndex]; 
         buffer[bufferIndex++] = //File[fileIndex]; 
         buffer[bufferIndex++] = //File[fileIndex]; 
     }
}               

Gdk::Colorspace colorSpace = Gdk::COLORSPACE_RGB;
Gdk::Colorspace colorSpace = (Gdk::Colorspace)0;
bool has_alpha = false;
int bits_per_sample = 8;
int rowStride = 360*3; // distance between rows in bytes
Glib::RefPtr<Gdk::Pixbuf> pixBuf = Gdk::Pixbuf::create_from_data((const guint8 *) buffer,
       colorSpace, has_alpha, bits_per_sample, 360, 640, rowStride);
Glib::RefPtr<Gdk::GC> gcContext;
pixBuf->render_to_drawable(drawingArea pointer, gcContext, 0, 0, 0 , 0 , 360, 640, Gdk::RGB_DITHER_MAX, 0, 0);
//It prints the image in the left half of drawing area. 

这和我预期的一样,但是当我尝试绘制两个图像时出现了问题。 为此,我在缓冲区中读取 first.pgm,然后读取 second.pgm(意味着总 [360*640*3]*2 字节) 现在我绘制它,如代码所示。

Glib::RefPtr<Gdk::GC>gcContext;
Gdk::Colorspace colorSpace = Gdk::COLORSPACE_RGB;
Gdk::Colorspace colorSpace = (Gdk::Colorspace)0;
bool has_alpha = false;
int bits_per_sample = 8;
int rowStride = 360*3; // distance between rows in bytes
Glib::RefPtr<Gdk::Pixbuf> pixBuf = Gdk::Pixbuf::create_from_data((const guint8 *) buffer,
       colorSpace, has_alpha, bits_per_sample, 720, 640, rowStride);
pixBuf->render_to_drawable(drawingarea pointer, gcContext, 0, 0, 0 , 0 , 720, 640, Gdk::RGB_DITHER_MAX, 0, 0);

在两半中,都会打印 first.pgm。

【问题讨论】:

    标签: c++ gtk gtk3 gtkmm gdkpixbuf


    【解决方案1】:

    您可以使用 create_from_file() 方法打开这些图像,然后将其复制到另一个将要显示的 Pixbuf。 这是我的代码:

    绘图区.h

    #ifndef DRAWINGAREA_H
    #define DRAWINGAREA_H
    
    #include <gtkmm.h>
    
    
    class DrawingArea : public Gtk::DrawingArea
    {
    public:
        DrawingArea();
    
    protected:
        // Override default signal handler:
        virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
    private:
        // 1.Pgm image
        Glib::RefPtr<Gdk::Pixbuf> image1;
        // 2.Pgm image
        Glib::RefPtr<Gdk::Pixbuf> image2;
        //Pixel buffer for display
        Glib::RefPtr<Gdk::Pixbuf> display;
        // Scale of the image
        double scale;
    };
    #endif // DRAWINGAREA_H
    

    绘图区.cpp

    #include "DrawingArea.h"
    
    DrawingArea::DrawingArea()
    {
        // Load the images
        image1 = Gdk::Pixbuf::create_from_file("first.pgm");
        image2 = Gdk::Pixbuf::create_from_file("second.pgm");
        //create a 720x640 rgb Pixbuf
        display = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB,0,8,720,640);
    }
    // Call when the display need to be updated
    bool DrawingArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
    {
        //copy the two images to the display Pixbuf with this function:
        //void Gdk::Pixbuf::copy_area   (int src_x,int src_y,int width,int height,const Glib::RefPtr< Gdk::Pixbuf >& dest_pixbuf,int dest_x,int dest_y)     
        image1->copy_area(0,0,360,640,display,0,0);
        image2->copy_area(0,0,360,640,display,360,0);
        // Place the display Pixbuf at the center of the window
        Gdk::Cairo::set_source_pixbuf(cr, display, 0,0);
        // Update the whole drawing area
        cr->rectangle(0, 0, display->get_width(), display->get_height());
        // Fill the area with the image
        cr->fill();
        // The event has been handled.
        return true;
    }
    

    还有main.cpp

    #include <DrawingArea.h>
    #include <gtkmm.h>
    
    
    int main(int argc, char* argv[])
    {
        // Initialize gtkmm and create the main window
        Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "betontaplfa");
        Gtk::Window window;
    
        // Create the drawing
        DrawingArea Dwg;
        // Insert the drawing in the window
        window.add(Dwg);
        // Resize the window
        window.resize(720,640);
        // Set the window title
        window.set_title("Pgm");
        // Show the drawing
        Dwg.show();
    
        // Start main loop
        return app->run(window);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多