skia2/include/core/SkImageEncoder.h
Hal Canary db6830162e SkImageEncoder: simplify API
(re-land 248ff02 & 2cb6cb7, with changes)

  - Hide SkImageEncoder class in private header.
  - SkImageEncoder::Type becomes SkEncodedImageFormat
  - SkEncodedFormat becomes SkEncodedImageFormat
  - SkImageEncoder static functions replaced with
    single function EncodeImage()
  - utility wrappers for EncodeImage() are in
    sk_tool_utils.h

TODO: remove link-time registration mechanism.
TODO: clean up clients use of API and flip the flag.
TODO: implement EncodeImage() in chromeium/skia/ext

Change-Id: I47d451e50be4d5c6c130869c7fa7c2857243d9f0
Reviewed-on: https://skia-review.googlesource.com/4909
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-on: https://skia-review.googlesource.com/5186
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
2016-11-23 16:40:32 +00:00

143 lines
4.9 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkImageEncoder_DEFINED
#define SkImageEncoder_DEFINED
#include "SkBitmap.h"
#include "SkEncodedImageFormat.h"
#include "SkPixelSerializer.h"
#include "SkStream.h"
#include "SkTRegistry.h"
/**
* 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
////////////////////////////////////////////////////////////////////////////////
class SkImageEncoder {
public:
enum Type {
#ifdef GOOGLE3
kUnknown_Type = (int)SkEncodedImageFormat::kUnknown,
#endif
kBMP_Type = (int)SkEncodedImageFormat::kBMP,
kGIF_Type = (int)SkEncodedImageFormat::kGIF,
kICO_Type = (int)SkEncodedImageFormat::kICO,
kJPEG_Type = (int)SkEncodedImageFormat::kJPEG,
kPNG_Type = (int)SkEncodedImageFormat::kPNG,
kWBMP_Type = (int)SkEncodedImageFormat::kWBMP,
kWEBP_Type = (int)SkEncodedImageFormat::kWEBP,
kKTX_Type = (int)SkEncodedImageFormat::kKTX,
};
static SkImageEncoder* Create(Type);
virtual ~SkImageEncoder() {}
/* Quality ranges from 0..100 */
enum {
kDefaultQuality = 80
};
/**
* 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.
*/
SkData* encodeData(const SkBitmap& bm, int quality) {
SkDynamicMemoryWStream buffer;
return this->encodeStream(&buffer, bm, quality)
? buffer.detachAsData().release()
: nullptr;
}
/**
* Encode bitmap 'bm' in the desired format, writing results to
* file 'file', at quality level 'quality' (which can be in range
* 0-100). Returns false on failure.
*/
bool encodeFile(const char path[], const SkBitmap& bm, int quality) {
SkFILEWStream file(path);
return this->encodeStream(&file, bm, quality);
}
/**
* Encode bitmap 'bm' in the desired format, writing results to
* stream 'stream', at quality level 'quality' (which can be in
* range 0-100). Returns false on failure.
*/
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);
}
protected:
/**
* 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;
};
#endif // SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS
#endif // SkImageEncoder_DEFINED