#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP

#include <memory>

#include <boost/gil/gil_all.hpp>

#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QImage>
#include <QtGui/QLabel>
#include <QtGui/QWidget>

//#include "image_details.hpp"

static void x_gradient(boost::gil::gray8c_view_t const &src, boost::gil::gray8s_view_t const &dst)
{
    for (int y = 0; y != src.height(); ++y)
        for (int x = 1; x != src.width() - 1; ++x)
            dst(x, y) = (src(x - 1, y) - src(x + 1, y)) / 2;
}

inline void ComputeXGradientGray8(unsigned char const *src_pixels, ptrdiff_t src_row_bytes,
                                  int w, int h, unsigned char *dst_pixels, ptrdiff_t dst_row_bytes)
{
    using namespace boost::gil;
    gray8c_view_t src = interleaved_view(w, h, (const gray8_pixel_t*)src_pixels, src_row_bytes);
    gray8s_view_t dst = interleaved_view(w, h, (     gray8s_pixel_t*)dst_pixels, dst_row_bytes);

    x_gradient(src, dst);
}

static inline void x_gradient_qt(QImage const &src, QImage &dst)
{
    ComputeXGradientGray8(src.bits(), src.bytesPerLine(), src.height(),
                          src.width(), dst.bits(), dst.bytesPerLine());
}        

static void read_image()
{    
    QImage src("../GIL_with_Qt/images_00/coneL.bmp");
    QImage dst(src.width(), src.height(), src.format());

    x_gradient_qt(src, dst); //this will cause segmentation fault                      
}

#endif // EXAMPLE_HPP

depth : 8bit
format : QImage::Format_Indexed8
image width : 450
image height : 375
bytes per scan line : 452

How to integrate QImage with the example provided by the boost::gil?
This would cause segmentation fault yet I don't know why.
The algorithm seems nothing wrong
Thanks for your help

Edited 4 Years Ago by stereomatching: make the codes become easier to read

Sorry, I find out the problem, I misplaced height and width
besides, the dst should be boost::gil::gray8_view_t

I solve the problem of segmentation fault
yet the dst image is pretty weird(only white and black)

template<typename T = QWidget>
static std::unique_ptr<T> view_image_pair(QImage const &src, QImage &dst)
{
    std::unique_ptr<T> window(new T);
    QHBoxLayout *layout = new QHBoxLayout(window.get());

    QLabel *src_label;
    layout->addWidget(src_label = new QLabel);
    src_label->setPixmap(QPixmap::fromImage(src));

    QLabel *dst_label;
    layout->addWidget(dst_label = new QLabel);
    dst_label->setPixmap(QPixmap::fromImage(dst));

    return std::move(window);
}

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

    std::unique_ptr<QWidget> window = view_image_pair(src, dst);
    x_gradient_qt(src, dst);

    window->show();

    a.exec();
}

What kind of mistakes I make this time?Thanks

Edited 4 Years Ago by stereomatching

Sorry, I solved the problem by myself
I forgot to measure the value of src(x - 1, y) and src(x + 1, y)
GIL makes the manipulation of pixels become much more easier
Thanks

This question has already been answered. Start a new discussion instead.