summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/filters/fastcolortransfer/fastcolortransfer.cc
blob: c43e5690044e002b6f943a1982e25f0e3c499c7e (plain)
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
30
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * This file is part of Chalk
 *
 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "fastcolortransfer.h"

#include <kgenericfactory.h>
#include <kurlrequester.h>

#include <kis_colorspace_factory_registry.h>
#include <kis_doc.h>
#include <kis_image.h>
#include <kis_iterators_pixel.h>
#include <kis_meta_registry.h>
#include <kis_paint_device.h>

#include "kis_wdg_fastcolortransfer.h"
#include "wdgfastcolortransfer.h"

typedef KGenericFactory<FastColorTransferPlugin> ChalkFastColorTransferFactory;
K_EXPORT_COMPONENT_FACTORY( chalkfastcolortransfer, ChalkFastColorTransferFactory( "chalk" ) )


FastColorTransferPlugin::FastColorTransferPlugin(TQObject *parent, const char *name, const TQStringList &)
        : KParts::Plugin(parent, name)
{
    setInstance(ChalkFastColorTransferFactory::instance());

    kdDebug(41006) << "Color Transfer Filter plugin. Class: "
          << className()
          << ", Parent: "
          << parent -> className()
          << "\n";

    if (parent->inherits("KisFilterRegistry")) {
        KisFilterRegistry * manager = dynamic_cast<KisFilterRegistry *>(parent);
        manager->add(new KisFilterFastColorTransfer());
    }
}

FastColorTransferPlugin::~FastColorTransferPlugin()
{
}

KisFilterFastColorTransfer::KisFilterFastColorTransfer() : KisFilter(id(), "colors", i18n("&Color Transfer..."))
{
}


KisFilterConfigWidget * KisFilterFastColorTransfer::createConfigurationWidget(TQWidget* parent, KisPaintDeviceSP )
{
    return new KisWdgFastColorTransfer(this, parent, "configuration of color to alpha");
}

KisFilterConfiguration* KisFilterFastColorTransfer::configuration(TQWidget* w)
{
    KisWdgFastColorTransfer * wCTA = dynamic_cast<KisWdgFastColorTransfer*>(w);
    KisFilterConfiguration* config = new KisFilterConfiguration(id().id(), 1);
    if(wCTA)
    {
        config->setProperty("filename", wCTA->widget()->fileNameURLRequester->url() );
    }
    return config;
}

void KisFilterFastColorTransfer::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* config, const TQRect& rect)
{
    kdDebug() << "Start transfering color" << endl;
    TQVariant value;
    TQString fileName;
    if (config && config->getProperty("filename", value))
    {
        fileName = value.toString();
    } else {
        kdDebug() << "No file name for the reference image was specified." << endl;
        return;
    }

    KisPaintDeviceSP ref;

    KisDoc d;
    d.import(fileName);
    KisImageSP importedImage = d.currentImage();

    if(importedImage)
    {
        ref = importedImage->projection();
    }
    if(!ref)
    {
        kdDebug() << "No reference image was specified." << endl;
        return;
    }

    // Convert ref and src to LAB
    KisColorSpace* labCS = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("LABA"),"");
    if(!labCS)
    {
        kdDebug() << "The LAB colorspace is not available." << endl;
        return;
    }

    setProgressTotalSteps(5);

    KisColorSpace* oldCS = src->colorSpace();
    KisPaintDeviceSP srcLAB = new KisPaintDevice(*src.data());
    srcLAB->convertTo(labCS);
    ref->convertTo(labCS);

    setProgress( 1 );

    // Compute the means and sigmas of src
    double meanL_src = 0., meanA_src = 0., meanB_src = 0.;
    double sigmaL_src = 0., sigmaA_src = 0., sigmaB_src = 0.;
    KisRectIteratorPixel srcLABIt = srcLAB->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), false );
    while(!srcLABIt.isDone())
    {
        const TQ_UINT16* data = reinterpret_cast<const TQ_UINT16*>(srcLABIt.oldRawData());
        TQ_UINT32 L = data[0];
        TQ_UINT32 A = data[1];
        TQ_UINT32 B = data[2];
        meanL_src += L;
        meanA_src += A;
        meanB_src += B;
        sigmaL_src += L*L;
        sigmaA_src += A*A;
        sigmaB_src += B*B;
        ++srcLABIt;
    }

    setProgress( 3 );

    double size = 1. / ( rect.width() * rect.height() );
    meanL_src *= size;
    meanA_src *= size;
    meanB_src *= size;
    sigmaL_src *= size;
    sigmaA_src *= size;
    sigmaB_src *= size;
    kdDebug() << size << " " << meanL_src << " " << meanA_src << " " << meanB_src << " " << sigmaL_src << " " << sigmaA_src << " " << sigmaB_src << endl;
    // Compute the means and sigmas of src
    double meanL_ref = 0., meanA_ref = 0., meanB_ref = 0.;
    double sigmaL_ref = 0., sigmaA_ref = 0., sigmaB_ref = 0.;
    KisRectIteratorPixel refIt = ref->createRectIterator(0, 0, importedImage->width(), importedImage->height(), false );
    while(!refIt.isDone())
    {
        const TQ_UINT16* data = reinterpret_cast<const TQ_UINT16*>(refIt.oldRawData());
        TQ_UINT32 L = data[0];
        TQ_UINT32 A = data[1];
        TQ_UINT32 B = data[2];
        meanL_ref += L;
        meanA_ref += A;
        meanB_ref += B;
        sigmaL_ref += L*L;
        sigmaA_ref += A*A;
        sigmaB_ref += B*B;
        ++refIt;
    }

    setProgress( 4 );

    size = 1. / ( importedImage->width() * importedImage->height() );
    meanL_ref *= size;
    meanA_ref *= size;
    meanB_ref *= size;
    sigmaL_ref *= size;
    sigmaA_ref *= size;
    sigmaB_ref *= size;
    kdDebug() << size << " " << meanL_ref << " " << meanA_ref << " " << meanB_ref << " " << sigmaL_ref << " " << sigmaA_ref << " " << sigmaB_ref << endl;

    // Transfer colors
    dst->convertTo(labCS);
    {
        double coefL = sqrt((sigmaL_ref - meanL_ref * meanL_ref) / (sigmaL_src - meanL_src * meanL_src));
        double coefA = sqrt((sigmaA_ref - meanA_ref * meanA_ref) / (sigmaA_src - meanA_src * meanA_src));
        double coefB = sqrt((sigmaB_ref - meanB_ref * meanB_ref) / (sigmaB_src - meanB_src * meanB_src));
        kdDebug() << coefL << " " << coefA << " " << coefB << endl;
        KisRectIteratorPixel dstIt = dst->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), true );
        while(!dstIt.isDone())
        {
            TQ_UINT16* data = reinterpret_cast<TQ_UINT16*>(dstIt.rawData());
            data[0] = (TQ_UINT16)CLAMP( ( (double)data[0] - meanL_src) * coefL + meanL_ref, 0., 65535.);
            data[1] = (TQ_UINT16)CLAMP( ( (double)data[1] - meanA_src) * coefA + meanA_ref, 0., 65535.);
            data[2] = (TQ_UINT16)CLAMP( ( (double)data[2] - meanB_src) * coefB + meanB_ref, 0., 65535.);
            ++dstIt;
        }
    }
    dst->convertTo(oldCS);
    setProgressDone(); // Must be called even if you don't really support progression
}