[cairo] Downing scaling images
Bill Spitzak
spitzak at gmail.com
Tue Dec 14 13:19:11 PST 2010
A different possible algorithm is to do downscaling in two steps. The
second step uses a scale close enough to 1:1 that bilinear sampling is
acceptable. Advantages of this are:
- The pixman code can be reused without changes
- The integer box filter is by far the fastest possible variable sized
filter you can do.
- The result of the first step can be saved by Cairo, making redrawing
the same transform (or similar ones such as rotations) much faster.
Disadvantages are:
- More blurring due to two filtering passes
- Blur will vary depending on the translation
- Space needed to store the intermediate surface (never more than 1/2
the full size surface).
- If the source is on the server it will have to be copied to cairo's
memory.
The algorithm is to reduce the transform to an integer scaling-only
transform and a remainder matrix which scales by more or equal to a
cutoff value. The cutoff needs to be chosen somewhere between .5 and 1,
larger values will make it blurrier but reduce bilinear sampling artifacts.
The integer scale is then used to do very fast box-filtering of the
image into a down-sized one (if it is 1:1 then this is skipped and the
original image is used). This is very fast: for a 1/N by 1/M scale each
output pixel is the average of a NxM rectangle of pixels and these
rectangles do not overlap.
The remainder transform is then used by the pixman bilinear filter to
sample this temporary image.
The temporary image is saved with the source surface, and invalidated
only if that surface is changed or if a different-sized temporary
surface is used.
Note that this is only an idea. I am worried about the blurring
artifacts. A direct comparison to the filtering I described before shows
that it has problems, though it is a lot better than some OpenGL mipmap
scaling I compared it to.
More information about the cairo
mailing list