File: plfilterintensity.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 (164 lines) | stat: -rw-r--r-- 4,758 bytes parent folder | download | duplicates (2)
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
/*
/--------------------------------------------------------------------
|
|      $Id: plfilterintensity.cpp,v 1.10 2004/09/11 12:41:36 uzadow Exp $
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "plfilterintensity.h"
#include "plbitmap.h"
#include "plpaintlibdefs.h"

#include  <math.h>

PLFilterIntensity::PLFilterIntensity(double intensity, PLBYTE offset, double exponent)
  : PLFilter(),
    m_intensity(intensity),
    m_offset(offset),
    m_exponent(exponent)
{

}

PLFilterIntensity::~PLFilterIntensity()
{

}

void PLFilterIntensity::Apply(PLBmpBase * pBmpSource, PLBmp * pBmpDest) const 
{
  double h, s, v;
  double intensityFactor;
  int total = 0;
  register int inc;
  extern void fp_rgb_to_hsv(double* r, double* g, double *b);
  extern void fp_hsv_to_rgb(double* r, double* g, double *b);

  // double intensity = 4.0 * m_intensity - 200.0;
  double intensity = m_intensity-20;
  intensity /= 4.0;
  PLASSERT (pBmpSource->GetBitsPerPixel() >= 24);

  pBmpDest->Create (pBmpSource->GetWidth(), 
                    pBmpSource->GetHeight(),
                    pBmpSource->GetPixelFormat(),
                    NULL, 0, 
                    pBmpSource->GetResolution());

  PLBYTE ** pSrcLines = pBmpSource->GetLineArray();
  PLBYTE ** pDstLines = pBmpDest->GetLineArray();

  register int destWidth = pBmpDest->GetWidth();

  if(pBmpSource->GetBitsPerPixel() == 24)
   inc = 3;

  if(pBmpSource->GetBitsPerPixel() == 32)
   inc = sizeof(PLPixel32);

  double csupp = intensity/pow(255.0, m_exponent);

  for (int y = 0; y < pBmpDest->GetHeight(); ++y)
  { // For each line
    PLBYTE * pSrcPixel = pSrcLines[y];
    PLBYTE * pDstPixel = pDstLines[y];

    for (register int x = 0; x < destWidth; ++x)
    {
       // Transform rgb->hsv
       h =(double) pSrcPixel[PL_RGBA_RED];
       s =(double) pSrcPixel[PL_RGBA_GREEN];
       v =(double) pSrcPixel[PL_RGBA_BLUE];

       fp_rgb_to_hsv(&h, &s, &v);

       // Modify the intensity
       if (v >= m_offset)
        intensityFactor = 1.0 + csupp * pow((v-m_offset), m_exponent);
       else
        intensityFactor = 1.0;

       v *= intensityFactor;

       // Transform back to rgb.
       fp_hsv_to_rgb(&h, &s, &v);

       pDstPixel[PL_RGBA_RED]   = (PLBYTE) h;
       pDstPixel[PL_RGBA_GREEN] = (PLBYTE) s;
       pDstPixel[PL_RGBA_BLUE]  = (PLBYTE) v;

       if(h >= 255.0)
         pDstPixel[PL_RGBA_RED]   = (PLBYTE) 255;
       if(s >= 255.0)
         pDstPixel[PL_RGBA_GREEN] = (PLBYTE) 255;
       if(v >= 255.0)
         pDstPixel[PL_RGBA_BLUE]  = (PLBYTE) 255;
       if(h <= 0.0)
         pDstPixel[PL_RGBA_RED]   = (PLBYTE) 0;
       if(s <= 0.0)
         pDstPixel[PL_RGBA_GREEN] = (PLBYTE) 0;
       if(v <= 0.0)
         pDstPixel[PL_RGBA_BLUE]  = (PLBYTE) 0;

       pSrcPixel += inc;
       pDstPixel += inc;
    }
  }
}

/*
/--------------------------------------------------------------------
|
|      $Log: plfilterintensity.cpp,v $
|      Revision 1.10  2004/09/11 12:41:36  uzadow
|      removed plstdpch.h
|
|      Revision 1.9  2004/09/09 16:52:45  artcom
|      refactored PixelFormat
|
|      Revision 1.8  2004/06/15 10:26:13  uzadow
|      Initial nonfunctioning version of plbmpbase.
|
|      Revision 1.7  2003/11/05 15:17:26  artcom
|      Added ability to specify initial data in PLBitmap::Create()
|
|      Revision 1.6  2002/08/04 20:08:01  uzadow
|      Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support.
|
|      Revision 1.5  2002/03/31 13:36:42  uzadow
|      Updated copyright.
|
|      Revision 1.4  2001/10/16 17:12:26  uzadow
|      Added support for resolution information (Luca Piergentili)
|
|      Revision 1.3  2001/10/06 22:03:26  uzadow
|      Added PL prefix to basic data types.
|
|      Revision 1.2  2001/10/06 15:32:22  uzadow
|      Removed types LPBYTE, DWORD, UCHAR, VOID and INT from the code.
|
|      Revision 1.1  2001/09/16 19:03:23  uzadow
|      Added global name prefix PL, changed most filenames.
|
|      Revision 1.5  2001/09/15 21:02:44  uzadow
|      Cleaned up stdpch.h and config.h to make them internal headers.
|
|      Revision 1.4  2001/02/04 14:31:52  uzadow
|      Member initialization list cleanup (Erik Hoffmann).
|
|      Revision 1.3  2001/01/15 15:05:31  uzadow
|      Added PLBmp::ApplyFilter() and PLBmp::CreateFilteredCopy()
|
|      Revision 1.2  2000/12/18 22:42:53  uzadow
|      Replaced RGBAPIXEL with PLPixel32.
|
|      Revision 1.1  2000/11/06 23:20:22  uzadow
|      Added Contrast, Intensity and Lightness filters by
|      Thomas Hirschmann
|
|
\--------------------------------------------------------------------
*/