Skip to content
Snippets Groups Projects
Commit 67d29661 authored by Antoine Lorence's avatar Antoine Lorence
Browse files

Quantification values are now signed

This allows quantification maps to apply on negative values
parent 4f2fe5c5
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ using namespace imagein;
Quantification::Quantification(int size_) {
this->size = size_;
threshold = new int[this->size - 1];
values = new imagein::Image::depth_t[this->size];
values = new int[this->size];
}
Quantification::Quantification(std::string filename) {
......@@ -39,7 +39,7 @@ Quantification::Quantification(std::string filename) {
this->size++;
if(this->size < 2) throw exception();
threshold = new int[this->size - 1];
values = new imagein::Image::depth_t[this->size];
values = new int[this->size];
for(int i = 0; i < size - 1; ++i) {
double n;
file >> n;
......@@ -68,9 +68,8 @@ void Quantification::saveAs(std::string filename) {
}
}
Quantifier::Quantifier(Quantification quant) {
for(int i = 0; i < 256; ++i) {
for(int i = 0; i < N_MAX_THRESHOLD; ++i) {
values[i] = quant.valueOf(i);
}
}
......@@ -80,11 +79,11 @@ Quantification Quantification::linearQuant(int size) {
Quantification quant(size);
for(int i = 0; i < size - 1; ++i) {
quant.threshold[i] = floor( (i + 1) * 256. / size + 0.5);
quant.threshold[i] = floor( (i + 1) * (float)N_MAX_THRESHOLD / size + 0.5);
}
if(size > 0) {
quant.values[0] = floor( quant.threshold[0] / 2. + 0.5 );
quant.values[size - 1] = floor( (256. + quant.threshold[size - 2]) / 2. + 0.5 );
quant.values[size - 1] = floor( ((float)N_MAX_THRESHOLD + quant.threshold[size - 2]) / 2. + 0.5 );
}
for(int i = 1; i < size - 1; ++i) {
quant.values[i] = floor( (double)(quant.threshold[i] + quant.threshold[i-1]) / 2. + 0.5 );
......@@ -93,7 +92,6 @@ Quantification Quantification::linearQuant(int size) {
return quant;
}
Quantification Quantification::nonLinearQuant(int size, const Image* image, unsigned int c) {
Quantification quant(size);
......@@ -114,7 +112,7 @@ Quantification Quantification::nonLinearQuant(int size, const Image* image, unsi
if(size > 0) {
quant.values[0] = floor( quant.threshold[0] / 2. + 0.5 );
quant.values[size - 1] = floor( (256. + quant.threshold[size - 2]) / 2. + 0.5 );
quant.values[size - 1] = floor( ((float)N_MAX_THRESHOLD + quant.threshold[size - 2]) / 2. + 0.5 );
}
for(int i = 1; i < size - 1; ++i) {
quant.values[i] = floor( (double)(quant.threshold[i] + quant.threshold[i-1]) / 2. + 0.5 );
......@@ -142,7 +140,7 @@ Quantification Quantification::nonLinearQuantOptimized(int size, const Image* im
if(size > 0) {
quant.values[0] = floor( quant.threshold[0] / 2. + 0.5 );
quant.values[size - 1] = floor( (256. + quant.threshold[size - 2]) / 2. + 0.5 );
quant.values[size - 1] = floor( ((float)N_MAX_THRESHOLD + quant.threshold[size - 2]) / 2. + 0.5 );
}
for(int i = 1; i < size - 1; ++i) {
quant.values[i] = floor( (double)(quant.threshold[i] + quant.threshold[i-1]) / 2. + 0.5 );
......@@ -157,10 +155,8 @@ Quantification Quantification::nonLinearQuantOptimized(int size, const Image* im
nb_points += histogram[j];
}
quant.values[0] = som_lum / nb_points + 0.5;
for(int i = 0; i < size - 2; ++i){
som_lum = 0;
nb_points = 0;
......@@ -171,10 +167,9 @@ Quantification Quantification::nonLinearQuantOptimized(int size, const Image* im
quant.values[i+1] = som_lum / nb_points + 0.5;
}
som_lum = 0;
nb_points = 0;
for(int j = quant.threshold[size-2]; j < 256; ++j) {
for(int j = quant.threshold[size-2]; j < N_MAX_THRESHOLD; ++j) {
som_lum += histogram[j] * j;
nb_points += histogram[j];
}
......
......@@ -30,9 +30,12 @@ struct Quantification {
static Quantification nonLinearQuantOptimized(int size, const imagein::Image *image, unsigned int c);
explicit Quantification(std::string filename);
void saveAs(std::string filename);
explicit Quantification(int size_);
inline imagein::Image::depth_t valueOf(imagein::Image::depth_t value) const {
inline int valueOf(int value) const {
for(int i = 0; i < this->size - 1; ++i) {
if(value < this->threshold[i]) {
return this->values[i];
......@@ -42,16 +45,16 @@ struct Quantification {
}
int size;
int* threshold;
imagein::Image::depth_t* values;
int* values;
};
struct Quantifier {
Quantifier(Quantification quant);
inline imagein::Image::depth_t valueOf(imagein::Image::depth_t value) {
inline int valueOf(imagein::Image::depth_t value) {
return this->values[value];
}
private:
imagein::Image::depth_t values[256];
int values[N_MAX_THRESHOLD];
};
#endif // QUANTIFICATION_H
......@@ -49,7 +49,6 @@ QuantificationDialog::QuantificationDialog(QWidget *parent, QString imgName) :
_sizeBox->setRange(2, 256);
_sizeBox->setValue(2);
_quantBox = new QComboBox();
_quantBox->addItem(tr("Linear with centered value"));
if(!_editorOnly) {
......@@ -91,14 +90,13 @@ QuantificationDialog::QuantificationDialog(QWidget *parent, QString imgName) :
}
}
void QuantificationDialog::methodChanged(int method) {
_editorWidget->setVisible((_editorOnly && method == 1) || (!_editorOnly && method == 3));
_saveButton->setEnabled(_editorOnly || method==3 || method == 0);
this->adjustSize();
}
Quantification QuantificationDialog::getQuantif(const Image* image, unsigned int c) {
Quantification QuantificationDialog::getQuantif(const Image* image, unsigned int c) {
int size = _sizeBox->value();
if(_editorOnly) return Quantification::linearQuant(size);
switch(_quantBox->currentIndex()) {
......
......@@ -26,6 +26,7 @@
#include "Quantification.h"
#include "QuantificationWidget.h"
#include <QPushButton>
class QuantificationDialog : public QDialog
{
Q_OBJECT
......
......@@ -56,21 +56,21 @@ QuantificationWidget::CentralWidget::CentralWidget(QWidget *parent) :
_thresholdBoxes[i] = new QSpinBox(this);
_thresholdBoxes[i]->setFixedSize(64, 28);
_thresholdBoxes[i]->move(QPoint((_firstWidth-64)/2, i * 32 + 16 + _yOffset));
_thresholdBoxes[i]->setRange(0, 255);
_thresholdBoxes[i]->setRange(-254, 255);
if(i >= _nThreshold) _thresholdBoxes[i]->setVisible(false);
}
for(int i = 0; i < N_MAX_THRESHOLD + 1; ++i) {
_valueBoxes[i] = new QSpinBox(this);
_valueBoxes[i]->setRange(0, 255);
_valueBoxes[i]->setFixedSize(64, 28);
_valueBoxes[i]->move(QPoint(_firstWidth + 32 + (_secondWidth-64)/2, i * 32 + _yOffset));
_valueBoxes[i]->setRange(-254, 255);
if(i > _nThreshold) _valueBoxes[i]->setVisible(false);
}
// editorLayout->addWidget(_valueBoxes[32], 32, 1);
// this->setFixedSize(_firstWidth + 32 + _secondWidth, _nThreshold * 32 + _yOffset + 28);
}
void QuantificationWidget::CentralWidget::paintEvent (QPaintEvent *e)
void QuantificationWidget::CentralWidget::paintEvent(QPaintEvent *)
{
QPainter *painter = new QPainter (this);
QPen pen(Qt::darkGray, 2, Qt::DashLine);
......@@ -82,13 +82,13 @@ void QuantificationWidget::CentralWidget::paintEvent (QPaintEvent *e)
}
painter->end ();
}
QSize QuantificationWidget::sizeHint () const {
return this->widget()->size();
}
QSize QuantificationWidget::CentralWidget::sizeHint () const {
return QSize(_firstWidth + 32 + _secondWidth, _nThreshold * 32 + _yOffset + 28);
}
void QuantificationWidget::setNbThreshold(int n) {
......@@ -103,7 +103,6 @@ void QuantificationWidget::setNbThreshold(int n) {
this->parentWidget()->parentWidget()->adjustSize();
}
void QuantificationWidget::CentralWidget::setNbThreshold(int n) {
this->_nThreshold = min(N_MAX_THRESHOLD, n);
for(int i = 0; i < N_MAX_THRESHOLD; ++i) {
......@@ -118,6 +117,7 @@ void QuantificationWidget::CentralWidget::setNbThreshold(int n) {
void QuantificationWidget::setQuantif(Quantification q) {
this->_centralWidget->setQuantif(q);
}
void QuantificationWidget::CentralWidget::setQuantif(Quantification q) {
this->_nThreshold = q.size - 1;
......@@ -136,8 +136,6 @@ void QuantificationWidget::CentralWidget::setQuantif(Quantification q) {
this->updateGeometry();
}
Quantification QuantificationWidget::getQuantif() const {
return _centralWidget->getQuantif();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment