1 /*
2 * Copyright 2018 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 "include/core/SkCanvas.h"
9 #include "include/core/SkData.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkSurface.h"
13 #include "include/encode/SkPngEncoder.h"
14 #include "modules/skcms/skcms.h"
15 #include "src/core/SkColorSpacePriv.h"
16
write_png(const char * path,sk_sp<SkImage> img)17 static void write_png(const char* path, sk_sp<SkImage> img) {
18 sk_sp<SkData> png = SkPngEncoder::Encode(nullptr, img.get(), {});
19 SkASSERT(png);
20 SkFILEWStream(path).write(png->data(), png->size());
21 }
22
main(int argc,char ** argv)23 int main(int argc, char** argv) {
24 const char* source_path = argc > 1 ? argv[1] : nullptr;
25 if (!source_path) {
26 SkDebugf("Please pass an image or profile to convert"
27 " as the first argument to this program.\n");
28 return 1;
29 }
30
31 const char* dst_profile_path = argc > 2 ? argv[2] : nullptr;
32 skcms_ICCProfile dst_profile = *skcms_sRGB_profile();
33 sk_sp<SkData> dst_blob;
34 if (dst_profile_path) {
35 dst_blob = SkData::MakeFromFileName(dst_profile_path);
36 if (!skcms_Parse(dst_blob->data(), dst_blob->size(), &dst_profile)) {
37 SkDebugf("Can't parse %s as an ICC profile.\n", dst_profile_path);
38 return 1;
39 }
40 }
41
42 auto blob = SkData::MakeFromFileName(source_path);
43
44 skcms_ICCProfile src_profile;
45 if (skcms_Parse(blob->data(), blob->size(), &src_profile)) {
46 // Transform white, black, primaries, and primary complements.
47 float src[] = {
48 0,0,0,
49 1,1,1,
50
51 1,0,0,
52 0,1,0,
53 0,0,1,
54
55 0,1,1,
56 1,0,1,
57 1,1,0,
58 };
59 float dst[24] = {0};
60
61 if (!skcms_Transform(
62 src, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &src_profile,
63 dst, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &dst_profile,
64 8)) {
65 SkDebugf("Cannot transform.\n");
66 return 1;
67 }
68 for (int i = 0; i < 8; i++) {
69 SkDebugf("(%g, %g, %g) --> (%+.4f, %+.4f, %+.4f)\n",
70 src[3*i+0], src[3*i+1], src[3*i+2],
71 dst[3*i+0], dst[3*i+1], dst[3*i+2]);
72 }
73 return 0;
74 }
75
76 sk_sp<SkImage> image = SkImages::DeferredFromEncodedData(blob);
77 if (!image) {
78 SkDebugf("Couldn't decode %s as an SkImage or an ICC profile.\n", source_path);
79 return 1;
80 }
81
82 image = image->makeRasterImage();
83 if (!image) {
84 SkDebugf("Converting to raster image failed.\n");
85 return 1;
86 }
87
88 SkPixmap pixmap;
89 if (!image->peekPixels(&pixmap)) {
90 SkDebugf("We really should be able to peek raster pixels.\n");
91 return 1;
92 }
93
94 sk_sp<SkColorSpace> dst_cs = SkColorSpace::Make(dst_profile);
95 if (!dst_cs) {
96 SkDebugf("We can't convert to this destination profile as-is. Coercing it.\n");
97 if (skcms_MakeUsableAsDestinationWithSingleCurve(&dst_profile)) {
98 dst_cs = SkColorSpace::Make(dst_profile);
99 }
100 if (!dst_cs) {
101 SkDebugf("We can't convert to this destination profile at all.\n");
102 return 1;
103 }
104 }
105
106 { // transform with skcms
107 SkColorSpace* src_cs = image->colorSpace() ? image->colorSpace()
108 : sk_srgb_singleton();
109 src_cs->toProfile(&src_profile);
110
111 skcms_PixelFormat fmt;
112 switch (pixmap.colorType()) {
113 case kRGBA_8888_SkColorType: fmt = skcms_PixelFormat_RGBA_8888; break;
114 case kBGRA_8888_SkColorType: fmt = skcms_PixelFormat_BGRA_8888; break;
115 default:
116 SkDebugf("color type %d not yet supported, imgcvt.cpp needs an update.\n",
117 pixmap.colorType());
118 return 1;
119 }
120
121 if (pixmap.alphaType() == kUnpremul_SkAlphaType) {
122 SkDebugf("not premul, that's weird.\n");
123 return 1;
124 }
125 auto alpha = skcms_AlphaFormat_PremulAsEncoded;
126
127 if (pixmap.rowBytes() != (size_t)pixmap.width() * pixmap.info().bytesPerPixel()) {
128 SkDebugf("not a tight pixmap, need a loop here\n");
129 return 1;
130 }
131
132 if (!skcms_Transform(pixmap.addr(), fmt,alpha, &src_profile,
133 pixmap.writable_addr(), fmt,alpha, &dst_profile,
134 pixmap.width() * pixmap.height())) {
135 SkDebugf("skcms_Transform() failed\n");
136 return 1;
137 }
138 pixmap.setColorSpace(dst_cs);
139
140 write_png("transformed-skcms.png", SkImages::RasterFromPixmapCopy(pixmap));
141 }
142
143 { // transform with writePixels()
144 sk_sp<SkSurface> surface = SkSurfaces::Raster(pixmap.info().makeColorSpace(dst_cs));
145 if (!surface) {
146 SkDebugf("couldn't create a surface\n");
147 return 1;
148 }
149
150 surface->writePixels(pixmap, 0,0);
151
152 write_png("transformed-writepixels.png", surface->makeImageSnapshot());
153 }
154
155 { // transform by drawing
156 sk_sp<SkSurface> surface = SkSurfaces::Raster(pixmap.info().makeColorSpace(dst_cs));
157 if (!surface) {
158 SkDebugf("couldn't create a surface\n");
159 return 1;
160 }
161
162 surface->getCanvas()->drawImage(image, 0,0);
163
164 write_png("transformed-draw.png", surface->makeImageSnapshot());
165 }
166
167 return 0;
168 }
169