chalk

kis_background.cpp
1/*
2 * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
3 *
4 * this program is free software; you can redistribute it and/or modify
5 * it under the terms of the gnu general public license as published by
6 * the free software foundation; either version 2 of the license, or
7 * (at your option) any later version.
8 *
9 * this program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * merchantability or fitness for a particular purpose. see the
12 * gnu general public license for more details.
13 *
14 * you should have received a copy of the gnu general public license
15 * along with this program; if not, write to the free software
16 * foundation, inc., 675 mass ave, cambridge, ma 02139, usa.
17 */
18#include "kis_global.h"
19#include "kis_background.h"
20#include "kis_integer_maths.h"
21
22KisBackground::KisBackground()
23 : TDEShared()
24{
25 m_patternTile = TQImage(PATTERN_WIDTH, PATTERN_HEIGHT, 32);
26 m_patternTile.setAlphaBuffer(false);
27
28 for (int y = 0; y < PATTERN_HEIGHT; y++)
29 {
30 for (int x = 0; x < PATTERN_WIDTH; x++)
31 {
32 TQ_UINT8 v = 128 + 63 * ((x / 16 + y / 16) % 2);
33 m_patternTile.setPixel(x, y, tqRgb(v, v, v));
34 }
35 }
36}
37
38KisBackground::~KisBackground()
39{
40}
41
42const TQImage& KisBackground::patternTile() const
43{
44 return m_patternTile;
45}
46
47void KisBackground::paintBackground(TQImage image, int imageLeftX, int imageTopY)
48{
49 int patternLeftX;
50
51 if (imageLeftX >= 0) {
52 patternLeftX = imageLeftX % PATTERN_WIDTH;
53 } else {
54 patternLeftX = (PATTERN_WIDTH - (-imageLeftX % PATTERN_WIDTH)) % PATTERN_WIDTH;
55 }
56
57 int patternTopY;
58
59 if (imageTopY >= 0) {
60 patternTopY = imageTopY % PATTERN_HEIGHT;
61 } else {
62 patternTopY = (PATTERN_HEIGHT - (-imageTopY % PATTERN_HEIGHT)) % PATTERN_HEIGHT;
63 }
64
65 int imageWidth = image.width();
66 int imageHeight = image.height();
67
68 int patternY = patternTopY;
69
70 for (int y = 0; y < imageHeight; y++)
71 {
72 TQRgb *imagePixelPtr = reinterpret_cast<TQRgb *>(image.scanLine(y));
73 const TQRgb *patternScanLine = reinterpret_cast<const TQRgb *>(m_patternTile.scanLine(patternY));
74 int patternX = patternLeftX;
75
76 for (int x = 0; x < imageWidth; x++)
77 {
78 TQRgb imagePixel = *imagePixelPtr;
79 TQ_UINT8 imagePixelAlpha = tqAlpha(imagePixel);
80
81 if (imagePixelAlpha != 255) {
82
83 TQRgb patternPixel = patternScanLine[patternX];
84 TQ_UINT8 imageRed = UINT8_BLEND(tqRed(imagePixel), tqRed(patternPixel), imagePixelAlpha);
85 TQ_UINT8 imageGreen = UINT8_BLEND(tqGreen(imagePixel), tqGreen(patternPixel), imagePixelAlpha);
86 TQ_UINT8 imageBlue = UINT8_BLEND(tqBlue(imagePixel), tqBlue(patternPixel), imagePixelAlpha);
87
88 *imagePixelPtr = tqRgba(imageRed, imageGreen, imageBlue, 255);
89 }
90
91 ++imagePixelPtr;
92 ++patternX;
93
94 if (patternX == PATTERN_WIDTH) {
95 patternX = 0;
96 }
97 }
98
99 ++patternY;
100
101 if (patternY == PATTERN_HEIGHT) {
102 patternY = 0;
103 }
104 }
105}
106
107void KisBackground::paintBackground(TQImage img, const TQRect& scaledImageRect, const TQSize& scaledImageSize, const TQSize& imageSize)
108{
109 if (scaledImageRect.isEmpty() || scaledImageSize.isEmpty() || imageSize.isEmpty()) {
110 return;
111 }
112
113 Q_ASSERT(img.size() == scaledImageRect.size());
114
115 if (img.size() != scaledImageRect.size()) {
116 return;
117 }
118
119 TQ_INT32 imageWidth = imageSize.width();
120 TQ_INT32 imageHeight = imageSize.height();
121
122 for (TQ_INT32 y = 0; y < scaledImageRect.height(); ++y) {
123
124 TQ_INT32 scaledY = scaledImageRect.y() + y;
125 TQ_INT32 srcY = (scaledY * imageHeight) / scaledImageSize.height();
126 TQ_INT32 patternY = srcY % PATTERN_HEIGHT;
127
128 TQRgb *imagePixelPtr = reinterpret_cast<TQRgb *>(img.scanLine(y));
129 const TQRgb *patternScanLine = reinterpret_cast<const TQRgb *>(m_patternTile.scanLine(patternY));
130
131 for (TQ_INT32 x = 0; x < scaledImageRect.width(); ++x) {
132
133 TQRgb imagePixel = *imagePixelPtr;
134 TQ_UINT8 imagePixelAlpha = tqAlpha(imagePixel);
135
136 if (imagePixelAlpha != 255) {
137
138 TQ_INT32 scaledX = scaledImageRect.x() + x;
139 TQ_INT32 srcX = (scaledX * imageWidth) / scaledImageSize.width();
140 TQ_INT32 patternX = srcX % PATTERN_WIDTH;
141
142 TQRgb patternPixel = patternScanLine[patternX];
143 TQ_UINT8 imageRed = UINT8_BLEND(tqRed(imagePixel), tqRed(patternPixel), imagePixelAlpha);
144 TQ_UINT8 imageGreen = UINT8_BLEND(tqGreen(imagePixel), tqGreen(patternPixel), imagePixelAlpha);
145 TQ_UINT8 imageBlue = UINT8_BLEND(tqBlue(imagePixel), tqBlue(patternPixel), imagePixelAlpha);
146
147 *imagePixelPtr = tqRgba(imageRed, imageGreen, imageBlue, 255);
148 }
149
150 ++imagePixelPtr;
151 }
152 }
153}
154
155