2009年10月21日 星期三

HW2

以下是我當時撰寫的程式碼:

int X,Y,node;

for(X=1;X<801;x++)

for(Y=1;Y<601;y++)

{

node=(GetRValue(Image1->Canvas->Pixels[X][Y]) //Pixels[X][Y]) 位於(XY)座標的像素

+GetGValue(Image1->Canvas->Pixels[X][Y])

+GetBValue(Image1->Canvas->Pixels[X][Y]))/2;

Image2->Canvas->Pixels[X][Y]=RGB(node,node,node);

}

RGB 三原色相加除以3就能弄出灰階。
Gray=(GetRValue(Image1->Canvas->Pixels[X][Y])+GetGValue(Image1->Canvas->Pixels[X][Y])+GetBValue(Image1->Canvas->Pixels[X][Y]))/3;

那時問東問西,終於問到一個同學跟我說到要相加在除3才會成功。

所以回家馬上試試看,結果成功了。我做的圖,就是當前最的開心農場,弄成詼諧了耶^^為了怕失敗,所以再做了一張圖做確認,也成功無誤。


在BCB的範例中,灰階調色盤的建立如下例:

// 定義調色盤結構
typedef struct {
TLogPalette lpal;
TPaletteEntry dummy[256];
} LogPal;

// 建立灰階調色盤
LogPal GrayPalette;
GrayPalette.lpal.palVersion = 0x300;
GrayPalette.lpal.palNumEntries = 256;
for(int Index = 0; Index < pered =" (unsigned" pegreen =" (unsigned" peblue =" (unsigned" peflags =" (unsigned">
建立完調色盤的內容,要產生給TBitmap的調色盤則用

Graphics::TBitmap *NewBMP = new Graphics::TBitmap();
NewBMP->PixelFormat = pf8bit; // 指定為 8bpp 的BMP
NewBMP->Width = width; // 你的影像寬度
NewBMP->Height = height; // 你的影像高度
NewBMP->Palette = CreatePalette(&GrayPalette.lpal); // 建立灰階調色盤

如果是8bits的彩色圖檔,不同索引值對應的顏色資訊就由上面的peRed、peGreen、peBlue來決定。CreatePalette()是Windows API,由調色盤資訊建立調色盤。
如果是讀入一張8bits的圖檔(到TImage1中),可以利用GetPaletteEntries()來取得調色盤中的顏色資訊。用法如下:

PALETTEENTRY pe[256]; // 接收調色盤資訊的陣列

GetPaletteEntries(
Image1->Picture->Bitmap->Palette, // handle of logical color palette
0, // first entry to retrieve
256, // number of entries to retrieve
pe // address of array receiving entries
);


心得:以上資料是我當時在做詼諧時,找到的相關資料,所以說呢...網路真的是很好的朋友,不懂時,可以請教它,不過當你要找時,不是那麼簡單找到的,也是要一篇一篇的閱讀,這篇才是你要的答案。


沒有留言:

張貼留言