PDA

View Full Version : Copy to a memory image png or bmp



giorgik
22nd April 2013, 13:05
Hello everyone, I have an image assets / stall.png where asset is a directory located under the root of my program. I would like to copy my image in memory starting from img_flip.
This is the code that I wrote:


QImage img;
img.load(filename);
if (img.isNull())
{
printf( "unable to open image: %s\n", filename );
return 0;
}

QImage gldata = QGLWidget::convertToGLFormat(img);
uchar *pixels = (uint8_t *)gldata.bits();

//vertically flip the image
int img_size = gldata.width() * gldata.height() * gldata.depth();
uint8_t *img_flip = new uint8_t[ img_size ];

const int row_width = gldata.width() * gldata.depth();

for( int i = 0; i < gldata.height(); i++ )
memcpy( img_flip + ( i * row_width ), pixels + ( (gldata.height() - i - 1) * row_width ),
row_width );

I must have made a mistake because the execution stops after copying with i = 7.
Can you help me fix my error ?

Santosh Reddy
22nd April 2013, 15:49
gldata.depth(); return the size in bit(s), so you will have to use gldata.depth()/8 for size in byte. Also bit depth 1 has to taken are of.

giorgik
22nd April 2013, 18:18
Thanks Santosh Reddy