[cairo] Downsampling bitmaps from RSVG quality

Samuel Williams space.ship.traveller at gmail.com
Wed Jul 22 21:43:45 PDT 2015


Hi.

I'm resizing and generating PNGs from SVG inputs.

I want the best possible quality but the downsampling of large input images
to small output images seems to be quite crude.

I've tried looking at how to use set_filter but it's not obvious how to use
this in conjunction with RSVG's render_rsvg_handle. I tried rendering to a
recording surface and then using a surface pattern with a filter set but
that didn't help. Here is an example of the Ruby code:

def save_surface_to_png(input_surface, output_path, size)
width, height = *size
 surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
context = Cairo::Context.new(surface)
context.antialias = Cairo::ANTIALIAS_SUBPIXEL
 pattern = Cairo::SurfacePattern.new(input_surface)
pattern.set_filter(Cairo::FILTER_BEST)
 context.set_source(pattern)
context.rectangle(0,0,width,height)
context.fill
 context.target.write_to_png(output_path.to_s)
 surface.finish
end

def convert_svg_to_png_cairo(input_path, output_path, size)
puts "#{input_path} -> #{output_path}"
width, height = *size
 surface = Cairo::RecordingSurface.new(0, 0, width, height)
context = Cairo::Context.new(surface)
context.antialias = Cairo::ANTIALIAS_SUBPIXEL
 svg_handle = RSVG::Handle.new_from_file(input_path.to_s)
 svg_dimensions = svg_handle.dimensions
scale_factor = size[0].to_f / svg_dimensions.width.to_f
 context.scale(scale_factor, scale_factor)
 context.render_rsvg_handle(svg_handle)
 context.show_page
 save_surface_to_png(surface, output_path, size)
 surface.finish
end


It's not entirely obvious what I'm doing wrong or if it's even possible. Is
there some way to set the filter quality used by RSVG?

Thanks
Samuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20150723/8d0a7125/attachment-0001.html>


More information about the cairo mailing list