how to get better shrunk images

506 views
Skip to first unread message

Michael Katz

unread,
Jun 24, 2023, 10:03:33 AM6/24/23
to skia-discuss
I'm drawing on a raster surface. I want to take the image on a surface and shrink it by a factor. I have code that looks like this:

SkImageInfo info = SkImageInfo::MakeN32( width, height, kUnpremul_SkAlphaType );
_surface = SkSurface::MakeRaster( info );
_canvas = _surface->getCanvas();
(draw some stuff on _canvas)
sk_sp<SkImage> image = _surface->makeImageSnapshot();

SkImageInfo info2 = SkImageInfo::MakeN32( width / 2, height / 2, kUnpremul_SkAlphaType );
_surface2 = SkSurface::MakeRaster( info2 );
_canvas2 = _surface2->getCanvas();
_canvas2->scale( 0.5, 0.5 );
_canvas2->drawImage( image, 0, 0 );

What I get out looks like the left column below. The right column shows what I expect. Is there a way to get that better quality out of Skia? Am I doing the whole process wrong? I played around with SkSamplingOptions to use a SkCubicResampler, but I think that's just for upsizing, not downsizing?


skia_sizes.png

craste...@gmail.com

unread,
Jun 24, 2023, 11:33:32 AM6/24/23
to skia-discuss
Have you looked into SkSamplingOptions ?

Michael Ludwig

unread,
Jun 27, 2023, 10:08:23 AM6/27/23
to skia-discuss
By default drawImage just uses nearest neighbor sampling, which will not handle this sort of progressive downsizing. You should use SkSamplingOptions{SkFilterMode::kLinear} in your drawImage call and it should be very similar to what's produced by paint.NET

Michael Katz

unread,
Jun 28, 2023, 8:18:23 AM6/28/23
to skia-discuss
Thanks. Perhaps that could be called out more clearly in the documentation as the way to get the best quality shrinking: https://api.skia.org/classSkCanvas.html#ab37570ba2c305284a79e88ad57a86313. From what I can see, that documentation doesn't even mention the FilterMode option with the drawImage() function. So I was trying every variant I could with the SamplingOptions and nothing was helping.

Michael Katz

unread,
Jun 28, 2023, 8:18:30 AM6/28/23
to skia-discuss
Thanks. Perhaps that could be called out better in the documentation as the way to do the highest quality shrinking. Here: https://api.skia.org/classSkCanvas.html. Or here https://api.skia.org/SkSamplingOptions_8h.html#a12f307632434a5814d5c6ad50f1c1551.


On Tuesday, June 27, 2023 at 7:08:23 AM UTC-7 michae...@google.com wrote:
Reply all
Reply to author
Forward
0 new messages