• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkEncodedImageFormat.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/Resources.h"
26 
27 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_WIN)
28 #include "src/images/SkImageEncoderPriv.h"
29 #endif
30 
31 namespace skiagm {
32 
33 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
34 static SkEncodedImageFormat kTypes[] {
35         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
36         SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
37 };
38 #elif defined(SK_BUILD_FOR_WIN)
39 // Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
40 static SkEncodedImageFormat kTypes[] {
41         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
42         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
43 };
44 #else
45 // Use WEBP in place of GIF.  Use PNG two extra times.  We don't support GIF, BMP, or ICO.
46 static SkEncodedImageFormat kTypes[] {
47         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
48         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
49 };
50 #endif
51 
encode_data(SkEncodedImageFormat type,const SkBitmap & bitmap)52 static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
53     SkPixmap src;
54     if (!bitmap.peekPixels(&src)) {
55         return nullptr;
56     }
57     SkDynamicMemoryWStream buf;
58     #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
59         return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
60     #elif defined(SK_BUILD_FOR_WIN)
61         return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
62     #else
63         switch (type) {
64             case SkEncodedImageFormat::kPNG: {
65                 bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options());
66                 return success ? buf.detachAsData() : nullptr;
67             }
68             case SkEncodedImageFormat::kJPEG: {
69                 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
70                 return success ? buf.detachAsData() : nullptr;
71             }
72             case SkEncodedImageFormat::kWEBP: {
73                 bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options());
74                 return success ? buf.detachAsData() : nullptr;
75             }
76             default:
77                 SkASSERT(false);
78                 return nullptr;
79         }
80     #endif
81 }
82 
83 class EncodePlatformGM : public GM {
84 public:
EncodePlatformGM()85     EncodePlatformGM() {}
86 
87 protected:
onShortName()88     SkString onShortName() override {
89         return SkString("encode-platform");
90     }
91 
onISize()92     SkISize onISize() override {
93         return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
94     }
95 
onDraw(SkCanvas * canvas,SkString * errorMsg)96     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
97         SkBitmap opaqueBm, premulBm, unpremulBm;
98 
99         if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
100             *errorMsg = "Could not load images/mandrill_256.png.png. "
101                         "Did you forget to set the resourcePath?";
102             return DrawResult::kFail;
103         }
104         SkBitmap tmp;
105         if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
106             *errorMsg = "Could not load images/yellow_rose.png. "
107                         "Did you forget to set the resourcePath?";
108             return DrawResult::kFail;
109         }
110         tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
111         tmp.reset();
112         unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
113         SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
114 
115         for (SkEncodedImageFormat type : kTypes) {
116             auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
117             auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
118             auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
119 
120             canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
121             canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
122             canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
123 
124             canvas->translate(256.0f, 0.0f);
125         }
126         return DrawResult::kOk;
127     }
128 
129 private:
130     typedef GM INHERITED;
131 };
132 
133 DEF_GM( return new EncodePlatformGM; )
134 }
135