开发者

Copying data correctly between Qt Qimage and Boost Multi Array

I want to copy the data from a Qt image into a Boost Multi Array, do some manipulation to the Multi Array and copy the data back to a QImage to display.

I am accessing the raw data with Qimage::bits() and trying to copy across with std::copy, and it seems there is a problem with data alignment that I don't understand. There is a note here about accessing the data for 32-bpp images, but the problem remains even if I convert the QImage to a different format.

I've put together a snippet illustrating a typical problem. There may well be multiple things I am doing incorrectly, so bear with me. Here I am trying to copy the top half of Image 2 to Image 1 and getting this output

#include <algorithm>
#include <boost/multi_array.hpp>
#include <开发者_运维技巧QImage>

typedef boost::multi_array<uchar, 3> image_type;

int main() {
    QString path = "/path/to/images/";
    QImage qimage_1(path + "image1.jpg");
    QImage qimage_2(path + "image2.jpg");

    image_type bimage_1(boost::extents[qimage_1.width()][qimage_1.height()][4]);
    image_type bimage_2(boost::extents[qimage_2.width()][qimage_2.height()][4]);

    std::copy(qimage_1.bits(), qimage_1.bits() + qimage_1.width()*qimage_1.height()*4, &bimage_1[0][0][0]);
    std::copy(qimage_2.bits(), qimage_2.bits() + qimage_2.width()*qimage_2.height()*4, &bimage_2[0][0][0]);

    // copy top half of image 2 to image 1
    for(int i = 0; i < qimage_1.width(); i++) {
        for(int j = 0; j < qimage_1.height()/2; j++) {
            bimage_1[i][j][0] = bimage_2[i][j][0];
            bimage_1[i][j][1] = bimage_2[i][j][1];
            bimage_1[i][j][2] = bimage_2[i][j][2];
            bimage_1[i][j][3] = bimage_2[i][j][3];
        }
    }

    std::copy(&bimage_1[0][0][0], &bimage_1[0][0][0] + bimage_1.num_elements(), qimage_1.bits());
    qimage_1.save(path + "output.png");
    return 0;
}

My .pro file simply contains SOURCES += main.cpp

Any help very much appreciated.


I think the easiest way is probably to copy it row by row using QImage::scanLine(row).
I think there could be padding at the end of each scanline that is throwing you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜