Recent

Author Topic: BGRABitmap transparency error on OSX  (Read 7453 times)

gregbug

  • New member
  • *
  • Posts: 5
BGRABitmap transparency error on OSX
« on: June 01, 2012, 12:39:49 am »
Hi! guys! i'm Gianluca from italy!!!
i'm using lazarus and BGRABitmap for make a freeware tool for Gideros SDK mobile...

i copy a portion of image from a TPicture (it's a PNG with transparency) in a TBGRABitmap then  flip it (on horizontal or vertical axes if needed) then copy back in a TPicture Box...

in windows 7 all is working perfect.!!!

but on osx (lion) the copied image is corrupted!

the code in TPaintBox "OnPaint" is:

Code: [Select]
procedure TfrmMain.imagePreviewPaint(Sender: TObject);
var
   xPos, yPos: Integer;
   w, h: integer;
begin
   if fcurrentSprite <> nil then
     begin
      xPos := ((scrlImage.Width div 2) - (fcurrentSprite.w div 2));
      yPos := ((scrlImage.Height div 2) - (fcurrentSprite.h div 2));
      animProject.preview.SetSize(fcurrentSprite.w, fcurrentSprite.h);
      //animProject.preview.Bitmap.Clear;
      animProject.preview.Canvas.CopyRect(
                                          Rect(0, 0, fcurrentSprite.w,  fcurrentSprite.h), AnimProject.TexturePack.Bitmap.Canvas,
                                          Rect(fcurrentSprite.x, fcurrentSprite.y, fcurrentSprite.x + fCurrentSprite.w, fcurrentSprite.y + fCurrentSprite.h));

         if fCurrentSprite.flipX then begin
          animProject.preview.HorizontalFlip;
         end;
         if fCurrentSprite.flipY then begin
         end;

         animProject.preview.draw(imagePreview.Canvas, xPos, yPos, false);
     end;
end;

any ideas? it's my bug ?

thanks!..
windows output: (that's fine):

(http://s19.postimage.org/vkft947kv/Snapshot_1.jpg)

osx Lion: (that's corrupted)

(http://s9.postimage.org/7i6gsnmdn/Schermata_06_2456080_alle_00_29_03.jpg)

PS: testet with lazarus "official" and last build (31/5) and BGRABitmap rev. 387 (that compile on osx lion with no errors)

please help!!!

thanks again,
Gianluca.

Shebuka

  • Sr. Member
  • ****
  • Posts: 422
Re: BGRABitmap transparency error on OSX
« Reply #1 on: June 01, 2012, 11:06:29 am »
I have similar problem with time ago
http://www.lazarus.freepascal.org/index.php/topic,12411.msg68109.html#msg68109

resolved by using TImage...

circular

  • Hero Member
  • *****
  • Posts: 3045
    • Personal webpage
Re: BGRABitmap transparency error on OSX
« Reply #2 on: June 01, 2012, 04:21:15 pm »
Maybe it would be necessary to create a special implementation for MacOS, like BGRAWinBitmap. But anyway, using Canvas for handling transparent bitmaps is not recommended. The Canvas property is only here to provide compatible functions for drawing with Brush and Pen, but should be avoided as much as possible. It can slow down your application too because using Canvas property need to create a TBitmap, and then this bitmap needs to be converted back to BGRABitmap data. CanvasBGRA property is there to provide these functions, but with transparency and antialiasing, and no implicit conversion.

Can you load initially the Png image directly into a TBGRABitmap ? So that you can then copy it in your preview using PutImage with dmSet drawing mode. Alternatively, you can assign a TBitmap to a BGRABitmap that will contain your image.

« Last Edit: June 01, 2012, 04:25:40 pm by circular »
Conscience is the debugger of the mind

gregbug

  • New member
  • *
  • Posts: 5
Re: BGRABitmap transparency error on OSX
« Reply #3 on: June 01, 2012, 11:06:27 pm »
thanks guys!!! for give time to me! :)

Maybe it would be necessary to create a special implementation for MacOS, like BGRAWinBitmap. But anyway, using Canvas for handling transparent bitmaps is not recommended. The Canvas property is only here to provide compatible functions for drawing with Brush and Pen, but should be avoided as much as possible. It can slow down your application too because using Canvas property need to create a TBitmap, and then this bitmap needs to be converted back to BGRABitmap data. CanvasBGRA property is there to provide these functions, but with transparency and antialiasing, and no implicit conversion.

Can you load initially the Png image directly into a TBGRABitmap ? So that you can then copy it in your preview using PutImage with dmSet drawing mode. Alternatively, you can assign a TBitmap to a BGRABitmap that will contain your image.

@circular

yes i can load png directly into tBGRABitmap...

coding now...

i keep you informaed... thanks again!! :)

gregbug

  • New member
  • *
  • Posts: 5
Re: BGRABitmap transparency error on OSX
« Reply #4 on: June 02, 2012, 12:53:31 am »
ok. i think that the problem (under osx) is in "CopyRect"

now i loading main png in TBGRABitmap then i need to copy only a portion of this image into the tPaintBox and i use

CopyRect... and in osx the image is corrupted...

if i use putImage (but how can i copy only a portion of image without copyrect?) the image is perfect...

i miss something ?  :-\

please help! :)   :-\

gregbug

  • New member
  • *
  • Posts: 5
Re: BGRABitmap transparency error on OSX
« Reply #5 on: June 02, 2012, 03:35:32 pm »
solved:
this copy work also on osx...

Code: [Select]
procedure copyRectGFX(xPos, yPos, width, height: integer; var Source, Destination: TBGRABitmap);
var
  x, y: integer;
  xd, yd: integer;
begin
  xd := 0; yd := 0;
  for y := yPos to yPos + height do begin
      for x := xPos to xPos + width do begin
          destination.CanvasBGRA.GammaExpandedPixels[xd, yd] := source.CanvasBGRA.GammaExpandedPixels[x, y];
          inc(xd);
      end;
      xd := 0;
      inc(yd);
  end;
end;

...


circular

  • Hero Member
  • *****
  • Posts: 3045
    • Personal webpage
Re: BGRABitmap transparency error on OSX
« Reply #6 on: June 02, 2012, 05:35:03 pm »
That is the slow way.

If you want to draw a portion that has size (sx,sy) into a bitmap of size (sx,sy), and that the upper coordinate in the source bitmap is (x,y), you can do :

Code: [Select]
dest := TBGRABitmap.Create(sx,sy);
dest.PutImage(-x,-y, source, dmSet);
...
dest.Free;

Maybe I should make a CopyRect procedures that does that.

In the meantime, you can use GetPart :
Code: [Select]
dest := source.GetPart(rect(x,y,x+sx,y+sy));

Which creates a new bitmap containing the portion you want.
« Last Edit: June 02, 2012, 05:56:52 pm by circular »
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 3045
    • Personal webpage
Re: BGRABitmap transparency error on OSX
« Reply #7 on: June 02, 2012, 06:16:16 pm »
I've added PutImagePart, which is an optimized way for doing PutImage(GetPart(.Free)), and also added CopyRect(x,y,...) in CanvasBGRA.

I notice that there is already CopyRect(Rect,...) in CanvasBGRA.
Conscience is the debugger of the mind

gregbug

  • New member
  • *
  • Posts: 5
Re: BGRABitmap transparency error on OSX
« Reply #8 on: June 02, 2012, 07:45:16 pm »
@circular
thanks for your fast support and help!

thnaks again.
Gianluca.