summaryrefslogtreecommitdiffstats
path: root/src/libs/dimg/filters/dimgthreadedfilter.cpp
blob: 205405e853bcd4837f8dd71cc2b20c970699be1a (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-05-25
 * Description : threaded image filter class.
 * 
 * Copyright (C) 2005-2007 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 <tqobject.h>
#include <tqevent.h>
#include <tqdeepcopy.h>

// Local includes.

#include "ddebug.h"
#include "dimgthreadedfilter.h"

namespace Digikam
{

DImgThreadedFilter::DImgThreadedFilter(DImg *orgImage, TQObject *parent, 
                                       const TQString& name)
                  : TQThread()
{
    // remove meta data
    m_orgImage      = orgImage->copyImageData();
    m_parent        = parent;
    m_cancel        = false;

    // See B.K.O #133026: make a deep copy of Qstring to prevent crash 
    // on Hyperthreading computer.
    m_name          = TQDeepCopy<TQString>(name);

    m_master        = 0;
    m_slave         = 0;
    m_progressBegin = 0;
    m_progressSpan  = 100;
}

DImgThreadedFilter::DImgThreadedFilter(DImgThreadedFilter *master, const DImg &orgImage, 
                                       const DImg &destImage, int progressBegin, int progressEnd, 
                                       const TQString& name)
{
    m_orgImage      = orgImage;
    m_destImage     = destImage;
    m_parent        = 0;
    m_cancel        = false;

    // See B.K.O #133026: make a deep copy of Qstring to prevent crash 
    // on Hyperthreading computer.
    m_name          = TQDeepCopy<TQString>(name);

    m_master        = master;
    m_slave         = 0;
    m_progressBegin = progressBegin;
    m_progressSpan  = progressEnd - progressBegin;

    m_master->setSlave(this);
}

DImgThreadedFilter::~DImgThreadedFilter()
{
    stopComputation();
    if (m_master)
        m_master->setSlave(0);
}

void DImgThreadedFilter::initFilter(void)
{
    m_destImage.reset();         
    m_destImage = DImg(m_orgImage.width(), m_orgImage.height(),
                       m_orgImage.sixteenBit(), m_orgImage.hasAlpha());
    
    if (m_orgImage.width() && m_orgImage.height())
    {
       if (m_parent)
          start();             // m_parent is valide, start thread ==> run()
       else
          startComputation();  // no parent : no using thread.
    }
    else  // No image data 
    {
       if (m_parent)           // If parent then send event about a problem.
       {
          postProgress(0, false, false);
          DDebug() << m_name << "::No valid image data !!! ..." << endl;
       }
    }
}

void DImgThreadedFilter::stopComputation(void)
{
    m_cancel = true;
    if (m_slave)
    {
        m_slave->m_cancel = true;
        // do not wait on slave, it is not running in its own separate thread!
        //m_slave->cleanupFilter();
    }
    wait();
    cleanupFilter();
}

void DImgThreadedFilter::postProgress(int progress, bool starting, bool success)
{
    if (m_master)
    {
        progress = modulateProgress(progress);
        m_master->postProgress(progress, starting, success);
    }
    else if (m_parent)
    {
       EventData *eventData = new EventData();
       eventData->progress  = progress;
       eventData->starting  = starting;
       eventData->success   = success;
       TQApplication::postEvent(m_parent, new TQCustomEvent(TQEvent::User, eventData));
    }
}

void DImgThreadedFilter::startComputation()
{
    // See B.K.O #133026: do not use kdDebug() statements in threaded implementation
    // to prevent crash under Hyperthreaded CPU.

    if (m_parent)
       postProgress(0, true, false);

    filterImage();
    
    if (!m_cancel)
    {
       if (m_parent)
          postProgress(0, false, true);
    }
    else
    {
       if (m_parent)
          postProgress(0, false, false);
    }
}

void DImgThreadedFilter::setSlave(DImgThreadedFilter *slave)
{
    m_slave = slave;
}

int DImgThreadedFilter::modulateProgress(int progress)
{
    return m_progressBegin + (int)((double)progress * (double)m_progressSpan / 100.0);
}

}  // NameSpace Digikam