//Advanced Delphi Systems Code: ads_BitmapConvert
unit ads_BitmapConvert;
{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_BitmapConvert.pas
This unit contains the following routines.

BitmapToDIB   BitmapToGif_1   BitmapToGif_2   BitmapToGif_3   BitmapToGif_4   BitmapToGif_5   BitmapToJPeg_1  BitmapToJPeg_2  BitmapToJPeg_3  BitmapToJPeg_4  BitmapToJPeg_5  BitmapToJPeg_6  GifToBitmap_1   GifToBitmap_2   GifToBitmap_3   GifToBitmap_4   GifToBitmap_5   GifToJPeg_1   GifToJPeg_2   GifToJPeg_3   GifToJPeg_4   GifToJPeg_5   JPegToBitmap_1  JPegToBitmap_2  JPegToBitmap_3  JPegToBitmap_4  JPegToBitmap_5  JPegToBitmap_6  JpegToGif_1   JpegToGif_2   JpegToGif_3   JpegToGif_4   JpegToGif_5  

*)
interface
Uses
  Windows, Graphics, Jpeg, GifImage, Dialogs;

procedure BitmapToDIB(Bitmap: TBitmap; var DIBHandle: THandle);

Function GifToJPeg(Gif: TGifImage;Jpeg: TJpegImage): Boolean; Overload;
Function GifToJPeg(GifFile,JpegFile: String): Boolean; Overload;
Function GifToJPeg(GifFile,JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Function GifToJPeg(GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Function GifToJPeg(GifFile: String): Boolean; Overload;

Function JpegToGif(Jpeg: TJpegImage;Gif: TGifImage): Boolean; Overload;
Function JpegToGif(JpegFile,GifFile: String): Boolean; Overload;
Function JpegToGif(JpegFile,GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Function JpegToGif(JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Function JpegToGif(JpegFile: String): Boolean; Overload;

Function GifToBitmap(Gif: TGifImage;Bitmap: TBitmap): Boolean; Overload;
Function GifToBitmap(GifFile,BitmapFile: String): Boolean; Overload;
Function GifToBitmap(GifFile,BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Function GifToBitmap(GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Function GifToBitmap(GifFile: String): Boolean; Overload;

Function BitmapToGif(Bitmap: TBitmap;Gif: TGifImage): Boolean; Overload;
Function BitmapToGif(BitmapFile,GifFile: String): Boolean; Overload;
Function BitmapToGif(BitmapFile,GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Function BitmapToGif(BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Function BitmapToGif(BitmapFile: String): Boolean; Overload;

Function JPegToBitmap(Jpeg: TJpegImage;Bitmap: TBitmap): Boolean; Overload;
Function JPegToBitmap(JpegFile: String;Bitmap: TBitmap): Boolean; Overload;
Function JPegToBitmap(JpegFile,BitmapFile: String): Boolean; Overload;
Function JPegToBitmap(JpegFile,BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Function JPegToBitmap(JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Function JPegToBitmap(JpegFile: String): Boolean; Overload;

Function BitmapToJPeg(Bitmap: TBitmap;Jpeg: TJpegImage): Boolean; Overload;
Function BitmapToJPeg(Bitmap: TBitmap;JpegFile: String): Boolean; Overload;
Function BitmapToJPeg(BitmapFile,JpegFile: String): Boolean; Overload;
Function BitmapToJPeg(BitmapFile,JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Function BitmapToJPeg(BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Function BitmapToJPeg(BitmapFile: String): Boolean; Overload;

implementation


Uses
  SysUtils;

//
Unit Description UnitIndex Master Index
procedure BitmapToDIB(Bitmap: TBitmap; var DIBHandle: THandle);
Var
  HeadSize : Cardinal;
  ImageSize: Cardinal;
  pDIBInfo : PBitmapInfo;
  pBits    : Pointer;
begin
  GetDIBSizes(Bitmap.Handle, HeadSize, ImageSize);
  DIBHandle:= GlobalAlloc(GHND or GMEM_DDESHARE, HeadSize + ImageSize);
  Assert(DIBHandle <> 0);
  pDIBInfo := GlobalLock(DIBHandle);
  {$WARNINGS OFF}
  pBits    := Pointer(LongInt(pDIBInfo) + HeadSize);
  {$WARNINGS ON}
  Try
    GetDIB(Bitmap.Handle, Bitmap.Palette, pDIBInfo^, pBits^);
  Except
    GlobalUnlock(DIBHandle);
    GlobalFree(DIBHandle);
    Raise;
  End;
  GlobalUnlock(DIBHandle);
End;

//
Unit Description UnitIndex Master Index
Function JPegToBitmap(Jpeg: TJpegImage;Bitmap: TBitmap): Boolean;
Begin
  Try
    Bitmap.Assign(JPeg);
    Result := True;
  Except
    Result := False;
  End;
End;

//
Unit Description UnitIndex Master Index
Function JPegToBitmap(JpegFile,BitmapFile: String): Boolean; Overload;
Var
  Bitmap : TBitmap;
  Jpeg   : TJpegImage;
Begin
  Result := False;
  If Not FileExists(JpegFile) Then Exit;
  Bitmap := TBitmap.Create();
  Jpeg   := TJpegImage.Create();
  Try
    Try
      Jpeg.LoadFromFile(JpegFile);
      Result := JPegToBitmap(Jpeg,Bitmap);
      If Result Then
      Begin
        If FileExists(BitmapFile) Then DeleteFile(BitmapFile);
        Bitmap.SaveToFile(BitmapFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Bitmap.Free;
    Jpeg  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function JPegToBitmap(JpegFile: String;Bitmap: TBitmap): Boolean; Overload;
Var
  Jpeg   : TJpegImage;
Begin
  Result := False;
  If Not FileExists(JpegFile) Then Exit;
  Jpeg   := TJpegImage.Create();
  Try
    Try
      Jpeg.LoadFromFile(JpegFile);
      Result := JPegToBitmap(Jpeg,Bitmap);
    Except
      Result := False;
    End;
  Finally
    Jpeg  .Free;
  End;
End;


//
Unit Description UnitIndex Master Index
Function JPegToBitmap(JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := JpegFile;
  FileExtNew := '.bmp';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := JPegToBitmap(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function JPegToBitmap(JpegFile: String): Boolean; Overload;
Begin
  Result := JPegToBitmap(JpegFile, True);
End;

//
Unit Description UnitIndex Master Index
Function JPegToBitmap(JpegFile,BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := JPegToBitmap(JpegFile,BitmapFile);
  If DeleteSource Then
  Begin
    If FileExists(JpegFile) Then DeleteFile(JpegFile);
    Result := Not FileExists(JpegFile);
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(BitmapFile,JpegFile: String): Boolean; Overload;
Var
  Bitmap : TBitmap;
  Jpeg   : TJpegImage;
Begin
  Result := False;
  If Not FileExists(BitmapFile) Then Exit;
  Bitmap := TBitmap.Create();
  Jpeg   := TJpegImage.Create();
  Try
    Try
      Bitmap.LoadFromFile(BitmapFile);
      Result := BitmapToJPeg(Bitmap,Jpeg);
      If Result Then
      Begin
        If FileExists(JpegFile) Then DeleteFile(JpegFile);
        Jpeg.SaveToFile(JpegFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Bitmap.Free;
    Jpeg  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(Bitmap: TBitmap;JpegFile: String): Boolean; Overload;
Var
  Jpeg   : TJpegImage;
Begin
  Jpeg   := TJpegImage.Create();
  Try
    Try
      Result := BitmapToJPeg(Bitmap,Jpeg);
      If Result Then
      Begin
        If FileExists(JpegFile) Then DeleteFile(JpegFile);
        Jpeg.SaveToFile(JpegFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Jpeg  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(Bitmap: TBitmap;Jpeg: TJpegImage): Boolean;
Begin
  Try
    If Jpeg.CompressionQuality<>100 Then Jpeg.CompressionQuality:=100;
    Jpeg.Assign(Bitmap);
    Result := True;
  Except
    Result := False;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := BitmapFile;
  FileExtNew := '.jpg';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := BitmapToJpeg(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(BitmapFile: String): Boolean; Overload;
Begin
  Result := BitmapToJPeg(BitmapFile, True);
End;

//
Unit Description UnitIndex Master Index
Function BitmapToJPeg(BitmapFile,JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := BitmapToJPeg(BitmapFile,JpegFile);
  If DeleteSource Then
  Begin
    If FileExists(BitmapFile) Then DeleteFile(BitmapFile);
    Result := Not FileExists(BitmapFile);
  End;
End;

//
Unit Description UnitIndex Master Index
Function  GifToBitmap(Gif: TGifImage;Bitmap: TBitmap): Boolean;
begin
  Result    := False;
  If Gif = nil Then Exit;
  Try
    Bitmap.Assign(Gif.Bitmap);
    Result := True;
  Except
    Result := False;
  End;
end;

//
Unit Description UnitIndex Master Index
Function GifToBitmap(GifFile,BitmapFile: String): Boolean; Overload;
Var
  Bitmap : TBitmap;
  Gif    : TGifImage;
Begin
  Result := False;
  If Not FileExists(GifFile) Then Exit;
  Bitmap := TBitmap.Create();
  Gif    := TGifImage.Create();
  Try
    Try
      Gif.LoadFromFile(GifFile);
      Result := GifToBitmap(Gif,Bitmap);
      If Result Then
      Begin
        If FileExists(BitmapFile) Then DeleteFile(BitmapFile);
        Bitmap.SaveToFile(BitmapFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Bitmap.Free;
    Gif   .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function GifToBitmap(GifFile,BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := GifToBitmap(GifFile,BitmapFile);
  If DeleteSource Then
  Begin
    If FileExists(GifFile) Then DeleteFile(GifFile);
    Result := Not FileExists(GifFile);
  End;
End;

//
Unit Description UnitIndex Master Index
Function GifToBitmap(GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := GifFile;
  FileExtNew := '.bmp';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := GifToBitmap(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function GifToBitmap(GifFile: String): Boolean; Overload;
Begin
  Result := GifToBitmap(GifFile, True);
End;

//
Unit Description UnitIndex Master Index
Function BitmapToGif(BitmapFile,GifFile: String): Boolean; Overload;
Var
  Bitmap : TBitmap;
  Gif   : TGifImage;
Begin
  Result := False;
  If Not FileExists(BitmapFile) Then Exit;
  Bitmap := TBitmap.Create();
  Gif    := TGifImage.Create();
  Try
    Try
      Bitmap.LoadFromFile(BitmapFile);
      Result := BitmapToGif(Bitmap,Gif);
      If Result Then
      Begin
        If FileExists(GifFile) Then DeleteFile(GifFile);
        Gif.SaveToFile(GifFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Bitmap.Free;
    Gif  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToGif(Bitmap: TBitmap;Gif: TGifImage): Boolean;
Begin
  Try
    Gif.Assign(Bitmap);
    Result := True;
  Except
    Result := False;
  End;
End;

//
Unit Description UnitIndex Master Index
Function BitmapToGif(BitmapFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := BitmapFile;
  FileExtNew := '.gif';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := BitmapToGif(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function BitmapToGif(BitmapFile: String): Boolean; Overload;
Begin
  Result := BitmapToGif(BitmapFile, True);
End;

//
Unit Description UnitIndex Master Index
Function BitmapToGif(BitmapFile,GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := BitmapToGif(BitmapFile,GifFile);
  If DeleteSource Then
  Begin
    If FileExists(BitmapFile) Then DeleteFile(BitmapFile);
    Result := Not FileExists(BitmapFile);
  End;
End;

//
Unit Description UnitIndex Master Index
Function GifToJpeg(Gif: TGifImage;Jpeg: TJpegImage): Boolean; Overload;
Var
  Bitmap : TBitmap;
Begin
  Result    := False;
  If Gif = nil Then Exit;
  Try
    Bitmap := TBitmap.Create();
    Try
      Bitmap.Assign(Gif.Bitmap);
      Result := BitmapToJPeg(Bitmap,Jpeg);
    Finally
      Bitmap.Free;
    End;
  Except
    Result := False;
  End;
End;

//
Unit Description UnitIndex Master Index
Function GifToJPeg(GifFile,JpegFile: String): Boolean; Overload;
Var
  Gif    : TGifImage;
  Jpeg   : TJpegImage;
Begin
  Result := False;
  If Not FileExists(GifFile) Then Exit;
  Gif    := TGifImage .Create();
  Jpeg   := TJpegImage.Create();
  Try
    Try
      Gif.LoadFromFile(GifFile);
      Result := GifToJPeg(Gif,Jpeg);
      If Result Then
      Begin
        If FileExists(JpegFile) Then DeleteFile(JpegFile);
        Jpeg.SaveToFile(JpegFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Gif   .Free;
    Jpeg  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function GifToJPeg(GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := GifFile;
  FileExtNew := '.jpg';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := GifToJpeg(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function GifToJPeg(GifFile: String): Boolean; Overload;
Begin
  Result := GifToJPeg(GifFile, True);
End;

//
Unit Description UnitIndex Master Index
Function GifToJPeg(GifFile,JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := GifToJPeg(GifFile,JpegFile);
  If DeleteSource Then
  Begin
    If FileExists(GifFile) Then DeleteFile(GifFile);
    Result := Not FileExists(GifFile);
  End;
End;

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

//
Unit Description UnitIndex Master Index
Function JpegToGif(Jpeg: TJpegImage;Gif: TGifImage): Boolean; Overload;
Var
  Bitmap : TBitmap;
Begin
  Result    := False;
  If Jpeg = nil Then Exit;
  Try
    Bitmap := TBitmap.Create();
    Try
      Bitmap.Assign(Jpeg);
      Result := BitmapToGif(Bitmap,Gif);
    Finally
      Bitmap.Free;
    End;
  Except
    Result := False;
  End;
End;

//
Unit Description UnitIndex Master Index
Function JpegToGif(JpegFile,GifFile: String): Boolean; Overload;
Var
  Jpeg    : TJpegImage;
  Gif   : TGifImage;
Begin
  Result := False;
  If Not FileExists(JpegFile) Then Exit;
  Jpeg    := TJpegImage .Create();
  Gif   := TGifImage.Create();
  Try
    Try
      Jpeg.LoadFromFile(JpegFile);
      Result := JpegToGif(Jpeg,Gif);
      If Result Then
      Begin
        If FileExists(GifFile) Then DeleteFile(GifFile);
        Gif.SaveToFile(GifFile);
      End;
    Except
      Result := False;
    End;
  Finally
    Jpeg   .Free;
    Gif  .Free;
  End;
End;

//
Unit Description UnitIndex Master Index
Function JpegToGif(JpegFile: String; DeleteSource: Boolean): Boolean; Overload;
Var
  FileExtOld : String;
  FileExtNew : String;
  FileNew    : String;
  FileOld    : String;
Begin
  FileOld    := JpegFile;
  FileExtNew := '.jpg';
  Result     := False;
  If Not FileExists(FileOld) Then Exit;
  FileExtOld := ExtractFileExt(FileOld);
  FileNew    := Copy(FileOld,1,Length(FileOld)-Length(FileExtOld))+FileExtNew;
  Result     := JpegToGif(FileOld,FileNew);
  If DeleteSource Then If Result Then If FileExists(FileOld) Then DeleteFile(FileOld);
End;

//
Unit Description UnitIndex Master Index
Function JpegToGif(JpegFile: String): Boolean; Overload;
Begin
  Result := JpegToGif(JpegFile, True);
End;

//
Unit Description UnitIndex Master Index
Function JpegToGif(JpegFile,GifFile: String; DeleteSource: Boolean): Boolean; Overload;
Begin
  Result := JpegToGif(JpegFile,GifFile);
  If DeleteSource Then
  Begin
    If FileExists(JpegFile) Then DeleteFile(JpegFile);
    Result := Not FileExists(JpegFile);
  End;
End;

end.
//