From 2b43ff98cb57d373ec20efd9c83bef7996b2be31 Mon Sep 17 00:00:00 2001 From: tomb0 Date: Sat, 14 Jun 2008 11:26:36 +0000 Subject: [PATCH] - added CreateFromData constructor git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@482 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/rgbgraphics/rgbtypes.pas | 38 ++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/components/rgbgraphics/rgbtypes.pas b/components/rgbgraphics/rgbtypes.pas index abf05e06f..082991367 100644 --- a/components/rgbgraphics/rgbtypes.pas +++ b/components/rgbgraphics/rgbtypes.pas @@ -73,10 +73,12 @@ type FWidth: Integer; FHeight: Integer; FRowPixelStride: Integer; + FDataOwner: Boolean; function GetSize: Integer; public constructor Create(AWidth, AHeight: Integer; ASizeOfPixel: Integer); virtual; constructor CreateAsCopy(ABitmap: TRGBBitmapCore; ASizeOfPixel: Integer); virtual; + constructor CreateFromData(AData: Pointer; AWidth, AHeight: Integer; ASizeOfPixel: Integer; ADataOwner: Boolean = False); virtual; destructor Destroy; override; procedure Assign(Source: TPersistent); override; @@ -95,6 +97,7 @@ type procedure Rotate180; virtual; procedure Rotate270; virtual; public + property DataOwner: Boolean read FDataOwner; property Width: Integer read FWidth; property Height: Integer read FHeight; property Pixels: PRGBPixel read FPixels; @@ -109,6 +112,7 @@ type public constructor Create(AWidth, AHeight: Integer); virtual; constructor CreateAsCopy(ABitmap: TRGBBitmapCore); virtual; + constructor CreateFromData(AData: Pointer; AWidth, AHeight: Integer; ADataOwner: Boolean = False); virtual; procedure LoadFromLazIntfImageAlpha(AImage: TLazIntfImage); virtual; procedure SaveToLazIntfImageAlpha(AImage: TLazIntfImage); virtual; @@ -132,6 +136,7 @@ type constructor Create(AWidth, AHeight: Integer); virtual; constructor CreateAsCopy(ABitmap: TRGBBitmapCore); virtual; constructor CreateFromLazIntfImage(AImage: TLazIntfImage); virtual; + constructor CreateFromData(AData: Pointer; AWidth, AHeight: Integer; ADataOwner: Boolean = False); virtual; procedure Assign(Source: TPersistent); override; procedure SwapWith(ABitmap: TRGBBitmapCore); override; @@ -509,6 +514,7 @@ begin FRowPixelStride := (((AWidth * ASizeOfPixel + 3) shr 2) shl 2) div ASizeOfPixel; FSizeOfPixel := ASizeOfPixel; + FDataOwner := True; GetMem(FPixels, FHeight * FRowPixelStride * FSizeOfPixel); end; @@ -521,14 +527,32 @@ begin FHeight := ABitmap.Height; FRowPixelStride := ABitmap.RowPixelStride; FSizeOfPixel := ASizeOfPixel; + + FDataOwner := True; GetMem(FPixels, FHeight * FRowPixelStride * SizeOfPixel); Move(ABitmap.Pixels^, FPixels^, FHeight * FRowPixelStride * SizeOfPixel); end; +constructor TRGBBitmapCore.CreateFromData(AData: Pointer; AWidth, AHeight: Integer; + ASizeOfPixel: Integer; ADataOwner: Boolean); +begin + inherited Create; + + FWidth := AWidth; + FHeight := AHeight; + // TODO: check on 64-bit arch. + // 32-bit alignment + FRowPixelStride := (((AWidth * ASizeOfPixel + 3) shr 2) shl 2) div ASizeOfPixel; + FSizeOfPixel := ASizeOfPixel; + FPixels := AData; + + FDataOwner := ADataOwner; +end; + destructor TRGBBitmapCore.Destroy; begin - FreeMem(FPixels); + if FDataOwner then FreeMem(FPixels); inherited; end; @@ -651,6 +675,12 @@ begin end; end; +constructor TRGB32BitmapCore.CreateFromData(AData: Pointer; AWidth, AHeight: Integer; + ADataOwner: Boolean); +begin + inherited CreateFromData(AData, AWidth, AHeight, SizeOf(TRGB32Pixel), ADataOwner); +end; + procedure TRGB32BitmapCore.Assign(Source: TPersistent); begin if (Source is TRGBBitmapCore) and ((Source as TRGBBitmapCore).SizeOfPixel = SizeOf(TRGB32Pixel)) then @@ -736,6 +766,12 @@ begin inherited CreateAsCopy(ABitmap, SizeOf(TRGB8Pixel)); end; +constructor TRGB8BitmapCore.CreateFromData(AData: Pointer; AWidth, AHeight: Integer; + ADataOwner: Boolean); +begin + inherited CreateFromData(AData, AWidth, AHeight, SizeOf(TRGB8Pixel), ADataOwner); +end; + procedure TRGB8BitmapCore.LoadFromLazIntfImageAlpha(AImage: TLazIntfImage); var I, J: Integer;