2016-08-16 22:11:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkData.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkEncodedImageFormat.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/encode/SkJpegEncoder.h"
|
|
|
|
#include "include/encode/SkPngEncoder.h"
|
|
|
|
#include "include/encode/SkWebpEncoder.h"
|
|
|
|
#include "tools/Resources.h"
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_WIN) \
|
|
|
|
|| defined(SK_ENABLE_NDK_IMAGES)
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/images/SkImageEncoderPriv.h"
|
|
|
|
#endif
|
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
namespace {
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
static const struct {
|
|
|
|
SkEncodedImageFormat format;
|
|
|
|
int quality;
|
|
|
|
} gRecs[] = {
|
2016-08-16 22:11:24 +00:00
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
2020-08-05 20:51:10 +00:00
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
|
|
|
{ SkEncodedImageFormat::kJPEG, 100},
|
|
|
|
{ SkEncodedImageFormat::kGIF, 100},
|
|
|
|
{ SkEncodedImageFormat::kBMP, 100},
|
|
|
|
{ SkEncodedImageFormat::kICO, 100},
|
2016-08-16 22:11:24 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2020-08-05 20:51:10 +00:00
|
|
|
// Our WIC encoder does not support GIF, BMP, or ICO.
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
|
|
|
{ SkEncodedImageFormat::kJPEG, 100},
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
2016-08-16 22:11:24 +00:00
|
|
|
#else
|
2020-08-05 20:51:10 +00:00
|
|
|
// We don't support GIF, BMP, or ICO. This applies to both NDK and SkEncoder.
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
|
|
|
{ SkEncodedImageFormat::kJPEG, 100},
|
|
|
|
{ SkEncodedImageFormat::kWEBP, 100}, // Lossless
|
|
|
|
{ SkEncodedImageFormat::kWEBP, 80}, // Lossy
|
|
|
|
{ SkEncodedImageFormat::kPNG, 100},
|
2016-08-16 22:11:24 +00:00
|
|
|
#endif
|
2020-08-05 20:51:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap, int quality) {
|
2016-11-30 22:07:59 +00:00
|
|
|
SkPixmap src;
|
|
|
|
if (!bitmap.peekPixels(&src)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkDynamicMemoryWStream buf;
|
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
|
|
|
return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
|
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2020-08-05 20:51:10 +00:00
|
|
|
return SkEncodeImageWithWIC(&buf, src, type, quality) ? buf.detachAsData() : nullptr;
|
|
|
|
#elif defined(SK_ENABLE_NDK_IMAGES)
|
|
|
|
return SkEncodeImageWithNDK(&buf, src, type, quality) ? buf.detachAsData() : nullptr;
|
2016-11-30 22:07:59 +00:00
|
|
|
#else
|
|
|
|
switch (type) {
|
2017-05-05 15:13:26 +00:00
|
|
|
case SkEncodedImageFormat::kPNG: {
|
2018-07-12 18:44:27 +00:00
|
|
|
bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options());
|
2017-05-05 15:13:26 +00:00
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2017-05-02 20:04:56 +00:00
|
|
|
case SkEncodedImageFormat::kJPEG: {
|
|
|
|
bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
|
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2017-05-05 18:02:13 +00:00
|
|
|
case SkEncodedImageFormat::kWEBP: {
|
2018-07-12 18:44:27 +00:00
|
|
|
bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options());
|
2017-05-05 18:02:13 +00:00
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2016-11-30 22:07:59 +00:00
|
|
|
default:
|
2020-08-05 20:51:10 +00:00
|
|
|
SkUNREACHABLE;
|
2016-11-30 22:07:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
2016-11-23 15:55:18 +00:00
|
|
|
}
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
2016-08-16 22:11:24 +00:00
|
|
|
class EncodePlatformGM : public GM {
|
|
|
|
public:
|
|
|
|
EncodePlatformGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("encode-platform");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2020-08-05 20:51:10 +00:00
|
|
|
return SkISize::Make(256 * SK_ARRAY_COUNT(gRecs), 256 * 3);
|
2016-08-16 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2016-08-16 22:11:24 +00:00
|
|
|
SkBitmap opaqueBm, premulBm, unpremulBm;
|
2018-11-26 20:34:12 +00:00
|
|
|
|
|
|
|
if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Could not load images/mandrill_256.png.png. "
|
|
|
|
"Did you forget to set the resourcePath?";
|
|
|
|
return DrawResult::kFail;
|
2018-11-26 20:34:12 +00:00
|
|
|
}
|
|
|
|
SkBitmap tmp;
|
|
|
|
if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Could not load images/yellow_rose.png. "
|
|
|
|
"Did you forget to set the resourcePath?";
|
|
|
|
return DrawResult::kFail;
|
2018-11-26 20:34:12 +00:00
|
|
|
}
|
|
|
|
tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
|
|
|
|
tmp.reset();
|
|
|
|
unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
|
|
|
|
SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2020-08-05 20:51:10 +00:00
|
|
|
for (const auto& rec : gRecs) {
|
|
|
|
auto fmt = rec.format; int q = rec.quality;
|
|
|
|
auto opaqueImage = SkImage::MakeFromEncoded(encode_data(fmt, opaqueBm, q));
|
|
|
|
auto premulImage = SkImage::MakeFromEncoded(encode_data(fmt, premulBm, q));
|
|
|
|
auto unpremulImage = SkImage::MakeFromEncoded(encode_data(fmt, unpremulBm, q));
|
2016-08-16 22:11:24 +00:00
|
|
|
|
|
|
|
canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
|
|
|
|
canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
|
|
|
|
canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
|
|
|
|
|
|
|
|
canvas->translate(256.0f, 0.0f);
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2016-08-16 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2016-08-16 22:11:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new EncodePlatformGM; )
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|