From ec9cf37f2b75219d8488fa2d0eba9f34140139b5 Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Sun, 2 Mar 2008 00:18:43 +0000 Subject: [PATCH] implement image saving --- include/GFitsCfitsio.hpp | 19 ++++++++++++------- include/GFitsImage.hpp | 20 ++++++++++++++++---- src/fits/GFitsImage.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- test/test_GFits.cpp | 15 ++++++++++++++- 4 files changed, 119 insertions(+), 21 deletions(-) diff --git a/include/GFitsCfitsio.hpp b/include/GFitsCfitsio.hpp index 1a8d377..7c85ec2 100644 --- a/include/GFitsCfitsio.hpp +++ b/include/GFitsCfitsio.hpp @@ -47,12 +47,16 @@ #define __ffgtcl(A, B, C, D, E, F) ffgtcl(A, B, C, D, E, F) #define __ffgcv(A, B, C, D, E, F, G, H, I, J) ffgcv(A, B, C, D, E, F, G, H, I, J) #define __ffgcvs(A, B, C, D, E, F, G, H, I) ffgcvs(A, B, C, D, E, F, G, H, I) -#define __TLOGICAL TLOGICAL -#define __TSTRING TSTRING -#define __TSHORT TSHORT -#define __TLONG TLONG -#define __TFLOAT TFLOAT -#define __TDOUBLE TDOUBLE +#define __ffpss(A, B, C, D, E, F) ffpss(A, B, C, D, E, F) +#define __TBIT TBIT +#define __TBYTE TBYTE +#define __TLOGICAL TLOGICAL +#define __TSTRING TSTRING +#define __TSHORT TSHORT +#define __TLONG TLONG +#define __TLONGLONG TLONGLONG +#define __TFLOAT TFLOAT +#define __TDOUBLE TDOUBLE /* __ Type definition ____________________________________________________ */ typedef fitsfile __fitsfile; @@ -81,8 +85,9 @@ typedef fitsfile __fitsfile; #define __ffgtcl(A, B, C, D, E, F) __dummy() #define __ffgcv(A, B, C, D, E, F, G, H, I, J) __dummy() #define __ffgcvs(A, B, C, D, E, F, G, H, I) __dummy() +#define __ffpss(A, B, C, D, E, F) __dummy() #define __TBIT 1 -#define __TSHORT 11 +#define __TBYTE 11 #define __TLOGICAL 14 #define __TSTRING 16 #define __TSHORT 21 diff --git a/include/GFitsImage.hpp b/include/GFitsImage.hpp index 5e31bd3..82d6409 100644 --- a/include/GFitsImage.hpp +++ b/include/GFitsImage.hpp @@ -40,6 +40,7 @@ class GFitsImage : public GFitsData { public: // Constructors and destructors GFitsImage(); + GFitsImage(int naxis, const int* naxes, int bitpix = -64); GFitsImage(const GFitsImage& image); ~GFitsImage(); @@ -50,6 +51,7 @@ public: void open(__fitsfile* fptr); void save(void); void close(void); + void link(const double* pixels); GFitsImage* clone(void) const; private: @@ -60,16 +62,26 @@ private: void connect(__fitsfile* fptr); // Private data area - __fitsfile m_fitsfile; - int m_bitpix; - int m_naxis; - long* m_naxes; + __fitsfile m_fitsfile; // FITS file + int m_bitpix; // Number of Bits/pixel + int m_naxis; // Image dimension + long* m_naxes; // Number of pixels in each dimension + int m_linked; // Pixels are linked (don't delete them!) + int m_type; // Pixel type + void* m_pixels; // Pixels }; /*************************************************************************** * Inline methods * ***************************************************************************/ +inline +void GFitsImage::link(const double* pixels) +{ + m_linked = 1; + m_type = __TDOUBLE; + m_pixels = (double*)pixels; +} inline GFitsImage* GFitsImage::clone(void) const { diff --git a/src/fits/GFitsImage.cpp b/src/fits/GFitsImage.cpp index 8f41b67..66a6dc7 100644 --- a/src/fits/GFitsImage.cpp +++ b/src/fits/GFitsImage.cpp @@ -51,6 +51,38 @@ GFitsImage::GFitsImage() : GFitsData() /***********************************************************************//** + * @brief Constructor + * + * @param naxis Image dimensions (0,1,2,3,4) + * @param naxes Number of pixels in each dimension + * @param bitpix Number of Bits per pixel (8, 16, 32, 64, -32, -64=default) + * + * Construct instance of GFitsImage 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. + ***************************************************************************/ +GFitsImage::GFitsImage(int naxis, const int* naxes, int bitpix) : GFitsData() +{ + // Initialise class members for clean destruction + init_members(); + + // Store number of axis and bitpix + m_naxis = naxis; + m_bitpix = bitpix; + + // Copy number of pixels in each dimension + if (m_naxis > 0) { + m_naxes = new long[m_naxis]; + for (int i = 0; i < m_naxis; ++i) + m_naxes[i] = naxes[i]; + } + + // Return + return; +} + + +/***********************************************************************//** * @brief Copy constructor * * @param image FITS image which should be used for construction @@ -153,11 +185,11 @@ void GFitsImage::open(__fitsfile* fptr) /***********************************************************************//** * @brief Save Image * - * IMPLEMENTATION NOT COMPLETE (IMAGE CUBE SAVING MISSING) + * Save image pixels into FITS file. In case that the HDU does not exist it + * is created. ***************************************************************************/ void GFitsImage::save(void) { -//cout << "GFitsImage::save " << m_fitsfile.Fptr << " " << (m_fitsfile.HDUposition)+1 << endl; // Move to HDU int status = 0; status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL, &status); @@ -185,11 +217,20 @@ void GFitsImage::save(void) throw GException::fits_error(G_SAVE, status); } - // Now save the image - // TBD - //status = ffpss(&m_fitsfile, 0, NULL, NULL, NULL, &status); - //if (status != 0) - // throw GException::fits_error(G_SAVE, status); + // Save the image pixels (if there are some ...) + if (m_naxis > 0) { + long* fpixel = new long[m_naxis]; + long* lpixel = new long[m_naxis]; + for (int i = 0; i < m_naxis; ++i) { + fpixel[i] = 1; + lpixel[i] = m_naxes[i]; + } + status = __ffpss(&m_fitsfile, m_type, fpixel, lpixel, m_pixels, &status); + if (status != 0) + throw GException::fits_error(G_SAVE, status); + delete [] fpixel; + delete [] lpixel; + } // Return return; @@ -229,6 +270,9 @@ void GFitsImage::init_members(void) m_bitpix = 8; m_naxis = 0; m_naxes = NULL; + m_linked = 0; + m_type = __TBYTE; + m_pixels = NULL; // Return return; @@ -247,12 +291,21 @@ void GFitsImage::copy_members(const GFitsImage& image) m_bitpix = image.m_bitpix; m_naxis = image.m_naxis; m_naxes = NULL; + m_linked = image.m_linked; + m_type = image.m_type; + m_pixels = NULL; - // Copy data + // Copy axes if (image.m_naxes != NULL && m_naxis > 0) { m_naxes = new long[m_naxis]; memcpy(m_naxes, image.m_naxes, m_naxis*sizeof(long)); } + + // Copy pixels (not if they have been linked!!!) + if (m_linked) + m_pixels = image.m_pixels; + else { + } // Return return; @@ -267,8 +320,23 @@ void GFitsImage::free_members(void) // Free memory if (m_naxes != NULL) delete [] m_naxes; + // If we have proper pixels then free them now (type dependent!) + if (!m_linked) { + if (m_pixels != NULL) { + switch (m_type) { + case __TDOUBLE: + delete [] (double*)m_pixels; + break; + default: + break; + } + } + } + // Mark memory as free - m_naxes = NULL; + m_naxes = NULL; + m_pixels = NULL; + m_linked = 0; // Return return; diff --git a/test/test_GFits.cpp b/test/test_GFits.cpp index da78a38..bf23679 100644 --- a/test/test_GFits.cpp +++ b/test/test_GFits.cpp @@ -295,13 +295,26 @@ void test_create(void) { std::cout << "Test GFits: Create FITS file: "; try { + // Allocate and open FITS file GFits fits; fits.open("test.fits"); cout << endl << fits; - GFitsImage image; + + // Define image + int naxis = 2; + int naxes[] = {2,4}; + double pixels[] = {0,1, 2,3, 4,5, 6,7}; + GFitsImage image(naxis, naxes); + image.link(pixels); + + // Define and append HDU GFitsHDU hdu(image); fits.append(&hdu); + + // Dump FITS file cout << endl << fits; + + // Save FITS file fits.save(); } catch (exception &e) { -- libgit2 0.21.4