1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4 #include "tools/HashAndEncode.h"
5
6 #include "include/core/SkColorSpace.h"
7 #include "include/core/SkColorType.h"
8 #include "include/core/SkString.h"
9 #include "include/encode/SkICC.h"
10 #include "modules/skcms/skcms.h"
11
12 #include <png.h>
13
rec2020()14 static sk_sp<SkColorSpace> rec2020() {
15 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
16 }
17
HashAndEncode(const SkBitmap & bitmap)18 HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
19 skcms_AlphaFormat srcAlpha;
20 switch (bitmap.alphaType()) {
21 case kUnknown_SkAlphaType: return;
22
23 case kOpaque_SkAlphaType:
24 case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul; break;
25 case kPremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
26 }
27
28 skcms_PixelFormat srcFmt;
29 switch (bitmap.colorType()) {
30 case kUnknown_SkColorType: return;
31
32 case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
33 case kRGB_565_SkColorType: srcFmt = skcms_PixelFormat_BGR_565; break;
34 case kARGB_4444_SkColorType: srcFmt = skcms_PixelFormat_ABGR_4444; break;
35 case kRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888; break;
36 case kBGRA_8888_SkColorType: srcFmt = skcms_PixelFormat_BGRA_8888; break;
37 case kSRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888_sRGB; break;
38 case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
39 case kBGRA_1010102_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102; break;
40 case kBGR_101010x_XR_SkColorType: srcFmt = skcms_PixelFormat_BGR_101010x_XR; break;
41 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
42 // skcms doesn't have R_8. Pretend it's G_8, but see below for color space trickery:
43 case kR8_unorm_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
44 case kRGBA_F16Norm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
45 case kRGBA_F16_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
46 case kRGBA_F32_SkColorType: srcFmt = skcms_PixelFormat_RGBA_ffff; break;
47 case kR16G16B16A16_unorm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_16161616LE; break;
48
49 case kRGB_888x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888;
50 srcAlpha = skcms_AlphaFormat_Opaque; break;
51 case kRGB_101010x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102;
52 srcAlpha = skcms_AlphaFormat_Opaque; break;
53 case kBGR_101010x_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102;
54 srcAlpha = skcms_AlphaFormat_Opaque; break;
55 case kRGB_F16F16F16x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh;
56 srcAlpha = skcms_AlphaFormat_Opaque; break;
57
58 case kR8G8_unorm_SkColorType: return;
59 case kR16G16_unorm_SkColorType: return;
60 case kR16G16_float_SkColorType: return;
61 case kA16_unorm_SkColorType: return;
62 case kA16_float_SkColorType: return;
63 case kRGBA_10x6_SkColorType: return;
64 case kBGRA_10101010_XR_SkColorType: return;
65 }
66
67 skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
68 if (auto cs = bitmap.colorSpace()) {
69 cs->toProfile(&srcProfile);
70 }
71
72 // NOTE: If the color type is R8, we told skcms it's actually G8 above. To get red PNGs,
73 // we tweak the source color space to throw away any green and blue:
74 if (bitmap.colorType() == kR8_unorm_SkColorType) {
75 srcProfile.toXYZD50.vals[0][1] = srcProfile.toXYZD50.vals[0][2] = 0;
76 srcProfile.toXYZD50.vals[1][1] = srcProfile.toXYZD50.vals[1][2] = 0;
77 srcProfile.toXYZD50.vals[2][1] = srcProfile.toXYZD50.vals[2][2] = 0;
78 }
79
80 // Our common format that can represent anything we draw and encode as a PNG:
81 // - 16-bit big-endian RGBA
82 // - unpremul
83 // - Rec. 2020 gamut and transfer function
84 skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
85 skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
86 skcms_ICCProfile dstProfile;
87 rec2020()->toProfile(&dstProfile);
88
89 int N = fSize.width() * fSize.height();
90 fPixels.reset(new uint64_t[N]);
91
92 const void* src = bitmap.getPixels();
93 void* dst = fPixels.get();
94 while (N > 0) {
95 int todo = std::min(N, 1<<27); // Keep todo*8 <= 1B; skcms requires N*bpp < MAX_INT.
96 if (!skcms_Transform(src, srcFmt, srcAlpha, &srcProfile,
97 dst, dstFmt, dstAlpha, &dstProfile, todo)) {
98 SkASSERT(false);
99 fPixels.reset(nullptr);
100 break;
101 }
102 src = (const char*)src + todo*SkColorTypeBytesPerPixel(bitmap.colorType());
103 dst = ( char*)dst + todo*sizeof(uint64_t);
104 N -= todo;
105 }
106 }
107
feedHash(SkWStream * st) const108 void HashAndEncode::feedHash(SkWStream* st) const {
109 st->write(&fSize, sizeof(fSize));
110 if (const uint64_t* px = fPixels.get()) {
111 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
112 }
113
114 // N.B. changing salt will change the hash of all images produced by DM,
115 // and will cause tens of thousands of new images to be uploaded to Gold.
116 int salt = 1;
117 st->write(&salt, sizeof(salt));
118 }
119
120 // NOTE: HashAndEncode uses libpng directly rather than through an abstraction
121 // like SkPngEncoder to make sure we get stable, portable results independent
122 // of any changes to Skia production encoder.
123
encodePNG(SkWStream * st,const char * md5,CommandLineFlags::StringArray key,CommandLineFlags::StringArray properties) const124 bool HashAndEncode::encodePNG(SkWStream* st,
125 const char* md5,
126 CommandLineFlags::StringArray key,
127 CommandLineFlags::StringArray properties) const {
128 if (!fPixels) {
129 return false;
130 }
131
132 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
133 if (!png) {
134 return false;
135 }
136
137 png_infop info = png_create_info_struct(png);
138 if (!info) {
139 png_destroy_write_struct(&png, &info);
140 return false;
141 }
142 auto write_to_stream = +[](png_structp png, png_bytep ptr, png_size_t len) {
143 auto st = (SkWStream*)png_get_io_ptr(png);
144 if (!st->write(ptr, len)) {
145 png_error(png, "HashAndEncode::encodePNG() failed writing stream");
146 }
147 };
148 png_set_write_fn(png, st, write_to_stream, nullptr);
149
150 SkString description;
151 description.append("Key: ");
152 for (int i = 0; i < key.size(); i++) {
153 description.appendf("%s ", key[i]);
154 }
155 description.append("Properties: ");
156 for (int i = 0; i < properties.size(); i++) {
157 description.appendf("%s ", properties[i]);
158 }
159 description.appendf("MD5: %s", md5);
160
161 png_text text[2];
162 text[0].key = const_cast<png_charp>("Author");
163 text[0].text = const_cast<png_charp>("DM unified Rec.2020");
164 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
165 text[1].key = const_cast<png_charp>("Description");
166 text[1].text = const_cast<png_charp>(description.c_str());
167 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
168 png_set_text(png, info, text, std::size(text));
169
170 png_set_IHDR(png, info, (png_uint_32)fSize.width()
171 , (png_uint_32)fSize.height()
172 , 16/*bits per channel*/
173 , PNG_COLOR_TYPE_RGB_ALPHA
174 , PNG_INTERLACE_NONE
175 , PNG_COMPRESSION_TYPE_DEFAULT
176 , PNG_FILTER_TYPE_DEFAULT);
177
178 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
179 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
180 png_set_compression_level(png, 1);
181
182 static const sk_sp<SkData> profile =
183 SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
184 png_set_iCCP(png, info,
185 "Rec.2020",
186 0/*compression type... no idea what options are available here*/,
187 (png_const_bytep)profile->data(),
188 (png_uint_32) profile->size());
189
190 png_write_info(png, info);
191 for (int y = 0; y < fSize.height(); y++) {
192 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
193 }
194 png_write_end(png, info);
195
196 png_destroy_write_struct(&png, &info);
197 return true;
198 }
199
200