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"
|
2017-01-19 22:14:07 +00:00
|
|
|
#include "SkColorSpace_Base.h"
|
2017-01-10 16:28:54 +00:00
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkImageEncoderPriv.h"
|
|
|
|
#include "SkPM4f.h"
|
|
|
|
#include "SkSRGB.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static const int imageWidth = 128;
|
|
|
|
static const int imageHeight = 128;
|
|
|
|
|
|
|
|
static inline int div_round_up(int a, int b) {
|
|
|
|
return (a + b - 1) / b;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return as_CSB(colorSpace)->makeLinearGamma();
|
|
|
|
}
|
|
|
|
|
|
|
|
return colorSpace;
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:28:54 +00:00
|
|
|
static void make_index8(SkBitmap* bitmap, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
|
|
|
|
const SkColor colors[] = {
|
|
|
|
0x800000FF, 0x8000FF00, 0x80FF0000, 0x80FFFF00,
|
|
|
|
};
|
|
|
|
|
|
|
|
auto toPMColor = [alphaType, colorSpace](SkColor color) {
|
2017-01-23 17:28:02 +00:00
|
|
|
// In the opaque/unpremul case, just convert to SkPMColor ordering.
|
|
|
|
if (kPremul_SkAlphaType != alphaType) {
|
2017-01-10 16:28:54 +00:00
|
|
|
return SkSwizzle_BGRA_to_PMColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linear premultiply.
|
|
|
|
if (colorSpace) {
|
|
|
|
uint32_t result;
|
|
|
|
Sk4f pmFloat = SkColor4f::FromColor(color).premul().to4f_pmorder();
|
|
|
|
SkNx_cast<uint8_t>(sk_linear_to_srgb_needs_trunc(pmFloat)).store(&result);
|
|
|
|
result = (result & 0x00FFFFFF) | (color & 0xFF000000);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy premultiply.
|
|
|
|
return SkPreMultiplyColor(color);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note that these are not necessarily premultiplied, but they are platform byte ordering.
|
|
|
|
SkPMColor pmColors[SK_ARRAY_COUNT(colors)];
|
|
|
|
for (int i = 0; i < (int) SK_ARRAY_COUNT(colors); i++) {
|
|
|
|
pmColors[i] = toPMColor(colors[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(imageWidth, imageHeight, kIndex_8_SkColorType,
|
|
|
|
alphaType, colorSpace);
|
2017-04-03 18:41:44 +00:00
|
|
|
bitmap->allocPixels(info, SkColorTable::Make(pmColors, SK_ARRAY_COUNT(pmColors)));
|
2017-01-10 16:28:54 +00:00
|
|
|
for (int y = 0; y < imageHeight; y++) {
|
|
|
|
for (int x = 0; x < imageWidth; x++) {
|
|
|
|
*bitmap->getAddr8(x, y) = (x / div_round_up(imageWidth, 2)) +
|
|
|
|
(y / div_round_up(imageHeight, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 kIndex_8_SkColorType:
|
|
|
|
make_index8(bitmap, alphaType, colorSpace);
|
|
|
|
return;
|
|
|
|
case kGray_8_SkColorType:
|
|
|
|
resource = "grayscale.jpg";
|
|
|
|
alphaType = kOpaque_SkAlphaType;
|
|
|
|
break;
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
resource = "color_wheel.jpg";
|
|
|
|
alphaType = kOpaque_SkAlphaType;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
resource = (kOpaque_SkAlphaType == alphaType) ? "color_wheel.jpg"
|
|
|
|
: "color_wheel.png";
|
|
|
|
break;
|
2017-01-23 17:28:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 22:14:07 +00:00
|
|
|
sk_sp<SkData> data = GetResourceAsData(resource);
|
2017-01-10 16:28:54 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
|
|
|
|
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
|
|
|
SkAutoLockPixels autoLockPixels(bitmap);
|
|
|
|
SkPixmap src;
|
|
|
|
if (!bitmap.peekPixels(&src)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkDynamicMemoryWStream buf;
|
2017-01-23 17:28:02 +00:00
|
|
|
|
2017-01-10 16:28:54 +00:00
|
|
|
SkEncodeOptions options;
|
|
|
|
if (bitmap.colorSpace()) {
|
2017-03-23 19:32:25 +00:00
|
|
|
options.fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
|
2017-01-10 16:28:54 +00:00
|
|
|
}
|
2017-01-23 17:28:02 +00:00
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case SkEncodedImageFormat::kPNG:
|
2017-01-27 23:16:40 +00:00
|
|
|
SkAssertResult(SkEncodeImageAsPNG(&buf, src, options));
|
2017-01-23 17:28:02 +00:00
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kWEBP:
|
2017-01-27 23:16:40 +00:00
|
|
|
SkAssertResult(SkEncodeImageAsWEBP(&buf, src, options));
|
|
|
|
break;
|
|
|
|
case SkEncodedImageFormat::kJPEG:
|
|
|
|
SkAssertResult(SkEncodeImageAsJPEG(&buf, src, 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-01-23 17:28:02 +00:00
|
|
|
kN32_SkColorType, kRGBA_F16_SkColorType, kIndex_8_SkColorType, kGray_8_SkColorType,
|
2017-01-27 23:16:40 +00:00
|
|
|
kRGB_565_SkColorType,
|
2017-01-19 22:14:07 +00:00
|
|
|
};
|
|
|
|
const SkAlphaType alphaTypes[] = {
|
|
|
|
kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
|
|
|
|
};
|
2017-01-10 16:28:54 +00:00
|
|
|
const sk_sp<SkColorSpace> colorSpaces[] = {
|
2017-02-07 18:56:11 +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
|
|
|
}
|