summaryrefslogtreecommitdiffstats
path: root/kdesktop/kshadowengine.cpp
blob: b90bf3b52780efde4b58f291856a1d4955c64a09 (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
/* This file is proposed to be part of the KDE libraries.
 * Copyright (C) 2003 Laur Ivan <laurivan@eircom.net>
 *
 * Many thanks to:
 *  - Bernardo Hung <deciare@gta.igs.net> for the enhanced shadow
 *    algorithm (currently used)
 *  - Tim Jansen <tim@tjansen.de> for the API updates and fixes.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kshadowengine.h"
#include <tqcolor.h>
#include "kshadowsettings.h"

KShadowEngine::KShadowEngine() :
	m_shadowSettings( new KShadowSettings )
{
}

KShadowEngine::~KShadowEngine()
{
  delete m_shadowSettings;
}

KShadowEngine::KShadowEngine(KShadowSettings *fx) :
	m_shadowSettings(0L)
{
  setShadowSettings(fx);
}


void KShadowEngine::setShadowSettings(KShadowSettings *fx)
{
  delete m_shadowSettings;

  m_shadowSettings = fx;
}

KShadowSettings *KShadowEngine::shadowSettings()
{
  return m_shadowSettings;
}

TQImage KShadowEngine::makeShadow(const TQPixmap& textPixmap, const TQColor &bgColor)
{
  TQImage result;

  // create a new image for for the shaddow
  int w = textPixmap.width();
  int h = textPixmap.height();

  // avoid calling these methods for every pixel
  int bgRed = bgColor.red();
  int bgGreen = bgColor.green();
  int bgBlue = bgColor.blue();

  int thick = m_shadowSettings->thickness() >> 1;

  double alphaShadow;

  /*
   *	This is the source pixmap
   */
  TQImage img = textPixmap.convertToImage().convertDepth(32);

  /*
   *	Resize the image if necessary
   */
  if ((result.width() != w) || (result.height() != h))
  {
    result.create(w, h, 32);
  }

  result.setAlphaBuffer(true);
  result.fill(0); // all black

  for (int i = thick; i < w - thick; i++)
  {
    for (int j = thick; j < h - thick; j++)
    {
      switch (m_shadowSettings->algorithm())
      {
      case KShadowSettings::DoubleLinearDecay:
	alphaShadow = doubleLinearDecay(img, i, j);
	break;
      case KShadowSettings::RadialDecay:
	alphaShadow = radialDecay(img, i, j);
	break;
      case KShadowSettings::NoDecay:
	alphaShadow = noDecay(img, i, j);
	break;
      case KShadowSettings::DefaultDecay:
      default:
	alphaShadow = defaultDecay(img, i, j);
      }

      alphaShadow = (alphaShadow > m_shadowSettings->maxOpacity()) ? m_shadowSettings->maxOpacity() : alphaShadow;

      // update the shadow's i,j pixel.
      result.setPixel(i,j, tqRgba(bgRed, bgGreen , bgBlue, (int) alphaShadow));
    }
  }
  return result;
}

// Multiplication factor for pixels directly above, under, or next to the text
#define AXIS_FACTOR 2.0
// Multiplication factor for pixels diagonal to the text
#define DIAGONAL_FACTOR 1.0

double KShadowEngine::defaultDecay(TQImage& source, int i, int j)
{
  if ((i < 1) || (j < 1) || (i > source.width() - 2) || (j > source.height() - 2))
    return 0;

  double alphaShadow;
  alphaShadow =(tqGray(source.pixel(i-1,j-1)) * DIAGONAL_FACTOR +
		tqGray(source.pixel(i-1,j  )) * AXIS_FACTOR +
		tqGray(source.pixel(i-1,j+1)) * DIAGONAL_FACTOR +
		tqGray(source.pixel(i  ,j-1)) * AXIS_FACTOR +
		0                         +
		tqGray(source.pixel(i  ,j+1)) * AXIS_FACTOR +
		tqGray(source.pixel(i+1,j-1)) * DIAGONAL_FACTOR +
		tqGray(source.pixel(i+1,j  )) * AXIS_FACTOR +
		tqGray(source.pixel(i+1,j+1)) * DIAGONAL_FACTOR) / m_shadowSettings->multiplicationFactor();

  return alphaShadow;
}

double KShadowEngine::doubleLinearDecay(TQImage& source, int i, int j)
{
  //printf("img: %p, %d %d\n", (char *) &source, i, j);
  return defaultDecay( source, i, j ); // for now
}

double KShadowEngine::radialDecay(TQImage& source, int i, int j)
{
  //printf("img: %p, %d %d\n", (char *) &source, i, j);
  return defaultDecay( source, i, j ); // for now
}

double KShadowEngine::noDecay(TQImage& source, int i, int j)
{
  // create a new image for for the shaddow
  int w = source.width();
  int h = source.height();
  int sx, sy;
  //int thick = m_shadowSettings->thickness() >> 1;

  double alphaShadow = 0;
  double opacity = 0;
  for (int k = 1; k <= m_shadowSettings->thickness(); k++) {
    /* Generate a shadow THICKNESS pixels thicker
     * on either side than the text image. Ensure
     * that i +/- k and j +/- k are within the
     * bounds of the text pixmap.
     */
    opacity = 0;
    for (int l = -k; l <= k; l++) {
      if (i < k)
	sx = 0;
      else if (i >= w - k)
	sx = w - 1;
      else
	sx = i + l;

      for (int m = -k; m <= k; m++) {
	if (j < k)
	  sy = 0;
	else if (j >= h - k)
	  sy = h - 1;
	else
	  sy = j + m;

	opacity += tqGray(source.pixel(sx, sy));
      }
    }
    alphaShadow += opacity / m_shadowSettings->multiplicationFactor();
  }
  return alphaShadow;
}