• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • fitsviewer
fitsview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  FITSView.cpp - FITS Image
3  -------------------
4  begin : Thu Jan 22 2004
5  copyright : (C) 2004 by Jasem Mutlaq
6  email : mutlaqja@ikarustech.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  * Some code fragments were adapted from Peter Kirchgessner's FITS plugin*
17  * See http://members.aol.com/pkirchg for more details. *
18  ***************************************************************************/
19 
20 #include <config-kstars.h>
21 
22 #include "fitsview.h"
23 
24 #include <cmath>
25 #include <cstdlib>
26 
27 #include <QApplication>
28 #include <QPaintEvent>
29 #include <QScrollArea>
30 #include <QFile>
31 #include <QCursor>
32 #include <QProgressDialog>
33 #include <QDateTime>
34 #include <QPainter>
35 #include <QPixmap>
36 
37 #include <KDebug>
38 #include <KLocale>
39 #include <KAction>
40 #include <KActionCollection>
41 #include <KStatusBar>
42 #include <KProgressDialog>
43 #include <KMessageBox>
44 #include <KFileDialog>
45 
46 #include "ksutils.h"
47 
48 #define ZOOM_DEFAULT 100.0
49 #define ZOOM_MIN 10
50 #define ZOOM_MAX 400
51 #define ZOOM_LOW_INCR 10
52 #define ZOOM_HIGH_INCR 50
53 
54 //#define FITS_DEBUG
55 
56 FITSLabel::FITSLabel(FITSView *img, QWidget *parent) : QLabel(parent)
57 {
58  image = img;
59 
60 }
61 
62 void FITSLabel::setSize(double w, double h)
63 {
64  width = w;
65  height = h;
66  size = w*h;
67 }
68 
69 FITSLabel::~FITSLabel() {}
70 
71 void FITSLabel::mouseMoveEvent(QMouseEvent *e)
72 {
73  double x,y;
74  FITSImage *image_data = image->getImageData();
75 
76  float *buffer = image_data->getImageBuffer();
77 
78  if (buffer == NULL)
79  return;
80 
81  x = round(e->x() / (image->getCurrentZoom() / ZOOM_DEFAULT));
82  y = round(e->y() / (image->getCurrentZoom() / ZOOM_DEFAULT));
83 
84  x = KSUtils::clamp(x, 1.0, width);
85  y = KSUtils::clamp(y, 1.0, height);
86 
87  emit newStatus(QString("%1 , %2").arg( (int)x ).arg( (int)y ), FITS_POSITION);
88 
89  // Range is 0 to dim-1 when accessing array
90  x -= 1;
91  y -= 1;
92 
93  emit newStatus(KGlobal::locale()->formatNumber( buffer[(int) (y * width + x)]), FITS_VALUE);
94 
95  if (image_data->hasWCS())
96  {
97  int index = x + y * width;
98 
99  wcs_point * wcs_coord = image_data->getWCSCoord();
100 
101  if (index > size)
102  return;
103 
104  ra.setD(wcs_coord[index].ra);
105  dec.setD(wcs_coord[index].dec);
106 
107  emit newStatus(QString("%1 , %2").arg( ra.toHMSString()).arg(dec.toDMSString()), FITS_WCS);
108  }
109 
110  setCursor(Qt::CrossCursor);
111 
112  e->accept();
113 }
114 
115 void FITSLabel::mousePressEvent(QMouseEvent *e)
116 {
117  double x,y;
118 
119  x = round(e->x() / (image->getCurrentZoom() / ZOOM_DEFAULT));
120  y = round(e->y() / (image->getCurrentZoom() / ZOOM_DEFAULT));
121 
122  x = KSUtils::clamp(x, 1.0, width);
123  y = KSUtils::clamp(y, 1.0, height);
124 
125  emit pointSelected(x, y);
126 
127 }
128 
129 FITSView::FITSView(QWidget * parent, FITSMode fitsMode) : QScrollArea(parent) , zoomFactor(1.2)
130 {
131  image_frame = new FITSLabel(this);
132  image_data = NULL;
133  display_image = NULL;
134  firstLoad = true;
135 
136  mode = fitsMode;
137 
138  setBackgroundRole(QPalette::Dark);
139 
140  guide_x = guide_y = guide_box = -1;
141 
142  currentZoom = 0.0;
143  markStars = false;
144 
145  connect(image_frame, SIGNAL(newStatus(QString,FITSBar)), this, SIGNAL(newStatus(QString,FITSBar)));
146 
147  image_frame->setMouseTracking(true);
148 
149  if (fitsMode == FITS_GUIDE)
150  connect(image_frame, SIGNAL(pointSelected(int,int)), this, SLOT(processPointSelection(int,int)));
151 
152  // Default size
153  resize(INITIAL_W, INITIAL_H);
154 }
155 
156 FITSView::~FITSView()
157 {
158  delete(display_image);
159  delete(image_data);
160 }
161 
162 bool FITSView::loadFITS ( const QString &inFilename )
163 {
164  QProgressDialog fitsProg;
165 
166  delete (image_data);
167  image_data = NULL;
168 
169  image_data = new FITSImage(mode);
170 
171  if (image_data->loadFITS(inFilename, &fitsProg) == false)
172  return false;
173 
174  image_data->getSize(&currentWidth, &currentHeight);
175 
176  image_width = currentWidth;
177  image_height = currentHeight;
178 
179  image_frame->setSize(currentWidth, currentHeight);
180 
181  hasWCS = image_data->hasWCS();
182 
183  delete (display_image);
184  display_image = NULL;
185 
186  display_image = new QImage(currentWidth, currentHeight, QImage::Format_Indexed8);
187 
188  display_image->setNumColors(256);
189  for (int i=0; i < 256; i++)
190  display_image->setColor(i, qRgb(i,i,i));
191 
192  // Rescale to fits window
193  if (firstLoad)
194  {
195  currentZoom = 100;
196 
197  if (rescale(ZOOM_FIT_WINDOW))
198  return false;
199 
200  firstLoad = false;
201  }
202  else
203  {
204  if (rescale(ZOOM_KEEP_LEVEL))
205  return false;
206  }
207 
208  setAlignment(Qt::AlignCenter);
209 
210  if (isVisible())
211  emit newStatus(QString("%1x%2").arg(currentWidth).arg(currentHeight), FITS_RESOLUTION);
212 
213  return true;
214 }
215 
216 int FITSView::saveFITS( const QString &newFilename )
217 {
218  return image_data->saveFITS(newFilename);
219 }
220 
221 
222 int FITSView::rescale(FITSZoom type)
223 {
224  float val=0;
225  double bscale, bzero;
226  double min, max;
227 
228  image_data->getMinMax(&min, &max);
229  float *image_buffer = image_data->getImageBuffer();
230 
231  if (min == max)
232  {
233  KMessageBox::error(0, i18n("FITS image is saturated and cannot be displayed."), i18n("FITS Open"));
234  return -1;
235  }
236 
237 
238  bscale = 255. / (max - min);
239  bzero = (-min) * (255. / (max - min));
240 
241  image_frame->setScaledContents(true);
242  currentWidth = display_image->width();
243  currentHeight = display_image->height();
244 
245  /* Fill in pixel values using indexed map, linear scale */
246  for (int j = 0; j < image_height; j++)
247  for (int i = 0; i < image_width; i++)
248  {
249  val = image_buffer[j * image_width + i];
250  display_image->setPixel(i, j, ((int) (val * bscale + bzero)));
251  }
252 
253  switch (type)
254  {
255  case ZOOM_FIT_WINDOW:
256  if ((display_image->width() > width() || display_image->height() > height()))
257  {
258  // Find the zoom level which will enclose the current FITS in the default window size (640x480)
259  currentZoom = floor( (INITIAL_W / currentWidth) * 10.) * 10.;
260 
261  /* If width is not the problem, try height */
262  if (currentZoom > ZOOM_DEFAULT)
263  currentZoom = floor( (INITIAL_H / currentHeight) * 10.) * 10.;
264 
265  currentWidth = image_width * (currentZoom / ZOOM_DEFAULT);
266  currentHeight = image_height * (currentZoom / ZOOM_DEFAULT);
267 
268 
269  if (currentZoom <= ZOOM_MIN)
270  emit actionUpdated("view_zoom_out", false);
271 
272 
273  }
274  else
275  {
276  currentZoom = 100;
277  currentWidth = image_width;
278  currentHeight = image_height;
279 
280  }
281 
282 
283  break;
284 
285  case ZOOM_KEEP_LEVEL:
286  {
287  currentWidth = image_width * (currentZoom / ZOOM_DEFAULT);
288  currentHeight = image_height * (currentZoom / ZOOM_DEFAULT);
289 
290  }
291  break;
292 
293  default:
294  currentZoom = 100;
295 
296  break;
297  }
298 
299  setWidget(image_frame);
300 
301 
302  if (type != ZOOM_KEEP_LEVEL)
303  emit newStatus(QString("%1%").arg(currentZoom), FITS_ZOOM);
304 
305  return 0;
306 
307 }
308 
309 void FITSView::ZoomIn()
310 {
311 
312  if (currentZoom < ZOOM_DEFAULT)
313  currentZoom += ZOOM_LOW_INCR;
314  else
315  currentZoom += ZOOM_HIGH_INCR;
316 
317 
318  emit actionUpdated("view_zoom_out", true);
319  if (currentZoom >= ZOOM_MAX)
320  emit actionUpdated("view_zoom_in", false);
321 
322  currentWidth = image_width * (currentZoom / ZOOM_DEFAULT);
323  currentHeight = image_height * (currentZoom / ZOOM_DEFAULT);
324 
325  updateFrame();
326 
327  newStatus(QString("%1%").arg(currentZoom), FITS_ZOOM);
328 
329 }
330 
331 void FITSView::ZoomOut()
332 {
333 
334  if (currentZoom <= ZOOM_DEFAULT)
335  currentZoom -= ZOOM_LOW_INCR;
336  else
337  currentZoom -= ZOOM_HIGH_INCR;
338 
339  if (currentZoom <= ZOOM_MIN)
340  emit actionUpdated("view_zoom_out", false);
341 
342  emit actionUpdated("view_zoom_in", true);
343 
344  currentWidth = image_width * (currentZoom / ZOOM_DEFAULT);
345  currentHeight = image_height * (currentZoom / ZOOM_DEFAULT);
346 
347  updateFrame();
348 
349  newStatus(QString("%1%").arg(currentZoom), FITS_ZOOM);
350 }
351 
352 void FITSView::updateFrame()
353 {
354 
355  QPixmap displayPixmap;
356  bool ok=false;
357 
358  if (display_image == NULL)
359  return;
360 
361  if (currentZoom != ZOOM_DEFAULT)
362  ok = displayPixmap.convertFromImage(display_image->scaled( (int) currentWidth, (int) currentHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
363  else
364  ok = displayPixmap.convertFromImage(*display_image);
365 
366 
367  if (ok == false)
368  return;
369 
370  QPainter painter(&displayPixmap);
371 
372  drawOverlay(&painter);
373 
374  image_frame->setPixmap(displayPixmap);
375  image_frame->resize( (int) currentWidth, (int) currentHeight);
376 }
377 
378 void FITSView::ZoomDefault()
379 {
380  emit actionUpdated("view_zoom_out", true);
381  emit actionUpdated("view_zoom_in", true);
382 
383 
384  currentZoom = ZOOM_DEFAULT;
385  currentWidth = image_width;
386  currentHeight = image_height;
387 
388  updateFrame();
389 
390  newStatus(QString("%1%").arg(currentZoom), FITS_ZOOM);
391 
392  update();
393 
394 }
395 
396 void FITSView::drawOverlay(QPainter *painter)
397 {
398  if (markStars)
399  drawStarCentroid(painter);
400 
401  if (mode == FITS_GUIDE)
402  drawGuideBox(painter);
403 
404 }
405 
406 void FITSView::drawStarCentroid(QPainter *painter)
407 {
408  painter->setPen(QPen(Qt::red, 2));
409 
410  int x1,y1, w;
411 
412  // image_data->getStarCenter();
413 
414  QList<Edge*> starCenters = image_data->getStarCenters();
415 
416  for (int i=0; i < starCenters.count() ; i++)
417  {
418  x1 = (starCenters[i]->x - starCenters[i]->width/2) * (currentZoom / ZOOM_DEFAULT);
419  y1 = (starCenters[i]->y - starCenters[i]->width/2) * (currentZoom / ZOOM_DEFAULT);
420  w = (starCenters[i]->width) * (currentZoom / ZOOM_DEFAULT);
421 
422  painter->drawEllipse(x1, y1, w, w);
423  }
424 }
425 
426 void FITSView::drawGuideBox(QPainter *painter)
427 {
428  painter->setPen(QPen(Qt::green, 2));
429 
430  int mid = guide_box/2;
431 
432  if (mid == -1 || guide_x == -1 || guide_y == -1)
433  return;
434 
435  int x1 = (guide_x - mid) * (currentZoom / ZOOM_DEFAULT);
436  int y1 = (guide_y - mid) * (currentZoom / ZOOM_DEFAULT);
437  int w = guide_box * (currentZoom / ZOOM_DEFAULT);
438 
439  painter->drawRect(x1, y1, w, w);
440 }
441 
442 void FITSView::setGuideSquare(int x, int y)
443 {
444  guide_x = x;
445  guide_y = y;
446 
447  updateFrame();
448 
449 
450 }
451 
452 void FITSView::setGuideBoxSize(int size)
453 {
454  guide_box = size;
455  updateFrame();
456 }
457 
458 void FITSView::toggleStars(bool enable)
459 {
460  markStars = enable;
461 
462  if (markStars == true)
463  {
464  int count = image_data->findStars();
465  if (count >= 0 && isVisible())
466  emit newStatus(i18np("%1 star detected.", "%1 stars detected.", count), FITS_MESSAGE);
467  }
468 }
469 
470 void FITSView::processPointSelection(int x, int y)
471 {
472  image_data->getCenterSelection(&x, &y);
473 
474  setGuideSquare(x,y);
475  emit guideStarSelected(x,y);
476 }
477 
478 #include "fitsview.moc"
FITSView::getImageData
FITSImage * getImageData()
Definition: fitsview.h:98
FITSView::setGuideSquare
void setGuideSquare(int x, int y)
Definition: fitsview.cpp:442
FITSView::loadFITS
bool loadFITS(const QString &filename)
Definition: fitsview.cpp:162
FITSImage
Definition: fitsimage.h:73
FITSView::processPointSelection
void processPointSelection(int x, int y)
Definition: fitsview.cpp:470
FITSImage::findStars
int findStars()
Definition: fitsimage.cpp:985
FITSImage::getWCSCoord
wcs_point * getWCSCoord()
Definition: fitsimage.h:123
FITSLabel::~FITSLabel
virtual ~FITSLabel()
Definition: fitsview.cpp:69
ZOOM_MAX
#define ZOOM_MAX
Definition: fitsview.cpp:50
ZOOM_FIT_WINDOW
Definition: fitscommon.h:23
QWidget
FITSLabel::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
Definition: fitsview.cpp:115
FITSLabel::setSize
void setSize(double w, double h)
Definition: fitsview.cpp:62
FITSView::guideStarSelected
void guideStarSelected(int x, int y)
FITSView
Definition: fitsview.h:81
FITSView::drawOverlay
void drawOverlay(QPainter *)
Definition: fitsview.cpp:396
FITS_MESSAGE
Definition: fitscommon.h:21
FITS_WCS
Definition: fitscommon.h:21
INITIAL_H
#define INITIAL_H
Definition: fitsimage.h:48
fitsview.h
QScrollArea
FITSImage::saveFITS
int saveFITS(const QString &filename)
Definition: fitsimage.cpp:268
FITSView::rescale
int rescale(FITSZoom type)
Definition: fitsview.cpp:222
FITSView::getCurrentZoom
double getCurrentZoom()
Definition: fitsview.h:99
FITSView::newStatus
void newStatus(const QString &msg, FITSBar id)
FITSLabel::pointSelected
void pointSelected(int x, int y)
FITSView::saveFITS
int saveFITS(const QString &filename)
Definition: fitsview.cpp:216
FITSView::~FITSView
~FITSView()
Definition: fitsview.cpp:156
FITSLabel::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: fitsview.cpp:71
FITSImage::hasWCS
bool hasWCS()
Definition: fitsimage.h:122
FITSView::toggleStars
void toggleStars(bool enable)
Definition: fitsview.cpp:458
FITSView::drawStarCentroid
void drawStarCentroid(QPainter *)
Definition: fitsview.cpp:406
ZOOM_LOW_INCR
#define ZOOM_LOW_INCR
Definition: fitsview.cpp:51
FITSView::ZoomDefault
void ZoomDefault()
Definition: fitsview.cpp:378
FITSImage::getStarCenters
QList< Edge * > getStarCenters()
Definition: fitsimage.h:97
ZOOM_KEEP_LEVEL
Definition: fitscommon.h:23
ZOOM_HIGH_INCR
#define ZOOM_HIGH_INCR
Definition: fitsview.cpp:52
FITS_RESOLUTION
Definition: fitscommon.h:21
FITS_VALUE
Definition: fitscommon.h:21
FITSView::drawGuideBox
void drawGuideBox(QPainter *)
Definition: fitsview.cpp:426
FITSImage::getImageBuffer
float * getImageBuffer()
Definition: fitsimage.h:91
FITS_POSITION
Definition: fitscommon.h:21
ZOOM_DEFAULT
#define ZOOM_DEFAULT
Definition: fitsview.cpp:48
FITSView::ZoomOut
void ZoomOut()
Definition: fitsview.cpp:331
ZOOM_MIN
#define ZOOM_MIN
Definition: fitsview.cpp:49
FITSImage::getMinMax
void getMinMax(double *min, double *max)
Definition: fitsimage.h:93
FITSLabel::FITSLabel
FITSLabel(FITSView *img, QWidget *parent=NULL)
Definition: fitsview.cpp:56
INITIAL_W
#define INITIAL_W
Definition: fitsimage.h:47
QLabel
FITSImage::getCenterSelection
void getCenterSelection(int *x, int *y)
Definition: fitsimage.cpp:1008
FITSView::FITSLabel
friend class FITSLabel
Definition: fitsview.h:150
FITSBar
FITSBar
Definition: fitscommon.h:21
FITS_ZOOM
Definition: fitscommon.h:21
KSUtils::clamp
T clamp(T x, T min, T max)
Clamp value into range.
Definition: ksutils.h:59
FITSView::FITSView
FITSView(QWidget *parent=0, FITSMode mode=FITS_NORMAL)
Definition: fitsview.cpp:129
dms::setD
void setD(const double &x)
Sets floating-point value of angle, in degrees.
Definition: dms.h:130
FITSView::updateFrame
void updateFrame()
Definition: fitsview.cpp:352
FITSView::actionUpdated
void actionUpdated(const QString &name, bool enable)
FITSView::setGuideBoxSize
void setGuideBoxSize(int size)
Definition: fitsview.cpp:452
FITSImage::getSize
void getSize(double *w, double *h)
Definition: fitsimage.h:92
ksutils.h
FITSImage::loadFITS
bool loadFITS(const QString &filename, QProgressDialog *progress=NULL)
Definition: fitsimage.cpp:96
FITSView::ZoomIn
void ZoomIn()
Definition: fitsview.cpp:309
FITSZoom
FITSZoom
Definition: fitscommon.h:23
wcs_point
Definition: fitsimage.h:55
FITSMode
FITSMode
Definition: fitscommon.h:20
QList
FITS_GUIDE
Definition: fitscommon.h:20
FITSLabel::newStatus
void newStatus(const QString &msg, FITSBar id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal