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-01-10 16:28:54 +00:00
|
|
|
#include "SkPM4f.h"
|
|
|
|
#include "SkSRGB.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;
|
|
|
|
|
2017-01-19 22:14:07 +00:00
|
|
|
sk_sp<SkColorSpace> fix_for_colortype(sk_sp<SkColorSpace> colorSpace, SkColorType colorType) {
|
|
|
|
if (kRGBA_F16_SkColorType == colorType) {
|
|
|
|
if (!colorSpace) {
|
2017-02-07 18:56:11 +00:00
|
|
|
return SkColorSpace::MakeSRGBLinear();
|
2017-01-19 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:09:31 +00:00
|
|
|
return colorSpace->makeLinearGamma();
|
2017-01-19 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return colorSpace;
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:28:54 +00:00
|
|
|
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);
|
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)
|
2017-01-19 22:14:07 +00:00
|
|
|
.makeColorSpace(fix_for_colortype(colorSpace, colorType));
|
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
|
|
|
|
2017-05-05 15:13:26 +00:00
|
|
|
SkPngEncoder::Options pngOptions;
|
2017-05-05 18:02:13 +00:00
|
|
|
SkWebpEncoder::Options webpOptions;
|
2017-07-11 15:59:48 +00:00
|
|
|
SkTransferFunctionBehavior behavior = bitmap.colorSpace()
|
|
|
|
? SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
|
|
|
|
pngOptions.fUnpremulBehavior = behavior;
|
|
|
|
webpOptions.fUnpremulBehavior = behavior;
|
2017-01-23 17:28:02 +00:00
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case SkEncodedImageFormat::kPNG:
|
2017-05-05 15:13:26 +00:00
|
|
|
SkAssertResult(SkPngEncoder::Encode(&buf, src, pngOptions));
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kWEBP:
|
2017-05-05 18:02:13 +00:00
|
|
|
SkAssertResult(SkWebpEncoder::Encode(&buf, src, webpOptions));
|
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
|
|
|
}
|