Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] added randomize pixels function to pixelate tool #3368

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ GeneralConf::GeneralConf(QWidget* parent)
initCopyPathAfterSave();
initCopyAndCloseAfterUpload();
initUploadWithoutConfirmation();
initRandomizePixelation();
initHistoryConfirmationToDelete();
initAntialiasingPinZoom();
initUploadHistoryMax();
Expand Down Expand Up @@ -81,6 +82,7 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
m_saveAfterCopy->setChecked(config.saveAfterCopy());
m_copyPathAfterSave->setChecked(config.copyPathAfterSave());
m_antialiasingPinZoom->setChecked(config.antialiasingPinZoom());
m_randomizePixelation->setChecked(config.randomizePixelation());
m_useJpgForClipboard->setChecked(config.useJpgForClipboard());
m_copyOnDoubleClick->setChecked(config.copyOnDoubleClick());
m_uploadWithoutConfirmation->setChecked(config.uploadWithoutConfirmation());
Expand Down Expand Up @@ -150,6 +152,11 @@ void GeneralConf::allowMultipleGuiInstancesChanged(bool checked)
ConfigHandler().setAllowMultipleGuiInstances(checked);
}

void GeneralConf::setRandomizePixelation(bool checked)
{
ConfigHandler().setRandomizePixelation(checked);
}

void GeneralConf::autoCloseIdleDaemonChanged(bool checked)
{
ConfigHandler().setAutoCloseIdleDaemon(checked);
Expand Down Expand Up @@ -264,6 +271,17 @@ void GeneralConf::initSaveLastRegion()
this,
&GeneralConf::saveLastRegion);
}
void GeneralConf::initRandomizePixelation()
kurayami07734 marked this conversation as resolved.
Show resolved Hide resolved
{
m_randomizePixelation = new QCheckBox(tr("Randomize pixelation"), this);
m_randomizePixelation->setToolTip(
tr("Randomly shuffle pixels to make it harder to recover the contents"));
m_scrollAreaLayout->addWidget(m_randomizePixelation);
connect(m_randomizePixelation,
&QCheckBox::clicked,
this,
&GeneralConf::setRandomizePixelation);
}

void GeneralConf::initShowSidePanelButton()
{
Expand Down
3 changes: 3 additions & 0 deletions src/config/generalconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private slots:
void allowMultipleGuiInstancesChanged(bool checked);
void autoCloseIdleDaemonChanged(bool checked);
void autostartChanged(bool checked);
void setRandomizePixelation(bool checked);
void historyConfirmationToDelete(bool checked);
void uploadHistoryMaxChanged(int max);
void undoLimit(int limit);
Expand Down Expand Up @@ -81,6 +82,7 @@ private slots:
void initShowHelp();
void initShowMagnifier();
void initShowSidePanelButton();
void initRandomizePixelation();
void initShowStartupLaunchMessage();
void initShowTrayIcon();
void initSquareMagnifier();
Expand Down Expand Up @@ -113,6 +115,7 @@ private slots:
QCheckBox* m_copyURLAfterUpload;
QCheckBox* m_copyPathAfterSave;
QCheckBox* m_antialiasingPinZoom;
QCheckBox* m_randomizePixelation;
QCheckBox* m_saveLastRegion;
QCheckBox* m_uploadWithoutConfirmation;
QPushButton* m_importButton;
Expand Down
19 changes: 18 additions & 1 deletion src/tools/pixelate/pixelatetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <QGraphicsScene>
#include <QImage>
#include <QPainter>
#include <algorithm>
#include <chrono> // std::chrono::system_clock
#include <cstring>
#include <random> // std::default_random_engine

PixelateTool::PixelateTool(QObject* parent)
: AbstractTwoPointTool(parent)
Expand Down Expand Up @@ -78,10 +82,23 @@ void PixelateTool::process(QPainter& painter, const QPixmap& pixmap)
QPixmap t = pixmap.copy(selectionScaled);
t = t.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
t = t.scaled(selection.width(), selection.height());
painter.drawImage(selection, t.toImage());
QImage image = t.toImage();
image = randomizePixels(image);
painter.drawImage(selection, image);
}
}

QImage PixelateTool::randomizePixels(const QImage& image)
{
QVector<QRgb> pixels(image.height() * image.width());
std::memcpy(pixels.data(), image.bits(), image.sizeInBytes());
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(
pixels.begin(), pixels.end(), std::default_random_engine(seed));
QImage shuffledImage(image.width(), image.height(), image.format());
std::memcpy(shuffledImage.bits(), pixels.data(), image.sizeInBytes());
return shuffledImage;
}
void PixelateTool::drawSearchArea(QPainter& painter, const QPixmap& pixmap)
{
Q_UNUSED(pixmap)
Expand Down
1 change: 1 addition & 0 deletions src/tools/pixelate/pixelatetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PixelateTool : public AbstractTwoPointTool

protected:
CaptureTool::Type type() const override;
QImage randomizePixels(const QImage& image);

public slots:
void pressed(CaptureContext& context) override;
Expand Down
1 change: 1 addition & 0 deletions src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
OPTION("saveAfterCopy" ,Bool ( false )),
OPTION("savePath" ,ExistingDir ( )),
OPTION("savePathFixed" ,Bool ( false )),
OPTION("randomizePixelation" ,Bool ( false )),
kurayami07734 marked this conversation as resolved.
Show resolved Hide resolved
OPTION("saveAsFileExtension" ,SaveFileExtension ( )),
OPTION("saveLastRegion" ,Bool (false )),
OPTION("uploadHistoryMax" ,LowerBoundedInt (0, 25 )),
Expand Down
1 change: 1 addition & 0 deletions src/utils/confighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ConfigHandler : public QObject
CONFIG_GETTER_SETTER(userColors, setUserColors, QVector<QColor>);
CONFIG_GETTER_SETTER(savePath, setSavePath, QString)
CONFIG_GETTER_SETTER(savePathFixed, setSavePathFixed, bool)
CONFIG_GETTER_SETTER(randomizePixelation, setRandomizePixelation, bool)
CONFIG_GETTER_SETTER(uiColor, setUiColor, QColor)
CONFIG_GETTER_SETTER(contrastUiColor, setContrastUiColor, QColor)
CONFIG_GETTER_SETTER(drawColor, setDrawColor, QColor)
Expand Down