summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/fitshistogram.cpp
blob: 399aa548d3dea7baec030e4b8d85e88b117dff5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/***************************************************************************
                          fitshistogram.cpp  -  FITS Historgram
                          -----------------
    begin                : Thu Mar 4th 2004
    copyright            : (C) 2004 by Jasem Mutlaq
    email                : mutlaqja@ikarustech.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
 #include "fitshistogram.h"
 #include "fitsviewer.h"
 #include "fitsimage.h"
 #include "indi/fitsrw.h"
 
 #include <math.h>
 #include <stdlib.h>
 
 #include <qpainter.h>
 #include <qslider.h>
 #include <qcursor.h>
 #include <qpen.h>
 #include <qpixmap.h>
 #include <qradiobutton.h>
 #include <qpushbutton.h>
 
 #include <kdebug.h>
 #include <klineedit.h>
 #include <klocale.h>
 
 
 
 FITSHistogram::FITSHistogram(QWidget *parent, const char * name) : histDialog(parent, name)
 {
   viewer = (FITSViewer *) parent;
   
   setModal(false);
   
   minSlider->setMinValue(0);
   minSlider->setMaxValue(BARS-1);
   minSlider->setValue(0);
   
   maxSlider->setMinValue(0);
   maxSlider->setMaxValue(BARS-1);
   maxSlider->setValue(BARS-1);
   
   type = 0;
   napply=0;
   
   histFrame->setCursor(Qt::CrossCursor);
   histFrame->setMouseTracking(true);
   setMouseTracking(true);
   
   connect(minSlider, SIGNAL(valueChanged(int)), this, SLOT(updateBoxes()));
   connect(minSlider, SIGNAL(valueChanged(int)), this, SLOT(updateIntenFreq(int )));
   connect(maxSlider, SIGNAL(valueChanged(int)), this, SLOT(updateBoxes()));
   connect(maxSlider, SIGNAL(valueChanged(int)), this, SLOT(updateIntenFreq(int )));
   connect(applyB, SIGNAL(clicked()), this, SLOT(applyScale()));

   constructHistogram(viewer->imgBuffer);
   
   updateBoxes();
   
}
 
 FITSHistogram::~FITSHistogram() {}
 
void FITSHistogram::updateBoxes()
{
        if (minSlider->value() == BARS)
	 minOUT->setText(QString("%1").arg((int) viewer->stats.max));
	else
   	 minOUT->setText(QString("%1").arg( (int) ( ceil (minSlider->value() * binSize) + viewer->stats.min)));
	 
	if (maxSlider->value() == BARS)
	 maxOUT->setText(QString("%1").arg((int) viewer->stats.max));
	else
   	 maxOUT->setText(QString("%1").arg( (int) ( ceil (maxSlider->value() * binSize) + viewer->stats.min)));

        update();
}

void FITSHistogram::applyScale()
{
  int swap;
  int min = minSlider->value();
  int max = maxSlider->value();
  
  FITSHistogramCommand *histC;
  //kdDebug() << "Width " << viewer->image->width << endl;
  
  if (min > max)
  {
    swap = min;
    min  = max;
    max  = swap;
  }
  
   min  = (int) (min * binSize + viewer->stats.min);
   max  = (int) (max * binSize + viewer->stats.min);

  
  napply++;
  
  // Auto
  if (autoR->isOn())
    type = 0;
  // Linear
  else if (linearR->isOn())
    type = 1;
  // Log
  else if (logR->isOn())
    type = 2;
  // Exp
  else if (sqrtR->isOn())
    type = 3;
  
  histC = new FITSHistogramCommand(viewer, this, type, min, max);
  viewer->history->addCommand(histC);
    
}
 
void FITSHistogram::constructHistogram(float * buffer)
{
  int maxHeight = 0;
  int height    = histFrame->height(); 
  int id;
  int index;
  int range = (int) (viewer->stats.max - viewer->stats.min);
    
  for (int i=0; i < BARS; i++)
      histArray[i] = 0;
    binSize = ( (double) range / (double) BARS); 
    
    //kdDebug() << "#2" << endl; 
    if (binSize == 0 || buffer == NULL)
     return;
    
     for (int i=0; i < viewer->stats.width * viewer->stats.height; i++)
     {
        id = (int) ((buffer[i] - viewer->stats.min) / binSize);
        if (id >= BARS) id = BARS - 1;
        histArray[id]++;
	
     }
     
     if (binSize < 1)
     for (int i=0; i < BARS - 1; i++)
         if (histArray[i] == 0)
	 {
	    index = (int) ceil(i * binSize);
            if (index == (int) (ceil ((i+1) * binSize)))
                histArray[i] = histArray[i+1];
         }
  
 maxHeight = findMax() / height;
 
 kdDebug() << "Maximum height is " << maxHeight << " -- binsize " << binSize << endl;
 
 histogram = new QPixmap(500, 150, 1);
 histogram->fill(Qt::black);
 QPainter p(histogram);
 QPen pen( white, 1);
 p.setPen(pen);
   
 for (int i=0; i < BARS; i++)
     p.drawLine(i, height , i, height - (int) ((double) histArray[i] / (double) maxHeight)); 
  
}

void FITSHistogram::paintEvent( QPaintEvent */*e*/)
{
  int height    = histFrame->height(); 
  int xMin = minSlider->value(), xMax = maxSlider->value();
  
  QPainter p(histFrame);
  QPen pen;
  pen.setWidth(1);
  
  bitBlt(histFrame, 0, 0, histogram);
  
  pen.setColor(blue);
  p.setPen(pen);
  
  p.drawLine(xMin, height - 2, xMin, height/2 -2);
  pen.setColor(red);
  p.setPen(pen);
  p.drawLine(xMax, 2, xMax, height/2 -2);
  
  
}

void FITSHistogram::mouseMoveEvent( QMouseEvent *e)
{
  int x = e->x();
  int y = e->y();
  
  x -= histFrame->x();
  y -= histFrame->y();
  
  if (x < 0 || x >= BARS || y < 0 || y > histFrame->height() )
   return;
  
  updateIntenFreq(x);
  
}

void FITSHistogram::updateIntenFreq(int x)
{

  int index = (int) ceil(x * binSize);
    
  intensityOUT->setText(QString("%1").arg((int) ( index + viewer->stats.min)));
  
  frequencyOUT->setText(QString("%1").arg(histArray[x]));
  
}


int FITSHistogram::findMax()
{
  int max =0;
  
  for (int i=0; i < BARS; i++)
    if (histArray[i] > max) max = histArray[i];
    
  return max;
}

FITSHistogramCommand::FITSHistogramCommand(QWidget * parent, FITSHistogram *inHisto, int newType, int lmin, int lmax)
{
  viewer    = (FITSViewer *) parent;
  type      = newType;
  histo     = inHisto;
  oldImage  = new QImage();
  // TODO apply maximum compression against this buffer
  buffer = (float *) malloc (viewer->image->width * viewer->image->height * sizeof(float));
   
  //if (buffer == NULL)
   //return;
   min = lmin;
   max = lmax;
}

FITSHistogramCommand::~FITSHistogramCommand() 
{
  free(buffer);
  delete (oldImage);
}
            
void FITSHistogramCommand::execute()
{
  float val, bufferVal;
  double coeff;
  FITSImage *image = viewer->image;
  int width  = image->width;
  int height = image->height;
  
  memcpy(buffer, viewer->imgBuffer,image->width * image->height * sizeof(float));
  *oldImage = image->displayImage->copy();
 
  switch (type)
  {
    case FITSImage::FITSAuto:
    case FITSImage::FITSLinear:
    for (int i=0; i < height; i++)
      for (int j=0; j < width; j++)
      {
              bufferVal = viewer->imgBuffer[i * width + j];
	      if (bufferVal < min) bufferVal = min;
	      else if (bufferVal > max) bufferVal = max;
	      //val = (int) (255. * ((double) (bufferVal - min) / (double) (max - min)));
	      //val = (int) (max * ((double) (bufferVal - min) / (double) (max - min)));
	      //val = (int) (bufferVal - min) * (max - min) + min;
	      //if (val < min) val = min;
	      //else if (val > max) val = max;
	      //image->reducedImgBuffer[i * width + j] = val;
	      viewer->imgBuffer[i * width + j] = bufferVal;
      	      
      }
     break;
     
    case FITSImage::FITSLog:
    //coeff = 255. / log(1 + max);
    coeff = max / log(1 + max);
    
    for (int i=0; i < height; i++)
      for (int j=0; j < width; j++)
      {
              bufferVal = viewer->imgBuffer[i * width + j];
	      if (bufferVal < min) bufferVal = min;
	      else if (bufferVal > max) bufferVal = max;
	      val = (coeff * log(1 + bufferVal));
	      if (val < min) val = min;
	      else if (val > max) val = max;
	      viewer->imgBuffer[i * width + j] = val;
	      //image->reducedImgBuffer[i * width + j] = val;
      	      //displayImage->setPixel(j, height - i - 1, val);
      }
      break;
      
    case FITSImage::FITSSqrt:
    //coeff = 255. / sqrt(max);
    coeff = max / sqrt(max);
    
    for (int i=0; i < height; i++)
      for (int j=0; j < width; j++)
      {
              bufferVal = (int) viewer->imgBuffer[i * width + j];
	      if (bufferVal < min) bufferVal = min;
	      else if (bufferVal > max) bufferVal = max;
	      val = (int) (coeff * sqrt(bufferVal));
	      //image->reducedImgBuffer[i * width + j] = val;
	      viewer->imgBuffer[i * width + j] = val;
      	      //displayImage->setPixel(j, height - i - 1, val);
      }
      
      break;
    
     
    default:
     break;
  }
       
   //int lmin= image->reducedImgBuffer[0];
   float lmin= viewer->imgBuffer[0];
   float lmax= viewer->imgBuffer[0];
   int totalPix = width * height;
   
   for (int i=1; i < totalPix; i++)
    //if ( image->reducedImgBuffer[i] < lmin) lmin = image->reducedImgBuffer[i];
    if ( viewer->imgBuffer[i] < lmin) lmin = viewer->imgBuffer[i];
    else if (viewer->imgBuffer[i] > lmax) lmax = viewer->imgBuffer[i];
    
   double datadiff = 255.;
   double pixdiff = lmax - lmin;
   double offs = -lmin * datadiff / pixdiff;
   double scale = datadiff / pixdiff;
   int tdata = 0;
  
     
 for (int i=0; i < height; i++)
  for (int j=0; j < width; j++)
  {
           //bp8 = (FITS_BITPIX8) image->reducedImgBuffer[i * width + j];
	   //bp8 = (FITS_BITPIX8) 
           tdata = (long) (viewer->imgBuffer[i * width + j] * scale + offs);
           if (tdata < 0) tdata = 0;
           else if (tdata > 255) tdata = 255;
	   image->displayImage->setPixel(j, height - i - 1, tdata);
  }
  
  viewer->calculateStats();
  
  //viewer->updateImgBuffer();
  if (histo != NULL)
  {
  	histo->constructHistogram(viewer->imgBuffer);
  	histo->update();
  	histo->updateBoxes();
  }
  
  viewer->image->zoomToCurrent();
  viewer->fitsChange();
  
}

void FITSHistogramCommand::unexecute()
{
  memcpy( viewer->imgBuffer, buffer, viewer->image->width * viewer->image->height * sizeof(float));
  viewer->calculateStats();
  *viewer->image->displayImage = oldImage->copy();
  viewer->image->zoomToCurrent();
  
  if (histo != NULL)
  {
  	histo->constructHistogram(viewer->imgBuffer);
  	histo->update();
  	histo->updateBoxes();
  }
}

QString FITSHistogramCommand::name() const
{

 switch (type)
 {
  case 0:
    return i18n("Auto Scale");
    break;
  case 1:
    return i18n("Linear Scale");
    break;
  case 2:
    return i18n("Logarithmic Scale");
    break;  
  case 3:
    return i18n("Square Root Scale");
    break;
  default:
   break;
 }
 
 return i18n("Unknown");

}



 

#include "fitshistogram.moc"