File: img_cs_base.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (248 lines) | stat: -rw-r--r-- 13,273 bytes parent folder | download | duplicates (8)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef IMG_CS_BASE_H_
#define IMG_CS_BASE_H_

// warning: temporary name/location, will change in near future

#include "img/img.h"

namespace img {

// RGB: GAMMA -> LINEAR 
template<int Channels, typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_gamma_precompensated_rgb_to_linear_rgb(const img::Image<Channels,ScalarType1,Safe1> &gamma_precompensated_rgb_image, img::Image<Channels,ScalarType2,Safe2> &linear_rgb_image)
{
  assert(gamma_precompensated_rgb_image.isValid());
  assert(!gamma_precompensated_rgb_image.attributes.hasColorspace(SRGB));
  assert(gamma_precompensated_rgb_image.attributes.hasRange(0.0,1.0));
  ScalarType2 old_gamma;
  gamma_precompensated_rgb_image.attributes.getGamma(old_gamma);
  assert((old_gamma>0.0)&&(old_gamma<1.0));
  if(Safe1 || Safe2){
    if(!gamma_precompensated_rgb_image.isValid())  throw ImageException("Invalid rgb image");
    if(gamma_precompensated_rgb_image.attributes.hasColorspace(SRGB)) throw ImageException("Invalid colorspace attribute");
    if(!gamma_precompensated_rgb_image.attributes.hasRange(0,1)) throw ImageException("Invalid range attribute");
    if(!((old_gamma>0.0)&&(old_gamma<1.0))) throw ImageException("Invalid gamma attribute");
  }
  linear_rgb_image.setZero(gamma_precompensated_rgb_image.width(),gamma_precompensated_rgb_image.height());
  linear_rgb_image.attributes=gamma_precompensated_rgb_image.attributes;
  
  for(int channel=0; channel<Channels; ++channel)
    for(int x_coord=0; x_coord<gamma_precompensated_rgb_image.width(); ++x_coord)
      for(int y_coord=0; y_coord<gamma_precompensated_rgb_image.height(); ++y_coord){
        linear_rgb_image.setValue(x_coord,y_coord,channel, pow(ScalarType2(gamma_precompensated_rgb_image.getValue(x_coord,y_coord,channel)),ScalarType2(1.0)/old_gamma));
      }
  linear_rgb_image.attributes.setGamma(ScalarType2(1.0));
}

// RGB: LINEAR -> GAMMA
// when converting from linear to gamma precompensated the gamma parameter mus be in range (0.0,1.0)
template<int Channels, typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_linear_rgb_to_gamma_precompensated_rgb(const img::Image<Channels,ScalarType1,Safe1> &linear_rgb_image, img::Image<Channels,ScalarType2,Safe2> &gamma_precompensated_rgb_image, ScalarType2 gamma=ScalarType2(1.0/2.2))
{
  assert(linear_rgb_image.isValid());
  assert(!linear_rgb_image.attributes.hasColorspace(SRGB));
  assert(linear_rgb_image.attributes.hasRange(0.0,1.0));
  assert(linear_rgb_image.attributes.hasGamma(1.0));
  assert((gamma>0.0)&&(gamma<1.0));
  if(Safe1 || Safe2){
    if(!linear_rgb_image.isValid())  throw img::ImageException("Invalid rgb image");
    if(linear_rgb_image.attributes.hasColorspace(SRGB)) throw ImageException("Invalid colorspace attribute");
    if(!linear_rgb_image.attributes.hasRange(0.0,1.0)) throw ImageException("Invalid range attribute");
    if(!linear_rgb_image.attributes.hasGamma(1.0)) throw ImageException("Invalid gamma attribute");
    if(!((gamma>0.0)&&(gamma<1.0))) throw ImageException("Invalid gamma parameter");
  }
  gamma_precompensated_rgb_image.setZero(linear_rgb_image.width(),linear_rgb_image.height());
  gamma_precompensated_rgb_image.attributes=linear_rgb_image.attributes;
  
  for(int channel=0; channel<Channels; ++channel)
    for(int x_coord=0; x_coord<linear_rgb_image.width(); ++x_coord)
      for(int y_coord=0; y_coord<linear_rgb_image.height(); ++y_coord){
        gamma_precompensated_rgb_image.setValue(x_coord,y_coord,channel, pow(ScalarType2(linear_rgb_image.getValue(x_coord,y_coord,channel)),gamma));
      }
  gamma_precompensated_rgb_image.attributes.setGamma(gamma);
}

// SRGB: GAMMA -> LINEAR 
template<int Channels, typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_gamma_precompensated_srgb_to_linear_srgb(const img::Image<Channels,ScalarType1,Safe1> &gamma_precompensated_srgb_image, img::Image<Channels,ScalarType2,Safe2> &linear_srgb_image)
{
  assert(gamma_precompensated_srgb_image.isValid());
  assert(gamma_precompensated_srgb_image.attributes.hasColorspace(SRGB));
  assert(gamma_precompensated_srgb_image.attributes.hasRange(0.0,1.0));
  assert(!gamma_precompensated_srgb_image.attributes.hasGamma(1.0));
  if(Safe1 || Safe2){
    if(!gamma_precompensated_srgb_image.isValid())  throw ImageException("Invalid rgb image");
    if(!gamma_precompensated_srgb_image.attributes.hasColorspace(SRGB)) throw ImageException("Invalid colorspace attribute");
    if(!gamma_precompensated_srgb_image.attributes.hasRange(0,1)) throw ImageException("Invalid range attribute");
    if(gamma_precompensated_srgb_image.attributes.hasGamma(1.0)) throw ImageException("Invalid gamma attribute");
  }
  linear_srgb_image.setZero(gamma_precompensated_srgb_image.width(),gamma_precompensated_srgb_image.height());
  linear_srgb_image.attributes=gamma_precompensated_srgb_image.attributes;
  
  for(int channel=0; channel<Channels; ++channel)
    for(int x_coord=0; x_coord<gamma_precompensated_srgb_image.width(); ++x_coord)
      for(int y_coord=0; y_coord<gamma_precompensated_srgb_image.height(); ++y_coord){
        ScalarType2 value = ScalarType2(gamma_precompensated_srgb_image.getValue(x_coord,y_coord,channel));
        if(value<=ScalarType2(0.04045))
          value = value / ScalarType2(12.92);
        else
          value = pow( (value+ScalarType2(0.055)) / ScalarType2(1.055), ScalarType2(2.4) );
        linear_srgb_image.setValue(x_coord, y_coord, channel, value);
      }
  linear_srgb_image.attributes.setGamma(ScalarType2(1.0));
}

// SRGB: LINEAR -> GAMMA
template<int Channels, typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_linear_srgb_to_gamma_precompensated_srgb(const img::Image<Channels,ScalarType1,Safe1> &linear_srgb_image, img::Image<Channels,ScalarType2,Safe2> &gamma_precompensated_srgb_image)
{
  assert(linear_srgb_image.isValid());
  assert(linear_srgb_image.attributes.hasColorspace(SRGB));
  assert(linear_srgb_image.attributes.hasRange(0.0,1.0));
  assert(linear_srgb_image.attributes.hasGamma(1.0));
  if(Safe1 || Safe2){
    if(!linear_srgb_image.isValid())  throw img::ImageException("Invalid rgb image");
    if(!linear_srgb_image.attributes.hasColorspace(SRGB)) throw ImageException("Invalid colorspace attribute");
    if(!linear_srgb_image.attributes.hasRange(0.0,1.0)) throw ImageException("Invalid range attribute");
    if(!linear_srgb_image.attributes.hasGamma(1.0)) throw ImageException("Invalid gamma attribute");
  }
  gamma_precompensated_srgb_image.setZero(linear_srgb_image.width(),linear_srgb_image.height());
  gamma_precompensated_srgb_image.attributes=linear_srgb_image.attributes;
  
  for(int channel=0; channel<Channels; ++channel)
    for(int x_coord=0; x_coord<linear_srgb_image.width(); ++x_coord)
      for(int y_coord=0; y_coord<linear_srgb_image.height(); ++y_coord){
        ScalarType2 value = ScalarType2(linear_srgb_image.getValue(x_coord,y_coord,channel));
        if(value<=ScalarType2(0.0031308))
          value =  value * ScalarType2(12.92);
        else
          value = (ScalarType2(1.055) * pow(value, ScalarType2(1.0/2.4))) - ScalarType2(0.055);
        gamma_precompensated_srgb_image.setValue(x_coord, y_coord, channel, value);
      }
  gamma_precompensated_srgb_image.attributes.setGamma(ScalarType2(2.2)); // approssimation
}

// SRGB -> XYZ
template<typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_srgb_to_xyz(const img::Image<3,ScalarType1,Safe1> &rgb_image, img::Image<3,ScalarType2,Safe2> &xyz_image)
{
  assert( rgb_image.isValid());
  assert( rgb_image.attributes.hasRange(0.0,1.0));
  assert( rgb_image.attributes.hasGamma(1.0));
  assert( rgb_image.attributes.hasColorspace(img::RGB) );
  if(Safe1 || Safe2){
    if(!rgb_image.isValid())  throw img::ImageException("Invalid rgb image");
    if(!rgb_image.attributes.hasRange(0.0,1.0))  throw img::ImageException("Invalid range attribute");
    if(!rgb_image.attributes.hasGamma(1.0))  throw img::ImageException("Invalid gamma attribute");
    if(!rgb_image.attributes.hasColorspace(img::RGB))  throw img::ImageException("Invalid colorspace attribute");
  }
  xyz_image.setZero(rgb_image.width(),rgb_image.height());
  xyz_image.attributes=rgb_image.attributes;

  for(int x_coord=0; x_coord<rgb_image.width(); ++x_coord)
    for(int y_coord=0; y_coord<rgb_image.height(); ++y_coord){
      const ScalarType2 R = static_cast<ScalarType2>(rgb_image.getValue(x_coord,y_coord,0));
      const ScalarType2 G = static_cast<ScalarType2>(rgb_image.getValue(x_coord,y_coord,1));
      const ScalarType2 B = static_cast<ScalarType2>(rgb_image.getValue(x_coord,y_coord,2));

      xyz_image.setValue(x_coord,y_coord,0, ( 0.412453f*R + 0.357580f*G + 0.180423f*B ) );
      xyz_image.setValue(x_coord,y_coord,1, ( 0.212671f*R + 0.715160f*G + 0.072169f*B ) );
      xyz_image.setValue(x_coord,y_coord,2, ( 0.019334f*R + 0.119193f*G + 0.950227f*B ) );
    }
    xyz_image.setColorspace(img::CIE_XYZ);
}

// XYZ -> SRGB
template<typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
inline void convert_xyz_to_rgb(const img::Image<3,ScalarType1,Safe1> &xyz_image, img::Image<3,ScalarType2,Safe2> &rgb_image)
{
  assert( xyz_image.isValid());
  assert( xyz_image.attributes.hasRange(0.0,1.0));
  assert( xyz_image.attributes.hasGamma(1.0));
  assert( xyz_image.attributes.hasColorspace(img::CIE_XYZ) );
  
  if(Safe1 || Safe2){
    if(!xyz_image.isValid())  throw img::ImageException("Invalid xyz image");
    if(!xyz_image.attributes.hasRange(0.0,1.0))  throw img::ImageException("Invalid range attribute");
    if(!xyz_image.attributes.hasGamma(1.0))  throw img::ImageException("Invalid gamma attribute");
    if(!rgb_image.attributes.hasColorspace(img::CIE_XYZ))  throw img::ImageException("Invalid colorspace attribute");
  }
  rgb_image.setZero(xyz_image.width(),xyz_image.height());
  rgb_image.attributes=xyz_image.attributes;

  for(int x=0; x<xyz_image.width(); ++x)
    for(int y=0; y<xyz_image.height(); ++y){
      const ScalarType2 X = static_cast<ScalarType2>(xyz_image.getValue(x,y,0));
      const ScalarType2 Y = static_cast<ScalarType2>(xyz_image.getValue(x,y,1));
      const ScalarType2 Z = static_cast<ScalarType2>(xyz_image.getValue(x,y,2));
      
      const ScalarType2 R = 3.240479f*X + -1.537150f*Y + -0.498535f*Z;
      const ScalarType2 G = -0.969256f*X + 1.875992f*Y + 0.041556f*Z;
      const ScalarType2 B = 0.055648f*X + -0.204043f*Y + 1.057311f*Z;
      
      rgb_image.setValue(x,y,0, ( R<0.0f?0.0f:(R>1.0f?1.0f:R) ) );
      rgb_image.setValue(x,y,1, ( G<0.0f?0.0f:(G>1.0f?1.0f:G) ) );
      rgb_image.setValue(x,y,2, ( B<0.0f?0.0f:(B>1.0f?1.0f:B) ) );
    }
    rgb_image.setColorspace(img::RGB);
}

///ora nn ho tempo:
//// AltriRGB -> XYZ
//// XYZ -> AltriRGB
//// XYZ -> LAB
//// LAB -> XYZ
//template<typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
//inline void convert_xyz_to_lab(const img::Image<3,ScalarType1,Safe1> &xyz_image, img::Image<3,ScalarType2,Safe2> &lab_image)
//{
//  assert(xyz_image.isValid());
//  if(Safe1 || Safe2){
//    if(!xyz_image.isValid())  throw img::ImageException("Invalid xyz image");
//  }
//  lab_image.setZero(xyz_image.width(),xyz_image.height());
//  
//  const ScalarType2 one_third=0.33333333333333333333333333333333333333333333333333333333333333f;
//  const ScalarType2 Xn = 0.9513f;
//  const ScalarType2 Yn = 1.000f;
//  const ScalarType2 Zn = 1.0886f;
//  
//  for(int x=0; x<xyz_image.width(); ++x)
//    for(int y=0; y<xyz_image.height(); ++y){
//      const ScalarType2 X_third = pow(static_cast<ScalarType2>(xyz_image.getValue(x,y,0))/Xn,one_third);
//      const ScalarType2 Y = static_cast<ScalarType2>(xyz_image.getValue(x,y,1))/Yn;
//      const ScalarType2 Y_third = pow(Y,one_third);
//      const ScalarType2 Z_third = pow(static_cast<ScalarType2>(xyz_image.getValue(x,y,2))/Zn,one_third);
//
//      lab_image.setValue(x,y,0, (Y > 0.008856f)?((116.0f*(Y_third)) - 16.0f):(903.3f*Y) );
//      lab_image.setValue(x,y,1, 500.0f * ((X_third) - (Y_third)) );
//      lab_image.setValue(x,y,2, 200.0f * ((Y_third) - (Z_third)) );
//    }
//}
//
//template<typename ScalarType1, bool Safe1,typename ScalarType2, bool Safe2> 
//inline void convert_lab_to_xyz(const img::Image<3,ScalarType1,Safe1> &lab_image, img::Image<3,ScalarType2,Safe2> &xyz_image)
//{
//  assert(0); // sistemare attributi
//  assert(lab_image.isValid());
//  if(Safe1 || Safe2){
//    if(!lab_image.isValid())  throw img::ImageException("Invalid lab image");
//  }
//  xyz_image.setZero(lab_image.width(),lab_image.height());
//
//  const ScalarType2 Xn = 0.9513f;
//  const ScalarType2 Yn = 1.000f;
//  const ScalarType2 Zn = 1.0886f;
//
//  for(int x=0; x<lab_image.width(); ++x)
//    for(int y=0; y<lab_image.height(); ++y){
//      const ScalarType2 P = (static_cast<ScalarType2>(lab_image.getValue(x,y,0))+16.0f)/116.0f;
//
//      xyz_image.setValue(x,y,0, Xn * pow(P + (lab_image.getValue(x,y,1)/500.0f), 3.0f) );
//      xyz_image.setValue(x,y,1, Yn * pow(P, 3.0f) );
//      xyz_image.setValue(x,y,2, Zn * pow(P - (lab_image.getValue(x,y,2)/200.0f), 3.0f) );
//    }
//}

} // end namespace img

#endif /*IMG_CS_BASE_H_*/