• 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 
8 #include <png.h>
9 
rec2020()10 static sk_sp<SkColorSpace> rec2020() {
11     return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
12 }
13 
HashAndEncode(const SkBitmap & bitmap)14 HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
15     skcms_AlphaFormat srcAlpha;
16     switch (bitmap.alphaType()) {
17         case kUnknown_SkAlphaType: return;
18 
19         case kOpaque_SkAlphaType:
20         case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul;        break;
21         case kPremul_SkAlphaType:   srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
22     }
23 
24     skcms_PixelFormat srcFmt;
25     switch (bitmap.colorType()) {
26         case kUnknown_SkColorType:            return;
27 
28         case kAlpha_8_SkColorType:            srcFmt = skcms_PixelFormat_A_8;             break;
29         case kRGB_565_SkColorType:            srcFmt = skcms_PixelFormat_BGR_565;         break;
30         case kARGB_4444_SkColorType:          srcFmt = skcms_PixelFormat_ABGR_4444;       break;
31         case kRGBA_8888_SkColorType:          srcFmt = skcms_PixelFormat_RGBA_8888;       break;
32         case kBGRA_8888_SkColorType:          srcFmt = skcms_PixelFormat_BGRA_8888;       break;
33         case kSRGBA_8888_SkColorType:         srcFmt = skcms_PixelFormat_RGBA_8888_sRGB;  break;
34         case kRGBA_1010102_SkColorType:       srcFmt = skcms_PixelFormat_RGBA_1010102;    break;
35         case kBGRA_1010102_SkColorType:       srcFmt = skcms_PixelFormat_BGRA_1010102;    break;
36         case kGray_8_SkColorType:             srcFmt = skcms_PixelFormat_G_8;             break;
37         // skcms doesn't have R_8. Pretend it's G_8, but see below for color space trickery:
38         case kR8_unorm_SkColorType:           srcFmt = skcms_PixelFormat_G_8;             break;
39         case kRGBA_F16Norm_SkColorType:       srcFmt = skcms_PixelFormat_RGBA_hhhh;       break;
40         case kRGBA_F16_SkColorType:           srcFmt = skcms_PixelFormat_RGBA_hhhh;       break;
41         case kRGBA_F32_SkColorType:           srcFmt = skcms_PixelFormat_RGBA_ffff;       break;
42         case kR16G16B16A16_unorm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_16161616LE; break;
43 
44         case kRGB_888x_SkColorType:           srcFmt = skcms_PixelFormat_RGBA_8888;
45                                               srcAlpha = skcms_AlphaFormat_Opaque;     break;
46         case kRGB_101010x_SkColorType:        srcFmt = skcms_PixelFormat_RGBA_1010102;
47                                               srcAlpha = skcms_AlphaFormat_Opaque;     break;
48         case kBGR_101010x_SkColorType:        srcFmt = skcms_PixelFormat_BGRA_1010102;
49                                               srcAlpha = skcms_AlphaFormat_Opaque;     break;
50 
51         case kR8G8_unorm_SkColorType:         return;
52         case kR16G16_unorm_SkColorType:       return;
53         case kR16G16_float_SkColorType:       return;
54         case kA16_unorm_SkColorType:          return;
55         case kA16_float_SkColorType:          return;
56     }
57 
58     skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
59     if (auto cs = bitmap.colorSpace()) {
60         cs->toProfile(&srcProfile);
61     }
62 
63     // NOTE: If the color type is R8, we told skcms it's actually G8 above. To get red PNGs,
64     // we tweak the source color space to throw away any green and blue:
65     if (bitmap.colorType() == kR8_unorm_SkColorType) {
66         srcProfile.toXYZD50.vals[0][1] = srcProfile.toXYZD50.vals[0][2] = 0;
67         srcProfile.toXYZD50.vals[1][1] = srcProfile.toXYZD50.vals[1][2] = 0;
68         srcProfile.toXYZD50.vals[2][1] = srcProfile.toXYZD50.vals[2][2] = 0;
69     }
70 
71     // Our common format that can represent anything we draw and encode as a PNG:
72     //   - 16-bit big-endian RGBA
73     //   - unpremul
74     //   - Rec. 2020 gamut and transfer function
75     skcms_PixelFormat dstFmt   = skcms_PixelFormat_RGBA_16161616BE;
76     skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
77     skcms_ICCProfile dstProfile;
78     rec2020()->toProfile(&dstProfile);
79 
80     int N = fSize.width() * fSize.height();
81     fPixels.reset(new uint64_t[N]);
82 
83     const void* src = bitmap.getPixels();
84     void* dst = fPixels.get();
85     while (N > 0) {
86         int todo = std::min(N, 1<<27);  // Keep todo*8 <= 1B; skcms requires N*bpp < MAX_INT.
87         if (!skcms_Transform(src, srcFmt, srcAlpha, &srcProfile,
88                              dst, dstFmt, dstAlpha, &dstProfile, todo)) {
89             SkASSERT(false);
90             fPixels.reset(nullptr);
91             break;
92         }
93         src = (char*)src + todo*SkColorTypeBytesPerPixel(bitmap.colorType());
94         dst = (char*)dst + todo*sizeof(uint64_t);
95         N -= todo;
96     }
97 }
98 
feedHash(SkWStream * st) const99 void HashAndEncode::feedHash(SkWStream* st) const {
100     st->write(&fSize, sizeof(fSize));
101     if (const uint64_t* px = fPixels.get()) {
102         st->write(px, sizeof(*px) * fSize.width() * fSize.height());
103     }
104 
105     // N.B. changing salt will change the hash of all images produced by DM,
106     // and will cause tens of thousands of new images to be uploaded to Gold.
107     int salt = 1;
108     st->write(&salt, sizeof(salt));
109 }
110 
111 // NOTE: HashAndEncode uses libpng directly rather than through an abstraction
112 // like SkPngEncoder to make sure we get stable, portable results independent
113 // of any changes to Skia production encoder.
114 
encodePNG(SkWStream * st,const char * md5,CommandLineFlags::StringArray key,CommandLineFlags::StringArray properties) const115 bool HashAndEncode::encodePNG(SkWStream* st,
116                               const char* md5,
117                               CommandLineFlags::StringArray key,
118                               CommandLineFlags::StringArray properties) const {
119     if (!fPixels) {
120         return false;
121     }
122 
123     png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
124     if (!png) {
125         return false;
126     }
127 
128     png_infop info = png_create_info_struct(png);
129     if (!info) {
130         png_destroy_write_struct(&png, &info);
131         return false;
132     }
133     auto write_to_stream = +[](png_structp png, png_bytep ptr, png_size_t len) {
134         auto st = (SkWStream*)png_get_io_ptr(png);
135         if (!st->write(ptr, len)) {
136             png_error(png, "HashAndEncode::encodePNG() failed writing stream");
137         }
138     };
139     png_set_write_fn(png, st, write_to_stream, nullptr);
140 
141     SkString description;
142     description.append("Key: ");
143     for (int i = 0; i < key.count(); i++) {
144         description.appendf("%s ", key[i]);
145     }
146     description.append("Properties: ");
147     for (int i = 0; i < properties.count(); i++) {
148         description.appendf("%s ", properties[i]);
149     }
150     description.appendf("MD5: %s", md5);
151 
152     png_text text[2];
153     text[0].key  = (png_charp)"Author";
154     text[0].text = (png_charp)"DM unified Rec.2020";
155     text[0].compression = PNG_TEXT_COMPRESSION_NONE;
156     text[1].key  = (png_charp)"Description";
157     text[1].text = (png_charp)description.c_str();
158     text[1].compression = PNG_TEXT_COMPRESSION_NONE;
159     png_set_text(png, info, text, SK_ARRAY_COUNT(text));
160 
161     png_set_IHDR(png, info, (png_uint_32)fSize.width()
162                           , (png_uint_32)fSize.height()
163                           , 16/*bits per channel*/
164                           , PNG_COLOR_TYPE_RGB_ALPHA
165                           , PNG_INTERLACE_NONE
166                           , PNG_COMPRESSION_TYPE_DEFAULT
167                           , PNG_FILTER_TYPE_DEFAULT);
168 
169     // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
170     png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
171     png_set_compression_level(png, 1);
172 
173     static const sk_sp<SkData> profile =
174         SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
175     png_set_iCCP(png, info,
176                  "Rec.2020",
177                  0/*compression type... no idea what options are available here*/,
178                  (png_const_bytep)profile->data(),
179                  (png_uint_32)    profile->size());
180 
181     png_write_info(png, info);
182     for (int y = 0; y < fSize.height(); y++) {
183         png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
184     }
185     png_write_end(png, info);
186 
187     png_destroy_write_struct(&png, &info);
188     return true;
189 }
190 
191