Debian Codegraph @

gm/encode_platform.cpp

raw
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm/gm.h"
9 #include "include/codec/SkEncodedImageFormat.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkStream.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypes.h"
22 #include "include/encode/SkJpegEncoder.h"
23 #include "include/encode/SkPngEncoder.h"
24 #include "include/encode/SkWebpEncoder.h"
25 #include "tools/DecodeUtils.h"
26 #include "tools/Resources.h"
27
28 namespace {
29
30 static const struct {
31 SkEncodedImageFormat format;
32 int quality;
33 } gRecs[] = {
34 // We don't support GIF, BMP, or ICO. This applies to both NDK and SkEncoder.
35 { SkEncodedImageFormat::kPNG, 100},
36 { SkEncodedImageFormat::kJPEG, 100},
37 { SkEncodedImageFormat::kWEBP, 100}, // Lossless
38 { SkEncodedImageFormat::kWEBP, 80}, // Lossy
39 { SkEncodedImageFormat::kPNG, 100},
40 };
41
42 } // anonymous namespace
43
44 static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap, int quality) {
45 SkPixmap src;
46 if (!bitmap.peekPixels(&src)) {
47 return nullptr;
48 }
49 switch (type) {
50 case SkEncodedImageFormat::kPNG:
51 return SkPngEncoder::Encode(src, {});
52 case SkEncodedImageFormat::kJPEG: {
53 SkJpegEncoder::Options opts;
54 opts.fQuality = quality;
55 return SkJpegEncoder::Encode(src, opts);
56 }
57 case SkEncodedImageFormat::kWEBP: {
58 SkWebpEncoder::Options opts;
59 opts.fQuality = quality;
60 return SkWebpEncoder::Encode(src, opts);
61 }
62 default:
63 SkUNREACHABLE;
64 }
65 }
66
67 namespace skiagm {
68
69 class EncodePlatformGM : public GM {
70 public:
71 EncodePlatformGM() {}
72
73 protected:
74 SkString getName() const override { return SkString("encode-platform"); }
75
76 SkISize getISize() override { return SkISize::Make(256 * std::size(gRecs), 256 * 3); }
77
78 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
79 SkBitmap opaqueBm, premulBm, unpremulBm;
80
81 if (!ToolUtils::GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
82 *errorMsg = "Could not load images/mandrill_256.png.png. "
83 "Did you forget to set the resourcePath?";
84 return DrawResult::kFail;
85 }
86 SkBitmap tmp;
87 if (!ToolUtils::GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
88 *errorMsg = "Could not load images/yellow_rose.png. "
89 "Did you forget to set the resourcePath?";
90 return DrawResult::kFail;
91 }
92 tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
93 tmp.reset();
94 unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
95 SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
96
97 for (const auto& rec : gRecs) {
98 auto fmt = rec.format; int q = rec.quality;
99 auto opaqueImage = SkImages::DeferredFromEncodedData(encode_data(fmt, opaqueBm, q));
100 auto premulImage = SkImages::DeferredFromEncodedData(encode_data(fmt, premulBm, q));
101 auto unpremulImage = SkImages::DeferredFromEncodedData(encode_data(fmt, unpremulBm, q));
102
103 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
104 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
105 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
106
107 canvas->translate(256.0f, 0.0f);
108 }
109 return DrawResult::kOk;
110 }
111
112 private:
113 using INHERITED = GM;
114 };
115
116 DEF_GM( return new EncodePlatformGM; )
117 } // namespace skiagm