void FilterEffect::copyPremultipliedImage(Uint8ClampedArray* destination, const IntRect& rect) { ASSERT(hasResult()); if (!m_premultipliedImageResult) { // We prefer a conversion from the image buffer. if (m_imageBufferResult) m_premultipliedImageResult = m_imageBufferResult->getPremultipliedImageData(IntRect(IntPoint(), m_absolutePaintRect.size())); else { IntSize inputSize(m_absolutePaintRect.size()); ASSERT(!ImageBuffer::sizeNeedsClamping(inputSize)); inputSize.scale(m_filter.filterScale()); m_premultipliedImageResult = Uint8ClampedArray::createUninitialized(inputSize.width() * inputSize.height() * 4); unsigned char* sourceComponent = m_unmultipliedImageResult->data(); unsigned char* destinationComponent = m_premultipliedImageResult->data(); unsigned char* end = sourceComponent + (inputSize.width() * inputSize.height() * 4); while (sourceComponent < end) { int alpha = sourceComponent[3]; destinationComponent[0] = static_cast<int>(sourceComponent[0]) * alpha / 255; destinationComponent[1] = static_cast<int>(sourceComponent[1]) * alpha / 255; destinationComponent[2] = static_cast<int>(sourceComponent[2]) * alpha / 255; destinationComponent[3] = alpha; sourceComponent += 4; destinationComponent += 4; } } } copyImageBytes(m_premultipliedImageResult.get(), destination, rect); }
void FilterEffect::copyUnmultipliedImage(Uint8ClampedArray* destination, const IntRect& rect) { ASSERT(hasResult()); if (!m_unmultipliedImageResult) { // We prefer a conversion from the image buffer. if (m_imageBufferResult) m_unmultipliedImageResult = m_imageBufferResult->getImageData(Unmultiplied, IntRect(IntPoint(), m_absolutePaintRect.size())); else { ASSERT(isFilterSizeValid(m_absolutePaintRect)); m_unmultipliedImageResult = Uint8ClampedArray::createUninitialized(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4); unsigned char* sourceComponent = m_premultipliedImageResult->data(); unsigned char* destinationComponent = m_unmultipliedImageResult->data(); unsigned char* end = sourceComponent + (m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4); while (sourceComponent < end) { int alpha = sourceComponent[3]; if (alpha) { destinationComponent[0] = static_cast<int>(sourceComponent[0]) * 255 / alpha; destinationComponent[1] = static_cast<int>(sourceComponent[1]) * 255 / alpha; destinationComponent[2] = static_cast<int>(sourceComponent[2]) * 255 / alpha; } else { destinationComponent[0] = 0; destinationComponent[1] = 0; destinationComponent[2] = 0; } destinationComponent[3] = alpha; sourceComponent += 4; destinationComponent += 4; } } } copyImageBytes(m_unmultipliedImageResult.get(), destination, rect); }