//--------------------------------------------------------------------- // // Function: PaletteSize // // Purpose: Given a pointer to a DIB, returns number of bytes // in the DIB's color table. // // Parms: lpbi == pointer to DIB header (either BITMAPINFOHEADER // or BITMAPCOREHEADER) // // History: Date Reason // 6/01/91 Created // //--------------------------------------------------------------------- static WORD PaletteSize (LPSTR lpbi) { if (IS_WIN30_DIB (lpbi)) return (DIBNumColors (lpbi) * sizeof (RGBQUAD)); else return (DIBNumColors (lpbi) * sizeof (RGBTRIPLE)); }
WORD PaletteSize(LPSTR lpDIB) { // calculate the size required by the palette if (IS_WIN30_DIB (lpDIB)) return (DIBNumColors(lpDIB) * sizeof(RGBQUAD)); else return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE)); }
WORD FAR CJpegFile::PaletteSize(LPSTR lpDIB) { /* calculate the size required by the palette */ if (IS_WIN30_DIB (lpDIB)) return (DIBNumColors(lpDIB) * sizeof(RGBQUAD)); else return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE)); }
// Umformen einer DIB in eine DDB HBITMAP CBildObjekt :: DIBToBitmap (void) { BITMAPINFOHEADER *lpBI; HPALETTE hPalT; HDC hDC; HBITMAP hBM; if (!m_hDIB) return NULL; // Bitmapheader lpBI = (BITMAPINFOHEADER *)GlobalLock (m_hDIB); if (!lpBI) return NULL; // Palette realisieren hDC = :: GetDC (NULL); if (m_hPal) { hPalT = SelectPalette (hDC, m_hPal, false); RealizePalette (hDC); } // device dependent bitmap (DDB) erzeugen hBM = CreateDIBitmap (hDC, lpBI, (long)CBM_INIT, (char *)lpBI + lpBI->biSize + DIBNumColors(lpBI)*sizeof(RGBQUAD), // PaletteSize (lpBI), (BITMAPINFO *)lpBI, DIB_RGB_COLORS); // alte Palette wiedereinstellen if (m_hPal) SelectPalette (hDC, hPalT, false); ReleaseDC (NULL, hDC); GlobalUnlock (m_hDIB); return hBM; }
// Liest Dateiheader und Farbtabelle im DIBformat ein HBITMAP CBildObjekt :: ReadDIBitmapInfo (unsigned int fh) { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; unsigned int iNumColors = 0; HBITMAP hBI = NULL; // Farbtabelle - HANDLE BITMAPINFOHEADER *lpBI = NULL; RGBQUAD *lpRGB; // Farbtabelle ULONG dwOffset; // Dateiposition if (fh == -1) return NULL; // ungültiges Dateihandle // Dateiheader (BITMAPFILEHEADER) lesen dwOffset = _llseek (fh, 0L, SEEK_CUR); if (_hread (fh, &bf, sizeof(bf)) != sizeof(bf)) return NULL; // Not a DIB ? if (!ISDIB (bf.bfType)) return NULL; // BitMapHeader (BITMAPINFOHEADER) lesen if (_hread (fh, &bi, sizeof(bi)) != sizeof(bi)) return NULL; // not a DIB iNumColors = DIBNumColors ((BITMAPINFOHEADER *)&bi); if (bi.biSize != sizeof(BITMAPINFOHEADER)) return NULL; // falsches Datenformat // Defaultgrößen setzen if (bi.biSizeImage == 0) bi.biSizeImage = WIDTHBYTES ((ULONG)bi.biWidth * bi.biBitCount) *bi.biHeight; if (bi.biClrUsed == 0) bi.biClrUsed = iNumColors; // Speicher für BitMap und Farbtabelle anfordern hBI = (HBITMAP)GlobalAlloc (GHND, (ULONG)bi.biSize + iNumColors * sizeof(RGBQUAD)); if (!hBI) return NULL; lpBI = (BITMAPINFOHEADER *)GlobalLock (hBI); *lpBI = bi; lpRGB = (RGBQUAD *)((char *)lpBI + bi.biSize); // Farbtabelle einlesen if (iNumColors) _hread (fh, lpRGB, iNumColors * sizeof(RGBQUAD)); // an Anfang der BitMap positionieren if (bf.bfOffBits != 0) _llseek (fh, dwOffset + bf.bfOffBits, SEEK_SET); m_dim = CSize(bi.biWidth, bi.biHeight); // Speicher wieder freigeben GlobalUnlock (hBI); return hBI; }
/* Bestimmen der DIB - Info aus einem CF_DIB Speicherblock */ bool CBildObjekt :: DIBInfo (BITMAPINFOHEADER *lpBI) { if (m_hDIB) { // BitmapInfoHeader kopieren *lpBI = *(BITMAPINFOHEADER *)GlobalLock (m_hDIB); // DefaultWerte setzen if (lpBI->biSizeImage == 0L) lpBI->biSizeImage = lpBI->biHeight * WIDTHBYTES ((ULONG)lpBI->biWidth*lpBI->biBitCount); if (lpBI->biClrUsed == 0L) lpBI->biClrUsed = DIBNumColors (lpBI); GlobalUnlock (m_hDIB); return true; } return false; }
// Bilden der Palette für BITMAPINFOSTRUCT HPALETTE CBildObjekt :: CreateBIPalette (BITMAPINFOHEADER *lpBI) { RGBQUAD *lpRGB; unsigned int iNumColors; LOGPALETTE *pPal; HPALETTE hPal = NULL; HGLOBAL h; // Anfangstests if (!lpBI) return NULL; if (lpBI->biSize != sizeof(BITMAPINFOHEADER)) return NULL; // Logische Palette füllen und vereinbaren lpRGB = (RGBQUAD *)((char *)lpBI + (unsigned int)lpBI->biSize); iNumColors = DIBNumColors (lpBI); h = (HGLOBAL)GlobalAlloc (GHND, sizeof(LOGPALETTE) + iNumColors * sizeof(PALETTEENTRY)); if (!h) return NULL; pPal = (LOGPALETTE *)GlobalLock(h); if (!pPal) return NULL; // Palette füllen pPal->palNumEntries = iNumColors; pPal->palVersion = 0x300; for (unsigned short i = 0; i < iNumColors; ++i) { pPal->palPalEntry[i].peRed = lpRGB[i].rgbRed; pPal->palPalEntry[i].peGreen = lpRGB[i].rgbGreen; pPal->palPalEntry[i].peBlue = lpRGB[i].rgbBlue; pPal->palPalEntry[i].peFlags = (BYTE)0; } hPal = CreatePalette (pPal); GlobalUnlock (h); GlobalFree (h); return hPal; }
// Einlesen der DIB in den Speicher HBITMAP CBildObjekt :: ReadDIB (unsigned fh) { ULONG dwBits; // Größe der DIB ULONG dwLen; HBITMAP h; BITMAPINFOHEADER bi; BITMAPINFOHEADER *lpbi; // Benoetigten Speicherplatz berechnen DIBInfo (&bi); dwBits = bi.biSizeImage; dwLen = bi.biSize + (ULONG)(DIBNumColors(&bi)*sizeof(RGBQUAD)); // PaletteSize (&bi); // Vergrößern des Speicherplatzes für gesamte Bitmap h = (HBITMAP)GlobalReAlloc (m_hDIB, dwLen + dwBits, GHND); if (!h) { /* gesamten Speicher freigeben */ GlobalFree (m_hDIB); m_hDIB = NULL; return NULL; } // DIB-Bits einlesen lpbi = (BITMAPINFOHEADER *)GlobalLock (h); if ((DWORD)_hread (fh, (char *)lpbi + dwLen, dwBits) != dwBits) { // LeseFehler GlobalUnlock (h); GlobalFree (h); GlobalFree (m_hDIB); // bisher angeforderten Speicher freigeben m_hDIB = NULL; return NULL; } GlobalUnlock (h); return h; }
HPALETTE CreateDIBPalette(HDIB hDIB) { LPLOGPALETTE lpPal; // pointer to a logical palette HANDLE hLogPal; // handle to a logical palette HPALETTE hPal = NULL; // handle to a palette int i, wNumColors; // loop index, number of colors in color table LPSTR lpbi; // pointer to packed-DIB LPBITMAPINFO lpbmi; // pointer to BITMAPINFO structure (Win3.0) LPBITMAPCOREINFO lpbmc; // pointer to BITMAPCOREINFO structure (OS/2) BOOL bWinStyleDIB; // Win3.0 DIB? // if handle to DIB is invalid, return NULL if (!hDIB) return NULL; // lock DIB memory block and get a pointer to it lpbi = (LPSTR)GlobalLock(hDIB); // get pointer to BITMAPINFO (Win 3.0) lpbmi = (LPBITMAPINFO)lpbi; // get pointer to BITMAPCOREINFO (OS/2 1.x) lpbmc = (LPBITMAPCOREINFO)lpbi; // get the number of colors in the DIB wNumColors = DIBNumColors(lpbi); // is this a Win 3.0 DIB? bWinStyleDIB = IS_WIN30_DIB(lpbi); if (wNumColors) { // allocate memory block for logical palette hLogPal = GlobalAlloc(GHND, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * wNumColors); // if not enough memory, clean up and return NULL if (!hLogPal) { GlobalUnlock(hDIB); return NULL; } // lock memory block and get pointer to it lpPal = (LPLOGPALETTE)GlobalLock(hLogPal); // set version and number of palette entries lpPal->palVersion = PALVERSION; lpPal->palNumEntries = wNumColors; // store RGB triples (if Win 3.0 DIB) or RGB quads (if OS/2 DIB) // into palette for (i = 0; i < wNumColors; i++) { if (bWinStyleDIB) { lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed; lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen; lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue; lpPal->palPalEntry[i].peFlags = 0; } else { lpPal->palPalEntry[i].peRed = lpbmc->bmciColors[i].rgbtRed; lpPal->palPalEntry[i].peGreen = lpbmc->bmciColors[i].rgbtGreen; lpPal->palPalEntry[i].peBlue = lpbmc->bmciColors[i].rgbtBlue; lpPal->palPalEntry[i].peFlags = 0; } } // create the palette and get handle to it hPal = CreatePalette(lpPal); // if error getting handle to palette, clean up and return NULL if (!hPal) { GlobalUnlock(hLogPal); GlobalFree(hLogPal); return NULL; } } // clean up GlobalUnlock(hLogPal); GlobalFree(hLogPal); GlobalUnlock(hDIB); // return handle to DIB's palette return hPal; }
void WMFUpdateState::DoDibBlt(CRect crSource, CRect crDest, DWORD dwROP, WORD wUsage /*=DIB_RGB_COLORS*/) { // Read the bitmap info header. This tells us how many colors we have. BITMAPINFOHEADER bmi; file.read(&bmi, sizeof(bmi)); // TRACE("DoBitBlt\n"); // Compute the size of the color table. // If wUsage is DIB_PAL_COLORS, then the color table is composed of 16-bit // indices into the currently realized palette. WORD wPaletteSize = DIBNumColors(&bmi) * (wUsage == DIB_PAL_COLORS ? sizeof(WORD) : sizeof(RGBQUAD)); BITMAPINFO* pbi = NULL; LPBYTE pData = NULL; TRY { if (wPaletteSize == 0) { // We do not have a palette. Just use the info header as is. pbi = (BITMAPINFO*)&bmi; } else { pbi = (BITMAPINFO*)(new BYTE[sizeof(bmi) + wPaletteSize]); // Copy over the header. pbi->bmiHeader = bmi; // Read the color table. file.read(pbi->bmiColors, wPaletteSize); } ASSERT(pbi != NULL); // Read the bitmap data. DWORD dwSize = pbi->bmiHeader.biSizeImage; if (dwSize == 0) { // Compute biSizeImage. dwSize = WIDTHBYTES (pbi->bmiHeader.biWidth * pbi->bmiHeader.biBitCount) * pbi->bmiHeader.biHeight; } pData = new BYTE[dwSize]; file.huge_read(pData, dwSize); // TRACE("DIB - w:%d, h:%d, b:%d (s: %d); ROP: %08lx\n", // pbi->bmiHeader.biWidth, // pbi->bmiHeader.biHeight, // pbi->bmiHeader.biBitCount, // dwSize, // dwROP); // Now, draw it! (Arguments similar to StretchDIBits). DrawBitmap(crDest, crSource, pData, pbi, wUsage, dwROP); } END_TRY // Free any memory we allocated. delete [] pData; if (wPaletteSize != 0) { // Free the bitmap info we allocated. delete [] (BYTE*)pbi; } }
/**************************************************************************** * * FUNCTION: MakeNewANDMaskBasedOnPoint * * PURPOSE: Creates a new AND mask for the icon image * * PARAMS: LPICONIMAGE lpIcon - pointer to icon image data * POINT pt - coords of transparent pixel * * RETURNS: BOOL - TRUE for success, FALSE for failure * * COMMENTS: Creates the AND mask using the color of the pixel at pt * as a transparent color. The XOR mask is changed as well. * This is because the OS expects the XOR mask to have the * AND mask already applied (ie black in transparent areas) * * History: * July '95 - Created * \****************************************************************************/ BOOL MakeNewANDMaskBasedOnPoint( LPICONIMAGE lpIcon, POINT pt ) { HBITMAP hXORBitmap, hOldXORBitmap; HDC hDC, hMemDC1; LPBYTE pXORBits; COLORREF crTransparentColor; LONG i,j; // Account for height*2 thing lpIcon->lpbi->bmiHeader.biHeight /= 2; // Need a DC hDC = GetDC( NULL ); // Use DIBSection for source hXORBitmap = CreateDIBSection( hDC, lpIcon->lpbi, DIB_RGB_COLORS, &pXORBits, NULL, 0 ); memcpy( pXORBits, lpIcon->lpXOR, (lpIcon->lpbi->bmiHeader.biHeight) * BytesPerLine((LPBITMAPINFOHEADER)(lpIcon->lpbi)) ); hMemDC1 = CreateCompatibleDC( hDC ); hOldXORBitmap = SelectObject( hMemDC1, hXORBitmap ); // Set the color table if need be if( lpIcon->lpbi->bmiHeader.biBitCount <= 8 ) SetDIBColorTable( hMemDC1, 0, DIBNumColors((LPSTR)(lpIcon->lpbi)), lpIcon->lpbi->bmiColors); // What's the transparent color? crTransparentColor = GetPixel( hMemDC1, pt.x, pt.y ); // Loop through the pixels for(i=0;i<lpIcon->lpbi->bmiHeader.biWidth;i++) { for(j=0;j<lpIcon->lpbi->bmiHeader.biHeight;j++) { // Is the source transparent at this point? if( GetPixel( hMemDC1, i, j ) == crTransparentColor ) { // Yes, so set the pixel in AND mask, and clear it in XOR mask SetMonoDIBPixel( lpIcon->lpAND, lpIcon->lpbi->bmiHeader.biWidth, lpIcon->lpbi->bmiHeader.biHeight, i, j, TRUE ); if( lpIcon->lpbi->bmiHeader.biBitCount == 1 ) SetMonoDIBPixel( pXORBits, lpIcon->lpbi->bmiHeader.biWidth, lpIcon->lpbi->bmiHeader.biHeight, i, j, FALSE ); else SetPixelV( hMemDC1, i, j, RGB(0,0,0) ); } else { // No, so clear pixel in AND mask SetMonoDIBPixel( lpIcon->lpAND, lpIcon->lpbi->bmiHeader.biWidth, lpIcon->lpbi->bmiHeader.biHeight, i, j, FALSE ); } } } // Flush the SetPixelV() calls GdiFlush(); SelectObject( hMemDC1, hOldXORBitmap ); // Copy the new XOR bits back to our storage memcpy( lpIcon->lpXOR, pXORBits, (lpIcon->lpbi->bmiHeader.biHeight) * BytesPerLine((LPBITMAPINFOHEADER)(lpIcon->lpbi)) ); // Clean up DeleteObject( hXORBitmap ); DeleteDC( hMemDC1 ); ReleaseDC( NULL, hDC ); // UnAccount for height*2 thing lpIcon->lpbi->bmiHeader.biHeight *= 2; return TRUE; }
WORD CIconExtractor::PaletteSize(LPSTR lpbi) { return (DIBNumColors(lpbi) * sizeof(RGBQUAD)); }
WORD CDib::PaletteSize(LPBYTE lpDIB) { return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE)); }
/**************************************************************************** * * FUNCTION: PaletteSize * * PURPOSE: Calculates the number of bytes in the color table. * * PARAMS: LPSTR lpbi - pointer to the CF_DIB memory block * * RETURNS: WORD - number of bytes in the color table * * * History: * July '95 - Copied <g> * \****************************************************************************/ WORD PaletteSize( LPSTR lpbi ) { return ( DIBNumColors( lpbi ) * sizeof( RGBQUAD ) ); }
BOOL WINAPI ReplaceColorPal(LPSTR lpDIB, BYTE * bpColorsTable) { // 循环变量 int i; // 颜色表中的颜色数目 WORD wNumColors; // 指向BITMAPINFO结构的指针(Win3.0) LPBITMAPINFO lpbmi; // 指向BITMAPCOREINFO结构的指针 LPBITMAPCOREINFO lpbmc; // 表明是否是Win3.0 DIB的标记 BOOL bWinStyleDIB; // 创建结果 BOOL bResult = FALSE; // 获取指向BITMAPINFO结构的指针(Win3.0) lpbmi = (LPBITMAPINFO)lpDIB; // 获取指向BITMAPCOREINFO结构的指针 lpbmc = (LPBITMAPCOREINFO)lpDIB; // 获取DIB中颜色表中的颜色数目 wNumColors =DIBNumColors(lpDIB); // 判断颜色数目是否是256色 if (wNumColors == 256) { // 判断是否是WIN3.0的DIB bWinStyleDIB = IS_WIN30_DIB(lpDIB); // 读取伪彩色编码,更新DIB调色板 for (i = 0; i < (int)wNumColors; i++) { if (bWinStyleDIB) { // 更新DIB调色板红色分量 lpbmi->bmiColors[i].rgbRed = bpColorsTable[i * 4]; // 更新DIB调色板绿色分量 lpbmi->bmiColors[i].rgbGreen = bpColorsTable[i * 4 + 1]; // 更新DIB调色板蓝色分量 lpbmi->bmiColors[i].rgbBlue = bpColorsTable[i * 4 + 2]; // 更新DIB调色板保留位 lpbmi->bmiColors[i].rgbReserved = 0; } else { // 更新DIB调色板红色分量 lpbmc->bmciColors[i].rgbtRed = bpColorsTable[i * 4]; // 更新DIB调色板绿色分量 lpbmc->bmciColors[i].rgbtGreen = bpColorsTable[i * 4 + 1]; // 更新DIB调色板蓝色分量 lpbmc->bmciColors[i].rgbtBlue = bpColorsTable[i * 4 + 2]; } } } // 返回 return bResult; }