1 /*
2  */
3 
4 /*
5 
6     Copyright (C) 2014 Ferrero Andrea
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 
22  */
23 
24 /*
25 
26     These files are distributed with PhotoFlow - http://aferrero2707.github.io/PhotoFlow/
27 
28  */
29 
30 #ifndef VIPS_CHANNEL_MIXER_H
31 #define VIPS_CHANNEL_MIXER_H
32 
33 #include "../base/format_info.hh"
34 #include "../base/pixel_processor.hh"
35 
36 namespace PF
37 {
38 
39 class ChannelMixerPar: public PixelProcessorPar
40 {
41   Property<float> red_mix, green_mix, blue_mix;
42 public:
ChannelMixerPar()43   ChannelMixerPar():
44     PixelProcessorPar(),
45     red_mix("red_mix",this,1),
46     green_mix("green_mix",this,0),
47     blue_mix("blue_mix",this,0)
48 {
49     set_type( "channel_mixer" );
50 
51     set_default_name( _("channel mixer") );
52 }
get_red_mix()53   float get_red_mix() { return red_mix.get(); }
get_green_mix()54   float get_green_mix() { return green_mix.get(); }
get_blue_mix()55   float get_blue_mix() { return blue_mix.get(); }
56 };
57 
58 
59 
60 
61 
62 template < typename T, colorspace_t CS, int CHMIN, int CHMAX, bool PREVIEW, class OP_PAR >
63 class ChannelMixerProc
64 {
65   ChannelMixerPar* par;
66 public:
ChannelMixerProc(ChannelMixerPar * p)67   ChannelMixerProc(ChannelMixerPar* p): par(p) {}
68 
process(T ** p,const int & n,const int & first,const int & nch,const int & x,const double & intensity,T * & pout)69   void process(T**p, const int& n, const int& first, const int& nch, const int& x, const double& intensity, T*& pout)
70   {
71     for( int ch = CHMIN; ch <= CHMAX; ch++ )
72       pout[x+ch] = p[first][x+ch];
73   }
74 };
75 
76 
77 template < typename T, int CHMIN, int CHMAX, bool PREVIEW, class OP_PAR >
78 class ChannelMixerProc<T,PF_COLORSPACE_RGB,CHMIN,CHMAX,PREVIEW,OP_PAR>
79 {
80   ChannelMixerPar* par;
81 public:
ChannelMixerProc(ChannelMixerPar * p)82   ChannelMixerProc(ChannelMixerPar* p): par(p) {}
83 
process(T ** p,const int & n,const int & first,const int & nch,const int & x,const double & intensity,T * & pout)84   void process(T**p, const int& n, const int& first, const int& nch, const int& x, const double& intensity, T*& pout)
85   {
86     float sum = par->get_red_mix() + par->get_green_mix() + par->get_blue_mix();
87     typename FormatInfo<T>::PROMOTED newval = 0;
88     int i = x;
89     if( sum < -1.0e-15 || sum > 1.0e-15 ) {
90       newval = (typename FormatInfo<T>::PROMOTED)( (par->get_red_mix()*p[first][x] +
91           par->get_green_mix()*p[first][x+1] +
92           par->get_blue_mix()*p[first][x+2])/sum );
93     }
94     clip(newval,pout[x]);
95     pout[x+1] = pout[x];
96     pout[x+2] = pout[x];
97   }
98 };
99 
100 
101 template < OP_TEMPLATE_DEF >
102 class ChannelMixer: public PixelProcessor< OP_TEMPLATE_IMP, ChannelMixerPar, ChannelMixerProc >
103 {
104 };
105 
106 
107 ProcessorBase* new_channel_mixer();
108 
109 }
110 
111 #endif
112 
113 
114