Question

I'm trying to construct QImages from data saved as 16-bit integers in a binary file. I'm able to load the data in fine, but my program fails when I use the QImage::loadFromData(QBytearray ba) function (returning false) as follows:

QBytearray frame;
QImage pic = QImage(256, 256, QImage::Format_RGB888);

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        // Access value of pixel at each location
        datum = store[i][j];

        for(int c = 0; c < 3; c++) {
            // Calculate colour at given pixel
            col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
            // Assign colour value to the pixel
            frame[c+3*j+3*i*width] = ((unsigned char)col);
        }
    }
}

pic.loadFromData(frame);

I repurposed this from Java code I had previously written which worked perfectly as intended (from the exact same data):

BufferedImage image = = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);

byte[] data = image.getRaster().getDataBuffer();

for (j=0; j<height; j++) {
    for (i=0; i<width; i++) {
        //Find value of the pixels at the location
        datum=data[j][i];
        for (c=0; c<3; c++) {
            //Calculate the colour at the given pixel
            col=(255.0f*((float)datum-(float)min)/((float)(max-min)));
            //Assign the colour value to the pixel
            data[c+3*i+3*j*width] = (byte)col;
        } 
    }
}

Can anybody help me to see where I'm getting this wrong? I've been stumped for days and am all out of ideas.

Était-ce utile?

La solution

Ok, assuming that you are in fact trying to set RGB values for individual pixels, after reading the QImage details, I see you can do it using the following:

value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);

So, something like:

QImage pic = QImage(256, 256, QImage::Format_RGB888);
QRgb value;
int r,b,g;

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        // Access value of pixel at each location
        datum = store[i][j];
        //I get really confused what is going on here... you don't seem to be actually using `c` for the calculation?
        for(int c = 0; c < 3; c++) { //let's just pretend you set the ints r,b,g in here somewhere
            // Calculate colour at given pixel
            col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
        }
        // Assign colour value to the pixel
        value = qRgb(r, g, b);
        pic.setPixel(i, j, value);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top