How to scale a SkBitmap

2,541 views
Skip to first unread message

silverburgh

unread,
Apr 13, 2009, 8:57:05 PM4/13/09
to skia-discuss
Hi,

I am trying to scale a SkBitmap. Here is what I try:
1. write the original bitmap to a file
2. scale the bitmap
3. write the final bitmap to another file

But what I find out is the size bitmap files do not change at all. I
would like to know what am I doing wrong:

Here is what I did:
bool success = SkImageEncoder::EncodeFile("/data/myimage.png", bitmap,
SkImageEncoder::kPNG_Type, 100);

SkCanvas canvas(bitmap);

float sx = 0.5;
float sy = 0.5;

success = canvas.scale( sx, sy);

success = SkImageEncoder::EncodeFile("/data/myimage2.png", bitmap,
SkImageEncoder::kPNG_Type, 100);

Thank you.

Mike Reed

unread,
Apr 14, 2009, 2:56:04 PM4/14/09
to skia-d...@googlegroups.com
canvas.scale(sx, sy) setup the canvas to perform scaling on any
subsequent draw call.

To create a new bitmap that is a scale of another one... (pseudo code,
not tested or compiled)

SkBitmap scale_bitmap(const SkBitmap& src, float sx, float sy) {
int width = (int)round(src.width() * sx);
int height = (int)round(src.height() * sy);
SkBitmap dst;
dst.setConfig(src.config(), width, height);
dst.allocPixels();
dst.eraseColor(0);

// canvas will draw into dst, capturing the scaled version of src
SkCanvas canvas(dst);
canvas.scale(sx, sy);
canvas.drawBitmap(src, 0, 0, NULL);

return dst;
Reply all
Reply to author
Forward
0 new messages