Skip to content

Bug 16721: Analysing why rectangle is not drawn in Cairo graphics #47

Open
@hturner

Description

@hturner

As described in Bug 16721, when drawing multiple thin rectangles to create an image, one (or more?) of the rectangles is not drawn, creating a white stripe:

png("ImageWithWhiteStripe.png", width = 330, height = 200, type = "cairo")
image(matrix(1, nrow = 300, ncol = 1))
dev.off()

This only affects cairo raster devices. A hypothesis from @pmur002 is that the width of the rectangles is smaller than a pixel, so if a rectangle lies exactly on the border of two pixels with less than half a pixel on each side, nothing is drawn in those pixels.

This is currently difficult to debug as the white stripe occurs a long way through a loop where the 300 rectangles are drawn. Also it is difficult to relate the rectangles to pixels as the image does not fill the device due to axes etc.

So there are two main things that would be useful here:

  • Simplifying the reprex, e.g. using R functions from {grid}. Trying to pin down when and why the bug occurs by considering different examples.
  • Debugging to find the relevant lines of code and the input arguments at that stage. This will be down in the C code. Is the bug in the R codebase, or is it in cairo_rectangle from the external Cairo graphics library?

Some starter code for the first challenge

library(grid)

# Explore thin rectangles aligned exactly with pixels (or not)
png(width=100, height=100)
grid.rect(x=.01, width=.01, just="left", gp=gpar(col=NA, fill="black"))
dev.off()

# View a small pixel much bigger (with a pixel boundary grid)
img <- readPNG("Rplot001.png")
grid.raster(img, interpolate=FALSE)
grid.segments(0:10/10, 0, 0:10/10, 1)

For the second challenge, print debugging may be helpful

# Print out the sequence of x-values where the rectangles are drawn
static void cairoRectPath(double x0, double y0, double x1, double y1, 
                          pX11Desc xd) 
{
    printf("%f, %f\n", x0, x1);
    cairo_rectangle(xd->cc, x0, y0, x1 - x0, y1 - y0);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    GraphicsIssues related to graphicsRR 2025Issues reserved for R Dev Day @ Rencontres R 2025needs analysisTrack down the cause of the bug, or identify as not a bugneeds reprexWrite a minimal reproducible example that demonstrates the bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions