1 /*
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  */
17 static sk_sp<SkColorSpace> gbr_color_space() {
18     return SkColorSpace::MakeSRGB()->makeColorSpin();
19 }
20 
21 /**
22  *  Create a color space with a steep transfer function.
23  */
24 static 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  */
33 static sk_sp<SkColorSpace> wide_gamut_color_space() {
34     return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
35                                  SkColorSpace::kRec2020_Gamut);
36 }
37 
38 int main(int argc, char** argv) {
39     sk_sp<SkImage> image = GetResourceAsImage("images/flutter_logo.jpg");
40     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 }
71