/* * This file is part of Krita * * SPDX-FileCopyrightText: 2021 L. E. Segovia * * SPDX-License-Identifier: GPL-2.0-or-later */ #include "KisDitherOpImpl.h" /** * THIS CLASS OVERRIDES THE STANDARD FACTORY. * Floating point CMYK uses different normalization for the color * and alpha channels. */ template class KisCmykDitherOpImpl : public KisDitherOpImpl { using srcChannelsType = typename srcCSTraits::channels_type; using dstChannelsType = typename dstCSTraits::channels_type; public: KisCmykDitherOpImpl(const KoID &srcId, const KoID &dstId) : KisDitherOpImpl(srcId, dstId) { } void dither(const quint8 *src, quint8 *dst, int x, int y) const override { ditherImpl(src, dst, x, y); } void dither(const quint8 *srcRowStart, int srcRowStride, quint8 *dstRowStart, int dstRowStride, int x, int y, int columns, int rows) const override { ditherImpl(srcRowStart, srcRowStride, dstRowStart, dstRowStride, x, y, columns, rows); } private: template::value, void>::type * = nullptr> inline void ditherImpl(const quint8 *src, quint8 *dst, int, int) const { memcpy(dst, src, srcCSTraits::pixelSize); } template::value, void>::type * = nullptr> inline void ditherImpl(const quint8 *src, quint8 *dst, int, int) const { const srcChannelsType *nativeSrc = srcCSTraits::nativeArray(src); dstChannelsType *nativeDst = dstCSTraits::nativeArray(dst); for (uint channelIndex = 0; channelIndex < srcCSTraits::channels_nb; ++channelIndex) { if (channelIndex == srcCSTraits::alpha_pos) { // The standard normalization. nativeDst[channelIndex] = KoColorSpaceMaths::scaleToA(nativeSrc[channelIndex]); } else { // Normalize using unitCMYKValue. nativeDst[channelIndex] = scaleToA(nativeSrc[channelIndex]); } } } template::type * = nullptr> inline void ditherImpl(const quint8 *src, quint8 *dst, int x, int y) const { const srcChannelsType *nativeSrc = srcCSTraits::nativeArray(src); dstChannelsType *nativeDst = dstCSTraits::nativeArray(dst); float f = factor(x, y); float s = scale(); // In (non-integer) CMYK, all channels except alpha are normalized // to a different range, [0, 100]. for (uint channelIndex = 0; channelIndex < srcCSTraits::channels_nb; ++channelIndex) { if (channelIndex == srcCSTraits::alpha_pos) { // The standard normalization. float c = KoColorSpaceMaths::scaleToA(nativeSrc[channelIndex]); c = KisDitherMaths::apply_dither(c, f, s); nativeDst[channelIndex] = KoColorSpaceMaths::scaleToA(c); ; } else { // Normalize using unitCMYKValue. float c = normalize(nativeSrc[channelIndex]); c = KisDitherMaths::apply_dither(c, f, s); nativeDst[channelIndex] = denormalize(c); ; } } } template::value, void>::type * = nullptr> inline void ditherImpl(const quint8 *srcRowStart, int srcRowStride, quint8 *dstRowStart, int dstRowStride, int, int, int columns, int rows) const { const quint8 *nativeSrc = srcRowStart; quint8 *nativeDst = dstRowStart; for (int y = 0; y < rows; ++y) { memcpy(nativeDst, nativeSrc, srcCSTraits::pixelSize * columns); nativeSrc += srcRowStride; nativeDst += dstRowStride; } } template::value, void>::type * = nullptr> inline void ditherImpl(const quint8 *srcRowStart, int srcRowStride, quint8 *dstRowStart, int dstRowStride, int, int, int columns, int rows) const { const quint8 *nativeSrc = srcRowStart; quint8 *nativeDst = dstRowStart; for (int y = 0; y < rows; ++y) { const srcChannelsType *srcPtr = srcCSTraits::nativeArray(nativeSrc); dstChannelsType *dstPtr = dstCSTraits::nativeArray(nativeDst); for (int x = 0; x < columns; ++x) { for (uint channelIndex = 0; channelIndex < srcCSTraits::channels_nb; ++channelIndex) { if (channelIndex == srcCSTraits::alpha_pos) { // The standard normalization. dstPtr[channelIndex] = KoColorSpaceMaths::scaleToA(srcPtr[channelIndex]); } else { // Normalize using unitCMYKValue. dstPtr[channelIndex] = scaleToA(srcPtr[channelIndex]); } } srcPtr += srcCSTraits::channels_nb; dstPtr += dstCSTraits::channels_nb; } nativeSrc += srcRowStride; nativeDst += dstRowStride; } } template::type * = nullptr> inline void ditherImpl(const quint8 *srcRowStart, int srcRowStride, quint8 *dstRowStart, int dstRowStride, int x, int y, int columns, int rows) const { const quint8 *nativeSrc = srcRowStart; quint8 *nativeDst = dstRowStart; float s = scale(); for (int a = 0; a < rows; ++a) { const srcChannelsType *srcPtr = srcCSTraits::nativeArray(nativeSrc); dstChannelsType *dstPtr = dstCSTraits::nativeArray(nativeDst); for (int b = 0; b < columns; ++b) { float f = factor(x + b, y + a); for (uint channelIndex = 0; channelIndex < srcCSTraits::channels_nb; ++channelIndex) { if (channelIndex == srcCSTraits::alpha_pos) { // The standard normalization. float c = KoColorSpaceMaths::scaleToA(srcPtr[channelIndex]); c = KisDitherMaths::apply_dither(c, f, s); dstPtr[channelIndex] = KoColorSpaceMaths::scaleToA(c); ; } else { // Normalize using unitCMYKValue. float c = normalize(srcPtr[channelIndex]); c = KisDitherMaths::apply_dither(c, f, s); dstPtr[channelIndex] = denormalize(c); ; } } srcPtr += srcCSTraits::channels_nb; dstPtr += dstCSTraits::channels_nb; } nativeSrc += srcRowStride; nativeDst += dstRowStride; } } // CMYK-specific normalization bits template::is_integer, void>::type * = nullptr> inline float normalize(A value) const { return static_cast(value) / KoColorSpaceMathsTraits::unitValue; }; template::is_integer, void>::type * = nullptr> inline float normalize(A value) const { return static_cast(value) / KoCmykColorSpaceMathsTraits::unitValueCMYK; }; template::is_integer, void>::type * = nullptr> inline A denormalize(float value) const { return static_cast(value * static_cast(KoColorSpaceMathsTraits::unitValue)); }; template::is_integer, void>::type * = nullptr> inline A denormalize(float value) const { return static_cast(value * static_cast(KoCmykColorSpaceMathsTraits::unitValueCMYK)); }; template inline B scaleToA(A c) const { return denormalize(normalize(c)); } template::is_integer, void>::type * = nullptr> inline constexpr float scale() const { return 0.f; // no dithering for floating point } template::is_integer, void>::type * = nullptr> inline constexpr float scale() const { return 1.f / static_cast(1 << dstCSTraits::depth); } template::type * = nullptr> inline float factor(int x, int y) const { return KisDitherMaths::dither_factor_bayer_8(x, y); } template::type * = nullptr> inline float factor(int x, int y) const { return KisDitherMaths::dither_factor_blue_noise_64(x, y); } }; template inline void addCmykDitherOpsByDepth(KoColorSpace *cs, const KoID &dstDepth) { const KoID &srcDepth {cs->colorDepthId()}; cs->addDitherOp(new KisCmykDitherOpImpl(srcDepth, dstDepth)); cs->addDitherOp(new KisCmykDitherOpImpl(srcDepth, dstDepth)); cs->addDitherOp(new KisCmykDitherOpImpl(srcDepth, dstDepth)); } template inline void addStandardDitherOps(KoColorSpace *cs) { static_assert(std::is_same::value || std::is_same::value || #ifdef HAVE_OPENEXR std::is_same::value || #endif std::is_same::value, "Missing colorspace, add a transform case!"); KIS_ASSERT(cs->pixelSize() == srcCSTraits::pixelSize); addCmykDitherOpsByDepth(cs, Integer8BitsColorDepthID); addCmykDitherOpsByDepth(cs, Integer16BitsColorDepthID); #ifdef HAVE_OPENEXR addCmykDitherOpsByDepth(cs, Float16BitsColorDepthID); #endif addCmykDitherOpsByDepth(cs, Float32BitsColorDepthID); }