skia2/gm/encode-alpha-jpeg.cpp
Brian Osman b62f50cf76 Replace nearly all kRespect with kIgnore
- Encoders and decoders always assume kIgnore.
- They are less opinionated about F16 and color space,
  we just trust the color space that's passed in, and
  put that directly in the image (no sRGB encoding).
- SkBitmap and SkPixmap read/write pixels functions were
  defaulting to kResepct, those are now always kIgnore.
- Many other bits of plumbing are simplified, and I
  added a default of kIgnore to SkImage::makeColorSpace,
  so we can phase out that argument entirely.
- Still need to add defaults to other public APIs that
  take SkTransferFunctionBehavior.

- This makes gold think that we've dramatically changed
  the contents of all F16 images, but that's because
  it doesn't understand the (now linear) color space
  that's embedded. Once we triage them all once, they
  will work fine (and they'll look perfect in the browser).

Bug: skia:
Change-Id: I62fa090f96cae1b67d181ce14bd91f34ff2ed747
Reviewed-on: https://skia-review.googlesource.com/140570
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2018-07-12 20:54:14 +00:00

98 lines
3.5 KiB
C++

/*
* Copyright 2017 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 "SkImage.h"
#include "SkImageInfoPriv.h"
#include "SkJpegEncoder.h"
#include "Resources.h"
namespace skiagm {
static inline void read_into_pixmap(SkPixmap* dst, SkImageInfo dstInfo, void* dstPixels,
sk_sp<SkImage> src) {
dst->reset(dstInfo, dstPixels, dstInfo.minRowBytes());
src->readPixels(*dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint);
}
static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src,
SkJpegEncoder::AlphaOption alphaOption) {
SkDynamicMemoryWStream dst;
SkJpegEncoder::Options options;
options.fAlphaOption = alphaOption;
SkJpegEncoder::Encode(&dst, src, options);
return SkImage::MakeFromEncoded(dst.detachAsData());
}
class EncodeJpegAlphaOptsGM : public GM {
public:
EncodeJpegAlphaOptsGM() {}
protected:
SkString onShortName() override {
return SkString("encode-alpha-jpeg");
}
SkISize onISize() override {
return SkISize::Make(400, 200);
}
void onDraw(SkCanvas* canvas) override {
sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png");
fStorage.reset(srcImg->width() * srcImg->height() *
SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
SkPixmap src;
SkImageInfo info = SkImageInfo::MakeN32Premul(srcImg->width(), srcImg->height(),
canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
read_into_pixmap(&src, info, fStorage.get(), srcImg);
// Encode 8888 premul.
auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
canvas->drawImage(img0, 0.0f, 0.0f);
canvas->drawImage(img1, 0.0f, 100.0f);
// Encode 8888 unpremul
info = info.makeAlphaType(kUnpremul_SkAlphaType);
read_into_pixmap(&src, info, fStorage.get(), srcImg);
img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
canvas->drawImage(img0, 100.0f, 0.0f);
canvas->drawImage(img1, 100.0f, 100.0f);
if (canvas->imageInfo().colorSpace()) {
// Encode F16 premul
info = SkImageInfo::Make(srcImg->width(), srcImg->height(), kRGBA_F16_SkColorType,
kPremul_SkAlphaType, SkColorSpace::MakeSRGBLinear());
read_into_pixmap(&src, info, fStorage.get(), srcImg);
img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
canvas->drawImage(img0, 200.0f, 0.0f);
canvas->drawImage(img1, 200.0f, 100.0f);
// Encode F16 unpremul
info = info.makeAlphaType(kUnpremul_SkAlphaType);
read_into_pixmap(&src, info, fStorage.get(), srcImg);
img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
canvas->drawImage(img0, 300.0f, 0.0f);
canvas->drawImage(img1, 300.0f, 100.0f);
}
}
private:
SkAutoTMalloc<uint8_t> fStorage;
typedef GM INHERITED;
};
DEF_GM( return new EncodeJpegAlphaOptsGM; )
};