• Welcome to Vampyre Imaging Library Forum. Please login or sign up.
 

[Help] Draw a line on TImage object (Linux vs Windows)

Started by mos, 2 September 2008, 19:58:23

Previous topic - Next topic

mos

I'm trying to draw a line on an instance of TImage,  I have wrote this code with Lazarus 0.9.25:


procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Color := clBlack;

  FImage1 := TSingleImage.Create;
  FBitmap1 := TImagingBitmap.Create;
  Image1.Picture.Graphic := FBitmap1;
  FImage1.LoadFromFile('Tigers.jpg');
  Image1.Picture.Graphic.Assign(FImage1);
 
  Image1.Picture.Bitmap.Canvas.Pen.Color := clWhite;
  Image1.Picture.Bitmap.Canvas.Line(0, 0, 200, 200);
end;


where Image TImage type on Form1 of TForm.

This code work ok on Linux (Fedora), I see a white line, but on Windows (WinXP) I see a black line.
If I set "Form1.Color := clRed;" on Windows I see a red line, in Linux is always ok, I see a white line.

In conclusion, in Windows the line color is the same of the Form color, Why?

Can you help me?


Goodbye

Galfar

I've found the reason: If any pixels of the bitmap have alpha>0 then
image is alpha blended on the form. All bitmaps converted
by Imaging from its images have full alpha information.
If you load that jpeg background it has alpha=255 so it is
blended correctly. But if you draw the line on canvas
using one of predefined color constant like clWhite they all
have alpha=0 (clWhite = TColor($FFFFFF)).
So the image is blended and only transparent thing is your line.

If you set the image alpha to 0 with following function it works ok:

procedure SetAlpha(const Image: TImageData; const Alpha: Word);
var
  Data: PByte;
  I: Integer;
begin
  with Image do
    Data := @PColor32Rec(Bits)^.A;
  for I := 0 to Image.Size div 4 - 1 do
  begin
    Data^ := Alpha;
    Inc(Data, 4);
  end;
end;



  Form1.Color := clGreen;
  FImage1 := TMultiImage.Create;
  FBitmap1 := TImagingBitmap.Create;
  Image1.Picture.Graphic := FBitmap1;
  FImage1.LoadFromFile('Tigers.jpg');
  FImage1.Format := ifA8R8G8B8;
  SetAlpha(FImage1.ImageDataPointer^, 0);
  Image1.Picture.Graphic.Assign(FImage1);

  Image1.Picture.Bitmap.Canvas.Pen.Color := clWhite;
  Image1.Picture.Bitmap.Canvas.Line(1, 1, 200, 200);     


Why it works this way or how to use canvas to draw on image with alpha
are questions for Lazarus forums, try there.

mos

Thank you very much, your solution work great under Linux and under Windows  :)


Bye

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Name:
Email:

Shortcuts: ALT+S save/post or ALT+P preview

SMF spam blocked by CleanTalk