First Commit

parents
#include <typeinfo>
#include "AlgorithmActionFactory.h"
AlgorithmActionFactory::AlgorithmActionFactory ()
{
}
AlgorithmActionFactory::~AlgorithmActionFactory ()
{
}
QMap<QString, QSharedPointer<IAlgorithmAction>> AlgorithmActionFactory::_actions;
QSharedPointer<IAlgorithmAction> AlgorithmActionFactory::getInstance (const QSharedPointer<IAlgorithmRequest> & request)
{
QString reqName = typeid (*request).name ();
if (_actions.find (reqName) == _actions.end ())
{
return QSharedPointer<IAlgorithmAction> (0);
}
return _actions[reqName];
}
bool AlgorithmActionFactory::registerAction (const QSharedPointer<IAlgorithmRequest> & request, const QSharedPointer<IAlgorithmAction> & action)
{
QString reqName = typeid (*request).name ();
if (_actions.contains(reqName))
{
return false;
}
_actions.insert (reqName, action);
return true;
}
#ifndef _ALGORITHM_ACTION_FACTORY_H
#define _ALGORITHM_ACTION_FACTORY_H
#include <QSharedPointer>
#include <QMap>
#include "IAlgorithmRequest.h"
#include "IAlgorithmAction.h"
class AlgorithmActionFactory
{
private:
AlgorithmActionFactory ();
~AlgorithmActionFactory ();
static QMap<QString, QSharedPointer<IAlgorithmAction>> _actions;
public:
static QSharedPointer<IAlgorithmAction> getInstance (const QSharedPointer<IAlgorithmRequest> & request);
static bool registerAction (const QSharedPointer<IAlgorithmRequest> & request, const QSharedPointer<IAlgorithmAction> & action);
};
#endif // _ALGORITHM_ACTION_FACTORY_H
#include "AlgorithmConfiguration.h"
AlgorithmConfiguration::AlgorithmConfiguration ()
{
}
AlgorithmConfiguration::~AlgorithmConfiguration ()
{
}
#ifndef _ALGORITHM_CONFIGURATION_H
#define _ALGORITHM_CONFIGURATION_H
class AlgorithmConfiguration
{
public:
AlgorithmConfiguration ();
~AlgorithmConfiguration ();
};
#endif // _ALGORITHM_CONFIGURATION_H
#include <QColor>
#include "AlgorithmLSBAction.h"
AlgorithmLSBAction::AlgorithmLSBAction ()
{
}
AlgorithmLSBAction::~AlgorithmLSBAction ()
{
}
bool AlgorithmLSBAction::encrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration)
{
QImage temporaryImage = image;
std::wstring temporaryMessage = message.toStdWString ();
int length = temporaryMessage.length ();
int x = 0, y = 0;
for (unsigned int mask = 1; mask; mask <<= 1)
{
QPoint point (x, y);
if (temporaryImage.valid (point))
{
QColor color (temporaryImage.pixel (point));
int red = (length & mask) ? color.red () | 1 : color.red () & ~1;
color.setRed (red);
temporaryImage.setPixel (point, color.rgb ());
if (++x >= temporaryImage.width ())
{
++y; x = 0;
}
}
else
{
return false;
}
}
for (int c = 0; c < length; c++)
{
wchar_t character = temporaryMessage[c];
for (wchar_t mask = 1; mask; mask <<= 1)
{
QPoint point (x, y);
if (temporaryImage.valid (point))
{
QColor color (temporaryImage.pixel (point));
int blue = (character & mask) ? color.blue () | 1 : color.blue () & ~1;
color.setBlue (blue);
temporaryImage.setPixel (point, color.rgb ());
if (++x >= temporaryImage.width ())
{
++y; x = 0;
}
}
else
{
return false;
}
}
}
image = temporaryImage;
return true;
}
bool AlgorithmLSBAction::decrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration)
{
QString temporary;
int length = 0;
int x = 0, y = 0;
for (unsigned int mask = 1; mask; mask <<= 1)
{
QPoint point (x, y);
if (image.valid (point))
{
QColor color (image.pixel (point));
if (color.red () & 1)
{
length |= mask;
}
if (++x >= image.width ())
{
++y; x = 0;
}
}
else
{
return false;
}
}
for (int c = 0; c < length; c++)
{
wchar_t character = 0;
for (wchar_t mask = 1; mask; mask <<= 1)
{
QPoint point (x, y);
if (image.valid (point))
{
QColor color (image.pixel (point));
if (color.blue () & 1)
{
character |= mask;
}
if (++x >= image.width ())
{
++y; x = 0;
}
}
else
{
return false;
}
}
temporary.append (character);
}
message = temporary;
return true;
}
#ifndef _ALGORITHM_LSB_ACTION_H
#define _ALGORITHM_LSB_ACTION_H
#include "IAlgorithmAction.h"
class AlgorithmLSBAction : public IAlgorithmAction
{
public:
AlgorithmLSBAction ();
~AlgorithmLSBAction ();
bool encrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration = QSharedPointer<AlgorithmConfiguration> (0));
bool decrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration = QSharedPointer<AlgorithmConfiguration> (0));
};
#endif // _ALGORITHM_LSB_ACTION_H
#include "AlgorithmLSBRequest.h"
AlgorithmLSBRequest::AlgorithmLSBRequest ()
{
_name = "LSB";
}
AlgorithmLSBRequest::~AlgorithmLSBRequest ()
{
}
#ifndef _ALGORITHM_LSB_REQUEST_H
#define _ALGORITHM_LSB_REQUEST_H
#include "IAlgorithmRequest.h"
class AlgorithmLSBRequest : public IAlgorithmRequest
{
public:
AlgorithmLSBRequest ();
~AlgorithmLSBRequest ();
};
#endif // _ALGORITHM_LSB_REQUEST_H
#include "AlgorithmRequestFactory.h"
AlgorithmRequestFactory::AlgorithmRequestFactory ()
{
}
AlgorithmRequestFactory::~AlgorithmRequestFactory ()
{
}
QMap<QString, QSharedPointer<IAlgorithmRequest>> AlgorithmRequestFactory::_requests;
QSharedPointer<IAlgorithmRequest> AlgorithmRequestFactory::getInstance (const QString & algorithm)
{
if (_requests.contains (algorithm))
{
return _requests[algorithm];
}
return QSharedPointer<IAlgorithmRequest> (0);
}
bool AlgorithmRequestFactory::registerRequest (const QString & algorithm, const QSharedPointer<IAlgorithmRequest> & request)
{
if (_requests.contains (algorithm))
{
return false;
}
_requests.insert (algorithm, request);
return true;
}
#ifndef _ALGORITHM_REQUEST_FACTORY_H
#define _ALGORITHM_REQUEST_FACTORY_H
#include <QSharedPointer>
#include <QMap>
#include "IAlgorithmRequest.h"
class AlgorithmRequestFactory
{
private:
AlgorithmRequestFactory ();
~AlgorithmRequestFactory ();
static QMap<QString, QSharedPointer<IAlgorithmRequest>> _requests;
public:
static QSharedPointer<IAlgorithmRequest> getInstance (const QString & algorithm);
static bool registerRequest (const QString & algorithm, const QSharedPointer<IAlgorithmRequest> & request);
};
#endif // _ALGORITHM_REQUEST_FACTORY_H
#ifndef _GLOBAL_DECLARATIONS_H
#define _GLOBAL_DECLARATIONS_H
#include <QString>
const QString APPLICATION_NAME = "ImageSteganography";
const QString APPLICATION_VERSION = "1.0";
const QString APPLICATION_ENCODING = "UTF-8";
const QString ORGANIZATION_NAME = "Efstathios Chatzikyriakidis";
const QString TRANSLATIONS_PATH = "Resources/Translations";
const QString STYLE_SHEETS_PATH = "Resources/StyleSheets";
const QString SPLASH_SCREEN_GRAPHIC = ":/UI/SplashScreen/Graphics/WindowBackground";
#endif // _GLOBAL_DECLARATIONS_H
#include <QCoreApplication>
#include <QLibraryInfo>
#include <QTextCodec>
#include <QLocale>
#include "I18N.h"
I18N::~I18N ()
{
}
I18N::I18N ()
{
}
I18N::I18N (const QString & path, const QString & encoding)
{
_path = path;
_encoding = encoding;
}
void I18N::setEncoding (const QString & encoding)
{
_encoding = encoding;
}
void I18N::setPath (const QString & path)
{
_path = path;
}
QString I18N::getEncoding ()
{
return _encoding;
}
QString I18N::getPath ()
{
return _path;
}
void I18N::Translate ()
{
QTextCodec::setCodecForCStrings (QTextCodec::codecForName (_encoding.toAscii ()));
QTextCodec::setCodecForTr (QTextCodec::codecForName (_encoding.toAscii ()));
_apTranslator.load (
QCoreApplication::applicationName () + "_" + QLocale::system ().name (),
QCoreApplication::applicationDirPath () + "/" + _path
);
_qtTranslator.load (
"qt_" + QLocale::system ().name (),
QLibraryInfo::location (QLibraryInfo::TranslationsPath)
);
QCoreApplication::installTranslator (&_apTranslator);
QCoreApplication::installTranslator (&_qtTranslator);
}
#ifndef _I18N_H
#define _I18N_H
#include <QTranslator>
class I18N
{
public:
I18N ();
~I18N ();
I18N (const QString & path, const QString & encoding);
void setPath (const QString & path);
void setEncoding (const QString & encoding);
QString getPath ();
QString getEncoding ();
void Translate ();
private:
QString _path, _encoding;
QTranslator _apTranslator, _qtTranslator;
};
#endif // _I18N_H
#include "IAlgorithmAction.h"
IAlgorithmAction::IAlgorithmAction ()
{
}
IAlgorithmAction::~IAlgorithmAction ()
{
}
#ifndef _IALGORITHM_ACTION_H
#define _IALGORITHM_ACTION_H
#include <QSharedPointer>
#include <QImage>
#include "AlgorithmConfiguration.h"
class IAlgorithmAction
{
public:
virtual ~IAlgorithmAction ();
virtual bool encrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration = QSharedPointer<AlgorithmConfiguration> (0)) = 0;
virtual bool decrypt (QImage & image, QString & message, QSharedPointer<AlgorithmConfiguration> configuration = QSharedPointer<AlgorithmConfiguration> (0)) = 0;
protected:
IAlgorithmAction ();
};
#endif // _IALGORITHM_ACTION_H
#include "IAlgorithmRequest.h"
IAlgorithmRequest::IAlgorithmRequest ()
{
}
IAlgorithmRequest::~IAlgorithmRequest ()
{
}
void IAlgorithmRequest::setName (const QString & name)
{
_name = name;
}
QString IAlgorithmRequest::getName ()
{
return _name;
}
#ifndef _IALGORITHM_REQUEST_H
#define _IALGORITHM_REQUEST_H
#include <QString>
class IAlgorithmRequest
{
public:
virtual ~IAlgorithmRequest ();
void setName (const QString & name);
QString getName ();
protected:
IAlgorithmRequest ();
QString _name;
};
#endif // _IALGORITHM_REQUEST_H
QT += gui
TEMPLATE = app
TARGET = ImageSteganography
RESOURCES += $${TARGET}.qrc
TRANSLATIONS = $${TARGET}_el_GR.ts
FORMS += MainWindow.ui
QMAKE_CXXFLAGS += -std=c++0x
CODECFORSRC = UTF-8
SOURCES += main.cpp \
MainWindow.cpp \
AlgorithmRequestFactory.cpp \
AlgorithmActionFactory.cpp \
IAlgorithmRequest.cpp \
IAlgorithmAction.cpp \
AlgorithmLSBRequest.cpp \
AlgorithmLSBAction.cpp \
I18N.cpp \
Settings.cpp \
Sleep.cpp \
Terminate.cpp \
StyleSheet.cpp \
AlgorithmConfiguration.cpp
HEADERS += MainWindow.h \
AlgorithmRequestFactory.h \
AlgorithmActionFactory.h \
IAlgorithmRequest.h \
IAlgorithmAction.h \
AlgorithmLSBRequest.h \
AlgorithmLSBAction.h \
GlobalDeclarations.h \
I18N.h \
Settings.h \
Sleep.h \
Terminate.h \
StyleSheet.h \
AlgorithmConfiguration.h
<RCC>
<qresource prefix="/UI/MainWindow/Graphics">
<file alias="WindowIcon">Resources/UI/MainWindow/Graphics/WindowIcon.png</file>
<file alias="AlgorithmsIcon">Resources/UI/MainWindow/Graphics/AlgorithmsIcon.png</file>
<file alias="AboutIcon">Resources/UI/MainWindow/Graphics/AboutIcon.png</file>
<file alias="HelpIcon">Resources/UI/MainWindow/Graphics/HelpIcon.png</file>
</qresource>
<qresource prefix="/UI/SplashScreen/Graphics">
<file alias="WindowBackground">Resources/UI/SplashScreen/Graphics/WindowBackground.png</file>
</qresource>
</RCC>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QMessageBox>
#include <QFileDialog>
#include <QUrl>
#include "AlgorithmRequestFactory.h"
#include "AlgorithmActionFactory.h"
#include "AlgorithmLSBRequest.h"
#include "AlgorithmLSBAction.h"
#include "MainWindow.h"
#include "ui_MainWindow.h"
const QString MainWindow::ACTION_ID = "ActionID";
MainWindow::MainWindow (QWidget * parent) : QMainWindow (parent), ui (new Ui::MainWindow)
{
ui->setupUi (this);
actionsGroup = QSharedPointer<QActionGroup> (new QActionGroup (this));
groupingActions ();
assignActionsIDs ();
registerAlgorithms ();
registerConnections ();
}
MainWindow::~MainWindow ()
{
delete ui;
}
void MainWindow::loadImageFromDisk ()
{
const QString fileName = QFileDialog::getOpenFileName (this, tr ("Open Image"), QDir::currentPath (), tr ("Images (*.bmp *.png *.ppm *.xpm)"));
if (!fileName.isEmpty ())
{
image = QImage (fileName);
if (image.isNull ())
{
QMessageBox::critical (this, tr ("Load Image Error"), tr ("Cannot load image: '%1'").arg (fileName));
return;
}
ui->graphicView->setPixmap (QPixmap::fromImage (image));
}
}
void MainWindow::saveImageToDisk ()
{
if (!image.isNull ())
{
const QString fileName = QFileDialog::getSaveFileName (this, tr ("Save Image"), QDir::currentPath (), tr ("Images (*.bmp *.png *.ppm *.xpm)"));
if (!fileName.isEmpty ())
{
if (!image.save (fileName))
{
QMessageBox::critical (this, tr ("Save Image Error"), tr ("Cannot save image: '%1'").arg (fileName));
}
}
}
}
void MainWindow::encryptToImage ()
{
if (!image.isNull ())
{
QString message = ui->messageEdit->toPlainText ();
if (!message.isEmpty ())
{
const QAction * action = actionsGroup->checkedAction ();
if (action != 0)
{
QSharedPointer<IAlgorithmRequest> request = AlgorithmRequestFactory::getInstance (action->property (ACTION_ID.toAscii ()).toString ());
if (!request.isNull ())
{
QSharedPointer<IAlgorithmAction> action = AlgorithmActionFactory::getInstance (request);
if (!action.isNull ())
{
if (action->encrypt (image, message))
{
ui->graphicView->setPixmap (QPixmap::fromImage (image));
}
else
{
QMessageBox::warning (this, tr ("Encryption Algorithm"), tr ("The message cannot be encrypted in the image!"));
}
}
}
}
}
}
}
void MainWindow::decryptFromImage ()
{
if (!image.isNull ())
{
const QAction * action = actionsGroup->checkedAction ();
if (action != 0)
{
QSharedPointer<IAlgorithmRequest> request = AlgorithmRequestFactory::getInstance (action->property (ACTION_ID.toAscii ()).toString ());
if (!request.isNull ())
{
QSharedPointer<IAlgorithmAction>