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