imgcvt.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkData.h"
  9. #include "include/core/SkImage.h"
  10. #include "include/core/SkStream.h"
  11. #include "include/core/SkSurface.h"
  12. #include "include/third_party/skcms/skcms.h"
  13. #include "src/core/SkColorSpacePriv.h"
  14. static void write_png(const char* path, sk_sp<SkImage> img) {
  15. sk_sp<SkData> png = img->encodeToData();
  16. SkFILEWStream(path).write(png->data(), png->size());
  17. }
  18. int main(int argc, char** argv) {
  19. const char* source_path = argc > 1 ? argv[1] : nullptr;
  20. if (!source_path) {
  21. SkDebugf("Please pass an image or profile to convert"
  22. " as the first argument to this program.\n");
  23. return 1;
  24. }
  25. const char* dst_profile_path = argc > 2 ? argv[2] : nullptr;
  26. skcms_ICCProfile dst_profile = *skcms_sRGB_profile();
  27. sk_sp<SkData> dst_blob;
  28. if (dst_profile_path) {
  29. dst_blob = SkData::MakeFromFileName(dst_profile_path);
  30. if (!skcms_Parse(dst_blob->data(), dst_blob->size(), &dst_profile)) {
  31. SkDebugf("Can't parse %s as an ICC profile.\n", dst_profile_path);
  32. return 1;
  33. }
  34. }
  35. auto blob = SkData::MakeFromFileName(source_path);
  36. skcms_ICCProfile src_profile;
  37. if (skcms_Parse(blob->data(), blob->size(), &src_profile)) {
  38. // Transform white, black, primaries, and primary complements.
  39. float src[] = {
  40. 0,0,0,
  41. 1,1,1,
  42. 1,0,0,
  43. 0,1,0,
  44. 0,0,1,
  45. 0,1,1,
  46. 1,0,1,
  47. 1,1,0,
  48. };
  49. float dst[24] = {0};
  50. if (!skcms_Transform(
  51. src, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &src_profile,
  52. dst, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &dst_profile,
  53. 8)) {
  54. SkDebugf("Cannot transform.\n");
  55. return 1;
  56. }
  57. for (int i = 0; i < 8; i++) {
  58. SkDebugf("(%g, %g, %g) --> (%+.4f, %+.4f, %+.4f)\n",
  59. src[3*i+0], src[3*i+1], src[3*i+2],
  60. dst[3*i+0], dst[3*i+1], dst[3*i+2]);
  61. }
  62. return 0;
  63. }
  64. sk_sp<SkImage> image = SkImage::MakeFromEncoded(blob);
  65. if (!image) {
  66. SkDebugf("Couldn't decode %s as an SkImage or an ICC profile.\n", source_path);
  67. return 1;
  68. }
  69. image = image->makeRasterImage();
  70. if (!image) {
  71. SkDebugf("Converting to raster image failed.\n");
  72. return 1;
  73. }
  74. SkPixmap pixmap;
  75. if (!image->peekPixels(&pixmap)) {
  76. SkDebugf("We really should be able to peek raster pixels.\n");
  77. return 1;
  78. }
  79. sk_sp<SkColorSpace> dst_cs = SkColorSpace::Make(dst_profile);
  80. if (!dst_cs) {
  81. SkDebugf("We can't convert to this destination profile as-is. Coercing it.\n");
  82. if (skcms_MakeUsableAsDestinationWithSingleCurve(&dst_profile)) {
  83. dst_cs = SkColorSpace::Make(dst_profile);
  84. }
  85. if (!dst_cs) {
  86. SkDebugf("We can't convert to this destination profile at all.\n");
  87. return 1;
  88. }
  89. }
  90. { // transform with skcms
  91. SkColorSpace* src_cs = image->colorSpace() ? image->colorSpace()
  92. : sk_srgb_singleton();
  93. src_cs->toProfile(&src_profile);
  94. skcms_PixelFormat fmt;
  95. switch (pixmap.colorType()) {
  96. case kRGBA_8888_SkColorType: fmt = skcms_PixelFormat_RGBA_8888; break;
  97. case kBGRA_8888_SkColorType: fmt = skcms_PixelFormat_BGRA_8888; break;
  98. default:
  99. SkDebugf("color type %d not yet supported, imgcvt.cpp needs an update.\n",
  100. pixmap.colorType());
  101. return 1;
  102. }
  103. if (pixmap.alphaType() == kUnpremul_SkAlphaType) {
  104. SkDebugf("not premul, that's weird.\n");
  105. return 1;
  106. }
  107. auto alpha = skcms_AlphaFormat_PremulAsEncoded;
  108. if (pixmap.rowBytes() != (size_t)pixmap.width() * pixmap.info().bytesPerPixel()) {
  109. SkDebugf("not a tight pixmap, need a loop here\n");
  110. return 1;
  111. }
  112. if (!skcms_Transform(pixmap.addr(), fmt,alpha, &src_profile,
  113. pixmap.writable_addr(), fmt,alpha, &dst_profile,
  114. pixmap.width() * pixmap.height())) {
  115. SkDebugf("skcms_Transform() failed\n");
  116. return 1;
  117. }
  118. pixmap.setColorSpace(dst_cs);
  119. write_png("transformed-skcms.png", SkImage::MakeRasterCopy(pixmap));
  120. }
  121. { // transform with writePixels()
  122. sk_sp<SkSurface> surface = SkSurface::MakeRaster(pixmap.info().makeColorSpace(dst_cs));
  123. if (!surface) {
  124. SkDebugf("couldn't create a surface\n");
  125. return 1;
  126. }
  127. surface->writePixels(pixmap, 0,0);
  128. write_png("transformed-writepixels.png", surface->makeImageSnapshot());
  129. }
  130. { // transform by drawing
  131. sk_sp<SkSurface> surface = SkSurface::MakeRaster(pixmap.info().makeColorSpace(dst_cs));
  132. if (!surface) {
  133. SkDebugf("couldn't create a surface\n");
  134. return 1;
  135. }
  136. surface->getCanvas()->drawImage(image, 0,0);
  137. write_png("transformed-draw.png", surface->makeImageSnapshot());
  138. }
  139. return 0;
  140. }