How to output this 24-bit image in Qt

I have an unsigned array of char that is defined as follows:

unsigned char **chars = new unsigned char *[H]; for(int i = 0; i < H; i++) { chars[i] = new unsigned char[W*3]; } 

Where H is the image height and W is the width, and the characters are filled in the rest of this function, looping through the x columns of the input image column. Fonts filled in order Red Green Blue

I am trying to read it something like this:

 QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB888); for (int i = 0 ; i < imheight ; i++) for (int j = 0 ; j < imwidth ; j++) { //not sure why, but have to flip j and i in the index for setPixel qi->setPixel(j,i,qRgb(imageData[i][j],imageData[i][j+1],imageData[i][j+2])); j+=3; } QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor)); QPixmap p1(p.scaled(ui->menuBar->width(),ui->menuBar->width(), Qt::KeepAspectRatio, Qt::SmoothTransformation )); ui->viewLabel->setPixmap(p1); ui->viewLabel->setFixedHeight(p1.height()); ui->viewLabel->setFixedWidth(p1.width()); 

where char characters were returned to this calling function in the imageData array.

What am I doing wrong? Should I use a different format, although I clearly highlight 3 unsigned characters per pixel (which is why I chose the RGB888 format). This code as sent returns an image, but it does not display correctly - it is partially scrambled, washed, etc.

thanks

+1
source share
1 answer

You can create a QImage directly from a data block without copying each pixel.

Since your image is stored with separate lines, you need something like

 QImage image = new QImage(width,height,QImage::RGB888) for (int h=0;h<height;h++) { // scanLine returns a ptr to the start of the data for that row memcpy(image.scanLine(h),chars[h],width*3); } 

if you use RGB32, then you need to manually set the alpha channel for each pixel to 0xff - just memset () all the data in 0xff first

+4
source

All Articles