0

I'm working on an image annotation tool. It involves drawing circles of different size on top of an image. For convenience, I'd like to modify the cursor to be a circle of some radius. The snippet below (custom_cursor() function in particular) allows me to use a custom cursor (drawn with cairo) and change its radius with bracket keys ("[" and "]"). The issue I have is obvious in this video: https://i.stack.imgur.com/N2v4v.jpg. Basically, at some values of radii there's a lot of artifacts covering the whole cursor image. What's the reason for this and is it possible to remove that noise?

import gi
import numpy as np
import cairo
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GdkPixbuf

class MainWindow(Gtk.Window):

  def __init__(self):
    Gtk.Window.__init__(self, title = "Test")
    self.maximize()
    self.radius = 137
    viewport = Gtk.Viewport()
    self.darea = Gtk.DrawingArea()
    self.darea.connect("draw", self.draw)
    self.pixbuf = GdkPixbuf.Pixbuf.new_from_file("any_image.jpg")
    self.darea.set_size_request(self.pixbuf.get_width(), self.pixbuf.get_height());
    grid = Gtk.Grid()
    self.add(grid)
    scrolled = Gtk.ScrolledWindow()
    scrolled.set_hexpand(True)
    scrolled.set_vexpand(True)
    scrolled.connect("key-press-event",self.press)
    viewport.add(self.darea)
    scrolled.add(viewport)
    grid.add(scrolled)
    self.custom_cursor(self.radius)

  def draw(self, widget, cr):
    Gdk.cairo_set_source_pixbuf(cr, self.pixbuf, 0, 0)
    cr.paint()
    self.darea.queue_draw()

  def custom_cursor(self, radius):
    display = self.get_screen().get_display()
    pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, True, 8, 2 * radius, 2 * radius)
    surface = Gdk.cairo_surface_create_from_pixbuf(pb, 0, None)
    context = cairo.Context(surface)
    context.arc(radius, radius, radius, 0, 2 * np.pi)
    context.set_source_rgba(0, 0, 0, 1)
    context.stroke()
    pbdrawn = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height())
    cursor = Gdk.Cursor.new_from_pixbuf(display, pbdrawn, radius, radius)
    self.darea.get_screen().get_root_window().set_cursor(cursor)

  def press(self, widget, event):
    if (event.keyval == 93):
      self.radius = self.radius + 2
      self.custom_cursor(self.radius)
    elif (event.keyval == 91):
      self.radius = self.radius - 2
      self.custom_cursor(self.radius)

win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Edit: It could be a graphics issue. I'd appreciate it if anybody could confirm that there's (no) noise.

  • What is your `custom_cursor` function doing exactly? It creates a pixbuf, converts it to a cairo surface, and then converts it to a pixbuf again? Why do you even start with a pixbuf? – Uli Schlachter Mar 21 '19 at 17:22
  • That's a fair question. You're right, there's no need to start with pixbuf. Problem solved. Thank you! – Juozas Miškinis Mar 21 '19 at 18:30

1 Answers1

0

From the documentation for gdk_pixbuf_new:

Note that the buffer is not cleared; you will have to fill it completely yourself.

https://developer.gnome.org/gdk-pixbuf/2.36/gdk-pixbuf-Image-Data-in-Memory.html#gdk-pixbuf-new

Basically, you are seeing uninitialised memory here.

I would suggest to just create a cairo image surface directly, instead of creating a pixbuf just to convert it to a cairo surface. Cairo initialises new image surface for you.

Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39