• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkICC.h"
5 #include "include/core/SkString.h"
6 #include "tools/HashAndEncode.h"
7 #include "png.h"
8 
rec2020()9 static sk_sp<SkColorSpace> rec2020() {
10     return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
11 }
12 
HashAndEncode(const SkBitmap & bitmap)13 HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
14     skcms_AlphaFormat srcAlpha;
15     switch (bitmap.alphaType()) {
16         case kUnknown_SkAlphaType: return;
17 
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()) {
25         case kUnknown_SkColorType: return;
26 
27         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;
37 
38         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     }
43 
44     skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
45     if (auto cs = bitmap.colorSpace()) {
46         cs->toProfile(&srcProfile);
47     }
48 
49     // Our common format that can represent anything we draw and encode as a PNG:
50     //   - 16-bit big-endian RGBA
51     //   - unpremul
52     //   - Rec. 2020 gamut and transfer function
53     skcms_PixelFormat dstFmt   = skcms_PixelFormat_RGBA_16161616BE;
54     skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
55     skcms_ICCProfile dstProfile;
56     rec2020()->toProfile(&dstProfile);
57 
58     int N = fSize.width() * fSize.height();
59     fPixels.reset(new uint64_t[N]);
60 
61     if (!skcms_Transform(bitmap.getPixels(), srcFmt, srcAlpha, &srcProfile,
62                          fPixels.get(),      dstFmt, dstAlpha, &dstProfile, N)) {
63         SkASSERT(false);
64         fPixels.reset(nullptr);
65     }
66 }
67 
write(SkWStream * st) const68 void HashAndEncode::write(SkWStream* st) const {
69     st->write(&fSize, sizeof(fSize));
70     if (const uint64_t* px = fPixels.get()) {
71         st->write(px, sizeof(*px) * fSize.width() * fSize.height());
72     }
73 
74     // N.B. changing salt will change the hash of all images produced by DM,
75     // and will cause tens of thousands of new images to be uploaded to Gold.
76     int salt = 1;
77     st->write(&salt, sizeof(salt));
78 }
79 
writePngTo(const char * path,const char * md5,CommandLineFlags::StringArray key,CommandLineFlags::StringArray properties) const80 bool HashAndEncode::writePngTo(const char* path,
81                                const char* md5,
82                                CommandLineFlags::StringArray key,
83                                CommandLineFlags::StringArray properties) const {
84     if (!fPixels) {
85         return false;
86     }
87 
88     FILE* f = fopen(path, "wb");
89     if (!f) {
90         return false;
91     }
92 
93     png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
94     if (!png) {
95         fclose(f);
96         return false;
97     }
98 
99     png_infop info = png_create_info_struct(png);
100     if (!info) {
101         png_destroy_write_struct(&png, &info);
102         fclose(f);
103         return false;
104     }
105 
106     SkString description;
107     description.append("Key: ");
108     for (int i = 0; i < key.count(); i++) {
109         description.appendf("%s ", key[i]);
110     }
111     description.append("Properties: ");
112     for (int i = 0; i < properties.count(); i++) {
113         description.appendf("%s ", properties[i]);
114     }
115     description.appendf("MD5: %s", md5);
116 
117     png_text text[2];
118     text[0].key  = (png_charp)"Author";
119     text[0].text = (png_charp)"DM unified Rec.2020";
120     text[0].compression = PNG_TEXT_COMPRESSION_NONE;
121     text[1].key  = (png_charp)"Description";
122     text[1].text = (png_charp)description.c_str();
123     text[1].compression = PNG_TEXT_COMPRESSION_NONE;
124     png_set_text(png, info, text, SK_ARRAY_COUNT(text));
125 
126     png_init_io(png, f);
127     png_set_IHDR(png, info, (png_uint_32)fSize.width()
128                           , (png_uint_32)fSize.height()
129                           , 16/*bits per channel*/
130                           , PNG_COLOR_TYPE_RGB_ALPHA
131                           , PNG_INTERLACE_NONE
132                           , PNG_COMPRESSION_TYPE_DEFAULT
133                           , PNG_FILTER_TYPE_DEFAULT);
134 
135     // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
136     png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
137     png_set_compression_level(png, 1);
138 
139     static const sk_sp<SkData> profile =
140         SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
141     png_set_iCCP(png, info,
142                  "Rec.2020",
143                  0/*compression type... no idea what options are available here*/,
144                  (png_const_bytep)profile->data(),
145                  (png_uint_32)    profile->size());
146 
147     png_write_info(png, info);
148     for (int y = 0; y < fSize.height(); y++) {
149         png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
150     }
151     png_write_end(png, info);
152 
153     png_destroy_write_struct(&png, &info);
154     fclose(f);
155     return true;
156 }
157 
158