Hi,

I need to convert a 8bpp gray DIB image to a QImage or QPixmap.

I had been trying multiple ways but it seems to not work.

This is the code:
Qt Code:
  1. int iWidth = pDib->Width();
  2. int iHeight = pDib->Height();
  3. int iBytes = iWidth*iHeight;
  4. uchar* uData = new uchar[iWidth*iHeight];
  5. strncpy((char*)uData,(char*)pDib->GetLinePtr(0),iBytes);
  6. QImage* image = new QImage(uData,iWidth,iHeight,QImage::Format_Indexed8);
  7. image->setNumColors(256);
  8. image->setColorTable(m_qPalette);
  9.  
  10. bool bSave = pImage->save("image.jpg","JPG"); //Returns true, but the image is all gray
To copy to clipboard, switch view to plain text mode 

This other code takes care that the QImage have to be 32 bit aligned:
Qt Code:
  1. int iWidth = pDib->Width();
  2. int iHeight = pDib->Height();
  3. int iBytes = iWidth*iHeight;
  4. uchar* uData = new uchar[iWidth*iHeight];
  5. int iColumnsAdd = (iWidth % 4);
  6. QImage* qImatge = new QImage(iWidth+iColumnsAdd ,iHeight,QImage::Format_Indexed8);
  7. uchar* uData2 = qImatge->bits();
  8. int iOriginalLineStart;
  9. int iDestLineStart;
  10. for (int i=0; i<iHeight; i++)
  11. {
  12. iOriginalLineStart = i*iWidth;
  13. iDestLineStart = ((i*iWidth) + (i*iColumnsAdd));
  14. memcpy(uData2+iDestLineStart,uData+iOriginalLineStart,iWidth);
  15. }
  16. qImatge->setNumColors(256);
  17. qImatge->setColorTable(m_qPalette);
  18. return(qImatge);
  19.  
  20. bool bSave = pImage->save("image.jpg","JPG"); //Returns true, but the image is all gray
To copy to clipboard, switch view to plain text mode 

Thanks,