~ubuntu-branches/ubuntu/natty/qtpfsgui/natty

« back to all changes in this revision

Viewing changes to src/HdrWizard/hdrcreation/responses.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-01-06 04:39:36 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080106043936-a9u9g7yih3w16ru5
Tags: 1.9.0-1
* New upstream release.
* Replace “COPYING” with “LICENSE” in the NOT_NEEDED variable of
  debian/rules, following upstream's renaming.
* Update debian/links accordingly.
* Delete the matching TODO item since there's no longer needed to have a
  patched (with HTML tags) license file to get a correct display in the
  “License agreement” tab.
* Update the gcc4.3 patch (drop the hunk touching src/Libpfs/pfs.cpp):
   - 20_gcc4.3_includes.
* Add a link from /usr/share/qtpfsgui/html to the HTML documentation
  under /usr/share/doc/qtpfsgui/html since the former is used at runtime
  to display the manual.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * @brief Standard response functions
3
 
 *
4
 
 * 
5
 
 * This file is a part of Qtpfsgui package.
6
 
 * ---------------------------------------------------------------------- 
7
 
 * Copyright (C) 2004 Grzegorz Krawczyk
8
 
 * Copyright (C) 2006-2007 Giuseppe Rota
9
 
 * 
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.
14
 
 *
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.
19
 
 *
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
 
 * ---------------------------------------------------------------------- 
24
 
 * 
25
 
 * @author Grzegorz Krawczyk, <gkrawczyk@users.sourceforge.net>
26
 
 * @author Giuseppe Rota <grota@users.sourceforge.net>
27
 
 *
28
 
 * $Id: responses.cpp,v 1.6 2006/09/13 14:27:06 gkrawczyk Exp $
29
 
 */
30
 
 
31
 
#include <iostream>
32
 
#include <vector>
33
 
 
34
 
#include <stdio.h>
35
 
#include <stdlib.h>
36
 
#include <math.h>
37
 
 
38
 
#include "responses.h"
39
 
 
40
 
#define MIN_WEIGHT 1e-3
41
 
 
42
 
void dump_gnuplot( const char* filename, const float* array, int M )
43
 
{
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]);
48
 
  fclose(fp);
49
 
  std::cerr << "GNUPLOT: save data to " << filename << std::endl;
50
 
}
51
 
 
52
 
void dump_gnuplot( const char* filename, const float* array, int M, int counter )
53
 
{
54
 
  char fn[2048];
55
 
  sprintf(fn, filename, counter);
56
 
  dump_gnuplot(fn, array, M);
57
 
}
58
 
 
59
 
void exposure_weights_icip06( float* w, int M, int Mmin, int Mmax )
60
 
{
61
 
  for( int m=0 ; m<M ; m++ )
62
 
    if( m<Mmin || m>Mmax )
63
 
      w[m] = 0.0f;
64
 
    else
65
 
      w[m]=1.0f-pow( ( (2.0f*float(m-Mmin)/float(Mmax-Mmin)) - 1.0f), 12.0f);
66
 
}
67
 
 
68
 
void weightsGauss( float* w, int M, int Mmin, int Mmax, float sigma )
69
 
{
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 )
74
 
      w[m] = 0.0f;
75
 
    else
76
 
    {
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 );
80
 
      
81
 
      if( weight<MIN_WEIGHT )           // ignore very low weights
82
 
        w[m] = 0.0f;
83
 
      else
84
 
        w[m] = weight;
85
 
    }
86
 
}
87
 
 
88
 
void weights_triangle( float* w, int M/*, int Mmin, int Mmax*/ )
89
 
{
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;
93
 
        }
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;
97
 
        }
98
 
//   for( int m=0 ; m<M ; m++ )
99
 
//     if( m<Mmin || m>Mmax )
100
 
//       w[m] = 0.0f;
101
 
//     else
102
 
//     {
103
 
//      if ( m<int(Mmin+ (Mmax-Mmin)/2.0f +1) )
104
 
//              w[m]=(m-Mmin)/float(Mmin+(Mmax-Mmin)/2.0f);
105
 
//      else
106
 
//              w[m]=( -m+Mmin+((Mmax-Mmin)) )/float(Mmin+(Mmax-Mmin)/2.0f);
107
 
//     }
108
 
 
109
 
//        if (w[i]<0.06f)w[i]=0;
110
 
}
111
 
 
112
 
void responseLinear( float* I, int M )
113
 
{
114
 
  for( int m=0 ; m<M ; m++ )
115
 
    I[m] = m / float(M-1); // range is not important, values are normalized later
116
 
}
117
 
 
118
 
 
119
 
void responseGamma( float* I, int M )
120
 
{
121
 
  float norm = M / 4.0f;
122
 
  
123
 
  // response curve decided empirically
124
 
  for( int m=0 ; m<M ; m++ )
125
 
    I[m] = powf( m/norm, 1.7f ) + 1e-4;
126
 
}
127
 
 
128
 
 
129
 
void responseLog10( float* I, int M )
130
 
{
131
 
  float mid = 0.5f * M;
132
 
  float norm = 0.0625f * M;
133
 
  
134
 
  for( int m=0 ; m<M ; m++ )
135
 
    I[m] = powf(10.0f, float(m - mid) / norm);
136
 
}
137
 
 
138
 
 
139
 
void responseSave( FILE* file, const float* I, int M, const char* name)
140
 
{
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");
148
 
 
149
 
  // save response
150
 
  for( int m=0 ; m<M ; m++ )
151
 
    if( I[m]!=0.0f )
152
 
      fprintf(file, " %15.9f %4d %15.9f\n", log10f(I[m]), m, I[m]);
153
 
    else
154
 
      fprintf(file, " %15.9f %4d %15.9f\n", -6.0f, m, I[m]);
155
 
  
156
 
  fprintf(file, "\n");
157
 
}
158
 
 
159
 
 
160
 
void weightsSave( FILE* file, const float* w, int M, const char* name)
161
 
{
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");
169
 
  
170
 
  // save weights
171
 
  for( int m=0 ; m<M ; m++ )
172
 
    fprintf(file, " %15.9f %4d\n", w[m], m);
173
 
  
174
 
  fprintf(file, "\n");
175
 
}
176
 
 
177
 
 
178
 
bool responseLoad( FILE* file, float* I, int M)
179
 
{
180
 
  char line[1024];
181
 
  int m=0,c=0;
182
 
  
183
 
  // parse response curve matrix header
184
 
  while( fgets(line, 1024, file) )
185
 
    if( sscanf(line, "# rows: %d\n", &m) == 1 )
186
 
      break;
187
 
  if( m!=M )
188
 
  {
189
 
    std::cerr << "response: number of input levels is different,"
190
 
              << " M=" << M << " m=" << m << std::endl; 
191
 
    return false;
192
 
  }
193
 
  while( fgets(line, 1024, file) )
194
 
    if( sscanf(line, "# columns: %d\n", &c) == 1 )
195
 
      break;
196
 
  if( c!=3 )
197
 
    return false;
198
 
  
199
 
  // read response
200
 
  float ignore;
201
 
  for( int i=0 ; i<M ; i++ )
202
 
  {
203
 
    float val;
204
 
    if( fscanf(file, " %f %d %f\n", &ignore, &m, &val) !=3 )
205
 
      return false;
206
 
    if( m<0 || m>M )
207
 
      std::cerr << "response: camera value out of range,"
208
 
                << " m=" << m << std::endl;
209
 
    else
210
 
      I[m] = val;
211
 
  }
212
 
  
213
 
  return true;
214
 
}
215
 
 
216
 
 
217
 
bool weightsLoad( FILE* file, float* w, int M)
218
 
{
219
 
  char line[1024];
220
 
  int m=0,c=0;
221
 
 
222
 
  // parse weighting function matrix header
223
 
  while( fgets(line, 1024, file) )
224
 
    if( sscanf(line, "# rows: %d\n", &m) == 1 )
225
 
      break;
226
 
  if( m!=M )
227
 
  {
228
 
    std::cerr << "response: number of input levels is different,"
229
 
              << " M=" << M << " m=" << m << std::endl; 
230
 
    return false;
231
 
  }
232
 
  while( fgets(line, 1024, file) )
233
 
    if( sscanf(line, "# columns: %d\n", &c) == 1 )
234
 
      break;
235
 
  if( c!=2 )
236
 
    return false;
237
 
  
238
 
  // read response 
239
 
  for( int i=0 ; i<M ; i++ )
240
 
    if( fscanf(file, " %f %d\n", &(w[i]), &m) !=2 )
241
 
      return false;
242
 
  
243
 
  return true;
244
 
}