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