summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/filters/threadtest/threadtest.cpp
blob: 8477c09c74acb14f626ba68f9a43129c898c20a1 (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
/*
 * This file is part of the KDE project
 *
 * 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 <stdlib.h>
#include <vector>

#include <tqpoint.h>
#include <tqrect.h>
#include <tqthread.h>

#include <tdelocale.h>
#include <kiconloader.h>
#include <kinstance.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>
#include <tdetempfile.h>
#include <kdebug.h>
#include <kgenericfactory.h>

#include <kis_doc.h>
#include <kis_image.h>
#include <kis_iterators_pixel.h>
#include <kis_layer.h>
#include <kis_filter_registry.h>
#include <kis_global.h>
#include <kis_types.h>

// #include <tdemessagebox.h>

#include "threadtest.h"

typedef KGenericFactory<ChalkThreadTest> ChalkThreadTestFactory;
K_EXPORT_COMPONENT_FACTORY( chalkthreadtest, ChalkThreadTestFactory( "chalk" ) )

ChalkThreadTest::ChalkThreadTest(TQObject *parent, const char *name, const TQStringList &)
        : KParts::Plugin(parent, name)
{
    setInstance(ChalkThreadTestFactory::instance());

    if (parent->inherits("KisFilterRegistry")) {
        KisFilterRegistry * r = dynamic_cast<KisFilterRegistry *>(parent);
        r->add(new KisFilterInvert());
    }
}

ChalkThreadTest::~ChalkThreadTest()
{
}

class InversionThread : public TQThread
{

public:

    InversionThread(const TQString & name, KisPaintDeviceSP src, KisPaintDeviceSP dst, const TQRect& rect)
        : TQThread()
        , m_name(name)
        , m_src(src)
        , m_dst(dst)
        , m_rect(rect)
        {
            kdDebug() << "Thread created " << m_name << ", " << TQThread::currentThread() << ", " << m_rect << "\n";
        };

    void run()
        {
            kdDebug() << "Thread started:" << m_name  << ", " << TQThread::currentThread() << "\n";

            KisRectIteratorPixel dstIt = m_dst->createRectIterator(m_rect.x(), m_rect.y(), m_rect.width(), m_rect.height(), true );
            KisRectIteratorPixel srcIt = m_src->createRectIterator(m_rect.x(), m_rect.y(), m_rect.width(), m_rect.height(), false);
            TQ_INT32 depth = m_src -> colorSpace() -> nColorChannels();

            kdDebug() << "Thread " << m_name << " starts loop \n";

            while( ! srcIt.isDone() ) {
                if ( srcIt.isSelected() ) {
                    for( int i = 0; i < depth; i++) {
                        dstIt.rawData()[i] = TQ_UINT8_MAX - srcIt.oldRawData()[i];
                   }
                }
                ++srcIt;
                ++dstIt;
            }
            kdDebug() << "Thread " << m_name << " finished \n";
        };

private:
    TQString m_name;
    KisPaintDeviceSP m_src;
    KisPaintDeviceSP m_dst;
    TQRect m_rect;

};

KisFilterInvert::KisFilterInvert() : KisFilter(id(), "colors", i18n("Invert with &Threads"))
{
}

void KisFilterInvert::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* /*config*/, const TQRect& rect)
{
    kdDebug() << "Going to invert " << rect << "\n";
    int h2 = rect.height() / 2;
    int w2 = rect.width() / 2;

    setProgressTotalSteps(4);
    InversionThread t1("t1", src, dst, TQRect(0, 0, w2, h2));
    InversionThread t2("t2", src, dst, TQRect(w2, 0, w2, h2));
    InversionThread t3("t3", src, dst, TQRect(0, h2, w2, h2));
    InversionThread t4("t4", src, dst, TQRect(w2, h2, w2, h2));

    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t1.wait();
    setProgress(1);
    t2.wait();
    setProgress(2);
    t3.wait();
    setProgress(3);
    t4.wait();
    setProgress(4);

    setProgressDone();
}