BOOL CRGBBitmapShow::CreateDIBPalette() { if(PaletteR!=NULL) delete PaletteR; PaletteR=NULL; if(PaletteG!=NULL) delete PaletteG; PaletteG=NULL; if(PaletteB!=NULL) delete PaletteB; PaletteB=NULL; if(!CreateDIBPalette(&PaletteR,0)) return FALSE; if(!CreateDIBPalette(&PaletteR,1)) return FALSE; if(!CreateDIBPalette(&PaletteR,2)) return FALSE; return TRUE; }
HBITMAP LoadResourceBitmap(HINSTANCE hInstance, LPCTSTR lpString, HPALETTE& rhPalette) { // Pre initialize in case of error rhPalette = NULL; HRSRC hRsrc = FindResource(hInstance, lpString, RT_BITMAP); if (hRsrc == NULL) return NULL; HGLOBAL hGlobal = LoadResource(hInstance, hRsrc); if (hGlobal == NULL) return NULL; LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal); if (lpbi == NULL) return NULL; HDC hdc = GetDC(NULL); int iNumColors; rhPalette = CreateDIBPalette ((LPBITMAPINFO)lpbi, iNumColors); if (rhPalette) { SelectPalette(hdc, rhPalette, FALSE); RealizePalette(hdc); } HBITMAP hBitmapFinal = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi, (LONG)CBM_INIT, (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS ); ReleaseDC(NULL,hdc); return (hBitmapFinal); }
HBITMAP ReadBitmapFile( HWND hwnd, char *file_name ) { FILE *fp; BITMAPFILEHEADER file_header; BITMAPINFOHEADER *h; /* to avoid typeing bitmap_info->... */ BITMAPINFO *bitmap_info; DWORD size; /* generic size - used repeatedly */ BYTE _HUGE *mask_ptr; /* pointer to bit array in memory */ HBITMAP bitmap_handle; HDC hdc; HPALETTE new_palette, old_palette; bitmap_handle = (HBITMAP)0; fp = fopen( file_name, "rb" ); if( fp == NULL ) return( bitmap_handle ); fread( &file_header, sizeof( BITMAPFILEHEADER ), 1, fp ); if( file_header.bfType != BITMAP_TYPE ) { fclose( fp ); return( bitmap_handle ); } bitmap_info = ReadBitmapInfo( fp ); if( bitmap_info != NULL ) { h = &bitmap_info->bmiHeader; fseek( fp, file_header.bfOffBits, SEEK_SET ); size = BITS_TO_BYTES( h->biWidth * h->biBitCount, h->biHeight ); #ifdef __NT__ mask_ptr = malloc( size ); memset( mask_ptr, 0, size ); #else mask_ptr = _halloc( size, 1 ); #endif if( mask_ptr != NULL ) { ReadInPieces( mask_ptr, fp, size ); new_palette = CreateDIBPalette( bitmap_info ); if( new_palette ) { hdc = GetDC( hwnd ); old_palette = SelectPalette( hdc, new_palette, FALSE ); RealizePalette( hdc ); bitmap_handle = CreateDIBitmap( hdc, h, CBM_INIT, mask_ptr, bitmap_info, DIB_RGB_COLORS ); SelectPalette( hdc, old_palette, FALSE ); DeleteObject( new_palette ); ReleaseDC( hwnd, hdc ); } #ifdef __NT__ free( mask_ptr ); #else _hfree( mask_ptr ); #endif } free( bitmap_info ); } fclose( fp ); return( bitmap_handle ); } /* ReadBitmapFile */
bool CBitmapShow::ResetPaletteColor(int*r,int*g,int*b) { int NumberColors=GetNumberColors(); for(int k=0;k<NumberColors;k++) { m_lpBitmapInfo->bmiColors[k].rgbRed=r[k]; m_lpBitmapInfo->bmiColors[k].rgbGreen=g[k]; m_lpBitmapInfo->bmiColors[k].rgbBlue=b[k]; } return CreateDIBPalette(); }
bool CRGBBitmapShow::InitialBitmap(int width,int height) { Width=width; Height=height; for(int k=0;k<3;k++) { if(!pShow[k].InitialBitmap(Width,Height)) return FALSE; } if(!CreateDIBPalette()) return false; return TRUE; }
// Bitmaps einlesen und Paletten initialisieren bool CBildObjekt :: InitDIB (const char *pcFullName) { int fh; /* Dateihandle (evtl. komprimiertes File) */ OFSTRUCT of; /* Dateistruktur */ HPALETTE hPalCurrent; /* PaletteStruct */ HBITMAP hDIBCurrent; /* DIB */ HBITMAP hBMCurrent; /* DDB */ // alte DIB freigeben FreeDIB (); // Datei eröffnen, Speicherplatz anlegen fh = OpenFile (pcFullName, (OFSTRUCT *)&of, OF_READ); if (fh < 0) return false; hDIBCurrent = ReadDIBitmapInfo (fh); // Farbtabelle lesen // Gültigkeit der DIB-Datei prüfen if (hDIBCurrent == NULL) { _lclose (fh); return false; } m_hDIB = hDIBCurrent; // im Objekt merken // PaletteSetup hPalCurrent = CreateDIBPalette (); // Palette bilden if (hPalCurrent == NULL) { _lclose (fh); return false; } m_hPal = hPalCurrent; // DIB einlesen hDIBCurrent = ReadDIB (fh); if (!hDIBCurrent) { _lclose (fh); return false; } m_hDIB = hDIBCurrent; _lclose (fh); // DDB bilden hBMCurrent = DIBToBitmap(); if (!hBMCurrent) return false; m_hDDB = hBMCurrent; return true; }
HBITMAP CCustomBitmap::LoadResourceBitmap(INT nResourceID, HPALETTE* lphPalette) { HRSRC hRsrc; HGLOBAL hGlobal; HBITMAP hBitmap = NULL; LPBITMAPINFOHEADER lpbi; HDC hDC; INT nNumColors; if (!(hRsrc = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(nResourceID), RT_BITMAP))) return NULL; hGlobal = LoadResource(AfxGetResourceHandle(), hRsrc); if (!hGlobal) return NULL; lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal); hDC = ::GetDC(NULL); *lphPalette = CreateDIBPalette((LPBITMAPINFO)lpbi, &nNumColors); if (*lphPalette) { SelectPalette(hDC, *lphPalette, FALSE); RealizePalette(hDC); } hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpbi, (LONG)CBM_INIT, (LPTSTR)lpbi + lpbi->biSize + nNumColors * sizeof(RGBQUAD), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS); ::ReleaseDC(NULL, hDC); UnlockResource(hGlobal); FreeResource(hGlobal); return hBitmap; }
bool CBitmapShow::InitialBitmap(int width,int height) { if((Width==width)&&(Height==height)) return true; Width=width; Height=height; if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); int colors=GetNumberColors(); if(colors==0) colors=256; m_lpBitmapInfo = (LPBITMAPINFO) GlobalAllocPtr(GHND,sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * colors); m_lpBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); m_lpBitmapInfo->bmiHeader.biWidth = Width; m_lpBitmapInfo->bmiHeader.biHeight = Height; m_lpBitmapInfo->bmiHeader.biCompression=GetCompressionKind(); m_lpBitmapInfo->bmiHeader.biSizeImage = 0; m_lpBitmapInfo->bmiHeader.biXPelsPerMeter = 0; m_lpBitmapInfo->bmiHeader.biYPelsPerMeter = 0; m_lpBitmapInfo->bmiHeader.biPlanes = 1; m_lpBitmapInfo->bmiHeader.biBitCount =GetBitCount(); m_lpBitmapInfo->bmiHeader.biClrUsed = 0; m_lpBitmapInfo->bmiHeader.biClrImportant = 0; ILineBytes=WIDTHBYTES(Width*GetBitCount()); m_lpBitmapInfo->bmiHeader.biSizeImage=ILineBytes*Height; for(int k=0;k<colors;k++) m_lpBitmapInfo->bmiColors[k].rgbRed=m_lpBitmapInfo->bmiColors[k].rgbGreen=m_lpBitmapInfo->bmiColors[k].rgbBlue=k; if(!CreateDIBPalette()) { if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); m_lpBitmapInfo=NULL; return false; } if(lpDIBBits!=NULL) GlobalFreePtr(lpDIBBits); lpDIBBits=NULL; lpDIBBits=(unsigned char*)GlobalAllocPtr(GHND,ILineBytes*Height); if(lpDIBBits==NULL) { if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); m_lpBitmapInfo=NULL; if(Palette!=NULL) delete Palette; Palette=NULL; return false; } return true; }
QPixmap* QTwainInterface::convertToPixmap(CDIB* pDib)//,const unsigned int nWidth,const unsigned int nHeight) { if (!pDib) return 0; unsigned int nUseWidth; unsigned int nUseHeight; // if (nUseWidth == 0) nUseWidth = pDib->Width(); // if (nUseHeight == 0) nUseHeight = pDib->Height(); //QPixmap* retval = new QPixmap(nUseWidth, nUseHeight); HANDLE hdib=pDib->DIBHandle(); HPALETTE pepe =CreateDIBPalette(hdib); HBITMAP ima =DIBToBitmap(hdib,pepe); QPixmap* retval2= new QPixmap(QPixmap::fromWinHBITMAP ( ima,QPixmap::NoAlpha)); //return retval2; //HDC hehe; /*pDib->BitBlt(hehe, 0, 0, nUseWidth, nUseHeight, 0, 0); QPixmap buffer(400, 200); ... bitBlt(&buffer, 0, 0, src, 0, 0, 400, 200); ... QPainter p(this); p.drawPixmap(0, 0, buffer); //HBITMAP ima =pDib->DIBToDDB(pdib); //QPixmap* retval2= new QPixmap(retval->fromWinHBITMAP ( ima,QPixmap::NoAlpha)); */ return retval2; } // !convertToPixmap()
HBITMAP CSplashWnd::LoadResourceBitmap(HINSTANCE hInstance, LPCTSTR lpString) { HGLOBAL hGlobal; HBITMAP hBitmapFinal = NULL; LPBITMAPINFOHEADER lpbi; HDC hdc; CDC* dc; int iNumColors; HRSRC hRsrc = FindResource(hInstance, lpString, RT_BITMAP); if (hRsrc) { hGlobal = LoadResource(hInstance, hRsrc); lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal); CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors); dc = GetDC(); hdc = dc->GetSafeHdc(); CPalette* oldpal = dc->SelectPalette(&Palette,FALSE); dc->RealizePalette(); hBitmapFinal = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi, (LONG)CBM_INIT, (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS ); dc->SelectPalette(oldpal,FALSE); dc->RealizePalette(); ReleaseDC( dc ); UnlockResource(hGlobal); FreeResource(hGlobal); } return (hBitmapFinal); }
HBITMAP LoadResourceBitmap(HINSTANCE hInstance, LPTSTR lpString, HPALETTE FAR* lphPalette) { HRSRC hRsrc; HGLOBAL hGlobal; HBITMAP hBitmapFinal = NULL; LPBITMAPINFOHEADER lpbi; HDC hdc; int iNumColors; if (hRsrc = FindResource(hInstance, lpString, RT_BITMAP)) { hGlobal = LoadResource(hInstance, hRsrc); lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal); hdc = GetDC(NULL); *lphPalette = CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors); if (*lphPalette) { SelectPalette(hdc,*lphPalette,FALSE); RealizePalette(hdc); } hBitmapFinal = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi, (LONG)CBM_INIT, (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS ); ReleaseDC(NULL,hdc); UnlockResource(hGlobal); FreeResource(hGlobal); } return (hBitmapFinal); }
HDIB ChangeDIBFormat(HDIB hDIB, WORD wBitCount, DWORD dwCompression) { HDC hDC; // Handle to DC HBITMAP hBitmap; // Handle to bitmap BITMAP Bitmap; // BITMAP data structure BITMAPINFOHEADER bi; // Bitmap info header LPBITMAPINFOHEADER lpbi; // Pointer to bitmap info HDIB hNewDIB = NULL; // Handle to new DIB HPALETTE hPal, hOldPal; // Handle to palette, prev pal WORD DIBBPP, NewBPP; // DIB bits per pixel, new bpp DWORD DIBComp, NewComp;// DIB compression, new compression // Check for a valid DIB handle if (!hDIB) return NULL; // Get the old DIB's bits per pixel and compression format lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB); DIBBPP = ((LPBITMAPINFOHEADER)lpbi)->biBitCount; DIBComp = ((LPBITMAPINFOHEADER)lpbi)->biCompression; GlobalUnlock(hDIB); // Validate wBitCount and dwCompression // They must match correctly (i.e., BI_RLE4 and 4 BPP or // BI_RLE8 and 8BPP, etc.) or we return failure if (wBitCount == 0) { NewBPP = DIBBPP; if ((dwCompression == BI_RLE4 && NewBPP == 4) || (dwCompression == BI_RLE8 && NewBPP == 8) || (dwCompression == BI_RGB)) NewComp = dwCompression; else return NULL; } else if (wBitCount == 1 && dwCompression == BI_RGB) { NewBPP = wBitCount; NewComp = BI_RGB; } else if (wBitCount == 4) { NewBPP = wBitCount; if (dwCompression == BI_RGB || dwCompression == BI_RLE4) NewComp = dwCompression; else return NULL; } else if (wBitCount == 8) { NewBPP = wBitCount; if (dwCompression == BI_RGB || dwCompression == BI_RLE8) NewComp = dwCompression; else return NULL; } else if (wBitCount == 24 && dwCompression == BI_RGB) { NewBPP = wBitCount; NewComp = BI_RGB; } else return NULL; // Save the old DIB's palette hPal = CreateDIBPalette(hDIB); if (!hPal) return NULL; // Convert old DIB to a bitmap hBitmap = DIBToBitmap(hDIB, hPal); if (!hBitmap) { DeleteObject(hPal); return NULL; } // Get info about the bitmap GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap); // Fill in the BITMAPINFOHEADER appropriately bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = Bitmap.bmWidth; bi.biHeight = Bitmap.bmHeight; bi.biPlanes = 1; bi.biBitCount = NewBPP; bi.biCompression = NewComp; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // Go allocate room for the new DIB hNewDIB = AllocRoomForDIB(bi, hBitmap); if (!hNewDIB) return NULL; // Get a pointer to the new DIB lpbi = (LPBITMAPINFOHEADER)GlobalLock(hNewDIB); // Get a DC and select/realize our palette in it hDC = GetDC(NULL); hOldPal = SelectPalette(hDC, hPal, FALSE); RealizePalette(hDC); // Call GetDIBits and get the new DIB bits if (!GetDIBits(hDC, hBitmap, 0, (UINT) lpbi->biHeight, (LPSTR)lpbi + (WORD)lpbi->biSize + PaletteSize((LPSTR)lpbi), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS)) { GlobalUnlock(hNewDIB); GlobalFree(hNewDIB); hNewDIB = NULL; } // Clean up and return SelectPalette(hDC, hOldPal, TRUE); RealizePalette(hDC); ReleaseDC(NULL, hDC); // Unlock the new DIB's memory block if (hNewDIB) GlobalUnlock(hNewDIB); DeleteObject(hBitmap); DeleteObject(hPal); return hNewDIB; }
static HBITMAP WRReadBitmap( BYTE *data, long offset, bool core, bitmap_info *info ) { DWORD size; /* generic size - used repeatedly */ BYTE _HUGE *mask_ptr; /* pointer to bit array in memory */ HDC hdc; HPALETTE new_palette, old_palette; BITMAPINFO *bm_info = NULL; BITMAPCOREINFO *bm_core = NULL; HBITMAP bitmap_handle; int pos; bitmap_handle = (HBITMAP)NULL; if( core ) { bm_core = WRReadCoreInfo( &data ); if( bm_core == NULL ) { return( bitmap_handle ); } size = BITS_TO_BYTES( bm_core->bmciHeader.bcWidth * bm_core->bmciHeader.bcBitCount, bm_core->bmciHeader.bcHeight ); } else { bm_info = WRReadDIBInfo( &data ); if( bm_info == NULL ) { return( bitmap_handle ); } size = BITS_TO_BYTES( bm_info->bmiHeader.biWidth * bm_info->bmiHeader.biBitCount, bm_info->bmiHeader.biHeight ); } pos = offset; mask_ptr = __halloc( size, 1 ); if( mask_ptr != NULL ) { WRReadInPieces( mask_ptr, data, size ); if( core ) { BITMAPCOREHEADER *h; h = &bm_core->bmciHeader; /* * This will cause a GP Fault! */ bitmap_handle = CreateBitmap( h->bcWidth, h->bcHeight, h->bcPlanes, h->bcBitCount, mask_ptr ); } else { if( bm_info->bmiHeader.biBitCount < 9 ) { /* Bitmap has palette, create it */ new_palette = CreateDIBPalette( bm_info ); if( new_palette != NULL ) { hdc = GetDC( (HWND)NULL ); old_palette = SelectPalette( hdc, new_palette, FALSE ); RealizePalette( hdc ); bitmap_handle = CreateDIBitmap( hdc, &bm_info->bmiHeader, CBM_INIT, mask_ptr, bm_info, DIB_RGB_COLORS ); SelectPalette( hdc, old_palette, FALSE ); DeleteObject( new_palette ); ReleaseDC( (HWND)NULL, hdc ); } } else { /* Bitmap with no palette*/ hdc = GetDC( (HWND)NULL ); bitmap_handle = CreateDIBitmap( hdc, &bm_info->bmiHeader, CBM_INIT, mask_ptr, bm_info, DIB_RGB_COLORS ); ReleaseDC( (HWND)NULL, hdc ); } } __hfree( mask_ptr ); } if( core ) { if( info != NULL ) { info->u.bm_core = bm_core; } else { MemFree( bm_core ); } } else { if( info != NULL ) { info->u.bm_info = bm_info; } else { MemFree( bm_info ); } } return( bitmap_handle ); }
// **************************************************************************** // // Function Name: LoadResourceBitmap( ) // // Description: Load DIB from resource and get it's palette // // Returns: bitmap handle or NULL on failure // // Exceptions: None // // **************************************************************************** // HBITMAP LoadResourceBitmap(HINSTANCE hInstance, LPSTR lpString, HPALETTE FAR* lphPalette) { // initialise all our variables HRSRC hRsrc = NULL; HGLOBAL hGlobal = NULL; HGLOBAL hTemp = NULL; DWORD dwSize = 0; HBITMAP hBitmapFinal = NULL; HPALETTE hPalOld = NULL; LPSTR lpRes = NULL; LPSTR lpNew = NULL; HDC hdc = NULL; int iNumColors = 0; LPBITMAPINFOHEADER lpbi = NULL; // find the resource in the resource file hRsrc = FindResource(hInstance, lpString, RT_BITMAP); if ( hRsrc != NULL ) { // get a handle to the resource data hTemp = LoadResource(hInstance, hRsrc); if ( hTemp != NULL ) { // get it's size dwSize = SizeofResource(hInstance, hRsrc); // lock the resource data lpRes = (char*) LockResource(hTemp); if ( lpRes != NULL ) { // allocate memory to copy the data hGlobal = GlobalAlloc(GHND, dwSize); if ( hGlobal != NULL ) { // lock the allocated memory lpNew = (char*)GlobalLock(hGlobal); if ( lpNew != NULL ) { // copy the data memcpy(lpNew, lpRes, dwSize); // unlock and free the resource UnlockResource(hTemp); FreeResource(hTemp); lpbi = (LPBITMAPINFOHEADER)lpNew; // get a screen DC compatible with the screen hdc = GetDC(NULL); // create a palette from the bitmap info header *lphPalette = CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors); // select the palette into the DC if (*lphPalette) { hPalOld = SelectPalette(hdc,*lphPalette,FALSE); RealizePalette(hdc); } // create the DIB bitmap using the DC's palette hBitmapFinal = CreateDIBitmap( hdc, (LPBITMAPINFOHEADER)lpbi, (LONG) CBM_INIT, (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS ); if ( *lphPalette && hPalOld ) SelectPalette(hdc,hPalOld,FALSE); // release the HDC ReleaseDC(NULL,hdc); // unlock the allocated bitmap data GlobalUnlock(hGlobal); } // GlobalLock failed // free the bitmpa data GlobalFree(hGlobal); } // GlobalAlloc failed } // LockResource failed } // LoadResource failed } // FindResource failed // return the bitmap return hBitmapFinal; }