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 "SkColorSpace_Base.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 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
818 } | 820 } |
819 return codec->getInfo().dimensions(); | 821 return codec->getInfo().dimensions(); |
820 } | 822 } |
821 | 823 |
822 Name ImageGenSrc::name() const { | 824 Name ImageGenSrc::name() const { |
823 return SkOSPath::Basename(fPath.c_str()); | 825 return SkOSPath::Basename(fPath.c_str()); |
824 } | 826 } |
825 | 827 |
826 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | 828 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ |
827 | 829 |
828 ColorCodecSrc::ColorCodecSrc(Path path, Mode mode) | 830 ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, sk_sp<SkColorSpace> dstSpace) |
829 : fPath(path) | 831 : fPath(path) |
830 , fMode(mode) | 832 , fMode(mode) |
833 , fDstSpace(dstSpace) | |
831 {} | 834 {} |
832 | 835 |
833 bool ColorCodecSrc::veto(SinkFlags flags) const { | 836 bool ColorCodecSrc::veto(SinkFlags flags) const { |
834 // Test to direct raster backends (8888 and 565). | 837 // Test to direct raster backends (8888 and 565). |
835 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; | 838 return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDir ect; |
836 } | 839 } |
837 | 840 |
841 static uint8_t clampFloatToByte(float v) { | |
842 v = v * 255.0f; | |
843 if (v > 255.0f) { | |
844 return 255; | |
845 } else if (v < 0.0f) { | |
846 return 0; | |
847 } else { | |
848 return (uint8_t) (v + 0.5f); | |
849 } | |
850 } | |
851 | |
838 Error ColorCodecSrc::draw(SkCanvas* canvas) const { | 852 Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
839 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { | 853 if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
840 return Error::Nonfatal("No need to test color correction to 565 backend. "); | 854 return Error::Nonfatal("No need to test color correction to 565 backend. "); |
841 } | 855 } |
842 | 856 |
843 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 857 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
844 if (!encoded) { | 858 if (!encoded) { |
845 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 859 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
846 } | 860 } |
847 | 861 |
848 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 862 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
849 if (nullptr == codec.get()) { | 863 if (nullptr == codec.get()) { |
850 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); | 864 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
851 } | 865 } |
852 | 866 |
853 SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType); | 867 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
854 SkBitmap bitmap; | 868 SkBitmap bitmap; |
855 if (!bitmap.tryAllocPixels(decodeInfo)) { | 869 if (!bitmap.tryAllocPixels(info)) { |
856 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), | 870 return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
857 decodeInfo.width(), decodeInfo.height()); | 871 info.width(), info.height()); |
872 } | |
873 | |
874 SkImageInfo decodeInfo = info; | |
875 if (kBaseline_Mode != fMode) { | |
876 decodeInfo = decodeInfo.makeColorType(kRGBA_8888_SkColorType); | |
877 } | |
878 | |
879 SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap. rowBytes()); | |
880 if (SkCodec::kSuccess != r) { | |
881 return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_st r(), r); | |
858 } | 882 } |
859 | 883 |
860 switch (fMode) { | 884 switch (fMode) { |
861 case kBaseline_Mode: | 885 case kBaseline_Mode: |
862 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowB ytes())) { | |
863 case SkCodec::kSuccess: | |
864 break; | |
865 default: | |
866 // Everything else is considered a failure. | |
867 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); | |
868 } | |
869 canvas->drawBitmap(bitmap, 0, 0); | 886 canvas->drawBitmap(bitmap, 0, 0); |
870 break; | 887 break; |
888 case kDst_HPZR30w_Mode: { | |
scroggo
2016/05/23 16:57:25
It almost seems like this should be a Sink. But it
msarett
2016/05/23 17:07:58
You're right that actually makes more logical sens
| |
889 sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace()); | |
890 if (!srcSpace) { | |
891 return SkStringPrintf("Cannot test color correction without a sr c profile."); | |
892 } else if (!as_CSB(srcSpace)->gammas()->isValues()) { | |
893 // FIXME (msarett): | |
894 // The conversion here doesn't cover all of the images that I've uploaded for | |
895 // testing. Once we support all of them, this should be a fatal error. | |
896 return Error::Nonfatal("Unimplemented gamma conversion."); | |
897 } | |
898 | |
899 // Build a matrix to transform to dst gamut. | |
900 // srcToDst = inverse(dstToXYZ) * srcToXYZ | |
901 const SkMatrix44& srcToXYZ = srcSpace->xyz(); | |
902 const SkMatrix44& dstToXYZ = fDstSpace->xyz(); | |
903 SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); | |
904 dstToXYZ.invert(&srcToDst); | |
905 srcToDst.postConcat(srcToXYZ); | |
906 | |
907 for (int y = 0; y < info.height(); y++) { | |
908 for (int x = 0; x < info.width(); x++) { | |
909 // Extract floats. | |
910 uint32_t* pixelPtr = (uint32_t*) bitmap.getAddr(x, y); | |
911 float src[3]; | |
912 src[0] = ((*pixelPtr >> 0) & 0xFF) / 255.0f; | |
913 src[1] = ((*pixelPtr >> 8) & 0xFF) / 255.0f; | |
914 src[2] = ((*pixelPtr >> 16) & 0xFF) / 255.0f; | |
915 | |
916 // Convert to linear. | |
917 src[0] = pow(src[0], as_CSB(srcSpace)->gammas()->fRed.fValue ); | |
918 src[1] = pow(src[1], as_CSB(srcSpace)->gammas()->fGreen.fVal ue); | |
919 src[2] = pow(src[2], as_CSB(srcSpace)->gammas()->fBlue.fValu e); | |
920 | |
921 // Convert to dst gamut. | |
922 float dst[3]; | |
923 dst[0] = src[0]*srcToDst.getFloat(0, 0) + src[1]*srcToDst.ge tFloat(1, 0) + | |
924 src[2]*srcToDst.getFloat(2, 0) + srcToDst.getFloat( 3, 0); | |
925 dst[1] = src[0]*srcToDst.getFloat(0, 1) + src[1]*srcToDst.ge tFloat(1, 1) + | |
926 src[2]*srcToDst.getFloat(2, 1) + srcToDst.getFloat( 3, 1); | |
927 dst[2] = src[0]*srcToDst.getFloat(0, 2) + src[1]*srcToDst.ge tFloat(1, 2) + | |
928 src[2]*srcToDst.getFloat(2, 2) + srcToDst.getFloat( 3, 2); | |
929 | |
930 // Convert to dst gamma. | |
931 dst[0] = pow(dst[0], 1.0f / as_CSB(fDstSpace)->gammas()->fRe d.fValue); | |
932 dst[1] = pow(dst[1], 1.0f / as_CSB(fDstSpace)->gammas()->fGr een.fValue); | |
933 dst[2] = pow(dst[2], 1.0f / as_CSB(fDstSpace)->gammas()->fBl ue.fValue); | |
934 | |
935 *pixelPtr = SkPackARGB32NoCheck(((*pixelPtr >> 24) & 0xFF), | |
936 clampFloatToByte(dst[0]), | |
937 clampFloatToByte(dst[1]), | |
938 clampFloatToByte(dst[2])); | |
939 } | |
940 } | |
941 | |
942 canvas->drawBitmap(bitmap, 0, 0); | |
943 break; | |
944 } | |
871 default: | 945 default: |
872 SkASSERT(false); | 946 SkASSERT(false); |
873 return "Invalid fMode"; | 947 return "Invalid fMode"; |
874 } | 948 } |
875 return ""; | 949 return ""; |
876 } | 950 } |
877 | 951 |
878 SkISize ColorCodecSrc::size() const { | 952 SkISize ColorCodecSrc::size() const { |
879 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 953 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
880 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 954 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1456 skr.visit(i, drawsAsSingletonPictures); | 1530 skr.visit(i, drawsAsSingletonPictures); |
1457 } | 1531 } |
1458 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); | 1532 sk_sp<SkPicture> macroPic(macroRec.finishRecordingAsPicture()); |
1459 | 1533 |
1460 canvas->drawPicture(macroPic); | 1534 canvas->drawPicture(macroPic); |
1461 return check_against_reference(bitmap, src, fSink); | 1535 return check_against_reference(bitmap, src, fSink); |
1462 }); | 1536 }); |
1463 } | 1537 } |
1464 | 1538 |
1465 } // namespace DM | 1539 } // namespace DM |
OLD | NEW |