blob: ca3cda1d73eccbb34593a96b72f2fca85616e1e0 [file] [log] [blame]
msarett36931c22016-08-16 15:11:24 -07001/*
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.h"
Hal Canarydb683012016-11-23 08:55:18 -07009
msarett36931c22016-08-16 15:11:24 -070010#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkData.h"
Hal Canarydb683012016-11-23 08:55:18 -070013#include "SkImageEncoderPriv.h"
msarett36931c22016-08-16 15:11:24 -070014#include "SkUnPreMultiply.h"
15
16namespace skiagm {
17
18static void make_opaque_256(SkBitmap* bitmap) {
19 GetResourceAsBitmap("mandrill_256.png", bitmap);
20}
21
22static void make_premul_256(SkBitmap* bitmap) {
23 SkBitmap tmp;
24 GetResourceAsBitmap("yellow_rose.png", &tmp);
25 tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
26 bitmap->lockPixels();
27}
28
29static void make_unpremul_256(SkBitmap* bitmap) {
30 make_premul_256(bitmap);
31 for (int y = 0; y < bitmap->height(); y++) {
32 for (int x = 0; x < bitmap->width(); x++) {
33 SkPMColor* pixel = bitmap->getAddr32(x, y);
34 *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel);
35 }
36 }
37 bitmap->setAlphaType(kUnpremul_SkAlphaType);
38}
39
msarett36931c22016-08-16 15:11:24 -070040#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -070041static SkEncodedImageFormat kTypes[] {
42 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
43 SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
msarett36931c22016-08-16 15:11:24 -070044};
45#elif defined(SK_BUILD_FOR_WIN)
46// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070047static SkEncodedImageFormat kTypes[] {
48 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
49 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070050};
51#else
52// Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070053static SkEncodedImageFormat kTypes[] {
54 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
55 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070056};
57#endif
58
Hal Canary1fcc4042016-11-30 17:07:59 -050059static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
60 SkAutoLockPixels autoLockPixels(bitmap);
61 SkPixmap src;
62 if (!bitmap.peekPixels(&src)) {
63 return nullptr;
64 }
65 SkDynamicMemoryWStream buf;
66 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
67 return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
68 #elif defined(SK_BUILD_FOR_WIN)
69 return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
70 #else
71 switch (type) {
72 case SkEncodedImageFormat::kPNG:
Matt Sarett84014f02017-01-10 11:28:54 -050073 return SkEncodeImageAsPNG(&buf, src, SkEncodeOptions()) ? buf.detachAsData()
74 : nullptr;
Hal Canary1fcc4042016-11-30 17:07:59 -050075 case SkEncodedImageFormat::kJPEG:
76 return SkEncodeImageAsJPEG(&buf, src, 100) ? buf.detachAsData() : nullptr;
77 case SkEncodedImageFormat::kWEBP:
78 return SkEncodeImageAsWEBP(&buf, src, 100) ? buf.detachAsData() : nullptr;
79 default:
80 SkASSERT(false);
81 return nullptr;
82 }
83 #endif
Hal Canarydb683012016-11-23 08:55:18 -070084}
msarett36931c22016-08-16 15:11:24 -070085
86class EncodePlatformGM : public GM {
87public:
88 EncodePlatformGM() {}
89
90protected:
91 SkString onShortName() override {
92 return SkString("encode-platform");
93 }
94
95 SkISize onISize() override {
96 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
97 }
98
99 void onDraw(SkCanvas* canvas) override {
100 SkBitmap opaqueBm, premulBm, unpremulBm;
101 make_opaque_256(&opaqueBm);
102 make_premul_256(&premulBm);
103 make_unpremul_256(&unpremulBm);
104
Hal Canarydb683012016-11-23 08:55:18 -0700105 for (SkEncodedImageFormat type : kTypes) {
Hal Canary1fcc4042016-11-30 17:07:59 -0500106 auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
107 auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
108 auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
msarett36931c22016-08-16 15:11:24 -0700109
110 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
111 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
112 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
113
114 canvas->translate(256.0f, 0.0f);
115 }
116 }
117
118private:
119 typedef GM INHERITED;
120};
121
122DEF_GM( return new EncodePlatformGM; )
123}