~ubuntu-branches/ubuntu/breezy/koffice/breezy-security

« back to all changes in this revision

Viewing changes to krita/plugins/colorrange/kis_tool_selectpicker.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-10-11 14:49:50 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051011144950-lwpngbifzp8nk0ds
Tags: 1:1.4.1-0ubuntu7
* SECURITY UPDATE: fix heap based buffer overflow in the RTF importer of KWord
* Opening specially crafted RTF files in KWord can cause
  execution of abitrary code.
* Add kubuntu_01_rtfimport_heap_overflow.diff
* References:
  CAN-2005-2971
  CESA-2005-005
  http://www.koffice.org/security/advisory-20051011-1.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 1999 Matthias Elter <me@kde.org>
 
3
 *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
 
4
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 */
 
20
 
 
21
#include <qpoint.h>
 
22
#include <qlayout.h>
 
23
#include <qcheckbox.h>
 
24
#include <qlabel.h>
 
25
#include <qcombobox.h>
 
26
#include <qcolor.h>
 
27
#include <qtimer.h>
 
28
 
 
29
#include <kapplication.h>
 
30
#include <kaction.h>
 
31
#include <klocale.h>
 
32
#include <knuminput.h>
 
33
 
 
34
#include <kis_cursor.h>
 
35
#include <kis_selection_manager.h>
 
36
#include <kis_canvas_subject.h>
 
37
#include <kis_image.h>
 
38
#include <kis_paint_device.h>
 
39
#include <kis_button_press_event.h>
 
40
#include <kis_canvas_subject.h>
 
41
#include <kis_selection_options.h>
 
42
#include <kis_selection.h>
 
43
#include <kis_paint_device.h>
 
44
#include <kis_iterators_pixel.h>
 
45
#include <kis_color_utilities.h>
 
46
#include <kis_selected_transaction.h>
 
47
#include <kis_undo_adapter.h>
 
48
 
 
49
#include "kis_tool_selectpicker.h"
 
50
 
 
51
void selectByColor(KisPaintDeviceSP dev, KisSelectionSP selection, const QColor & c, int fuzziness, enumSelectionMode mode)
 
52
{
 
53
        // XXX: Multithread this!
 
54
        Q_INT32 x, y, w, h;
 
55
 
 
56
        QUANTUM opacity;
 
57
        dev -> exactBounds(x, y, w, h);
 
58
 
 
59
        KisStrategyColorSpaceSP cs = dev -> colorStrategy();
 
60
        KisProfileSP profile = dev -> profile();
 
61
 
 
62
        for (int y2 = y; y2 < h - y; ++y2) {
 
63
                KisHLineIterator hiter = dev -> createHLineIterator(x, y2, w, false);
 
64
                KisHLineIterator selIter = selection -> createHLineIterator(x, y2, w, true);
 
65
                while (!hiter.isDone()) {
 
66
                        QColor c2;
 
67
                        cs -> toQColor(hiter.rawData(), &c2, &opacity, profile);
 
68
 
 
69
                        // Don't try to select transparent pixels. The Gimp has an option to match transparent pixels; we don't, for the moment.
 
70
                        if (opacity > OPACITY_TRANSPARENT) {
 
71
 
 
72
                                Q_UINT8 match = matchColors(c, c2, fuzziness);
 
73
                                //kdDebug() << " Match: " << QString::number(match) << ", mode: " << mode << "\n";
 
74
                                if (mode == SELECTION_ADD) {
 
75
                                        Q_UINT8 d = *(selIter.rawData());
 
76
                                        if (d + match > MAX_SELECTED) {
 
77
                                                *(selIter.rawData()) = MAX_SELECTED;
 
78
                                        }
 
79
                                        else {
 
80
                                                *(selIter.rawData()) = match + d;
 
81
                                        }
 
82
 
 
83
                                }
 
84
                                else if (mode == SELECTION_SUBTRACT) {
 
85
                                        Q_UINT8 selectedness = *(selIter.rawData());
 
86
                                        if (match < selectedness) {
 
87
                                                *(selIter.rawData()) = selectedness - match;
 
88
                                        }
 
89
                                        else {
 
90
                                                *(selIter.rawData()) = 0;
 
91
                                        }
 
92
                                }
 
93
                        }
 
94
                        ++hiter;
 
95
                        ++selIter;
 
96
                }
 
97
        }
 
98
 
 
99
}
 
100
 
 
101
 
 
102
 
 
103
KisToolSelectPicker::KisToolSelectPicker()
 
104
{
 
105
        setName("tool_selectpicker");
 
106
        setCursor(KisCursor::pickerCursor());
 
107
        m_subject = 0;
 
108
        m_optWidget = 0;
 
109
        m_selectionOptionsWidget = 0;
 
110
        m_fuzziness = 20;
 
111
        m_currentSelectAction = m_defaultSelectAction = SELECTION_ADD;
 
112
        m_timer = new QTimer(this);
 
113
        connect(m_timer, SIGNAL(timeout()), SLOT(slotTimer()) );
 
114
}
 
115
 
 
116
KisToolSelectPicker::~KisToolSelectPicker()
 
117
{
 
118
}
 
119
 
 
120
void KisToolSelectPicker::activate()
 
121
{
 
122
        KisToolNonPaint::activate();
 
123
        m_timer->start(50);
 
124
        setPickerCursor(m_currentSelectAction);
 
125
 
 
126
        if (m_selectionOptionsWidget) {
 
127
                m_selectionOptionsWidget -> slotActivated();
 
128
        }
 
129
}
 
130
 
 
131
void KisToolSelectPicker::clear()
 
132
{
 
133
        m_timer->stop();
 
134
}
 
135
 
 
136
void KisToolSelectPicker::buttonPress(KisButtonPressEvent *e)
 
137
{
 
138
        kdDebug() << "button press: " << m_subject << "\n";
 
139
 
 
140
        if (m_subject) {
 
141
                KisImageSP img;
 
142
                KisPaintDeviceSP dev;
 
143
                QPoint pos;
 
144
                QColor c;
 
145
                QUANTUM opacity;
 
146
 
 
147
                if (e -> button() != QMouseEvent::LeftButton && e -> button() != QMouseEvent::RightButton)
 
148
                        return;
 
149
 
 
150
                if (!(img = m_subject -> currentImg()))
 
151
                        return;
 
152
 
 
153
                dev = img -> activeDevice();
 
154
 
 
155
                if (!dev || !dev -> visible())
 
156
                        return;
 
157
 
 
158
 
 
159
                pos = QPoint(e -> pos().floorX(), e -> pos().floorY());
 
160
 
 
161
                KisSelectedTransaction *t = new KisSelectedTransaction(i18n("Selection Picker"),dev);
 
162
 
 
163
                dev -> pixel(pos.x(), pos.y(), &c, &opacity);
 
164
                kdDebug() << "Going to select colors similar to: " << c.red() << ", " << c.green() << ", "<< c.blue() << "\n";
 
165
                if (opacity > OPACITY_TRANSPARENT)
 
166
                        selectByColor(dev, dev -> selection(), c, m_fuzziness, m_currentSelectAction);
 
167
                else
 
168
                        m_subject -> selectionManager() -> selectAll();
 
169
 
 
170
                if(img -> undoAdapter())
 
171
                        img -> undoAdapter() -> addCommand(t);
 
172
                m_subject -> canvasController() -> updateCanvas();
 
173
                m_selectionOptionsWidget -> ensureMaskColor();
 
174
        }
 
175
}
 
176
 
 
177
void KisToolSelectPicker::slotTimer()
 
178
{
 
179
#if KDE_IS_VERSION(3,4,0)
 
180
        int state = kapp->keyboardMouseState() & (Qt::ShiftButton|Qt::ControlButton|Qt::AltButton);
 
181
#else
 
182
        int state = kapp->keyboardModifiers() & (KApplication::ShiftModifier
 
183
                        |KApplication::ControlModifier|KApplication::Modifier1);
 
184
#endif
 
185
        enumSelectionMode action;
 
186
 
 
187
        if (state == Qt::ShiftButton)
 
188
                action = SELECTION_ADD;
 
189
        else if (state == Qt::ControlButton)
 
190
                action = SELECTION_SUBTRACT;
 
191
        else
 
192
                action = m_defaultSelectAction;
 
193
 
 
194
        if (action != m_currentSelectAction) {
 
195
                m_currentSelectAction = action;
 
196
                setPickerCursor(action);
 
197
        }
 
198
}
 
199
 
 
200
void KisToolSelectPicker::setPickerCursor(enumSelectionMode action)
 
201
{
 
202
        switch (action) {
 
203
                case SELECTION_ADD:
 
204
                        m_subject -> setCanvasCursor(KisCursor::pickerPlusCursor());
 
205
                        break;
 
206
                case SELECTION_SUBTRACT:
 
207
                        m_subject -> setCanvasCursor(KisCursor::pickerMinusCursor());
 
208
        }
 
209
}
 
210
 
 
211
void KisToolSelectPicker::setup(KActionCollection *collection)
 
212
{
 
213
        m_action = static_cast<KRadioAction *>(collection -> action(name()));
 
214
 
 
215
        if (m_action == 0) {
 
216
                m_action = new KRadioAction(i18n("Tool &Selection Picker"), "tool_picker_selection", Qt::Key_E, this, SLOT(activate()), collection, name());
 
217
                Q_CHECK_PTR(m_action);
 
218
                m_action -> setExclusiveGroup("tools");
 
219
                m_ownAction = true;
 
220
        }
 
221
}
 
222
 
 
223
void KisToolSelectPicker::update(KisCanvasSubject *subject)
 
224
{
 
225
        super::update(subject);
 
226
        m_subject = subject;
 
227
}
 
228
 
 
229
void KisToolSelectPicker::slotSetFuzziness(int fuzziness)
 
230
{
 
231
        m_fuzziness = fuzziness;
 
232
}
 
233
 
 
234
void KisToolSelectPicker::slotSetAction(int action)
 
235
{
 
236
        m_defaultSelectAction = (enumSelectionMode)action;
 
237
}
 
238
 
 
239
QWidget* KisToolSelectPicker::createOptionWidget(QWidget* parent)
 
240
{
 
241
        m_optWidget = new QWidget(parent);
 
242
        Q_CHECK_PTR(m_optWidget);
 
243
 
 
244
        m_optWidget -> setCaption(i18n("Selection Picker"));
 
245
 
 
246
        QVBoxLayout * l = new QVBoxLayout(m_optWidget);
 
247
        Q_CHECK_PTR(l);
 
248
 
 
249
        m_selectionOptionsWidget = new KisSelectionOptions(m_optWidget, m_subject);
 
250
        Q_CHECK_PTR(m_selectionOptionsWidget);
 
251
 
 
252
        l -> addWidget(m_selectionOptionsWidget);
 
253
        connect (m_selectionOptionsWidget, SIGNAL(actionChanged(int)), this, SLOT(slotSetAction(int)));
 
254
 
 
255
        QHBoxLayout * hbox = new QHBoxLayout(l);
 
256
        Q_CHECK_PTR(hbox);
 
257
 
 
258
        QLabel * lbl = new QLabel(i18n("Fuzziness: "), m_optWidget);
 
259
        Q_CHECK_PTR(lbl);
 
260
 
 
261
        hbox -> addWidget(lbl);
 
262
 
 
263
        KIntNumInput * input = new KIntNumInput(m_optWidget, "fuzziness");
 
264
        Q_CHECK_PTR(input);
 
265
 
 
266
        input -> setRange(0, 200, 10, true);
 
267
        input -> setValue(20);
 
268
        hbox -> addWidget(input);
 
269
        connect(input, SIGNAL(valueChanged(int)), this, SLOT(slotSetFuzziness(int)));
 
270
 
 
271
        return m_optWidget;
 
272
}
 
273
 
 
274
QWidget* KisToolSelectPicker::optionWidget()
 
275
{
 
276
        return m_optWidget;
 
277
}
 
278
 
 
279
#include "kis_tool_selectpicker.moc"