2012年5月26日 星期六

Qt and GIL(generic image library) : 00--Bind QImage and GIL together

  QImage is pretty good at reading and saving image, but it is pretty inconvenient to manipulate the raw pixels of QImage when the speed is critical.GIL give us easy solutions to deal with the raw data of the QImage.If we bind the GIL and QImage together, we could develop generic and effective algorithms much more easier.

   I would like to record and share my learning experiences on how to bind the GIL and QImage together .It is pretty easy to do it with the power of the GIL, but it did take me some times
to figure it out. I hope this could be a help for other programmers who would like to make
QImage and GIL to work together.

   The first example is deduce the x_gradient of the gray image(same as the example of boost), since boost already show us the basic of the GIL, I would focus on how to bind the QImage with the GIL in this post.

void x_gradient_1(boost::gil::gray8c_view_t const &src, boost::gil::gray8_view_t const &dst)
{
  for (int y = 0; y != src.height(); ++y)
        for (int x = 1; x != src.width() - 1; ++x)
        {
            if(src(x - 1, y) > src(x + 1, y))
                dst(x, y) = (src(x - 1, y) - src(x + 1, y)) / 2;
            else
                dst(x, y) = (src(x + 1, y) - src(x - 1, y)) / 2;
        }
}

void read_gray_image(QApplication &a)
{
    QImage src("../GIL_with_Qt/images_00/coneL.bmp");
    QImage dst(src.size(), src.format());    

    //must process the QImages before adding them to QLabel
    gray8c_view_t src_view = interleaved_view(src.width(), src.height(), (gray8_pixel_t const*)src.constBits(), src.bytesPerLine());
    gray8_view_t dst_view = interleaved_view(dst.width(), dst.height(), (     gray8_pixel_t*)dst.bits(), dst.bytesPerLine());
    x_gradient_1(src_view, dst_view);

    //to do : write some codes to show the image   

    a.exec();
}
Pretty simple, but binding the QImage to the view of GIL is too verbose, so I develop a factory to ease the chores. You could have the codes from my github "https://github.com/stereomatchingkiss/gil_with_qt_adaptor"

沒有留言:

張貼留言