2018-07-27 13:54:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2019-04-29 14:28:22 +00:00
|
|
|
#include "include/third_party/skcms/skcms.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkColorSpacePriv.h"
|
2018-10-23 17:21:53 +00:00
|
|
|
|
|
|
|
static void write_png(const char* path, sk_sp<SkImage> img) {
|
|
|
|
sk_sp<SkData> png = img->encodeToData();
|
|
|
|
SkFILEWStream(path).write(png->data(), png->size());
|
|
|
|
}
|
2018-07-27 13:54:43 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
add profile->profile mode to imgcvt
E.g.
out/imgcvt /System/Library/ColorSync/Profiles/ITU-2020.icc ~/skcms/profiles/mobile/sRGB_parametric.icc
(0, 0, 0) --> (+0.0000, +0.0000, +0.0000)
(1, 1, 1) --> (+0.9998, +0.9998, +0.9998)
(1, 0, 0) --> (+1.2481, -0.3879, -0.1435)
(0, 1, 0) --> (-0.7903, +1.0561, -0.3501)
(0, 0, 1) --> (-0.2992, -0.0886, +1.0503)
(0, 1, 1) --> (-0.8325, +1.0527, +1.0077)
(1, 0, 1) --> (+1.2239, -0.4000, +1.0428)
(1, 1, 0) --> (+1.0312, +1.0035, -0.3791)
Change-Id: Ieb38a3b519f00ae56491c3265ed9725888b692e9
Reviewed-on: https://skia-review.googlesource.com/152360
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-06 18:21:09 +00:00
|
|
|
const char* source_path = argc > 1 ? argv[1] : nullptr;
|
|
|
|
if (!source_path) {
|
|
|
|
SkDebugf("Please pass an image or profile to convert"
|
|
|
|
" as the first argument to this program.\n");
|
2018-07-27 13:54:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
add profile->profile mode to imgcvt
E.g.
out/imgcvt /System/Library/ColorSync/Profiles/ITU-2020.icc ~/skcms/profiles/mobile/sRGB_parametric.icc
(0, 0, 0) --> (+0.0000, +0.0000, +0.0000)
(1, 1, 1) --> (+0.9998, +0.9998, +0.9998)
(1, 0, 0) --> (+1.2481, -0.3879, -0.1435)
(0, 1, 0) --> (-0.7903, +1.0561, -0.3501)
(0, 0, 1) --> (-0.2992, -0.0886, +1.0503)
(0, 1, 1) --> (-0.8325, +1.0527, +1.0077)
(1, 0, 1) --> (+1.2239, -0.4000, +1.0428)
(1, 1, 0) --> (+1.0312, +1.0035, -0.3791)
Change-Id: Ieb38a3b519f00ae56491c3265ed9725888b692e9
Reviewed-on: https://skia-review.googlesource.com/152360
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-06 18:21:09 +00:00
|
|
|
const char* dst_profile_path = argc > 2 ? argv[2] : nullptr;
|
|
|
|
skcms_ICCProfile dst_profile = *skcms_sRGB_profile();
|
2019-01-04 16:03:42 +00:00
|
|
|
sk_sp<SkData> dst_blob;
|
add profile->profile mode to imgcvt
E.g.
out/imgcvt /System/Library/ColorSync/Profiles/ITU-2020.icc ~/skcms/profiles/mobile/sRGB_parametric.icc
(0, 0, 0) --> (+0.0000, +0.0000, +0.0000)
(1, 1, 1) --> (+0.9998, +0.9998, +0.9998)
(1, 0, 0) --> (+1.2481, -0.3879, -0.1435)
(0, 1, 0) --> (-0.7903, +1.0561, -0.3501)
(0, 0, 1) --> (-0.2992, -0.0886, +1.0503)
(0, 1, 1) --> (-0.8325, +1.0527, +1.0077)
(1, 0, 1) --> (+1.2239, -0.4000, +1.0428)
(1, 1, 0) --> (+1.0312, +1.0035, -0.3791)
Change-Id: Ieb38a3b519f00ae56491c3265ed9725888b692e9
Reviewed-on: https://skia-review.googlesource.com/152360
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-06 18:21:09 +00:00
|
|
|
if (dst_profile_path) {
|
2019-01-04 16:03:42 +00:00
|
|
|
dst_blob = SkData::MakeFromFileName(dst_profile_path);
|
|
|
|
if (!skcms_Parse(dst_blob->data(), dst_blob->size(), &dst_profile)) {
|
add profile->profile mode to imgcvt
E.g.
out/imgcvt /System/Library/ColorSync/Profiles/ITU-2020.icc ~/skcms/profiles/mobile/sRGB_parametric.icc
(0, 0, 0) --> (+0.0000, +0.0000, +0.0000)
(1, 1, 1) --> (+0.9998, +0.9998, +0.9998)
(1, 0, 0) --> (+1.2481, -0.3879, -0.1435)
(0, 1, 0) --> (-0.7903, +1.0561, -0.3501)
(0, 0, 1) --> (-0.2992, -0.0886, +1.0503)
(0, 1, 1) --> (-0.8325, +1.0527, +1.0077)
(1, 0, 1) --> (+1.2239, -0.4000, +1.0428)
(1, 1, 0) --> (+1.0312, +1.0035, -0.3791)
Change-Id: Ieb38a3b519f00ae56491c3265ed9725888b692e9
Reviewed-on: https://skia-review.googlesource.com/152360
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-06 18:21:09 +00:00
|
|
|
SkDebugf("Can't parse %s as an ICC profile.\n", dst_profile_path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto blob = SkData::MakeFromFileName(source_path);
|
|
|
|
|
|
|
|
skcms_ICCProfile src_profile;
|
|
|
|
if (skcms_Parse(blob->data(), blob->size(), &src_profile)) {
|
|
|
|
// Transform white, black, primaries, and primary complements.
|
|
|
|
float src[] = {
|
|
|
|
0,0,0,
|
|
|
|
1,1,1,
|
|
|
|
|
|
|
|
1,0,0,
|
|
|
|
0,1,0,
|
|
|
|
0,0,1,
|
|
|
|
|
|
|
|
0,1,1,
|
|
|
|
1,0,1,
|
|
|
|
1,1,0,
|
|
|
|
};
|
|
|
|
float dst[24] = {0};
|
|
|
|
|
|
|
|
if (!skcms_Transform(
|
|
|
|
src, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &src_profile,
|
|
|
|
dst, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Unpremul, &dst_profile,
|
|
|
|
8)) {
|
|
|
|
SkDebugf("Cannot transform.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
SkDebugf("(%g, %g, %g) --> (%+.4f, %+.4f, %+.4f)\n",
|
|
|
|
src[3*i+0], src[3*i+1], src[3*i+2],
|
|
|
|
dst[3*i+0], dst[3*i+1], dst[3*i+2]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> image = SkImage::MakeFromEncoded(blob);
|
2018-07-27 13:54:43 +00:00
|
|
|
if (!image) {
|
add profile->profile mode to imgcvt
E.g.
out/imgcvt /System/Library/ColorSync/Profiles/ITU-2020.icc ~/skcms/profiles/mobile/sRGB_parametric.icc
(0, 0, 0) --> (+0.0000, +0.0000, +0.0000)
(1, 1, 1) --> (+0.9998, +0.9998, +0.9998)
(1, 0, 0) --> (+1.2481, -0.3879, -0.1435)
(0, 1, 0) --> (-0.7903, +1.0561, -0.3501)
(0, 0, 1) --> (-0.2992, -0.0886, +1.0503)
(0, 1, 1) --> (-0.8325, +1.0527, +1.0077)
(1, 0, 1) --> (+1.2239, -0.4000, +1.0428)
(1, 1, 0) --> (+1.0312, +1.0035, -0.3791)
Change-Id: Ieb38a3b519f00ae56491c3265ed9725888b692e9
Reviewed-on: https://skia-review.googlesource.com/152360
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-06 18:21:09 +00:00
|
|
|
SkDebugf("Couldn't decode %s as an SkImage or an ICC profile.\n", source_path);
|
2018-07-27 13:54:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
image = image->makeRasterImage();
|
|
|
|
if (!image) {
|
|
|
|
SkDebugf("Converting to raster image failed.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPixmap pixmap;
|
|
|
|
if (!image->peekPixels(&pixmap)) {
|
|
|
|
SkDebugf("We really should be able to peek raster pixels.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
sk_sp<SkColorSpace> dst_cs = SkColorSpace::Make(dst_profile);
|
|
|
|
if (!dst_cs) {
|
2019-01-04 16:03:42 +00:00
|
|
|
SkDebugf("We can't convert to this destination profile as-is. Coercing it.\n");
|
|
|
|
if (skcms_MakeUsableAsDestinationWithSingleCurve(&dst_profile)) {
|
|
|
|
dst_cs = SkColorSpace::Make(dst_profile);
|
|
|
|
}
|
|
|
|
if (!dst_cs) {
|
|
|
|
SkDebugf("We can't convert to this destination profile at all.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-07-27 13:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
{ // transform with skcms
|
|
|
|
SkColorSpace* src_cs = image->colorSpace() ? image->colorSpace()
|
|
|
|
: sk_srgb_singleton();
|
|
|
|
src_cs->toProfile(&src_profile);
|
|
|
|
|
|
|
|
skcms_PixelFormat fmt;
|
|
|
|
switch (pixmap.colorType()) {
|
|
|
|
case kRGBA_8888_SkColorType: fmt = skcms_PixelFormat_RGBA_8888; break;
|
|
|
|
case kBGRA_8888_SkColorType: fmt = skcms_PixelFormat_BGRA_8888; break;
|
|
|
|
default:
|
|
|
|
SkDebugf("color type %d not yet supported, imgcvt.cpp needs an update.\n",
|
|
|
|
pixmap.colorType());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-01-04 16:03:42 +00:00
|
|
|
if (pixmap.alphaType() == kUnpremul_SkAlphaType) {
|
2018-10-23 17:21:53 +00:00
|
|
|
SkDebugf("not premul, that's weird.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto alpha = skcms_AlphaFormat_PremulAsEncoded;
|
|
|
|
|
|
|
|
if (pixmap.rowBytes() != (size_t)pixmap.width() * pixmap.info().bytesPerPixel()) {
|
|
|
|
SkDebugf("not a tight pixmap, need a loop here\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skcms_Transform(pixmap.addr(), fmt,alpha, &src_profile,
|
|
|
|
pixmap.writable_addr(), fmt,alpha, &dst_profile,
|
|
|
|
pixmap.width() * pixmap.height())) {
|
|
|
|
SkDebugf("skcms_Transform() failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pixmap.setColorSpace(dst_cs);
|
|
|
|
|
|
|
|
write_png("transformed-skcms.png", SkImage::MakeRasterCopy(pixmap));
|
2018-07-27 13:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
{ // transform with writePixels()
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRaster(pixmap.info().makeColorSpace(dst_cs));
|
|
|
|
if (!surface) {
|
|
|
|
SkDebugf("couldn't create a surface\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
surface->writePixels(pixmap, 0,0);
|
|
|
|
|
|
|
|
write_png("transformed-writepixels.png", surface->makeImageSnapshot());
|
2018-07-27 13:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
{ // transform by drawing
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRaster(pixmap.info().makeColorSpace(dst_cs));
|
|
|
|
if (!surface) {
|
|
|
|
SkDebugf("couldn't create a surface\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-07-27 13:54:43 +00:00
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
surface->getCanvas()->drawImage(image, 0,0);
|
2018-07-27 13:54:43 +00:00
|
|
|
|
2018-10-23 17:21:53 +00:00
|
|
|
write_png("transformed-draw.png", surface->makeImageSnapshot());
|
|
|
|
}
|
2018-07-27 13:54:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|