summaryrefslogtreecommitdiffstats
path: root/kolourpaint/pixmapfx/kpeffectbalance.cpp
blob: 946814d51186de3a5a2ced583a0ffae173f098c5 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define DEBUG_KP_EFFECT_BALANCE 0


#include <kpeffectbalance.h>

#include <math.h>

#include <tqfontmetrics.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>

#include <kcombobox.h>
#include <kdebug.h>
#include <kimageeffect.h>
#include <klocale.h>
#include <knuminput.h>

#include <kppixmapfx.h>


#if DEBUG_KP_EFFECT_BALANCE
    #include <tqdatetime.h>
#endif


kpEffectBalanceCommand::kpEffectBalanceCommand (int channels,
        int brightness, int contrast, int gamma,
        bool actOnSelection,
        kpMainWindow *mainWindow)
    : kpColorEffectCommand (i18n ("Balance"), actOnSelection, mainWindow),
      m_channels (channels),
      m_brightness (brightness), m_contrast (contrast), m_gamma (gamma)
{
}

kpEffectBalanceCommand::~kpEffectBalanceCommand ()
{
}


static inline int between0And255 (int val)
{
    if (val < 0)
        return 0;
    else if (val > 255)
        return 255;
    else
        return val;
}


static inline int brightness (int base, int strength)
{
    return between0And255 (base + strength * 255 / 50);
}

static inline int contrast (int base, int strength)
{
    return between0And255 ((base - 127) * (strength + 50) / 50 + 127);
}

static inline int gamma (int base, int strength)
{
    return between0And255 (tqRound (255.0 * pow (base / 255.0, 1.0 / pow (10, strength / 50.0))));
}


static inline int brightnessContrastGamma (int base,
                                           int newBrightness,
                                           int newContrast,
                                           int newGamma)
{
    return gamma (contrast (brightness (base, newBrightness),
                            newContrast),
                  newGamma);
}

static inline TQRgb brightnessContrastGammaForRGB (TQRgb rgb,
    int channels,
    int brightness, int contrast, int gamma)
{
    int red = tqRed (rgb);
    int green = tqGreen (rgb);
    int blue = tqBlue (rgb);


    if (channels & kpEffectBalanceCommand::Red)
        red = brightnessContrastGamma (red, brightness, contrast, gamma);
    if (channels & kpEffectBalanceCommand::Green)
        green = brightnessContrastGamma (green, brightness, contrast, gamma);
    if (channels & kpEffectBalanceCommand::Blue)
        blue = brightnessContrastGamma (blue, brightness, contrast, gamma);


    return tqRgba (red, green, blue, tqAlpha (rgb));
}


// public static
TQPixmap kpEffectBalanceCommand::applyColorEffect (const TQPixmap &pixmap,
    int channels,
    int brightness, int contrast, int gamma)
{
#if DEBUG_KP_EFFECT_BALANCE
    kdDebug () << "kpEffectBalanceCommand::applyColorEffect("
               << "channels=" << channels
               << ",brightness=" << brightness
               << ",contrast=" << contrast
               << ",gamma=" << gamma
               << ")" << endl;
    TQTime timer; timer.start ();
#endif

    TQImage image = kpPixmapFX::convertToImage (pixmap);
#if DEBUG_KP_EFFECT_BALANCE
    kdDebug () << "\tconvertToImage=" << timer.restart () << endl;
#endif


    TQ_UINT8 transformRed [256],
            transformGreen [256],
            transformBlue [256];

    for (int i = 0; i < 256; i++)
    {
        TQ_UINT8 applied = (TQ_UINT8) brightnessContrastGamma (i, brightness, contrast, gamma);

        if (channels & kpEffectBalanceCommand::Red)
            transformRed [i] = applied;
        else
            transformRed [i] = i;

        if (channels & kpEffectBalanceCommand::Green)
            transformGreen [i] = applied;
        else
            transformGreen [i] = i;

        if (channels & kpEffectBalanceCommand::Blue)
            transformBlue [i] = applied;
        else
            transformBlue [i] = i;
    }

#if DEBUG_KP_EFFECT_BALANCE
    kdDebug () << "\tbuild lookup=" << timer.restart () << endl;
#endif


    if (image.depth () > 8)
    {
        for (int y = 0; y < image.height (); y++)
        {
            for (int x = 0; x < image.width (); x++)
            {
                const TQRgb rgb = image.pixel (x, y);

                const TQ_UINT8 red = (TQ_UINT8) tqRed (rgb);
                const TQ_UINT8 green = (TQ_UINT8) tqGreen (rgb);
                const TQ_UINT8 blue = (TQ_UINT8) tqBlue (rgb);
                const TQ_UINT8 alpha = (TQ_UINT8) tqAlpha (rgb);

                image.setPixel (x, y,
                    tqRgba (transformRed [red],
                           transformGreen [green],
                           transformBlue [blue],
                           alpha));

            #if 0
                image.setPixel (x, y,
                    brightnessContrastGammaForRGB (image.pixel (x, y),
                        channels,
                        brightness, contrast, gamma));
            #endif
            }
        }
    }
    else
    {
        for (int i = 0; i < image.numColors (); i++)
        {
            const TQRgb rgb = image.color (i);

            const TQ_UINT8 red = (TQ_UINT8) tqRed (rgb);
            const TQ_UINT8 green = (TQ_UINT8) tqGreen (rgb);
            const TQ_UINT8 blue = (TQ_UINT8) tqBlue (rgb);
            const TQ_UINT8 alpha = (TQ_UINT8) tqAlpha (rgb);

            image.setColor (i,
                tqRgba (transformRed [red],
                       transformGreen [green],
                       transformBlue [blue],
                       alpha));

        #if 0
            image.setColor (i,
                brightnessContrastGammaForRGB (image.color (i),
                    channels,
                    brightness, contrast, gamma));
        #endif
        }

    }
#if DEBUG_KP_EFFECT_BALANCE
    kdDebug () << "\teffect=" << timer.restart () << endl;
#endif

    const TQPixmap retPixmap = kpPixmapFX::convertToPixmap (image);
#if DEBUG_KP_EFFECT_BALANCE
    kdDebug () << "\tconvertToPixmap=" << timer.restart () << endl;
#endif

    return retPixmap;
}

// protected virtual [base kpColorEffectCommand]
TQPixmap kpEffectBalanceCommand::applyColorEffect (const TQPixmap &pixmap)
{
    return applyColorEffect (pixmap, m_channels,
                             m_brightness, m_contrast, m_gamma);
}



kpEffectBalanceWidget::kpEffectBalanceWidget (bool actOnSelection,
                                              kpMainWindow *mainWindow,
                                              TQWidget *parent, const char *name)
    : kpColorEffectWidget (actOnSelection, mainWindow, parent, name)
{
    TQGridLayout *lay = new TQGridLayout (this, 5, 5, marginHint (), spacingHint ());


    TQLabel *brightnessLabel = new TQLabel (i18n ("&Brightness:"), this);
    m_brightnessInput = new KIntNumInput (0/*value*/, this);
    m_brightnessInput->setRange (-50, 50, 1/*step*/, true/*slider*/);
    TQPushButton *brightnessResetPushButton = new TQPushButton (i18n ("Re&set"), this);

    TQLabel *contrastLabel = new TQLabel (i18n ("Co&ntrast:"), this);
    m_contrastInput = new KIntNumInput (0/*value*/, this);
    m_contrastInput->setRange (-50, 50, 1/*step*/, true/*slider*/);
    TQPushButton *contrastResetPushButton = new TQPushButton (i18n ("&Reset"), this);

    TQLabel *gammaLabel = new TQLabel (i18n ("&Gamma:"), this);
    m_gammaInput = new KIntNumInput (0/*value*/, this);
    m_gammaInput->setRange (-50, 50, 1/*step*/, true/*slider*/);
    // TODO: This is what should be shown in the m_gammaInput spinbox
    m_gammaLabel = new TQLabel (this);
    // TODO: This doesn't seem to be wide enough with some fonts so the
    //       whole tqlayout moves when we drag the gamma slider.
    m_gammaLabel->setMinimumWidth (m_gammaLabel->fontMetrics ().width (" 10.00 "));
    m_gammaLabel->tqsetAlignment (m_gammaLabel->tqalignment () | TQt::AlignRight);
    TQPushButton *gammaResetPushButton = new TQPushButton (i18n ("Rese&t"), this);


    TQWidget *spaceWidget = new TQLabel (this);
    spaceWidget->setFixedSize (1, spacingHint ());


    TQLabel *channelLabel = new TQLabel (i18n ("C&hannels:"), this);
    m_channelsComboBox = new KComboBox (this);
    m_channelsComboBox->insertItem (i18n ("All"));
    m_channelsComboBox->insertItem (i18n ("Red"));
    m_channelsComboBox->insertItem (i18n ("Green"));
    m_channelsComboBox->insertItem (i18n ("Blue"));


    TQPushButton *resetPushButton = new TQPushButton (i18n ("Reset &All Values"), this);


    brightnessLabel->setBuddy (m_brightnessInput);
    contrastLabel->setBuddy (m_contrastInput);
    gammaLabel->setBuddy (m_gammaInput);

    channelLabel->setBuddy (m_channelsComboBox);


    lay->addWidget (brightnessLabel, 0, 0);
    lay->addMultiCellWidget (m_brightnessInput, 0, 0, 1, 2);
    lay->addWidget (brightnessResetPushButton, 0, 4);

    lay->addWidget (contrastLabel, 1, 0);
    lay->addMultiCellWidget (m_contrastInput, 1, 1, 1, 2);
    lay->addWidget (contrastResetPushButton, 1, 4);

    lay->addWidget (gammaLabel, 2, 0);
    lay->addMultiCellWidget (m_gammaInput, 2, 2, 1, 2);
    lay->addWidget (m_gammaLabel, 2, 3);
    lay->addWidget (gammaResetPushButton, 2, 4);

    lay->addMultiCellWidget (spaceWidget, 3, 3, 0, 4);
    lay->addMultiCellWidget (resetPushButton, 4, 4, 2, 4, TQt::AlignRight);

    lay->addWidget (channelLabel, 4, 0);
    lay->addWidget (m_channelsComboBox, 4, 1, TQt::AlignLeft);
    //lay->addWidget (resetPushButton, 4, 2, TQt::AlignRight);

    lay->setColStretch (1, 1);


    // (no need for settingsChangedDelayed() since BCG effect is so fast :))
    connect (m_brightnessInput, TQT_SIGNAL (valueChanged (int)),
             this, TQT_SIGNAL (settingsChangedNoWaitCursor ()));
    connect (m_contrastInput, TQT_SIGNAL (valueChanged (int)),
             this, TQT_SIGNAL (settingsChangedNoWaitCursor ()));

    connect (m_gammaInput, TQT_SIGNAL (valueChanged (int)),
             this, TQT_SLOT (recalculateGammaLabel ()));
    connect (m_gammaInput, TQT_SIGNAL (valueChanged (int)),
             this, TQT_SIGNAL (settingsChangedNoWaitCursor ()));

    connect (m_channelsComboBox, TQT_SIGNAL (activated (int)),
             this, TQT_SIGNAL (settingsChanged ()));

    connect (brightnessResetPushButton, TQT_SIGNAL (clicked ()),
             this, TQT_SLOT (resetBrightness ()));
    connect (contrastResetPushButton, TQT_SIGNAL (clicked ()),
             this, TQT_SLOT (resetContrast ()));
    connect (gammaResetPushButton, TQT_SIGNAL (clicked ()),
             this, TQT_SLOT (resetGamma ()));

    connect (resetPushButton, TQT_SIGNAL (clicked ()),
             this, TQT_SLOT (resetAll ()));


    recalculateGammaLabel ();
}

kpEffectBalanceWidget::~kpEffectBalanceWidget ()
{
}


// public virtual [base kpColorEffectWidget]
TQString kpEffectBalanceWidget::caption () const
{
    return i18n ("Settings");
}


// public virtual [base kpColorEffectWidget]
bool kpEffectBalanceWidget::isNoOp () const
{
    return (brightness () == 0 && contrast () == 0 && gamma () == 0);
}

// public virtual [base kpColorEffectWidget]
TQPixmap kpEffectBalanceWidget::applyColorEffect (const TQPixmap &pixmap)
{
    return kpEffectBalanceCommand::applyColorEffect (pixmap,
               channels (), brightness (), contrast (), gamma ());
}

// public virtual [base kpColorEffectWidget]
kpColorEffectCommand *kpEffectBalanceWidget::createCommand () const
{
    return new kpEffectBalanceCommand (channels (),
                                       brightness (), contrast (), gamma (),
                                       m_actOnSelection,
                                       m_mainWindow);
}


// protected
int kpEffectBalanceWidget::channels () const
{
    switch (m_channelsComboBox->currentItem ())
    {
    default:
    case 0:
        return kpEffectBalanceCommand::RGB;

    case 1:
        return kpEffectBalanceCommand::Red;

    case 2:
        return kpEffectBalanceCommand::Green;

    case 3:
        return kpEffectBalanceCommand::Blue;
    }
}


// protected
int kpEffectBalanceWidget::brightness () const
{
    return m_brightnessInput->value ();
}

// protected
int kpEffectBalanceWidget::contrast () const
{
    return m_contrastInput->value ();
}

// protected
int kpEffectBalanceWidget::gamma () const
{
    return m_gammaInput->value ();
}


// protected slot
void kpEffectBalanceWidget::recalculateGammaLabel ()
{
    m_gammaLabel->setText (
        " " +
        TQString::number (pow (10, gamma () / 50.0),
                         'f'/*[-]9.9*/,
                         2/*precision*/) +
        " ");
    m_gammaLabel->tqrepaint ();
}


// protected slot
void kpEffectBalanceWidget::resetBrightness ()
{
    if (brightness () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_brightnessInput->setValue (0);
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}

// protected slot
void kpEffectBalanceWidget::resetContrast ()
{
    if (contrast () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_contrastInput->setValue (0);
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}

// protected slot
void kpEffectBalanceWidget::resetGamma ()
{
    if (gamma () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_gammaInput->setValue (0);
    recalculateGammaLabel ();
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}


// protected slot
void kpEffectBalanceWidget::resetAll ()
{
    if (isNoOp ())
        return;

    // Prevent multiple settingsChanged() which would normally result in
    // redundant, expensive preview repaints
    blockSignals (true);

    resetBrightness ();
    resetContrast ();
    resetGamma ();

    recalculateGammaLabel ();

    blockSignals (false);

    emit settingsChanged ();
}


#include <kpeffectbalance.moc>