Skip to content
Snippets Groups Projects
Commit a381753b authored by Sacha Percot-Tétu's avatar Sacha Percot-Tétu
Browse files

Added Translation operation

parent 9a9b99e6
No related branches found
No related tags found
No related merge requests found
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QDialog>
#include <QSpinBox>
#include <QCheckBox>
#include "ImgWidget.h"
#include "TranslateOp.h"
using namespace std;
using namespace imagein;
TranslateOp::TranslateOp() : Operation("Translation") {
}
bool TranslateOp::needCurrentImg() {
return true;
}
std::vector<QWidget*> TranslateOp::operator()(const Image* img, const map<const Image*, string>& imgList) {
vector<QWidget*> result;
QString imgName(imgList.find(img)->second.c_str());
QDialog* dialog = new QDialog();
dialog->setWindowTitle(QString(dialog->tr("Translating %1")).arg(imgName));
dialog->setMinimumWidth(180);
QFormLayout* layout = new QFormLayout();
dialog->setLayout(layout);
layout->setSizeConstraint(QLayout::SetFixedSize);
QSpinBox* xSpinBox = new QSpinBox(dialog);
QSpinBox* ySpinBox = new QSpinBox(dialog);
QCheckBox* expandBox = new QCheckBox("Expand image", dialog);
QSpinBox* valueSpinBox = new QSpinBox(dialog);
xSpinBox->setRange(-65536, 65535);
ySpinBox->setRange(-65536, 65535);
valueSpinBox->setRange(0, 255);
valueSpinBox->setValue(0);
layout->insertRow(0, "X offset : ", xSpinBox);
layout->insertRow(1, "Y offset : ", ySpinBox);
layout->insertRow(2, expandBox);
layout->insertRow(3, "Fill value : ", valueSpinBox);
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
layout->insertRow(4, buttonBox);
QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
QDialog::DialogCode code = static_cast<QDialog::DialogCode>(dialog->exec());
if(code!=QDialog::Accepted) return result;
int dx = xSpinBox->value();
int dy = ySpinBox->value();
Image::depth_t fillValue = valueSpinBox->value();
Image* resImg;
if(expandBox->isChecked()) {
resImg = new Image(img->getWidth() + abs(dx), img->getHeight() + abs(dy), img->getNbChannels(), fillValue);
dx = max(0, dx);
dy = max(0, dy);
for(unsigned int c = 0; c < img->getNbChannels(); ++c) {
for(unsigned int j = 0; j < img->getHeight(); ++j) {
for(unsigned int i = 0; i < img->getWidth(); ++i) {
Image::depth_t value = img->getPixel(i, j, c);
resImg->setPixel(i + dx, j + dy, c, value);
}
}
}
}
else {
resImg = new Image(img->getWidth(), img->getHeight(), img->getNbChannels(), fillValue);
for(unsigned int c = 0; c < resImg->getNbChannels(); ++c) {
for(unsigned int j = max(dy, 0); j < resImg->getHeight()+min(dy,0); ++j) {
for(unsigned int i = max(dx, 0); i < resImg->getWidth()+min(dx,0); ++i) {
Image::depth_t value = img->getPixel(i-dx, j-dy, c);
resImg->setPixel(i, j, c, value);
}
}
}
}
result.push_back(new ImgWidget(resImg, QString("Translated %1:%2").arg(dx).arg(dy).toStdString()));
return result;
}
/*
* Copyright 2011-2012 INSA Rennes
*
* This file is part of EIImage.
*
* EIImage 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 3 of the License, or
* (at your option) any later version.
*
* EIImage is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EIImage. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRANSLATION_H
#define TRANSLATION_H
#include <QCoreApplication>
#include <vector>
#include <QDialog>
#include <QSpinBox>
#include <QLabel>
#include <QCheckBox>
#include <QRadioButton>
#include "Operation.h"
#include "Image.h"
class QWidget;
class TranslateOp : public Operation {
public:
TranslateOp();
std::vector<QWidget*> operator()(const imagein::Image*, const std::map<const imagein::Image*, std::string>&);
bool needCurrentImg();
private:
};
#endif //!TRANSLATION_H
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