2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "Resources.h"
14 #include "SkImageEncoderPriv.h"
15 #include "SkJpegEncoder.h"
16 #include "SkPngEncoder.h"
17 #include "SkUnPreMultiply.h"
18 #include "SkWebpEncoder.h"
22 static void make_opaque_256(SkBitmap* bitmap) {
23 GetResourceAsBitmap("mandrill_256.png", bitmap);
26 static void make_premul_256(SkBitmap* bitmap) {
28 GetResourceAsBitmap("yellow_rose.png", &tmp);
29 tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
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);
40 bitmap->setAlphaType(kUnpremul_SkAlphaType);
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,
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,
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,
62 static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
64 if (!bitmap.peekPixels(&src)) {
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;
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;
80 case SkEncodedImageFormat::kJPEG: {
81 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
82 return success ? buf.detachAsData() : nullptr;
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;
97 class EncodePlatformGM : public GM {
102 SkString onShortName() override {
103 return SkString("encode-platform");
106 SkISize onISize() override {
107 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
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);
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));
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);
125 canvas->translate(256.0f, 0.0f);
130 typedef GM INHERITED;
133 DEF_GM( return new EncodePlatformGM; )