Chromium Code Reviews| Index: Source/platform/graphics/ColorSpaceFilter.cpp |
| diff --git a/Source/platform/graphics/ColorSpaceFilter.cpp b/Source/platform/graphics/ColorSpaceFilter.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..721628539fd66038bb4449aff8ca9e1c7063afef |
| --- /dev/null |
| +++ b/Source/platform/graphics/ColorSpaceFilter.cpp |
| @@ -0,0 +1,209 @@ |
| +/* |
| + * Copyright (C) 2014 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "platform/graphics/ColorSpaceFilter.h" |
| + |
| +#include "platform/graphics/ColorSpaceProfile.h" |
| +#include "platform/graphics/ColorSpaceTransform.h" |
| +#include "third_party/skia/include/core/SkColorFilter.h" |
| +#include "third_party/skia/include/core/SkColorPriv.h" |
| +#include "third_party/skia/include/core/SkReadBuffer.h" |
| +#include "third_party/skia/include/core/SkString.h" |
| +#include "third_party/skia/include/core/SkUnPreMultiply.h" |
| +#include "third_party/skia/include/core/SkWriteBuffer.h" |
| +#include "third_party/skia/include/core/SkWriter32.h" |
| +#include "wtf/RefPtr.h" |
| + |
| +#if USE(QCMSLIB) |
| + |
| +class ColorSpaceTransformFilter : public SkColorFilter { |
| +public: |
| + static PassRefPtr<ColorSpaceTransformFilter> create(qcms_transform* transform) |
| + { |
| + return adoptRef(new ColorSpaceTransformFilter(transform)); |
| + } |
| + |
| + // Color space transforms do not change pixel alpha. |
| + |
| + virtual uint32_t getFlags() const |
| + { |
| + return this->SkColorFilter::getFlags() | SkColorFilter::kAlphaUnchanged_Flag; |
| + } |
| + |
| + virtual void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const |
| + { |
| + // 1) Copy |src| pixel values to |dst| converting from BGRA to RGBA format. |
| + |
| + int offset = count; // Record the offset to the first alpha pixel (if any). |
| + |
| + for (int i = 0; i < count; ++i) { |
| + SkPMColor pixel = src[i]; |
| + |
| + if (SkGetPackedA32(pixel) != 255) { |
| + offset = i; |
| + break; |
| + } |
| + |
| + dst[i] = SkSwizzle_RB(pixel); |
| + } |
| + |
| + // 2) Unpremultiply remaining |src| pixel values into |dst| in RGBA format. |
| + |
| + static const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
| + |
| + unsigned char* output = reinterpret_cast<unsigned char*>(&dst[offset]); |
| + |
| + for (int i = offset; i < count; ++i) { |
| + SkPMColor pixel = src[i]; |
| + |
| + unsigned b = SkGetPackedB32(pixel); |
| + unsigned g = SkGetPackedG32(pixel); |
| + unsigned r = SkGetPackedR32(pixel); |
| + unsigned a = SkGetPackedA32(pixel); |
| + |
| + if (a != 255) { |
| + SkUnPreMultiply::Scale scale = table[a]; |
| + b = SkUnPreMultiply::ApplyScale(scale, b); |
| + g = SkUnPreMultiply::ApplyScale(scale, g); |
| + r = SkUnPreMultiply::ApplyScale(scale, r); |
| + } |
| + |
| + ASSERT(b <= 255); |
| + ASSERT(g <= 255); |
| + ASSERT(r <= 255); |
| + ASSERT(a <= 255); |
| + |
| + *output++ = r; |
| + *output++ = g; |
| + *output++ = b; |
| + *output++ = a; |
| + } |
| + |
| + // 3) Color correct the |dst| RGBA pixels in-situ - request BGRA output. |
| + |
| + qcms_transform_data_type(m_transform->transform(), dst, dst, count, QCMS_OUTPUT_BGRX); |
| + |
| + // 4) Premultiply the |dst| BGRA pixels (starting from the alpha offset). |
| + |
| + static const unsigned kDiv255 = static_cast<unsigned>(1.0 / 255 * (1 << 24)) + 1; |
| + |
| + unsigned char* input = reinterpret_cast<unsigned char*>(&dst[offset]); |
| + |
| + for (int i = offset; i < count; ++i) { |
| + unsigned b = *input++; |
| + unsigned g = *input++; |
| + unsigned r = *input++; |
| + unsigned a = *input++; |
| + |
| + if (a != 255) { |
| + unsigned alpha = a * kDiv255; |
| + b = SkMulS16(b, alpha) >> 24; |
| + g = SkMulS16(g, alpha) >> 24; |
| + r = SkMulS16(r, alpha) >> 24; |
| + } |
| + |
| + ASSERT(b <= 255); |
| + ASSERT(g <= 255); |
| + ASSERT(r <= 255); |
| + ASSERT(a <= 255); |
| + |
| + dst[i] = SkPackARGB32NoCheck(a, r, g, b); |
| + } |
| + } |
| + |
| + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ColorSpaceTransformFilter) |
| + |
| +protected: |
| + explicit ColorSpaceTransformFilter(qcms_transform* transform) |
| + : m_transform(WebCore::ColorSpaceTransform::create(transform)) |
| + { |
| + ASSERT(transform); |
| + } |
| + |
| + void flatten(SkWriteBuffer& sender) const |
| + { |
| + ASSERT(m_transform.get()); // Once you go flat, you never go back. |
| + |
| + this->SkColorFilter::flatten(sender); |
| + sender.writeFunctionPtr(const_cast<ColorSpaceTransformFilter*>(this)->m_transform.release().leakRef()); |
|
Stephen White
2014/07/03 18:38:17
This definitely won't work out-of-process. For now
|
| + } |
| + |
| + explicit ColorSpaceTransformFilter(SkReadBuffer& receiver) |
| + : SkColorFilter(receiver) |
| + { |
| + m_transform = adoptRef(static_cast<WebCore::ColorSpaceTransform*>(receiver.readFunctionPtr())); |
| + } |
| + |
| +#ifndef SK_IGNORE_TO_STRING |
| + |
| + void toString(SkString* str) const |
| + { |
| + str->printf("ColorSpaceTransformFilter: (transform: %p)", m_transform.get()); |
| + } |
| + |
| +#endif // SK_IGNORE_TO_STRING |
| + |
| +private: |
| + RefPtr<WebCore::ColorSpaceTransform> m_transform; |
| +}; |
| + |
| +inline PassRefPtr<SkColorFilter> createColorSpaceTransformFilter(qcms_transform* transform) |
| +{ |
| + if (!transform) |
| + return nullptr; |
| + |
| + return ColorSpaceTransformFilter::create(transform); |
| +} |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtr<SkColorFilter> createColorSpaceFilter(ColorSpaceProfile* source, ColorSpaceProfile* target) |
| +{ |
| + if (!source || !target) |
| + return nullptr; |
| + |
| + return createColorSpaceTransformFilter(qcms_transform_create(source->profile(), QCMS_DATA_RGBA_8, target->profile(), QCMS_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL)); |
| +} |
| + |
| +} // namespace WebCore |
| + |
| +#else |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtr<SkColorFilter> createColorSpaceFilter(ColorSpaceProfile*, ColorSpaceProfile*); |
| +{ |
| + return nullptr; |
| +} |
| + |
| +} // namespace WebCore |
| + |
| +#endif // USE(QCMSLIB) |