blob: 683c2fe34aa412eb0da5e95c68e9ab6f8fc7fd10 [file] [log] [blame]
Matt Sarett9f1c4032017-05-17 10:06:32 -04001/*
2 * Copyright 2017 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 "SkColorSpacePriv.h"
9#include "SkImage.h"
10#include "SkPngEncoder.h"
11
12#include "Resources.h"
13
14/**
15 * Create a color space that swaps the red, green, and blue channels.
16 */
17static sk_sp<SkColorSpace> gbr_color_space() {
Brian Osman36703d92017-12-12 14:09:31 -050018 return SkColorSpace::MakeSRGB()->makeColorSpin();
Matt Sarett9f1c4032017-05-17 10:06:32 -040019}
20
21/**
22 * Create a color space with a steep transfer function.
23 */
24static sk_sp<SkColorSpace> tf_color_space() {
25 SkColorSpaceTransferFn fn;
26 fn.fA = 1.f; fn.fB = 0.f; fn.fC = 0.f; fn.fD = 0.f; fn.fE = 0.f; fn.fF = 0.f; fn.fG = 50.f;
27 return SkColorSpace::MakeRGB(fn, SkColorSpace::kSRGB_Gamut);
28}
29
30/**
31 * Create a wide gamut color space.
32 */
33static sk_sp<SkColorSpace> wide_gamut_color_space() {
34 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
35 SkColorSpace::kRec2020_Gamut);
36}
37
38int main(int argc, char** argv) {
Hal Canaryc465d132017-12-08 10:21:31 -050039 sk_sp<SkImage> image = GetResourceAsImage("images/flutter_logo.jpg");
Matt Sarett9f1c4032017-05-17 10:06:32 -040040 if (!image) {
41 SkDebugf("Cannot find flutter_logo.jpg in resources.\n");
42 return 1;
43 }
44
45 // Encode an image with a gbr color space.
46 SkImageInfo info = SkImageInfo::MakeN32(image->width(), image->height(), kOpaque_SkAlphaType,
47 gbr_color_space());
48 size_t rowBytes = info.minRowBytes();
49 SkAutoTMalloc<uint8_t> storage(rowBytes * image->height());
50 SkPixmap src(info, storage.get(), rowBytes);
51 image->readPixels(src, 0, 0, SkImage::kDisallow_CachingHint);
52 SkFILEWStream dst0("gbr.png");
53 SkPngEncoder::Options opts;
54 opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; // Does not matter for opaque src
55 SkAssertResult(SkPngEncoder::Encode(&dst0, src, opts));
56
57 // Encode an image with steep transfer function.
58 src.setColorSpace(tf_color_space());
59 image->readPixels(src, 0, 0, SkImage::kDisallow_CachingHint);
60 SkFILEWStream dst1("tf.png");
61 SkAssertResult(SkPngEncoder::Encode(&dst1, src, opts));
62
63 // Encode a wide gamut image.
64 src.setColorSpace(wide_gamut_color_space());
65 image->readPixels(src, 0, 0, SkImage::kDisallow_CachingHint);
66 SkFILEWStream dst2("wide-gamut.png");
67 SkAssertResult(SkPngEncoder::Encode(&dst2, src, opts));
68
69 return 0;
70}