File: shadow.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (236 lines) | stat: -rw-r--r-- 6,110 bytes parent folder | download | duplicates (3)
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
/*
/--------------------------------------------------------------------
|
|      $Id: shadow.cpp,v 1.6 2002/03/31 13:36:42 uzadow Exp $
|      Shadow Graphic item class
|
|      A shadowed region on a canvas. The item is basically a
|      partially transparent region. Transparency is defined by an
|      alpha DIB and a global transparency value. Most of the code
|      was just copied from CDIBGrItem & changed a bit.
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "stdafx.h"
#include "shadow.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC (CShadowItem, CGrItem);


CShadowItem::CShadowItem
    ( int x,             // Position on the canvas
      int y,
      int w,             // Width
      int h,             // Height
      int z,             // Position in z-Order
      PLBYTE Opacity,      // Opacity of the object. 255 is completely
                         // opaque, 0 is completely transparent.
      PLPixel32 * pColor, // Shadow color.
      PLBmp * pAlphaBmp   // Alpha channel (must be 8 bpp). Can be NULL.
    )
  : CGrItem (x, y, w, h, z, Opacity)
{
  m_Color = *pColor;
  m_pAlphaBmp = pAlphaBmp;
  if (m_pAlphaBmp)
    m_pLineArray = m_pAlphaBmp->GetLineArray();
   else
    m_pLineArray = NULL;
}


CShadowItem::~CShadowItem
    ()
{
}


void CShadowItem::Draw
    ( PLBmp * pCanvas,
      CRect * pUpdateRect
    )
    // Responsible for drawing the object on the canvas.
{
//  ASSERT_VALID (pCanvas);
  ASSERT (pCanvas->GetBitsPerPixel() == 32);

  // Perform clipping.
  CRect ClipRect (pUpdateRect);
  clip (pCanvas, &ClipRect);

  // Draw
  if (m_pAlphaBmp)
  {
    if (m_w == m_pAlphaBmp->GetWidth() &&
        m_h == m_pAlphaBmp->GetHeight())
      drawClippedNoScale (pCanvas, &ClipRect);
     else
      drawClipped (pCanvas, &ClipRect);
  }
  else
    drawClippedNoScale (pCanvas, &ClipRect);

  // If this fails, the procedure destroyed something.
//  ASSERT_VALID (pCanvas);
}


void CShadowItem::drawClippedNoScale
    ( PLBmp * pCanvas,
      CRect * pRect
    )
    // Draws the object. pRect must have been clipped already.
    // Assumes that no scaling is nessesary.
{
  PLBYTE * pDest;
  PLBYTE * pAlpha;
  PLBYTE ** pDestLineArray = pCanvas->GetLineArray();

  // Set up loop invariants.
  int SrcXOfs = (pRect->left-m_x);

  for (int sy = pRect->top; sy<pRect->bottom; sy++)
  { // For each line
    pDest = pDestLineArray[sy] +  pRect->left*4;
    if (m_pAlphaBmp)
    {
      pAlpha = m_pLineArray[sy-m_y] + SrcXOfs;

      drawAlphaLine (pDest, pAlpha, pRect);
    }
    else
      drawFadeLine (pDest, pRect);
  }
}


inline void CShadowItem::drawAlphaLine
    ( PLBYTE * pDest,
      PLBYTE * pAlpha,
      CRect * pRect
    )
{
  int alpha;
  int negalpha;
  PLBYTE * pColor = (PLBYTE *)(&m_Color);  // Well...

  for (int sx = pRect->left; sx<pRect->right; sx++)
  { // For each pixel
    alpha = ((*pAlpha) * m_Opacity)>>8;
    negalpha = 255-alpha;
    pDest[PL_RGBA_RED] = (pDest[PL_RGBA_RED]*negalpha +
                       *(pColor+PL_RGBA_RED)*alpha)>>8;
    pDest[PL_RGBA_GREEN] = (pDest[PL_RGBA_GREEN]*negalpha +
                         *(pColor+PL_RGBA_GREEN)*alpha)>>8;
    pDest[PL_RGBA_BLUE] = (pDest[PL_RGBA_BLUE]*negalpha +
                        *(pColor+PL_RGBA_BLUE)*alpha)>>8;

    pDest += 4;
    pAlpha++;
  }
}


inline void CShadowItem::drawFadeLine
    ( PLBYTE * pDest,
      CRect * pRect
    )
    // Draws one line. No scaling. Assumes alpha channel doesn't
    // exist.
{
  int negalpha;
  PLBYTE * pColor = (PLBYTE *)(&m_Color);  // Well...
  PLBYTE Color[4];
  Color[PL_RGBA_BLUE] = *(pColor+PL_RGBA_BLUE)*m_Opacity>>8;
  Color[PL_RGBA_GREEN] = *(pColor+PL_RGBA_GREEN)*m_Opacity>>8;
  Color[PL_RGBA_RED] = *(pColor+PL_RGBA_RED)*m_Opacity>>8;
  negalpha = 255-m_Opacity;

  for (int sx = pRect->left; sx<pRect->right; sx++)
  { // For each pixel
    pDest[PL_RGBA_BLUE] = (pDest[PL_RGBA_BLUE]*negalpha>>8)+Color[PL_RGBA_BLUE];
    pDest[PL_RGBA_GREEN] = (pDest[PL_RGBA_GREEN]*negalpha>>8)+Color[PL_RGBA_GREEN];
    pDest[PL_RGBA_RED] = (pDest[PL_RGBA_RED]*negalpha>>8)+Color[PL_RGBA_RED];

    pDest += 4;
  }
}


void CShadowItem::drawClipped
    ( PLBmp * pCanvas,
      CRect * pRect
    )
    // Draws the object. pRect must have been clipped already.
{
  PLBYTE * pDest;
  PLBYTE * pAlpha = NULL;
  int SrcLineNum;
  PLBYTE ** pDestLineArray = pCanvas->GetLineArray();

  double YScale = (double)(m_pAlphaBmp->GetHeight())/m_h;

  for (int sy = pRect->top; sy<pRect->bottom; sy++)
  { // For each line
    pDest = pDestLineArray[sy] + pRect->left*4;
    SrcLineNum = (int)((sy-m_y)*YScale);
    pAlpha = m_pLineArray [SrcLineNum];
    drawScaleLine (pDest, pAlpha, pRect);
  }
}


inline void CShadowItem::drawScaleLine
    ( PLBYTE * pDest,
      PLBYTE * pAlpha,
      CRect * pRect
    )
{
  int alpha;
  int negalpha;
  int SrcOfs;
  double XScale = (double)(m_pAlphaBmp->GetWidth())/m_w;
  int xs = (int)(XScale*65536);
  PLBYTE * pColor = (PLBYTE *)(&m_Color);  // Well...

  for (int sx = pRect->left-m_x; sx<pRect->right-m_x; sx++)
  { // For each pixel
    SrcOfs = (sx*xs)>>16;
    if (pAlpha)
      alpha = ((*(pAlpha+SrcOfs)) * m_Opacity)>>8;
     else
      alpha = m_Opacity;
    negalpha = 255-alpha;
    pDest[PL_RGBA_BLUE] = (pDest[PL_RGBA_BLUE]*negalpha +
                       pColor[PL_RGBA_BLUE]*alpha)>>8;
    pDest[PL_RGBA_GREEN] = (pDest[PL_RGBA_GREEN]*negalpha +
                        pColor[PL_RGBA_GREEN]*alpha)>>8;
    pDest[PL_RGBA_RED] = (pDest[PL_RGBA_RED]*negalpha +
                      pColor[PL_RGBA_RED]*alpha)>>8;
    pDest += 4;
  }
}


/*
/--------------------------------------------------------------------
|
|      $Log: shadow.cpp,v $
|      Revision 1.6  2002/03/31 13:36:42  uzadow
|      Updated copyright.
|
|      Revision 1.5  2001/09/16 19:03:23  uzadow
|      Added global name prefix PL, changed most filenames.
|
|
--------------------------------------------------------------------
*/