2
* @brief Standard response functions
5
* This file is a part of Qtpfsgui package.
6
* ----------------------------------------------------------------------
7
* Copyright (C) 2004 Grzegorz Krawczyk
8
* Copyright (C) 2006-2007 Giuseppe Rota
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* ----------------------------------------------------------------------
25
* @author Grzegorz Krawczyk, <gkrawczyk@users.sourceforge.net>
26
* @author Giuseppe Rota <grota@users.sourceforge.net>
28
* $Id: responses.cpp,v 1.6 2006/09/13 14:27:06 gkrawczyk Exp $
38
#include "responses.h"
40
#define MIN_WEIGHT 1e-3
42
void dump_gnuplot( const char* filename, const float* array, int M )
44
FILE* fp = fopen(filename, "w");
45
fprintf(fp, "# GNUPlot dump\n");
46
for( int i=0 ; i<M ; i++ )
47
fprintf(fp, "%4d %16.9f\n", i, array[i]);
49
std::cerr << "GNUPLOT: save data to " << filename << std::endl;
52
void dump_gnuplot( const char* filename, const float* array, int M, int counter )
55
sprintf(fn, filename, counter);
56
dump_gnuplot(fn, array, M);
59
void exposure_weights_icip06( float* w, int M, int Mmin, int Mmax )
61
for( int m=0 ; m<M ; m++ )
62
if( m<Mmin || m>Mmax )
65
w[m]=1.0f-pow( ( (2.0f*float(m-Mmin)/float(Mmax-Mmin)) - 1.0f), 12.0f);
68
void weightsGauss( float* w, int M, int Mmin, int Mmax, float sigma )
70
float mid = Mmin + (Mmax-Mmin)/2.0f - 0.5f;
71
float mid2 = (mid-Mmin) * (mid-Mmin);
72
for( int m=0 ; m<M ; m++ )
73
if( m<Mmin || m>Mmax )
77
// gkrawczyk: that's not really a gaussian, but equation is
78
// taken from Robertson02 paper.
79
float weight = exp( -sigma * (m-mid) * (m-mid) / mid2 );
81
if( weight<MIN_WEIGHT ) // ignore very low weights
88
void weights_triangle( float* w, int M/*, int Mmin, int Mmax*/ )
90
for(int i=0;i<int(float(M)/2.0f);i++) {
91
w[i]=i/ (float(M)/2.0f);
92
if (w[i]<0.06f)w[i]=0;
94
for(int i=int(float(M)/2.0f);i<M;i++) {
95
w[i]=(M-1-i)/(float(M)/2.0f);
96
if (w[i]<0.06f)w[i]=0;
98
// for( int m=0 ; m<M ; m++ )
99
// if( m<Mmin || m>Mmax )
103
// if ( m<int(Mmin+ (Mmax-Mmin)/2.0f +1) )
104
// w[m]=(m-Mmin)/float(Mmin+(Mmax-Mmin)/2.0f);
106
// w[m]=( -m+Mmin+((Mmax-Mmin)) )/float(Mmin+(Mmax-Mmin)/2.0f);
109
// if (w[i]<0.06f)w[i]=0;
112
void responseLinear( float* I, int M )
114
for( int m=0 ; m<M ; m++ )
115
I[m] = m / float(M-1); // range is not important, values are normalized later
119
void responseGamma( float* I, int M )
121
float norm = M / 4.0f;
123
// response curve decided empirically
124
for( int m=0 ; m<M ; m++ )
125
I[m] = powf( m/norm, 1.7f ) + 1e-4;
129
void responseLog10( float* I, int M )
131
float mid = 0.5f * M;
132
float norm = 0.0625f * M;
134
for( int m=0 ; m<M ; m++ )
135
I[m] = powf(10.0f, float(m - mid) / norm);
139
void responseSave( FILE* file, const float* I, int M, const char* name)
141
// response curve matrix header
142
fprintf(file, "# Camera response curve, channel %s\n", name);
143
fprintf(file, "# data layout: log10(response) | camera output | response\n");
144
fprintf(file, "# name: %s\n", name);
145
fprintf(file, "# type: matrix\n");
146
fprintf(file, "# rows: %d\n", M);
147
fprintf(file, "# columns: 3\n");
150
for( int m=0 ; m<M ; m++ )
152
fprintf(file, " %15.9f %4d %15.9f\n", log10f(I[m]), m, I[m]);
154
fprintf(file, " %15.9f %4d %15.9f\n", -6.0f, m, I[m]);
160
void weightsSave( FILE* file, const float* w, int M, const char* name)
162
// weighting function matrix header
163
fprintf(file, "# Weighting function\n");
164
fprintf(file, "# data layout: weight | camera output\n");
165
fprintf(file, "# name: %s\n", name);
166
fprintf(file, "# type: matrix\n");
167
fprintf(file, "# rows: %d\n", M);
168
fprintf(file, "# columns: 2\n");
171
for( int m=0 ; m<M ; m++ )
172
fprintf(file, " %15.9f %4d\n", w[m], m);
178
bool responseLoad( FILE* file, float* I, int M)
183
// parse response curve matrix header
184
while( fgets(line, 1024, file) )
185
if( sscanf(line, "# rows: %d\n", &m) == 1 )
189
std::cerr << "response: number of input levels is different,"
190
<< " M=" << M << " m=" << m << std::endl;
193
while( fgets(line, 1024, file) )
194
if( sscanf(line, "# columns: %d\n", &c) == 1 )
201
for( int i=0 ; i<M ; i++ )
204
if( fscanf(file, " %f %d %f\n", &ignore, &m, &val) !=3 )
207
std::cerr << "response: camera value out of range,"
208
<< " m=" << m << std::endl;
217
bool weightsLoad( FILE* file, float* w, int M)
222
// parse weighting function matrix header
223
while( fgets(line, 1024, file) )
224
if( sscanf(line, "# rows: %d\n", &m) == 1 )
228
std::cerr << "response: number of input levels is different,"
229
<< " M=" << M << " m=" << m << std::endl;
232
while( fgets(line, 1024, file) )
233
if( sscanf(line, "# columns: %d\n", &c) == 1 )
239
for( int i=0 ; i<M ; i++ )
240
if( fscanf(file, " %f %d\n", &(w[i]), &m) !=2 )