| 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/codec/SkCodec.h" |
| 10 |
#include "include/codec/SkEncodedImageFormat.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImage.h" |
| 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/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 |
#include <memory> |
| 28 |
|
| 29 |
namespace skiagm { |
| 30 |
|
| 31 |
static const int imageWidth = 128; |
| 32 |
static const int imageHeight = 128; |
| 33 |
|
| 34 |
static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType, |
| 35 |
sk_sp<SkColorSpace> colorSpace) { |
| 36 |
const char* resource; |
| 37 |
switch (colorType) { |
| 38 |
case kGray_8_SkColorType: |
| 39 |
resource = "images/grayscale.jpg"; |
| 40 |
alphaType = kOpaque_SkAlphaType; |
| 41 |
break; |
| 42 |
case kRGB_565_SkColorType: |
| 43 |
resource = "images/color_wheel.jpg"; |
| 44 |
alphaType = kOpaque_SkAlphaType; |
| 45 |
break; |
| 46 |
default: |
| 47 |
resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg" |
| 48 |
: "images/color_wheel.png"; |
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
sk_sp<SkData> data = GetResourceAsData(resource); |
| 53 |
if (!data) { |
| 54 |
return; |
| 55 |
} |
| 56 |
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); |
| 57 |
SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType) |
| 58 |
.makeAlphaType(alphaType) |
| 59 |
.makeColorSpace(colorSpace); |
| 60 |
bitmap->allocPixels(dstInfo); |
| 61 |
codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes()); |
| 62 |
} |
| 63 |
|
| 64 |
static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) { |
| 65 |
SkPixmap src; |
| 66 |
if (!bitmap.peekPixels(&src)) { |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
sk_sp<SkData> data; |
| 70 |
|
| 71 |
switch (format) { |
| 72 |
case SkEncodedImageFormat::kPNG: |
| 73 |
data = SkPngEncoder::Encode(src, SkPngEncoder::Options()); |
| 74 |
break; |
| 75 |
case SkEncodedImageFormat::kWEBP: |
| 76 |
data = SkWebpEncoder::Encode(src, SkWebpEncoder::Options()); |
| 77 |
break; |
| 78 |
case SkEncodedImageFormat::kJPEG: |
| 79 |
data = SkJpegEncoder::Encode(src, SkJpegEncoder::Options()); |
| 80 |
break; |
| 81 |
default: |
| 82 |
SK_ABORT("Unsupported format %d", (int)format); |
| 83 |
break; |
| 84 |
} |
| 85 |
SkAssertResult(data); |
| 86 |
return data; |
| 87 |
} |
| 88 |
|
| 89 |
class EncodeSRGBGM : public GM { |
| 90 |
public: |
| 91 |
EncodeSRGBGM(SkEncodedImageFormat format) |
| 92 |
: fEncodedFormat(format) |
| 93 |
{} |
| 94 |
|
| 95 |
protected: |
| 96 |
SkString getName() const override { |
| 97 |
const char* format = nullptr; |
| 98 |
switch (fEncodedFormat) { |
| 99 |
case SkEncodedImageFormat::kPNG: |
| 100 |
format = "png"; |
| 101 |
break; |
| 102 |
case SkEncodedImageFormat::kWEBP: |
| 103 |
format = "webp"; |
| 104 |
break; |
| 105 |
case SkEncodedImageFormat::kJPEG: |
| 106 |
format = "jpg"; |
| 107 |
break; |
| 108 |
default: |
| 109 |
break; |
| 110 |
} |
| 111 |
return SkStringPrintf("encode-srgb-%s", format); |
| 112 |
} |
| 113 |
|
| 114 |
SkISize getISize() override { return SkISize::Make(imageWidth * 2, imageHeight * 15); } |
| 115 |
|
| 116 |
void onDraw(SkCanvas* canvas) override { |
| 117 |
const SkColorType colorTypes[] = { |
| 118 |
kN32_SkColorType, kRGBA_F16_SkColorType, |
| 119 |
#if !defined(SK_ENABLE_NDK_IMAGES) |
| 120 |
// These fail with the NDK encoders because there is a mismatch between |
| 121 |
// Gray_8 and Alpha_8 |
| 122 |
kGray_8_SkColorType, |
| 123 |
#endif |
| 124 |
kRGB_565_SkColorType, |
| 125 |
}; |
| 126 |
const SkAlphaType alphaTypes[] = { |
| 127 |
kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType, |
| 128 |
}; |
| 129 |
const sk_sp<SkColorSpace> colorSpaces[] = { |
| 130 |
nullptr, SkColorSpace::MakeSRGB(), |
| 131 |
}; |
| 132 |
|
| 133 |
SkBitmap bitmap; |
| 134 |
for (SkColorType colorType : colorTypes) { |
| 135 |
for (SkAlphaType alphaType : alphaTypes) { |
| 136 |
canvas->save(); |
| 137 |
for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) { |
| 138 |
make(&bitmap, colorType, alphaType, colorSpace); |
| 139 |
auto data = encode_data(bitmap, fEncodedFormat); |
| 140 |
auto image = SkImages::DeferredFromEncodedData(data); |
| 141 |
canvas->drawImage(image.get(), 0.0f, 0.0f); |
| 142 |
canvas->translate((float) imageWidth, 0.0f); |
| 143 |
} |
| 144 |
canvas->restore(); |
| 145 |
canvas->translate(0.0f, (float) imageHeight); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
private: |
| 151 |
SkEncodedImageFormat fEncodedFormat; |
| 152 |
|
| 153 |
using INHERITED = GM; |
| 154 |
}; |
| 155 |
|
| 156 |
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); ) |
| 157 |
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); ) |
| 158 |
DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); ) |
| 159 |
} // namespace skiagm |