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.h"
9
10 #include "Resources.h"
11 #include "SkCanvas.h"
12 #include "SkData.h"
13 #include "SkImage.h"
14 #include "SkImageEncoderPriv.h"
15 #include "SkJpegEncoder.h"
16 #include "SkPngEncoder.h"
17 #include "SkUnPreMultiply.h"
18 #include "SkWebpEncoder.h"
19
20 namespace skiagm {
21
make_opaque_256(SkBitmap * bitmap)22 static void make_opaque_256(SkBitmap* bitmap) {
23 GetResourceAsBitmap("images/mandrill_256.png", bitmap);
24 }
25
make_premul_256(SkBitmap * bitmap)26 static void make_premul_256(SkBitmap* bitmap) {
27 SkBitmap tmp;
28 GetResourceAsBitmap("images/yellow_rose.png", &tmp);
29 tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
30 }
31
make_unpremul_256(SkBitmap * bitmap)32 static 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
43 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
44 static SkEncodedImageFormat kTypes[] {
45 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
46 SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
47 };
48 #elif defined(SK_BUILD_FOR_WIN)
49 // Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
50 static SkEncodedImageFormat kTypes[] {
51 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
52 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
53 };
54 #else
55 // Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO.
56 static SkEncodedImageFormat kTypes[] {
57 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
58 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
59 };
60 #endif
61
encode_data(SkEncodedImageFormat type,const SkBitmap & bitmap)62 static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
63 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) {
74 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 }
80 case SkEncodedImageFormat::kJPEG: {
81 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
82 return success ? buf.detachAsData() : nullptr;
83 }
84 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 }
90 default:
91 SkASSERT(false);
92 return nullptr;
93 }
94 #endif
95 }
96
97 class EncodePlatformGM : public GM {
98 public:
EncodePlatformGM()99 EncodePlatformGM() {}
100
101 protected:
onShortName()102 SkString onShortName() override {
103 return SkString("encode-platform");
104 }
105
onISize()106 SkISize onISize() override {
107 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
108 }
109
onDraw(SkCanvas * canvas)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
116 for (SkEncodedImageFormat type : kTypes) {
117 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));
120
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
129 private:
130 typedef GM INHERITED;
131 };
132
133 DEF_GM( return new EncodePlatformGM; )
134 }
135