summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_scale_visitor.cpp
blob: a06d674d1e9236b97b5c620f5413e40553d16dc5 (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 <[email protected]>
 *
 *  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 <tqdatetime.h>

#include <kdebug.h>
#include <tdelocale.h>

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


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

    TQRect rect = m_dev -> exactBounds();
    TQ_INT32 width = rect.width();
    TQ_INT32 height =  rect.height();
    m_pixelSize=m_dev -> pixelSize();

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

    TQ_UINT8* newData = new TQ_UINT8[targetW * targetH * m_pixelSize ];
    TQ_CHECK_PTR(newData);

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

    TQ_UINT8* pel = new TQ_UINT8[ m_pixelSize ];
    TQ_CHECK_PTR(pel);

    TQ_UINT8 *pel2 = new TQ_UINT8[ m_pixelSize ];
    TQ_CHECK_PTR(pel2);

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


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

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

    //create array of pointers to intermediate rows that are actually used simultaneously and allocate memory for the rows
    TQ_UINT8 **tmpRowsMem;
    if(m_sy < 1.0)
    {
        tmpRowsMem = new TQ_UINT8*[ (int)(fwidth / m_sy * 2 + 1) ];
        for(int i = 0; i < (int)(fwidth / m_sy * 2 + 1); i++)
        {
             tmpRowsMem[i] = new TQ_UINT8[ width * m_pixelSize ];
             TQ_CHECK_PTR(tmpRowsMem[i]);
        }
    }
    else
    {
        tmpRowsMem = new TQ_UINT8*[ (int)(fwidth * 2 + 1) ];
        for(int i = 0; i < (int)(fwidth * 2 + 1); i++)
        {
            tmpRowsMem[i] = new TQ_UINT8[ width * m_pixelSize ];
            TQ_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);
    }

    TQTime starttime = TQTime::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 TQ_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>(tqRound(weight[channel])) : pel[channel];
                        tmp[ x * m_pixelSize + channel ] = static_cast<TQ_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>(tqRound(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];
        }
    }

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

int KisScaleWorker::calcContrib(ContribList *contrib, double scale, double fwidth, int srcwidth, KisFilterStrategy* filterStrategy, TQ_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;
        TQ_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 */