add a few more ways to convert images to imgcvt

Interestingly, the draw ends up a little different than the
writePixels() and skcms paths.  Maybe just premul/unpremul?

Bug: chromium:867813
Change-Id: I8bf645c8354cbeb9a07512add258e8defc755366
Reviewed-on: https://skia-review.googlesource.com/c/164621
Commit-Queue: Mike Klein <mtklein@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Auto-Submit: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
Mike Klein 2018-10-23 13:21:53 -04:00 committed by Skia Commit-Bot
parent 05c8f461d0
commit fd731e4684

View File

@ -6,10 +6,17 @@
*/
#include "../third_party/skcms/skcms.h"
#include "SkCanvas.h"
#include "SkColorSpacePriv.h"
#include "SkData.h"
#include "SkImage.h"
#include "SkStream.h"
#include "SkSurface.h"
static void write_png(const char* path, sk_sp<SkImage> img) {
sk_sp<SkData> png = img->encodeToData();
SkFILEWStream(path).write(png->data(), png->size());
}
int main(int argc, char** argv) {
const char* source_path = argc > 1 ? argv[1] : nullptr;
@ -81,40 +88,72 @@ int main(int argc, char** argv) {
return 1;
}
SkColorSpace* src_cs = image->colorSpace() ? image->colorSpace()
: sk_srgb_singleton();
src_cs->toProfile(&src_profile);
sk_sp<SkColorSpace> dst_cs = SkColorSpace::Make(dst_profile);
if (!dst_cs) {
SkDebugf("We can't convert to this destination profile.\n");
return 1;
}
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());
{ // 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;
}
if (pixmap.alphaType() != kPremul_SkAlphaType) {
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));
}
if (pixmap.alphaType() != kPremul_SkAlphaType) {
SkDebugf("not premul, that's weird.\n");
return 1;
}
auto alpha = skcms_AlphaFormat_PremulAsEncoded;
{ // 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;
}
if (pixmap.rowBytes() != (size_t)pixmap.width() * pixmap.info().bytesPerPixel()) {
SkDebugf("not a tight pixmap, need a loop here\n");
return 1;
surface->writePixels(pixmap, 0,0);
write_png("transformed-writepixels.png", surface->makeImageSnapshot());
}
skcms_Transform(pixmap.addr(), fmt,alpha, &src_profile,
pixmap.writable_addr(), fmt,alpha, &dst_profile,
pixmap.width() * pixmap.height());
pixmap.setColorSpace(SkColorSpace::Make(dst_profile));
{ // 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;
}
sk_sp<SkImage> transformed = SkImage::MakeRasterCopy(pixmap);
sk_sp<SkData> png = transformed->encodeToData();
surface->getCanvas()->drawImage(image, 0,0);
SkFILEWStream("transformed.png").write(png->data(), png->size());
write_png("transformed-draw.png", surface->makeImageSnapshot());
}
return 0;
}