summaryrefslogtreecommitdiffstats
path: root/src/imageplugins/sheartool/imageeffect_sheartool.cpp
blob: e65bd91bab752d7ed8a76a41a45db82724d6f6f6 (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2004-12-23
 * Description : a plugin to shear an image
 * 
 * Copyright (C) 2004-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * 
 * 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, 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.
 * 
 * ============================================================ */
 
// TQt includes. 
 
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tqlayout.h>
#include <tqimage.h>

// KDE includes.

#include <tdelocale.h>
#include <tdeaboutdata.h>
#include <kiconloader.h>
#include <tdeapplication.h>
#include <kstandarddirs.h>
#include <knuminput.h>
#include <kseparator.h>
#include <kcursor.h>
#include <tdeconfig.h>

// Local includes.

#include "version.h"
#include "ddebug.h"
#include "dimg.h"
#include "imageiface.h"
#include "imagewidget.h"
#include "sheartool.h"
#include "imageeffect_sheartool.h"
#include "imageeffect_sheartool.moc"

namespace DigikamShearToolImagesPlugin
{

ImageEffect_ShearTool::ImageEffect_ShearTool(TQWidget* parent)
                     : Digikam::ImageGuideDlg(parent, i18n("Shear Tool"), "sheartool",
                                              false, true, true, 
                                              Digikam::ImageGuideWidget::HVGuideMode)
{
    // No need Abort button action.
    showButton(User1, false); 
    
    TQString whatsThis;
    
    // About data and help button.
    
    TDEAboutData* about = new TDEAboutData("digikam",
                                       I18N_NOOP("Shear Tool"), 
                                       digikam_version,
                                       I18N_NOOP("A digiKam image plugin to shear an image."),
                                       TDEAboutData::License_GPL,
                                       "(c) 2004-2008, Gilles Caulier",
                                       0,
                                       "http://www.digikam.org");
    
    about->addAuthor("Gilles Caulier", I18N_NOOP("Author and maintainer"),
                     "caulier dot gilles at gmail dot com");
    
    about->addAuthor("Pieter Z. Voloshyn", I18N_NOOP("Shear algorithm"), 
                     "pieter dot voloshyn at gmail dot com"); 

    setAboutData(about);
    
    TQWhatsThis::add( m_imagePreviewWidget, i18n("<p>This is the shearing image operation preview. "
                                           "If you move the mouse cursor on this preview, "
                                           "a vertical and horizontal dashed line will be drawn "
                                           "to guide you in adjusting the shearing correction. "
                                           "Release the left mouse button to freeze the dashed "
                                           "line's position."));
                                           
    // -------------------------------------------------------------

    TQString temp;
    Digikam::ImageIface iface(0, 0);

    TQWidget *gboxSettings     = new TQWidget(plainPage());
    TQGridLayout* gridSettings = new TQGridLayout( gboxSettings, 11, 2, spacingHint());
    
    TQLabel *label1 = new TQLabel(i18n("New width:"), gboxSettings);
    m_newWidthLabel = new TQLabel(temp.setNum( iface.originalWidth()) + i18n(" px"), gboxSettings);
    m_newWidthLabel->setAlignment( AlignBottom | AlignRight );
    
    TQLabel *label2 = new TQLabel(i18n("New height:"), gboxSettings);
    m_newHeightLabel = new TQLabel(temp.setNum( iface.originalHeight()) + i18n(" px"), gboxSettings);
    m_newHeightLabel->setAlignment( AlignBottom | AlignRight );

    gridSettings->addMultiCellWidget(label1, 0, 0, 0, 0);
    gridSettings->addMultiCellWidget(m_newWidthLabel, 0, 0, 1, 2);
    gridSettings->addMultiCellWidget(label2, 1, 1, 0, 0);
    gridSettings->addMultiCellWidget(m_newHeightLabel, 1, 1, 1, 2);
    
    KSeparator *line = new KSeparator(Horizontal, gboxSettings);
    gridSettings->addMultiCellWidget(line, 2, 2, 0, 2);
    
    TQLabel *label3 = new TQLabel(i18n("Main horizontal angle:"), gboxSettings);
    m_mainHAngleInput = new KIntNumInput(gboxSettings);
    m_mainHAngleInput->setRange(-45, 45, 1, true);
    m_mainHAngleInput->setValue(0);
    TQWhatsThis::add( m_mainHAngleInput, i18n("<p>The main horizontal shearing angle, in degrees."));
    gridSettings->addMultiCellWidget(label3, 3, 3, 0, 2);
    gridSettings->addMultiCellWidget(m_mainHAngleInput, 4, 4, 0, 2);
            
    TQLabel *label4 = new TQLabel(i18n("Fine horizontal angle:"), gboxSettings);
    m_fineHAngleInput = new KDoubleNumInput(gboxSettings);
    m_fineHAngleInput->setRange(-5.0, 5.0, 0.01, true);
    m_fineHAngleInput->setValue(0);
    TQWhatsThis::add( m_fineHAngleInput, i18n("<p>This value in degrees will be added to main horizontal angle value "
                                             "to set fine adjustments."));
    gridSettings->addMultiCellWidget(label4, 5, 5, 0, 2);
    gridSettings->addMultiCellWidget(m_fineHAngleInput, 6, 6, 0, 2);
                
    TQLabel *label5 = new TQLabel(i18n("Main vertical angle:"), gboxSettings);
    m_mainVAngleInput = new KIntNumInput(gboxSettings);
    m_mainVAngleInput->setRange(-45, 45, 1, true);
    m_mainVAngleInput->setValue(0);
    TQWhatsThis::add( m_mainVAngleInput, i18n("<p>The main vertical shearing angle, in degrees."));
    gridSettings->addMultiCellWidget(label5, 7, 7, 0, 0);
    gridSettings->addMultiCellWidget(m_mainVAngleInput, 8, 8, 0, 2);

    TQLabel *label6 = new TQLabel(i18n("Fine vertical angle:"), gboxSettings);
    m_fineVAngleInput = new KDoubleNumInput(gboxSettings);
    m_fineVAngleInput->setRange(-5.0, 5.0, 0.01, true);
    m_fineVAngleInput->setValue(0);
    TQWhatsThis::add( m_fineVAngleInput, i18n("<p>This value in degrees will be added to main vertical angle value "
                                             "to set fine adjustments."));
    gridSettings->addMultiCellWidget(label6, 9, 9, 0, 2);
    gridSettings->addMultiCellWidget(m_fineVAngleInput, 10, 10, 0, 2);
                                             
    m_antialiasInput = new TQCheckBox(i18n("Anti-Aliasing"), gboxSettings);
    TQWhatsThis::add( m_antialiasInput, i18n("<p>Enable this option to apply the anti-aliasing filter "
                                            "to the sheared image. "
                                            "To smooth the target image, it will be blurred a little."));
    gridSettings->addMultiCellWidget(m_antialiasInput, 11, 11, 0, 2);
            
    setUserAreaWidget(gboxSettings); 

    // -------------------------------------------------------------
    
    connect(m_mainHAngleInput, TQ_SIGNAL(valueChanged (int)),
            this, TQ_SLOT(slotTimer()));

    connect(m_fineHAngleInput, TQ_SIGNAL(valueChanged (double)),
            this, TQ_SLOT(slotTimer()));
                
    connect(m_mainVAngleInput, TQ_SIGNAL(valueChanged (int)),
            this, TQ_SLOT(slotTimer()));
    
    connect(m_fineVAngleInput, TQ_SIGNAL(valueChanged (double)),
            this, TQ_SLOT(slotTimer()));
    
    connect(m_antialiasInput, TQ_SIGNAL(toggled (bool)),
            this, TQ_SLOT(slotEffect()));  
}

ImageEffect_ShearTool::~ImageEffect_ShearTool()
{
}

void ImageEffect_ShearTool::readUserSettings(void)
{
    TDEConfig *config = kapp->config();
    config->setGroup("sheartool Tool Dialog");
    m_mainHAngleInput->setValue(config->readNumEntry("Main HAngle", 0));
    m_mainVAngleInput->setValue(config->readNumEntry("Main VAngle", 0));
    m_fineHAngleInput->setValue(config->readDoubleNumEntry("Fine HAngle", 0.0));
    m_fineVAngleInput->setValue(config->readDoubleNumEntry("Fine VAngle", 0.0));
    m_antialiasInput->setChecked(config->readBoolEntry("Anti Aliasing", true));
    slotEffect();
}

void ImageEffect_ShearTool::writeUserSettings(void)
{
    TDEConfig *config = kapp->config();
    config->setGroup("sheartool Tool Dialog");
    config->writeEntry("Main HAngle", m_mainHAngleInput->value());
    config->writeEntry("Main VAngle", m_mainVAngleInput->value());
    config->writeEntry("Fine HAngle", m_fineHAngleInput->value());
    config->writeEntry("Fine VAngle", m_fineVAngleInput->value());
    config->writeEntry("Anti Aliasing", m_antialiasInput->isChecked());
    config->sync();
}

void ImageEffect_ShearTool::resetValues()
{
    m_mainHAngleInput->blockSignals(true);
    m_mainVAngleInput->blockSignals(true);
    m_fineHAngleInput->blockSignals(true);
    m_fineVAngleInput->blockSignals(true);
    m_antialiasInput->blockSignals(true);
    m_mainHAngleInput->setValue(0);
    m_mainVAngleInput->setValue(0);
    m_fineHAngleInput->setValue(0.0);
    m_fineVAngleInput->setValue(0.0);
    m_antialiasInput->setChecked(true);
    m_mainHAngleInput->blockSignals(false);
    m_mainVAngleInput->blockSignals(false);
    m_fineHAngleInput->blockSignals(false);
    m_fineVAngleInput->blockSignals(false);
    m_antialiasInput->blockSignals(false);    
} 

void ImageEffect_ShearTool::prepareEffect()
{
    kapp->setOverrideCursor( KCursor::waitCursor() );
    m_mainHAngleInput->setEnabled(false);
    m_mainVAngleInput->setEnabled(false);
    m_fineHAngleInput->setEnabled(false);
    m_fineVAngleInput->setEnabled(false);
    m_antialiasInput->setEnabled(false);

    float hAngle      = m_mainHAngleInput->value() + m_fineHAngleInput->value();
    float vAngle      = m_mainVAngleInput->value() + m_fineVAngleInput->value();
    bool antialiasing = m_antialiasInput->isChecked();    
    TQColor background = paletteBackgroundColor().rgb();

    Digikam::ImageIface* iface = m_imagePreviewWidget->imageIface();
    int orgW = iface->originalWidth();
    int orgH = iface->originalHeight();
    
    uchar *data = iface->getPreviewImage();
    Digikam::DImg image(iface->previewWidth(), iface->previewHeight(), iface->previewSixteenBit(),
                        iface->previewHasAlpha(), data);
    delete [] data;

    m_threadedFilter = dynamic_cast<Digikam::DImgThreadedFilter *>(
                       new ShearTool(&image, this, hAngle, vAngle, antialiasing, background, orgW, orgH));    
}

void ImageEffect_ShearTool::prepareFinal()
{
    m_mainHAngleInput->setEnabled(false);
    m_mainVAngleInput->setEnabled(false);
    m_fineHAngleInput->setEnabled(false);
    m_fineVAngleInput->setEnabled(false);
    m_antialiasInput->setEnabled(false);

    float hAngle      = m_mainHAngleInput->value() + m_fineHAngleInput->value();
    float vAngle      = m_mainVAngleInput->value() + m_fineVAngleInput->value();
    bool antialiasing = m_antialiasInput->isChecked();    
    TQColor background = TQt::black;
            
    Digikam::ImageIface iface(0, 0);
    int orgW = iface.originalWidth();
    int orgH = iface.originalHeight();

    uchar *data = iface.getOriginalImage();
    Digikam::DImg orgImage(orgW, orgH, iface.originalSixteenBit(),
                           iface.originalHasAlpha(), data);
    delete [] data;
    
    m_threadedFilter = dynamic_cast<Digikam::DImgThreadedFilter *>(
                       new ShearTool(&orgImage, this, hAngle, vAngle, antialiasing, background, orgW, orgH));
}

void ImageEffect_ShearTool::putPreviewData(void)
{
    Digikam::ImageIface* iface = m_imagePreviewWidget->imageIface();
    int w = iface->previewWidth();
    int h = iface->previewHeight();
        
    Digikam::DImg imTemp = m_threadedFilter->getTargetImage().smoothScale(w, h, TQSize::ScaleMin);
    Digikam::DImg imDest( w, h, m_threadedFilter->getTargetImage().sixteenBit(),
                                m_threadedFilter->getTargetImage().hasAlpha() );
    
    imDest.fill( Digikam::DColor(paletteBackgroundColor().rgb(),
                                 m_threadedFilter->getTargetImage().sixteenBit()) );
    imDest.bitBltImage(&imTemp, (w-imTemp.width())/2, (h-imTemp.height())/2);

    iface->putPreviewImage((imDest.smoothScale(iface->previewWidth(),
                                               iface->previewHeight())).bits());
                 
    m_imagePreviewWidget->updatePreview();    
    TQSize newSize = dynamic_cast<ShearTool *>(m_threadedFilter)->getNewSize();
    TQString temp;
    m_newWidthLabel->setText(temp.setNum( newSize.width()) + i18n(" px") );
    m_newHeightLabel->setText(temp.setNum( newSize.height()) + i18n(" px") );
}

void ImageEffect_ShearTool::putFinalData(void)
{
    Digikam::ImageIface iface(0, 0);    
    Digikam::DImg targetImage = m_threadedFilter->getTargetImage();
    iface.putOriginalImage(i18n("Shear Tool"),
                           targetImage.bits(),
                           targetImage.width(), targetImage.height());
}

void ImageEffect_ShearTool::renderingFinished()
{
    m_mainHAngleInput->setEnabled(true);
    m_mainVAngleInput->setEnabled(true);
    m_fineHAngleInput->setEnabled(true);
    m_fineVAngleInput->setEnabled(true);
    m_antialiasInput->setEnabled(true);
    kapp->restoreOverrideCursor();
}

}  // NameSpace DigikamShearToolImagesPlugin