File indexing completed on 2024-05-12 04:19:43

0001 /***************************************************************************
0002 Gwenview: an image viewer
0003 
0004                           fitsimage.cpp  -  FITS Image
0005                              -------------------
0006     begin                : Tue Feb 24 2004
0007     copyright            : (C) 2004 by Jasem Mutlaq
0008     copyright            : (C) 2017 by Csaba Kertesz
0009     email                : mutlaqja@ikarustech.com
0010  ***************************************************************************/
0011 
0012 /***************************************************************************
0013  *                                                                         *
0014  *   This program is free software; you can redistribute it and/or modify  *
0015  *   it under the terms of the GNU General Public License as published by  *
0016  *   the Free Software Foundation; either version 2 of the License, or     *
0017  *   (at your option) any later version.                                   *
0018  *                                                                         *
0019  *   Some code fragments were adapted from Peter Kirchgessner's FITS plugin*
0020  *   See http://members.aol.com/pkirchg for more details.                  *
0021  ***************************************************************************/
0022 
0023 #pragma once
0024 
0025 #include "bayer.h"
0026 
0027 typedef enum {
0028     FITS_NORMAL,
0029     FITS_FOCUS,
0030     FITS_GUIDE,
0031     FITS_CALIBRATE,
0032     FITS_ALIGN,
0033 } FITSMode;
0034 
0035 #ifdef WIN32
0036 // This header must be included before fitsio.h to avoid compiler errors with Visual Studio
0037 #include <windows.h>
0038 #endif
0039 
0040 #include <fitsio.h>
0041 
0042 #include <QIODevice>
0043 #include <QRect>
0044 
0045 class FITSData
0046 {
0047 public:
0048     FITSData();
0049     ~FITSData();
0050 
0051     /* Loads FITS image, scales it, and displays it in the GUI */
0052     bool loadFITS(QIODevice &buffer);
0053     /* Calculate stats */
0054     void calculateStats(bool refresh = false);
0055 
0056     // Access functions
0057     void clearImageBuffers();
0058     uint8_t *getImageBuffer();
0059 
0060     int getDataType()
0061     {
0062         return data_type;
0063     }
0064 
0065     // Stats
0066     unsigned int getSize()
0067     {
0068         return stats.samples_per_channel;
0069     }
0070     uint16_t getWidth()
0071     {
0072         return stats.width;
0073     }
0074     uint16_t getHeight()
0075     {
0076         return stats.height;
0077     }
0078 
0079     // Statistics
0080     int getNumOfChannels()
0081     {
0082         return channels;
0083     }
0084     void getMinMax(double *min, double *max, uint8_t channel = 0)
0085     {
0086         *min = stats.min[channel];
0087         *max = stats.max[channel];
0088     }
0089 
0090     // Debayer
0091     bool debayer();
0092     bool debayer_8bit();
0093     bool debayer_16bit();
0094 
0095     // FITS Record
0096     int getFITSRecord(QString &recordList, int &nkeys);
0097 
0098     // Create autostretch image from FITS File
0099     static QImage FITSToImage(QIODevice &buffer);
0100 
0101     QString getLastError() const;
0102 
0103 private:
0104     int calculateMinMax(bool refresh = false);
0105     bool checkDebayer();
0106 
0107     // Templated functions
0108     template<typename T>
0109     bool debayer();
0110 
0111     template<typename T>
0112     void calculateMinMax();
0113     /* Calculate running average & standard deviation using Welford’s method for computing variance */
0114     template<typename T>
0115     void runningAverageStdDev();
0116 
0117     template<typename T>
0118     void convertToQImage(double dataMin, double dataMax, double scale, double zero, QImage &image);
0119 
0120     /// Pointer to CFITSIO FITS file struct
0121     fitsfile *fptr{nullptr};
0122 
0123     /// FITS image data type (TBYTE, TUSHORT, TINT, TFLOAT, TLONG, TDOUBLE)
0124     int data_type{0};
0125     /// Number of channels
0126     int channels{1};
0127     /// Generic data image buffer
0128     uint8_t *imageBuffer{nullptr};
0129 
0130     /// Our very own file name
0131     QString filename;
0132     /// FITS Mode (Normal, WCS, Guide, Focus..etc)
0133     FITSMode mode;
0134 
0135     uint8_t *bayerBuffer{nullptr};
0136     /// Bayer parameters
0137     BayerParams debayerParams;
0138 
0139     /// Stats struct to hold statistical data about the FITS data
0140     struct {
0141         double min[3], max[3];
0142         double mean[3];
0143         double stddev[3];
0144         double median[3];
0145         double SNR{0};
0146         int bitpix{8};
0147         int bytesPerPixel{1};
0148         int ndim{2};
0149         uint32_t samples_per_channel{0};
0150         uint16_t width{0};
0151         uint16_t height{0};
0152     } stats;
0153 
0154     QString lastError;
0155 };