Advertisement

How to improve this LoadBitmap function?

Started by January 24, 2000 10:28 AM
0 comments, last by SmilingBandit 25 years, 1 month ago
The following code opens a Bitmap file and reads the data into a new created DirectDrawSurface. It works (if BitsPerPixel of Bitmap and surface are the same), but it works slower than my other GDI based function. I have tested out, that nearly all time is consumed by blitting and rotating and less time by reading the bitmap file. Strange: Im some cases it seems to be faster to do stretching and rotating in two steps than in one! In others not! Please help me to improve. LPDIRECTDRAWSURFACE7 LoadFastBmp(char* szFilename,LPDDSURFACEDESC2 lpddsd) { FILE *pDatei; BITMAPFILEHEADER bmpFileHeader; BITMAPINFO bmpInfo; HRESULT ddrval; DDBLTFX ddBltFx; LPDIRECTDRAWSURFACE7 lpdds,lpddsTemp; BOOL bStretch=false; pDatei=fopen(szFilename,"r"); if(pDatei) { // read BitmapFileHeader and BitmapInfo fread(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,pDatei); fread(&bmpInfo,sizeof(BITMAPINFO),1,pDatei); // set file pointer to begin of bitmap data fseek(pDatei,bmpFileHeader.bfSize- bmpInfo.bmiHeader.biSizeImage,SEEK_SET); // initialize lpddsd of the destination surface if (lpddsd==NULL) { lpddsd=(LPDDSURFACEDESC2)malloc(sizeof(DDSURFACEDESC2)); memset(lpddsd,0,sizeof(DDSURFACEDESC2)); lpddsd->dwSize=sizeof(DDSURFACEDESC2); lpddsd->dwFlags=DDSD_CAPS/DDSD_WIDTH/DDSD_HEIGHT; lpddsd->ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN; lpddsd->dwWidth=bmpInfo.bmiHeader.biWidth; lpddsd->dwHeight=bmpInfo.bmiHeader.biHeight; } // test if to stretch if ((bmpInfo.bmiHeader.biWidth!=lpddsd->dwWidth)// (bmpInfo.bmiHeader.biHeight!=lpddsd->dwHeight)) bStretch=true; // create offscreen surface ddrval=lpdd7->CreateSurface(lpddsd,&lpdds,NULL); // create tempory surface for stretching and rotating memset(lpddsd,0,sizeof(DDSURFACEDESC2)); lpddsd->dwSize=sizeof(DDSURFACEDESC2); lpddsd->dwFlags=DDSD_CAPS/DDSD_WIDTH/DDSD_HEIGHT; lpddsd->ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN; lpddsd->dwWidth=bmpInfo.bmiHeader.biWidth; lpddsd->dwHeight=bmpInfo.bmiHeader.biHeight; // lpdd7->CreateSurface(lpddsd,&lpddsTemp,NULL); /////////////////////////////////////////////////////////// //Read Bitmap-Data into temporary surface /////////////////////////////////////////////////////////// lpddsTemp->Lock(NULL,lpddsd,DDLOCK_WAIT,NULL); fread((UCHAR*)(lpddsd->lpSurface), bmpInfo.bmiHeader.biBitCount/8, bmpInfo.bmiHeader.biHeight*bmpInfo.bmiHeader.biWidth, pDatei); // lpddsTemp->Unlock(NULL); fclose(pDatei); // rotate temporary surface DD_INIT_STRUCT(ddBltFx); ddBltFx.dwDDFX=DDBLTFX_MIRRORUPDOWN; lpddsTemp->Blt(NULL,lpddsTemp, NULL,DDBLT_WAIT/DDBLT_ASYNC/DDBLT_DDFX,&ddBltFx); // stretch while blitting to destination surface if (bStretch) { DD_INIT_STRUCT(ddBltFx); lpdds->Blt(NULL,lpddsTemp,NULL, DDBLT_WAIT/DDBLT_ASYNC,&ddBltFx); lpddsTemp->Release(); return lpdds; } else { lpdds->Release(); return lpddsTemp; } } else //if fopen failed { OutputDebugString("Datei konnte nicht geöffnet werden.\n"); return NULL; } return NULL; } The Smiling One (:
The Smiling One (:
Have a look at the file ddutil.cpp that comes with the DirectX SDK. It contains a function for creating a surface and loading a bitmap onto it. I don''t know if it''s faster than your version, but it''s less code (48 lines)

No sense being pessimistic. It wouldn''t work anyway.
===========================There are 10 types of people in the world. Those that understand binary and those that don't.( My views in no way reflect the views of my employer. )

This topic is closed to new replies.

Advertisement