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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/codec/SkCodec.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"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#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/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"
|
2017-01-10 16:28:54 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <memory>
|
|
|
|
|
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();
|
2020-07-31 00:24:57 +00:00
|
|
|
for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
|
2017-01-10 16:28:54 +00:00
|
|
|
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;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2017-01-10 16:28:54 +00:00
|
|
|
};
|
|
|
|
|
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); )
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|