Lazarus

Programming => Graphics => Graphics and Multimedia => BGRABitmap and LazPaint => Topic started by: Dibo on October 17, 2012, 11:12:15 pm

Title: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 17, 2012, 11:12:15 pm
Hi,

Referring to this topic about BGRA Controls bug: http://www.lazarus.freepascal.org/index.php/topic,18596.msg105177.html

I created this new topic for BGRA Bitmap. I attached simple demo with paint box which cause drawing bug reported in above BGRA Controls topic.

I have still this same version of BGRABitmap, but just updated Lazarus from SVN and can confirm this bug. But seems that something is wrong with GTK or someone fixed very old bug in LCL (attached demo is using simple BGRABitmap.Draw(Canvas, 0,0,false) method).
Circular, I want post this issue on lazarus bugtracker but maybe you want look at this demo. If you don't have Linux: it's looks like BGRABitmap.Draw() always draw starting from form left-top corner (0,0), but you don's see what is drawing if paintbox is on form center, if you move paint box to form left-top coner then you will se red rectangle. Have you any idea what it could be? :) Some hazardous change in LCL :D

This code in pure LCL is working well:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. var b: TBGRABitmap;
  3. begin
  4. //  b := TBGRABitmap.Create(PaintBox1.Width, PaintBox1.Height);
  5. //  b.Fill(ColorToBGRA(ColorToRGB(clRed)));
  6. //  b.Draw(PaintBox1.Canvas, 0, 0,False);
  7. //  b.Free;
  8.   PaintBox1.Canvas.Brush.Color := clRed;
  9.   PaintBox1.Canvas.FillRect(PaintBox1.ClientRect);
  10. end;
  11.  

Regards
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on October 19, 2012, 12:20:07 pm
All I can tell you is that Gtk drawing is using conversion of type of pointers, so that it is not garanteed that the good fields are used.

So maybe it is related to the way Gtk contexts are defined. They are accessed like this :
Code: [Select]
TGtkDeviceContext(ACanvas.Handle)
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 19, 2012, 01:11:19 pm
Hm, what I should mention in this bug report on lazarus bug tracker?
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 19, 2012, 08:29:00 pm
Something is going wrong with GTK on latest lazarus from SVN. I can't compile BGRA Bitmap (ver 6.1) without comment TGtkDeviceContext type. This same with other controls like VirtualTreeView (but there is more errors related to GTK)
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on October 19, 2012, 11:30:04 pm
I do not know what you should mention. Maybe simply that TGtkDeviceContext type seems to have been changed.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: LuizAmérico on October 20, 2012, 04:20:26 pm
About TGtkContext:

The class name changed. To compile both in new and old LCL see how i did in http://code.google.com/p/luipack/source/detail?r=901 (Must have LCLVersion unit in uses)

About Gtk coordinates:

It was changed to fix some bugs. Please add a bugreport with a small demo so we can look where the bug is
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: BigChimp on October 20, 2012, 04:26:45 pm
@Luiz:
Code: [Select]
{$if (lcl_major <> 1) or (lcl_minor <> 0)}Wouldn't this be very nasty if Lazarus 1.2 is ever released?
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 20, 2012, 04:55:57 pm
About TGtkContext:

The class name changed. To compile both in new and old LCL see how i did in http://code.google.com/p/luipack/source/detail?r=901 (Must have LCLVersion unit in uses)

About Gtk coordinates:

It was changed to fix some bugs. Please add a bugreport with a small demo so we can look where the bug is

In bgra bitmap is:

Code: [Select]
{$IFDEF LCLgtk2}
type TGtkDeviceContext = TGtk2DeviceContext;
{$ENDIF} 

And it should be changed to:
Code: [Select]
{$IFDEF LCLgtk2}
{$MACRO ON}
{$if (lcl_major <> 1) or (lcl_minor <> 0)}
{$define type TGtkDeviceContext = TGtk2DeviceContext;}
{$endif}
{$ENDIF} 

Is it ok? I don't know what "define" in this context means. Now BGRABitmap is compiling on Lazarus 1.0.2 but I don't know if this trick will work on older lazarus. I will report now draw issue. Thanks
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: BigChimp on October 20, 2012, 05:11:43 pm
The macro evaluates the compiler defines/constants (whatever they're called) at build time. There is similar functionality for the FPC version using FPC_FULLVERSION

I would rather do something like this; see lclversion.pas for the numbering scheme by the way.

Note: haven't tested the numbers themselves or even if this code compiles:
Edit: ah, I already corrected an error
Code: [Select]
// Start with enabling macros for all widgetsets so you're not surprised later on that something doesn't compile right:
{$MACRO ON}
{$IF LCL_FULLVERSION<1000200} //for laz older than say 1.0.2; you can also use >= and other operators
//((lcl_major *  100 + lcl_minor) * 100 + lcl_release) * 100 + lcl_patch;
//1.0.2.0
  {$IFDEF LCLgtk2}
  // This code will be compiled only if all IFDEFs are true, so the type will only be compiled/appear
  // in LCL/Lazarus older than 1.0.2
  type TGtkDeviceContext = TGtk2DeviceContext;
  {$ENDIF LCLgtk2}
{$ENDIF LCL_FULLVERSION>=10002}

... hope I didn't make a mistake somewhere...
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 22, 2012, 12:46:41 pm
I have posted report issue here:
http://bugs.freepascal.org/view.php?id=23168
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on October 22, 2012, 04:18:18 pm
Circular I think I fixed it.

BGRAGTKBitmap unit, TBGRAGtkBitmap.DrawTransparent method. Existing code:
Code: Pascal  [Select][+][-]
  1.   gdk_pixbuf_render_to_drawable(FPixBuf,
  2.     TGtkDeviceContext(ACanvas.Handle).Drawable,
  3.     TGtkDeviceContext(ACanvas.Handle).GC,
  4.     0,0,
  5.     TGtkDeviceContext(ACanvas.Handle).Offset.X+Rect.Left,
  6.     TGtkDeviceContext(ACanvas.Handle).Offset.y+Rect.Top,
  7.     Width,Height,
  8.     GDK_RGB_DITHER_NORMAL,0,0);  
  9.  
I changed to:
Code: [Select]
  gdk_pixbuf_render_to_drawable(FPixBuf,
    TGtkDeviceContext(ACanvas.Handle).Drawable,
    TGtkDeviceContext(ACanvas.Handle).GC,
    0,0,
    TGtkDeviceContext(ACanvas.Handle).ClipRect.X+Rect.Left,
    TGtkDeviceContext(ACanvas.Handle).ClipRect.y+Rect.Top,
    Width,Height,
    GDK_RGB_DITHER_NORMAL,0,0);   
Seems that before Offset had some values, but now X and Y are 0. I changed to ClipRect and now it is working. I will ask in buckracker issue why this rect is now 0
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on October 22, 2012, 07:52:49 pm
Ok. Well, let's wait for their answer then.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on November 03, 2012, 05:26:49 pm
@circular: There is a new answer about this issue:
http://bugs.freepascal.org/view.php?id=23168

Seems that ClipRec is not safety too. Paul posted example to use
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on November 04, 2012, 09:24:49 pm
Ok. I'm updating... Done on subversion.

Does the corrected version for Gtk (offset + window translation) work ?
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on November 05, 2012, 11:46:47 am
Is this correct link for svn? svn checkout svn://svn.code.sf.net/p/lazpaint/code/ lazpaint-code
Seems that there is old bgrabitmap (5.9.4)

About new solution. I thought that it is working, but my test project still remembered path to previous BGRABitmap. Now I see that solution posted by Paul doesn't work. I need to ask him:

This:
Code: Pascal  [Select][+][-]
  1. p := Rect.TopLeft;
  2. DpToLP(ACanvas.Handle, P, 1);
  3.  
... return correct points, but in negative. I must use Abs() function:
Code: Pascal  [Select][+][-]
  1. TGtkDeviceContext(ACanvas.Handle).Offset.X+abs(P.X)
  2. TGtkDeviceContext(ACanvas.Handle).Offset.Y+abs(P.Y)
  3.  
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Osvaldo Arruda on November 05, 2012, 02:03:45 pm
1. Error:

  P := Point(x,y);
  DpToLP(ACanvas.Handle, @P, 1);

TO:
  P := Point(x,y);
  DpToLP(ACanvas.Handle, P, 1);


2. Erorr:

  P := Point(x,y);
  DpToLP(ACanvas.Handle, @P, 1);

TO:

  P := Point(x,y);
  DpToLP(CanvasSource.Handle, P, 1);
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on November 05, 2012, 04:00:06 pm
@Dibo:

Ok I wait for more information.

Maybe we need to take the opposite number instead.

About svn, I'm still using old svn repository. I had problem with the new one. I'll try to switch to the new repository when I have some time to do so.

@Osvaldo:

I suppose you're right if it's simply a "var" type. But it seems to work anyway. Is there a syntax extension that allows to use @ here? It's rather C syntax using &.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Dibo on November 05, 2012, 04:26:54 pm
About svn, I'm still using old svn repository. I had problem with the new one. I'll try to switch to the new repository when I have some time to do so.
What is old svn link? Can't find it on wiki
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on November 05, 2012, 09:42:59 pm
Ok i've updated the new svn.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: Thomas Munk on October 25, 2013, 10:40:37 pm
I'm not sure - what's the status of this problem?


I doing a new project in Lazarus 1.0.12 (both Windows and Linux/GTK2). I have checked out new newest BGRABitmap from svn.


I'm using TBGRABitmap.Draw on a TGraphicControl descendant's canvas - and it's not working: in a simple for x,y loop painting the same bitmap in a grid, they position like random (on Linux). In Windows it paints fine.


Then I changed the code in TBGRAGtkBitmap.DrawTransparent as described in Reply #10 - and now it works on Linux.
P: TPoint and DPtoLP can then be removed.


Is current svn supposed to be working or does it need this fix?


btw, Thanx for a fine lib
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: VTwin on January 19, 2014, 11:25:24 pm
I'm using Lazarus 1.0.14 with BGRABitmap 7.2, and find what seems to be a related bug, so thought I would mention it here.

When drawing a bitmap to a PaintBox canvas, OS X and Windows use local coordinates, but Linux uses global. My code looks like this:

Code: [Select]
    fBuffer := BmpResize(fBitmap, w, h, fFineResample);
    {$IFDEF LINUX} // bug!!!
    fBuffer.Draw(fView.Canvas, x + fView.Left, y + fView.Top, fOpaque);
    {$ELSE}
    fBuffer.Draw(fView.Canvas, x, y, fOpaque);
    {$ENDIF}

where fBuffer and fBitmap are TBGRABitmaps and fView is a PaintBox. This took me many hours to track down, maybe helpful to someone.

Cheers,
Frederick
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on March 27, 2014, 04:33:08 am
Frederick, does the fix mentionned by Thomas Munk solve your problem too?

It is to use the following code in BGRAGtkBitmap.pas, in the procedure TBGRAGtkBitmap.DrawTransparent(ACanvas: TCanvas; Rect: TRect) :
Quote
  gdk_pixbuf_render_to_drawable(FPixBuf,
    TGtkDeviceContext(ACanvas.Handle).Drawable,
    TGtkDeviceContext(ACanvas.Handle).GC,
    0,0,
    TGtkDeviceContext(ACanvas.Handle).ClipRect.X+Rect.Left,
    TGtkDeviceContext(ACanvas.Handle).ClipRect.y+Rect.Top,
    Width,Height,
    GDK_RGB_DITHER_NORMAL,0,0);   
Instead of
Quote
  P := Rect.TopLeft;
  DpToLP(ACanvas.Handle, P, 1);
  gdk_pixbuf_render_to_drawable(FPixBuf,
    TGtkDeviceContext(ACanvas.Handle).Drawable,
    TGtkDeviceContext(ACanvas.Handle).GC,
    0,0,
    TGtkDeviceContext(ACanvas.Handle).Offset.X + abs(P.X),
    TGtkDeviceContext(ACanvas.Handle).Offset.Y + abs(P.Y),
    Width,Height,
    GDK_RGB_DITHER_NORMAL,0,0);   

I don't have Linux and I am getting contradictory feedbacks, so I need more confirmations to know what code to put there!
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: lainz on March 27, 2014, 06:37:14 pm
Basically it means that you need to get linux, at least in a virtual machine :)

I'll need the bgrabitmap linux working for some project, so i'll need my virtual machine too.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on March 30, 2014, 06:39:37 pm
Yes. I found Lubuntu that seems light enough for me.
Title: Re: BGRABitmap - drawing coordinates changed since Lazarus 1.0.2
Post by: circular on March 31, 2014, 12:33:55 am
Ok I guess I understand.

We need to use LPtoDP function (logical to device) instead of DPtoLP. This explains the sign issue we had before.

I am not surewhy then Dibo had a problem and proposed to use ClipRect. Maybe it is because on Linux, the packages are not recompiled automatically when the files in it are modified.

I have fixed it on SVN, at least it was working on my virtual machine with Lubuntu with a PaintBox.
TinyPortal © 2005-2018