blob: 3f2b8478fae14be47d3737f20a1308199270c07e [file] [log] [blame]
Mike Klein735c7ba2019-03-25 12:57:58 -05001// 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004#include "include/core/SkICC.h"
5#include "include/core/SkString.h"
6#include "tools/HashAndEncode.h"
Mike Klein735c7ba2019-03-25 12:57:58 -05007#include "png.h"
8
9static constexpr skcms_TransferFunction k2020_TF =
10 {2.22222f, 0.909672f, 0.0903276f, 0.222222f, 0.0812429f, 0, 0};
11
12static sk_sp<SkColorSpace> rec2020() {
13 return SkColorSpace::MakeRGB(k2020_TF, SkNamedGamut::kRec2020);
14}
15
16HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
17 skcms_AlphaFormat srcAlpha;
18 switch (bitmap.alphaType()) {
Mike Klein9b462092019-03-27 13:52:35 -050019 case kUnknown_SkAlphaType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050020
21 case kOpaque_SkAlphaType:
22 case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul; break;
23 case kPremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
24 }
25
26 skcms_PixelFormat srcFmt;
27 switch (bitmap.colorType()) {
Mike Klein9b462092019-03-27 13:52:35 -050028 case kUnknown_SkColorType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050029
30 case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
31 case kRGB_565_SkColorType: srcFmt = skcms_PixelFormat_BGR_565; break;
32 case kARGB_4444_SkColorType: srcFmt = skcms_PixelFormat_ABGR_4444; break;
33 case kRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888; break;
34 case kBGRA_8888_SkColorType: srcFmt = skcms_PixelFormat_BGRA_8888; break;
35 case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
36 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
37 case kRGBA_F16Norm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
38 case kRGBA_F16_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
39 case kRGBA_F32_SkColorType: srcFmt = skcms_PixelFormat_RGBA_ffff; break;
40
41 case kRGB_888x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888;
42 srcAlpha = skcms_AlphaFormat_Opaque; break;
43 case kRGB_101010x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102;
44 srcAlpha = skcms_AlphaFormat_Opaque; break;
45 }
46
47 skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
48 if (auto cs = bitmap.colorSpace()) {
49 cs->toProfile(&srcProfile);
50 }
51
52 // Our common format that can represent anything we draw and encode as a PNG:
53 // - 16-bit big-endian RGBA
54 // - unpremul
55 // - Rec. 2020 gamut and transfer function
56 skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
57 skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
58 skcms_ICCProfile dstProfile;
59 rec2020()->toProfile(&dstProfile);
60
61 int N = fSize.width() * fSize.height();
62 fPixels.reset(new uint64_t[N]);
63
64 if (!skcms_Transform(bitmap.getPixels(), srcFmt, srcAlpha, &srcProfile,
65 fPixels.get(), dstFmt, dstAlpha, &dstProfile, N)) {
66 SkASSERT(false);
67 fPixels.reset(nullptr);
68 }
69}
70
71void HashAndEncode::write(SkWStream* st) const {
72 st->write(&fSize, sizeof(fSize));
73 if (const uint64_t* px = fPixels.get()) {
74 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
75 }
76
77 // N.B. changing salt will change the hash of all images produced by DM,
78 // and will cause tens of thousands of new images to be uploaded to Gold.
79 int salt = 1;
80 st->write(&salt, sizeof(salt));
81}
82
83bool HashAndEncode::writePngTo(const char* path,
84 const char* md5,
85 CommandLineFlags::StringArray key,
86 CommandLineFlags::StringArray properties) const {
87 if (!fPixels) {
88 return false;
89 }
90
91 FILE* f = fopen(path, "wb");
92 if (!f) {
93 return false;
94 }
95
96 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
97 if (!png) {
98 fclose(f);
99 return false;
100 }
101
102 png_infop info = png_create_info_struct(png);
103 if (!info) {
104 png_destroy_write_struct(&png, &info);
105 fclose(f);
106 return false;
107 }
108
109 SkString description;
110 description.append("Key: ");
111 for (int i = 0; i < key.count(); i++) {
112 description.appendf("%s ", key[i]);
113 }
114 description.append("Properties: ");
115 for (int i = 0; i < properties.count(); i++) {
116 description.appendf("%s ", properties[i]);
117 }
118 description.appendf("MD5: %s", md5);
119
120 png_text text[2];
121 text[0].key = (png_charp)"Author";
122 text[0].text = (png_charp)"DM unified Rec.2020";
123 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
124 text[1].key = (png_charp)"Description";
125 text[1].text = (png_charp)description.c_str();
126 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
127 png_set_text(png, info, text, SK_ARRAY_COUNT(text));
128
129 png_init_io(png, f);
130 png_set_IHDR(png, info, (png_uint_32)fSize.width()
131 , (png_uint_32)fSize.height()
132 , 16/*bits per channel*/
133 , PNG_COLOR_TYPE_RGB_ALPHA
134 , PNG_INTERLACE_NONE
135 , PNG_COMPRESSION_TYPE_DEFAULT
136 , PNG_FILTER_TYPE_DEFAULT);
137
138 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
139 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
Mike Klein22a5e342019-03-27 12:36:11 -0500140 png_set_compression_level(png, 1);
Mike Klein735c7ba2019-03-25 12:57:58 -0500141
142 static const sk_sp<SkData> profile = SkWriteICCProfile(k2020_TF, SkNamedGamut::kRec2020);
143 png_set_iCCP(png, info,
144 "Rec.2020",
145 0/*compression type... no idea what options are available here*/,
146 (png_const_bytep)profile->data(),
147 (png_uint_32) profile->size());
148
149 png_write_info(png, info);
150 for (int y = 0; y < fSize.height(); y++) {
151 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
152 }
153 png_write_end(png, info);
154
155 png_destroy_write_struct(&png, &info);
156 fclose(f);
157 return true;
158}
159