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