Solved

Making Bitmap Palette Darker

Posted on 2000-03-25
5
381 Views
Last Modified: 2010-05-18
Hi.
I want to make a bitmap darker.
Lets say R := R div 2, G := G div 2, B := B div 2.

Does anyone have an example of how to get the bitmap palette, change it and put it back.

Thanks in advance.
0
Comment
Question by:TheSwine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2657286
Hi
maybe you find something here at the graphics info and links section:

http://www.efg2.com/lab/library/delphi/Default.htm
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2658211
Hi TheSwine,

I write a procedure to manipulate with colors in TBitmap.
//
procedure MixScanLine( P : PByteArray; Trans,Len : word; R,G,B : byte);
//
where
   P : PByteArray - pointer to scanline of bitmap
   Trans - (1..99) transparency
   R,G,B - (0..255) colors to mixup, if all of color =0, then Bitmap becam darker, 255 - lighter. How lighter or darker depend from Trans parameter.

You may play with parameters to see what happen. The sample of using this procedure is bellow.

-----------------
procedure TForm1.SpeedButton2Click(Sender: TObject);
var P : PByteArray;
    Y : integer;
begin
   for Y:=0 to Image1.Picture.Bitmap.Height-1 do
   begin
      P:=Image1.Picture.Bitmap.ScanLine[Y];
      MixScanLine(P,40,Image1.Picture.Bitmap.Width-1,128,0,128);
   end;
   Image1.Invalidate;
end;
------------------

procedure MixScanLine( P : PByteArray; Trans,Len : word; R,G,B : byte);
begin
   asm
      push esi
      push edi
      push ebx
      push edx

      mov  esi,P
      mov  edi,esi
      mov  bl,100
      mov  cx,Len
      mov  dx,Trans

    @0:
      lodsb
      mul   dl
      div   bl
      add   al,byte ptr B
      stosb

      lodsb
      mul   dl
      div   bl
      add   al,byte ptr G
      stosb

      lodsb
      mul   dl
      div   bl
      add   al,byte ptr R
      stosb


      dec   cx
      jnz   @0

      pop  edx
      pop  ebx
      pop  edi
      pop  esi
   end;
end;


I hope, you like it.

Cheers,
Igor.
0
 
LVL 10

Accepted Solution

by:
Lischke earned 100 total points
ID: 2660043
Hi,

here's how to manipulate the palette (not all the bitmap bits):

procedure OffsetPalette(Bitmap: TBitmap; dR, dG, dB: ShortInt);

var
  Pal: TMaxLogPalette;
  I: Integer;

begin
  GetPaletteEntries(Bitmap.Palette, 0, 256, Pal);
  for I := 0 to 255 do
  begin
    Pal.palPalEntry[I].peRed := Pal.palPalEntry[I].peRed + dR;
    Pal.palPalEntry[I].peGreen := Pal.palPalEntry[I].peGreen + dG;
    Pal.palPalEntry[I].peBlue := Pal.palPalEntry[I].peBlue + dB;
  end;
  Pal.palVersion := $300;
  Pal.palNumEntries := 256;
  Bitmap.Palette := CreatePalette(PLogPalette(@Pal)^);
end;

You need to make sure there's no overflow (< 0 or > 255 when applying the differences).

Ciao, Mike
0
 
LVL 1

Author Comment

by:TheSwine
ID: 2660494
That's works but still there are some problems:
When I load bitmap file to an image component and try this it works, but if I copy the bitmap from other bitmap (Screen e.g.) that doesn't work. when I check the palette value after copying is return 0, is there any way of creating the bitmap i copy to an independable HPalette?

I tried some things but nothing worked so source would be nice.

Here is my source:

procedure TForm2.FormShow(Sender: TObject);
var
  DeskTopDC: HDc;
  DeskTopCanvas: TCanvas;
  DeskTopRect: TRect;
begin

  DeskTopDC := GetWindowDC(GetDeskTopWindow);
  DeskTopCanvas := TCanvas.Create;
  DeskTopCanvas.Handle := DeskTopDC;
  DeskTopRect := Rect(Left, Top, Left+Width, Top + Height);

  Bg.Picture.Bitmap.Width := Width;
  Bg.Picture.Bitmap.Height := Height;
  Bg.Picture.Bitmap.Canvas.CopyRect(Rect(0,0,Width,Height),DeskTopCanvas,DeskTopRect);

  ReleaseDC(GetDeskTopWindow,DeskTopDC);

  Fade;

end;

procedure TForm2.Fade;
var
  Pal: TMaxLogPalette;
  I: Integer;
begin
  if Bg.Picture.Bitmap.Palette = 0 then ShowMessage('Not Good');

  GetPaletteEntries(Bg.Picture.Bitmap.Palette, 0, 256, Pal);
  for I := 0 to 255 do
  begin
    Pal.palPalEntry[I].peRed := Pal.palPalEntry[I].peRed div 3;
    Pal.palPalEntry[I].peGreen := Pal.palPalEntry[I].peGreen div 3;
    Pal.palPalEntry[I].peBlue := Pal.palPalEntry[I].peBlue div 3;
  end;
  Pal.palVersion := $300;
  Pal.palNumEntries := 256;
  Bg.Picture.Bitmap.Palette := CreatePalette(PLogPalette(@Pal)^);
end;

Notes :
*
Form2 is shown whenever I press button on form 1.
*
Bg is TImage component on Form2.
If I load any bitmap into it, The Fade procedure will work.
If it's empty... it's not working.

Thanks in advance.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2660677
You can of course only the palette of images which have actually a palette. Otherwise you need to manipulate the bitmap bits. There is no exception.

You can convert any image to one with palette by decreasing its color depth to at most 8 bit per pixel (set TBitmap.PixelFormat := pf8Bit before you do CopyRect).

Ciao, Mike
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 77
Dynamically Created Query 3 81
Browsing a TTreeView in Delphi 5 49
Error when I try to post by IdHttp, error 500 - Help 2 25
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question