GFitsDblImage.cpp
23 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/***************************************************************************
* GFitsDblImage.cpp - FITS double precision image class *
* ----------------------------------------------------------------------- *
* copyright : (C) 2008 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* ----------------------------------------------------------------------- *
***************************************************************************/
/* __ Includes ___________________________________________________________ */
#include <iostream>
#include "GException.hpp"
#include "GFitsDblImage.hpp"
/* __ Namespaces _________________________________________________________ */
/* __ Method name definitions ____________________________________________ */
#define G_OPERATOR2 "GFitsDblImage::operator() (int, int)"
#define G_OPEN "GFitsDblImage::open(fitsfile*)"
#define G_SAVE "GFitsDblImage::save()"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= GFitsDblImage constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Constructor
***************************************************************************/
GFitsDblImage::GFitsDblImage() : GFitsImage()
{
// Initialise class members for clean destruction
init_members();
// Set bitpix
m_bitpix = -64;
// Return
return;
}
/***********************************************************************//**
* @brief Constructor
*
* @param naxis Image dimension (0,1,2,3,4)
* @param naxes Number of pixels in each dimension
*
* Construct instance of GFitsDblImage by specifying the image dimension and
* the number of pixels in each dimension. Note that this constructor does
* not allocate any memory for the actual image.
***************************************************************************/
GFitsDblImage::GFitsDblImage(int naxis, const int* naxes) : GFitsImage(naxis, naxes)
{
// Initialise class members for clean destruction
init_members();
// Set bitpix
m_bitpix = -64;
// Return
return;
}
/***********************************************************************//**
* @brief Constructor
*
* @param naxis Image dimension (0,1,2,3,4)
* @param naxes Number of pixels in each dimension
* @param pixels Pointer to image pixel array
*
* Construct instance of GFitsDblImage by specifying the image dimension, the
* number of pixels in each dimension and an image pixel array. If the
* 'pixels' pointer is valid (i.e. not NULL) the pixel data is indeed copied
* by this constructor. In case of memory shortage a combination of
* GFitsDblImage::GFitsDblImage(int naxis, const int* naxes)
* and
* GFitsDblImage::link(double* pixels)
* may be prefered to avoid duplication of the pixel data.
***************************************************************************/
GFitsDblImage::GFitsDblImage(int naxis, const int* naxes, const double* pixels)
: GFitsImage(naxis, naxes)
{
// Initialise class members for clean destruction
init_members();
// Copy pixels into object
if (m_num_pixels > 0 && pixels != NULL) {
m_pixels = new double[m_num_pixels];
memcpy(m_pixels, pixels, m_num_pixels*sizeof(double));
}
// Set bitpix
m_bitpix = -64;
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param image FITS image which should be used for construction
***************************************************************************/
GFitsDblImage::GFitsDblImage(const GFitsDblImage& image) : GFitsImage(image)
{
// Initialise class members for clean destruction
init_members();
// Copy members
copy_members(image);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GFitsDblImage::~GFitsDblImage()
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= GFitsDblImage operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param image FITS image to be assigned
***************************************************************************/
GFitsDblImage& GFitsDblImage::operator= (const GFitsDblImage& image)
{
// Execute only if object is not identical
if (this != &image) {
// Copy base class members
this->GFitsImage::operator=(image);
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(image);
} // endif: object was not identical
// Return this object
return *this;
}
/***********************************************************************//**
* @brief 1D image pixel access operator
*
* @param[in] ix Pixel index
*
* @exception GException::fits_wrong_image_operator
* Image is not a 1D image
*
* Provides access to a pixel of a 1D image. No range checking is performed.
* Use the at(ix) method if range checking is required.
***************************************************************************/
double& GFitsDblImage::operator() (const int& ix)
{
// Operator is only valid for 1D images
if (m_naxis != 1)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 1);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) fetch_pixels();
// Return image pixel
return m_pixels[ix];
}
/***********************************************************************//**
* @brief 1D image pixel access operator (const variant)
*
* @param[in] ix Pixel index
*
* @exception GException::fits_wrong_image_operator
* Image is not a 1D image
*
* Provides access to a pixel of a 1D image. No range checking is performed.
* Use the at(ix) method if range checking is required.
***************************************************************************/
const double& GFitsDblImage::operator() (const int& ix) const
{
// Operator is only valid for 1D images
if (m_naxis != 1)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 1);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) ((GFitsDblImage*)this)->fetch_pixels();
// Return image pixel
return m_pixels[ix];
}
/***********************************************************************//**
* @brief 2D image pixel access operator
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 2D image
*
* Provides access to a pixel of a 2D image. No range checking is performed.
* Use the at(ix,iy) method if range checking is required.
***************************************************************************/
double& GFitsDblImage::operator() (const int& ix, const int& iy)
{
// Operator is only valid for 2D images
if (m_naxis != 2)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 2);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) fetch_pixels();
// Calculate pixel offset
int offset = ix + iy * m_naxes[0];
// Return image pixel
return m_pixels[offset];
}
/***********************************************************************//**
* @brief 2D image pixel access operator (const variant)
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 2D image
*
* Provides access to a pixel of a 2D image. No range checking is performed.
* Use the at(ix,iy) method if range checking is required.
***************************************************************************/
const double& GFitsDblImage::operator() (const int& ix, const int& iy) const
{
// Operator is only valid for 2D images
if (m_naxis != 2)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 2);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) ((GFitsDblImage*)this)->fetch_pixels();
// Calculate pixel offset
int offset = ix + iy * m_naxes[0];
// Return image pixel
return m_pixels[offset];
}
/***********************************************************************//**
* @brief 3D image pixel access operator
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
* @param[in] iz Pixel index in third dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 3D image
*
* Provides access to a pixel of a 3D image. No range checking is performed.
* Use the at(ix,iy,iz) method if range checking is required.
***************************************************************************/
double& GFitsDblImage::operator() (const int& ix, const int& iy, const int& iz)
{
// Operator is only valid for 3D images
if (m_naxis != 3)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 3);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) fetch_pixels();
// Calculate pixel offset
int offset = ix + m_naxes[0] * (iy + iz * m_naxes[1]);
// Return image pixel
return m_pixels[offset];
}
/***********************************************************************//**
* @brief 3D image pixel access operator (const variant)
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
* @param[in] iz Pixel index in third dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 3D image
*
* Provides access to a pixel of a 3D image. No range checking is performed.
* Use the at(ix,iy,iz) method if range checking is required.
***************************************************************************/
const double& GFitsDblImage::operator() (const int& ix, const int& iy, const int& iz) const
{
// Operator is only valid for 3D images
if (m_naxis != 3)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 3);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) ((GFitsDblImage*)this)->fetch_pixels();
// Calculate pixel offset
int offset = ix + m_naxes[0] * (iy + iz * m_naxes[1]);
// Return image pixel
return m_pixels[offset];
}
/***********************************************************************//**
* @brief 4D image pixel access operator
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
* @param[in] iz Pixel index in third dimension
* @param[in] it Pixel index in forth dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 4D image
*
* Provides access to a pixel of a 4D image. No range checking is performed.
* Use the at(ix,iy,iz,it) method if range checking is required.
***************************************************************************/
double& GFitsDblImage::operator() (const int& ix, const int& iy, const int& iz, const int& it)
{
// Operator is only valid for 4D images
if (m_naxis != 4)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 4);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) fetch_pixels();
// Calculate pixel offset
int offset = ix + m_naxes[0] * (iy + m_naxes[1] * (iz + it * m_naxes[2]));
// Return image pixel
return m_pixels[offset];
}
/***********************************************************************//**
* @brief 4D image pixel access operator (const variant)
*
* @param[in] ix Pixel index in first dimension
* @param[in] iy Pixel index in second dimension
* @param[in] iz Pixel index in third dimension
* @param[in] it Pixel index in forth dimension
*
* @exception GException::fits_wrong_image_operator
* Image is not a 4D image
*
* Provides access to a pixel of a 4D image. No range checking is performed.
* Use the at(ix,iy,iz,it) method if range checking is required.
***************************************************************************/
const double& GFitsDblImage::operator() (const int& ix, const int& iy, const int& iz, const int& it) const
{
// Operator is only valid for 4D images
if (m_naxis != 4)
throw GException::fits_wrong_image_operator(G_OPERATOR2, m_naxis, 4);
// If image pixels are not available then allocate them now
if (m_pixels == NULL) ((GFitsDblImage*)this)->fetch_pixels();
// Calculate pixel offset
int offset = ix + m_naxes[0] * (iy + m_naxes[1] * (iz + it * m_naxes[2]));
// Return image pixel
return m_pixels[offset];
}
/*==========================================================================
= =
= GFitsDblImage public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Open FITS image
*
* @param fptr FITS file pointer
*
* Open FITS image in FITS file. Opening means connecting the FITS file
* pointer to the image and reading the image and axes dimensions.
***************************************************************************/
void GFitsDblImage::open(__fitsfile* fptr)
{
// Initialise base class members
GFitsImage::init_members();
// Initialise class members
init_members();
// Open image
open_image(fptr);
// Return
return;
}
/***********************************************************************//**
* @brief Link external pixel array to image
*
* @param pixels Pointer to external pixel array that should be linked
*
* This method allows to use an externally allocated pixel array within
* the class. No control over the existence of this pixel array is performed
* hence the user has to assure its existence over the entire lifetime of
* the class instance. Using of an externally allocated array may be
* preferred in case of large image arrays that are created outside the
* class. In this case linking prevents a duplication of the image array.
* In all other cases, linking should be better avoided.
***************************************************************************/
void GFitsDblImage::link(double* pixels)
{
// Free any existing pixels
if (!m_linked) {
if (m_pixels != NULL) delete [] m_pixels;
}
// Link pixel array to image
m_pixels = pixels;
// Flag that pixels have been linked
m_linked = 1;
// Return
return;
}
/***********************************************************************//**
* @brief Save FITS image
*
* Saves the image into the FITS file.
***************************************************************************/
void GFitsDblImage::save(void)
{
// Save a double precision image
save_image(__TDOUBLE, m_pixels);
// Return
return;
}
/***********************************************************************//**
* @brief Close FITS image
*
* Closing a FITS image resets the object into its initial state. Closing
* does NOT save the image into the FITS file. Use the save method for this
* purpose.
***************************************************************************/
void GFitsDblImage::close(void)
{
// Free members
free_members();
// Initialise members
init_members();
// Free base class members
GFitsImage::free_members();
// Initialise base class members
GFitsImage::init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Clone FITS image
*
* Cloning provides a copy of the FITS file. Cloning is used to allocate
* derived classes into a base class pointer.
***************************************************************************/
GFitsDblImage* GFitsDblImage::clone(void) const
{
// Clone this image
return new GFitsDblImage(*this);
}
/***********************************************************************//**
* @brief Set NULL value
*
* @param value Value to which NULL pixels should be set
*
* NOTE: FOR THE MOMENT THE IMAGE IS NOT RE-LOADED IF THE NULL VALUE IS
* CHANGED !!!!
***************************************************************************/
void GFitsDblImage::set_nullval(const double* value)
{
// If NULL value is empty then reset the NULL value
if (value == NULL) {
if (m_nulval != NULL) delete m_nulval;
m_nulval = NULL;
}
// ... otherwise copy value into NULL value
else {
if (m_nulval != NULL) delete m_nulval;
m_nulval = new double;
*m_nulval = *value;
}
// Re-load image
// NOTE: THIS WILL LEAD TO A LOSS OF MODIFICATIONS; ISSUE SAVE BEFORE !!!
//if (m_data != NULL) {
// //save();
// load();
//}
// Return
return;
}
/*==========================================================================
= =
= GFitsDblImage private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GFitsDblImage::init_members(void)
{
// Initialise members
m_linked = 0;
m_pixels = NULL;
m_anynul = 0;
m_nulval = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param image FITS image to copy
*
* Copy class members. If the pixel array is linked the pointer to the array
* is copied. Otherwise a copy of the image pixels will be created.
***************************************************************************/
void GFitsDblImage::copy_members(const GFitsDblImage& image)
{
// Copy attributes
m_linked = image.m_linked;
m_anynul = image.m_anynul;
// Copy pixels
if (m_linked)
m_pixels = image.m_pixels;
else {
if (m_num_pixels > 0 && image.m_pixels != NULL) {
m_pixels = new double[m_num_pixels];
memcpy(m_pixels, image.m_pixels, m_num_pixels*sizeof(double));
}
}
// Copy NULL value
if (image.m_nulval != NULL) {
if (m_nulval != NULL) delete m_nulval;
m_nulval = new double;
*m_nulval = *image.m_nulval;
}
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GFitsDblImage::free_members(void)
{
// If we have proper pixels then free them now
if (!m_linked) {
if (m_pixels != NULL) delete [] m_pixels;
}
// Free NULL value
if (m_nulval != NULL) delete m_nulval;
// Mark memory as free
m_linked = 0;
m_pixels = NULL;
m_nulval = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Fetch image pixels
*
* Fetch the image pixels. This function is in general called if pixel
* values should be read or written yet no pixel array is allocated. In case
* that pixels existed already before they will be deleted before fetching
* new ones.
* There are two possibilities to fetch the pixels:
* (1) In case that a FITS file is attached to the image the pixel array
* will be loaded from the file.
* (2) In case that no FITS file is attached a new pixel array will be
* allocated that is initalised to 0.
***************************************************************************/
void GFitsDblImage::fetch_pixels(void)
{
// Fetch only if there are pixels in image
if (m_num_pixels > 0) {
// If we have pixels allocated then free them now
if (!m_linked) {
if (m_pixels != NULL) delete [] m_pixels;
}
// Allocate fresh image pixels
m_pixels = new double[m_num_pixels];
// Case 1: A FITS is attached. Load pixels now.
if (m_fitsfile.Fptr != NULL)
load_image(__TDOUBLE, m_pixels, m_nulval, &m_anynul);
// Case 2: No FITS file is attached. Allocate pixels and set them all to 0.0
else {
for (int i = 0; i < m_num_pixels; ++i)
m_pixels[i] = 0.0;
}
// Mark pixel as proper to the class instance
m_linked = 0;
}
// Return
return;
}
/*==========================================================================
= =
= GFitsDblImage friends =
= =
==========================================================================*/
/*==========================================================================
= =
= Other functions used by GFitsDblImage =
= =
==========================================================================*/