Kstars

fitsoverlay.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Jasem Mutlaq <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "fitsoverlay.h"
8 
9 #include "fitsimage.h"
10 
11 #include <KTemporaryFile>
12 #include <KIO/CopyJob>
13 #include <KIO/JobUiDelegate>
14 
15 FITSOverlay::FITSOverlay()
16 {
17 }
18 
19 void FITSOverlay::addFITSOverlay(const dms &ra, const dms &dec, const QUrl &imageURL)
20 {
21  m_ImageUrl = imageURL;
22  this->ra = ra;
23  this->dec = dec;
24 
25  // check URL
26  if (!m_ImageUrl.isValid())
27  kDebug() << "URL is malformed: " << m_ImageUrl;
28 
29  // FIXME: check the logic with temporary files. Races are possible
30  {
31  KTemporaryFile tempfile;
32  tempfile.open();
33  file.setFileName(tempfile.fileName());
34  } // we just need the name and delete the tempfile from disc; if we don't do it, a dialog will be show
35 
36  loadImageFromURL();
37 }
38 
39 FITSOverlay::~FITSOverlay()
40 {
41  if (downloadJob)
42  {
43  // close job quietly, without emitting a result
44  downloadJob->kill(KJob::Quietly);
45  delete downloadJob;
46  }
47 
48  qDeleteAll(fList);
49 }
50 
51 void FITSOverlay::loadImageFromURL()
52 {
53  QUrl saveURL = QUrl::fromPath(file.fileName());
54  if (!saveURL.isValid())
55  kDebug() << "tempfile-URL is malformed\n";
56 
57  qDebug() << "Starting download job for URL " << m_ImageUrl << endl;
58 
59  downloadJob = KIO::copy(m_ImageUrl, saveURL); // starts the download asynchron
60  connect(downloadJob, SIGNAL(result(KJob*)), SLOT(downloadReady(KJob*)));
61 }
62 
63 void FITSOverlay::downloadReady(KJob *job)
64 {
65  // set downloadJob to 0, but don't delete it - the job will be deleted automatically !!!
66  downloadJob = 0;
67 
68  if (job->error())
69  {
70  static_cast<KIO::Job *>(job)->ui()->showErrorMessage();
71  return;
72  }
73 
74  file.close(); // to get the newest information from the file and not any information from opening of the file
75 
76  qDebug() << "Download OK , opening image now ..." << endl;
77  if (file.exists())
78  {
79  openImage();
80  return;
81  }
82 }
83 
84 void FITSOverlay::openImage()
85 {
86  FOverlay *newFO = new FOverlay;
87 
88  newFO->image_data = new FITSImage(FITS_NORMAL);
89 
90  qDebug() << "Reading FITS file ..." << endl;
91  bool result = newFO->image_data->loadFITS(file.fileName());
92 
93  if (result == false)
94  {
95  delete (newFO->image_data);
96  return;
97  }
98 
99  qDebug() << "Read successful, creating fits overlay now ..." << endl;
100 
101  int image_width, image_height;
102  double min, max, bzero, bscale, val;
103  float *image_buffer;
104 
105  image_width = newFO->image_data->getWidth();
106  image_height = newFO->image_data->getHeight();
107  min = newFO->image_data->getMin();
108  max = newFO->image_data->getMax();
109 
110  QImage image(image_width, image_height, QImage::Format_Indexed8);
111 
112  image_buffer = newFO->image_data->getImageBuffer();
113 
114  bscale = 255. / (max - min);
115  bzero = (-min) * (255. / (max - min));
116 
117  image.setNumColors(256);
118  for (int i = 0; i < 256; i++)
119  image.setColor(i, qRgb(i, i, i));
120 
121  /* Fill in pixel values using indexed map, linear scale */
122  for (int j = 0; j < image_height; j++)
123  for (int i = 0; i < image_width; i++)
124  {
125  val = image_buffer[j * image_width + i];
126  image.setPixel(i, j, ((int)(val * bscale + bzero)));
127  }
128 
129  newFO->pix.convertFromImage(image);
130  newFO->ra = ra;
131  newFO->dec = dec;
132  newFO->pix_width = image_width;
133  newFO->pix_height = image_height;
134 
135  qDebug() << "Added a new pixmap FITS!" << endl;
136 
137  fList.append(newFO);
138 }
139 
140 bool FITSOverlay::contains(const dms &ra, const dms &dec)
141 {
142  return false;
143 }
144 
145 #include "fitsoverlay.moc"
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QTextStream & endl(QTextStream &stream)
virtual QString fileName() const const override
bool isValid() const const
QTextStream & dec(QTextStream &stream)
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:37
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.