Mike Klein | 7af351a | 2018-07-27 09:54:43 -0400 | [diff] [blame] | 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 | |
| 8 | #include "../third_party/skcms/skcms.h" |
| 9 | #include "SkColorSpacePriv.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | int main(int argc, char** argv) { |
| 15 | const char* image_path = argc > 1 ? argv[1] : nullptr; |
| 16 | if (!image_path) { |
| 17 | SkDebugf("Please pass an image to convert as the first argument to this program.\n"); |
| 18 | return 1; |
| 19 | } |
| 20 | |
| 21 | sk_sp<SkImage> image = SkImage::MakeFromEncoded(SkData::MakeFromFileName(image_path)); |
| 22 | if (!image) { |
| 23 | SkDebugf("Couldn't decode %s as an SkImage.\n", image_path); |
| 24 | return 1; |
| 25 | } |
| 26 | |
| 27 | image = image->makeRasterImage(); |
| 28 | if (!image) { |
| 29 | SkDebugf("Converting to raster image failed.\n"); |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | SkPixmap pixmap; |
| 34 | if (!image->peekPixels(&pixmap)) { |
| 35 | SkDebugf("We really should be able to peek raster pixels.\n"); |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | SkColorSpace* src_cs = image->colorSpace() ? image->colorSpace() |
| 40 | : sk_srgb_singleton(); |
| 41 | skcms_ICCProfile src_profile; |
| 42 | src_cs->toProfile(&src_profile); |
| 43 | |
| 44 | const char* dst_profile_path = argc > 2 ? argv[2] : nullptr; |
| 45 | skcms_ICCProfile dst_profile = *skcms_sRGB_profile(); |
| 46 | if (dst_profile_path) { |
| 47 | sk_sp<SkData> blob = SkData::MakeFromFileName(dst_profile_path); |
| 48 | if (!skcms_Parse(blob->data(), blob->size(), &dst_profile)) { |
| 49 | SkDebugf("Can't parse %s as an ICC profile.\n", dst_profile_path); |
| 50 | return 1; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | skcms_PixelFormat fmt; |
| 55 | switch (pixmap.colorType()) { |
| 56 | case kRGBA_8888_SkColorType: fmt = skcms_PixelFormat_RGBA_8888; break; |
| 57 | case kBGRA_8888_SkColorType: fmt = skcms_PixelFormat_BGRA_8888; break; |
| 58 | default: |
| 59 | SkDebugf("color type %d not yet supported, imgcvt.cpp needs an update.\n", |
| 60 | pixmap.colorType()); |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | if (pixmap.alphaType() != kPremul_SkAlphaType) { |
| 65 | SkDebugf("not premul, that's weird.\n"); |
| 66 | return 1; |
| 67 | } |
| 68 | auto alpha = skcms_AlphaFormat_PremulAsEncoded; |
| 69 | |
| 70 | if (pixmap.rowBytes() != (size_t)pixmap.width() * pixmap.info().bytesPerPixel()) { |
| 71 | SkDebugf("not a tight pixmap, need a loop here\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | skcms_Transform(pixmap.addr(), fmt,alpha, &src_profile, |
| 76 | pixmap.writable_addr(), fmt,alpha, &dst_profile, |
| 77 | pixmap.width() * pixmap.height()); |
| 78 | pixmap.setColorSpace(SkColorSpace::Make(dst_profile)); |
| 79 | |
| 80 | sk_sp<SkImage> transformed = SkImage::MakeRasterCopy(pixmap); |
| 81 | sk_sp<SkData> png = transformed->encodeToData(); |
| 82 | |
| 83 | SkFILEWStream("transformed.png").write(png->data(), png->size()); |
| 84 | |
| 85 | return 0; |
| 86 | } |