HashAndEncode.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "include/core/SkICC.h"
  4. #include "include/core/SkString.h"
  5. #include "tools/HashAndEncode.h"
  6. #include "png.h"
  7. static sk_sp<SkColorSpace> rec2020() {
  8. return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
  9. }
  10. HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
  11. skcms_AlphaFormat srcAlpha;
  12. switch (bitmap.alphaType()) {
  13. case kUnknown_SkAlphaType: return;
  14. case kOpaque_SkAlphaType:
  15. case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul; break;
  16. case kPremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
  17. }
  18. skcms_PixelFormat srcFmt;
  19. switch (bitmap.colorType()) {
  20. case kUnknown_SkColorType: return;
  21. case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
  22. case kRGB_565_SkColorType: srcFmt = skcms_PixelFormat_BGR_565; break;
  23. case kARGB_4444_SkColorType: srcFmt = skcms_PixelFormat_ABGR_4444; break;
  24. case kRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888; break;
  25. case kBGRA_8888_SkColorType: srcFmt = skcms_PixelFormat_BGRA_8888; break;
  26. case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
  27. case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
  28. case kRGBA_F16Norm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
  29. case kRGBA_F16_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
  30. case kRGBA_F32_SkColorType: srcFmt = skcms_PixelFormat_RGBA_ffff; break;
  31. case kRGB_888x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888;
  32. srcAlpha = skcms_AlphaFormat_Opaque; break;
  33. case kRGB_101010x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102;
  34. srcAlpha = skcms_AlphaFormat_Opaque; break;
  35. }
  36. skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
  37. if (auto cs = bitmap.colorSpace()) {
  38. cs->toProfile(&srcProfile);
  39. }
  40. // Our common format that can represent anything we draw and encode as a PNG:
  41. // - 16-bit big-endian RGBA
  42. // - unpremul
  43. // - Rec. 2020 gamut and transfer function
  44. skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
  45. skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
  46. skcms_ICCProfile dstProfile;
  47. rec2020()->toProfile(&dstProfile);
  48. int N = fSize.width() * fSize.height();
  49. fPixels.reset(new uint64_t[N]);
  50. if (!skcms_Transform(bitmap.getPixels(), srcFmt, srcAlpha, &srcProfile,
  51. fPixels.get(), dstFmt, dstAlpha, &dstProfile, N)) {
  52. SkASSERT(false);
  53. fPixels.reset(nullptr);
  54. }
  55. }
  56. void HashAndEncode::write(SkWStream* st) const {
  57. st->write(&fSize, sizeof(fSize));
  58. if (const uint64_t* px = fPixels.get()) {
  59. st->write(px, sizeof(*px) * fSize.width() * fSize.height());
  60. }
  61. // N.B. changing salt will change the hash of all images produced by DM,
  62. // and will cause tens of thousands of new images to be uploaded to Gold.
  63. int salt = 1;
  64. st->write(&salt, sizeof(salt));
  65. }
  66. bool HashAndEncode::writePngTo(const char* path,
  67. const char* md5,
  68. CommandLineFlags::StringArray key,
  69. CommandLineFlags::StringArray properties) const {
  70. if (!fPixels) {
  71. return false;
  72. }
  73. FILE* f = fopen(path, "wb");
  74. if (!f) {
  75. return false;
  76. }
  77. png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  78. if (!png) {
  79. fclose(f);
  80. return false;
  81. }
  82. png_infop info = png_create_info_struct(png);
  83. if (!info) {
  84. png_destroy_write_struct(&png, &info);
  85. fclose(f);
  86. return false;
  87. }
  88. SkString description;
  89. description.append("Key: ");
  90. for (int i = 0; i < key.count(); i++) {
  91. description.appendf("%s ", key[i]);
  92. }
  93. description.append("Properties: ");
  94. for (int i = 0; i < properties.count(); i++) {
  95. description.appendf("%s ", properties[i]);
  96. }
  97. description.appendf("MD5: %s", md5);
  98. png_text text[2];
  99. text[0].key = (png_charp)"Author";
  100. text[0].text = (png_charp)"DM unified Rec.2020";
  101. text[0].compression = PNG_TEXT_COMPRESSION_NONE;
  102. text[1].key = (png_charp)"Description";
  103. text[1].text = (png_charp)description.c_str();
  104. text[1].compression = PNG_TEXT_COMPRESSION_NONE;
  105. png_set_text(png, info, text, SK_ARRAY_COUNT(text));
  106. png_init_io(png, f);
  107. png_set_IHDR(png, info, (png_uint_32)fSize.width()
  108. , (png_uint_32)fSize.height()
  109. , 16/*bits per channel*/
  110. , PNG_COLOR_TYPE_RGB_ALPHA
  111. , PNG_INTERLACE_NONE
  112. , PNG_COMPRESSION_TYPE_DEFAULT
  113. , PNG_FILTER_TYPE_DEFAULT);
  114. // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
  115. png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
  116. png_set_compression_level(png, 1);
  117. static const sk_sp<SkData> profile =
  118. SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
  119. png_set_iCCP(png, info,
  120. "Rec.2020",
  121. 0/*compression type... no idea what options are available here*/,
  122. (png_const_bytep)profile->data(),
  123. (png_uint_32) profile->size());
  124. png_write_info(png, info);
  125. for (int y = 0; y < fSize.height(); y++) {
  126. png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
  127. }
  128. png_write_end(png, info);
  129. png_destroy_write_struct(&png, &info);
  130. fclose(f);
  131. return true;
  132. }