summaryrefslogtreecommitdiffstats
path: root/digikam/imageplugins/border/border.cpp
blob: ab9de5747cdff8f48a23985d388a2fe5ef6fd537 (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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-05-25
 * Description : border threaded image filter.
 * 
 * Copyright 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright 2006-2007 by Marcel Wiesweg <marcel dot wiesweg at gmx dot 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, 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.
 * 
 * ============================================================ */

// C++ includes.

#include <cmath>
#include <cstdlib>

// Qt includes.

#include <qpoint.h>
#include <qregion.h>
#include <qpointarray.h>

// Local includes.

#include "dimg.h"
#include "ddebug.h"
#include "border.h"

namespace DigikamBorderImagesPlugin
{

Border::Border(Digikam::DImg *image, QObject *parent, int orgWidth, int orgHeight,
               QString borderPath, int borderType, float borderPercent,
               Digikam::DColor solidColor, 
               Digikam::DColor niepceBorderColor,
               Digikam::DColor niepceLineColor, 
               Digikam::DColor bevelUpperLeftColor,
               Digikam::DColor bevelLowerRightColor, 
               Digikam::DColor decorativeFirstColor,
               Digikam::DColor decorativeSecondColor)
      : Digikam::DImgThreadedFilter(image, parent, "Border")
{ 
    m_orgWidth        = orgWidth;
    m_orgHeight       = orgHeight;
    m_orgRatio        = (float)m_orgWidth / (float)m_orgHeight;
    m_borderType      = borderType;
    m_borderPath      = borderPath;
    int size          = (image->width() > image->height()) ? image->height() : image->width();
    m_borderMainWidth = (int)(size * borderPercent);
    m_border2ndWidth  = (int)(size * 0.005);
    
    // Clamp internal border with to 1 pixel to be visible with small image.    
    if (m_border2ndWidth < 1) m_border2ndWidth = 1; 
    
    m_solidColor            = solidColor;
    m_niepceBorderColor     = niepceBorderColor;
    m_niepceLineColor       = niepceLineColor;
    m_bevelUpperLeftColor   = bevelUpperLeftColor;
    m_bevelLowerRightColor  = bevelLowerRightColor;
    m_decorativeFirstColor  = decorativeFirstColor;
    m_decorativeSecondColor = decorativeSecondColor;

    m_preserveAspectRatio   = true;    

    initFilter();
}

Border::Border(Digikam::DImg *orgImage, QObject *parent, int orgWidth, int orgHeight,
               QString borderPath, int borderType, 
               int borderWidth1, int borderWidth2, int borderWidth3, int borderWidth4,
               Digikam::DColor solidColor, 
               Digikam::DColor niepceBorderColor,
               Digikam::DColor niepceLineColor, 
               Digikam::DColor bevelUpperLeftColor,
               Digikam::DColor bevelLowerRightColor, 
               Digikam::DColor decorativeFirstColor,
               Digikam::DColor decorativeSecondColor)
      : Digikam::DImgThreadedFilter(orgImage, parent, "Border")
{ 
    m_orgWidth              = orgWidth;
    m_orgHeight             = orgHeight;

    m_borderType            = borderType;
    m_borderWidth1          = borderWidth1;
    m_borderWidth2          = borderWidth2;
    m_borderWidth3          = borderWidth3;
    m_borderWidth4          = borderWidth4;
    
    m_solidColor            = solidColor;
    m_niepceBorderColor     = niepceBorderColor;
    m_niepceLineColor       = niepceLineColor;
    m_bevelUpperLeftColor   = bevelUpperLeftColor;
    m_bevelLowerRightColor  = bevelLowerRightColor;
    m_decorativeFirstColor  = decorativeFirstColor;
    m_decorativeSecondColor = decorativeSecondColor;
    
    m_borderPath            = borderPath;

    m_preserveAspectRatio   = false;    
    
    initFilter();
}

void Border::filterImage(void)
{
    switch (m_borderType)
    {
        case SolidBorder:
            if (m_preserveAspectRatio)
                solid(m_orgImage, m_destImage, m_solidColor, m_borderMainWidth);
            else
                solid2(m_orgImage, m_destImage, m_solidColor, m_borderWidth1);
            break;
        
        case NiepceBorder:
            if (m_preserveAspectRatio)
                niepce(m_orgImage, m_destImage, m_niepceBorderColor, m_borderMainWidth, 
                       m_niepceLineColor, m_border2ndWidth);
            else
                niepce2(m_orgImage, m_destImage, m_niepceBorderColor, m_borderWidth1, 
                        m_niepceLineColor, m_borderWidth4);
            break;
    
        case BeveledBorder:
            if (m_preserveAspectRatio)
                bevel(m_orgImage, m_destImage, m_bevelUpperLeftColor,
                      m_bevelLowerRightColor, m_borderMainWidth);
            else
                bevel2(m_orgImage, m_destImage, m_bevelUpperLeftColor,
                       m_bevelLowerRightColor, m_borderWidth1);
            break;
    
        case PineBorder:
        case WoodBorder: 
        case PaperBorder: 
        case ParqueBorder: 
        case IceBorder:
        case LeafBorder: 
        case MarbleBorder:
        case RainBorder:
        case CratersBorder:
        case DriedBorder:
        case PinkBorder:
        case StoneBorder:
        case ChalkBorder:
        case GraniteBorder:
        case RockBorder:
        case WallBorder:
            if (m_preserveAspectRatio)
                pattern(m_orgImage, m_destImage, m_borderMainWidth, 
                        m_decorativeFirstColor, m_decorativeSecondColor,
                        m_border2ndWidth, m_border2ndWidth);
            else
                pattern2(m_orgImage, m_destImage, m_borderWidth1, 
                         m_decorativeFirstColor, m_decorativeSecondColor,
                         m_borderWidth2, m_borderWidth2);
            break;
    }
}

// -- Methods to preserve aspect ratio of image ------------------------------------------

void Border::solid(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &fg, int borderWidth)
{
    if (m_orgWidth > m_orgHeight)
    {
        int height = src.height() + borderWidth*2;
        dest = Digikam::DImg((int)(height*m_orgRatio), height, src.sixteenBit(), src.hasAlpha());
        dest.fill(fg);
        dest.bitBltImage(&src, (dest.width()-src.width())/2, borderWidth);
    }
    else
    {
        int width = src.width() + borderWidth*2;
        dest = Digikam::DImg(width, (int)(width/m_orgRatio), src.sixteenBit(), src.hasAlpha());
        dest.fill(fg);
        dest.bitBltImage(&src, borderWidth, (dest.height()-src.height())/2);
    }
}

void Border::niepce(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &fg, 
                    int borderWidth, const Digikam::DColor &bg, int lineWidth)
{
    Digikam::DImg tmp;
    solid(src, tmp, bg, lineWidth);
    solid(tmp, dest, fg, borderWidth);
}

void Border::bevel(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &topColor, 
                   const Digikam::DColor &btmColor, int borderWidth)
{
    int width, height;

    if (m_orgWidth > m_orgHeight)
    {
        height = src.height() + borderWidth*2;
        width  = (int)(height*m_orgRatio);
    }
    else
    {
        width  = src.width() + borderWidth*2;
        height = (int)(width/m_orgRatio);
    }

    dest = Digikam::DImg(width, height, src.sixteenBit(), src.hasAlpha());
    dest.fill(topColor);

    QPointArray btTriangle(3);
    btTriangle.setPoint(0, width, 0);
    btTriangle.setPoint(1, 0, height);
    btTriangle.setPoint(2, width, height);
    QRegion btRegion(btTriangle);

    for(int x=0 ; x < width ; x++)
    {
        for(int y=0 ; y < height ; y++)
        {
            if (btRegion.contains(QPoint(x, y)))
                dest.setPixelColor(x, y, btmColor);
        }
    }

    if (m_orgWidth > m_orgHeight)
    {
        dest.bitBltImage(&src, (dest.width()-src.width())/2, borderWidth);
    }
    else
    {
        dest.bitBltImage(&src, borderWidth, (dest.height()-src.height())/2);
    }
}

void Border::pattern(Digikam::DImg &src, Digikam::DImg &dest, int borderWidth,
                     const Digikam::DColor &firstColor, const Digikam::DColor &secondColor, 
                     int firstWidth, int secondWidth)
{
    // Original image with the first solid border around.
    Digikam::DImg tmp; 
    solid(src, tmp, firstColor, firstWidth);
  
    // Border tiled image using pattern with second solid border around.
    int width, height;

    if (m_orgWidth > m_orgHeight)
    {
        height = tmp.height() + borderWidth*2;
        width  = (int)(height*m_orgRatio);
    }
    else
    {
        width  = tmp.width() + borderWidth*2;
        height = (int)(width/m_orgRatio);
    }

    Digikam::DImg tmp2(width, height, tmp.sixteenBit(), tmp.hasAlpha());
    DDebug() << "Border File:" << m_borderPath << endl;
    Digikam::DImg border(m_borderPath);
    if ( border.isNull() )
        return;
    
    border.convertToDepthOfImage(&tmp2);

    for (int x = 0 ; x < width ; x+=border.width())
        for (int y = 0 ; y < height ; y+=border.height())
            tmp2.bitBltImage(&border, x, y);
      
    solid(tmp2, dest, secondColor, secondWidth);

    // Merge both images to one.
    if (m_orgWidth > m_orgHeight)
    {
        dest.bitBltImage(&tmp, (dest.width()-tmp.width())/2, borderWidth);
    }
    else
    {
        dest.bitBltImage(&tmp, borderWidth, (dest.height()-tmp.height())/2);
    }
}

// -- Methods to not-preserve aspect ratio of image ------------------------------------------


void Border::solid2(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &fg, int borderWidth)
{
    dest = Digikam::DImg(src.width() + borderWidth*2, src.height() + borderWidth*2, 
                         src.sixteenBit(), src.hasAlpha());
    dest.fill(fg);
    dest.bitBltImage(&src, borderWidth, borderWidth);
}

void Border::niepce2(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &fg, int borderWidth, 
                     const Digikam::DColor &bg, int lineWidth)
{
    Digikam::DImg tmp;
    solid2(src, tmp, bg, lineWidth);
    solid2(tmp, dest, fg, borderWidth);
}

void Border::bevel2(Digikam::DImg &src, Digikam::DImg &dest, const Digikam::DColor &topColor, 
                    const Digikam::DColor &btmColor, int borderWidth)
{
    int x, y;
    int wc;

    dest = Digikam::DImg(src.width() + borderWidth*2, 
                         src.height() + borderWidth*2, 
                         src.sixteenBit(), src.hasAlpha());

    // top

    for(y=0, wc = (int)dest.width()-1; y < borderWidth; ++y, --wc)
    {
        for(x=0; x < wc; ++x)
            dest.setPixelColor(x, y, topColor);

        for(;x < (int)dest.width(); ++x)
            dest.setPixelColor(x, y, btmColor);
    }

    // left and right

    for(; y < (int)dest.height()-borderWidth; ++y)
    {
       for(x=0; x < borderWidth; ++x)
           dest.setPixelColor(x, y, topColor);

       for(x = (int)dest.width()-1; x > (int)dest.width()-borderWidth-1; --x)
           dest.setPixelColor(x, y, btmColor);
    }

    // bottom

    for(wc = borderWidth; y < (int)dest.height(); ++y, --wc)
    {
       for(x=0; x < wc; ++x)
           dest.setPixelColor(x, y, topColor);

       for(; x < (int)dest.width(); ++x)
           dest.setPixelColor(x, y, btmColor);
    }

    dest.bitBltImage(&src, borderWidth, borderWidth);
}

void Border::pattern2(Digikam::DImg &src, Digikam::DImg &dest, int borderWidth,
                      const Digikam::DColor &firstColor, const Digikam::DColor &secondColor, 
                      int firstWidth, int secondWidth)
{
    // Border tile.

    int w = m_orgWidth + borderWidth*2;
    int h = m_orgHeight + borderWidth*2;

    DDebug() << "Border File:" << m_borderPath << endl;
    Digikam::DImg border(m_borderPath);
    if ( border.isNull() )
        return;

    Digikam::DImg borderImg(w, h, src.sixteenBit(), src.hasAlpha());
    border.convertToDepthOfImage(&borderImg);

    for (int x = 0 ; x < w ; x+=border.width())
        for (int y = 0 ; y < h ; y+=border.height())
            borderImg.bitBltImage(&border, x, y);

    // First line around the pattern tile.
    Digikam::DImg tmp = borderImg.smoothScale(src.width() + borderWidth*2,
                                              src.height() + borderWidth*2 );

    solid2(tmp, dest, firstColor, firstWidth);

    // Second line around original image.
    tmp.reset();
    solid2(src, tmp, secondColor, secondWidth);

    // Copy original image.
    dest.bitBltImage(&tmp, borderWidth, borderWidth);
}

}  // NameSpace DigikamBorderImagesPlugin