Click to See Complete Forum and Search --> : compresses the bitmap


Richard_Li
June 15th, 2005, 02:47 PM
I am trying to use stretchBlt to compresses a (B/W) bitmap file in the disk, but I don't know how to get two correct "hdc"s. Does anybody have any sample code for me? Thanks.

golanshahar
June 15th, 2005, 03:15 PM
read this:
http://www.codeguru.com/Cpp/G-M/bitmap/imagemanipulation/article.php/c4881/

if i helped dont forget to rate :-)
Cheers

Richard_Li
June 15th, 2005, 03:42 PM
I read it before posting the Question. Actually, I don't know how to define hdcDest and hdcSrc in C code before calling StretchBlt.

NoHero
June 15th, 2005, 03:57 PM
I read it before posting the Question. Actually, I don't know how to define hdcDest and hdcSrc in C code before calling StretchBlt.

Are you using pure C or C++?

Richard_Li
June 15th, 2005, 04:07 PM
Using C.

golanshahar
June 15th, 2005, 04:25 PM
if you using pure C then u have nothing to do with ::StrecthBlt(), u need to implement alone a scale function, that work on the lraw bits u having.
its not that complicated to do.

here is a pesoudo code just to give you the idea how to scale an image to half size...

// allocate half space for the scaled image
unsigned char *lpNewScale = new unsigned char[WIDTH/2 * HEIGHT/2 * 4];
int count = 0;
for (int i=0;i<WIDTH;i++)
{
for (int j=0;j<HEIGHT;j++)
{
lpNewScale[count] = lpSrcImage[i][j];
count+=2;
}
}


if i helped dont forget to rate :-)
Cheers

Richard_Li
June 15th, 2005, 05:24 PM
Here is my function to read the bitmap then to compress it. If the function 'StretchBlt" can't be used in my code even including wingdi.h, how to modify the current code to get bitmap compressed. Please give me a help.

BOOL CompresseMyBitmap( LPCTSTR strFileName)
{
BITMAPFILEHEADER bmfHeader;
ULONG iBytesRead;
BOOL bGood = FALSE;
char szNewFileName[256];
int iFileSzie,iSzieFH,nColors;
int i = 0;
int j = 0;
HANDLE hNewFile, hInputFile;
DWORD nPackedDIBLen;
PBYTE pBuf, pBufNew; //Prepare for the new buffer
LPBITMAPINFOHEADER bmiHeader;
LPBITMAPINFO bmInfo;
LPVOID lpDIBBits;
LPBYTE pBmpData;
int iBmpDataLen,iAlignedXBytes;

PBYTE pBufOriginal;

LPVOID ppvBits=NULL,lpBits;
HANDLE hSection = NULL;
DWORD dwOffset = 0;
HBITMAP hBmp;

HDC hIDC, hDestDC, hSrcDC;
HBITMAP hBMP;
LPVOID info;

hInputFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hInputFile == INVALID_HANDLE_VALUE)
return FALSE;

//To get a new file name for compressed bitmap
GetNewFileName2(strFileName, szNewFileName);

//Now create the new file
hNewFile = CreateFile(szNewFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

if(hNewFile == INVALID_HANDLE_VALUE)
return FALSE;

// Read file header
bGood = ReadFile(hInputFile, &bmfHeader, sizeof(bmfHeader), &iBytesRead, NULL);
if(bGood == FALSE)
return FALSE;

// File type should be 'BM'
if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
return FALSE;

// Get length of the remainder of the file and allocate memory
iFileSzie = GetFileSize(hInputFile, NULL);
iSzieFH = sizeof(BITMAPFILEHEADER);
nPackedDIBLen = iFileSzie - iSzieFH;
pBuf = (PBYTE)malloc(nPackedDIBLen);
pBufNew = (PBYTE)malloc(nPackedDIBLen); //Prepare for the new buffer
memset(pBufNew, 0xFF, nPackedDIBLen);

// Read the remainder of the bitmap file.
ReadFile(hInputFile,pBuf,nPackedDIBLen, &iBytesRead,NULL);
CloseHandle(hInputFile);

bmiHeader = (LPBITMAPINFOHEADER)pBuf ;
bmInfo = (LPBITMAPINFO)pBuf ;

//New added
//hBmp = CreateDIBSection(NULL,bmInfo,DIB_RGB_COLORS,&lpBits, hSection,dwOffset);

// If bmiHeader->biClrUsed is zero we have to infer the number
// of colors from the number of bits used to specify it.
nColors = bmInfo->bmiHeader.biClrUsed ? bmInfo->bmiHeader.biClrUsed :
1 << bmInfo->bmiHeader.biBitCount;

if( bmInfo->bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo->bmiColors + bmInfo->bmiHeader.biClrUsed) +
((bmInfo->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo->bmiColors + nColors);

pBmpData = (BYTE*)lpDIBBits;
iBmpDataLen = nPackedDIBLen - sizeof(BITMAPINFOHEADER) - nColors * 4; //Total bmp data bytes
iAlignedXBytes = iBmpDataLen / bmiHeader->biHeight; //Number of bytes in x axis

bmfHeader.bfSize = iSzieFH + sizeof(BITMAPINFOHEADER) + nColors * 4 + bmiHeader->biSizeImage;

for(j = 0; j < bmiHeader->biHeight; j++)
{
pBufOriginal = pBmpData + j * iAlignedXBytes;
for(i = 0; i < (int)(bmiHeader->biWidth/8); i++)
{
pBufNew[j*(int)(bmiHeader->biWidth/8) + i] = pBufOriginal[i];
}
}

WriteFile(hNewFile, &bmfHeader, sizeof(bmfHeader), &iBytesRead, NULL);
WriteFile(hNewFile, bmInfo, sizeof(*bmiHeader) + nColors * 4, &iBytesRead, NULL);
WriteFile(hNewFile, pBufNew, bmiHeader->biSizeImage, &iBytesRead, NULL);

//**************************************************//
//Here are the code that I try to define HDC to compress the bitmap
hIDC = GetDC (NULL);
hDestDC = GetDC (NULL);
hBMP = CreateDIBitmap(hIDC, bmiHeader,CBM_INIT,&iBytesRead, bmInfo, DIB_RGB_COLORS);

GetObject(hBMP, sizeof(info), (PTSTR) &info);
hSrcDC = CreateCompatibleDC (hDestDC);
SelectObject (hSrcDC, hBMP);

StretchBlt (hDestDC,0,0,248,48,hSrcDC,0,0,bmiHeader->biWidth,bmiHeader->biHeight,SRCCOPY);
DeleteDC (hDestDC);
DeleteDC (hSrcDC);
//End of compressing bitmap

CloseHandle(hNewFile);

free(pBuf);
free(pBufNew);

return TRUE;

}