1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
void __fastcall TForm1::Greylevels1Click(TObject *Sender)
{
jPixel *Pixel;
Byte *Srce, *Dest;
int grey, Start, blue, red, green, c1;
Graphics::TBitmap *B = Image1->Picture->Bitmap;
switch(B->PixelFormat)
{
case pf8bit :
//Couleurs disponibles pour cette image
GetPaletteEntries(B->Palette, 0, 256, Colors);
//Niveaux de gris correspondants
for(int j = 0; j < 256; j++)
{
GreyLevels[j] = ((Colors[j].peRed * 30) +
(Colors[j].peGreen * 59) +
(Colors[j].peBlue * 11)) / 100;
}
//Transfert des indexes de srce à dest
Bitmap->Width = B->Width;
Bitmap->Height = B->Height;
for(int y = 0; y < B->Height; y++)
{
Srce = (Byte*)B->ScanLine[y];
Dest = (Byte*)Bitmap->ScanLine[y];
for(int x = 0; x < B->Width; x++)
{
Dest[x] = (Byte)GreyLevels[ Srce[x] ];
}
}
//On assigne le bitmap à celui de l'image
//Ce qui affecte également la palette de l'image
Image1->Picture->Bitmap->Assign(Bitmap);
break;
case pf24bit :
Start = GetTickCount(); //pour mesurer le temps
for(int y = 0; y < B->Height; y++)
{
Pixel = (jPixel*)B->ScanLine[y];
for(int x = 0; x < B->Width; x++)
{
//Non optimisé :
grey = ((Pixel[x].Red * 30) +
(Pixel[x].Green *59) +
(Pixel[x].Blue *11)) / 100;
Pixel[x].Red = (Byte)grey;
Pixel[x].Green = (Byte)grey;
Pixel[x].Blue = (Byte)grey;
}
}
//Pour faire écho du temps passé
ShowMessage(IntToStr(GetTickCount() - Start));
//Par contre, il faut rafraîchir l'image
Image1->Repaint();
break;
}
} |