How can i write a SkBitmap to a file

6,677 views
Skip to first unread message

lucius

unread,
Apr 13, 2009, 12:50:55 PM4/13/09
to skia-discuss
Hi,

If I create a SkBitmap object in skia, how can I write it to a file?

Thank you.

Marc-Antoine Ruel

unread,
Apr 13, 2009, 12:54:26 PM4/13/09
to skia-d...@googlegroups.com
That's quite a general question !


has some example embedded in.

M-A

Mike Reed

unread,
Apr 13, 2009, 1:07:03 PM4/13/09
to skia-d...@googlegroups.com
Skia doesn't have its own file-format, but it does have some encoder
support prewritten.

<SkImageEncoder.h>

enum Type {
kJPEG_Type,
kPNG_Type
};
static bool EncodeFile(const char file[], const SkBitmap&, Type,
int quality);

To invoke...

bool success = SkImageEncoder::EncodeFile("myimage.png", bitmap,
SkImageEncoder::kPNG_Type, 100);

Lucius Fox

unread,
Apr 13, 2009, 5:31:48 PM4/13/09
to skia-d...@googlegroups.com
On Mon, Apr 13, 2009 at 10:07 AM, Mike Reed <re...@android.com> wrote:
>
> Skia doesn't have its own file-format, but it does have some encoder
> support prewritten.
>
> <SkImageEncoder.h>
>
>    enum Type {
>        kJPEG_Type,
>        kPNG_Type
>    };
>    static bool EncodeFile(const char file[], const SkBitmap&, Type,
>                           int quality);
>
> To invoke...
>
> bool success = SkImageEncoder::EncodeFile("myimage.png", bitmap,
> SkImageEncoder::kPNG_Type, 100);
>

Thank you.

Can you please tell me how can I covert it in memory (instead of
file)? I think there is
a class called SkWStream and I can call SkImgEncoder::EncodeStream() instead.
But my question is how will SkWStream allocate memory during the png
image encoding?

This is because we don't know how much memory is needed before the
image encoding.

Mike Reed

unread,
Apr 14, 2009, 2:51:01 PM4/14/09
to skia-d...@googlegroups.com
SkWStream is mostly abstract, so you can create your own subclass to
take care of actually storing the compressed data. There are existing
subclass that write it to a file, or into RAM, but if they do not
sure, you can create your own.

There is no sure way to predict how large the compressed version will
be, without actually compressing it.

Lucius Fox

unread,
Apr 14, 2009, 4:02:22 PM4/14/09
to skia-d...@googlegroups.com
Thank you, Mike.

I am trying to encode PNG to RAM.

So I am using:
SkMemoryStream memStream;
SkImageEncoder::EncodeStream(&memStream, newBitmap,
SkImageEncoder::kPNG_Type, 100);

But SkMemoryStream is not a child class of SkWStream. How can I get a
SkWStream from a SkMemoryStream?

Thank you.

Mike Reed

unread,
Apr 14, 2009, 5:25:47 PM4/14/09
to skia-d...@googlegroups.com
Use SkDynamicMemoryWStream, and when you are done encoding, call

size_t size = stream.getOffset(); // returns the number of bytes written

Now you know how big the encoded data is. Next you can either peek
into a flattened cache of that data, by calling

const char* peek = stream.getStream(); // ptr only valid until the
next write() call

or have the stream object copy it into a buffer that you allocate.

void* buffer = malloc(size);
stream.copyTo(buffer);

Liu Ke

unread,
Apr 15, 2009, 12:34:16 PM4/15/09
to skia-d...@googlegroups.com
Hi all,

I'm trying to use SkPath  draw some polygon lines with Gradient. By use SkGradientShader::CreateLinear(), it is successed to create polygon lines with gardient between two points.

My problem is: how could i draw gardient along the polygon lines, and vertical gardient along the polygon lines.

Thanks.

----------------
Best Regards
Liu Ke

Mike Reed

unread,
Apr 15, 2009, 1:08:09 PM4/15/09
to skia-d...@googlegroups.com
Good feature request!

Shaders are unaware of anything geometric. They are applied across
whatever is being drawn (e.g. rects, paths, text) but the orientation
of the shader is determined solely by the canvas' matrix and the
shader's local matrix, but not by the shape or direction of the shape
being drawn.

I think having shaders follow a stroked polygon would be cool,
changing their orientation as the polygon bent etc, but it is not
something Skia knows how to do today.

mike
Reply all
Reply to author
Forward
0 new messages