我试图从二进制文件中保存为16位整数的数据构建QImages。我可以正常加载数据,但是当我使用QImage :: loadFromData(QBytearray ba)函数(返回false)时,我的程序失败,如下所示:
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);
我从之前编写的Java代码中重新利用了这个代码,这些代码完全符合预期(来自完全相同的数据):
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;
}
}
}
任何人都可以帮我看看我错在哪里吗?我已经被困了好几天,而且都是出于想法。
答案 0 :(得分:0)
好的,假设您实际上是在尝试为单个像素设置RGB值,在阅读QImage details之后,我发现您可以使用以下方法来实现:
value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);
所以,比如:
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);
}
}