~ubuntu-branches/ubuntu/vivid/luminance-hdr/vivid-proposed

« back to all changes in this revision

Viewing changes to src/Filter/pfsgammaandlevels.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Metzler
  • Date: 2012-11-03 09:09:25 UTC
  • mfrom: (3.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20121103090925-6b12x4a4cpqd3u4q
Tags: 2.3.0-2
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
    return v;
46
46
}
47
47
 
48
 
//! \note I assume that *in* contains only value between [0,1]
49
 
void gamma_levels_array(const pfs::Array2D* in, pfs::Array2D* out,
50
 
                        float black_in, float white_in,
51
 
                        float black_out, float white_out, float gamma)
52
 
{
53
 
    // same formula used inside GammaAndLevels::refreshLUT()
54
 
    //float value = powf( ( ((float)(i)/255.0f) - bin ) / (win-bin), expgamma);
55
 
    //LUT[i] = clamp(blackout+value*(whiteout-blackout),0,255);
56
 
 
57
 
    const float* in_vector = in->getRawData();
58
 
    float* out_vector = out->getRawData();
59
 
 
60
 
    const int ELEMS = in->getCols()*in->getRows();
61
 
 
62
 
    if (gamma != 1.0f)
63
 
    {
64
 
#pragma omp parallel for
65
 
        for (int idx = 0; idx < ELEMS; ++idx)
66
 
        {
67
 
            float tmp = (in_vector[idx] - black_in)/(white_in - black_in);
68
 
            tmp = powf(tmp, gamma);
69
 
 
70
 
            tmp = black_out + tmp*(white_out-black_out);
71
 
 
72
 
            out_vector[idx] = clamp(tmp, 0.0f, 1.0f);
73
 
        }
74
 
    }
75
 
    else
76
 
    {
77
 
#pragma omp parallel for
78
 
        for (int idx = 0; idx < ELEMS; ++idx)
79
 
        {
80
 
            float tmp = (in_vector[idx] - black_in)/(white_in - black_in);
81
 
            //tmp = powf(tmp, gamma);
82
 
 
83
 
            tmp = black_out + tmp*(white_out-black_out);
84
 
 
85
 
            out_vector[idx] = clamp(tmp, 0.0f, 1.0f);
86
 
        }
87
 
    }
88
 
}
 
48
////! \note I assume that *in* contains only value between [0,1]
 
49
//void gamma_levels_array(const pfs::Array2D* in, pfs::Array2D* out,
 
50
//                        float black_in, float white_in,
 
51
//                        float black_out, float white_out, float gamma)
 
52
//{
 
53
//    // same formula used inside GammaAndLevels::refreshLUT()
 
54
//    //float value = powf( ( ((float)(i)/255.0f) - bin ) / (win-bin), expgamma);
 
55
//    //LUT[i] = clamp(blackout+value*(whiteout-blackout),0,255);
 
56
 
 
57
//    const float* in_vector = in->getRawData();
 
58
//    float* out_vector = out->getRawData();
 
59
 
 
60
//    const int ELEMS = in->getCols()*in->getRows();
 
61
 
 
62
//    if (gamma != 1.0f)
 
63
//    {
 
64
//#pragma omp parallel for
 
65
//        for (int idx = 0; idx < ELEMS; ++idx)
 
66
//        {
 
67
//            float tmp = (in_vector[idx] - black_in)/(white_in - black_in);
 
68
//            tmp = powf(tmp, gamma);
 
69
 
 
70
//            tmp = black_out + tmp*(white_out-black_out);
 
71
 
 
72
//            out_vector[idx] = clamp(tmp, 0.0f, 1.0f);
 
73
//        }
 
74
//    }
 
75
//    else
 
76
//    {
 
77
//#pragma omp parallel for
 
78
//        for (int idx = 0; idx < ELEMS; ++idx)
 
79
//        {
 
80
//            float tmp = (in_vector[idx] - black_in)/(white_in - black_in);
 
81
//            //tmp = powf(tmp, gamma);
 
82
 
 
83
//            tmp = black_out + tmp*(white_out-black_out);
 
84
 
 
85
//            out_vector[idx] = clamp(tmp, 0.0f, 1.0f);
 
86
//        }
 
87
//    }
 
88
//}
89
89
 
90
90
}
91
91
 
92
92
namespace pfs
93
93
{
94
94
 
95
 
pfs::Frame* gamma_levels(pfs::Frame* inFrame, float black_in, float white_in,
96
 
                         float black_out, float white_out, float gamma)
 
95
pfs::Frame* gamma_levels(pfs::Frame* inFrame,
 
96
                         float black_in, float white_in,
 
97
                         float black_out, float white_out,
 
98
                         float gamma)
97
99
{
98
100
#ifdef TIMER_PROFILING
99
101
    msec_timer f_timer;
104
106
             << "White in =" << white_in << "white out =" << white_out
105
107
             << "Gamma =" << gamma;
106
108
 
107
 
    pfs::DOMIO pfsio;
108
 
 
109
109
    const int outWidth   = inFrame->getWidth();
110
110
    const int outHeight  = inFrame->getHeight();
111
111
 
112
 
    pfs::Frame *outFrame = pfsio.createFrame(outWidth, outHeight);
113
 
 
114
 
    pfs::ChannelIterator *it = inFrame->getChannels();
115
 
 
116
 
    while (it->hasNext())
 
112
    pfs::Frame* outFrame = new pfs::Frame(outWidth, outHeight);
 
113
 
 
114
    pfs::Channel *Xc, *Yc, *Zc;
 
115
    inFrame->getXYZChannels( Xc, Yc, Zc );
 
116
    assert( Xc != NULL && Yc != NULL && Zc != NULL );
 
117
 
 
118
    const float* R_i = Xc->getRawData();
 
119
    const float* G_i = Yc->getRawData();
 
120
    const float* B_i = Zc->getRawData();
 
121
 
 
122
    outFrame->createXYZChannels( Xc, Yc, Zc );
 
123
    assert( Xc != NULL && Yc != NULL && Zc != NULL );
 
124
 
 
125
    float* R_o = Xc->getRawData();
 
126
    float* G_o = Yc->getRawData();
 
127
    float* B_o = Zc->getRawData();
 
128
 
 
129
    // float exp_gamma = 1.f/gamma;
 
130
    for (int idx = 0; idx < outWidth*outHeight; ++idx)
117
131
    {
118
 
      pfs::Channel *inCh  = it->getNext();
119
 
      pfs::Channel *outCh = outFrame->createChannel(inCh->getName());
120
 
 
121
 
      pfs::Array2D* inArray2D   = inCh->getChannelData();
122
 
      pfs::Array2D* outArray2D  = outCh->getChannelData();
123
 
 
124
 
      gamma_levels_array(inArray2D, outArray2D, black_in, white_in,
125
 
                         black_out, white_out, gamma);
 
132
        float red = R_i[idx];
 
133
        float green = G_i[idx];
 
134
        float blue = B_i[idx];
 
135
 
 
136
        float L = 0.2126f * red
 
137
                + 0.7152f * green
 
138
                + 0.0722f * blue; // number between [0..1]
 
139
 
 
140
        float c = powf(L, gamma - 1.0f);
 
141
 
 
142
        red = (red - black_in) / (white_in - black_in);
 
143
        red *= c;
 
144
 
 
145
        green = (green - black_in) / (white_in - black_in);
 
146
        green *= c;
 
147
 
 
148
        blue = (blue - black_in) / (white_in - black_in);
 
149
        blue *= c;
 
150
 
 
151
        R_o[idx] = clamp(black_out + red * (white_out - black_out), 0.f, 1.f);
 
152
        G_o[idx] = clamp(black_out + green * (white_out - black_out), 0.f, 1.f);
 
153
        B_o[idx] = clamp(black_out + blue * (white_out - black_out), 0.f, 1.f);
126
154
    }
127
155
 
128
156
    pfs::copyTags(inFrame, outFrame);