blob: 27c915bdd9d348b176ab1f193baf0cf71f01d0dc [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
Kevin Lubick5e8f45f2022-03-31 15:07:44 -04004#include "tools/HashAndEncode.h"
5
6#include "include/core/SkColorSpace.h"
Brian Osman211d63b2023-09-01 20:39:58 +00007#include "include/core/SkColorType.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkString.h"
Kevin Lubick3b233922023-03-23 09:26:34 -04009#include "include/encode/SkICC.h"
Kevin Lubick52904d32022-06-29 15:34:49 -040010#include "modules/skcms/skcms.h"
Kevin Lubick7f5b19b2021-11-23 09:08:05 -050011
12#include <png.h>
Mike Klein735c7ba2019-03-25 12:57:58 -050013
Mike Klein735c7ba2019-03-25 12:57:58 -050014static sk_sp<SkColorSpace> rec2020() {
Hal Canarybe67a172019-05-24 10:13:36 -040015 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
Mike Klein735c7ba2019-03-25 12:57:58 -050016}
17
18HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
19 skcms_AlphaFormat srcAlpha;
20 switch (bitmap.alphaType()) {
Mike Klein9b462092019-03-27 13:52:35 -050021 case kUnknown_SkAlphaType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050022
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()) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -040030 case kUnknown_SkColorType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050031
Mike Kleind58e01b62020-07-17 13:52:41 -050032 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;
Brian Osman9f1e06a2021-08-10 14:39:18 -040037 case kSRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888_sRGB; break;
Mike Kleind58e01b62020-07-17 13:52:41 -050038 case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
39 case kBGRA_1010102_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102; break;
Aaron Clarkefe37efc2023-02-16 14:40:00 -080040 case kBGR_101010x_XR_SkColorType: srcFmt = skcms_PixelFormat_BGR_101010x_XR; break;
Mike Kleind58e01b62020-07-17 13:52:41 -050041 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
Brian Osmana7a23242022-02-08 10:34:38 -050042 // 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;
Mike Kleind58e01b62020-07-17 13:52:41 -050044 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;
Mike Klein735c7ba2019-03-25 12:57:58 -050048
Robert Phillipsea1b30b2019-09-19 16:05:48 -040049 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;
Mike Kleinf7eb0542020-02-11 12:19:08 -060053 case kBGR_101010x_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102;
54 srcAlpha = skcms_AlphaFormat_Opaque; break;
Mike Kleind58e01b62020-07-17 13:52:41 -050055
Robert Phillipsea1b30b2019-09-19 16:05:48 -040056 case kR8G8_unorm_SkColorType: return;
57 case kR16G16_unorm_SkColorType: return;
58 case kR16G16_float_SkColorType: return;
59 case kA16_unorm_SkColorType: return;
60 case kA16_float_SkColorType: return;
Brian Osman211d63b2023-09-01 20:39:58 +000061 case kRGBA_10x6_SkColorType: return;
Mike Klein735c7ba2019-03-25 12:57:58 -050062 }
63
64 skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
65 if (auto cs = bitmap.colorSpace()) {
66 cs->toProfile(&srcProfile);
67 }
68
Brian Osmana7a23242022-02-08 10:34:38 -050069 // NOTE: If the color type is R8, we told skcms it's actually G8 above. To get red PNGs,
70 // we tweak the source color space to throw away any green and blue:
71 if (bitmap.colorType() == kR8_unorm_SkColorType) {
72 srcProfile.toXYZD50.vals[0][1] = srcProfile.toXYZD50.vals[0][2] = 0;
73 srcProfile.toXYZD50.vals[1][1] = srcProfile.toXYZD50.vals[1][2] = 0;
74 srcProfile.toXYZD50.vals[2][1] = srcProfile.toXYZD50.vals[2][2] = 0;
75 }
76
Mike Klein735c7ba2019-03-25 12:57:58 -050077 // Our common format that can represent anything we draw and encode as a PNG:
78 // - 16-bit big-endian RGBA
79 // - unpremul
80 // - Rec. 2020 gamut and transfer function
81 skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
82 skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
83 skcms_ICCProfile dstProfile;
84 rec2020()->toProfile(&dstProfile);
85
86 int N = fSize.width() * fSize.height();
87 fPixels.reset(new uint64_t[N]);
88
Mike Kleind50ac5c2021-02-02 18:48:34 -060089 const void* src = bitmap.getPixels();
90 void* dst = fPixels.get();
91 while (N > 0) {
92 int todo = std::min(N, 1<<27); // Keep todo*8 <= 1B; skcms requires N*bpp < MAX_INT.
93 if (!skcms_Transform(src, srcFmt, srcAlpha, &srcProfile,
94 dst, dstFmt, dstAlpha, &dstProfile, todo)) {
95 SkASSERT(false);
96 fPixels.reset(nullptr);
97 break;
98 }
Brian Osmanc9943f12023-11-17 16:39:23 -050099 src = (const char*)src + todo*SkColorTypeBytesPerPixel(bitmap.colorType());
100 dst = ( char*)dst + todo*sizeof(uint64_t);
Mike Kleind50ac5c2021-02-02 18:48:34 -0600101 N -= todo;
Mike Klein735c7ba2019-03-25 12:57:58 -0500102 }
103}
104
Mike Klein989f5bf2020-10-07 10:11:50 -0500105void HashAndEncode::feedHash(SkWStream* st) const {
Mike Klein735c7ba2019-03-25 12:57:58 -0500106 st->write(&fSize, sizeof(fSize));
107 if (const uint64_t* px = fPixels.get()) {
108 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
109 }
110
111 // N.B. changing salt will change the hash of all images produced by DM,
112 // and will cause tens of thousands of new images to be uploaded to Gold.
113 int salt = 1;
114 st->write(&salt, sizeof(salt));
115}
116
Mike Klein989f5bf2020-10-07 10:11:50 -0500117// NOTE: HashAndEncode uses libpng directly rather than through an abstraction
118// like SkPngEncoder to make sure we get stable, portable results independent
119// of any changes to Skia production encoder.
Mike Klein735c7ba2019-03-25 12:57:58 -0500120
Mike Klein989f5bf2020-10-07 10:11:50 -0500121bool HashAndEncode::encodePNG(SkWStream* st,
122 const char* md5,
123 CommandLineFlags::StringArray key,
124 CommandLineFlags::StringArray properties) const {
125 if (!fPixels) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500126 return false;
127 }
128
129 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
130 if (!png) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500131 return false;
132 }
133
134 png_infop info = png_create_info_struct(png);
135 if (!info) {
136 png_destroy_write_struct(&png, &info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500137 return false;
138 }
Mike Klein989f5bf2020-10-07 10:11:50 -0500139 auto write_to_stream = +[](png_structp png, png_bytep ptr, png_size_t len) {
140 auto st = (SkWStream*)png_get_io_ptr(png);
141 if (!st->write(ptr, len)) {
142 png_error(png, "HashAndEncode::encodePNG() failed writing stream");
143 }
144 };
145 png_set_write_fn(png, st, write_to_stream, nullptr);
Mike Klein735c7ba2019-03-25 12:57:58 -0500146
147 SkString description;
148 description.append("Key: ");
Herb Derby6a0a4c42022-09-30 16:43:14 -0400149 for (int i = 0; i < key.size(); i++) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500150 description.appendf("%s ", key[i]);
151 }
152 description.append("Properties: ");
Herb Derby6a0a4c42022-09-30 16:43:14 -0400153 for (int i = 0; i < properties.size(); i++) {
Mike Klein735c7ba2019-03-25 12:57:58 -0500154 description.appendf("%s ", properties[i]);
155 }
156 description.appendf("MD5: %s", md5);
157
158 png_text text[2];
Brian Osmanc9943f12023-11-17 16:39:23 -0500159 text[0].key = const_cast<png_charp>("Author");
160 text[0].text = const_cast<png_charp>("DM unified Rec.2020");
Mike Klein735c7ba2019-03-25 12:57:58 -0500161 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
Brian Osmanc9943f12023-11-17 16:39:23 -0500162 text[1].key = const_cast<png_charp>("Description");
163 text[1].text = const_cast<png_charp>(description.c_str());
Mike Klein735c7ba2019-03-25 12:57:58 -0500164 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
Herb Derbyb780fc22022-06-28 08:27:35 -0400165 png_set_text(png, info, text, std::size(text));
Mike Klein735c7ba2019-03-25 12:57:58 -0500166
Mike Klein735c7ba2019-03-25 12:57:58 -0500167 png_set_IHDR(png, info, (png_uint_32)fSize.width()
168 , (png_uint_32)fSize.height()
169 , 16/*bits per channel*/
170 , PNG_COLOR_TYPE_RGB_ALPHA
171 , PNG_INTERLACE_NONE
172 , PNG_COMPRESSION_TYPE_DEFAULT
173 , PNG_FILTER_TYPE_DEFAULT);
174
175 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
176 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
Mike Klein22a5e342019-03-27 12:36:11 -0500177 png_set_compression_level(png, 1);
Mike Klein735c7ba2019-03-25 12:57:58 -0500178
Hal Canarybe67a172019-05-24 10:13:36 -0400179 static const sk_sp<SkData> profile =
180 SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
Mike Klein735c7ba2019-03-25 12:57:58 -0500181 png_set_iCCP(png, info,
182 "Rec.2020",
183 0/*compression type... no idea what options are available here*/,
184 (png_const_bytep)profile->data(),
185 (png_uint_32) profile->size());
186
187 png_write_info(png, info);
188 for (int y = 0; y < fSize.height(); y++) {
189 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
190 }
191 png_write_end(png, info);
192
193 png_destroy_write_struct(&png, &info);
Mike Klein735c7ba2019-03-25 12:57:58 -0500194 return true;
195}
196