centralize encoding to SkData

Bug: skia:
Change-Id: If3a9a6de54cf76d03e4d159b54b07a4ea6d5cda9
Reviewed-on: https://skia-review.googlesource.com/83020
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2017-12-08 16:20:58 -05:00 committed by Skia Commit-Bot
parent 42b6cffaad
commit 25eef6b342
3 changed files with 36 additions and 7 deletions

View File

@ -9,6 +9,7 @@
#define SkImageEncoder_DEFINED
#include "SkBitmap.h"
#include "SkData.h"
#include "SkEncodedImageFormat.h"
#include "SkStream.h"
@ -33,6 +34,7 @@
*/
SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
SkEncodedImageFormat format, int quality);
/**
* The following helper function wraps SkEncodeImage().
*/
@ -41,4 +43,26 @@ inline bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFor
return src.peekPixels(&pixmap) && SkEncodeImage(dst, pixmap, f, q);
}
/**
* Encode SkPixmap in the given binary image format.
*
* @param src source pixels.
* @param format image format, not all formats are supported.
* @param quality range from 0-100, this is supported by jpeg and webp.
* higher values correspond to improved visual quality, but less compression.
*
* @return encoded data or nullptr if input is bad or format is unsupported.
*
* Will always return nullptr if Skia is compiled without image
* encoders.
*
* Note that webp encodes will use webp lossy compression.
*/
SK_API sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality);
/**
* Helper that extracts the pixmap from the bitmap, and then calls SkEncodePixmap()
*/
SK_API sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality);
#endif // SkImageEncoder_DEFINED

View File

@ -23,7 +23,6 @@
#include "SkPixelSerializer.h"
#include "SkReadPixelsRec.h"
#include "SkSpecialImage.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkSurface.h"
@ -98,8 +97,7 @@ sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) cons
SkBitmap bm;
SkColorSpace* legacyColorSpace = nullptr;
if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) {
SkDynamicMemoryWStream buf;
return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData() : nullptr;
return SkEncodeBitmap(bm, type, quality);
}
return nullptr;
}
@ -118,12 +116,9 @@ sk_sp<SkData> SkImage::encodeToData(SkPixelSerializer* serializer) const {
if (serializer) {
return serializer->encodeToData(pmap);
} else {
SkDynamicMemoryWStream buf;
return SkEncodeImage(&buf, pmap, SkEncodedImageFormat::kPNG, 100)
? buf.detachAsData() : nullptr;
return SkEncodePixmap(pmap, SkEncodedImageFormat::kPNG, 100);
}
}
return nullptr;
}

View File

@ -79,3 +79,13 @@ bool SkEncoder::encodeRows(int numRows) {
return true;
}
sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality) {
SkDynamicMemoryWStream stream;
return SkEncodeImage(&stream, src, format, quality) ? stream.detachAsData() : nullptr;
}
sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality) {
SkPixmap pixmap;
return src.peekPixels(&pixmap) ? SkEncodePixmap(pixmap, format, quality) : nullptr;
}