summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/paintops/defaultpaintops/kis_brushop.cc
blob: 285b4d6fc3a2107d05feb94589fca0215ec2d714 (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
/*
 *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
 *  Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
 *  Copyright (c) 2004 Clarence Dang <dang@kde.org>
 *  Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
 *  Copyright (c) 2004 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 <string.h>

#include <tqrect.h>
#include <tqwidget.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqtoolbutton.h>

#include <kdebug.h>

#include "kcurve.h"
#include "kis_brush.h"
#include "kis_global.h"
#include "kis_paint_device.h"
#include "kis_layer.h"
#include "kis_painter.h"
#include "kis_types.h"
#include "kis_paintop.h"
#include "kis_input_device.h"
#include "kis_selection.h"
#include "kis_brushop.h"

#include "kis_dlgbrushcurvecontrol.h"

KisPaintOp * KisBrushOpFactory::createOp(const KisPaintOpSettings *settings, KisPainter * painter)
{
    const KisBrushOpSettings *brushopSettings = dynamic_cast<const KisBrushOpSettings *>(settings);
    Q_ASSERT(settings == 0 || brushopSettings != 0);

    KisPaintOp * op = new KisBrushOp(brushopSettings, painter);
    TQ_CHECK_PTR(op);
    return op;
}

KisBrushOpSettings::KisBrushOpSettings(TQWidget *parent)
    : super(parent)
{
    m_optionsWidget = new TQWidget(parent, "brush option widget");
    TQHBoxLayout * l = new TQHBoxLayout(m_optionsWidget);
    l->setAutoAdd(true);
    m_pressureVariation = new TQLabel(i18n("Pressure variation: "), m_optionsWidget);
    m_size =  new TQCheckBox(i18n("Size"), m_optionsWidget);
    m_size->setChecked(true);
    m_opacity = new TQCheckBox(i18n("Opacity"), m_optionsWidget);
    m_darken = new TQCheckBox(i18n("Darken"), m_optionsWidget);
    m_curveControl = new WdgBrushCurveControl(m_optionsWidget);
    TQToolButton* moreButton = new TQToolButton(TQt::UpArrow, m_optionsWidget);
    moreButton->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
    moreButton->setMinimumSize(TQSize(24,24)); // Bah, I had hoped the above line would make this unneeded
    connect(moreButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotCustomCurves()));

    m_customSize = false;
    m_customOpacity = false;
    m_customDarken = false;
    // the curves will get filled in when the slot gets accepted
}

void KisBrushOpSettings::slotCustomCurves() {
    if (m_curveControl->exec() == TQDialog::Accepted) {
        m_customSize = m_curveControl->sizeCheckbox->isChecked();
        m_customOpacity = m_curveControl->opacityCheckbox->isChecked();
        m_customDarken = m_curveControl->darkenCheckbox->isChecked();

        if (m_customSize) {
            transferCurve(m_curveControl->sizeCurve, m_sizeCurve);
        }
        if (m_customOpacity) {
            transferCurve(m_curveControl->opacityCurve, m_opacityCurve);
        }
        if (m_customDarken) {
            transferCurve(m_curveControl->darkenCurve, m_darkenCurve);
        }
    }
}

void KisBrushOpSettings::transferCurve(KCurve* curve, double* target) {
    double value;
    for (int i = 0; i < 256; i++) {
        value = curve->getCurveValue( i / 255.0);
        if (value < PRESSURE_MIN)
            target[i] = PRESSURE_MIN;
        else if (value > PRESSURE_MAX)
            target[i] = PRESSURE_MAX;
        else
            target[i] = value;
    }
}


bool KisBrushOpSettings::varySize() const
{
    return m_size->isChecked();
}

bool KisBrushOpSettings::varyOpacity() const
{
    return m_opacity->isChecked();
}

bool KisBrushOpSettings::varyDarken() const
{
    return m_darken->isChecked();
}

KisPaintOpSettings* KisBrushOpFactory::settings(TQWidget * parent, const KisInputDevice& inputDevice)
{
    if (inputDevice == KisInputDevice::mouse()) {
        // No options for mouse, only tablet devices
        return 0;
    } else {
        return new KisBrushOpSettings(parent);
    }
}

KisBrushOp::KisBrushOp(const KisBrushOpSettings *settings, KisPainter *painter)
    : super(painter)
    , m_pressureSize(true)
    , m_pressureOpacity(false)
    , m_pressureDarken(false)
    , m_customSize(false)
    , m_customOpacity(false)
    , m_customDarken(false)
{
    if (settings != 0) {
        m_pressureSize = settings->varySize();
        painter->setVaryBrushSpacingWithPressureWhenDrawingALine( m_pressureSize );

        m_pressureOpacity = settings->varyOpacity();
        m_pressureDarken = settings->varyDarken();
        m_customSize = settings->customSize();
        m_customOpacity = settings->customOpacity();
        m_customDarken = settings->customDarken();
        if (m_customSize) {
            memcpy(m_sizeCurve, settings->sizeCurve(), 256 * sizeof(double));
        }
        if (m_customOpacity) {
            memcpy(m_opacityCurve, settings->opacityCurve(), 256 * sizeof(double));
        }
        if (m_customDarken) {
            memcpy(m_darkenCurve, settings->darkenCurve(), 256 * sizeof(double));
        }
    }
}

KisBrushOp::~KisBrushOp()
{
    m_painter->setVaryBrushSpacingWithPressureWhenDrawingALine( true );
}

void KisBrushOp::paintAt(const KisPoint &pos, const KisPaintInformation& info)
{
    KisPaintInformation adjustedInfo(info);
    if (!m_pressureSize)
        adjustedInfo.pressure = PRESSURE_DEFAULT;
    else if (m_customSize)
        adjustedInfo.pressure = scaleToCurve(adjustedInfo.pressure, m_sizeCurve);

    // Painting should be implemented according to the following algorithm:
    // retrieve brush
    // if brush == mask
    //          retrieve mask
    // else if brush == image
    //          retrieve image
    // subsample (mask | image) for position -- pos should be double!
    // apply filters to mask (colour | gradient | pattern | etc.
    // composite filtered mask into temporary layer
    // composite temporary layer into target layer
    // @see: doc/brush.txt

    if (!m_painter->device()) return;

    KisBrush *brush = m_painter->brush();

    Q_ASSERT(brush);
    if (!brush) return;
    if (! brush->canPaintFor(adjustedInfo) )
        return;

    KisPaintDeviceSP device = m_painter->device();

    KisPoint hotSpot = brush->hotSpot(adjustedInfo);
    KisPoint pt = pos - hotSpot;

    // Split the coordinates into integer plus fractional parts. The integer
    // is where the dab will be positioned and the fractional part determines
    // the sub-pixel positioning.
    TQ_INT32 x;
    double xFraction;
    TQ_INT32 y;
    double yFraction;

    splitCoordinate(pt.x(), &x, &xFraction);
    splitCoordinate(pt.y(), &y, &yFraction);

    KisPaintDeviceSP dab = 0;

    TQ_UINT8 origOpacity = m_painter->opacity();
    KisColor origColor = m_painter->paintColor();

    if (m_pressureOpacity) {
        if (!m_customOpacity)
            m_painter->setOpacity((TQ_INT8)(origOpacity * info.pressure));
        else
            m_painter->setOpacity((TQ_INT8)(origOpacity * scaleToCurve(info.pressure, m_opacityCurve)));
    }

    if (m_pressureDarken) {
        KisColor darkened = origColor;
        // Darken docs aren't really clear about what exactly the amount param can have as value...
        TQ_UINT32 darkenAmount;
        if (!m_customDarken)
            darkenAmount = (TQ_INT32)(255  - 75 * info.pressure);
        else
            darkenAmount = (TQ_INT32)(255  - 75 * scaleToCurve(info.pressure, m_darkenCurve));

        darkened.colorSpace()->darken(origColor.data(), darkened.data(),
            darkenAmount, false, 0.0, 1);
        m_painter->setPaintColor(darkened);
    }

    if (brush->brushType() == IMAGE || brush->brushType() == PIPE_IMAGE) {
        dab = brush->image(device->colorSpace(), adjustedInfo, xFraction, yFraction);
    }
    else {
        KisAlphaMaskSP mask = brush->mask(adjustedInfo, xFraction, yFraction);
        dab = computeDab(mask);
    }

    m_painter->setPressure(adjustedInfo.pressure);

    TQRect dabRect = TQRect(0, 0, brush->maskWidth(adjustedInfo),
                          brush->maskHeight(adjustedInfo));
    TQRect dstRect = TQRect(x, y, dabRect.width(), dabRect.height());

    KisImage * image = device->image();

    if (image != 0) {
        dstRect &= image->bounds();
    }

    if (dstRect.isNull() || dstRect.isEmpty() || !dstRect.isValid()) return;

    TQ_INT32 sx = dstRect.x() - x;
    TQ_INT32 sy = dstRect.y() - y;
    TQ_INT32 sw = dstRect.width();
    TQ_INT32 sh = dstRect.height();

    if (m_source->hasSelection()) {
        m_painter->bltSelection(dstRect.x(), dstRect.y(), m_painter->compositeOp(), dab.data(),
                                m_source->selection(), m_painter->opacity(), sx, sy, sw, sh);
    }
    else {
        m_painter->bitBlt(dstRect.x(), dstRect.y(), m_painter->compositeOp(), dab.data(), m_painter->opacity(), sx, sy, sw, sh);
    }
    m_painter->addDirtyRect(dstRect);

    m_painter->setOpacity(origOpacity);
    m_painter->setPaintColor(origColor);
}

#include "kis_brushop.moc"