~ubuntu-branches/ubuntu/trusty/digikam/trusty

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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** ===========================================================
 * @file
 *
 * This file is a part of digiKam project
 * <a href="http://www.digikam.org">http://www.digikam.org</a>
 *
 * @date   2012-06-01
 * @brief  Transitions, AspectRatioCorrection and otherImageEffects
 *
 * @author Copyright (C) 2012 by A Janardhan Reddy <annapareddyjanardhanreddy at gmail dot com>
 *         Copyright (C) 2012 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.
 *
 * ============================================================ */

#include "processimage.moc"

// C++ includes

#include <cmath>

// Local includes

#include "magickiface.h"

#define INC(v) incValue(v, step, steps)
#define DEC(v) decValue(v, step, steps)

namespace KIPIPlugins
{

ProcessImage::ProcessImage(MagickApi* const api)
{
    m_api = api;
}

MagickImage* ProcessImage::aspectRatioCorrection(MagickImage& img, double aspectratio, ASPECTCORRECTION_TYPE aspectcorrection)
{
    MagickImage* newimg    = 0;
    double img_aspectratio = (double) img.getWidth() / (double) img.getHeight();

    // tolerate some error in double value
    if(abs(img_aspectratio - aspectratio) <= 0.001)
        return &img;

    if (aspectcorrection == ASPECTCORRECTION_TYPE_AUTO)
    {
        // find out whether we should use fitin, fillin or none
        if (img_aspectratio > 1.0)
            aspectcorrection = ASPECTCORRECTION_TYPE_FILLIN;
        else
            aspectcorrection = ASPECTCORRECTION_TYPE_FITIN;
    }

    switch (aspectcorrection)
    {
        case ASPECTCORRECTION_TYPE_FITIN:
            //fit in with black bars
            if (img_aspectratio < aspectratio)
            {
                // resulting image will have black bars on right and left side
                if (!(newimg = m_api->createImage("black", img.getHeight() * aspectratio,img.getHeight())))
                    Q_EMIT signalProcessError("couldn't create image\n");

                m_api->overlayImage(*newimg, (newimg->getWidth() - img.getWidth()) / 2,0, img);
            }
            else
            {
                // resulting image will have black bars on top and bottom
                if (!(newimg = m_api->createImage("black", img.getHeight() * aspectratio, img.getHeight())))
                    Q_EMIT signalProcessError("couldn't create image\n");

                m_api->overlayImage(*newimg, 0, (newimg->getHeight() - img.getHeight()) / 2, img);
            }
            break;

        case ASPECTCORRECTION_TYPE_FILLIN:
            // part of image is cut off
            if (img_aspectratio < aspectratio)
            {
                // cut on top and bottom side
                if (!(newimg = m_api->createImage("black", img.getHeight() * aspectratio, img.getHeight())))
                    Q_EMIT signalProcessError("couldn't create image\n");

                m_api->bitblitImage(*newimg, 0, 0, img, 0, (img.getHeight() - newimg->getHeight()) / 2, newimg->getWidth(), newimg->getHeight());
            }
            else
            {
                // cut on right and left side
                if (!(newimg = m_api->createImage("black", img.getHeight() * aspectratio, img.getHeight())))
                    Q_EMIT signalProcessError("couldn't create image\n");

                m_api->bitblitImage(*newimg, 0, 0, img,(img.getWidth() - newimg->getWidth()) / 2,0, newimg->getWidth(), newimg->getHeight());
            }
            break;

        case ASPECTCORRECTION_TYPE_NONE:
        default:
            // no correction needed
            break;
    }

    if (newimg)
    {
        // swap the images if we got a new one
        m_api->freeImage(img);
        img = *newimg;
    }
    return &img;
}

int ProcessImage::incValue(int v, int step, int steps) const
{
    return (v) * (step + 1) / steps;
}

int ProcessImage::decValue(int v, int step, int steps) const
{
    return (v) * (steps - step - 1) / steps;
}

MagickImage* ProcessImage::transition(const MagickImage& from, const MagickImage& to, int type, int step, int steps)
{
    int w, h;

    if (step < 0 || step >= steps)
        Q_EMIT signalProcessError(QString("step: %1 is out of range (%2)").arg(step).arg(steps));

    // create a new target image and copy the from image onto
    MagickImage* const dst = m_api->createImage("black", w = from.getWidth(), h = from.getHeight());

    switch (type)
    {
        // sliding

        case TRANSITION_TYPE_SLIDE_L2R:
            m_api->overlayImage(*dst, 0,       0, from);
            m_api->overlayImage(*dst, DEC(-w), 0, to);
            break;

        case TRANSITION_TYPE_SLIDE_R2L:
            m_api->overlayImage(*dst, 0,      0, from);
            m_api->overlayImage(*dst, DEC(w), 0, to);
            break;

        case TRANSITION_TYPE_SLIDE_T2B:
            m_api->overlayImage(*dst, 0, 0,       from);
            m_api->overlayImage(*dst, 0, DEC(-h), to);
            break;

        case TRANSITION_TYPE_SLIDE_B2T:
            m_api->overlayImage(*dst, 0, 0,      from);
            m_api->overlayImage(*dst, 0, DEC(h), to);
            break;

        // pushing

        case TRANSITION_TYPE_PUSH_L2R:
            m_api->overlayImage(*dst, INC(w),  0, from);
            m_api->overlayImage(*dst, DEC(-w), 0, to);
            break;

        case TRANSITION_TYPE_PUSH_R2L:
            m_api->overlayImage(*dst, INC(-w), 0, from);
            m_api->overlayImage(*dst, DEC(w),  0, to);
            break;

        case TRANSITION_TYPE_PUSH_T2B:
            m_api->overlayImage(*dst, 0, INC(h),  from);
            m_api->overlayImage(*dst, 0, DEC(-h), to);
            break;

        case TRANSITION_TYPE_PUSH_B2T:
            m_api->overlayImage(*dst, 0, INC(-h), from);
            m_api->overlayImage(*dst, 0, DEC(h),  to);
            break;

        // swapping

        case TRANSITION_TYPE_SWAP_L2R:
            if (step < steps / 2)
            {
                m_api->overlayImage(*dst, INC(w),  0, to);
                m_api->overlayImage(*dst, INC(-w), 0, from);
            }
            else
            {
                m_api->overlayImage(*dst, DEC(-w), 0, from);
                m_api->overlayImage(*dst, DEC(w),  0, to);
            }
            break;

        case TRANSITION_TYPE_SWAP_R2L:
            if (step < steps / 2)
            {
                m_api->overlayImage(*dst, INC(-w), 0, to);
                m_api->overlayImage(*dst, INC(w),  0, from);
            }
            else
            {
                m_api->overlayImage(*dst, DEC(w),  0, from);
                m_api->overlayImage(*dst, DEC(-w), 0, to);
            }
            break;

        case TRANSITION_TYPE_SWAP_T2B:
            if (step < steps / 2)
            {
                m_api->overlayImage(*dst, 0, INC(h),  to);
                m_api->overlayImage(*dst, 0, INC(-h), from);
            }
            else
            {
                m_api->overlayImage(*dst, 0, DEC(-h), from);
                m_api->overlayImage(*dst, 0, DEC(h),  to);
            }
            break;

        case TRANSITION_TYPE_SWAP_B2T:
            if (step < steps / 2)
            {
                m_api->overlayImage(*dst, 0, INC(-h), to);
                m_api->overlayImage(*dst, 0, INC(h),  from);
            }
            else
            {
                m_api->overlayImage(*dst, 0, DEC(h),  from);
                m_api->overlayImage(*dst, 0, DEC(-h), to);
            }
            break;

        // rolling

        case TRANSITION_TYPE_ROLL_L2R:
            if (INC(w))
                m_api->scaleblitImage(*dst, 0,      0, INC(w), h, to,   0, 0, w, h);
            if (DEC(w))
                m_api->scaleblitImage(*dst, INC(w), 0, DEC(w), h, from, 0, 0, w, h);
            break;

        case TRANSITION_TYPE_ROLL_R2L:
            if (DEC(w))
                m_api->scaleblitImage(*dst, 0,      0, DEC(w), h, from, 0, 0, w, h);
            if (INC(w))
                m_api->scaleblitImage(*dst, DEC(w), 0, INC(w), h, to,   0, 0, w, h);
            break;

        case TRANSITION_TYPE_ROLL_T2B:
            if (INC(h))
                m_api->scaleblitImage(*dst, 0, 0,      w, INC(h), to,   0, 0, w, h);
            if (DEC(h))
                m_api->scaleblitImage(*dst, 0, INC(h), w, DEC(h), from, 0, 0, w, h);
            break;

        case TRANSITION_TYPE_ROLL_B2T:
            if (DEC(h))
                m_api->scaleblitImage(*dst, 0, 0,      w, DEC(h), from, 0, 0, w, h);
            if (INC(h))
                m_api->scaleblitImage(*dst, 0, DEC(h), w, INC(h), to,   0, 0, w, h);
            break;

        // fade

        case TRANSITION_TYPE_FADE:
        default:
            m_api->blendImage(*dst, from, to, 1.0 / steps * step);
            break;
    }

    return dst;
}

GeoImage* ProcessImage::getGeometry(const GeoImage& from, const GeoImage& to, int image_width, int image_height, 
                                    int step, int steps) const
{
    GeoImage* const geometry = new GeoImage();
    // compute the dimesions in current step
    steps--;

    if (step <= 0)
    {
        geometry->x = lround(from.x);
        geometry->y = lround(from.y);
        geometry->w = lround(from.w);
        geometry->h = lround(from.h);
    }
    else if (step >= steps)
    {
        geometry->x = lround(to.x);
        geometry->y = lround(to.y);
        geometry->w = lround(to.w);
        geometry->h = lround(to.h);
    }
    else
    {
        geometry->x = lround(from.x + (to.x - from.x) * (double) step / (double) steps);
        geometry->y = lround(from.y + (to.y - from.y) * (double) step / (double) steps);
        geometry->w = lround(from.w + (to.w - from.w) * (double) step / (double) steps);
        geometry->h = lround(from.h + (to.h - from.h) * (double) step / (double) steps);
    }

    geometry->x = qMin(qMax(geometry->x, 0), image_width  - 1);
    geometry->y = qMin(qMax(geometry->y, 0), image_height - 1);
    geometry->w = qMin(qMax(geometry->w, 0), image_width  - 1 - geometry->x);
    geometry->h = qMin(qMax(geometry->h, 0), image_height - 1 - geometry->y);

    return geometry;
}

} // namespace KIPIPlugins