indexcolors.cpp 4.98 KB
Newer Older
Halla Rempt's avatar
Halla Rempt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright 2014 Manuel Riecke <spell1337@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software.
 */

#include "indexcolors.h"

#include <kpluginfactory.h>
#include <filter/kis_filter_registry.h>
#include <kis_global.h>
#include <KoColorSpaceMaths.h>
#include <KoColorSpaceRegistry.h>
30
#include <filter/kis_color_transformation_configuration.h>
Halla Rempt's avatar
Halla Rempt committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <widgets/kis_multi_integer_filter_widget.h>

#include "kiswdgindexcolors.h"
#include "palettegeneratorconfig.h"

K_PLUGIN_FACTORY(IndexColorsFactory, registerPlugin<IndexColors>();)
K_EXPORT_PLUGIN(IndexColorsFactory("krita"))

IndexColors::IndexColors(QObject *parent, const QVariantList &)
    : QObject(parent)
{
    KisFilterRegistry::instance()->add(KisFilterSP(new KisFilterIndexColors()));
}

IndexColors::~IndexColors()
{
}

KisFilterIndexColors::KisFilterIndexColors() : KisColorTransformationFilter(id(), categoryArtistic(), i18n("&Index Colors..."))
{
    setColorSpaceIndependence(FULLY_INDEPENDENT); // Technically it is TO_LAB16 but that would only display a warning we don't want
    // This filter will always degrade the color space, that is it's purpose
    setSupportsPainting(true);
    setShowConfigurationWidget(true);
}

KoColorTransformation* KisFilterIndexColors::createTransformation(const KoColorSpace* cs, const KisFilterConfiguration* config) const
{
59
    IndexColorPalette pal;
Halla Rempt's avatar
Halla Rempt committed
60 61 62

    PaletteGeneratorConfig palCfg;
    palCfg.fromByteArray(config->getProperty("paletteGen").toByteArray());
63 64
    pal = palCfg.generate();
    if(config->getBool("reduceColorsEnabled"))
Halla Rempt's avatar
Halla Rempt committed
65
    {
66 67 68
        int maxClrs = config->getInt("colorLimit");
        while(pal.numColors() > maxClrs)
            pal.mergeMostReduantColors();
Halla Rempt's avatar
Halla Rempt committed
69 70 71 72 73 74 75 76 77 78 79 80 81
    }

    pal.similarityFactors.L = config->getFloat("LFactor");
    pal.similarityFactors.a = config->getFloat("aFactor");
    pal.similarityFactors.b = config->getFloat("bFactor");
    return new KisIndexColorTransformation(pal, cs, config->getInt("alphaSteps"));
}

KisConfigWidget* KisFilterIndexColors::createConfigurationWidget(QWidget* parent, const KisPaintDeviceSP dev) const
{
    Q_UNUSED(dev);
    KisWdgIndexColors* w = new KisWdgIndexColors(parent);
    w->setup(
82 83
        QStringList() << i18nc("Color palette shade", "Bright") << i18nc("Color palette shade", "Light") << i18nc("Color palette shade", "Base") << i18nc("Color palette shade", "Shadow")
        , 4
Halla Rempt's avatar
Halla Rempt committed
84 85 86 87 88 89
    );
    return w;
}

KisFilterConfiguration* KisFilterIndexColors::factoryConfiguration(const KisPaintDeviceSP) const
{
90
    KisColorTransformationConfiguration* config = new KisColorTransformationConfiguration(id().id(), 0);
Halla Rempt's avatar
Halla Rempt committed
91 92 93 94 95 96 97

    PaletteGeneratorConfig palCfg; // Default constructor is factory config
    config->setProperty("paletteGen",     palCfg.toByteArray());

    config->setProperty("LFactor",    1.f);
    config->setProperty("aFactor",    1.f);
    config->setProperty("bFactor",    1.f);
98 99 100
    config->setProperty("reduceColorsEnabled", false);
    config->setProperty("colorLimit", 32);
    config->setProperty("alphaSteps", 1);
Halla Rempt's avatar
Halla Rempt committed
101 102 103
    return config;
}

104
KisIndexColorTransformation::KisIndexColorTransformation(IndexColorPalette palette, const KoColorSpace* cs, int alphaSteps)
Halla Rempt's avatar
Halla Rempt committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    : m_colorSpace(cs),
      m_psize(cs->pixelSize())
{
    m_palette = palette;

    static const qreal max = KoColorSpaceMathsTraits<quint16>::max;
    if(alphaSteps > 0)
    {
        m_alphaStep = max / alphaSteps;
        m_alphaHalfStep = m_alphaStep / 2;
    }
    else
    {
        m_alphaStep = 0;
        m_alphaHalfStep = 0;
    }
}

void KisIndexColorTransformation::transform(const quint8* src, quint8* dst, qint32 nPixels) const
{
    union
    {
        quint16 laba[4];
        LabColor lab;
    } clr;
    while (nPixels--)
    {
        m_colorSpace->toLabA16(src, reinterpret_cast<quint8 *>(clr.laba), 1);
        clr.lab = m_palette.getNearestIndex(clr.lab);
        if(m_alphaStep)
        {
            quint16 amod = clr.laba[3] % m_alphaStep;
137
            clr.laba[3] = clr.laba[3] + (amod > m_alphaHalfStep ? m_alphaStep - amod : -amod);
Halla Rempt's avatar
Halla Rempt committed
138 139 140 141 142 143
        }
        m_colorSpace->fromLabA16(reinterpret_cast<quint8 *>(clr.laba), dst, 1);
        src += m_psize;
        dst += m_psize;
    }
}