PDA

View Full Version : QGraphicsView (using int array) advice and display help



enricong
10th April 2012, 02:44
I receive image data from an API as an int pointer to an array laid out in memory like a C array x[i][j][k]

What would be the recommended way to display this image in a graphics view?
I am currently using a nested loop to loop through the entire array and "QImage::setPixel(x,y,Qrgb)" to set every the color of every pixel. Then I turn the QImage into a Qpixmap, add it to a GraphicsScene, then link it to a GraphicsView.

ChrisW67
10th April 2012, 06:22
What would be the recommended way to display this image in a graphics view?
We have no idea what your three-dimensional integer array data represents or what a logical way to display it would be. How does a 3d array represent image data? Is it a stack of multiple images?

I am currently using a nested loop to loop through the entire array and "QImage::setPixel(x,y,Qrgb)" to set every the color of every pixel.
Where do x, y, and the arguments for the colour come from in the three-dimensional array?

enricong
10th April 2012, 18:49
Sorry, I should have provided more info

it is an int array of width,height,color
color is a RGB triplet of three integers (0-255)



for (int i=0; i<height; i++)
for (int j=0; j<width; j++)
{
int pix = i*width*3+j*3;
img->setPixel(i,j,qRgb(data[pix],data[pix+1],data[pix+2]));
}


The above codes works but I don't know if there is a better way to do this

ChrisW67
11th April 2012, 04:06
If the data is an array of ints (i.e. 4 byte numbers) then there's no better way I am aware of.

If the array is actually of bytes (unsigned char) then:


// Some test data: grey gradient 64x64 pixel image
uchar data[64 * 64 * 3];
int offset = 0;
for (int i = 0; i < 64; ++i) {
for (int j = 0; j < 64; ++j) {
data[offset++] = (i+j)*2;
data[offset++] = (i+j)*2;
data[offset++] = (i+j)*2;
}
}

// Create an image from that
QImage image(data, 64, 64, 64 * 3, QImage::Format_RGB888);
image.save("test.png");

works.

enricong
11th April 2012, 14:40
I did not realize that is what RGB888 meant. I am able to request the size of the int so I was able to put it in a uchar.

Thanks