blob: dd7ca6e8a8f77ae05e1724c8019afeb779cc1771 [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"
Florin Malitaab244f02017-05-03 19:16:58 +000013#include "SkImage.h"
Hal Canarydb683012016-11-23 08:55:18 -070014#include "SkImageEncoderPriv.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040015#include "SkJpegEncoder.h"
Matt Sarettc367d032017-05-05 11:13:26 -040016#include "SkPngEncoder.h"
msarett36931c22016-08-16 15:11:24 -070017#include "SkUnPreMultiply.h"
Matt Sarett04c37312017-05-05 14:02:13 -040018#include "SkWebpEncoder.h"
msarett36931c22016-08-16 15:11:24 -070019
20namespace skiagm {
21
22static void make_opaque_256(SkBitmap* bitmap) {
23 GetResourceAsBitmap("mandrill_256.png", bitmap);
24}
25
26static void make_premul_256(SkBitmap* bitmap) {
27 SkBitmap tmp;
28 GetResourceAsBitmap("yellow_rose.png", &tmp);
29 tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
msarett36931c22016-08-16 15:11:24 -070030}
31
32static void make_unpremul_256(SkBitmap* bitmap) {
33 make_premul_256(bitmap);
34 for (int y = 0; y < bitmap->height(); y++) {
35 for (int x = 0; x < bitmap->width(); x++) {
36 SkPMColor* pixel = bitmap->getAddr32(x, y);
37 *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel);
38 }
39 }
40 bitmap->setAlphaType(kUnpremul_SkAlphaType);
41}
42
msarett36931c22016-08-16 15:11:24 -070043#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -070044static SkEncodedImageFormat kTypes[] {
45 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
46 SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
msarett36931c22016-08-16 15:11:24 -070047};
48#elif defined(SK_BUILD_FOR_WIN)
49// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070050static SkEncodedImageFormat kTypes[] {
51 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
52 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070053};
54#else
55// 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 -070056static SkEncodedImageFormat kTypes[] {
57 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
58 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070059};
60#endif
61
Hal Canary1fcc4042016-11-30 17:07:59 -050062static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
Hal Canary1fcc4042016-11-30 17:07:59 -050063 SkPixmap src;
64 if (!bitmap.peekPixels(&src)) {
65 return nullptr;
66 }
67 SkDynamicMemoryWStream buf;
68 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
69 return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
70 #elif defined(SK_BUILD_FOR_WIN)
71 return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
72 #else
73 switch (type) {
Matt Sarettc367d032017-05-05 11:13:26 -040074 case SkEncodedImageFormat::kPNG: {
75 SkPngEncoder::Options options;
76 options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
77 bool success = SkPngEncoder::Encode(&buf, src, options);
78 return success ? buf.detachAsData() : nullptr;
79 }
Matt Sarett26b44df2017-05-02 16:04:56 -040080 case SkEncodedImageFormat::kJPEG: {
81 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
82 return success ? buf.detachAsData() : nullptr;
83 }
Matt Sarett04c37312017-05-05 14:02:13 -040084 case SkEncodedImageFormat::kWEBP: {
85 SkWebpEncoder::Options options;
86 options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
87 bool success = SkWebpEncoder::Encode(&buf, src, options);
88 return success ? buf.detachAsData() : nullptr;
89 }
Hal Canary1fcc4042016-11-30 17:07:59 -050090 default:
91 SkASSERT(false);
92 return nullptr;
93 }
94 #endif
Hal Canarydb683012016-11-23 08:55:18 -070095}
msarett36931c22016-08-16 15:11:24 -070096
97class EncodePlatformGM : public GM {
98public:
99 EncodePlatformGM() {}
100
101protected:
102 SkString onShortName() override {
103 return SkString("encode-platform");
104 }
105
106 SkISize onISize() override {
107 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
108 }
109
110 void onDraw(SkCanvas* canvas) override {
111 SkBitmap opaqueBm, premulBm, unpremulBm;
112 make_opaque_256(&opaqueBm);
113 make_premul_256(&premulBm);
114 make_unpremul_256(&unpremulBm);
115
Hal Canarydb683012016-11-23 08:55:18 -0700116 for (SkEncodedImageFormat type : kTypes) {
Hal Canary1fcc4042016-11-30 17:07:59 -0500117 auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
118 auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
119 auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
msarett36931c22016-08-16 15:11:24 -0700120
121 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
122 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
123 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
124
125 canvas->translate(256.0f, 0.0f);
126 }
127 }
128
129private:
130 typedef GM INHERITED;
131};
132
133DEF_GM( return new EncodePlatformGM; )
134}