• 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
fitshistogram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  fitshistogram.cpp - FITS Historgram
3  -----------------
4  begin : Thu Mar 4th 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  ***************************************************************************/
17 
18 #include "fitsviewer.h"
19 #include "fitshistogram.h"
20 #include "fitstab.h"
21 #include "fitsview.h"
22 #include "fitsimage.h"
23 
24 #include <cmath>
25 #include <cstdlib>
26 
27 #include <QPainter>
28 #include <QSlider>
29 #include <QCursor>
30 #include <QPen>
31 #include <QPixmap>
32 #include <QRadioButton>
33 #include <QPushButton>
34 #include <QMouseEvent>
35 #include <QPaintEvent>
36 
37 #include <KUndoStack>
38 #include <kdebug.h>
39 #include <klineedit.h>
40 #include <klocale.h>
41 #include <kmessagebox.h>
42 
43 //#define HIST_LOG
44 
45 #define LOW_PASS_MARGIN 0.01
46 #define LOW_PASS_LIMIT .05
47 
48 histogramUI::histogramUI(QDialog *parent) : QDialog(parent)
49 {
50  setupUi(parent);
51  setModal(false);
52 
53 }
54 
55 FITSHistogram::FITSHistogram(QWidget *parent) : QDialog(parent)
56 {
57  ui = new histogramUI(this);
58 
59  tab = (FITSTab *) parent;
60 
61  type = FITS_AUTO;
62  napply = 0;
63 
64  connect(ui->applyB, SIGNAL(clicked()), this, SLOT(applyScale()));
65  connect(ui->minOUT, SIGNAL(editingFinished()), this, SLOT(updateLowerLimit()));
66  connect(ui->maxOUT, SIGNAL(editingFinished()), this, SLOT(updateUpperLimit()));
67 
68  connect(ui->minSlider, SIGNAL(valueChanged(int)), this, SLOT(minSliderUpdated(int)));
69  connect(ui->maxSlider, SIGNAL(valueChanged(int)), this, SLOT(maxSliderUpdated(int)));
70 
71  ui->histFrame->init();
72 
73  constructHistogram(ui->histFrame->getHistWidth(), ui->histFrame->getHistHeight());
74 }
75 
76 FITSHistogram::~FITSHistogram()
77 {
78  //free (histArray);
79 }
80 
81 void FITSHistogram::constructHistogram(int hist_width, int hist_height)
82 {
83  int id;
84  double fits_w=0, fits_h=0;
85  FITSImage *image_data = tab->getImage()->getImageData();
86  float *buffer = image_data->getImageBuffer();
87 
88  image_data->getSize(&fits_w, &fits_h);
89  image_data->getMinMax(&fits_min, &fits_max);
90 
91  int pixel_range = (int) (fits_max - fits_min);
92 
93  #ifdef HIST_LOG
94  qDebug() << "fits MIN: " << fits_min << " - fits MAX: " << fits_max << " - pixel range: " << pixel_range;
95  #endif
96 
97  if (hist_width > histArray.size())
98  histArray.resize(hist_width);
99 
100  cumulativeFreq.resize(histArray.size());
101 
102  for (int i=0; i < hist_width; i++)
103  {
104  histArray[i] = 0;
105  cumulativeFreq[i] = 0;
106  }
107 
108  binWidth = ((double) hist_width / (double) pixel_range);
109  //binRoundSize = (int) floor(binSize);
110 
111  #ifdef HIST_LOG
112  qDebug() << "Hist Array is now " << hist_width << " wide..., pixel range is " << pixel_range << " Bin width is " << binWidth << endl;
113  #endif
114 
115  if (binWidth == 0 || buffer == NULL)
116  return;
117 
118  for (int i=0; i < fits_w * fits_h ; i++)
119  {
120  id = (int) round((buffer[i] - fits_min) * binWidth);
121 
122  if (id >= hist_width)
123  id = hist_width - 1;
124  else if (id < 0)
125  id=0;
126 
127  //histArray[id]++;
128  histArray[id]++;
129  }
130 
131  // Cumuliative Frequency
132  for (int i=0; i < histArray.size(); i++)
133  for (int j=0; j <= i; j++)
134  cumulativeFreq[i] += histArray[j];
135 
136 
137  mean = (image_data->getAverage()-fits_min)*binWidth;
138  mean_p_std = (image_data->getAverage()-fits_min+image_data->getStdDev()*3)*binWidth;
139 
140  // Indicator of information content of an image in a typical star field.
141  JMIndex = mean_p_std - mean;
142 
143 
144  #ifdef HIST_LOG
145  qDebug() << "Mean " << mean << " , mean plus std " << mean_p_std << " JMIndex " << JMIndex << endl;
146  #endif
147 
148  // if (mean == 0)
149  // JMIndex = 0;
150  // Reject diffuse images by setting JMIndex to zero.
151  if (mean && image_data->getMode() == FITS_NORMAL && mean_p_std / mean < 2)
152  JMIndex =0;
153 
154  #ifdef HIST_LOG
155  qDebug() << "Final JMIndex " << JMIndex << endl;
156  #endif
157 
158  // Normalize histogram height. i.e. the maximum value will take the whole height of the widget
159  histFactor = ((double) hist_height) / ((double) findMax(hist_width));
160 
161  histogram_height = hist_height;
162  histogram_width = hist_width;
163 
164  updateBoxes(fits_min, fits_max);
165 
166  ui->histFrame->update();
167 
168 }
169 
170 void FITSHistogram::updateBoxes(int lower_limit, int upper_limit)
171 {
172 
173  ui->minSlider->setMinimum(lower_limit);
174  ui->maxSlider->setMinimum(lower_limit);
175 
176  ui->minSlider->setMaximum(upper_limit);
177  ui->maxSlider->setMaximum(upper_limit);
178 
179  ui->minSlider->setValue(lower_limit);
180  ui->maxSlider->setValue(upper_limit);
181 
182  ui->minOUT->setText(QString::number(lower_limit));
183  ui->maxOUT->setText(QString::number(upper_limit));
184 
185 }
186 
187 void FITSHistogram::applyScale()
188 {
189 
190  int min = ui->minSlider->value();
191  int max = ui->maxSlider->value();
192 
193  FITSHistogramCommand *histC;
194 
195  napply++;
196 
197  // Auto
198  if (ui->autoR->isChecked())
199  type = FITS_AUTO;
200  // Linear
201  else if (ui->linearR->isChecked())
202  type = FITS_LINEAR;
203  // Log
204  else if (ui->logR->isChecked())
205  type = FITS_LOG;
206  // Exp
207  else if (ui->sqrtR->isChecked())
208  type = FITS_SQRT;
209 
210  histC = new FITSHistogramCommand(tab, this, type, min, max);
211 
212  tab->getUndoStack()->push(histC);
213 }
214 
215 void FITSHistogram::applyFilter(FITSScale ftype)
216 {
217  int min = ui->minSlider->value();
218  int max = ui->maxSlider->value();
219 
220  napply++;
221 
222  FITSHistogramCommand *histC;
223 
224  type = ftype;
225 
226  histC = new FITSHistogramCommand(tab, this, type, min, max);
227 
228  tab->getUndoStack()->push(histC);
229 
230 }
231 
232 void FITSHistogram::minSliderUpdated(int value)
233 {
234  if (value >= ui->maxSlider->value())
235  {
236  ui->minSlider->setValue(ui->minOUT->text().toInt());
237  return;
238  }
239 
240  ui->minOUT->setText(QString::number(value));
241 }
242 
243 void FITSHistogram::maxSliderUpdated(int value)
244 {
245  if (value <= ui->minSlider->value())
246  {
247  ui->maxSlider->setValue(ui->maxOUT->text().toInt());
248  return;
249  }
250 
251  ui->maxOUT->setText(QString::number(value));
252 }
253 
254 void FITSHistogram::updateIntenFreq(int x)
255 {
256  if (x < 0 || x >= histogram_width)
257  return;
258 
259  ui->intensityOUT->setText(QString("%1").arg( ceil(x / binWidth) + tab->getImage()->getImageData()->getMin()));
260 
261  ui->frequencyOUT->setText(QString("%1").arg(histArray[x]));
262 }
263 
264 void FITSHistogram::updateLowerLimit()
265 {
266  bool conversion_ok = false;
267  int newLowerLimit=0;
268 
269  newLowerLimit = ui->minOUT->text().toInt(&conversion_ok);
270 
271  if (conversion_ok == false)
272  return;
273 
274  ui->minSlider->setValue(newLowerLimit);
275 }
276 
277 void FITSHistogram::updateUpperLimit()
278 {
279  bool conversion_ok = false;
280  int newUpperLimit=0;
281 
282  newUpperLimit = ui->maxOUT->text().toInt(&conversion_ok);
283 
284  if (conversion_ok == false)
285  return;
286 
287  ui->maxSlider->setValue(newUpperLimit);
288 }
289 
290 int FITSHistogram::findMax(int hist_width)
291 {
292  double max = -1e9;
293 
294  for (int i=0; i < hist_width; i++)
295  if (histArray[i] > max) max = histArray[i];
296 
297  return ((int) max);
298 }
299 
300 void FITSHistogram::updateHistogram()
301 {
302  constructHistogram(ui->histFrame->maximumWidth(), ui->histFrame->maximumHeight());
303  //constructHistogram(histogram_width, histogram_height);
304 }
305 
306 FITSHistogramCommand::FITSHistogramCommand(QWidget * parent, FITSHistogram *inHisto, FITSScale newType, int lmin, int lmax)
307 {
308  tab = (FITSTab *) parent;
309  type = newType;
310  histogram = inHisto;
311  buffer = (float *) malloc (tab->getImage()->getImageData()->getWidth() * tab->getImage()->getImageData()->getHeight() * sizeof(float));
312 
313  min = lmin;
314  max = lmax;
315 }
316 
317 FITSHistogramCommand::~FITSHistogramCommand()
318 {
319  free(buffer);
320  //delete (oldImage);
321 }
322 
323 void FITSHistogramCommand::redo()
324 {
325 
326  FITSView *image = tab->getImage();
327  FITSImage *image_data = image->getImageData();
328 
329  float *image_buffer = image_data->getImageBuffer();
330  int width = image_data->getWidth();
331  int height = image_data->getHeight();
332 
333  memcpy(buffer, image_buffer, width * height * sizeof(float));
334 
335 
336  switch (type)
337  {
338  case FITS_AUTO:
339  case FITS_LINEAR:
340  image_data->applyFilter(FITS_LINEAR, image_buffer, min, max);
341  break;
342 
343  case FITS_LOG:
344  image_data->applyFilter(FITS_LOG, image_buffer, min, max);
345  break;
346 
347  case FITS_SQRT:
348  image_data->applyFilter(FITS_SQRT, image_buffer, min, max);
349  break;
350 
351  default:
352  image_data->applyFilter(type, image_buffer);
353  break;
354 
355 
356  }
357 
358  if (histogram != NULL)
359  {
360  histogram->updateHistogram();
361 
362  if (tab->getViewer()->isStarsMarked())
363  image_data->findStars();
364  }
365 
366  image->rescale(ZOOM_KEEP_LEVEL);
367  image->updateFrame();
368 
369 }
370 
371 void FITSHistogramCommand::undo()
372 {
373  FITSView *image = tab->getImage();
374  FITSImage *image_data = image->getImageData();
375  memcpy( image_data->getImageBuffer(), buffer, image_data->getWidth() * image_data->getHeight() * sizeof(float));
376  image_data->calculateStats(true);
377 
378 
379 
380  if (histogram != NULL)
381  {
382  histogram->updateHistogram();
383 
384  if (tab->getViewer()->isStarsMarked())
385  image_data->findStars();
386  }
387 
388  image->rescale(ZOOM_KEEP_LEVEL);
389  image->updateFrame();
390 
391 }
392 
393 QString FITSHistogramCommand::text() const
394 {
395 
396  switch (type)
397  {
398  case FITS_AUTO:
399  return i18n("Auto Scale");
400  break;
401  case FITS_LINEAR:
402  return i18n("Linear Scale");
403  break;
404  case FITS_LOG:
405  return i18n("Logarithmic Scale");
406  break;
407  case FITS_SQRT:
408  return i18n("Square Root Scale");
409  break;
410 
411  default:
412  if (type-1 <= FITSViewer::filterTypes.count())
413  return FITSViewer::filterTypes.at(type-1);
414  break;
415  }
416 
417  return i18n("Unknown");
418 
419 }
420 
421 #include "fitshistogram.moc"
FITSHistogramCommand::undo
virtual void undo()
Definition: fitshistogram.cpp:371
FITSView::getImageData
FITSImage * getImageData()
Definition: fitsview.h:98
histogramUI
Definition: fitshistogram.h:39
fitsimage.h
fitstab.h
FITSImage
Definition: fitsimage.h:73
FITSViewer::isStarsMarked
bool isStarsMarked()
Definition: fitsviewer.h:65
FITSImage::findStars
int findStars()
Definition: fitsimage.cpp:985
FITSTab::getImage
FITSView * getImage()
Definition: fitstab.h:43
FITS_AUTO
Definition: fitscommon.h:22
QDialog
FITSHistogram::applyScale
void applyScale()
Definition: fitshistogram.cpp:187
FITSTab::getViewer
FITSViewer * getViewer()
Definition: fitstab.h:45
QWidget
FITS_NORMAL
Definition: fitscommon.h:20
FITSHistogram::constructHistogram
void constructHistogram(int hist_width, int hist_height)
Definition: fitshistogram.cpp:81
FITSView
Definition: fitsview.h:81
FITSHistogram::napply
int napply
Definition: fitshistogram.h:69
FITSHistogram::~FITSHistogram
~FITSHistogram()
Definition: fitshistogram.cpp:76
FITS_SQRT
Definition: fitscommon.h:22
fitsview.h
FITSHistogram::updateIntenFreq
void updateIntenFreq(int x)
Definition: fitshistogram.cpp:254
FITSHistogram::type
FITSScale type
Definition: fitshistogram.h:68
FITSHistogram::updateHistogram
void updateHistogram()
Definition: fitshistogram.cpp:300
FITSView::rescale
int rescale(FITSZoom type)
Definition: fitsview.cpp:222
FITSHistogram::findMax
int findMax(int hist_width)
Definition: fitshistogram.cpp:290
FITSImage::getHeight
long getHeight()
Definition: fitsimage.h:99
fitshistogram.h
FITSHistogramCommand
Definition: fitshistogram.h:98
FITSHistogramCommand::~FITSHistogramCommand
virtual ~FITSHistogramCommand()
Definition: fitshistogram.cpp:317
FITSImage::getMode
FITSMode getMode()
Definition: fitsimage.h:103
FITSImage::applyFilter
void applyFilter(FITSScale type, float *image=NULL, int min=-1, int max=-1)
Definition: fitsimage.cpp:829
FITSHistogram::fits_min
double fits_min
Definition: fitshistogram.h:72
FITSScale
FITSScale
Definition: fitscommon.h:22
FITSImage::getMin
double getMin()
Definition: fitsimage.h:94
FITSHistogram::fits_max
double fits_max
Definition: fitshistogram.h:72
FITSHistogram::binWidth
double binWidth
Definition: fitshistogram.h:71
FITSHistogramCommand::text
virtual QString text() const
Definition: fitshistogram.cpp:393
histogramUI::histogramUI
histogramUI(QDialog *parent=0)
Definition: fitshistogram.cpp:48
FITSHistogram::histFactor
double histFactor
Definition: fitshistogram.h:70
FITS_LINEAR
Definition: fitscommon.h:22
FITSHistogramCommand::FITSHistogramCommand
FITSHistogramCommand(QWidget *parent, FITSHistogram *inHisto, FITSScale newType, int lmin, int lmax)
Definition: fitshistogram.cpp:306
FITSHistogram::maxSliderUpdated
void maxSliderUpdated(int value)
Definition: fitshistogram.cpp:243
FITSTab::getUndoStack
QUndoStack * getUndoStack()
Definition: fitstab.h:41
FITSHistogram::updateLowerLimit
void updateLowerLimit()
Definition: fitshistogram.cpp:264
ZOOM_KEEP_LEVEL
Definition: fitscommon.h:23
FITS_LOG
Definition: fitscommon.h:22
FITSImage::getImageBuffer
float * getImageBuffer()
Definition: fitsimage.h:91
FITSHistogram::FITSHistogram
FITSHistogram(QWidget *parent)
Definition: fitshistogram.cpp:55
FITSImage::getWidth
long getWidth()
Definition: fitsimage.h:98
FITSHistogram
Definition: fitshistogram.h:48
FITSImage::getMinMax
void getMinMax(double *min, double *max)
Definition: fitsimage.h:93
fitsviewer.h
FITSHistogram::minSliderUpdated
void minSliderUpdated(int value)
Definition: fitshistogram.cpp:232
FITSView::updateFrame
void updateFrame()
Definition: fitsview.cpp:352
FITSHistogram::updateBoxes
void updateBoxes(int lowerLimit, int upperLimit)
Definition: fitshistogram.cpp:170
FITSHistogramCommand::redo
virtual void redo()
Definition: fitshistogram.cpp:323
FITSTab
Definition: fitstab.h:31
FITSHistogram::tab
FITSTab * tab
Definition: fitshistogram.h:73
FITSViewer::filterTypes
static QStringList filterTypes
Definition: fitsviewer.h:71
FITSImage::getSize
void getSize(double *w, double *h)
Definition: fitsimage.h:92
FITSHistogram::updateUpperLimit
void updateUpperLimit()
Definition: fitshistogram.cpp:277
FITSImage::calculateStats
void calculateStats(bool refresh=false)
Definition: fitsimage.cpp:387
FITSImage::getStdDev
double getStdDev()
Definition: fitsimage.h:100
FITSImage::getAverage
double getAverage()
Definition: fitsimage.h:101
FITSHistogram::applyFilter
void applyFilter(FITSScale ftype)
Definition: fitshistogram.cpp:215
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