File indexing completed on 2024-05-26 04:32:08

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Sahil Nagpal <nagpal.sahil01@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kis_burnmidtones_adjustment.h"
0008 #include <KoConfig.h>
0009 
0010 #include <kis_debug.h>
0011 #include <klocalizedstring.h>
0012 #ifdef HAVE_OPENEXR
0013 #include <half.h>
0014 #endif
0015 
0016 #include <KoColorConversions.h>
0017 #include <KoColorModelStandardIds.h>
0018 #include <KoColorSpace.h>
0019 #include <KoColorSpaceTraits.h>
0020 #include <KoColorTransformation.h>
0021 #include <KoID.h>
0022 
0023 template<typename _channel_type_, typename traits>
0024 class KisBurnMidtonesAdjustment : public KoColorTransformation
0025 {
0026     typedef traits RGBTrait;
0027     typedef typename RGBTrait::Pixel RGBPixel;
0028 
0029 public:
0030     KisBurnMidtonesAdjustment(){}
0031 
0032     void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
0033     {
0034         const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8);
0035         RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8);
0036         float value_red, value_green, value_blue;
0037         const float factor(1.0 + exposure * (0.333333));
0038         while(nPixels > 0) {
0039 
0040             value_red = pow((float)KoColorSpaceMaths<_channel_type_, float>::scaleToA(src->red), factor);
0041             value_green = pow((float)KoColorSpaceMaths<_channel_type_, float>::scaleToA(src->green), factor);
0042             value_blue = pow((float)KoColorSpaceMaths<_channel_type_, float>::scaleToA(src->blue), factor);
0043             
0044             dst->red = KoColorSpaceMaths< float, _channel_type_ >::scaleToA(value_red);
0045             dst->green = KoColorSpaceMaths< float, _channel_type_ >::scaleToA(value_green);
0046             dst->blue = KoColorSpaceMaths< float, _channel_type_ >::scaleToA(value_blue);
0047             dst->alpha = src->alpha;
0048             
0049             --nPixels;
0050             ++src;
0051             ++dst;
0052         }
0053     }
0054 
0055     QList<QString> parameters() const override
0056     {
0057         QList<QString> list;
0058         list << "exposure";
0059         return list;
0060     }
0061 
0062     int parameterId(const QString& name) const override
0063     {
0064         if (name == "exposure")
0065         return 0;
0066         return -1;
0067     }
0068 
0069     void setParameter(int id, const QVariant& parameter) override
0070     {
0071         switch(id)
0072         {
0073         case 0:
0074             exposure = parameter.toDouble();
0075             break;
0076         default:
0077             ;
0078         }
0079     }
0080 private:
0081 
0082     float exposure {0.0f};
0083  };
0084 
0085  KisBurnMidtonesAdjustmentFactory::KisBurnMidtonesAdjustmentFactory()
0086     : KoColorTransformationFactory("BurnMidtones")
0087 {
0088 }
0089 
0090 QList< QPair< KoID, KoID > > KisBurnMidtonesAdjustmentFactory::supportedModels() const
0091 {
0092     QList< QPair< KoID, KoID > > l;
0093     l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer8BitsColorDepthID));
0094     l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer16BitsColorDepthID));
0095     l.append(QPair< KoID, KoID >(RGBAColorModelID , Float16BitsColorDepthID));
0096     l.append(QPair< KoID, KoID >(RGBAColorModelID , Float32BitsColorDepthID));
0097     return l;
0098 }
0099 
0100 KoColorTransformation* KisBurnMidtonesAdjustmentFactory::createTransformation(const KoColorSpace* colorSpace, QHash<QString, QVariant> parameters) const
0101 {
0102     KoColorTransformation * adj;
0103     if (colorSpace->colorModelId() != RGBAColorModelID) {
0104         dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisBurnMidtonesAdjustment::createTransformation";
0105         return 0;
0106     }
0107     if (colorSpace->colorDepthId() == Float32BitsColorDepthID) {
0108         adj = new KisBurnMidtonesAdjustment< float, KoRgbTraits < float > >();
0109     }
0110 #ifdef HAVE_OPENEXR
0111     else if (colorSpace->colorDepthId() == Float16BitsColorDepthID) {
0112         adj = new KisBurnMidtonesAdjustment< half, KoRgbTraits < half > >();
0113     }
0114 #endif
0115     else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) {
0116         adj = new KisBurnMidtonesAdjustment< quint16, KoBgrTraits < quint16 > >();
0117     } else if (colorSpace->colorDepthId() == Integer8BitsColorDepthID) {
0118         adj = new KisBurnMidtonesAdjustment< quint8, KoBgrTraits < quint8 > >();
0119     } else {
0120         dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisBurnMidtonesAdjustment::createTransformation";
0121         return 0;
0122     }
0123     adj->setParameters(parameters);
0124     return adj;
0125 
0126 }