Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "SkColorFilter.h" |
| 9 | #include "SkColorSpaceXformer.h" |
| 10 | #include "SkColorSpaceXform_Base.h" |
| 11 | #include "SkDrawLooper.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkImage_Base.h" |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 14 | #include "SkImageFilter.h" |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 15 | #include "SkImagePriv.h" |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 16 | #include "SkMakeUnique.h" |
| 17 | |
| 18 | std::unique_ptr<SkColorSpaceXformer> SkColorSpaceXformer::Make(sk_sp<SkColorSpace> dst) { |
| 19 | std::unique_ptr<SkColorSpaceXform> fromSRGB = SkColorSpaceXform_Base::New( |
| 20 | SkColorSpace::MakeSRGB().get(), dst.get(), SkTransferFunctionBehavior::kIgnore); |
| 21 | if (!fromSRGB) { |
| 22 | return nullptr; |
| 23 | } |
| 24 | |
| 25 | auto xformer = std::unique_ptr<SkColorSpaceXformer>(new SkColorSpaceXformer()); |
| 26 | xformer->fDst = std::move(dst); |
| 27 | xformer->fFromSRGB = std::move(fromSRGB); |
| 28 | return xformer; |
| 29 | } |
| 30 | |
| 31 | sk_sp<SkImage> SkColorSpaceXformer::apply(const SkImage* src) { |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 32 | return src->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 35 | sk_sp<SkImage> SkColorSpaceXformer::apply(const SkBitmap& src) { |
| 36 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(src, kNever_SkCopyPixelsMode); |
| 37 | if (!image) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 41 | sk_sp<SkImage> xformed = image->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 42 | // We want to be sure we don't let the kNever_SkCopyPixelsMode image escape this stack frame. |
| 43 | SkASSERT(xformed != image); |
| 44 | return xformed; |
| 45 | } |
| 46 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 47 | sk_sp<SkColorFilter> SkColorSpaceXformer::apply(const SkColorFilter* colorFilter) { |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 48 | return colorFilter->makeColorSpace(this); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Mike Klein | e908b94 | 2017-04-25 13:09:40 -0400 | [diff] [blame] | 51 | sk_sp<SkImageFilter> SkColorSpaceXformer::apply(const SkImageFilter* imageFilter) { |
| 52 | return imageFilter->makeColorSpace(this); |
| 53 | } |
| 54 | |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 55 | void SkColorSpaceXformer::apply(SkColor* xformed, const SkColor* srgb, int n) { |
| 56 | SkAssertResult(fFromSRGB->apply(SkColorSpaceXform::kBGRA_8888_ColorFormat, xformed, |
| 57 | SkColorSpaceXform::kBGRA_8888_ColorFormat, srgb, |
| 58 | n, kUnpremul_SkAlphaType)); |
| 59 | } |
| 60 | |
| 61 | SkColor SkColorSpaceXformer::apply(SkColor srgb) { |
| 62 | SkColor xformed; |
| 63 | this->apply(&xformed, &srgb, 1); |
| 64 | return xformed; |
| 65 | } |
| 66 | |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 67 | SkPaint SkColorSpaceXformer::apply(const SkPaint& src) { |
| 68 | SkPaint dst = src; |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 69 | |
| 70 | // All SkColorSpaces have the same black point. |
| 71 | if (src.getColor() & 0xffffff) { |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 72 | dst.setColor(this->apply(src.getColor())); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (auto shader = src.getShader()) { |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 76 | dst.setShader(shader->makeColorSpace(this)); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 79 | if (auto cf = src.getColorFilter()) { |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 80 | dst.setColorFilter(this->apply(cf)); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (auto looper = src.getDrawLooper()) { |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 84 | dst.setDrawLooper(looper->makeColorSpace(this)); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 87 | if (auto imageFilter = src.getImageFilter()) { |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 88 | dst.setImageFilter(this->apply(imageFilter)); |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Matt Sarett | c15bb7b | 2017-04-25 13:53:11 -0400 | [diff] [blame] | 91 | return dst; |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 92 | } |