View Issue Details

IDProjectCategoryView StatusLast Update
0036472PatchesWidgetsetpublic2020-01-08 00:17
ReporterCudaText man Assigned ToJuha Manninen  
PrioritynormalSeverityminorReproducibilityN/A
Status resolvedResolutionreopened 
PlatformUbuntu 19.10  
Product Version2.1 (SVN) 
Summary0036472: gtk3: Canvas.Pixels[]
DescriptionPatch implements it (reworks old bad code).
demo added too (4 black pixels in the corners of square).
TagsNo tags attached.
Fixed in Revisionr62472, r62473, r62509
LazTarget-
WidgetsetGTK 3
Attached Files

Relationships

related to 0036444 assignedJuha Manninen gtk3: LineTo must not include final x,y 

Activities

CudaText man

2019-12-22 13:49

reporter  

px.diff (2,244 bytes)   
Index: lcl/interfaces/gtk3/gtk3object.inc
===================================================================
--- lcl/interfaces/gtk3/gtk3object.inc	(revision 62430)
+++ lcl/interfaces/gtk3/gtk3object.inc	(working copy)
@@ -609,22 +609,10 @@
   end;
 end;
 
-procedure TGtk3WidgetSet.DCSetPixel(CanvasHandle: HDC; X, Y: integer;
-  AColor: TGraphicsColor);
-var
-  ASavedPenColor: TColor;
-  cr: PCairo_t;
+procedure TGtk3WidgetSet.DCSetPixel(CanvasHandle: HDC; X, Y: Integer; AColor: TGraphicsColor);
 begin
   if IsValidDC(CanvasHandle) then
-  begin
-    cr := TGtk3DeviceContext(CanvasHandle).Widget;
-    ASavedPenColor := TGtk3DeviceContext(CanvasHandle).CurrentPen.Color;
-    TGtk3DeviceContext(CanvasHandle).CurrentPen.Color := AColor;
-    cairo_move_to(cr, X, Y);
-    cairo_line_to(cr, X, Y);
-    cairo_stroke(cr);
-    TGtk3DeviceContext(CanvasHandle).CurrentPen.Color := ASavedPenColor;
-  end;
+    TGtk3DeviceContext(CanvasHandle).drawPixel(X, Y, AColor);
 end;
 
 procedure TGtk3WidgetSet.DCRedraw(CanvasHandle: HDC);
Index: lcl/interfaces/gtk3/gtk3objects.pas
===================================================================
--- lcl/interfaces/gtk3/gtk3objects.pas	(revision 62430)
+++ lcl/interfaces/gtk3/gtk3objects.pas	(working copy)
@@ -212,7 +212,7 @@
     procedure CreateObjects;
     procedure DeleteObjects;
   public
-    procedure drawPoint(x1: Integer; y1: Integer);
+    procedure drawPixel(x, y: Integer; AColor: TColor);
     procedure drawRect(x1, y1, w, h: Integer; const AFill, ABorder: Boolean);
     procedure drawRoundRect(x, y, w, h, rx, ry: Integer);
     procedure drawText(x: Integer; y: Integer; const s: String); overload;
@@ -1211,11 +1211,13 @@
     FreeAndNil(FvImage);
 end;
 
-procedure TGtk3DeviceContext.drawPoint(x1: Integer; y1: Integer);
+procedure TGtk3DeviceContext.drawPixel(x, y: Integer; AColor: TColor);
+// Seems that painting line from (a-1, b-1) to (a,b) gives one pixel
 begin
-  applyPen;
-  cairo_move_to(Widget , x1, y1);
-  cairo_line_to(Widget, x1, y1);
+  SetSourceColor(AColor);
+  cairo_set_line_width(Widget, 1);
+  cairo_move_to(Widget, x - PixelOffset, y - PixelOffset);
+  cairo_line_to(Widget, x + PixelOffset, y + PixelOffset);
   cairo_stroke(Widget);
 end;
 
px.diff (2,244 bytes)   

CudaText man

2019-12-22 13:50

reporter  

Juha Manninen

2019-12-22 14:12

developer   ~0120015

drawPoint is replaced with drawPixel. Is there some naming convention behind it? I did not check other widgetsets now.

AColor parameter is now required. What if the desired color is already set? Maybe there should be an overloaded procedure without color.

CudaText man

2019-12-22 14:20

reporter   ~0120016

convention? I used DrawPixel beccause it's called by Widgetset.DCSetPixels.

What if the desired color is already set? --- it cannot be set, it's always parameter of C.Pixels[a,b]:= Color.

CudaText man

2019-12-22 20:30

reporter   ~0120026

Last edited: 2019-12-23 22:05

View 2 revisions

Linked with 0036444
pls don't apply until we find why linked issue is not stable.

CudaText man

2019-12-27 20:39

reporter   ~0120089

@Juha
I don't get reply at Stackoverflow about Cairo, so... let's 1) test my patch for Canvas.Pixels (with my demo), 2) if it works for you- apply it.

Juha Manninen

2019-12-28 16:21

developer   ~0120105

Sorry but what exactly should should be tested with your "Canvas figures tests" app?
I don't see any DrawPixel calls there. I also don't see any difference after applying the patch.

CudaText man

2019-12-28 16:26

reporter   ~0120106

This demo has

  c.Pixels[100, 300]:= clBlack;
  c.Pixels[110, 300]:= clBlack;
  c.Pixels[100, 310]:= clBlack;
  c.Pixels[110, 310]:= clBlack;

so it must paint 4 black pixels in the corner of square, just near the red+blue lines (lines are square sides).
You can change clBlack to another color to see it.

Juha Manninen

2019-12-30 16:37

developer   ~0120157

Ok, it was difficult to see but I made some other tests and it looks OK.
I applied the patch. Thanks.

CudaText man

2019-12-30 23:07

reporter   ~0120163

now I made GETTER of Canvas.Pixels.
tested, no need to upload demo, very simple test.
  c.Pixels[100, 300]:= clYellow;
  c.Pixels[110, 300]:= clYellow;
  c.Pixels[100, 310]:= clYellow;
  c.Pixels[110, 310]:= clWhite-2;
  if c.Pixels[110, 310]<>clWhite-2 then
    caption:= 'not ok: '+IntToStr(c.Pixels[110, 310])
  else
    caption:= 'ok';
getpx.diff (1,341 bytes)   
Index: lcl/interfaces/gtk3/gtk3object.inc
===================================================================
--- lcl/interfaces/gtk3/gtk3object.inc	(revision 62472)
+++ lcl/interfaces/gtk3/gtk3object.inc	(working copy)
@@ -588,23 +588,20 @@
     Result := HCURSOR({%H-}PtrUInt(gdk_cursor_new(CursorValue)));
 end;
 
-function TGtk3WidgetSet.DCGetPixel(CanvasHandle: HDC; X, Y: integer
-  ): TGraphicsColor;
+function TGtk3WidgetSet.DCGetPixel(CanvasHandle: HDC; X, Y: integer): TGraphicsColor;
 var
-  ANewPix: PGdkPixbuf;
+  pixbuf: PGdkPixbuf;
+  pixels: pointer;
 begin
-  {$IFDEF GTK3DEBUGNOTIMPLEMENTED}
-  DebugLn('WARNING: TGtk3WidgetSet.DCGetPixel not implemented ...');
-  {$ENDIF}
   Result := 0;
   if IsValidDC(CanvasHandle) then
   begin
-    if (TGtk3DeviceContext(CanvasHandle).ParentPixmap <> nil) then
+    pixbuf := gdk_pixbuf_get_from_surface(TGtk3DeviceContext(CanvasHandle).CairoSurface, X, Y, 1, 1);
+    if Assigned(pixbuf) then
     begin
-      ANewPix := gdk_pixbuf_new_subpixbuf(TGtk3DeviceContext(CanvasHandle).ParentPixmap, X, Y, 1, 1);
-      // cairo_get_c
-      // gdk_pixbuf_get_pixels(ANewPix);
-      ANewPix^.unref;
+      pixels := gdk_pixbuf_get_pixels(pixbuf);
+      if Assigned(pixels) then
+        Result := PLongInt(pixels)^ and $FFFFFF; // take first 3 bytes at pixels^
     end;
   end;
 end;
getpx.diff (1,341 bytes)   

Juha Manninen

2019-12-31 11:53

developer   ~0120168

I applied the GetPixel patch. Thanks.

CudaText man

2020-01-04 11:15

reporter   ~0120207

Last edited: 2020-01-04 11:16

View 2 revisions

Small refactor (moved code of DCGetPixel into Gtk3DeviceContext class) and deleted dead code.

refac.diff (2,881 bytes)   
Index: lcl/interfaces/gtk3/gtk3object.inc
===================================================================
--- lcl/interfaces/gtk3/gtk3object.inc	(revision 62485)
+++ lcl/interfaces/gtk3/gtk3object.inc	(working copy)
@@ -589,21 +589,11 @@
 end;
 
 function TGtk3WidgetSet.DCGetPixel(CanvasHandle: HDC; X, Y: integer): TGraphicsColor;
-var
-  pixbuf: PGdkPixbuf;
-  pixels: pointer;
 begin
-  Result := 0;
   if IsValidDC(CanvasHandle) then
-  begin
-    pixbuf := gdk_pixbuf_get_from_surface(TGtk3DeviceContext(CanvasHandle).CairoSurface, X, Y, 1, 1);
-    if Assigned(pixbuf) then
-    begin
-      pixels := gdk_pixbuf_get_pixels(pixbuf);
-      if Assigned(pixels) then
-        Result := PLongInt(pixels)^ and $FFFFFF; // take first 3 bytes at pixels^
-    end;
-  end;
+    Result := TGtk3DeviceContext(CanvasHandle).getPixel(X, Y)
+  else
+    Result := 0;
 end;
 
 procedure TGtk3WidgetSet.DCSetPixel(CanvasHandle: HDC; X, Y: Integer; AColor: TGraphicsColor);
Index: lcl/interfaces/gtk3/gtk3objects.pas
===================================================================
--- lcl/interfaces/gtk3/gtk3objects.pas	(revision 62485)
+++ lcl/interfaces/gtk3/gtk3objects.pas	(working copy)
@@ -213,11 +213,11 @@
     procedure DeleteObjects;
   public
     procedure drawPixel(x, y: Integer; AColor: TColor);
+    function getPixel(x, y: Integer): TColor;
     procedure drawRect(x1, y1, w, h: Integer; const AFill, ABorder: Boolean);
     procedure drawRoundRect(x, y, w, h, rx, ry: Integer);
     procedure drawText(x: Integer; y: Integer; const s: String); overload;
     procedure drawText(x,y,w,h,flags: Integer; const s: String); overload;
-    procedure drawLine(x1: Integer; y1: Integer; x2: Integer; y2: Integer);
     procedure drawEllipse(x, y, w, h: Integer; AFill, ABorder: Boolean);
     procedure drawSurface(targetRect: PRect; Surface: Pcairo_surface_t; sourceRect: PRect;
       mask: PGdkPixBuf; maskRect: PRect);
@@ -1221,6 +1221,21 @@
   cairo_stroke(Widget);
 end;
 
+function TGtk3DeviceContext.getPixel(x, y: Integer): TColor;
+var
+  pixbuf: PGdkPixbuf;
+  pixels: pointer;
+begin
+  Result := 0;
+  pixbuf := gdk_pixbuf_get_from_surface(CairoSurface, X, Y, 1, 1);
+  if Assigned(pixbuf) then
+  begin
+    pixels := gdk_pixbuf_get_pixels(pixbuf);
+    if Assigned(pixels) then
+      Result := PLongInt(pixels)^ and $FFFFFF; // take first 3 bytes at pixels^
+  end;
+end;
+
 procedure TGtk3DeviceContext.drawRect(x1, y1, w, h: Integer; const AFill, ABorder: Boolean);
 begin
   cairo_save(Widget);
@@ -1310,14 +1325,6 @@
   end;
 end;
 
-procedure TGtk3DeviceContext.drawLine(x1: Integer; y1: Integer; x2: Integer;
-  y2: Integer);
-begin
-  ApplyPen;
-  cairo_move_to(Widget, x1, y1);
-  cairo_line_to(Widget, x2, y2);
-end;
-
 procedure TGtk3DeviceContext.drawEllipse(x, y, w, h: Integer; AFill, ABorder: Boolean);
 var
   save_matrix:cairo_matrix_t;
refac.diff (2,881 bytes)   

Juha Manninen

2020-01-04 19:20

developer   ~0120221

Why isn't TGtk3DeviceContext.drawLine needed any more?

CudaText man

2020-01-06 12:58

reporter   ~0120231

Last edited: 2020-01-06 12:58

View 2 revisions

Juha,
because it was not used anywhere, and was not working (to paint line, function missed call cairo_stroke(), and missed PixelOffset usage)

Juha Manninen

2020-01-08 00:17

developer   ~0120260

I applied refac.diff.
Thanks.

Issue History

Date Modified Username Field Change
2019-12-22 13:49 CudaText man New Issue
2019-12-22 13:49 CudaText man File Added: px.diff
2019-12-22 13:50 CudaText man File Added: Canvas figures tests.zip
2019-12-22 14:12 Juha Manninen Note Added: 0120015
2019-12-22 14:20 CudaText man Note Added: 0120016
2019-12-22 20:30 CudaText man Note Added: 0120026
2019-12-23 21:36 Juha Manninen Relationship added related to 0036444
2019-12-23 22:05 Bart Broersma Note Edited: 0120026 View Revisions
2019-12-27 20:39 CudaText man Note Added: 0120089
2019-12-28 16:21 Juha Manninen Note Added: 0120105
2019-12-28 16:26 CudaText man Note Added: 0120106
2019-12-30 16:37 Juha Manninen Assigned To => Juha Manninen
2019-12-30 16:37 Juha Manninen Status new => resolved
2019-12-30 16:37 Juha Manninen Resolution open => fixed
2019-12-30 16:37 Juha Manninen Fixed in Revision => r62472
2019-12-30 16:37 Juha Manninen LazTarget => -
2019-12-30 16:37 Juha Manninen Widgetset GTK 3 => GTK 3
2019-12-30 16:37 Juha Manninen Note Added: 0120157
2019-12-30 23:07 CudaText man File Added: getpx.diff
2019-12-30 23:07 CudaText man Note Added: 0120163
2019-12-30 23:07 CudaText man Status resolved => assigned
2019-12-30 23:07 CudaText man Resolution fixed => reopened
2019-12-31 11:53 Juha Manninen Status assigned => resolved
2019-12-31 11:53 Juha Manninen Fixed in Revision r62472 => r62472, r62473
2019-12-31 11:53 Juha Manninen Widgetset GTK 3 => GTK 3
2019-12-31 11:53 Juha Manninen Note Added: 0120168
2020-01-04 11:15 CudaText man File Added: refac.diff
2020-01-04 11:15 CudaText man Note Added: 0120207
2020-01-04 11:16 CudaText man Status resolved => assigned
2020-01-04 11:16 CudaText man Note Edited: 0120207 View Revisions
2020-01-04 19:20 Juha Manninen Note Added: 0120221
2020-01-06 12:58 CudaText man Note Added: 0120231
2020-01-06 12:58 CudaText man Note Edited: 0120231 View Revisions
2020-01-08 00:17 Juha Manninen Status assigned => resolved
2020-01-08 00:17 Juha Manninen Fixed in Revision r62472, r62473 => r62472, r62473, r62509
2020-01-08 00:17 Juha Manninen Widgetset GTK 3 => GTK 3
2020-01-08 00:17 Juha Manninen Note Added: 0120260