// 範例程式: 讀取 GIF 影像中的調色盤資料 // 2009/12/17 by Yeuan-Kuen Lee // yeuankuenlee@gmail.com // 全域變數宣告 typedef struct { Byte bBlue; Byte bGreen; Byte bRed; } COLOR_TYPE; COLOR_TYPE crColor[1]; COLOR_TYPE crPalette[256]; COLOR_TYPE crSortedPalette[256]; float fLuminance[256]; void __fastcall TForm1::ReadGIFPalette1Click(TObject *Sender) { FILE *imGIF; BYTE bM, bCR, bPixel; int iCornerX,iCornerY; pcImageMode->ActivePage = tsPalette; OpenDialog1->FilterIndex = 2; OpenDialog1->Execute(); lblGIFFileName->Caption = "File Name: "+OpenDialog1->FileName; imPalette->Canvas->Brush->Color = clWhite; imPalette->Canvas->FillRect(Rect(0,0,323,323)); for (i=0;i<256;i++) { crPalette[i].bRed=0; crPalette[i].bGreen=0; crPalette[i].bBlue=0; crSortedPalette[i].bRed = 0; crSortedPalette[i].bGreen = 0; crSortedPalette[i].bBlue = 0; fLuminance[i]=0; } imGIF = fopen(OpenDialog1->FileName.c_str(), "rb"); // Read Header.. fseek(imGIF,0L,SEEK_SET); fread(GIFHeader,13,1,imGIF); lblVersion->Caption = "Version: " + AnsiString(GIFHeader[0].cVersion[0])+AnsiString(GIFHeader[0].cVersion[1])+AnsiString(GIFHeader[0].cVersion[2])+AnsiString(GIFHeader[0].cVersion[3])+AnsiString(GIFHeader[0].cVersion[4])+AnsiString(GIFHeader[0].cVersion[5]); iImageWidth = GIFHeader[0].bScreenWidth[0]+ GIFHeader[0].bScreenWidth[1] * 256; iImageHeight = GIFHeader[0].bScreenHeight[0]+ GIFHeader[0].bScreenHeight[1] * 256; lblScreenWidth->Caption = "Screen Width : " + AnsiString(iImageWidth); lblScreenHeight->Caption = "Screen Height : " + AnsiString(iImageHeight); bM = GIFHeader[0].bMCrPixel >> 7; lblM->Caption = "M: " + AnsiString(bM); bCR = GIFHeader[0].bMCrPixel << 1; bCR = bCR >> 5; bCR = bCR + 1; lblCR->Caption = "CR: " + AnsiString(bCR); bPixel = GIFHeader[0].bMCrPixel << 5; bPixel = bPixel >> 5; bPixel = bPixel + 1; lblPixel->Caption = "Pixel: " + AnsiString(bPixel); lblBackgroundColor->Caption= "Background Color Index: "+AnsiString(GIFHeader[0].bBackgroundColorIndex); iTotalColor = 1; for (i=0;iCaption = "Total Color: "+AnsiString(iTotalColor); iRowCount = iTotalColor / 16; if ((iTotalColor % 16)!= 0) iRowCount++; // 開始讀取調色盤資料 if ( bM == 1) { fread(crPalette,3,iTotalColor,imGIF); for (i=0;iCanvas->Brush->Color =(TColor)RGB(crPalette[k].bRed,crPalette[k].bGreen,crPalette[k].bBlue); fLuminance[k] = 0.299*crPalette[k].bRed + 0.587*crPalette[k].bGreen + 0.114 * crPalette[k].bBlue; crSortedPalette[k].bRed = crPalette[k].bRed; crSortedPalette[k].bGreen = crPalette[k].bGreen; crSortedPalette[k].bBlue = crPalette[k].bBlue; iCornerX = i * 20 + 4; iCornerY = j * 20 + 4; imPalette->Canvas->FillRect(Rect(iCornerY,iCornerX,iCornerY+16,iCornerX+16)); } } } }