From 1a84ef8f8a6dd7788ebf4887f4e0e6d98dff8b9b Mon Sep 17 00:00:00 2001 From: marc Date: Wed, 11 Apr 2007 23:54:17 +0000 Subject: [PATCH] * Added dataowner to LazIntfImage * fixes imagelist code git-svn-id: trunk@10938 - --- lcl/controls.pp | 6 ++--- lcl/imglist.pp | 4 ++-- lcl/include/imglist.inc | 25 ++++++++++++--------- lcl/intfgraphics.pas | 50 +++++++++++++++++++++++++++-------------- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/lcl/controls.pp b/lcl/controls.pp index 377422cfe4..7e630a45ec 100644 --- a/lcl/controls.pp +++ b/lcl/controls.pp @@ -1829,11 +1829,11 @@ type TImageList = class(TDragImageList) published - property BkColor: TColor; + property BkColor; Property Height; property Masked; - Property Width; - Property OnChange; + property Width; + property OnChange; end; diff --git a/lcl/imglist.pp b/lcl/imglist.pp index 7cda322a4d..532902aad8 100644 --- a/lcl/imglist.pp +++ b/lcl/imglist.pp @@ -147,7 +147,7 @@ type {$ifndef IMGLIST_OLDSTYLE} procedure InternalMove(ACurIndex, ANewIndex: Cardinal; AIgnoreCurrent: Boolean); - function InternalSetImage(AIndex: Integer; AImage: TRawImage): PRGBAQuad; + function InternalSetImage(AIndex: Integer; AImage: TRawImage; AFreeImage: Boolean): PRGBAQuad; {$endif} procedure NotifyChangeLink; @@ -163,7 +163,7 @@ type {$endif} protected procedure CheckIndex(AIndex: Integer; AForInsert: Boolean = False); - procedure FillDescription(var ADesc: TRawImageDescription); + procedure FillDescription(out ADesc: TRawImageDescription); procedure GetImages(Index: Integer; const Image, Mask: TBitmap); procedure Initialize; virtual; procedure DefineProperties(Filer: TFiler); override; diff --git a/lcl/include/imglist.inc b/lcl/include/imglist.inc index c25663447b..4cfbd82da3 100644 --- a/lcl/include/imglist.inc +++ b/lcl/include/imglist.inc @@ -579,7 +579,7 @@ end; Fills the description with the default info of the imagedata ------------------------------------------------------------------------------} -procedure TCustomImageList.FillDescription(var ADesc: TRawImageDescription); +procedure TCustomImageList.FillDescription(out ADesc: TRawImageDescription); begin ADesc.Format := ricfRGBA; ADesc.HasPalette := False; @@ -592,11 +592,11 @@ begin ADesc.BitsPerPixel := 32; ADesc.LineEnd := rileDWordBoundary; ADesc.RedPrec := 8; // red precision. bits for red - ADesc.RedShift := 24; + ADesc.RedShift := 8; ADesc.GreenPrec := 8; ADesc.GreenShift := 16; ADesc.BluePrec := 8; - ADesc.BlueShift := 8; + ADesc.BlueShift := 24; ADesc.AlphaPrec := 8; ADesc.AlphaShift := 0; ADesc.AlphaSeparate := False; @@ -628,7 +628,7 @@ begin RawImg.DataSize := FWidth * FHeight * SizeOF(FData[0]); RawImg.Data := @FData[Index * FWidth * FHeight]; - IntfImg := TLazIntfImage.Create(RawImg); + IntfImg := TLazIntfImage.Create(RawImg, False); Image.LoadFromIntfImage(IntfImg); IntfImg.Free; {$endif} @@ -807,11 +807,15 @@ begin then InternalMove(FCount - 1, AIndex, True); if AMask = nil - then msk := 0 + then begin + if AImage.MaskHandleAllocated + then msk := AImage.MaskHandle + else msk := 0; + end else msk := AMask.Handle; R := Rect(0, 0, FWidth, FHeight); GetRawImageFromBitmap(AImage.Handle, msk, R, RawImg); - ImgData := InternalSetImage(AIndex, RawImg); + ImgData := InternalSetImage(AIndex, RawImg, True); if HandleAllocated then TWSCustomImageListClass(WidgetSetClass).Insert(Self, AIndex, ImgData); @@ -913,24 +917,25 @@ end; Method: TCustomImageList.InternalSetImage Params: AIndex: the index of the location where the image should be set AImage: the new image + AFreeImage: if set, the rawimagedata is freed Returns: Pointer to the updated image data Copies the imagedata into the FData array ------------------------------------------------------------------------------} {$ifndef IMGLIST_OLDSTYLE} -function TCustomImageList.InternalSetImage(AIndex: Integer; AImage: TRawImage): PRGBAQuad; +function TCustomImageList.InternalSetImage(AIndex: Integer; AImage: TRawImage; AFreeImage: Boolean): PRGBAQuad; var RawImg: TRawImage; SrcImg, DstImg: TLazIntfImage; begin - SrcImg := TLazIntfImage.Create(AImage); + SrcImg := TLazIntfImage.Create(AImage, AFreeImage); FillChar(RawImg, SizeOf(RawImg), 0); FillDescription(RawImg.Description); Result := @FData[AIndex * FWidth * FHeight]; RawImg.DataSize := FWidth * FHeight * SizeOF(FData[0]); RawImg.Data := PByte(Result); - DstImg := TLazIntfImage.Create(RawImg); + DstImg := TLazIntfImage.Create(RawImg, False); DstImg.CopyPixels(SrcImg); DstImg.Free; @@ -1327,7 +1332,7 @@ begin else msk := AMask.Handle; R := Rect(0, 0, FWidth, FHeight); GetRawImageFromBitmap(AImage.Handle, AMask.Handle, R, RawImage); - ImgData := InternalSetImage(AIndex, RawImage); + ImgData := InternalSetImage(AIndex, RawImage, True); if HandleAllocated then TWSCustomImageListClass(WidgetSetClass).Replace(Self, AIndex, ImgData); {$endif} diff --git a/lcl/intfgraphics.pas b/lcl/intfgraphics.pas index 06b2c02519..9dfb9a4c73 100644 --- a/lcl/intfgraphics.pas +++ b/lcl/intfgraphics.pas @@ -129,6 +129,7 @@ type FWriteRawImageBits: TOnWriteRawImageBits; FAlphaReadRawImageBits: TOnReadRawImageBits; FAlphaWriteRawImageBits: TOnWriteRawImageBits; + FDataOwner: Boolean; function GetTColors(x, y: integer): TGraphicsColor; procedure SetAutoCreateMask(const AValue: boolean); procedure SetTColors(x, y: integer; const AValue: TGraphicsColor); @@ -173,7 +174,7 @@ type procedure SetColor_BPP32_R8G8B8_A1_BIO_TTB_RBO(x, y: integer; const Value: TFPColor); public constructor Create(AWidth, AHeight: integer); override; - constructor Create(ARawImage: TRawImage); + constructor Create(ARawImage: TRawImage; ADataOwner: Boolean); destructor Destroy; override; procedure BeginUpdate; procedure EndUpdate; @@ -188,7 +189,7 @@ type procedure LoadFromBitmap(Bitmap, MaskBitmap: HBitmap; AWidth: integer = -1; AHeight: integer = -1); virtual; procedure CreateBitmap(var Bitmap, MaskBitmap: HBitmap; AlwaysCreateMask: boolean); virtual; - procedure SetRawImage(const RawImage: TRawImage); virtual; + procedure SetRawImage(const RawImage: TRawImage; ADataOwner: Boolean = True); virtual; procedure GetRawImage(out RawImage: TRawImage); virtual; procedure FillPixels(const Color: TFPColor); virtual; procedure CopyPixels(Src: TFPCustomImage); virtual; @@ -404,7 +405,8 @@ function dbgs(const FPColor: TFPColor): string; overload; implementation -uses Graphics; +uses + Graphics; var IsSpaceChar, IsNumberChar, IsHexNumberChar: array[char] of Boolean; @@ -1559,16 +1561,18 @@ end; procedure TLazIntfImage.FreePixelData; begin - ReallocMem(FPixelData,0); - FPixelDataSize:=0; - ReallocMem(FLineStarts,0); + if FDataOwner then + ReallocMem(FPixelData, 0); + FPixelDataSize := 0; + ReallocMem(FLineStarts, 0); end; procedure TLazIntfImage.FreeMaskData; begin - ReallocMem(FMaskData,0); - FMaskDataSize:=0; - ReallocMem(FMaskLineStarts,0); + if FDataOwner then + ReallocMem(FMaskData, 0); + FMaskDataSize := 0; + ReallocMem(FMaskLineStarts, 0); end; procedure TLazIntfImage.CreateAllData; @@ -1630,17 +1634,19 @@ end; constructor TLazIntfImage.Create(AWidth, AHeight: integer); begin + FDataOwner := true; FAutoCreateMask:=true; OnGetInternalColor:=@GetColor_NULL; OnSetInternalColor:=@SetColor_NULL; inherited Create(AWidth, AHeight); end; -constructor TLazIntfImage.Create(ARawImage: TRawImage); +constructor TLazIntfImage.Create(ARawImage: TRawImage; ADataOwner: Boolean); begin - Create(FDataDescription.Width, FDataDescription.Height); - FDataDescription := ARawImage.Description; + Create(FDataDescription.Width, FDataDescription.Height); + FDataOwner := ADataOwner; + FPixelData := ARawImage.Data; FPixelDataSize := ARawImage.DataSize; FMaskData := ARawImage.Mask; @@ -1786,7 +1792,7 @@ begin raise FPImageException.Create('Failed to create bitmaps'); end; -procedure TLazIntfImage.SetRawImage(const RawImage: TRawImage); +procedure TLazIntfImage.SetRawImage(const RawImage: TRawImage; ADataOwner: Boolean); var OldRawImage: TRawImage; begin @@ -1800,6 +1806,7 @@ begin FPixelDataSize:=RawImage.DataSize; FMaskData:=RawImage.Mask; FMaskDataSize:=RawImage.MaskSize; + FDataOwner := ADataOwner; SetSize(FDataDescription.Width,FDataDescription.Height); fCreateAllDataNeeded:=false; CreateRawImageLineStarts(Width,Height,FDataDescription.BitsPerPixel, @@ -1979,12 +1986,13 @@ end; procedure TLazIntfImage.CopyPixels(Src: TFPCustomImage); var - y: Integer; - x: Integer; + x, y, xStop, yStop: Integer; SrcImg: TLazIntfImage; begin +{ if (Src.Width<>Width) or (Src.Height<>Height) then SetSize(Src.Width,Src.Height); +} if Src is TLazIntfImage then begin SrcImg:=TLazIntfImage(Src); if CompareMem(@FDataDescription,@SrcImg.FDataDescription, @@ -2000,8 +2008,16 @@ begin end; // copy pixels - for y:=0 to Height-1 do - for x:=0 to Width-1 do + xStop := SrcImg.FDataDescription.Width; + if Width < xStop + then xStop := Width; + yStop := SrcImg.FDataDescription.Height; + if Height < yStop + then yStop := Height; + Dec(xStop); + Dec(yStop); + for y:=0 to yStop do + for x:=0 to xStop do Colors[x,y]:=Src.Colors[x,y]; end;