All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ContrastWizardPage.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/rp/ContrastWizardPage.cpp
22 
23  \brief This file defines a class for a Contrast Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../raster/Raster.h"
31 #include "ContrastWizardPage.h"
32 #include "RasterHistogramDialog.h"
33 #include "RasterNavigatorWidget.h"
34 #include "ui_ContrastWizardPageForm.h"
35 
36 // Qt
37 #include <QGridLayout>
38 #include <QtGui/QMessageBox>
39 #include <QtGui/QCheckBox>
40 
41 // Boost
42 #include <boost/lexical_cast.hpp>
43 
44 //STL
45 #include <memory>
46 
48  : QWizardPage(parent),
49  m_ui(new Ui::ContrastWizardPageForm),
50  m_layer(0)
51 {
52 // setup controls
53  m_ui->setupUi(this);
54 
56 
57 //build form
58  QGridLayout* displayLayout = new QGridLayout(m_ui->m_frame);
60  m_navigator->showAsPreview(true);
61  m_navigator->hideColorCompositionTool(true);
62  displayLayout->addWidget(m_navigator.get());
63  displayLayout->setContentsMargins(0,0,0,0);
64 
66 
67 //connects
68  connect(m_ui->m_histogramPushButton, SIGNAL(clicked()), this, SLOT(showHistogram()));
69  connect(m_ui->m_contrastTypeComboBox, SIGNAL(activated(int)), this, SLOT(onContrastTypeComboBoxActivated(int)));
70  connect(m_navigator.get(), SIGNAL(previewClicked()), this, SLOT(apply()));
71 
72 //configure page
73  this->setTitle(tr("Contrast"));
74  this->setSubTitle(tr("Select the type of contrast and set their specific parameters."));
75 
76  onContrastTypeComboBoxActivated(m_ui->m_contrastTypeComboBox->currentIndex());
77 
78  m_ui->m_histogramPushButton->setEnabled(false);
79  m_ui->m_histogramPushButton->setVisible(false);
80 }
81 
83 {
84 }
85 
87 {
88  int nBands = m_ui->m_bandTableWidget->rowCount();
89 
90  for(int i = 0; i < nBands; ++i)
91  {
92  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
93 
94  if(checkBox->isChecked())
95  {
96  return true;
97  }
98  }
99 
100  return false;
101 }
102 
104 {
105  m_layer = layer;
106 
107  std::list<te::map::AbstractLayerPtr> list;
108 
109  list.push_back(m_layer);
110 
111  m_navigator->set(m_layer);
112 
113  m_histogramDlg->set(m_layer);
114 
115  listBands();
116 }
117 
119 {
120  return m_layer;
121 }
122 
124 {
125  //get contrast type
126  int index = m_ui->m_contrastTypeComboBox->currentIndex();
127  int contrastType = m_ui->m_contrastTypeComboBox->itemData(index).toInt();
128  int nBands = m_ui->m_bandTableWidget->rowCount();
129 
130  te::rp::Contrast::InputParameters algoInputParams;
131 
133  {
135 
136  for(int i = 0; i < nBands; ++i)
137  {
138  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
139 
140  if(checkBox->isChecked())
141  {
142  QString valueMin = m_ui->m_bandTableWidget->item(i, 1)->text();
143  algoInputParams.m_lCMinInput.push_back(valueMin.toDouble());
144 
145  QString valueMax = m_ui->m_bandTableWidget->item(i, 2)->text();
146  algoInputParams.m_lCMaxInput.push_back(valueMax.toDouble());
147  }
148  }
149  }
151  {
153 
154  for(int i = 0; i < nBands; ++i)
155  {
156  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
157 
158  if(checkBox->isChecked())
159  {
160  QString valueMax = m_ui->m_bandTableWidget->item(i, 1)->text();
161  algoInputParams.m_hECMaxInput.push_back(valueMax.toDouble());
162  }
163  }
164  }
166  {
168 
169  for(int i = 0; i < nBands; ++i)
170  {
171  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
172 
173  if(checkBox->isChecked())
174  {
175  QString valueMean = m_ui->m_bandTableWidget->item(i, 1)->text();
176  algoInputParams.m_sMASCMeanInput.push_back(valueMean.toDouble());
177 
178  QString valueStdDev = m_ui->m_bandTableWidget->item(i, 2)->text();
179  algoInputParams.m_sMASCStdInput.push_back(valueStdDev.toDouble());
180  }
181  }
182  }
183 
184  for(int i = 0; i < nBands; ++i)
185  {
186  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
187 
188  if(checkBox->isChecked())
189  {
190  algoInputParams.m_inRasterBands.push_back(i);
191  }
192  }
193 
194  return algoInputParams;
195 }
196 
198 {
199  QApplication::setOverrideCursor(Qt::WaitCursor);
200 
201  //get preview raster
202  te::rst::Raster* inputRst = m_navigator->getExtentRaster();
203 
204  //set contrast parameters
205  te::rp::Contrast::InputParameters algoInputParams = getInputParams();
206 
207  algoInputParams.m_inRasterPtr = inputRst;
208 
209  te::rp::Contrast::OutputParameters algoOutputParams;
210 
211  std::map<std::string, std::string> rinfo;
212  rinfo["MEM_RASTER_NROWS"] = inputRst->getNumberOfRows();
213  rinfo["MEM_RASTER_NCOLS"] = inputRst->getNumberOfColumns();
214  rinfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast<std::string>(inputRst->getBandDataType(0));
215  rinfo["MEM_RASTER_NBANDS"] = boost::lexical_cast<std::string>(inputRst->getNumberOfBands());
216 
217  algoOutputParams.m_createdOutRasterDSType = "MEM";
218  algoOutputParams.m_createdOutRasterInfo = rinfo;
219 
220  //run contrast
221  te::rp::Contrast algorithmInstance;
222 
223  try
224  {
225  if(algorithmInstance.initialize(algoInputParams))
226  {
227  if(algorithmInstance.execute(algoOutputParams))
228  {
229  m_navigator->drawRaster(algoOutputParams.m_outRasterPtr);
230  }
231  }
232  }
233  catch(...)
234  {
235  QMessageBox::warning(this, tr("Warning"), tr("Constrast error."));
236  }
237 
238  QApplication::restoreOverrideCursor();
239 
240  //delete input raster dataset
241  delete inputRst;
242 }
243 
245 {
246  m_ui->m_contrastTypeComboBox->clear();
247 
248  m_ui->m_contrastTypeComboBox->addItem(tr("Linear"), te::rp::Contrast::InputParameters::LinearContrastT);
249  m_ui->m_contrastTypeComboBox->addItem(tr("Histogram Equalization"), te::rp::Contrast::InputParameters::HistogramEqualizationContrastT);
250  m_ui->m_contrastTypeComboBox->addItem(tr("Mean and Standard Deviation"), te::rp::Contrast::InputParameters::SetMeanAndStdContrastT);
251 }
252 
254 {
255  assert(m_layer.get());
256 
257  //get input raster
258  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
259 
260  if(ds.get())
261  {
262  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
263 
264  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
265 
266  if(inputRst.get())
267  {
268  for(unsigned int i = 0; i < inputRst->getNumberOfBands(); ++i)
269  {
270  m_ui->m_bandTableWidget->setRowCount(0);
271 
272  // initializing the list of bands
273  for(unsigned b = 0 ; b < inputRst->getNumberOfBands(); b++)
274  {
275  int newrow = m_ui->m_bandTableWidget->rowCount();
276  m_ui->m_bandTableWidget->insertRow(newrow);
277 
278  QString bName(tr("Band "));
279  bName.append(QString::number(b));
280 
281  QCheckBox* bandCheckBox = new QCheckBox(bName, this);
282 
283  //if(inputRst->getNumberOfBands() == 1)
284  bandCheckBox->setChecked(true);
285 
286  m_ui->m_bandTableWidget->setCellWidget(newrow, 0, bandCheckBox);
287  }
288  }
289  }
290  }
291 
292  m_ui->m_bandTableWidget->resizeColumnsToContents();
293  m_ui->m_bandTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
294 
295  onContrastTypeComboBoxActivated(m_ui->m_contrastTypeComboBox->currentIndex());
296 }
297 
299 {
300  assert(m_layer.get());
301 
302  m_histogramDlg->show();
303 }
304 
306 {
307  int contrastType = m_ui->m_contrastTypeComboBox->itemData(index).toInt();
308 
310  {
311  QStringList list;
312  list.append(tr("Band"));
313  list.append(tr("Minimum"));
314  list.append(tr("Maximum"));
315 
316  m_ui->m_bandTableWidget->setColumnCount(3);
317  m_ui->m_bandTableWidget->setHorizontalHeaderLabels(list);
318 
319  int nBands = m_ui->m_bandTableWidget->rowCount();
320 
321  for(int i = 0; i < nBands; ++i)
322  {
323  QTableWidgetItem* itemMin = new QTableWidgetItem("0");
324  itemMin->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
325  m_ui->m_bandTableWidget->setItem(i, 1, itemMin);
326 
327  QTableWidgetItem* itemMax = new QTableWidgetItem("255");
328  itemMax->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
329  m_ui->m_bandTableWidget->setItem(i, 2, itemMax);
330  }
331  }
333  {
334  QStringList list;
335  list.append(tr("Band"));
336  list.append(tr("Maximum"));
337 
338  m_ui->m_bandTableWidget->setColumnCount(2);
339  m_ui->m_bandTableWidget->setHorizontalHeaderLabels(list);
340 
341  int nBands = m_ui->m_bandTableWidget->rowCount();
342 
343  for(int i = 0; i < nBands; ++i)
344  {
345  QTableWidgetItem* itemMax = new QTableWidgetItem("255");
346  itemMax->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
347  m_ui->m_bandTableWidget->setItem(i, 1, itemMax);
348  }
349  }
351  {
352  QStringList list;
353  list.append(tr("Band"));
354  list.append(tr("Mean"));
355  list.append(tr("Std Dev"));
356 
357  m_ui->m_bandTableWidget->setColumnCount(3);
358  m_ui->m_bandTableWidget->setHorizontalHeaderLabels(list);
359 
360  int nBands = m_ui->m_bandTableWidget->rowCount();
361 
362  for(int i = 0; i < nBands; ++i)
363  {
364  QTableWidgetItem* itemMean = new QTableWidgetItem("127");
365  itemMean->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
366  m_ui->m_bandTableWidget->setItem(i, 1, itemMean);
367 
368  QTableWidgetItem* itemStdDev = new QTableWidgetItem("50");
369  itemStdDev->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
370  m_ui->m_bandTableWidget->setItem(i, 2, itemStdDev);
371  }
372  }
373 
374  m_ui->m_bandTableWidget->resizeColumnsToContents();
375  m_ui->m_bandTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
376 }
This file defines a class for a RasterHistogram Dialog.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Contrast.cpp:147
This file has the RasterNavigatorWidget class.
te::rst::Raster * m_outRasterPtr
A pointer to a valid initiated raster instance where the result must be written, leave NULL to create...
Definition: Contrast.h:122
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
Contrast enhancement.
Definition: Contrast.h:56
Contrast output parameters.
Definition: Contrast.h:118
std::auto_ptr< te::qt::widgets::RasterNavigatorWidget > m_navigator
std::vector< unsigned int > m_inRasterBands
Bands to be processed from the input raster.
Definition: Contrast.h:91
This class is used to navigate over a DataSetLayer (having a raster representation) and given a set o...
std::vector< double > m_lCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:81
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
std::map< std::string, std::string > m_createdOutRasterInfo
The necessary information to create the raster (as described in te::raster::RasterFactory), leave empty if the result must be written to the raster pointed m_outRasterPtr.
Definition: Contrast.h:130
This class is a dialog for the RasterHistogram widget.
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Definition: Contrast.h:128
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Contrast.h:89
std::auto_ptr< Ui::ContrastWizardPageForm > m_ui
void set(te::map::AbstractLayerPtr layer)
This method is used to set the selected layer for contrast operation.
std::vector< double > m_lCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:79
ContrastType m_type
The contrast type to be applied.
Definition: Contrast.h:77
Contrast input parameters.
Definition: Contrast.h:64
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
std::vector< double > m_hECMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:83
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
te::rp::Contrast::InputParameters getInputParams()
std::auto_ptr< te::qt::widgets::RasterHistogramDialog > m_histogramDlg
An abstract class for raster data strucutures.
Definition: Raster.h:70
std::vector< double > m_sMASCMeanInput
The mean greyscale to be applied in each band.
Definition: Contrast.h:85
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::vector< double > m_sMASCStdInput
The standard deviation to be applied in each band.
Definition: Contrast.h:87
This file defines a class for a Contrast Wizard page.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Contrast.cpp:248