[cairo] Extending XRender to support non-blurry edges for transformations(?)

Bill Spitzak spitzak at thefoundry.co.uk
Thu Jul 31 05:45:32 PDT 2008

Carl Worth wrote:

> It's easy to describe the high-level result desired, (a rotated, scaled
> image with internal blurring but no blurring on the edges). And it's
> possible to achieve that result with cairo, (EXTEND_PAD,
> cairo_rectangle, cairo_fill).

Yes this is correct, the result should be *exactly* this.

> But for some reason, proposals keep coming up trying to make
> cairo_paint, (or XRenderComposite), achieve that same result. And these
> proposals keep suggesting there is some extend mode that can achieve it.
> How do you define that extend mode exactly? Recall that an extend mode
> is defined by what values should be returned when attempting to
> reference a pixel that is "outside" a source surface.
> The magic behavior would require an extend mode that returns different
> values for the same (x,y) samples depending on internal state of the
> filtering engine that is requesting those samples.

The problem is that you are thinking of this as a "sampling mode", a 
control for a function that takes a *source* x,y and returns a color. It 
is not that, it is a "filtering mode". The "filtering mode" controls a 
function that produces *OUTPUT* pixels, not input pixels. This function 
takes a source image, a transformation between the source and 
destination, and the x,y of a *destination* pixel.

Here are some filtering modes:

PAD: use the derivative of the transformation to figure out the size of 
a quadralateral in the source image. Maximize this quadralaterial with a 
1x1 square. Center it on the back-projected center of the output pixel. 
Get all the pixels from the source area (using the sampling mode) and do 
a weighted sum based on a filter kernel scaled to the size of this 

BLACK: (the new default setting of Cairo) get the exact same value as 
EXTEND_PAD and then multiply it by an antialiased rectangle of the size 
of the source transformed by the transformation.

SQUARES: This is the same as EXTEND_CLAMP except the EXTEND_PAD 
algorithim is modified by not maximizing the quadralateral with a 1x1 
square. The resulting image when scaled up is therefore made of 
antialiased squares for the pixels. Many users prefer this over the 
technically correct, and this is what OSX is doing.

There *may* be sampling modes, but they should consist of 
PAD/MIRROR/REPEAT with seperate controls for horizontal/vertical. I 
recommend that these just be extra bits or'd with the filtering mode as 
it is easier for users to understand, and actual efficient 
implementations probably require the filtering and sampling to be done 
by the same code.

There should *NOT* be the current "black" sampling mode. This is easily 
duplicated by adding an edge of black pixels all around the image and 
then using PAD. It may be desirable for the filtering mode to use the 
edge of the source rectangle, this would also match what most other 
api's do, and if the source rectangle is outside the image it could 
treat those pixels as black, making it quite possible to duplicate the 
current EXTEND_NONE output.

Bill Spitzak, Senior Software Engineer
The Foundry, 1 Wardour Street, London, W1D 6PA, UK
Tel: +44 (0)20 7434 0449 * Fax: +44 (0)20 7434 1550 * Web: 
The Foundry Visionmongers Ltd * Registered in England and Wales No: 4642027

More information about the cairo mailing list