blob: e14278f0e9eb264fc00f6cbc42150522b43b285a [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
Mike Klein735c7ba2019-03-25 12:57:58 -05009static sk_sp<SkColorSpace> rec2020() {
Hal Canarybe67a172019-05-24 10:13:36 -040010 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
Mike Klein735c7ba2019-03-25 12:57:58 -050011}
12
13HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
14 skcms_AlphaFormat srcAlpha;
15 switch (bitmap.alphaType()) {
Mike Klein9b462092019-03-27 13:52:35 -050016 case kUnknown_SkAlphaType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050017
18 case kOpaque_SkAlphaType:
19 case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul; break;
20 case kPremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
21 }
22
23 skcms_PixelFormat srcFmt;
24 switch (bitmap.colorType()) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -040025 case kUnknown_SkColorType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050026
Mike Kleind58e01b62020-07-17 13:52:41 -050027 case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
28 case kRGB_565_SkColorType: srcFmt = skcms_PixelFormat_BGR_565; break;
29 case kARGB_4444_SkColorType: srcFmt = skcms_PixelFormat_ABGR_4444; break;
30 case kRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888; break;
31 case kBGRA_8888_SkColorType: srcFmt = skcms_PixelFormat_BGRA_8888; break;
32 case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
33 case kBGRA_1010102_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102; break;
34 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
35 case kRGBA_F16Norm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
36 case kRGBA_F16_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
37 case kRGBA_F32_SkColorType: srcFmt = skcms_PixelFormat_RGBA_ffff; break;
38 case kR16G16B16A16_unorm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_16161616LE; break;
Mike Klein735c7ba2019-03-25 12:57:58 -050039
Robert Phillipsea1b30b2019-09-19 16:05:48 -040040 case kRGB_888x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888;
41 srcAlpha = skcms_AlphaFormat_Opaque; break;
42 case kRGB_101010x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102;
43 srcAlpha = skcms_AlphaFormat_Opaque; break;
Mike Kleinf7eb0542020-02-11 12:19:08 -060044 case kBGR_101010x_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102;
45 srcAlpha = skcms_AlphaFormat_Opaque; break;
Mike Kleind58e01b62020-07-17 13:52:41 -050046
Robert Phillipsea1b30b2019-09-19 16:05:48 -040047 case kR8G8_unorm_SkColorType: return;
48 case kR16G16_unorm_SkColorType: return;
49 case kR16G16_float_SkColorType: return;
50 case kA16_unorm_SkColorType: return;
51 case kA16_float_SkColorType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050052 }
53
54 skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
55 if (auto cs = bitmap.colorSpace()) {
56 cs->toProfile(&srcProfile);
57 }
58
59 // Our common format that can represent anything we draw and encode as a PNG:
60 // - 16-bit big-endian RGBA
61 // - unpremul
62 // - Rec. 2020 gamut and transfer function
63 skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
64 skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
65 skcms_ICCProfile dstProfile;
66 rec2020()->toProfile(&dstProfile);
67
68 int N = fSize.width() * fSize.height();
69 fPixels.reset(new uint64_t[N]);
70
71 if (!skcms_Transform(bitmap.getPixels(), srcFmt, srcAlpha, &srcProfile,
72 fPixels.get(), dstFmt, dstAlpha, &dstProfile, N)) {
73 SkASSERT(false);
74 fPixels.reset(nullptr);
75 }
76}
77
78void HashAndEncode::write(SkWStream* st) const {
79 st->write(&fSize, sizeof(fSize));
80 if (const uint64_t* px = fPixels.get()) {
81 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
82 }
83
84 // N.B. changing salt will change the hash of all images produced by DM,
85 // and will cause tens of thousands of new images to be uploaded to Gold.
86 int salt = 1;
87 st->write(&salt, sizeof(salt));
88}
89
90bool HashAndEncode::writePngTo(const char* path,
91 const char* md5,
92 CommandLineFlags::StringArray key,
93 CommandLineFlags::StringArray properties) const {
94 if (!fPixels) {
95 return false;
96 }
97
98 FILE* f = fopen(path, "wb");
99 if (!f) {
100 return false;
101 }
102
103 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
104 if (!png) {
105 fclose(f);
106 return false;
107 }
108
109 png_infop info = png_create_info_struct(png);
110 if (!info) {
111 png_destroy_write_struct(&png, &info);
112 fclose(f);
113 return false;
114 }
115
116 SkString description;
117 description.append("Key: ");
118 for (int i = 0; i < key.count(); i++) {
119 description.appendf("%s ", key[i]);
120 }
121 description.append("Properties: ");
122 for (int i = 0; i < properties.count(); i++) {
123 description.appendf("%s ", properties[i]);
124 }
125 description.appendf("MD5: %s", md5);
126
127 png_text text[2];
128 text[0].key = (png_charp)"Author";
129 text[0].text = (png_charp)"DM unified Rec.2020";
130 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
131 text[1].key = (png_charp)"Description";
132 text[1].text = (png_charp)description.c_str();
133 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
134 png_set_text(png, info, text, SK_ARRAY_COUNT(text));
135
136 png_init_io(png, f);
137 png_set_IHDR(png, info, (png_uint_32)fSize.width()
138 , (png_uint_32)fSize.height()
139 , 16/*bits per channel*/
140 , PNG_COLOR_TYPE_RGB_ALPHA
141 , PNG_INTERLACE_NONE
142 , PNG_COMPRESSION_TYPE_DEFAULT
143 , PNG_FILTER_TYPE_DEFAULT);
144
145 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
146 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
Mike Klein22a5e342019-03-27 12:36:11 -0500147 png_set_compression_level(png, 1);
Mike Klein735c7ba2019-03-25 12:57:58 -0500148
Hal Canarybe67a172019-05-24 10:13:36 -0400149 static const sk_sp<SkData> profile =
150 SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
Mike Klein735c7ba2019-03-25 12:57:58 -0500151 png_set_iCCP(png, info,
152 "Rec.2020",
153 0/*compression type... no idea what options are available here*/,
154 (png_const_bytep)profile->data(),
155 (png_uint_32) profile->size());
156
157 png_write_info(png, info);
158 for (int y = 0; y < fSize.height(); y++) {
159 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
160 }
161 png_write_end(png, info);
162
163 png_destroy_write_struct(&png, &info);
164 fclose(f);
165 return true;
166}
167