2017-01-10 16:28:54 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkData.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkImage.h"
|
2017-01-10 16:28:54 +00:00
|
|
|
#include "SkImageEncoderPriv.h"
|
2017-05-02 20:04:56 +00:00
|
|
|
#include "SkJpegEncoder.h"
|
2017-05-05 15:13:26 +00:00
|
|
|
#include "SkPngEncoder.h"
|
2017-05-05 18:02:13 +00:00
|
|
|
#include "SkWebpEncoder.h"
|
2017-01-10 16:28:54 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static const int imageWidth = 128;
|
|
|
|
static const int imageHeight = 128;
|
|
|
|
|
|
|
|
static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
|
|
|
|
sk_sp<SkColorSpace> colorSpace) {
|
2017-01-27 23:16:40 +00:00
|
|
|
const char* resource;
|
|
|
|
switch (colorType) {
|
|
|
|
case kGray_8_SkColorType:
|
2017-12-08 15:21:31 +00:00
|
|
|
resource = "images/grayscale.jpg";
|
2017-01-27 23:16:40 +00:00
|
|
|
alphaType = kOpaque_SkAlphaType;
|
|
|
|
break;
|
|
|
|
case kRGB_565_SkColorType:
|
2017-12-08 15:21:31 +00:00
|
|
|
resource = "images/color_wheel.jpg";
|
2017-01-27 23:16:40 +00:00
|
|
|
alphaType = kOpaque_SkAlphaType;
|
|
|
|
break;
|
|
|
|
default:
|
2017-12-08 15:21:31 +00:00
|
|
|
resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
|
|
|
|
: "images/color_wheel.png";
|
2017-01-27 23:16:40 +00:00
|
|
|
break;
|
2017-01-23 17:28:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 22:14:07 +00:00
|
|
|
sk_sp<SkData> data = GetResourceAsData(resource);
|
2018-11-26 20:34:12 +00:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
|
2017-01-10 16:28:54 +00:00
|
|
|
SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
|
|
|
|
.makeAlphaType(alphaType)
|
2018-08-27 19:16:02 +00:00
|
|
|
.makeColorSpace(colorSpace);
|
2017-01-10 16:28:54 +00:00
|
|
|
bitmap->allocPixels(dstInfo);
|
|
|
|
codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
|
|
|
|
}
|
|
|
|
|
2017-01-23 17:28:02 +00:00
|
|
|
static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) {
|
2017-01-10 16:28:54 +00:00
|
|
|
SkPixmap src;
|
|
|
|
if (!bitmap.peekPixels(&src)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkDynamicMemoryWStream buf;
|
2017-01-23 17:28:02 +00:00
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case SkEncodedImageFormat::kPNG:
|
2018-07-12 18:44:27 +00:00
|
|
|
SkAssertResult(SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()));
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kWEBP:
|
2018-07-12 18:44:27 +00:00
|
|
|
SkAssertResult(SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()));
|
2017-01-27 23:16:40 +00:00
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kJPEG:
|
2017-05-02 20:04:56 +00:00
|
|
|
SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-10 16:28:54 +00:00
|
|
|
return buf.detachAsData();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EncodeSRGBGM : public GM {
|
|
|
|
public:
|
2017-01-23 17:28:02 +00:00
|
|
|
EncodeSRGBGM(SkEncodedImageFormat format)
|
|
|
|
: fEncodedFormat(format)
|
|
|
|
{}
|
2017-01-10 16:28:54 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
2017-01-23 17:28:02 +00:00
|
|
|
const char* format = nullptr;
|
|
|
|
switch (fEncodedFormat) {
|
|
|
|
case SkEncodedImageFormat::kPNG:
|
2017-01-27 23:16:40 +00:00
|
|
|
format = "png";
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kWEBP:
|
2017-01-27 23:16:40 +00:00
|
|
|
format = "webp";
|
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kJPEG:
|
|
|
|
format = "jpg";
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-27 23:16:40 +00:00
|
|
|
return SkStringPrintf("encode-srgb-%s", format);
|
2017-01-10 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2017-01-27 23:16:40 +00:00
|
|
|
return SkISize::Make(imageWidth * 2, imageHeight * 15);
|
2017-01-10 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2017-01-19 22:14:07 +00:00
|
|
|
const SkColorType colorTypes[] = {
|
2017-07-12 19:10:28 +00:00
|
|
|
kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType,
|
2017-01-19 22:14:07 +00:00
|
|
|
};
|
|
|
|
const SkAlphaType alphaTypes[] = {
|
2017-07-12 19:10:28 +00:00
|
|
|
kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
|
2017-01-19 22:14:07 +00:00
|
|
|
};
|
2017-01-10 16:28:54 +00:00
|
|
|
const sk_sp<SkColorSpace> colorSpaces[] = {
|
2017-07-12 19:10:28 +00:00
|
|
|
nullptr, SkColorSpace::MakeSRGB(),
|
2017-01-10 16:28:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
for (SkColorType colorType : colorTypes) {
|
|
|
|
for (SkAlphaType alphaType : alphaTypes) {
|
|
|
|
canvas->save();
|
|
|
|
for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
|
|
|
|
make(&bitmap, colorType, alphaType, colorSpace);
|
2017-01-23 17:28:02 +00:00
|
|
|
auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat));
|
2017-01-10 16:28:54 +00:00
|
|
|
canvas->drawImage(image.get(), 0.0f, 0.0f);
|
|
|
|
canvas->translate((float) imageWidth, 0.0f);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0.0f, (float) imageHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-23 17:28:02 +00:00
|
|
|
SkEncodedImageFormat fEncodedFormat;
|
|
|
|
|
2017-01-10 16:28:54 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2017-01-23 17:28:02 +00:00
|
|
|
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
|
|
|
|
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
|
2017-01-27 23:16:40 +00:00
|
|
|
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
|
2017-01-10 16:28:54 +00:00
|
|
|
}
|