summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_background.cc
blob: 506d49e8e49234de276c649edc75b3b59df183cd (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
/*
 *  Copyright (c) 2002 Patrick Julien <[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., 675 mass ave, cambridge, ma 02139, usa.
 */
#include "kis_global.h"
#include "kis_background.h"
#include "kis_integer_maths.h"

KisBackground::KisBackground()
    : TDEShared()
{
    m_patternTile = TQImage(PATTERN_WIDTH, PATTERN_HEIGHT, 32);
    m_patternTile.setAlphaBuffer(false);

    for (int y = 0; y < PATTERN_HEIGHT; y++)
    {
        for (int x = 0; x < PATTERN_WIDTH; x++)
        {
            TQ_UINT8 v = 128 + 63 * ((x / 16 + y / 16) % 2);
            m_patternTile.setPixel(x, y, tqRgb(v, v, v));
        }
    }
}

KisBackground::~KisBackground()
{
}

const TQImage& KisBackground::patternTile() const
{
    return m_patternTile;
}

void KisBackground::paintBackground(TQImage image, int imageLeftX, int imageTopY)
{
    int patternLeftX;

    if (imageLeftX >= 0) {
        patternLeftX = imageLeftX % PATTERN_WIDTH;
    } else {
        patternLeftX = (PATTERN_WIDTH - (-imageLeftX % PATTERN_WIDTH)) % PATTERN_WIDTH;
    }

    int patternTopY;

    if (imageTopY >= 0) {
        patternTopY = imageTopY % PATTERN_HEIGHT;
    } else {
        patternTopY = (PATTERN_HEIGHT - (-imageTopY % PATTERN_HEIGHT)) % PATTERN_HEIGHT;
    }

    int imageWidth = image.width();
    int imageHeight = image.height();

    int patternY = patternTopY;

    for (int y = 0; y < imageHeight; y++)
    {
        TQRgb *imagePixelPtr = reinterpret_cast<TQRgb *>(image.scanLine(y));
        const TQRgb *patternScanLine = reinterpret_cast<const TQRgb *>(m_patternTile.scanLine(patternY));
        int patternX = patternLeftX;

        for (int x = 0; x < imageWidth; x++)
        {
            TQRgb imagePixel = *imagePixelPtr;
            TQ_UINT8 imagePixelAlpha = tqAlpha(imagePixel);

            if (imagePixelAlpha != 255) {

                TQRgb patternPixel = patternScanLine[patternX];
                TQ_UINT8 imageRed = UINT8_BLEND(tqRed(imagePixel), tqRed(patternPixel), imagePixelAlpha);
                TQ_UINT8 imageGreen = UINT8_BLEND(tqGreen(imagePixel), tqGreen(patternPixel), imagePixelAlpha);
                TQ_UINT8 imageBlue = UINT8_BLEND(tqBlue(imagePixel), tqBlue(patternPixel), imagePixelAlpha);

                *imagePixelPtr = tqRgba(imageRed, imageGreen, imageBlue, 255);
            }

            ++imagePixelPtr;
            ++patternX;

            if (patternX == PATTERN_WIDTH) {
                patternX = 0;
            }
        }

        ++patternY;

        if (patternY == PATTERN_HEIGHT) {
            patternY = 0;
        }
    }
}

void KisBackground::paintBackground(TQImage img, const TQRect& scaledImageRect, const TQSize& scaledImageSize, const TQSize& imageSize)
{
    if (scaledImageRect.isEmpty() || scaledImageSize.isEmpty() || imageSize.isEmpty()) {
        return;
    }

    Q_ASSERT(img.size() == scaledImageRect.size());

    if (img.size() != scaledImageRect.size()) {
        return;
    }

    TQ_INT32 imageWidth = imageSize.width();
    TQ_INT32 imageHeight = imageSize.height();

    for (TQ_INT32 y = 0; y < scaledImageRect.height(); ++y) {

        TQ_INT32 scaledY = scaledImageRect.y() + y;
        TQ_INT32 srcY = (scaledY * imageHeight) / scaledImageSize.height();
        TQ_INT32 patternY = srcY % PATTERN_HEIGHT;

        TQRgb *imagePixelPtr = reinterpret_cast<TQRgb *>(img.scanLine(y));
        const TQRgb *patternScanLine = reinterpret_cast<const TQRgb *>(m_patternTile.scanLine(patternY));

        for (TQ_INT32 x = 0; x < scaledImageRect.width(); ++x) {

            TQRgb imagePixel = *imagePixelPtr;
            TQ_UINT8 imagePixelAlpha = tqAlpha(imagePixel);

            if (imagePixelAlpha != 255) {

                TQ_INT32 scaledX = scaledImageRect.x() + x;
                TQ_INT32 srcX = (scaledX * imageWidth) / scaledImageSize.width();
                TQ_INT32 patternX = srcX % PATTERN_WIDTH;

                TQRgb patternPixel = patternScanLine[patternX];
                TQ_UINT8 imageRed = UINT8_BLEND(tqRed(imagePixel), tqRed(patternPixel), imagePixelAlpha);
                TQ_UINT8 imageGreen = UINT8_BLEND(tqGreen(imagePixel), tqGreen(patternPixel), imagePixelAlpha);
                TQ_UINT8 imageBlue = UINT8_BLEND(tqBlue(imagePixel), tqBlue(patternPixel), imagePixelAlpha);

                *imagePixelPtr = tqRgba(imageRed, imageGreen, imageBlue, 255);
            }

            ++imagePixelPtr;
        }
    }
}