PDA

View Full Version : DIB to QImage or QPixmap



^NyAw^
8th August 2008, 21:18
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:


int iWidth = pDib->Width();
int iHeight = pDib->Height();
int iBytes = iWidth*iHeight;
uchar* uData = new uchar[iWidth*iHeight];
strncpy((char*)uData,(char*)pDib->GetLinePtr(0),iBytes);
QImage* image = new QImage(uData,iWidth,iHeight,QImage::Format_Indexed 8);
image->setNumColors(256);
image->setColorTable(m_qPalette);

bool bSave = pImage->save("image.jpg","JPG"); //Returns true, but the image is all gray


This other code takes care that the QImage have to be 32 bit aligned:


int iWidth = pDib->Width();
int iHeight = pDib->Height();
int iBytes = iWidth*iHeight;
uchar* uData = new uchar[iWidth*iHeight];
int iColumnsAdd = (iWidth % 4);
QImage* qImatge = new QImage(iWidth+iColumnsAdd ,iHeight,QImage::Format_Indexed8);
uchar* uData2 = qImatge->bits();
int iOriginalLineStart;
int iDestLineStart;
for (int i=0; i<iHeight; i++)
{
iOriginalLineStart = i*iWidth;
iDestLineStart = ((i*iWidth) + (i*iColumnsAdd));
memcpy(uData2+iDestLineStart,uData+iOriginalLineSt art,iWidth);
}
qImatge->setNumColors(256);
qImatge->setColorTable(m_qPalette);
return(qImatge);

bool bSave = pImage->save("image.jpg","JPG"); //Returns true, but the image is all gray


Thanks,