gtk: fix memory leak (internal mask was never freed), cleanup

git-svn-id: trunk@19488 -
This commit is contained in:
paul 2009-04-18 16:16:06 +00:00
parent 78e77af80a
commit c42b65b456
3 changed files with 19 additions and 84 deletions

View File

@ -12,7 +12,7 @@
* * * *
* This file is part of the Lazarus Component Library (LCL) * * This file is part of the Lazarus Component Library (LCL) *
* * * *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, * * See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. * * for details about the copyright. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
@ -5526,81 +5526,6 @@ begin
FreeMem(ImgData); FreeMem(ImgData);
end; end;
{
function GdkPixbufAddBitmapMask
pixbuf - original pixbuf to mask
mask - mask bitmap
mask_value - bit value that should be masked
Returns new pixbuf with applied mask
}
function GdkPixbufAddBitmapMask(pixbuf: PGdkPixbuf; mask: PGdkBitmap; mask_value: byte): PGdkPixbuf;
var
i, j, w, h, r, n: integer;
src, dest: Pguchar;
Image: PGdkImage;
s_buf, d_buf: PCardinal;
begin
Result := nil;
// first check for null images
if (pixbuf = nil) or (mask = nil) then
exit;
n := gdk_pixbuf_get_n_channels(pixbuf);
gdk_drawable_get_size(mask, @w, @h);
// second check. we cannot process pixbuf with channels <> 4
// or with different size that image
if (n <> 4) or (w <> gdk_pixbuf_get_width(pixbuf)) or
(h <> gdk_pixbuf_get_height(pixbuf)) then
exit;
r := gdk_pixbuf_get_rowstride(pixbuf);
// we need Image to read pixels
Image := gdk_drawable_get_image(mask, 0, 0, w, h);
// source pixels of pixbuf
src := gdk_pixbuf_get_pixels(pixbuf);
// allocate destination buffer for new pixbuf
dest := g_malloc(4 * r * h);
// to run on buffer we need one more pointer to it
d_buf := PCardinal(dest);
for i := 0 to h - 1 do
begin
// buffer of source row
s_buf := PCardinal(src + i * r);
for j := 0 to w - 1 do
begin
if gdk_image_get_pixel(Image, j, i) = mask_value then
begin
// if pixel should be masked we fill color with zeros
d_buf^ := 0;
end else
begin
// in other case we copy it from source
d_buf^ := s_buf^;
end;
inc(d_buf);
inc(s_buf);
end;
end;
// image is no more needed, so can be disposed
gdk_image_unref(Image);
// craete result pixbuf from destination buffer
Result := gdk_pixbuf_new_from_data(dest, GDK_COLORSPACE_RGB, true, 8, w, h, r, nil, nil);
// if pixbuf is not created, then destination data should be freed
if Result = nil then
g_free(dest);
end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
method CreatePixbufFromDrawable method CreatePixbufFromDrawable
Params: ASource: The source drawable Params: ASource: The source drawable

View File

@ -13,7 +13,7 @@
* * * *
* This file is part of the Lazarus Component Library (LCL) * * This file is part of the Lazarus Component Library (LCL) *
* * * *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, * * See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. * * for details about the copyright. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
@ -609,8 +609,6 @@ procedure LoadPixbufFromLazResource(const ResourceName: string;
var Pixbuf: PGdkPixbuf); var Pixbuf: PGdkPixbuf);
procedure LoadXPMFromLazResource(const ResourceName: string; procedure LoadXPMFromLazResource(const ResourceName: string;
Window: PGdkWindow; var PixmapImg, PixmapMask: PGdkPixmap); Window: PGdkWindow; var PixmapImg, PixmapMask: PGdkPixmap);
function GdkPixbufAddBitmapMask(pixbuf: PGdkPixbuf; mask:
PGdkBitmap; mask_value: byte): PGdkPixbuf;
function CreatePixbufFromDrawable(ASource: PGdkDrawable; AColorMap:PGdkColormap; AIncludeAplha: Boolean; ASrcX, ASrcY, ADstX, ADstY, AWidth, AHeight :longint): PGdkPixbuf; function CreatePixbufFromDrawable(ASource: PGdkDrawable; AColorMap:PGdkColormap; AIncludeAplha: Boolean; ASrcX, ASrcY, ADstX, ADstY, AWidth, AHeight :longint): PGdkPixbuf;

View File

@ -2482,11 +2482,23 @@ begin
{$IFDEF DebugGDKTraps} {$IFDEF DebugGDKTraps}
BeginGDKErrorTrap; BeginGDKErrorTrap;
{$ENDIF} {$ENDIF}
if GDIBitmapObject <> nil then case GDIBitmapType of
gdk_bitmap_unref(GDIBitmapObject); gbBitmap:
If (Visual <> nil) and (not SystemVisual) then begin
if GDIBitmapObject <> nil then
gdk_bitmap_unref(GDIBitmapObject);
end;
gbPixmap:
begin
if GDIPixmapObject.Image <> nil then
gdk_pixmap_unref(GDIPixmapObject.Image);
if GDIPixmapObject.Mask <> nil then
gdk_bitmap_unref(GDIPixmapObject.Mask);
end;
end;
if (Visual <> nil) and (not SystemVisual) then
gdk_visual_unref(Visual); gdk_visual_unref(Visual);
If Colormap <> nil then if Colormap <> nil then
gdk_colormap_unref(Colormap); gdk_colormap_unref(Colormap);
{$IFDEF DebugGDKTraps} {$IFDEF DebugGDKTraps}
EndGDKErrorTrap; EndGDKErrorTrap;