File: fitscolour.cpp

package info (click to toggle)
munipack 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,104 kB
  • sloc: cpp: 29,677; sh: 4,909; f90: 2,872; makefile: 278; python: 140; xml: 72; awk: 12
file content (429 lines) | stat: -rw-r--r-- 8,934 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*

  xmunipack - colours

  Copyright © 2009-2014, 2019-22 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


  Color images:

  rawtran -o IMG_5807.fits -X "-q 3 -n 500" IMG_5807.CR2

 * important: -q selects adequate interpolation method,
              -n 500 selects threshold for wavelets


*/


#include "fits.h"
#include <wx/wx.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <wx/tokenzr.h>

/* CIE XYZ, D65 white point */
#define D65_Xn 95.047
#define D65_Yn 100.000
#define D65_Zn 108.883
#define D65_WHITEPOINT_X 0.31271
#define D65_WHITEPOINT_Y 0.32902

/* white-point for Luv, D65 */
#define D65_uw 0.19783
#define D65_vw 0.46832


// --- FitsColour


FitsColour::FitsColour():
  ispace(COLOURSPACE_XYZ),saturation(1.0),
  nitevision(false),meso_level(0.0),meso_width(1.0),
  ncolours(3),nbands(3),trafo(0),level(0),weight(0),
  Sn(Scotopic(D65_Xn,D65_Yn,D65_Zn)/100), uw(D65_uw),vw(D65_vw)
{
  SetTrans(3,3);
}

FitsColour::FitsColour(const wxString& cdatafile, const FitsArray& array):
  ispace(COLOURSPACE_XYZ),saturation(1.0),
  nitevision(false),meso_level(0.0),meso_width(1.0),
  ncolours(0),nbands(0),trafo(0),level(0),weight(0),
  Sn(Scotopic(D65_Xn,D65_Yn,D65_Zn)/100), uw(D65_uw),vw(D65_vw)
{
  Init(cdatafile,array);
}

FitsColour::FitsColour(const FitsColour& c):
  ispace(c.ispace),saturation(c.saturation),
  nitevision(c.nitevision),meso_level(c.meso_level),meso_width(c.meso_width),
  ncolours(c.ncolours),nbands(c.nbands),trafo(0),level(0),weight(0),
  Sn(c.Sn),uw(c.uw),vw(c.vw)
{
  int n = ncolours*nbands;
  trafo = new float[n];
  std::copy(c.trafo,c.trafo+n,trafo);
  level = new float[nbands];
  weight = new float[nbands];
  std::copy(c.level,c.level+nbands,level);
  std::copy(c.weight,c.weight+nbands,weight);
}

FitsColour& FitsColour::FitsColour::operator=(const FitsColour& other)
{
  if( this != &other ) {
    ispace = other.ispace;
    saturation = other.saturation;
    nitevision = other.nitevision;
    meso_level = other.meso_level;
    meso_width = other.meso_width;
    ncolours = other.ncolours;
    nbands = other.nbands;
    Sn = other.Sn;
    uw = other.uw;
    vw = other.vw;

    delete[] trafo;
    delete[] level;
    delete[] weight;

    int n = ncolours*nbands;
    trafo = new float[n];
    level = new float[nbands];
    weight = new float[nbands];
    std::copy(other.trafo,other.trafo+n,trafo);
    std::copy(other.level,other.level+nbands,level);
    std::copy(other.weight,other.weight+nbands,weight);
  }
  return *this;
}


FitsColour::~FitsColour()
{
  delete[] trafo;
  delete[] level;
  delete[] weight;
}

void FitsColour::Init(const wxString& cdatafile, const FitsArray& array)
{
  wxString cs = array.GetKey(FITS_KEY_CSPACE);
  if( cs.IsEmpty() )
    cs = "RGB";
  // RGB is supposed due to compatibility with other software

  SetTrans(cs,cdatafile);
}

float FitsColour::GetWeight(int n) const
{
  wxASSERT(weight && 0 <= n && n < nbands);
  return weight[n];
}

float FitsColour::GetLevel(int n) const
{
  wxASSERT(level && 0 <= n && n < nbands);
  return level[n];
}


void FitsColour::SetWeight(int n, float w)
{
  wxASSERT(weight && 0 <= n && n < nbands);
  weight[n] = w;
}

void FitsColour::SetLevel(int n, float x)
{
  wxASSERT(level && 0 <= n && n < nbands);
  level[n] = x;
}

void FitsColour::SetTrans(int n, int m, float x)
{
  wxASSERT(trafo && 0 <= n && n < ncolours && 0 <= m && m < nbands);
  trafo[m*nbands + n] = x;
}


void FitsColour::SetTrans(int n, int m)
{
  delete[] weight;
  delete[] level;
  delete[] trafo;

  ncolours = n;
  nbands = m;
  weight = new float[n];
  level = new float[n];
  trafo = new float[n*m];
  for(int i = 0; i < n; i++) {
    weight[i] = 1.0;
    level[i] = 0.0;
    for(int j = 0; j < m; j++)
      trafo[j*n + i] = i == j ? 1.0 : 0.0;
  }
}


void FitsColour::SetTrans(const wxString& cs)
{
  cspace = cs;

  if( cspace.Find("XYZ") != wxNOT_FOUND ) {
    size_t n = 3;
    SetTrans(n,n);
    for(size_t i = 0; i < n; i++)
      for(size_t j = 0; j < n; j++)
	SetTrans(i,j,0.0);

    for(size_t i = 0; i < n; i++) {
      SetTrans(i,i,1.0);
      SetLevel(i,0.0);
      SetWeight(i,1.0);
    }
  }
}

void FitsColour::SetTrans(const wxString& cs, const wxString& filename)
{
  cspace = cs;

  wxFileInputStream input(filename);
  wxTextInputStream text(input," ,\t");

  while(input.IsOk() && ! input.Eof()) {
    wxString line,ilabel,olabel;
    int n,m;

    line = text.ReadLine();
    line.Trim();
    if( line.IsEmpty() ) continue;

    wxArrayString a;
    wxStringTokenizer t(line,"'");
    int i = 0;
    while ( t.HasMoreTokens() ) {
      wxString x = t.GetNextToken();
      x.Trim();
      if( ! x.IsEmpty() ) {
	a.Add(x);
      }
      i++;
    }

    if( a.GetCount() == 2 ) {
      ilabel = a[0];
      olabel = a[1];
    }

    if( input.Eof() ) break;
    n = text.Read32();
    if( input.Eof() ) break;
    m = text.Read32();
    if( input.Eof() ) break;

    float *cmatrix = new float[n*m];
    for(int i = 0; i < n*m; i++) {
      if( input.Eof() ) break;
      cmatrix[i]= text.ReadDouble();
    }

    //   wxLogDebug(ilabel + " > " +olabel+ " , " +cspace+ " , " + Type_str(COLORSPACE_XYZ));

    if( ilabel == cspace && olabel == Type_str(COLOURSPACE_XYZ) ) {

      SetTrans(n,m);
      return;
    }
    delete[] cmatrix;
  }


  // proper colorspace data not found
  SetTrans(3,3);
}


int FitsColour::GetColours() const
{
  return ncolours;
}

int FitsColour::GetBands() const
{
  return nbands;
}

float FitsColour::GetTrans(int n, int m) const
{
  wxASSERT(trafo && 0 <= n && n < ncolours && 0 <= m && m < nbands);
  return *(trafo+m*nbands + n);
}

wxString FitsColour::GetColourspace() const
{
  return cspace;
}


float FitsColour::GetWhitePointX() const
{
  float s = 6*uw - 16*vw + 12;
  return 9*uw/s;
}

float FitsColour::GetWhitePointY() const
{
  float s = 6*uw - 16*vw + 12;
  return 4*vw/s;
}


void FitsColour::SetWhitePoint(float x, float y)
{
  wxASSERT(0 < x && x < 1 && 0 < y && y < 1);

  float s = -2*x + 12*y + 3;
  uw = 4*x/s;
  vw = 9*y/s;
}


void FitsColour::SetSaturation(float x)
{
  saturation = x;
}

void FitsColour::SetMesoLevel(float x)
{
  meso_level = x;
}

void FitsColour::SetMesoWidth(float x)
{
  meso_width = x;
}

void FitsColour::SetNiteVision(bool t)
{
  nitevision = t;
}

wxString FitsColour::Type_str(int n)
{
  switch(n){
  case COLOURSPACE_XYZ:      return "XYZ";
  default:        return wxEmptyString;
  }
}


wxArrayString FitsColour::Type_str()
{
  wxArrayString a;

  for(int i = COLOURSPACE_XYZ+1; i < COLOURSPACE_LAST; i++)
    a.Add(Type_str(i));

  return a;
}


float FitsColour::Scotopic(float X, float Y, float Z) const
{
  return 0.36169f*Z + 1.18214f*Y - 0.80498f*X;
}


void FitsColour::Instr_XYZ(long npix, size_t nband, const float **d,
			   float *Z, float *Y, float *X)
{
  wxASSERT(nband == (size_t) nbands);

  long nbytes = npix*sizeof(float);
  memcpy(X,d[0],nbytes);
  memcpy(Y,d[1],nbytes);
  memcpy(Z,d[2],nbytes);
  return;

  if( GetColourspace().Find("XYZ") != wxNOT_FOUND ) {
    wxASSERT(ncolours == nbands);

    long nbytes = npix*sizeof(float);
    memcpy(X,d[0],nbytes);
    memcpy(Y,d[1],nbytes);
    memcpy(Z,d[2],nbytes);
  }
  else {
    wxASSERT(weight && level && trafo);

    // allocate all on heap?

    float cb[ncolours][nbands];
    for(int i = 0; i < ncolours; i++)
      for(int j = 0; j < nbands; j++) {
	cb[i][j] = GetTrans(i,j);
      }

    float b[nbands];
    float c[ncolours];
    for(long i = 0; i < npix; i++) {

      for(int j = 0; j < nbands; j++) {
	b[j] = weight[j]*(d[j][i] - level[j]);
	if( b[j] < 0.0 ) b[j] = 0.0;
      }

      for(int l = 0; l < ncolours; l++) {
	float s = 0.0;
	for(int j = 0; j < nbands; j++)
	  s = s + cb[l][j]*b[j];
	c[l] = s;
      }

      X[i] = c[0];
      Y[i] = c[1];
      Z[i] = c[2];
    }
  }

}


void FitsColour::NiteVision(float X, float Z, float& x, float& y, float& Y) const
{
  const float xw = D65_WHITEPOINT_X;
  const float yw = D65_WHITEPOINT_Y;
  const float xyw = xw / yw;

  // simplified transformation for photo-,meso-, and scotopic
  float S = Scotopic(X,Y,Z) / Sn;
  float w = (1.0f + tanh((Y - meso_level)/meso_width)) / 2.0f;

  float Yw = Y*w;
  float Sw = S*(1 - w);
  float D = Yw/y + Sw/yw;

  Y = Yw + Sw;
  x = ((x/y)*Yw  + xyw*Sw) / D;
  y = Y / D;
}