Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/codec/SkCodec.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkColorSpace.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkData.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkEncodedImageFormat.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkPixmap.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkStream.h" |
| 21 | #include "include/core/SkString.h" |
| 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/encode/SkJpegEncoder.h" |
| 24 | #include "include/encode/SkPngEncoder.h" |
| 25 | #include "include/encode/SkWebpEncoder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "tools/Resources.h" |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 27 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 28 | #include <memory> |
| 29 | |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 30 | namespace skiagm { |
| 31 | |
| 32 | static const int imageWidth = 128; |
| 33 | static const int imageHeight = 128; |
| 34 | |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 35 | static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType, |
| 36 | sk_sp<SkColorSpace> colorSpace) { |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 37 | const char* resource; |
| 38 | switch (colorType) { |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 39 | case kGray_8_SkColorType: |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 40 | resource = "images/grayscale.jpg"; |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 41 | alphaType = kOpaque_SkAlphaType; |
| 42 | break; |
| 43 | case kRGB_565_SkColorType: |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 44 | resource = "images/color_wheel.jpg"; |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 45 | alphaType = kOpaque_SkAlphaType; |
| 46 | break; |
| 47 | default: |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 48 | resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg" |
| 49 | : "images/color_wheel.png"; |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 50 | break; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 53 | sk_sp<SkData> data = GetResourceAsData(resource); |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 54 | if (!data) { |
| 55 | return; |
| 56 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 57 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 58 | SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType) |
| 59 | .makeAlphaType(alphaType) |
Brian Osman | 6b62296 | 2018-08-27 19:16:02 +0000 | [diff] [blame] | 60 | .makeColorSpace(colorSpace); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 61 | bitmap->allocPixels(dstInfo); |
| 62 | codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes()); |
| 63 | } |
| 64 | |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 65 | static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) { |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 66 | SkPixmap src; |
| 67 | if (!bitmap.peekPixels(&src)) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | SkDynamicMemoryWStream buf; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 71 | |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 72 | switch (format) { |
| 73 | case SkEncodedImageFormat::kPNG: |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 74 | SkAssertResult(SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options())); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 75 | break; |
| 76 | case SkEncodedImageFormat::kWEBP: |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 77 | SkAssertResult(SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options())); |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 78 | break; |
| 79 | case SkEncodedImageFormat::kJPEG: |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 80 | SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options())); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 85 | return buf.detachAsData(); |
| 86 | } |
| 87 | |
| 88 | class EncodeSRGBGM : public GM { |
| 89 | public: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 90 | EncodeSRGBGM(SkEncodedImageFormat format) |
| 91 | : fEncodedFormat(format) |
| 92 | {} |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 93 | |
| 94 | protected: |
| 95 | SkString onShortName() override { |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 96 | const char* format = nullptr; |
| 97 | switch (fEncodedFormat) { |
| 98 | case SkEncodedImageFormat::kPNG: |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 99 | format = "png"; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 100 | break; |
| 101 | case SkEncodedImageFormat::kWEBP: |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 102 | format = "webp"; |
| 103 | break; |
| 104 | case SkEncodedImageFormat::kJPEG: |
| 105 | format = "jpg"; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 110 | return SkStringPrintf("encode-srgb-%s", format); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SkISize onISize() override { |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 114 | return SkISize::Make(imageWidth * 2, imageHeight * 15); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void onDraw(SkCanvas* canvas) override { |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 118 | const SkColorType colorTypes[] = { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 119 | kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType, |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 120 | }; |
| 121 | const SkAlphaType alphaTypes[] = { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 122 | kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType, |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 123 | }; |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 124 | const sk_sp<SkColorSpace> colorSpaces[] = { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 125 | nullptr, SkColorSpace::MakeSRGB(), |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | SkBitmap bitmap; |
| 129 | for (SkColorType colorType : colorTypes) { |
| 130 | for (SkAlphaType alphaType : alphaTypes) { |
| 131 | canvas->save(); |
| 132 | for (sk_sp<SkColorSpace> colorSpace : colorSpaces) { |
| 133 | make(&bitmap, colorType, alphaType, colorSpace); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 134 | auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat)); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 135 | canvas->drawImage(image.get(), 0.0f, 0.0f); |
| 136 | canvas->translate((float) imageWidth, 0.0f); |
| 137 | } |
| 138 | canvas->restore(); |
| 139 | canvas->translate(0.0f, (float) imageHeight); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 145 | SkEncodedImageFormat fEncodedFormat; |
| 146 | |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 147 | typedef GM INHERITED; |
| 148 | }; |
| 149 | |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 150 | DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); ) |
| 151 | DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); ) |
Matt Sarett | e95941f | 2017-01-27 18:16:40 -0500 | [diff] [blame] | 152 | DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); ) |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 153 | } |