~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to imageplugins/channelmixer/channelmixertool.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2010-04-09 21:30:01 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409213001-4bfyibrd359rn7o3
Tags: 2:1.2.0-0ubuntu1
* New upstream release (LP: #560576)
* Remove all patches, fixed upstream
  - Remove quilt build-depend

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ============================================================
2
 
 *
3
 
 * This file is a part of digiKam project
4
 
 * http://www.digikam.org
5
 
 *
6
 
 * Date        : 2005-02-26
7
 
 * Description : image channels mixer.
8
 
 *
9
 
 * Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
10
 
 *
11
 
 * Load and save mixer gains methods inspired from
12
 
 * Gimp 2.2.3 and copyrighted 2002 by Martin Guldahl <mguldahl at xmission dot com>
13
 
 *
14
 
 * This program is free software; you can redistribute it
15
 
 * and/or modify it under the terms of the GNU General
16
 
 * Public License as published by the Free Software Foundation;
17
 
 * either version 2, or (at your option)
18
 
 * any later version.
19
 
 *
20
 
 * This program is distributed in the hope that it will be useful,
21
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 
 * GNU General Public License for more details.
24
 
 *
25
 
 * ============================================================ */
26
 
 
27
 
#include "channelmixertool.moc"
28
 
 
29
 
// C++ includes
30
 
 
31
 
#include <cstdio>
32
 
#include <cmath>
33
 
#include <cstring>
34
 
#include <cstdlib>
35
 
#include <cerrno>
36
 
 
37
 
// Qt includes
38
 
 
39
 
#include <QCheckBox>
40
 
#include <QColor>
41
 
#include <QFile>
42
 
#include <QFrame>
43
 
#include <QGridLayout>
44
 
#include <QGroupBox>
45
 
#include <QHBoxLayout>
46
 
#include <QLabel>
47
 
#include <QPainter>
48
 
#include <QPixmap>
49
 
#include <QPushButton>
50
 
#include <QSpinBox>
51
 
#include <QTimer>
52
 
#include <QToolButton>
53
 
 
54
 
// KDE includes
55
 
 
56
 
#include <kaboutdata.h>
57
 
#include <kapplication.h>
58
 
#include <kconfig.h>
59
 
#include <kcursor.h>
60
 
#include <kfiledialog.h>
61
 
#include <kglobal.h>
62
 
#include <kglobalsettings.h>
63
 
#include <khelpmenu.h>
64
 
#include <kicon.h>
65
 
#include <kiconloader.h>
66
 
#include <klocale.h>
67
 
#include <kmenu.h>
68
 
#include <kmessagebox.h>
69
 
#include <kselector.h>
70
 
#include <kstandarddirs.h>
71
 
#include <kvbox.h>
72
 
 
73
 
// LibKDcraw includes
74
 
 
75
 
#include <libkdcraw/rnuminput.h>
76
 
 
77
 
// Local includes
78
 
 
79
 
#include "colorgradientwidget.h"
80
 
#include "daboutdata.h"
81
 
#include "dimg.h"
82
 
#include "dimgimagefilters.h"
83
 
#include "editortoolsettings.h"
84
 
#include "histogrambox.h"
85
 
#include "histogramwidget.h"
86
 
#include "imagehistogram.h"
87
 
#include "imageiface.h"
88
 
#include "imageguidewidget.h"
89
 
#include "version.h"
90
 
 
91
 
using namespace KDcrawIface;
92
 
using namespace Digikam;
93
 
 
94
 
namespace DigikamChannelMixerImagesPlugin
95
 
{
96
 
 
97
 
class ChannelMixerToolPriv
98
 
{
99
 
public:
100
 
 
101
 
    ChannelMixerToolPriv() :
102
 
        configGroupName("channelmixer Tool"),
103
 
        configMonochromeEntry("Monochrome"),
104
 
        configPreserveLuminosityEntry("PreserveLuminosity"),
105
 
        configRedRedGainEntry("RedRedGain"),
106
 
        configRedGreenGainEntry("RedGreenGain"),
107
 
        configRedBlueGainEntry("RedBlueGain"),
108
 
        configGreenRedGainEntry("GreenRedGain"),
109
 
        configGreenGreenGainEntry("GreenGreenGain"),
110
 
        configGreenBlueGainEntry("GreenBlueGain"),
111
 
        configBlueRedGainEntry("BlueRedGain"),
112
 
        configBlueGreenGainEntry("BlueGreenGain"),
113
 
        configBlueBlueGainEntry("BlueBlueGain"),
114
 
        configBlackRedGainEntry("BlackRedGain"),
115
 
        configBlackGreenGainEntry("BlackGreenGain"),
116
 
        configBlackBlueGainEntry("BlackBlueGain"),
117
 
        configHistogramChannelEntry("Histogram Channel"),
118
 
        configHistogramScaleEntry("Histogram Scale"),
119
 
 
120
 
        destinationPreviewData(0),
121
 
        redRedGain(0.0),
122
 
        redGreenGain(0.0),
123
 
        redBlueGain(0.0),
124
 
        greenRedGain(0.0),
125
 
        greenGreenGain(0.0),
126
 
        greenBlueGain(0.0),
127
 
        blueRedGain(0.0),
128
 
        blueGreenGain(0.0),
129
 
        blueBlueGain(0.0),
130
 
        blackRedGain(0.0),
131
 
        blackGreenGain(0.0),
132
 
        blackBlueGain(0.0),
133
 
        resetButton(0),
134
 
        preserveLuminosity(0),
135
 
        monochrome(0),
136
 
        redGain(0),
137
 
        greenGain(0),
138
 
        blueGain(0),
139
 
        previewWidget(0),
140
 
        gboxSettings(0)
141
 
        {}
142
 
 
143
 
    const QString       configGroupName;
144
 
    const QString       configMonochromeEntry;
145
 
    const QString       configPreserveLuminosityEntry;
146
 
    const QString       configRedRedGainEntry;
147
 
    const QString       configRedGreenGainEntry;
148
 
    const QString       configRedBlueGainEntry;
149
 
    const QString       configGreenRedGainEntry;
150
 
    const QString       configGreenGreenGainEntry;
151
 
    const QString       configGreenBlueGainEntry;
152
 
    const QString       configBlueRedGainEntry;
153
 
    const QString       configBlueGreenGainEntry;
154
 
    const QString       configBlueBlueGainEntry;
155
 
    const QString       configBlackRedGainEntry;
156
 
    const QString       configBlackGreenGainEntry;
157
 
    const QString       configBlackBlueGainEntry;
158
 
    const QString       configHistogramChannelEntry;
159
 
    const QString       configHistogramScaleEntry;
160
 
 
161
 
    uchar*              destinationPreviewData;
162
 
 
163
 
    double              redRedGain;
164
 
    double              redGreenGain;
165
 
    double              redBlueGain;
166
 
    double              greenRedGain;
167
 
    double              greenGreenGain;
168
 
    double              greenBlueGain;
169
 
    double              blueRedGain;
170
 
    double              blueGreenGain;
171
 
    double              blueBlueGain;
172
 
    double              blackRedGain;
173
 
    double              blackGreenGain;
174
 
    double              blackBlueGain;
175
 
 
176
 
    QPushButton*        resetButton;
177
 
 
178
 
    QCheckBox*          preserveLuminosity;
179
 
    QCheckBox*          monochrome;
180
 
 
181
 
    RDoubleNumInput*    redGain;
182
 
    RDoubleNumInput*    greenGain;
183
 
    RDoubleNumInput*    blueGain;
184
 
 
185
 
    ImageGuideWidget*   previewWidget;
186
 
    EditorToolSettings* gboxSettings;
187
 
};
188
 
 
189
 
ChannelMixerTool::ChannelMixerTool(QObject* parent)
190
 
                : EditorTool(parent),
191
 
                  d(new ChannelMixerToolPriv)
192
 
{
193
 
    setObjectName("channelmixer");
194
 
    setToolName(i18n("Channel Mixer"));
195
 
    setToolIcon(SmallIcon("channelmixer"));
196
 
 
197
 
    // -------------------------------------------------------------
198
 
 
199
 
    d->destinationPreviewData = 0;
200
 
 
201
 
    d->previewWidget = new ImageGuideWidget;
202
 
    d->previewWidget->setWhatsThis(i18n("You can see here the image's color channels' "
203
 
                                        "gain adjustments preview. You can pick a color on the image "
204
 
                                        "to see the corresponding color level on the histogram."));
205
 
    setToolView(d->previewWidget);
206
 
    setPreviewModeMask(PreviewToolBar::AllPreviewModes);
207
 
    
208
 
    // -------------------------------------------------------------
209
 
 
210
 
    d->gboxSettings = new EditorToolSettings;
211
 
    d->gboxSettings->setButtons(EditorToolSettings::Default|
212
 
                                EditorToolSettings::Load|
213
 
                                EditorToolSettings::SaveAs|
214
 
                                EditorToolSettings::Ok|
215
 
                                EditorToolSettings::Cancel);
216
 
 
217
 
    d->gboxSettings->setTools(EditorToolSettings::Histogram);
218
 
    d->gboxSettings->setHistogramType(Digikam::RGB);
219
 
 
220
 
    QGridLayout* grid = new QGridLayout(d->gboxSettings->plainPage());
221
 
 
222
 
    // -------------------------------------------------------------
223
 
 
224
 
    QLabel *redLabel = new QLabel(i18n("Red:"), d->gboxSettings->plainPage());
225
 
    d->redGain       = new RDoubleNumInput(d->gboxSettings->plainPage());
226
 
    d->redGain->setDecimals(0);
227
 
    d->redGain->setRange(-200.0, 200.0, 1);
228
 
    d->redGain->setDefaultValue(0);
229
 
    d->redGain->setWhatsThis(i18n("Select the red color gain, as a percentage, "
230
 
                                 "for the current channel."));
231
 
 
232
 
    QLabel *greenLabel = new QLabel(i18n("Green:"), d->gboxSettings->plainPage());
233
 
    d->greenGain       = new RDoubleNumInput(d->gboxSettings->plainPage());
234
 
    d->greenGain->setDecimals(0);
235
 
    d->greenGain->setRange(-200.0, 200.0, 1);
236
 
    d->greenGain->setDefaultValue(0);
237
 
    d->greenGain->setWhatsThis(i18n("Select the green color gain, as a percentage, "
238
 
                                    "for the current channel."));
239
 
 
240
 
    QLabel *blueLabel = new QLabel(i18n("Blue:"), d->gboxSettings->plainPage());
241
 
    d->blueGain       = new RDoubleNumInput(d->gboxSettings->plainPage());
242
 
    d->blueGain->setDecimals(0);
243
 
    d->blueGain->setRange(-200.0, 200.0, 1);
244
 
    d->blueGain->setDefaultValue(0);
245
 
    d->blueGain->setWhatsThis(i18n("Select the blue color gain, as a percentage, "
246
 
                                   "for the current channel."));
247
 
 
248
 
    d->resetButton = new QPushButton(i18n("&Reset"), d->gboxSettings->plainPage());
249
 
    d->resetButton->setIcon(KIconLoader::global()->loadIcon("document-revert", KIconLoader::Toolbar));
250
 
    d->resetButton->setWhatsThis(i18n("Reset color channels' gains settings from "
251
 
                                      "the currently selected channel."));
252
 
 
253
 
    // -------------------------------------------------------------
254
 
 
255
 
    d->monochrome = new QCheckBox(i18n("Monochrome"), d->gboxSettings->plainPage());
256
 
    d->monochrome->setWhatsThis(i18n("Enable this option if you want the image rendered "
257
 
                                     "in monochrome mode. "
258
 
                                     "In this mode, the histogram will display only luminosity values."));
259
 
 
260
 
    d->preserveLuminosity = new QCheckBox(i18n("Preserve luminosity"), d->gboxSettings->plainPage());
261
 
    d->preserveLuminosity->setWhatsThis(i18n("Enable this option is you want preserve "
262
 
                                             "the image luminosity."));
263
 
 
264
 
    // -------------------------------------------------------------
265
 
 
266
 
    grid->addWidget(redLabel,               0, 0, 1, 1);
267
 
    grid->addWidget(d->redGain,             0, 1, 1, 4);
268
 
    grid->addWidget(greenLabel,             1, 0, 1, 1);
269
 
    grid->addWidget(d->greenGain,           1, 1, 1, 4);
270
 
    grid->addWidget(blueLabel,              2, 0, 1, 1);
271
 
    grid->addWidget(d->blueGain,            2, 1, 1, 4);
272
 
    grid->addWidget(d->resetButton,         3, 0, 1, 2);
273
 
    grid->addWidget(d->monochrome,          4, 0, 1, 5);
274
 
    grid->addWidget(d->preserveLuminosity,  5, 0, 1, 5);
275
 
    grid->setRowStretch(6, 10);
276
 
    grid->setMargin(d->gboxSettings->spacingHint());
277
 
    grid->setSpacing(d->gboxSettings->spacingHint());
278
 
 
279
 
    setToolSettings(d->gboxSettings);
280
 
    init();
281
 
 
282
 
    // -------------------------------------------------------------
283
 
    // Channels and scale selection slots.
284
 
 
285
 
    connect(d->previewWidget, SIGNAL(spotPositionChangedFromTarget( const Digikam::DColor &, const QPoint & )),
286
 
            this, SLOT(slotColorSelectedFromTarget( const Digikam::DColor & )));
287
 
 
288
 
    connect(d->previewWidget, SIGNAL(signalResized()),
289
 
            this, SLOT(slotEffect()));
290
 
 
291
 
    // -------------------------------------------------------------
292
 
    // Gains settings slots.
293
 
 
294
 
    connect(d->redGain, SIGNAL(valueChanged(double)),
295
 
            this, SLOT(slotGainsChanged()));
296
 
 
297
 
    connect(d->greenGain, SIGNAL(valueChanged(double)),
298
 
            this, SLOT(slotGainsChanged()));
299
 
 
300
 
    connect(d->blueGain, SIGNAL(valueChanged(double)),
301
 
            this, SLOT(slotGainsChanged()));
302
 
 
303
 
    connect(d->preserveLuminosity, SIGNAL(toggled (bool)),
304
 
            this, SLOT(slotEffect()));
305
 
 
306
 
    connect(d->monochrome, SIGNAL(toggled (bool)),
307
 
            this, SLOT(slotMonochromeActived(bool)));
308
 
 
309
 
    // -------------------------------------------------------------
310
 
    // Buttons slots.
311
 
 
312
 
    connect(d->resetButton, SIGNAL(clicked()),
313
 
            this, SLOT(slotResetCurrentChannel()));
314
 
}
315
 
 
316
 
ChannelMixerTool::~ChannelMixerTool()
317
 
{
318
 
    if (d->destinationPreviewData)
319
 
       delete [] d->destinationPreviewData;
320
 
 
321
 
    delete d;
322
 
}
323
 
 
324
 
void ChannelMixerTool::slotResetCurrentChannel()
325
 
{
326
 
    switch (d->gboxSettings->histogramBox()->channel())
327
 
    {
328
 
        case GreenChannel:
329
 
            d->greenRedGain     = 0.0;
330
 
            d->greenGreenGain   = 1.0;
331
 
            d->greenBlueGain    = 0.0;
332
 
            break;
333
 
 
334
 
        case BlueChannel:
335
 
            d->blueRedGain      = 0.0;
336
 
            d->blueGreenGain    = 0.0;
337
 
            d->blueBlueGain     = 1.0;
338
 
            break;
339
 
 
340
 
        default:                        // Red or monochrome.
341
 
            if (d->monochrome->isChecked())
342
 
            {
343
 
                d->blackRedGain   = 1.0;
344
 
                d->blackGreenGain = 0.0;
345
 
                d->blackBlueGain  = 0.0;
346
 
            }
347
 
            else
348
 
            {
349
 
                d->redRedGain = 1.0;
350
 
                d->redGreenGain = 0.0;
351
 
                d->redBlueGain = 0.0;
352
 
            }
353
 
            break;
354
 
    }
355
 
 
356
 
    adjustSliders();
357
 
    slotEffect();
358
 
    d->gboxSettings->histogramBox()->histogram()->reset();
359
 
}
360
 
 
361
 
void ChannelMixerTool::slotColorSelectedFromTarget( const DColor& color )
362
 
{
363
 
    d->gboxSettings->histogramBox()->histogram()->setHistogramGuideByColor(color);
364
 
}
365
 
 
366
 
void ChannelMixerTool::slotGainsChanged()
367
 
{
368
 
    switch(d->gboxSettings->histogramBox()->channel())
369
 
    {
370
 
       case GreenChannel:           // Green.
371
 
          d->greenRedGain   = d->redGain->value()   / 100.0;
372
 
          d->greenGreenGain = d->greenGain->value() / 100.0;
373
 
          d->greenBlueGain  = d->blueGain->value()  / 100.0;
374
 
          break;
375
 
 
376
 
       case BlueChannel:            // Blue.
377
 
          d->blueRedGain   = d->redGain->value()   / 100.0;
378
 
          d->blueGreenGain = d->greenGain->value() / 100.0;
379
 
          d->blueBlueGain  = d->blueGain->value()  / 100.0;
380
 
          break;
381
 
 
382
 
       default:                         // Red or monochrome.
383
 
          if ( d->monochrome->isChecked() )
384
 
          {
385
 
             d->blackRedGain   = d->redGain->value()   / 100.0;
386
 
             d->blackGreenGain = d->greenGain->value() / 100.0;
387
 
             d->blackBlueGain  = d->blueGain->value()  / 100.0;
388
 
          }
389
 
          else
390
 
          {
391
 
             d->redRedGain   = d->redGain->value()   / 100.0;
392
 
             d->redGreenGain = d->greenGain->value() / 100.0;
393
 
             d->redBlueGain  = d->blueGain->value()  / 100.0;
394
 
          }
395
 
          break;
396
 
    }
397
 
 
398
 
    slotTimer();
399
 
}
400
 
 
401
 
void ChannelMixerTool::adjustSliders()
402
 
{
403
 
    d->redGain->blockSignals(true);
404
 
    d->greenGain->blockSignals(true);
405
 
    d->blueGain->blockSignals(true);
406
 
 
407
 
    switch(d->gboxSettings->histogramBox()->channel())
408
 
    {
409
 
       case GreenChannel:           // Green.
410
 
          d->redGain->setDefaultValue(0);
411
 
          d->greenGain->setDefaultValue(100);
412
 
          d->blueGain->setDefaultValue(0);
413
 
          d->redGain->setValue(d->greenRedGain     * 100.0);
414
 
          d->greenGain->setValue(d->greenGreenGain * 100.0);
415
 
          d->blueGain->setValue(d->greenBlueGain   * 100.0);
416
 
          break;
417
 
 
418
 
       case BlueChannel:            // Blue.
419
 
          d->redGain->setDefaultValue(0);
420
 
          d->greenGain->setDefaultValue(0);
421
 
          d->blueGain->setDefaultValue(100);
422
 
          d->redGain->setValue(d->blueRedGain     * 100.0);
423
 
          d->greenGain->setValue(d->blueGreenGain * 100.0);
424
 
          d->blueGain->setValue(d->blueBlueGain   * 100.0);
425
 
          break;
426
 
 
427
 
       default:          // Red or monochrome.
428
 
          if ( d->monochrome->isChecked() )
429
 
          {
430
 
             d->redGain->setDefaultValue(100);
431
 
             d->greenGain->setDefaultValue(0);
432
 
             d->blueGain->setDefaultValue(0);
433
 
             d->redGain->setValue(d->blackRedGain     * 100.0);
434
 
             d->greenGain->setValue(d->blackGreenGain * 100.0);
435
 
             d->blueGain->setValue(d->blackBlueGain   * 100.0);
436
 
          }
437
 
          else
438
 
          {
439
 
             d->redGain->setDefaultValue(100);
440
 
             d->greenGain->setDefaultValue(0);
441
 
             d->blueGain->setDefaultValue(0);
442
 
             d->redGain->setValue(d->redRedGain     * 100.0);
443
 
             d->greenGain->setValue(d->redGreenGain * 100.0);
444
 
             d->blueGain->setValue(d->redBlueGain   * 100.0);
445
 
          }
446
 
          break;
447
 
    }
448
 
 
449
 
    d->redGain->blockSignals(false);
450
 
    d->greenGain->blockSignals(false);
451
 
    d->blueGain->blockSignals(false);
452
 
}
453
 
 
454
 
void ChannelMixerTool::slotMonochromeActived(bool mono)
455
 
{
456
 
    d->gboxSettings->histogramBox()->setChannelEnabled(!mono);
457
 
    d->gboxSettings->histogramBox()->setChannel(RedChannel);
458
 
}
459
 
 
460
 
void ChannelMixerTool::slotEffect()
461
 
{
462
 
    ImageIface* iface = d->previewWidget->imageIface();
463
 
    uchar *data       = iface->getPreviewImage();
464
 
    int w             = iface->previewWidth();
465
 
    int h             = iface->previewHeight();
466
 
    bool sb           = iface->previewSixteenBit();
467
 
 
468
 
    // Create the new empty destination image data space.
469
 
    d->gboxSettings->histogramBox()->histogram()->stopHistogramComputation();
470
 
 
471
 
    if (d->destinationPreviewData)
472
 
       delete [] d->destinationPreviewData;
473
 
 
474
 
    d->destinationPreviewData = new uchar[w*h*(sb ? 8 : 4)];
475
 
    DImgImageFilters filter;
476
 
 
477
 
    if (d->monochrome->isChecked())
478
 
    {
479
 
       filter.channelMixerImage(data, w, h, sb,                                 // Image data.
480
 
                d->preserveLuminosity->isChecked(),                              // Preserve luminosity.
481
 
                d->monochrome->isChecked(),                                      // Monochrome.
482
 
                d->blackRedGain, d->blackGreenGain, d->blackBlueGain,              // Red channel gains.
483
 
                0.0,            1.0,              0.0,                          // Green channel gains (not used).
484
 
                0.0,            0.0,              1.0);                         // Blue channel gains (not used).
485
 
    }
486
 
    else
487
 
    {
488
 
       filter.channelMixerImage(data, w, h, sb,                                 // Image data.
489
 
                d->preserveLuminosity->isChecked(),                              // Preserve luminosity.
490
 
                d->monochrome->isChecked(),                                      // Monochrome.
491
 
                d->redRedGain,   d->redGreenGain,   d->redBlueGain,                // Red channel gains.
492
 
                d->greenRedGain, d->greenGreenGain, d->greenBlueGain,              // Green channel gains.
493
 
                d->blueRedGain,  d->blueGreenGain,  d->blueBlueGain);              // Blue channel gains.
494
 
    }
495
 
 
496
 
    iface->putPreviewImage(data);
497
 
    d->previewWidget->updatePreview();
498
 
 
499
 
    // Update histogram.
500
 
    memcpy (d->destinationPreviewData, data, w*h*(sb ? 8 : 4));
501
 
    d->gboxSettings->histogramBox()->histogram()->updateData(d->destinationPreviewData, w, h, sb, 0, 0, 0, false);
502
 
    delete [] data;
503
 
}
504
 
 
505
 
void ChannelMixerTool::finalRendering()
506
 
{
507
 
    kapp->setOverrideCursor( Qt::WaitCursor );
508
 
    ImageIface* iface = d->previewWidget->imageIface();
509
 
    uchar *data       = iface->getOriginalImage();
510
 
    int w             = iface->originalWidth();
511
 
    int h             = iface->originalHeight();
512
 
    bool sb           = iface->originalSixteenBit();
513
 
 
514
 
    DImgImageFilters filter;
515
 
 
516
 
    if (d->monochrome->isChecked())
517
 
    {
518
 
       filter.channelMixerImage(data, w, h, sb,                     // Image data.
519
 
                d->preserveLuminosity->isChecked(),                  // Preserve luminosity.
520
 
                d->monochrome->isChecked(),                          // Monochrome.
521
 
                d->blackRedGain, d->blackGreenGain, d->blackBlueGain,  // Red channel gains.
522
 
                0.0,            1.0,              0.0,              // Green channel gains (not used).
523
 
                0.0,            0.0,              1.0);             // Blue channel gains (not used).
524
 
    }
525
 
    else
526
 
    {
527
 
       filter.channelMixerImage(data, w, h, sb,                     // Image data.
528
 
                d->preserveLuminosity->isChecked(),                  // Preserve luminosity.
529
 
                d->monochrome->isChecked(),                          // Monochrome.
530
 
                d->redRedGain,   d->redGreenGain,   d->redBlueGain,    // Red channel gains.
531
 
                d->greenRedGain, d->greenGreenGain, d->greenBlueGain,  // Green channel gains.
532
 
                d->blueRedGain,  d->blueGreenGain,  d->blueBlueGain);  // Blue channel gains.
533
 
    }
534
 
 
535
 
    iface->putOriginalImage(i18n("Channel Mixer"), data);
536
 
    delete [] data;
537
 
    kapp->restoreOverrideCursor();
538
 
}
539
 
 
540
 
void ChannelMixerTool::slotChannelChanged()
541
 
{
542
 
    if (d->monochrome->isChecked())
543
 
        d->gboxSettings->histogramBox()->setGradientColors(QColor("black"), QColor("white"));
544
 
 
545
 
    adjustSliders();
546
 
    slotEffect();
547
 
}
548
 
 
549
 
void ChannelMixerTool::readSettings()
550
 
{
551
 
    KSharedConfig::Ptr config = KGlobal::config();
552
 
    KConfigGroup group = config->group(d->configGroupName);
553
 
 
554
 
    d->monochrome->setChecked(group.readEntry(d->configMonochromeEntry,                 false));
555
 
    d->preserveLuminosity->setChecked(group.readEntry(d->configPreserveLuminosityEntry, false));
556
 
 
557
 
    d->redRedGain     = group.readEntry(d->configRedRedGainEntry,   1.0);
558
 
    d->redGreenGain   = group.readEntry(d->configRedGreenGainEntry, 0.0);
559
 
    d->redBlueGain    = group.readEntry(d->configRedBlueGainEntry,  0.0);
560
 
 
561
 
    d->greenRedGain   = group.readEntry(d->configGreenRedGainEntry,   0.0);
562
 
    d->greenGreenGain = group.readEntry(d->configGreenGreenGainEntry, 1.0);
563
 
    d->greenBlueGain  = group.readEntry(d->configGreenBlueGainEntry,  0.0);
564
 
 
565
 
    d->blueRedGain    = group.readEntry(d->configBlueRedGainEntry,   0.0);
566
 
    d->blueGreenGain  = group.readEntry(d->configBlueGreenGainEntry, 0.0);
567
 
    d->blueBlueGain   = group.readEntry(d->configBlueBlueGainEntry,  1.0);
568
 
 
569
 
    d->blackRedGain   = group.readEntry(d->configBlackRedGainEntry,   1.0);
570
 
    d->blackGreenGain = group.readEntry(d->configBlackGreenGainEntry, 0.0);
571
 
    d->blackBlueGain  = group.readEntry(d->configBlackBlueGainEntry,  0.0);
572
 
 
573
 
    adjustSliders();
574
 
 
575
 
    // we need to call the set methods here, otherwise the histogram will not be updated correctly
576
 
    d->gboxSettings->histogramBox()->setChannel(group.readEntry(d->configHistogramChannelEntry,
577
 
                        (int)LuminosityChannel));
578
 
    d->gboxSettings->histogramBox()->setScale((HistogramScale)group.readEntry(d->configHistogramScaleEntry,
579
 
                        (int)LogScaleHistogram));
580
 
 
581
 
    d->gboxSettings->histogramBox()->histogram()->reset();
582
 
 
583
 
    slotEffect();
584
 
}
585
 
 
586
 
void ChannelMixerTool::writeSettings()
587
 
{
588
 
    KSharedConfig::Ptr config = KGlobal::config();
589
 
    KConfigGroup group = config->group(d->configGroupName);
590
 
 
591
 
    group.writeEntry(d->configHistogramChannelEntry, d->gboxSettings->histogramBox()->channel());
592
 
    group.writeEntry(d->configHistogramScaleEntry,   (int)d->gboxSettings->histogramBox()->scale());
593
 
 
594
 
    group.writeEntry(d->configMonochromeEntry,         d->monochrome->isChecked());
595
 
    group.writeEntry(d->configPreserveLuminosityEntry, d->preserveLuminosity->isChecked());
596
 
 
597
 
    group.writeEntry(d->configRedRedGainEntry,     d->redRedGain);
598
 
    group.writeEntry(d->configRedGreenGainEntry,   d->redGreenGain);
599
 
    group.writeEntry(d->configRedBlueGainEntry,    d->redBlueGain);
600
 
 
601
 
    group.writeEntry(d->configGreenRedGainEntry,   d->greenRedGain);
602
 
    group.writeEntry(d->configGreenGreenGainEntry, d->greenGreenGain);
603
 
    group.writeEntry(d->configGreenBlueGainEntry,  d->greenBlueGain);
604
 
 
605
 
    group.writeEntry(d->configBlueRedGainEntry,    d->blueRedGain);
606
 
    group.writeEntry(d->configBlueGreenGainEntry,  d->blueGreenGain);
607
 
    group.writeEntry(d->configBlueBlueGainEntry,   d->blueBlueGain);
608
 
 
609
 
    group.writeEntry(d->configBlackRedGainEntry,   d->blackRedGain);
610
 
    group.writeEntry(d->configBlackGreenGainEntry, d->blackGreenGain);
611
 
    group.writeEntry(d->configBlackBlueGainEntry,  d->blackBlueGain);
612
 
 
613
 
    config->sync();
614
 
}
615
 
 
616
 
void ChannelMixerTool::slotResetSettings()
617
 
{
618
 
    d->monochrome->blockSignals(true);
619
 
    d->preserveLuminosity->blockSignals(true);
620
 
 
621
 
    d->redRedGain     = 1.0;
622
 
    d->redGreenGain   = 0.0;
623
 
    d->redBlueGain    = 0.0;
624
 
 
625
 
    d->greenRedGain   = 0.0;
626
 
    d->greenGreenGain = 1.0;
627
 
    d->greenBlueGain  = 0.0;
628
 
 
629
 
    d->blueRedGain    = 0.0;
630
 
    d->blueGreenGain  = 0.0;
631
 
    d->blueBlueGain   = 1.0;
632
 
 
633
 
    d->blackRedGain   = 1.0;
634
 
    d->blackGreenGain = 0.0;
635
 
    d->blackBlueGain  = 0.0;
636
 
 
637
 
    adjustSliders();
638
 
 
639
 
    d->monochrome->blockSignals(false);
640
 
    d->preserveLuminosity->blockSignals(false);
641
 
    d->gboxSettings->histogramBox()->histogram()->reset();
642
 
    d->gboxSettings->histogramBox()->setChannel(RedChannel);
643
 
}
644
 
 
645
 
// Load all gains.
646
 
void ChannelMixerTool::slotLoadSettings()
647
 
{
648
 
    KUrl loadGainsFileUrl;
649
 
    FILE *fp = 0L;
650
 
 
651
 
    loadGainsFileUrl = KFileDialog::getOpenUrl(KGlobalSettings::documentPath(),
652
 
                                            QString( "*" ), kapp->activeWindow(),
653
 
                                            QString( i18n("Select Gimp Gains Mixer File to Load")) );
654
 
    if ( loadGainsFileUrl.isEmpty() )
655
 
       return;
656
 
 
657
 
    fp = fopen(QFile::encodeName(loadGainsFileUrl.toLocalFile()), "r");
658
 
 
659
 
    if ( fp )
660
 
    {
661
 
        bool monochrome;
662
 
        int  currentOutputChannel = RedChannel;
663
 
        char buf1[1024];
664
 
        char buf2[1024];
665
 
        char buf3[1024];
666
 
 
667
 
        buf1[0] = '\0';
668
 
 
669
 
        fgets(buf1, 1023, fp);
670
 
 
671
 
        fscanf (fp, "%*s %s", buf1);
672
 
 
673
 
        // Get the current output channel in dialog.
674
 
 
675
 
        if (strcmp (buf1, "RED") == 0)
676
 
            currentOutputChannel = RedChannel;
677
 
        else if (strcmp (buf1, "GREEN") == 0)
678
 
            currentOutputChannel = GreenChannel;
679
 
        else if (strcmp (buf1, "BLUE") == 0)
680
 
            currentOutputChannel = BlueChannel;
681
 
 
682
 
        fscanf (fp, "%*s %s", buf1); // preview flag, preserved for compatibility
683
 
 
684
 
        fscanf (fp, "%*s %s", buf1);
685
 
 
686
 
        if (strcmp (buf1, "true") == 0)
687
 
            monochrome = true;
688
 
        else
689
 
            monochrome = false;
690
 
 
691
 
        fscanf (fp, "%*s %s", buf1);
692
 
 
693
 
        if (strcmp (buf1, "true") == 0)
694
 
            d->preserveLuminosity->setChecked(true);
695
 
        else
696
 
            d->preserveLuminosity->setChecked(false);
697
 
 
698
 
        fscanf (fp, "%*s %s %s %s", buf1, buf2, buf3);
699
 
        d->redRedGain = atof(buf1);
700
 
        d->redGreenGain = atof(buf2);
701
 
        d->redBlueGain = atof(buf3);
702
 
 
703
 
        fscanf (fp, "%*s %s %s %s", buf1, buf2, buf3);
704
 
        d->greenRedGain = atof(buf1);
705
 
        d->greenGreenGain = atof(buf2);
706
 
        d->greenBlueGain = atof(buf3);
707
 
 
708
 
        fscanf (fp, "%*s %s %s %s", buf1, buf2, buf3);
709
 
        d->blueRedGain = atof(buf1);
710
 
        d->blueGreenGain = atof(buf2);
711
 
        d->blueBlueGain = atof(buf3);
712
 
 
713
 
        fscanf (fp, "%*s %s %s %s", buf1, buf2, buf3);
714
 
        d->blackRedGain = atof(buf1);
715
 
        d->blackGreenGain = atof(buf2);
716
 
        d->blackBlueGain = atof(buf3);
717
 
 
718
 
        fclose(fp);
719
 
 
720
 
        // Refresh settings.
721
 
        d->monochrome->setChecked(monochrome);
722
 
        d->gboxSettings->histogramBox()->setChannel(currentOutputChannel);
723
 
    }
724
 
    else
725
 
    {
726
 
        KMessageBox::error(kapp->activeWindow(),
727
 
                           i18n("Cannot load settings from the Gains Mixer text file."));
728
 
        return;
729
 
    }
730
 
}
731
 
 
732
 
// Save all gains.
733
 
void ChannelMixerTool::slotSaveAsSettings()
734
 
{
735
 
    KUrl saveGainsFileUrl;
736
 
    FILE *fp = 0L;
737
 
 
738
 
    saveGainsFileUrl = KFileDialog::getSaveUrl(KGlobalSettings::documentPath(),
739
 
                                               QString( "*" ), kapp->activeWindow(),
740
 
                                               QString( i18n("Gimp Gains Mixer File to Save")) );
741
 
    if ( saveGainsFileUrl.isEmpty() )
742
 
       return;
743
 
 
744
 
    fp = fopen(QFile::encodeName(saveGainsFileUrl.toLocalFile()), "w");
745
 
 
746
 
    if ( fp )
747
 
    {
748
 
        const char *str = 0L;
749
 
        char        buf1[256];
750
 
        char        buf2[256];
751
 
        char        buf3[256];
752
 
 
753
 
        switch (d->gboxSettings->histogramBox()->channel())
754
 
        {
755
 
           case RedChannel:
756
 
              str = "RED";
757
 
              break;
758
 
           case GreenChannel:
759
 
              str = "GREEN";
760
 
              break;
761
 
           case BlueChannel:
762
 
              str = "BLUE";
763
 
              break;
764
 
           default:
765
 
              kWarning() <<  "Unknown Color channel gains";
766
 
              break;
767
 
        }
768
 
 
769
 
        fprintf (fp, "# Channel Mixer Configuration File\n");
770
 
 
771
 
        fprintf (fp, "CHANNEL: %s\n", str);
772
 
        fprintf (fp, "PREVIEW: %s\n", "true"); // preserved for compatibility
773
 
        fprintf (fp, "MONOCHROME: %s\n",
774
 
                 d->monochrome->isChecked() ? "true" : "false");
775
 
        fprintf (fp, "PRESERVE_LUMINOSITY: %s\n",
776
 
                 d->preserveLuminosity->isChecked() ? "true" : "false");
777
 
 
778
 
        sprintf (buf1, "%5.3f", d->redRedGain);
779
 
        sprintf (buf2, "%5.3f", d->redGreenGain);
780
 
        sprintf (buf3, "%5.3f", d->redBlueGain);
781
 
        fprintf (fp, "RED: %s %s %s\n", buf1, buf2,buf3);
782
 
 
783
 
        sprintf (buf1, "%5.3f", d->greenRedGain);
784
 
        sprintf (buf2, "%5.3f", d->greenGreenGain);
785
 
        sprintf (buf3, "%5.3f", d->greenBlueGain);
786
 
        fprintf (fp, "GREEN: %s %s %s\n", buf1, buf2,buf3);
787
 
 
788
 
        sprintf (buf1, "%5.3f", d->blueRedGain);
789
 
        sprintf (buf2, "%5.3f", d->blueGreenGain);
790
 
        sprintf (buf3, "%5.3f", d->blueBlueGain);
791
 
        fprintf (fp, "BLUE: %s %s %s\n", buf1, buf2,buf3);
792
 
 
793
 
        sprintf (buf1, "%5.3f", d->blackRedGain);
794
 
        sprintf (buf2, "%5.3f", d->blackGreenGain);
795
 
        sprintf (buf3, "%5.3f", d->blackBlueGain);
796
 
        fprintf (fp, "BLACK: %s %s %s\n", buf1, buf2,buf3);
797
 
 
798
 
        fclose (fp);
799
 
    }
800
 
    else
801
 
    {
802
 
        KMessageBox::error(kapp->activeWindow(),
803
 
                           i18n("Cannot save settings to the Gains Mixer text file."));
804
 
        return;
805
 
    }
806
 
}
807
 
 
808
 
}  // namespace DigikamChannelMixerImagesPlugin