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