summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_scale_visitor.h
blob: afe358ad0f5c5383c3ba796e9c3e9563b33b34e0 (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
/*
 *  copyright (c) 2004, 2005 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
 *
 *  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., 675 mass ave, cambridge, ma 02139, usa.
 */

#ifndef KIS_SCALE_VISITOR_H_
#define KIS_SCALE_VISITOR_H_

#include "klocale.h"

#include "kis_progress_subject.h"
#include "kis_progress_display_interface.h"
#include "kis_thread.h"
#include "kis_layer_visitor.h"
#include "kis_types.h"
#include "kis_layer.h"
#include "kis_group_layer.h"
#include "kis_paint_layer.h"
#include "kis_adjustment_layer.h"
#include "kis_transaction.h"
#include "kis_undo_adapter.h"
#include "kis_selection.h"

class KisProgressDisplayInterface;
class KisFilterStrategy;

class KisScaleWorker : public KisThread {

    /* Structs for the image rescaling routine */
    class Contrib {
    public:
        TQ_INT32 m_pixel;
        double m_weight;
    };

    class ContribList {
    public:
        TQ_INT32  n;  //number of contributors
        Contrib *p; //pointer to list of contributions
    };

public:

    KisScaleWorker(KisPaintDevice * dev, double sx, double sy,
                   KisFilterStrategy *filterStrategy)
        : KisThread()
        , m_dev(dev)
        , m_sx(sx)
        , m_sy(sy)
        , m_filterStrategy(filterStrategy) {};

    virtual ~KisScaleWorker() {};

    void run();

private:
    TQ_INT32 m_pixelSize;
    KisPaintDevice * m_dev;
    double m_sx, m_sy;
    KisFilterStrategy * m_filterStrategy;


    /**
     * calc_x_contrib()
     *
     * Calculates the filter weights for a single target column.
     * contribX->p must be freed afterwards.
     *
     * Returns -1 if error, 0 otherwise.
     */
    int calcContrib(ContribList *contribX, double cale, double fwidth, int srcwidth, KisFilterStrategy *filterStrategy, TQ_INT32 i);

    ContribList * contrib;  //array of contribution lists


};


class KisScaleVisitor : public KisLayerVisitor, KisProgressSubject {

public:

    KisScaleVisitor(KisImageSP img,
                    double sx, 
                    double sy, 
                    KisProgressDisplayInterface *progress, 
                    KisFilterStrategy *filterStrategy) 
        : KisLayerVisitor()
        , m_img(img)
        , m_sx(sx)
        , m_sy(sy)
        , m_progress(progress)
        , m_filterStrategy(filterStrategy)
    {
        if ( progress )
            progress -> setSubject(this, true, true);
        emit notifyProgressStage(i18n("Scaling..."),0);
    }

    virtual ~KisScaleVisitor()
    {
        // Wait for all threads to finish
        KisThread * t;
        int threadcount = m_scalethreads.count();
        int i = 0;
        for ( t = m_scalethreads.first(); t; t = m_scalethreads.next()) {
            //progress info
            if (t) t->wait();
            emit notifyProgress((100 / threadcount) * i);
            ++i;

        }
        emit notifyProgressDone();
        // Delete all threads
        m_scalethreads.setAutoDelete(true);
        m_scalethreads.clear();
    }

    bool visit(KisPaintLayer *layer) 
    {
        // XXX: If all is well, then the image's undoadapter will have started a macro for us
        //      This will break in a more multi-threaded environment
        if (m_img->undoAdapter() && m_img->undoAdapter()->undo()) {
            KisTransaction * cmd = new KisTransaction("", layer->paintDevice());
            m_img->undoAdapter()->addCommand(cmd);
        }

        KisScaleWorker * scaleThread = new KisScaleWorker(layer->paintDevice(),
                                                     m_sx, m_sy, m_filterStrategy);
        m_scalethreads.append(scaleThread);
        scaleThread->start();
        //scaleThread->run();
        layer->setDirty();
        return true;
    }

    bool visit(KisGroupLayer *layer)
    {
        //KisScaleVisitor visitor (m_img, m_sx, m_sy, m_progress, m_filterStrategy);

        // XXX: Maybe faster to scale the projection and do something clever to avoid 
	//      recompositing everything?
	layer->resetProjection(); 
       

        KisLayerSP child = layer->firstChild();
        while (child) {
            child->accept(*this);
            child = child->nextSibling();
        }

        return true;
    }

    bool visit(KisPartLayer */*layer*/)
    {
        return true;
    }

    virtual bool visit(KisAdjustmentLayer* layer)
    {
        KisThread * scaleThread = new KisScaleWorker(layer->selection().data(), m_sx, m_sy, m_filterStrategy);
        m_scalethreads.append(scaleThread);
        scaleThread->start();
        layer->resetCache();
        layer->setDirty();
        return true;
    }
    
    
    // Implement KisProgressSubject
    virtual void cancel() 
    {
        KisThread * t;
        for ( t = m_scalethreads.first(); t; t = m_scalethreads.next()) {
            t->cancel();
        }      
    }
    

private:

    TQPtrList<KisThread> m_scalethreads;
    KisImageSP m_img;
    double m_sx;
    double m_sy;
    KisProgressDisplayInterface * m_progress;
    KisFilterStrategy * m_filterStrategy;
};

#endif // KIS_SCALE_VISITOR_H_