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.
store
a hard-coded set of predetermined values? If so, what was the result? If not, could you do that and give us the result? What isframe
when you finish building it? – MirroredFateframe
is when I've finished building it? Size? – StrongoQImage::loadFromData(QByteArray ba)
function works exactly as advertised. It seems as if the size discrepancy is the major issue, but even if I drop the colour setting (cfor
loop) I still have an array 65536 elements long. – Strongostore
, and aQByteArray
frame. You are moving unsigned chars (1 byte) from thestore
toframe
, but you need 3 bytes total for your RGB value. This meansframe
is essentially an array of bytes equal to 256*256*3, or 196609 bytes. I don't see where you are putting in header info to tell which kind of image it is, or any sort of meta characters to give the size... – MirroredFate