//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #define __MAIN_PROGRAM__ #include "ImageKit.h" #include "ImgKDef.h" //--------------------------------------------------------------------------- #pragma link "IMGKITLib_TLB" #pragma link "ImgSldLib_TLB" #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- #define ALIGNLONG(a) ((a) == ((a)/32*32) ? (((a)/32)*4) : (((a)/32+1)*4)) void __fastcall TForm1::Button2Click(TObject *Sender) { if (!OpenDialog1->Execute()) return; ImgKitFile1->FileName = OpenDialog1->FileName; ImgKitFile1->Action = 0; ImgKitFile1->Execute(); Graphics::TBitmap *bm = new Graphics::TBitmap(); LPBITMAPINFOHEADER lpBIH; lpBIH = (LPBITMAPINFOHEADER)GlobalLock((void *)ImgKitFile1->Handle); int ColorTableSize = DibNumColors(lpBIH) * sizeof(RGBQUAD); bm->Width = lpBIH->biWidth; bm->Height = lpBIH->biHeight; switch (lpBIH->biBitCount) { case 1: bm->PixelFormat = pf1bit; break; case 4: bm->PixelFormat = pf4bit; break; case 8: bm->PixelFormat = pf8bit; break; case 24: bm->PixelFormat = pf24bit; break; } HPALETTE hPal = CreateBIPalette(lpBIH); if (hPal != NULL) bm->Palette = hPal; size_t WidthByte = ALIGNLONG(lpBIH->biWidth * lpBIH->biBitCount); char *lpBits = (char *)lpBIH + lpBIH->biSize + ColorTableSize + lpBIH->biSizeImage - WidthByte; for (long y = 0; y < bm->Height; y++) { memcpy(bm->ScanLine[y], lpBits, WidthByte); lpBits -= WidthByte; } Image1->Picture->Bitmap->Assign(bm); if (hPal != NULL) DeleteObject(hPal); delete bm; Image1->Refresh(); } //--------------------------------------------------------------------------- /******************************************************************** * ビットマップデータ(DIB)から論理パレットを作成 * ********************************************************************/ #define PALVERSION 0x300 #define MAXPALETTE 256 // max. # supported palette entries HPALETTE TForm1::CreateBIPalette(LPBITMAPINFOHEADER lpbi) { HANDLE hPal; HPALETTE hPalette = NULL; WORD nNumColors; WORD i; LPLOGPALETTE pPal; // 論理パレット LPRGBQUAD pRgb; BYTE Red; BYTE Green; BYTE Blue; if (!lpbi) return NULL; if (lpbi->biSize != sizeof(BITMAPINFOHEADER)) return NULL; // カラーデータへのポインタ(|-BITMAPINFOHEADER-|-カラーデータ-|-ピクセルデータ-|) pRgb = (LPRGBQUAD)((LPSTR)lpbi + (WORD)lpbi->biSize); nNumColors = DibNumColors(lpbi); if (nNumColors) { // Allocate for the logical palette structure hPal = GlobalAlloc(GHND, sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY)); if (!hPal) return NULL; pPal = (LPLOGPALETTE)GlobalLock(hPal); if (!pPal) { GlobalFree(hPal); return NULL; } pPal->palNumEntries = nNumColors; pPal->palVersion = PALVERSION; // 論理パレットの palette entries に DIB のカラーデータをセット for (i = 0; i < nNumColors; i++) { pPal->palPalEntry[i].peRed = pRgb[i].rgbRed; pPal->palPalEntry[i].peGreen = pRgb[i].rgbGreen; pPal->palPalEntry[i].peBlue = pRgb[i].rgbBlue; pPal->palPalEntry[i].peFlags = (BYTE)0; } hPalette = CreatePalette(pPal); GlobalUnlock(hPal); GlobalFree(hPal); } else if (lpbi->biBitCount == 24) { // A 24 bitcount DIB has no color table entries so, set // the number of to the maximum value (256). nNumColors = MAXPALETTE; hPal = GlobalAlloc(GHND, sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY)); if (!hPal) return NULL; pPal = (LPLOGPALETTE)GlobalLock(hPal); if (!pPal) { GlobalFree(hPal); return NULL; } pPal->palNumEntries = nNumColors; pPal->palVersion = PALVERSION; Red = Green = Blue = 0; // Generate 256 (= 8*8*4) RGB combinations to fill // the palette entries. for (i = 0; i < pPal->palNumEntries; i++) { pPal->palPalEntry[i].peRed = Red; pPal->palPalEntry[i].peGreen = Green; pPal->palPalEntry[i].peBlue = Blue; pPal->palPalEntry[i].peFlags = (BYTE)0; if (!(Red += 32)) if (!(Green += 32)) Blue += 64; } hPalette = CreatePalette(pPal); GlobalUnlock(hPal); GlobalFree(hPal); } return hPalette; } /********************************************************************** * 色数の取得 * **********************************************************************/ int TForm1::DibNumColors(LPVOID pv) { int Bits; LPBITMAPINFOHEADER lpbi; LPBITMAPCOREHEADER lpbc; lpbi = (LPBITMAPINFOHEADER)pv; lpbc = (LPBITMAPCOREHEADER)pv; if (lpbi->biSize != sizeof(BITMAPCOREHEADER)) { if (lpbi->biClrUsed != 0) return (WORD)lpbi->biClrUsed; Bits = lpbi->biBitCount; } else Bits = lpbc->bcBitCount; switch (Bits) { case 1: return 2; case 4: return 16; case 8: return 256; default: return 0; // 24ビットのDIBはカラーテーブルなし } }