* fixed MemLeak in AssignFromLazIntfImage
[glBitmap.git] / glBitmap.pas
index 04297fa..1e95efa 100644 (file)
@@ -242,7 +242,7 @@ unit glBitmap;
 {.$DEFINE GLB_DELPHI}
 
 // activate to enable the support for TLazIntfImage from Lazarus
-{$DEFINE GLB_LAZARUS}
+{.$DEFINE GLB_LAZARUS}
 
 
 
@@ -254,7 +254,7 @@ unit glBitmap;
 
 // activate to enable Lazarus TPortableNetworkGraphic support
 // if you enable this pngImage and libPNG will be ignored
-{$DEFINE GLB_LAZ_PNG}
+{.$DEFINE GLB_LAZ_PNG}
 
 // activate to enable png support with the unit pngimage -> http://pngdelphi.sourceforge.net/
 // if you enable pngimage the libPNG will be ignored
@@ -268,7 +268,7 @@ unit glBitmap;
 
 // activate to enable Lazarus TJPEGImage support
 // if you enable this delphi jpegs and libJPEG will be ignored
-{$DEFINE GLB_LAZ_JPEG}
+{.$DEFINE GLB_LAZ_JPEG}
 
 // if you enable delphi jpegs the libJPEG will be ignored
 {.$DEFINE GLB_DELPHI_JPEG}
@@ -443,11 +443,12 @@ interface
 uses
   {$IFNDEF GLB_NATIVE_OGL}      dglOpenGL,                          {$ENDIF}
   {$IF DEFINED(GLB_WIN) AND
-       DEFINED(GLB_NATIVE_OGL)} windows,                            {$IFEND}
+       (DEFINED(GLB_NATIVE_OGL) OR
+        DEFINED(GLB_DELPHI))}   windows,                            {$IFEND}
 
   {$IFDEF GLB_SDL}              SDL,                                {$ENDIF}
   {$IFDEF GLB_LAZARUS}          IntfGraphics, GraphType, Graphics,  {$ENDIF}
-  {$IFDEF GLB_DELPHI}           Dialogs, Graphics,                  {$ENDIF}
+  {$IFDEF GLB_DELPHI}           Dialogs, Graphics, Types,           {$ENDIF}
 
   {$IFDEF GLB_SDL_IMAGE}        SDL_image,                          {$ENDIF}
   {$IFDEF GLB_PNGIMAGE}         pngimage,                           {$ENDIF}
@@ -859,6 +860,7 @@ type
    EglBitmapSizeToLarge       = class(EglBitmap);
    EglBitmapNonPowerOfTwo     = class(EglBitmap);
    EglBitmapUnsupportedFormat = class(EglBitmap)
+   public
      constructor Create(const aFormat: TglBitmapFormat); overload;
      constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
    end;
@@ -922,6 +924,7 @@ type
     fTarget: GLuint;
     fAnisotropic: Integer;
     fDeleteTextureOnFree: Boolean;
+    fFreeDataOnDestroy: Boolean;
     fFreeDataAfterGenTexture: Boolean;
     fData: PByte;
     fIsResident: Boolean;
@@ -964,6 +967,7 @@ type
     procedure SetCustomData(const aValue: Pointer);
     procedure SetCustomName(const aValue: String);
     procedure SetCustomNameW(const aValue: WideString);
+    procedure SetFreeDataOnDestroy(const aValue: Boolean);
     procedure SetDeleteTextureOnFree(const aValue: Boolean);
     procedure SetFormat(const aValue: TglBitmapFormat);
     procedure SetFreeDataAfterGenTexture(const aValue: Boolean);
@@ -1002,6 +1006,7 @@ type
     property CustomData:  Pointer    read fCustomData  write SetCustomData;
 
     property DeleteTextureOnFree:     Boolean read fDeleteTextureOnFree     write SetDeleteTextureOnFree;
+    property FreeDataOnDestroy:       Boolean read fFreeDataOnDestroy       write SetFreeDataOnDestroy;
     property FreeDataAfterGenTexture: Boolean read fFreeDataAfterGenTexture write SetFreeDataAfterGenTexture;
 
     property Dimension:  TglBitmapPixelPosition  read fDimension;
@@ -1102,7 +1107,7 @@ type
     constructor Create; overload;
     constructor Create(const aFileName: String); overload;
     constructor Create(const aStream: TStream); overload;
-    constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat); overload;
+    constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
     constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
     constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
     constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
@@ -1224,7 +1229,8 @@ function CreateGrayPalette: HPALETTE;
 implementation
 
 uses
-  Math, syncobjs, typinfo;
+  Math, syncobjs, typinfo
+  {$IFDEF GLB_DELPHI}, Types{$ENDIF};
 
 type
 {$IFNDEF fpc}
@@ -4173,6 +4179,14 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetFreeDataOnDestroy(const aValue: Boolean);
+begin
+  if fFreeDataOnDestroy = aValue then
+    exit;
+  fFreeDataOnDestroy := aValue;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
 begin
   if fDeleteTextureOnFree = aValue then
@@ -4332,7 +4346,6 @@ begin
   fTarget     := 0;
   fIsResident := false;
 
-  fFormat                  := glBitmapGetDefaultFormat;
   fMipMap                  := glBitmapDefaultMipmap;
   fFreeDataAfterGenTexture := glBitmapGetDefaultFreeDataAfterGenTexture;
   fDeleteTextureOnFree     := glBitmapGetDefaultDeleteTextureOnFree;
@@ -4347,8 +4360,10 @@ procedure TglBitmap.BeforeDestruction;
 var
   NewData: PByte;
 begin
-  NewData := nil;
-  SetDataPointer(NewData, tfEmpty); //be careful, Data could be freed by this method
+  if fFreeDataOnDestroy then begin
+    NewData := nil;
+    SetDataPointer(NewData, tfEmpty); //be careful, Data could be freed by this method
+  end;
   if (fID > 0) and fDeleteTextureOnFree then
     glDeleteTextures(1, @fID);
   inherited BeforeDestruction;
@@ -4415,7 +4430,7 @@ begin
       FreeMem(tmpData);
     raise;
   end;
-  AddFunc(Self, aFunc, false, Format, aArgs);
+  AddFunc(Self, aFunc, false, aFormat, aArgs);
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -4920,7 +4935,7 @@ begin
 
   rid.Width        := Width;
   rid.Height       := Height;
-  rid.Depth        := CountSetBits(FormatDesc.Range.r or FormatDesc.Range.g or FormatDesc.Range.b or FormatDesc.Range.a);
+  rid.Depth        := CountSetBits(FormatDesc.RedMask or FormatDesc.GreenMask or FormatDesc.BlueMask or FormatDesc.AlphaMask);
   rid.BitOrder     := riboBitsInOrder;
   rid.ByteOrder    := riboLSBFirst;
   rid.LineOrder    := riloTopToBottom;
@@ -4953,6 +4968,42 @@ var
   FormatDesc: TFormatDescriptor;
   ImageData: PByte;
   ImageSize: Integer;
+  CanCopy: Boolean;
+
+  procedure CopyConvert;
+  var
+    bfFormat: TbmpBitfieldFormat;
+    pSourceLine, pDestLine: PByte;
+    pSourceMD, pDestMD: Pointer;
+    x, y: Integer;
+    pixel: TglBitmapPixelData;
+  begin
+    bfFormat  := TbmpBitfieldFormat.Create;
+    with aImage.DataDescription do begin
+      bfFormat.RedMask   := ((1 shl RedPrec)   - 1) shl RedShift;
+      bfFormat.GreenMask := ((1 shl GreenPrec) - 1) shl GreenShift;
+      bfFormat.BlueMask  := ((1 shl BluePrec)  - 1) shl BlueShift;
+      bfFormat.AlphaMask := ((1 shl AlphaPrec) - 1) shl AlphaShift;
+      bfFormat.PixelSize := BitsPerPixel / 8;
+    end;
+    pSourceMD := bfFormat.CreateMappingData;
+    pDestMD   := FormatDesc.CreateMappingData;
+    try
+      for y := 0 to aImage.Height-1 do begin
+        pSourceLine := aImage.PixelData + y * aImage.DataDescription.BytesPerLine;
+        pDestLine   := ImageData        + y * Round(FormatDesc.PixelSize * aImage.Width);
+        for x := 0 to aImage.Width-1 do begin
+          bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
+          FormatDesc.Map(pixel, pDestLine, pDestMD);
+        end;
+      end;
+    finally
+      FormatDesc.FreeMappingData(pDestMD);
+      bfFormat.FreeMappingData(pSourceMD);
+      bfFormat.Free;
+    end;
+  end;
+
 begin
   result := false;
   if not Assigned(aImage) then
@@ -4971,10 +5022,17 @@ begin
   if (f = tfEmpty) then
     exit;
 
+  CanCopy :=
+    (Round(FormatDesc.PixelSize * 8)     = aImage.DataDescription.Depth) and
+    (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
+
   ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
   ImageData := GetMem(ImageSize);
   try
-    Move(aImage.PixelData^, ImageData^, (aImage.Width * aImage.Height * aImage.DataDescription.BitsPerPixel) shr 3);
+    if CanCopy then
+      Move(aImage.PixelData^, ImageData^, ImageSize)
+    else
+      CopyConvert;
     SetDataPointer(ImageData, f, aImage.Width, aImage.Height); //be careful, Data could be freed by this method
   except
     if Assigned(ImageData) then
@@ -5621,6 +5679,8 @@ begin
   glbReadOpenGLExtensions;
 {$ENDIF}
   inherited Create;
+  fFormat            := glBitmapGetDefaultFormat;
+  fFreeDataOnDestroy := true;
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -5638,27 +5698,30 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat);
+constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte);
 var
-  Image: PByte;
   ImageSize: Integer;
 begin
   Create;
-  ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
-  GetMem(Image, ImageSize);
-  try
-    FillChar(Image^, ImageSize, #$FF);
-    SetDataPointer(Image, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
-  except
-    if Assigned(Image) then
-      FreeMem(Image);
-    raise;
+  if not Assigned(aData) then begin
+    ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
+    GetMem(aData, ImageSize);
+    try
+      FillChar(aData^, ImageSize, #$FF);
+      SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
+    except
+      if Assigned(aData) then
+        FreeMem(aData);
+      raise;
+    end;
+  end else begin
+    SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
+    fFreeDataOnDestroy := false;
   end;
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat;
-  const aFunc: TglBitmapFunction; const aArgs: Pointer);
+constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
 begin
   Create;
   LoadFromFunc(aSize, aFunc, aFormat, aArgs);
@@ -5977,13 +6040,15 @@ procedure TglBitmap.SavePNG(const aStream: TStream);
 var
   png: TPortableNetworkGraphic;
   intf: TLazIntfImage;
+  raw: TRawImage;
 begin
   png  := TPortableNetworkGraphic.Create;
   intf := TLazIntfImage.Create(0, 0);
   try
     if not AssignToLazIntfImage(intf) then
       raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
-    png.LoadFromIntfImage(intf);
+    intf.GetRawImage(raw);
+    png.LoadFromRawImage(raw, false);
     png.SaveToStream(aStream);
   finally
     png.Free;
@@ -6513,13 +6578,15 @@ procedure TglBitmap.SaveJPEG(const aStream: TStream);
 var
   jpeg: TJPEGImage;
   intf: TLazIntfImage;
+  raw: TRawImage;
 begin
   jpeg := TJPEGImage.Create;
   intf := TLazIntfImage.Create(0, 0);
   try
     if not AssignToLazIntfImage(intf) then
       raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
-    jpeg.LoadFromIntfImage(intf);
+    intf.GetRawImage(raw);
+    jpeg.LoadFromRawImage(raw, false);
     jpeg.SaveToStream(aStream);
   finally
     intf.Free;
@@ -8555,8 +8622,15 @@ initialization
 finalization
   TFormatDescriptor.Finalize;
 
+{$IFDEF GLB_NATIVE_OGL}
+  if Assigned(GL_LibHandle) then
+    glbFreeLibrary(GL_LibHandle);
+
 {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
+  if Assigned(GLU_LibHandle) then
+    glbFreeLibrary(GLU_LibHandle);
   FreeAndNil(InitOpenGLCS);
 {$ENDIF}
+{$ENDIF}  
 
 end.