summaryrefslogtreecommitdiffstats
path: root/krita/core/kis_scale_visitor.cc
blob: 424db7c62b81168f60f7c331350b076ef6c0ac3e (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
/*
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <qdatetime.h>

#include <kdebug.h>
#include <klocale.h>

#include "kis_paint_device.h"
#include "kis_scale_visitor.h"
#include "kis_filter_strategy.h"


void KisScaleWorker::run()
{
    double fwidth = m_filterStrategy->support();

    QRect rect = m_dev -> exactBounds();
    Q_INT32 width = rect.width();
    Q_INT32 height =  rect.height();
    m_pixelSize=m_dev -> pixelSize();

    // compute size of target image
    if ( m_sx == 1.0F && m_sy == 1.0F ) {
        return;
    }
    Q_INT32 targetW = QABS( qRound( m_sx * width ) );
    Q_INT32 targetH = QABS( qRound( m_sy * height ) );

    Q_UINT8* newData = new Q_UINT8[targetW * targetH * m_pixelSize ];
    Q_CHECK_PTR(newData);

    double* weight = new double[ m_pixelSize ];    /* filter calculation variables */

    Q_UINT8* pel = new Q_UINT8[ m_pixelSize ];
    Q_CHECK_PTR(pel);

    Q_UINT8 *pel2 = new Q_UINT8[ m_pixelSize ];
    Q_CHECK_PTR(pel2);

    bool* bPelDelta = new bool[ m_pixelSize ];
    ContribList    *contribX;
    ContribList    contribY;
    const Q_INT32 BLACK_PIXEL=0;
    const Q_INT32 WHITE_PIXEL=255;


    // create intermediate row to hold vertical dst row zoom
    Q_UINT8 * tmp = new Q_UINT8[ width * m_pixelSize ];
    Q_CHECK_PTR(tmp);

    //create array of pointers to intermediate rows
    Q_UINT8 **tmpRows = new Q_UINT8*[ height ];

    //create array of pointers to intermediate rows that are actually used simultaneously and allocate memory for the rows
    Q_UINT8 **tmpRowsMem;
    if(m_sy < 1.0)
    {
        tmpRowsMem = new Q_UINT8*[ (int)(fwidth / m_sy * 2 + 1) ];
        for(int i = 0; i < (int)(fwidth / m_sy * 2 + 1); i++)
        {
             tmpRowsMem[i] = new Q_UINT8[ width * m_pixelSize ];
             Q_CHECK_PTR(tmpRowsMem[i]);
        }
    }
    else
    {
        tmpRowsMem = new Q_UINT8*[ (int)(fwidth * 2 + 1) ];
        for(int i = 0; i < (int)(fwidth * 2 + 1); i++)
        {
            tmpRowsMem[i] = new Q_UINT8[ width * m_pixelSize ];
            Q_CHECK_PTR(tmpRowsMem[i]);
        }
    }

    // build x weights
    contribX = new ContribList[ targetW ];
    for(int x = 0; x < targetW; x++)
    {
        calcContrib(&contribX[x], m_sx, fwidth, width, m_filterStrategy, x);
    }

    QTime starttime = QTime::currentTime ();

    for(int y = 0; y < targetH; y++)
    {
        // build y weights
        calcContrib(&contribY, m_sy, fwidth, height, m_filterStrategy, y);

        //copy pixel data to temporary arrays
        for(int srcpos = 0; srcpos < contribY.n; srcpos++)
        {
            if (!(contribY.p[srcpos].m_pixel < 0 || contribY.p[srcpos].m_pixel >= height))
            {
                tmpRows[contribY.p[srcpos].m_pixel] = new Q_UINT8[ width * m_pixelSize ];
                //tmpRows[ contribY.p[srcpos].m_pixel ] = tmpRowsMem[ srcpos ];
                m_dev ->readBytes(tmpRows[contribY.p[srcpos].m_pixel], 0, contribY.p[srcpos].m_pixel, width, 1);
            }
        }

        /* Apply vert filter to make dst row in tmp. */
        for(int x = 0; x < width; x++)
        {
            for(int channel = 0; channel < m_pixelSize; channel++){
                weight[channel] = 0.0;
                bPelDelta[channel] = FALSE;
                pel[channel]=tmpRows[contribY.p[0].m_pixel][ x * m_pixelSize + channel ];
            }
            for(int srcpos = 0; srcpos < contribY.n; srcpos++)
            {
                if (!(contribY.p[srcpos].m_pixel < 0 || contribY.p[srcpos].m_pixel >= height)){
                    for(int channel = 0; channel < m_pixelSize; channel++)
                    {
                        pel2[channel]=tmpRows[contribY.p[srcpos].m_pixel][ x * m_pixelSize + channel ];
                        if(pel2[channel] != pel[channel]) bPelDelta[channel] = TRUE;
                            weight[channel] += pel2[channel] * contribY.p[srcpos].m_weight;
                    }
                }
            }

            for(int channel = 0; channel < m_pixelSize; channel++){
                weight[channel] = bPelDelta[channel] ? static_cast<int>(qRound(weight[channel])) : pel[channel];
                        tmp[ x * m_pixelSize + channel ] = static_cast<Q_UINT8>(CLAMP(weight[channel], BLACK_PIXEL, WHITE_PIXEL));
            }
        } /* next row in temp column */
        delete[] contribY.p;

        for(int x = 0; x < targetW; x++)
        {
            for(int channel = 0; channel < m_pixelSize; channel++){
                weight[channel] = 0.0;
                bPelDelta[channel] = FALSE;
                pel[channel] = tmp[ contribX[x].p[0].m_pixel * m_pixelSize + channel ];
            }
            for(int srcpos = 0; srcpos < contribX[x].n; srcpos++)
            {
                for(int channel = 0; channel < m_pixelSize; channel++){
                    pel2[channel] = tmp[ contribX[x].p[srcpos].m_pixel * m_pixelSize + channel ];
                    if(pel2[channel] != pel[channel])
                        bPelDelta[channel] = TRUE;
                    weight[channel] += pel2[channel] * contribX[x].p[srcpos].m_weight;
                }
            }
            for(int channel = 0; channel < m_pixelSize; channel++){
                weight[channel] = bPelDelta[channel] ? static_cast<int>(qRound(weight[channel])) : pel[channel];
                int currentPos = (y*targetW+x) * m_pixelSize; // try to be at least a little efficient
                if (weight[channel]<0)
                    newData[currentPos + channel] = 0;
                else if (weight[channel]>255)
                    newData[currentPos + channel] = 255;
                else
                    newData[currentPos + channel] = (uchar)weight[channel];
             }
        } /* next dst row */
    } /* next dst column */

    // XXX: I'm thinking that we should be able to cancel earlier, in the look.
    if(!isCanceled()){
        m_dev -> writeBytes( newData, 0, 0, targetW, targetH);
        m_dev -> crop(0, 0, targetW, targetH);
    }

    /* free the memory allocated for horizontal filter weights */
    for(int x = 0; x < targetW; x++)
        delete[] contribX[x].p;
    delete[] contribX;

    delete[] newData;
    delete[] pel;
    delete[] pel2;
    delete[] tmp;
    delete[] weight;
    delete[] bPelDelta;

    if(m_sy < 1.0)
    {
        for(int i = 0; i < (int)(fwidth / m_sy * 2 + 1); i++)
        {
            delete[] tmpRowsMem[i];
        }
    }
    else
    {
        for(int i = 0; i < (int)(fwidth * 2 + 1); i++)
        {
            delete[] tmpRowsMem[i];
        }
    }

    QTime stoptime = QTime::currentTime ();
    return;
}

int KisScaleWorker::calcContrib(ContribList *contrib, double scale, double fwidth, int srcwidth, KisFilterStrategy* filterStrategy, Q_INT32 i)
{
        //ContribList* contribX: receiver of contrib info
        //double m_sx: horizontal zooming scale
        //double fwidth: Filter sampling width
        //int dstwidth: Target bitmap width
        //int srcwidth: Source bitmap width
        //double (*filterf)(double): Filter proc
        //int i: Pixel column in source bitmap being processed

        double width;
        double fscale;
        double center, begin, end;
        double weight;
        Q_INT32 k, n;

        if(scale < 1.0)
        {
                //Shrinking image
                width = fwidth / scale;
                fscale = 1.0 / scale;

                contrib->n = 0;
                contrib->p = new Contrib[ (int)(width * 2 + 1) ];

                center = (double) i / scale;
                begin = ceil(center - width);
                end = floor(center + width);
                for(int srcpos = (int)begin; srcpos <= end; ++srcpos)
                {
                        weight = center - (double) srcpos;
                        weight = filterStrategy->valueAt(weight / fscale) / fscale;
                        if(srcpos < 0)
                                n = -srcpos;
                        else if(srcpos >= srcwidth)
                                n = (srcwidth - srcpos) + srcwidth - 1;
                        else
                                n = srcpos;

                        k = contrib->n++;
                        contrib->p[k].m_pixel = n;
                        contrib->p[k].m_weight = weight;
                }
        }
        else
        {
                // Expanding image
                contrib->n = 0;
                contrib->p = new Contrib[ (int)(fwidth * 2 + 1) ];

                center = (double) i / scale;
                begin = ceil(center - fwidth);
                end = floor(center + fwidth);

                for(int srcpos = (int)begin; srcpos <= end; ++srcpos)
                {
                        weight = center - (double) srcpos;
                        weight = filterStrategy->valueAt(weight);
                        if(srcpos < 0) {
                                n = -srcpos;
                        } else if(srcpos >= srcwidth) {
                                n = (srcwidth - srcpos) + srcwidth - 1;
                        } else {
                                n = srcpos;
                        }
                        k = contrib->n++;
                        contrib->p[k].m_pixel = n;
                        contrib->p[k].m_weight = weight;
                }
        }
        return 0;
} /* calc_x_contrib */