File indexing completed on 2024-04-21 14:45:49
0001 /* 0002 SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "fitsoverlay.h" 0008 0009 #include "fitsimage.h" 0010 0011 #include <KTemporaryFile> 0012 #include <KIO/CopyJob> 0013 #include <KIO/JobUiDelegate> 0014 0015 FITSOverlay::FITSOverlay() 0016 { 0017 } 0018 0019 void FITSOverlay::addFITSOverlay(const dms &ra, const dms &dec, const QUrl &imageURL) 0020 { 0021 m_ImageUrl = imageURL; 0022 this->ra = ra; 0023 this->dec = dec; 0024 0025 // check URL 0026 if (!m_ImageUrl.isValid()) 0027 kDebug() << "URL is malformed: " << m_ImageUrl; 0028 0029 // FIXME: check the logic with temporary files. Races are possible 0030 { 0031 KTemporaryFile tempfile; 0032 tempfile.open(); 0033 file.setFileName(tempfile.fileName()); 0034 } // we just need the name and delete the tempfile from disc; if we don't do it, a dialog will be show 0035 0036 loadImageFromURL(); 0037 } 0038 0039 FITSOverlay::~FITSOverlay() 0040 { 0041 if (downloadJob) 0042 { 0043 // close job quietly, without emitting a result 0044 downloadJob->kill(KJob::Quietly); 0045 delete downloadJob; 0046 } 0047 0048 qDeleteAll(fList); 0049 } 0050 0051 void FITSOverlay::loadImageFromURL() 0052 { 0053 QUrl saveURL = QUrl::fromPath(file.fileName()); 0054 if (!saveURL.isValid()) 0055 kDebug() << "tempfile-URL is malformed\n"; 0056 0057 qDebug() << "Starting download job for URL " << m_ImageUrl << endl; 0058 0059 downloadJob = KIO::copy(m_ImageUrl, saveURL); // starts the download asynchron 0060 connect(downloadJob, SIGNAL(result(KJob*)), SLOT(downloadReady(KJob*))); 0061 } 0062 0063 void FITSOverlay::downloadReady(KJob *job) 0064 { 0065 // set downloadJob to 0, but don't delete it - the job will be deleted automatically !!! 0066 downloadJob = 0; 0067 0068 if (job->error()) 0069 { 0070 static_cast<KIO::Job *>(job)->ui()->showErrorMessage(); 0071 return; 0072 } 0073 0074 file.close(); // to get the newest information from the file and not any information from opening of the file 0075 0076 qDebug() << "Download OK , opening image now ..." << endl; 0077 if (file.exists()) 0078 { 0079 openImage(); 0080 return; 0081 } 0082 } 0083 0084 void FITSOverlay::openImage() 0085 { 0086 FOverlay *newFO = new FOverlay; 0087 0088 newFO->image_data = new FITSImage(FITS_NORMAL); 0089 0090 qDebug() << "Reading FITS file ..." << endl; 0091 bool result = newFO->image_data->loadFITS(file.fileName()); 0092 0093 if (result == false) 0094 { 0095 delete (newFO->image_data); 0096 return; 0097 } 0098 0099 qDebug() << "Read successful, creating fits overlay now ..." << endl; 0100 0101 int image_width, image_height; 0102 double min, max, bzero, bscale, val; 0103 float *image_buffer; 0104 0105 image_width = newFO->image_data->getWidth(); 0106 image_height = newFO->image_data->getHeight(); 0107 min = newFO->image_data->getMin(); 0108 max = newFO->image_data->getMax(); 0109 0110 QImage image(image_width, image_height, QImage::Format_Indexed8); 0111 0112 image_buffer = newFO->image_data->getImageBuffer(); 0113 0114 bscale = 255. / (max - min); 0115 bzero = (-min) * (255. / (max - min)); 0116 0117 image.setNumColors(256); 0118 for (int i = 0; i < 256; i++) 0119 image.setColor(i, qRgb(i, i, i)); 0120 0121 /* Fill in pixel values using indexed map, linear scale */ 0122 for (int j = 0; j < image_height; j++) 0123 for (int i = 0; i < image_width; i++) 0124 { 0125 val = image_buffer[j * image_width + i]; 0126 image.setPixel(i, j, ((int)(val * bscale + bzero))); 0127 } 0128 0129 newFO->pix.convertFromImage(image); 0130 newFO->ra = ra; 0131 newFO->dec = dec; 0132 newFO->pix_width = image_width; 0133 newFO->pix_height = image_height; 0134 0135 qDebug() << "Added a new pixmap FITS!" << endl; 0136 0137 fList.append(newFO); 0138 } 0139 0140 bool FITSOverlay::contains(const dms &ra, const dms &dec) 0141 { 0142 return false; 0143 } 0144 0145 #include "fitsoverlay.moc"