summaryrefslogtreecommitdiffstats
path: root/kolourpaint/pixmapfx/kpeffectinvert.cpp
blob: d6229aca60054254210cdb11cb7dab23bb4b3406 (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

/*
   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_INVERT 0


#include <kpeffectinvert.h>

#include <tqcheckbox.h>
#include <tqimage.h>
#include <tqlayout.h>
#include <tqpixmap.h>

#include <kdebug.h>
#include <tdelocale.h>

#include <kppixmapfx.h>


//
// kpEffectInvertCommand
//

kpEffectInvertCommand::kpEffectInvertCommand (int channels,
                                              bool actOnSelection,
                                              kpMainWindow *mainWindow)
    : kpColorEffectCommand (channels == RGB ?
                                i18n ("Invert Colors") : i18n ("Invert"),
                            actOnSelection, mainWindow),
      m_channels (channels)
{
}

kpEffectInvertCommand::kpEffectInvertCommand (bool actOnSelection,
                                              kpMainWindow *mainWindow)
    : kpColorEffectCommand (i18n ("Invert Colors"), actOnSelection, mainWindow),
      m_channels (RGB)
{
}

kpEffectInvertCommand::~kpEffectInvertCommand ()
{
}


// public static
void kpEffectInvertCommand::apply (TQPixmap *destPixmapPtr, int channels)
{
    TQImage image = kpPixmapFX::convertToImage (*destPixmapPtr);
    apply (&image, channels);
    *destPixmapPtr = kpPixmapFX::convertToPixmap (image);
}

// public static
TQPixmap kpEffectInvertCommand::apply (const TQPixmap &pm, int channels)
{
    TQImage image = kpPixmapFX::convertToImage (pm);
    apply (&image, channels);
    return kpPixmapFX::convertToPixmap (image);
}

// public static
void kpEffectInvertCommand::apply (TQImage *destImagePtr, int channels)
{
    TQRgb mask = tqRgba ((channels & Red) ? 0xFF : 0,
                       (channels & Green) ? 0xFF : 0,
                       (channels & Blue) ? 0xFF : 0,
                       0/*don't invert alpha*/);
#if DEBUG_KP_EFFECT_INVERT
    kdDebug () << "kpEffectInvertCommand::apply(channels=" << channels
               << ") mask=" << (int *) mask
               << endl;
#endif

    if (destImagePtr->depth () > 8)
    {
    #if 0
        // SYNC: TODO: TQt BUG - invertAlpha argument is inverted!!!
        destImagePtr->invertPixels (true/*no invert alpha (TQt 3.2)*/);
    #else
        // Above version works for TQt 3.2 at least.
        // But this version will always work (slower, though) and supports
        // inverting particular channels.
        for (int y = 0; y < destImagePtr->height (); y++)
        {
            for (int x = 0; x < destImagePtr->width (); x++)
            {
                destImagePtr->setPixel (x, y, destImagePtr->pixel (x, y) ^ mask);
            }
        }
    #endif
    }
    else
    {
        for (int i = 0; i < destImagePtr->numColors (); i++)
        {
            destImagePtr->setColor (i, destImagePtr->color (i) ^ mask);
        }
    }
}

// public static
TQImage kpEffectInvertCommand::apply (const TQImage &img, int channels)
{
    TQImage retImage = img;
    apply (&retImage, channels);
    return retImage;
}


//
// kpEffectInvertCommand implements kpColorEffectCommand interface
//

// protected virtual [base kpColorEffectCommand]
TQPixmap kpEffectInvertCommand::applyColorEffect (const TQPixmap &pixmap)
{
    return apply (pixmap, m_channels);
}


//
// kpEffectInvertWidget
//

kpEffectInvertWidget::kpEffectInvertWidget (bool actOnSelection,
                                            kpMainWindow *mainWindow,
                                            TQWidget *parent,
                                            const char *name)
    : kpColorEffectWidget (actOnSelection, mainWindow, parent, name)
{
    TQVBoxLayout *topLevelLay = new TQVBoxLayout (this, marginHint (), spacingHint ());


    TQWidget *centerWidget = new TQWidget (this);
    topLevelLay->addWidget (centerWidget, 0/*stretch*/, TQt::AlignCenter);


    TQVBoxLayout *centerWidgetLay = new TQVBoxLayout (centerWidget,
                                                    0/*margin*/,
                                                    spacingHint ());


    m_redCheckBox = new TQCheckBox (i18n ("&Red"), centerWidget);
    m_greenCheckBox = new TQCheckBox (i18n ("&Green"), centerWidget);
    m_blueCheckBox = new TQCheckBox (i18n ("&Blue"), centerWidget);

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

    m_allCheckBox = new TQCheckBox (i18n ("&All"), centerWidget);


    m_redCheckBox->setChecked (false);
    m_greenCheckBox->setChecked (false);
    m_blueCheckBox->setChecked (false);

    m_allCheckBox->setChecked (false);


    centerWidgetLay->addWidget (m_redCheckBox);
    centerWidgetLay->addWidget (m_greenCheckBox);
    centerWidgetLay->addWidget (m_blueCheckBox);

    centerWidgetLay->addWidget (spaceWidget);

    centerWidgetLay->addWidget (m_allCheckBox);


    m_inSignalHandler = false;
    connect (m_redCheckBox, TQT_SIGNAL (toggled (bool)),
             this, TQT_SLOT (slotRGBCheckBoxToggled ()));
    connect (m_greenCheckBox, TQT_SIGNAL (toggled (bool)),
             this, TQT_SLOT (slotRGBCheckBoxToggled ()));
    connect (m_blueCheckBox, TQT_SIGNAL (toggled (bool)),
             this, TQT_SLOT (slotRGBCheckBoxToggled ()));

    connect (m_allCheckBox, TQT_SIGNAL (toggled (bool)),
             this, TQT_SLOT (slotAllCheckBoxToggled ()));
}

kpEffectInvertWidget::~kpEffectInvertWidget ()
{
}


// public
int kpEffectInvertWidget::channels () const
{
#if DEBUG_KP_EFFECT_INVERT
    kdDebug () << "kpEffectInvertWidget::channels()"
               << " isChecked: r=" << m_redCheckBox->isChecked ()
               << " g=" << m_greenCheckBox->isChecked ()
               << " b=" << m_blueCheckBox->isChecked ()
               << endl;
#endif

    int channels = 0;


    if (m_redCheckBox->isChecked ())
        channels |= kpEffectInvertCommand::Red;

    if (m_greenCheckBox->isChecked ())
        channels |= kpEffectInvertCommand::Green;

    if (m_blueCheckBox->isChecked ())
        channels |= kpEffectInvertCommand::Blue;


#if DEBUG_KP_EFFECT_INVERT
    kdDebug () << "\treturning channels=" << (int *) channels << endl;
#endif
    return channels;
}


//
// kpEffectInvertWidget implements kpColorEffectWidget interface
//

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


// public virtual [base kpColorEffectWidget]
bool kpEffectInvertWidget::isNoOp () const
{
    return (channels () == kpEffectInvertCommand::None);
}

// public virtual [base kpColorEffectWidget]
TQPixmap kpEffectInvertWidget::applyColorEffect (const TQPixmap &pixmap)
{
    return kpEffectInvertCommand::apply (pixmap, channels ());
}


// public virtual [base kpColorEffectWidget]
kpColorEffectCommand *kpEffectInvertWidget::createCommand () const
{
    return new kpEffectInvertCommand (channels (),
                                      m_actOnSelection,
                                      m_mainWindow);
}


// protected slots
void kpEffectInvertWidget::slotRGBCheckBoxToggled ()
{
    if (m_inSignalHandler)
        return;

    m_inSignalHandler = true;

    //blockSignals (true);
    m_allCheckBox->setChecked (m_redCheckBox->isChecked () &&
                               m_blueCheckBox->isChecked () &&
                               m_greenCheckBox->isChecked ());
    //blockSignals (false);

    emit settingsChanged ();

    m_inSignalHandler = false;
}

// protected slot
void kpEffectInvertWidget::slotAllCheckBoxToggled ()
{
    if (m_inSignalHandler)
        return;

    m_inSignalHandler = true;

    //blockSignals (true);
    m_redCheckBox->setChecked (m_allCheckBox->isChecked ());
    m_greenCheckBox->setChecked (m_allCheckBox->isChecked ());
    m_blueCheckBox->setChecked (m_allCheckBox->isChecked ());
    //blockSignals (false);

    emit settingsChanged ();

    m_inSignalHandler = false;
}


#include <kpeffectinvert.moc>