2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-12-19 20:26:07 +00:00
|
|
|
|
2009-01-06 20:16:26 +00:00
|
|
|
#ifndef SkImageEncoder_DEFINED
|
|
|
|
#define SkImageEncoder_DEFINED
|
|
|
|
|
2016-11-22 16:03:03 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkEncodedImageFormat.h"
|
|
|
|
#include "SkPixelSerializer.h"
|
|
|
|
#include "SkStream.h"
|
2009-01-06 20:16:26 +00:00
|
|
|
|
2016-11-22 16:03:03 +00:00
|
|
|
/**
|
|
|
|
* Encode SkPixmap in the given binary image format.
|
|
|
|
*
|
|
|
|
* @param dst results are written to this stream.
|
|
|
|
* @param src source pixels.
|
|
|
|
* @param format image format, not all formats are supported.
|
|
|
|
* @param quality range from 0-100, not all formats respect quality.
|
|
|
|
*
|
|
|
|
* @return false iff input is bad or format is unsupported.
|
|
|
|
*
|
|
|
|
* Will always return false if Skia is compiled without image
|
|
|
|
* encoders.
|
|
|
|
*
|
|
|
|
* For examples of encoding an image to a file or to a block of memory,
|
|
|
|
* see tools/sk_tool_utils.h.
|
|
|
|
*/
|
|
|
|
SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
|
|
|
|
SkEncodedImageFormat format, int quality);
|
|
|
|
/**
|
|
|
|
* The following helper function wraps SkEncodeImage().
|
|
|
|
*/
|
|
|
|
inline bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFormat f, int q) {
|
|
|
|
SkAutoLockPixels autoLockPixels(src);
|
|
|
|
SkPixmap pixmap;
|
|
|
|
return src.peekPixels(&pixmap) && SkEncodeImage(dst, pixmap, f, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-01-06 20:16:26 +00:00
|
|
|
|
|
|
|
class SkImageEncoder {
|
|
|
|
public:
|
|
|
|
enum Type {
|
2016-11-22 16:03:03 +00:00
|
|
|
kBMP_Type = (uint8_t)SkEncodedImageFormat::kBMP,
|
|
|
|
kGIF_Type = (uint8_t)SkEncodedImageFormat::kGIF,
|
|
|
|
kICO_Type = (uint8_t)SkEncodedImageFormat::kICO,
|
|
|
|
kJPEG_Type = (uint8_t)SkEncodedImageFormat::kJPEG,
|
|
|
|
kPNG_Type = (uint8_t)SkEncodedImageFormat::kPNG,
|
|
|
|
kWBMP_Type = (uint8_t)SkEncodedImageFormat::kWBMP,
|
|
|
|
kWEBP_Type = (uint8_t)SkEncodedImageFormat::kWEBP,
|
|
|
|
kKTX_Type = (uint8_t)SkEncodedImageFormat::kKTX,
|
2009-01-06 20:16:26 +00:00
|
|
|
};
|
|
|
|
static SkImageEncoder* Create(Type);
|
|
|
|
|
2016-11-22 16:03:03 +00:00
|
|
|
virtual ~SkImageEncoder() {}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2009-01-06 20:16:26 +00:00
|
|
|
/* Quality ranges from 0..100 */
|
|
|
|
enum {
|
|
|
|
kDefaultQuality = 80
|
|
|
|
};
|
|
|
|
|
2013-05-20 16:33:41 +00:00
|
|
|
/**
|
|
|
|
* Encode bitmap 'bm', returning the results in an SkData, at quality level
|
|
|
|
* 'quality' (which can be in range 0-100). If the bitmap cannot be
|
|
|
|
* encoded, return null. On success, the caller is responsible for
|
|
|
|
* calling unref() on the data when they are finished.
|
|
|
|
*/
|
2016-11-22 16:03:03 +00:00
|
|
|
SkData* encodeData(const SkBitmap& bm, int quality) {
|
|
|
|
SkDynamicMemoryWStream buffer;
|
|
|
|
return this->encodeStream(&buffer, bm, quality)
|
|
|
|
? buffer.detachAsData().release()
|
|
|
|
: nullptr;
|
|
|
|
}
|
2013-05-20 16:33:41 +00:00
|
|
|
|
2012-11-16 18:44:18 +00:00
|
|
|
/**
|
|
|
|
* Encode bitmap 'bm' in the desired format, writing results to
|
|
|
|
* file 'file', at quality level 'quality' (which can be in range
|
2013-05-20 16:33:41 +00:00
|
|
|
* 0-100). Returns false on failure.
|
2012-11-16 18:44:18 +00:00
|
|
|
*/
|
2016-11-22 16:03:03 +00:00
|
|
|
bool encodeFile(const char path[], const SkBitmap& bm, int quality) {
|
|
|
|
SkFILEWStream file(path);
|
|
|
|
return this->encodeStream(&file, bm, quality);
|
|
|
|
}
|
2012-11-16 18:44:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode bitmap 'bm' in the desired format, writing results to
|
|
|
|
* stream 'stream', at quality level 'quality' (which can be in
|
2013-05-20 16:33:41 +00:00
|
|
|
* range 0-100). Returns false on failure.
|
2012-11-16 18:44:18 +00:00
|
|
|
*/
|
2016-11-22 16:03:03 +00:00
|
|
|
bool encodeStream(SkWStream* dst, const SkBitmap& src, int quality) {
|
|
|
|
return this->onEncode(dst, src, SkMin32(100, SkMax32(0, quality)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkData* EncodeData(const SkImageInfo& info, const void* pixels, size_t rowBytes,
|
|
|
|
Type t, int quality) {
|
|
|
|
SkPixmap pixmap(info, pixels, rowBytes, nullptr);
|
|
|
|
return SkImageEncoder::EncodeData(pixmap, t, quality);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkData* EncodeData(const SkBitmap& src, Type t, int quality) {
|
|
|
|
SkDynamicMemoryWStream buf;
|
|
|
|
return SkEncodeImage(&buf, src, (SkEncodedImageFormat)t, quality)
|
|
|
|
? buf.detachAsData().release() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkData* EncodeData(const SkPixmap& pixmap, Type t, int quality) {
|
|
|
|
SkDynamicMemoryWStream buf;
|
|
|
|
return SkEncodeImage(&buf, pixmap, (SkEncodedImageFormat)t, quality)
|
|
|
|
? buf.detachAsData().release() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool EncodeFile(const char path[], const SkBitmap& src, Type t, int quality) {
|
|
|
|
SkFILEWStream file(path);
|
|
|
|
return SkEncodeImage(&file, src, (SkEncodedImageFormat)t, quality);
|
|
|
|
}
|
|
|
|
static bool EncodeStream(SkWStream* dst, const SkBitmap& bm, Type t, int quality) {
|
|
|
|
return SkEncodeImage(dst, bm, (SkEncodedImageFormat)t, quality);
|
|
|
|
}
|
2015-12-10 20:40:23 +00:00
|
|
|
|
2009-01-06 20:16:26 +00:00
|
|
|
protected:
|
2012-11-16 18:44:18 +00:00
|
|
|
/**
|
|
|
|
* Encode bitmap 'bm' in the desired format, writing results to
|
|
|
|
* stream 'stream', at quality level 'quality' (which can be in
|
|
|
|
* range 0-100).
|
|
|
|
*
|
|
|
|
* This must be overridden by each SkImageEncoder implementation.
|
|
|
|
*/
|
|
|
|
virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
|
2009-01-06 20:16:26 +00:00
|
|
|
};
|
|
|
|
|
2016-11-22 16:03:03 +00:00
|
|
|
#endif // SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS
|
|
|
|
#endif // SkImageEncoder_DEFINED
|