//Advanced Delphi Systems Code: ads_BitmapCombine
unit ads_BitmapCombine;
{Copyright(c)2016 Advanced Delphi Systems

 Richard Maley
 Advanced Delphi Systems
 12613 Maidens Bower Drive
 Potomac, MD 20854 USA
 phone 301-840-1554
 dickmaley@advdelphisys.com

 The code herein can be used or modified by anyone.  Please retain references
 to Richard Maley at Advanced Delphi Systems.  If you make improvements to the
 code please send your improvements to dickmaley@advdelphisys.com so that the
 entire Delphi community can benefit.  All comments are welcome.
}
(*
UnitIndex Master Index Implementation Section Download Units
Description: ads_BitmapCombine.pas
This unit contains the following routines.

BitmapCombine   BitmapCombineHoriz_1  BitmapCombineHoriz_2  BitmapCombineHoriz_3  BitmapCombineHoriz_4  BitmapCombineVert_1   BitmapCombineVert_2   BitmapCombineVert_3   BitmapCombineVert_4  

*)
interface
Uses
  Windows, Graphics;

//*****************************

Function BitmapCombineHoriz(
  BitmapLeft      : TBitmap;
  BitmapRight     : TBitmap;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;

Function BitmapCombineHoriz(
  BitmapFileLeft  : String;
  BitmapFileRight : String;
  BitmapFileNew   : String
  ): Boolean; OverLoad;

Function BitmapCombineHoriz(
  BitmapFileLeft  : String;
  BitmapFileRight : String;
  BitmapRightLeft : Integer;
  BitmapFileNew   : String
  ): Boolean; OverLoad;

Function BitmapCombineHoriz(
  BitmapLeft      : TBitmap;
  BitmapRight     : TBitmap;
  BitmapRightLeft : Integer;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;

//*****************************

Function BitmapCombineVert(
  BitmapTop        : TBitmap;
  BitmapBottom     : TBitmap;
  BitmapNew        : TBitmap
  ): Boolean; OverLoad;

Function BitmapCombineVert(
  BitmapTop       : TBitmap;
  BitmapBottom    : TBitmap;
  BitmapBottomTop : Integer;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;

Function BitmapCombineVert(
  BitmapFileTop   : String;
  BitmapFileBottom: String;
  BitmapBottomTop : Integer;
  BitmapFileNew   : String
  ): Boolean; OverLoad;

Function BitmapCombineVert(
  BitmapFileTop   : String;
  BitmapFileBottom: String;
  BitmapFileNew   : String
  ): Boolean; OverLoad;

//*****************************

Function BitmapCombine(
  BackgroundColor : TColor;
  BitmapBack      : TBitmap;
  BitmapBackRect  : TRect;
  BitmapFront     : TBitmap;
  BitmapFrontRect : TRect;
  BitmapNewHeight : Integer;
  BitmapNewWidth  : Integer;
  BitmapOut       : TBitmap
  ): Boolean;

implementation


Uses ads_BitmapResize, Types, SysUtils;

//
Unit Description UnitIndex Master Index
Function BitmapCombine(
  BackgroundColor : TColor;
  BitmapBack      : TBitmap;
  BitmapBackRect  : TRect;
  BitmapFront     : TBitmap;
  BitmapFrontRect : TRect;
  BitmapNewHeight : Integer;
  BitmapNewWidth  : Integer;
  BitmapOut       : TBitmap
  ): Boolean;
Var
  BitmapNew       : TBitmap;
  BitmapNewRect   : TRect;
  BitmapBK        : TBitmap;
  BitmapFT        : TBitmap;
  NewHeight       : Integer;
  NewWidth        : Integer;
  boRetValBK      : Boolean;
  boRetValFT      : Boolean;
Begin
  Result          := False;
  If BitmapBack  = nil Then Exit;
  If BitmapFront = nil Then Exit;
  Try
    BitmapNew     := TBitmap.Create();
    BitmapBK      := TBitmap.Create();
    BitmapFT      := TBitmap.Create();
    Try
      boRetValBK  := True;
      BitmapBK.Assign(BitmapBack);
      NewHeight   := BitmapBackRect.Bottom-BitmapBackRect.Top;
      NewWidth    := BitmapBackRect.Right-BitmapBackRect.Left;
      If
      (BitmapBK.Width  <> NewWidth)
      And
      (BitmapBK.Height <> NewHeight)
      Then
      Begin
        boRetValBK :=
          BitmapResize(
            BitmapBK     , //Bitmap       : TBitmap;
            NewHeight    , //NewHeight    : Integer;
            NewWidth     );//NewWidth     : Integer): Boolean; Overload;
      End;
      If Not boRetValBK Then Exit;
      
      boRetValFT  := True;
      BitmapFT.Assign(BitmapFront);
      NewHeight   := BitmapFrontRect.Bottom-BitmapFrontRect.Top;
      NewWidth    := BitmapFrontRect.Right-BitmapFrontRect.Left;
      If
      (BitmapFT.Width  <> NewWidth)
      And
      (BitmapFT.Height <> NewHeight)
      Then
      Begin
        boRetValFT :=
          BitmapResize(
            BitmapFT     , //Bitmap       : TBitmap;
            NewHeight    , //NewHeight    : Integer;
            NewWidth     );//NewWidth     : Integer): Boolean; Overload;
      End;
      If Not boRetValFT Then Exit;
      BitmapNewRect.Right := BitmapNewWidth-1;
      BitmapNewRect.Top   := 0;
      BitmapNewRect.Left  := 0;
      BitmapNewRect.Bottom:= BitmapNewHeight-1;
      BitmapNew.Width  := BitmapNewWidth;
      BitmapNew.Height := BitmapNewHeight;
      BitmapNew.Palette:= BitmapBack.Palette;
      BitmapNew.Canvas.Brush.Color := BackgroundColor;
      BitmapNew.Canvas.FloodFill(0,0,clBlack,fsBorder);
      BitmapNew.
        Canvas.
          Copyrect(
            BitmapBackRect,
            BitmapBK.Canvas,
            Rect(0,
                 0,
                 BitmapBK.Width,
                 BitmapBK.Height));

      BitmapNew.
        Canvas.
          Copyrect(
            BitmapFrontRect,
            BitmapFT.Canvas,
            Rect(0,
                 0,
                 BitmapFT.Width,
                 BitmapFT.Height));
      BitmapOut.Width  := BitmapNew.Width;
      BitmapOut.Height := BitmapNew.Height;
      BitmapOut.Palette:= BitmapNew.Palette;
      BitmapOut.
        Canvas.
          Copyrect(
            Rect(0,
                 0,
                 BitmapNew.Width,
                 BitmapNew.Height),
            BitmapNew.Canvas,
            Rect(0,
                 0,
                 BitmapNew.Width,
                 BitmapNew.Height));
      Result      := True;
    Finally
      BitmapNew   .Free;
      BitmapBK    .Free;
      BitmapFT    .Free;
    End;
  Except
    Result := False;
  End;
End;

//*****************************

//
Unit Description UnitIndex Master Index
Function BitmapCombineHoriz(
  BitmapLeft      : TBitmap;
  BitmapRight     : TBitmap;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;
Var
  BitmapRightLeft  : Integer;
Begin
  BitmapRightLeft  := BitmapLeft.Width+1;
  Result :=
    BitmapCombineHoriz(
      BitmapLeft      , //BitmapLeft      : TBitmap;
      BitmapRight     , //BitmapRight     : TBitmap;
      BitmapRightLeft , //BitmapRightLeft : Integer;
      BitmapNew         //BitmapNew       : TBitmap
                      );//): Boolean; OverLoad;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineHoriz(
  BitmapLeft      : TBitmap;
  BitmapRight     : TBitmap;
  BitmapRightLeft : Integer;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;
Var
  BackgroundColor : TColor;
  BitmapBackRect  : TRect;
  BitmapFrontRect : TRect;
  BitmapNewHeight : Integer;
  BitmapNewWidth  : Integer;
Begin
    BackgroundColor := clWhite;
    BitmapBackRect  := Rect(0,0,BitmapLeft.Width,BitmapLeft.Height);
    BitmapFrontRect := Rect(BitmapRightLeft,0,BitmapRightLeft+BitmapRight.Width,BitmapRight.Height);
    If BitmapLeft.Height >= BitmapRight.Height Then
    Begin
      BitmapNewHeight := BitmapLeft.Height;
    End
    Else
    Begin
      BitmapNewHeight := BitmapRight.Height;
    End;
    BitmapNewWidth  := BitmapRightLeft+BitmapRight.Width;   
    Result :=
      BitmapCombine(
        BackgroundColor , //BackgroundColor : TColor;
        BitmapLeft      , //BitmapBack      : TBitmap;
        BitmapBackRect  , //BitmapBackRect  : TRect;
        BitmapRight     , //BitmapFront     : TBitmap;
        BitmapFrontRect , //BitmapFrontRect : TRect;
        BitmapNewHeight , //BitmapNewHeight : Integer;
        BitmapNewWidth  , //BitmapNewWidth  : Integer;
        BitmapNew         //BitmapOut       : TBitmap
                        );//): Boolean;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineHoriz(
  BitmapFileLeft  : String;
  BitmapFileRight : String;
  BitmapRightLeft : Integer;
  BitmapFileNew   : String
  ): Boolean; OverLoad;
Var
  BitmapLeft      : TBitmap;
  BitmapRight     : TBitmap;
  BitmapNew       : TBitmap;
Begin
  Result          := False;
  If Not FileExists(BitmapFileLeft)  Then Exit;
  If Not FileExists(BitmapFileRight) Then Exit;
  BitmapLeft      := TBitmap.Create();
  BitmapRight     := TBitmap.Create();
  BitmapNew       := TBitmap.Create();
  Try
    BitmapLeft.LoadFromFile(BitmapFileLeft);
    BitmapRight.LoadFromFile(BitmapFileRight);
    Result :=
      BitmapCombineHoriz(
        BitmapLeft      , //BitmapLeft      : TBitmap;
        BitmapRight     , //BitmapRight     : TBitmap;
        BitmapRightLeft , //BitmapRightLeft : Integer;
        BitmapNew         //BitmapNew       : TBitmap
                        );//): Boolean; OverLoad;
    If Result Then
    Begin
      Try
        If FileExists(BitmapFileNew) Then DeleteFile(BitmapFileNew);
        BitmapNew.SaveToFile(BitmapFileNew);
      Except
        Result := False;
      End;
    End;
  Finally
    BitmapLeft    .Free;
    BitmapRight   .Free;
    BitmapNew     .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineHoriz(
  BitmapFileLeft  : String;
  BitmapFileRight : String;
  BitmapFileNew   : String
  ): Boolean; OverLoad;
Var
  BitmapRightLeft : Integer;
  BitmapLeft      : TBitmap;
Begin
  Result          := False;
  If Not FileExists(BitmapFileLeft)  Then Exit;
  If Not FileExists(BitmapFileRight) Then Exit;
  BitmapLeft      := TBitmap.Create();
  Try
    BitmapLeft.LoadFromFile(BitmapFileLeft);
    BitmapRightLeft := BitmapLeft.Width;
  Finally
    BitmapLeft.Free;
  End;
  Result :=
    BitmapCombineHoriz(
      BitmapFileLeft  , //BitmapFileLeft  : String;
      BitmapFileRight , //BitmapFileRight : String;
      BitmapRightLeft , //BitmapRightLeft : Integer;
      BitmapFileNew     //BitmapFileNew   : String
                      );//): Boolean; OverLoad;
End;

//*****************************

//
Unit Description UnitIndex Master Index
Function BitmapCombineVert(
  BitmapTop        : TBitmap;
  BitmapBottom     : TBitmap;
  BitmapNew        : TBitmap
  ): Boolean; OverLoad;
Var
  BitmapBottomTop  : Integer;
Begin
  BitmapBottomTop  := BitmapTop.Height+1;
  Result :=
    BitmapCombineVert(
      BitmapTop       , //BitmapLeft      : TBitmap;
      BitmapBottom    , //BitmapRight     : TBitmap;
      BitmapBottomTop , //BitmapRightLeft : Integer;
      BitmapNew         //BitmapNew       : TBitmap
                      );//): Boolean; OverLoad;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineVert(
  BitmapTop       : TBitmap;
  BitmapBottom    : TBitmap;
  BitmapBottomTop : Integer;
  BitmapNew       : TBitmap
  ): Boolean; OverLoad;
Var
  BackgroundColor : TColor;
  BitmapBackRect  : TRect;
  BitmapFrontRect : TRect;
  BitmapNewHeight : Integer;
  BitmapNewWidth  : Integer;
Begin
    BackgroundColor := clWhite;
    BitmapBackRect  := Rect(0,0,BitmapTop.Width,BitmapTop.Height);
    BitmapFrontRect := Rect(0,BitmapBottomTop,BitmapBottom.Width,BitmapBottomTop+BitmapBottom.Height);
    If BitmapTop.Width >= BitmapBottom.Width Then
    Begin
      BitmapNewWidth := BitmapTop.Width;
    End
    Else
    Begin
      BitmapNewWidth := BitmapBottom.Width;
    End;
    BitmapNewHeight  := BitmapBottomTop+BitmapBottom.Height;
    Result :=
      BitmapCombine(
        BackgroundColor , //BackgroundColor : TColor;
        BitmapTop       , //BitmapBack      : TBitmap;
        BitmapBackRect  , //BitmapBackRect  : TRect;
        BitmapBottom    , //BitmapFront     : TBitmap;
        BitmapFrontRect , //BitmapFrontRect : TRect;
        BitmapNewHeight , //BitmapNewHeight : Integer;
        BitmapNewWidth  , //BitmapNewWidth  : Integer;
        BitmapNew         //BitmapOut       : TBitmap
                        );//): Boolean;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineVert(
  BitmapFileTop   : String;
  BitmapFileBottom: String;
  BitmapBottomTop : Integer;
  BitmapFileNew   : String
  ): Boolean; OverLoad;
Var
  BitmapTop       : TBitmap;
  BitmapBottom    : TBitmap;
  BitmapNew       : TBitmap;
Begin
  Result          := False;
  If Not FileExists(BitmapFileTop)    Then Exit;
  If Not FileExists(BitmapFileBottom) Then Exit;
  BitmapTop       := TBitmap.Create();
  BitmapBottom    := TBitmap.Create();
  BitmapNew       := TBitmap.Create();
  Try
    BitmapTop   .LoadFromFile(BitmapFileTop);
    BitmapBottom.LoadFromFile(BitmapFileBottom);
    Result :=
      BitmapCombineVert(
        BitmapTop       , //BitmapLeft      : TBitmap;
        BitmapBottom    , //BitmapRight     : TBitmap;
        BitmapBottomTop , //BitmapRightLeft : Integer;
        BitmapNew         //BitmapNew       : TBitmap
                        );//): Boolean; OverLoad;
    If Result Then
    Begin
      Try
        If FileExists(BitmapFileNew) Then DeleteFile(BitmapFileNew);
        BitmapNew.SaveToFile(BitmapFileNew);
      Except
        Result := False;
      End;
    End;
  Finally
    BitmapTop     .Free;
    BitmapBottom  .Free;
    BitmapNew     .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapCombineVert(
  BitmapFileTop   : String;
  BitmapFileBottom: String;
  BitmapFileNew   : String
  ): Boolean; OverLoad;
Var
  BitmapBottomTop : Integer;
  BitmapTop       : TBitmap;
Begin
  Result          := False;
  If Not FileExists(BitmapFileTop)    Then Exit;
  If Not FileExists(BitmapFileBottom) Then Exit;
  BitmapTop       := TBitmap.Create();
  Try
    BitmapTop.LoadFromFile(BitmapFileTop);
    BitmapBottomTop := BitmapTop.Height;
  Finally
    BitmapTop.Free;
  End;
  Result :=
    BitmapCombineVert(
      BitmapFileTop   , //BitmapFileLeft  : String;
      BitmapFileBottom, //BitmapFileRight : String;
      BitmapBottomTop , //BitmapRightLeft : Integer;
      BitmapFileNew     //BitmapFileNew   : String
                      );//): Boolean; OverLoad;
End;

end.
//