Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
| 9 #include "SkAndroidCodec.h" | 9 #include "SkAndroidCodec.h" |
| 10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
| 11 #include "SkCodecImageGenerator.h" | 11 #include "SkCodecImageGenerator.h" |
| 12 #include "SkColorSpace.h" | |
| 13 #include "SkColorSpacePriv.h" | |
| 12 #include "SkCommonFlags.h" | 14 #include "SkCommonFlags.h" |
| 13 #include "SkData.h" | 15 #include "SkData.h" |
| 14 #include "SkDocument.h" | 16 #include "SkDocument.h" |
| 15 #include "SkError.h" | 17 #include "SkError.h" |
| 16 #include "SkImageGenerator.h" | 18 #include "SkImageGenerator.h" |
| 17 #include "SkImageGeneratorCG.h" | 19 #include "SkImageGeneratorCG.h" |
| 18 #include "SkImageGeneratorWIC.h" | 20 #include "SkImageGeneratorWIC.h" |
| 19 #include "SkMallocPixelRef.h" | 21 #include "SkMallocPixelRef.h" |
| 20 #include "SkMultiPictureDraw.h" | 22 #include "SkMultiPictureDraw.h" |
| 21 #include "SkNullCanvas.h" | 23 #include "SkNullCanvas.h" |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 821 } | 823 } |
| 822 return codec->getInfo().dimensions(); | 824 return codec->getInfo().dimensions(); |
| 823 } | 825 } |
| 824 | 826 |
| 825 Name ImageGenSrc::name() const { | 827 Name ImageGenSrc::name() const { |
| 826 return SkOSPath::Basename(fPath.c_str()); | 828 return SkOSPath::Basename(fPath.c_str()); |
| 827 } | 829 } |
| 828 | 830 |
| 829 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | 831 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ |
| 830 | 832 |
| 831 ColorCodecSrc::ColorCodecSrc(Path path, Mode mode) | 833 ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, sk_sp<SkColorSpace> dstSpace) |
| 832 : fPath(path) | 834 : fPath(path) |
| 833 , fMode(mode) | 835 , fMode(mode) |
| 836 , fDstSpace(dstSpace) | |
| 834 {} | 837 {} |
| 835 | 838 |
| 836 bool ColorCodecSrc::veto(SinkFlags flags) const { | 839 bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 837 // Test to direct raster backends (8888 and 565). | 840 // Test to direct raster backends (8888 and 565). |
| 838 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; | 841 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; |
| 839 } | 842 } |
| 840 | 843 |
| 844 static uint8_t clampFloatToByte(float v) { | |
| 845 v = v * 255.0f; | |
| 846 if (v > 255.0f) { | |
| 847 return 255; | |
| 848 } else if (v < 0.0f) { | |
| 849 return 0; | |
| 850 } else { | |
| 851 return (uint8_t) (v + 0.5f); | |
| 852 } | |
| 853 } | |
| 854 | |
| 841 Error ColorCodecSrc::draw(SkCanvas* canvas) const { | 855 Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
| 842 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { | 856 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| 843 return Error::Nonfatal("No need to test color correction to 565 backend. "); | 857 return Error::Nonfatal("No need to test color correction to 565 backend. "); |
| 844 } | 858 } |
| 845 | 859 |
| 846 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 860 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 847 if (!encoded) { | 861 if (!encoded) { |
| 848 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 862 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 849 } | 863 } |
| 850 | 864 |
| 851 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 865 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 852 if (nullptr == codec.get()) { | 866 if (nullptr == codec.get()) { |
| 853 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); | 867 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 854 } | 868 } |
| 855 | 869 |
| 856 SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType); | 870 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 871 uint32_t width = info.width(); | |
|
scroggo
2016/05/23 16:12:38
Why did you store these in local variables? It loo
msarett
2016/05/23 16:20:03
Can't remember, I think a draft implementation may
| |
| 872 uint32_t height = info.height(); | |
| 857 SkBitmap bitmap; | 873 SkBitmap bitmap; |
| 858 if (!bitmap.tryAllocPixels(decodeInfo)) { | 874 if (!bitmap.tryAllocPixels(info)) { |
| 859 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), | 875 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| 860 decodeInfo.width(), decodeInfo.height()); | 876 info.width(), info.height()); |
| 877 } | |
| 878 | |
| 879 SkImageInfo decodeInfo = info; | |
| 880 if (kBaseline_Mode != fMode) { | |
| 881 decodeInfo = decodeInfo.makeColorType(kRGBA_8888_SkColorType); | |
| 882 } | |
| 883 | |
| 884 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap. rowBytes()); | |
| 885 if (SkCodec::kSuccess != r) { | |
| 886 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_st r(), r); | |
| 861 } | 887 } |
| 862 | 888 |
| 863 switch (fMode) { | 889 switch (fMode) { |
| 864 case kBaseline_Mode: | 890 case kBaseline_Mode: |
| 865 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowB ytes())) { | |
| 866 case SkCodec::kSuccess: | |
| 867 break; | |
| 868 default: | |
| 869 // Everything else is considered a failure. | |
| 870 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); | |
| 871 } | |
| 872 canvas->drawBitmap(bitmap, 0, 0); | 891 canvas->drawBitmap(bitmap, 0, 0); |
| 873 break; | 892 break; |
| 893 case kDst_HPZR30w_Mode: { | |
| 894 sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace()); | |
| 895 if (!srcSpace) { | |
| 896 return SkStringPrintf("Cannot test color correction without a sr c profile."); | |
| 897 } else if (!srcSpace->gammas()->isValues()) { | |
| 898 // The conversion here doesn't cover all of the images that I've uploaded for | |
| 899 // testing. Once we support all of them, this should be a fatal error. | |
|
scroggo
2016/05/23 16:12:38
FIXME?
msarett
2016/05/23 16:20:03
Done.
| |
| 900 return Error::Nonfatal("Unimplemented gamma conversion."); | |
| 901 } | |
| 902 | |
| 903 // Build a matrix to transform to dst gamut. | |
| 904 // srcToDst = inverse(dstToXYZ) * srcToXYZ | |
| 905 const SkMatrix44& srcToXYZ = srcSpace->xyz(); | |
| 906 const SkMatrix44& dstToXYZ = fDstSpace->xyz(); | |
| 907 SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); | |
| 908 dstToXYZ.invert(&srcToDst); | |
| 909 srcToDst.postConcat(srcToXYZ); | |
| 910 | |
| 911 for (uint32_t y = 0; y < height; y++) { | |
| 912 for (uint32_t x = 0; x < width; x++) { | |
| 913 // Extract floats. | |
| 914 uint32_t* pixelPtr = (uint32_t*) bitmap.getAddr(x, y); | |
| 915 float src[3]; | |
| 916 src[0] = ((*pixelPtr >> 0) & 0xFF) / 255.0f; | |
| 917 src[1] = ((*pixelPtr >> 8) & 0xFF) / 255.0f; | |
| 918 src[2] = ((*pixelPtr >> 16) & 0xFF) / 255.0f; | |
| 919 | |
| 920 // Convert to linear. | |
| 921 src[0] = pow(src[0], srcSpace->gammas()->fRed.fValue); | |
| 922 src[1] = pow(src[1], srcSpace->gammas()->fGreen.fValue); | |
| 923 src[2] = pow(src[2], srcSpace->gammas()->fBlue.fValue); | |
| 924 | |
| 925 // Conver to dst gamut. | |
|
scroggo
2016/05/23 16:12:38
Convert*
msarett
2016/05/23 16:20:03
Done.
| |
| 926 float dst[3]; | |
| 927 dst[0] = src[0]*srcToDst.getFloat(0, 0) + src[1]*srcToDst.ge tFloat(1, 0) + | |
| 928 src[2]*srcToDst.getFloat(2, 0) + srcToDst.getFloat( 3, 0); | |
| 929 dst[1] = src[0]*srcToDst.getFloat(0, 1) + src[1]*srcToDst.ge tFloat(1, 1) + | |
| 930 src[2]*srcToDst.getFloat(2, 1) + srcToDst.getFloat( 3, 1); | |
| 931 dst[2] = src[0]*srcToDst.getFloat(0, 2) + src[1]*srcToDst.ge tFloat(1, 2) + | |
| 932 src[2]*srcToDst.getFloat(2, 2) + srcToDst.getFloat( 3, 2); | |
| 933 | |
| 934 // Convert to dst gamma. | |
| 935 dst[0] = pow(dst[0], 1.0f / fDstSpace->gammas()->fRed.fValue ); | |
| 936 dst[1] = pow(dst[1], 1.0f / fDstSpace->gammas()->fGreen.fVal ue); | |
| 937 dst[2] = pow(dst[2], 1.0f / fDstSpace->gammas()->fBlue.fValu e); | |
| 938 | |
| 939 *pixelPtr = SkPackARGB32NoCheck(((*pixelPtr >> 24) & 0xFF), | |
| 940 clampFloatToByte(dst[0]), | |
| 941 clampFloatToByte(dst[1]), | |
| 942 clampFloatToByte(dst[2])); | |
| 943 } | |
| 944 } | |
| 945 | |
| 946 canvas->drawBitmap(bitmap, 0, 0); | |
| 947 break; | |
| 948 } | |
| 874 default: | 949 default: |
| 875 SkASSERT(false); | 950 SkASSERT(false); |
| 876 return "Invalid fMode"; | 951 return "Invalid fMode"; |
| 877 } | 952 } |
| 878 return ""; | 953 return ""; |
| 879 } | 954 } |
| 880 | 955 |
| 881 SkISize ColorCodecSrc::size() const { | 956 SkISize ColorCodecSrc::size() const { |
| 882 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 957 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 883 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 958 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1459 skr.visit(i, drawsAsSingletonPictures); | 1534 skr.visit(i, drawsAsSingletonPictures); |
| 1460 } | 1535 } |
| 1461 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); | 1536 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); |
| 1462 | 1537 |
| 1463 canvas->drawPicture(macroPic); | 1538 canvas->drawPicture(macroPic); |
| 1464 return check_against_reference(bitmap, src, fSink); | 1539 return check_against_reference(bitmap, src, fSink); |
| 1465 }); | 1540 }); |
| 1466 } | 1541 } |
| 1467 | 1542 |
| 1468 } // namespace DM | 1543 } // namespace DM |
| OLD | NEW |