File: fitsgeometry.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 (333 lines) | stat: -rw-r--r-- 7,871 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
/*

  xmunipack - fits image geometry

  Copyright © 2018-2022 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/>.

*/


#include "fits.h"
#include <cfloat>
#include <wx/wx.h>
#include <algorithm>

#ifdef __WXDEBUG__
#include <wx/debug.h>
#include <wx/txtstrm.h>
#include <wx/wfstream.h>
#endif



using namespace std;


// ------------   FitsGeometry


FitsGeometry::FitsGeometry(const FitsArray& a): FitsArray(a)
{
  wxASSERT(IsOk());
}

void FitsGeometry::ShrinkSubwin(int shrink, int x, int y, int w, int h,
				int iwidth, int iheight, float *idata) const
{
  //wxLogDebug("FitsGeometry::ShrinkSubwin");

  wxASSERT(shrink >= 1 && iwidth > 0 && iheight > 0 && x >= 0 && y >= 0 &&
	   w > 0 && h > 0);

  const float *array = PixelData();
  int width = GetWidth();
  int height = GetHeight();
  size_t npixels = width*height;
  size_t ipixels = iwidth*iheight;

  const int imin = x / shrink;
  const int jmin = y / shrink;
  const int imax = min(imin + w / shrink,iwidth);
  const int jmax = min(jmin + h / shrink,iheight);

  for(int k = 0; k < GetDepth(); k++) {
    const float *src = array + k*npixels;
    float *dst = idata + k*ipixels;
    for(int j = jmin; j < jmax; j++) {
      int j0 = shrink*j;
      float *drow = dst + j*iwidth;
      for(int i = imin; i < imax; i++) {
	int i0 = shrink*i;
	drow[i] = MeanSquare(src,width,height,i0,j0,shrink);
      }
    }
  }
}



FitsArray FitsGeometry::GetSubArray(int xoff, int yoff, int w, int h)
{
  long npixels = w*h;
  long *ns = new long[2];
  float *a = new float[npixels];

  ns[0] = w;
  ns[1] = h;

  const float *array = PixelData();
  wxASSERT(array);
  int width = GetWidth();

  switch(Naxis()) {

  case 2:

    /*
    wxLogDebug("FitsGeometry::GetSubArray %d %d %d %d %d %d",xoff,yoff,w,h,
	       int(data->naxes[0]),int(data->naxes[1]));
    */
    wxASSERT(xoff >= 0 && yoff >= 0 &&
	     xoff + w <= GetWidth() && yoff + h <= GetHeight());

    for(int j = 0; j < h; j++) {
      const float *src = array + (yoff + j)*width + xoff;
      copy(src,src+w,a+j*w);
    }

    break;

  default:

    wxFAIL_MSG("FitsGeom::SubArray arbitrary dimension isn't implemented yet.");
    break;
  }

  return FitsArray(header,GetHduType(),2,ns,a);
}

void FitsGeometry::SetSubArray(int xoff, int yoff, const FitsArray& sub)
{
  // JUST ONLY *TWO* DIMENSIONS ARE IMPLEMENTED

  wxASSERT(sub.Naxis() == 2 && Naxis() == 2);
  float *array = GetData();
  wxASSERT(array);
  int width = GetWidth();

  /*
  wxLogDebug("FitsGeometry::SetSubArray X: %d %d %d",
	     (int)xoff,(int)sub.Naxes(0),int(data->naxes[0]));
  wxLogDebug("FitsGeometry::SetSubArray Y: %d %d %d",
	     (int)yoff,(int)sub.Naxes(1),int(data->naxes[1]));
  */
  wxASSERT(0 <= xoff && (xoff + sub.Naxes(0)) <= GetWidth());
  wxASSERT(0 <= yoff && (yoff + sub.Naxes(1)) <= GetHeight());

  int w = sub.GetWidth();
  int h = sub.GetHeight();
  const float *a = sub.PixelData();
  wxASSERT(a);

  /*
  wxLogDebug("%d %d %d %d %d %d %d %d",(int)h,(int)sub.Naxes(0),(int)w,
	     (int)sub.Naxes(1),(int)data->naxes[0],(int)xoff,
	     (int)data->naxes[1],(int)yoff);
  */

  for(int j = 0; j < h; j++) {
    const float *src = a + j*w;
    float *dest = array + (yoff + j)*width + xoff;
    copy(src,src+w,dest);
  }
}


float FitsGeometry::MeanLine(int x, int w) const
{
  const float *array = PixelData();
  wxASSERT(array && Naxis() == 1);

  int i0 = x;
  int dx = w / 2;

  int i1 = max(i0 - dx,0);
  int i2 = min(long(i0 + dx),Naxes(0));

  float s = 0;
  float n = 0;
  for(int i = i1; i <= i2; i++) {
    // s = s + Pixel(i);
    // wxASSERT(0 <= i && i < data->npixels);
    //    s = s + *(data->array + i);
    s = s + *(array + i);
    n = n + 1;
  }

  if( n > 0 ) return s/n; else return 0.0;
}


float FitsGeometry::MeanRect(int x, int y, int w, int h) const
{
  return MeanRect(PixelData(),Naxes(0),Naxes(1),x,y,w,h);
}

float FitsGeometry::MeanRect(const float *data, long width, long height,
			     int x, int y, int w, int h) const
{
  int i0 = x;
  int j0 = y;
  int dx = w / 2;
  int dy = h / 2;

  int i1 = max(i0 - dx,0);
  int j1 = max(j0 - dy,0);
  int i2 = min(i0 + dx,int(width)-1);
  int j2 = min(j0 + dy,int(height)-1);

  float s = 0.0;
  int n = (j2 - j1 + 1)*(i2 - i1 + 1);
  const float *ai = data + i1;
  for(int j = j1; j <= j2; j++) {
    const float *a = ai + j*width;
    for(int i = i1; i <= i2; i++)
      s += *a++;
  }
  return s / n;
}

float FitsGeometry::MeanSquare(const float *pic, int width, int height, int x, int y, int d) const
{
  wxASSERT(pic && x >= 0 && y >= 0 && d >= 1);

  const int i1 = x;
  const int j1 = y;
  const int i2 = min(x+d,width);
  const int j2 = min(y+d,height);

  float s = 0.0;
  for(int j = j1; j < j2; j++) {
    const float *row = pic + j*width;
    for(int i = i1; i < i2; i++)
      s += row[i];
  }
  int n = (j2 - j1)*(i2 - i1);
  return n > 0 ? s / n : 0;
}


float FitsGeometry::MeanRect_debug(int x, int y, int w, int h) const
{
  const float *array = PixelData();
  wxASSERT(array && Naxis() == 2);
  int width = Naxes(0);
  //  int height = Naxes(1);

  /*
  const int dmax = max(w,h)/2;

  int i0 = int(x + 0.5);
  int j0 = int(y + 0.5);
  int dx = min(int(w/2.0 - 0.5),dmax);
  int dy = min(int(h/2.0 - 0.5),dmax);

  wxASSERT(dx >= 0 && dy >= 0);

  int i1 = max(i0 - dx,0);
  int j1 = max(j0 - dy,0);
  int i2 = min(long(i0 + dx),data->naxes[0]-1);
  int j2 = min(long(j0 + dy),data->naxes[1]-1);
  */

  int i0 = x;
  int j0 = y;
  int dx = w / 2;
  int dy = h / 2;

  wxASSERT(dx >= 0 && dy >= 0);

  int i1 = max(i0 - dx,0);
  int j1 = max(j0 - dy,0);
  int i2 = min(long(i0 + dx),Naxes(0)-1);
  int j2 = min(long(j0 + dy),Naxes(1)-1);
  //  int i2 = min(long(i0 + dx),data->naxes[0]-1);
  //  int j2 = min(long(j0 + dy),data->naxes[1]-1);


  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!

  //  if( dx > 0 && dy > 0 )
  //    wxLogDebug(_("%d %d"),dx,dy);

//   int ww = i2 - i1 + 1;
//   int hh = j2 - j1 + 1;
//   ww = dx;
//   hh = dy;
//   if(  ww > 0 && hh > 0 ) {
//     float a[ww*hh];
//     const float *array = data->array;
//     wxASSERT(array);
//     for(int j = 0; j < hh; j++)
//       memcpy(a+j*ww,array+(j1+j)*data->naxes[0]+i1,ww*sizeof(float));

//     //  return a[0];
//     return FitsArrayStat::QMed(ww*hh,a,max((ww*hh)/2,0));
//   }
//   else
//     return Pixel(i1,j1);


  ///!!!!!!!!!!!!!!!!!!!!!!!!!!!

  float s = 0.0;

#ifdef __WXDEBUG__
  int n = 0;
#else
  int n = (j2 - j1 + 1)*(i2 - i1 + 1);
#endif

  //  float *ai = data->array + i1;
  const float *ai = array + i1;
  //  int idim = data->naxes[0];
  int idim = width;
  for(int j = j1; j <= j2; j++) {
    //    float *a = data->array + j*data->naxes[0] + i1;
    const float *a = ai + j*idim;
    for(int i = i1; i <= i2; i++) {
      //      s = s + Pixel(i,j);
      //      wxASSERT(0 <= i && i < data->npixels);
      s += *a++;
      //      s = s + *(a + i);
      //      wxLogDebug("%f",(float)*(a + i));
#ifdef __WXDEBUG__
      n = n + 1;
#endif
    }
  }
#ifdef __WXDEBUG__
  int nx = j2 - j1;
  int ny = i2 - i1;
  int nn = (nx + 1)*(ny + 1);
  if( n != nn )
    wxLogDebug("FitsGeometry::MeanRect: %d %d %d %d",n,nn,nx,ny);
#endif
  wxASSERT(n == (j2 - j1 + 1)*(i2 - i1 +1) );
  if( n > 0 ) return s/n; else return 0.0;
}