FMX.Float4ToPixel (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use a Float4ToPixel method. Float4ToPixel is used to merge two specified bitmaps, by adding vertical lines from the second bitmap to the first one. The parameters of the next method specify the two bitmaps to be merged, the distance between two consecutive lines, and the height of the lines to be added.

Code

public
    { Public declarations }
    procedure TestFloat4ToPixel(DestBitmap, ScrBitmap: TBitmap;
      Distance, LineHeigth: integer);
procedure TForm1.Button1Click(Sender: TObject);
var
  ImgSource: TBitmap;
  ImgDestination: TBitmap;
begin
  ImgSource := TBitmap.Create;
  ImgDestination := TBitmap.Create;
  try
    ImgSource.LoadFromFile('PhotoSource.jpg');
    ImgDestination.LoadFromFile('PhotoDestination.jpg');
  finally
    TestFloat4ToPixel(ImgDestination, ImgSource, 50, 30);
    ImgDestination.SaveToFile('PhotoResult.jpg');
    ShowMessage('Image Saved');
  end;
  ImgSource.Free;
  ImgDestination.Free;
end;
procedure TForm1.TestFloat4ToPixel(DestBitmap, ScrBitmap: TBitmap;
  Distance, LineHeigth: integer);
var
  bitdata1, bitdata2: TBitmapData;
  I: integer;
  J: integer;
  K: integer;
begin
  if (DestBitmap.Map(TMapAccess.Write, bitdata1) and
    ScrBitmap.Map(TMapAccess.Read, bitdata2)) then
    try
      for I := 0 to DestBitmap.Width - 1 do
        for J := 0 to Round(DestBitmap.Height / Distance) - 1 do
        begin
          for K := 0 to LineHeigth do
            Float4ToPixel
              (TAlphaColorF.Create(PixelToAlphaColor(@PAlphaColorArray
              (bitdata2.Data)[(J * Distance + K) *
              (bitdata1.Pitch div PixelFormatBytes[ScrBitmap.PixelFormat]) + I],
              ScrBitmap.PixelFormat)), @PAlphaColorArray(bitdata1.Data)
              [(J * Distance + K) * (bitdata1.Pitch div PixelFormatBytes
              [DestBitmap.PixelFormat]) + I], DestBitmap.PixelFormat);
        end;
    finally
      DestBitmap.Unmap(bitdata1);
      ScrBitmap.Unmap(bitdata2);
    end;
end;

Result example:

Destination bitmap

Source bitmap

TestFloat4ToPixel(DestBitmap, ScrBitmap,50,20)

Input Bitmap.png

Source Bitmap.png

Float4ToPixel result.png

Uses

See Also