From f99ca6c78c700aa3a6b0f42a84b5e1898fe3f11a Mon Sep 17 00:00:00 2001 From: Jeremy Borgman Date: Mon, 22 Jun 2020 14:35:56 -0500 Subject: [PATCH] Changed blur tool to pixelate instead --- .clang-format | 89 ++++++++++++++++++++ src/tools/blur/blurtool.cpp | 160 ++++++++++++++++++++++++------------ 2 files changed, 196 insertions(+), 53 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..5778e924 --- /dev/null +++ b/.clang-format @@ -0,0 +1,89 @@ +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: true +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 0 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 2 +UseTab: Never + diff --git a/src/tools/blur/blurtool.cpp b/src/tools/blur/blurtool.cpp index a927da8f..fda0c392 100644 --- a/src/tools/blur/blurtool.cpp +++ b/src/tools/blur/blurtool.cpp @@ -16,73 +16,127 @@ // along with Flameshot. If not, see . #include "blurtool.h" -#include +#include #include #include #include -#include +#include +#include -BlurTool::BlurTool(QObject *parent) : AbstractTwoPointTool(parent) { +BlurTool::BlurTool(QObject *parent) : AbstractTwoPointTool(parent) {} +QIcon BlurTool::icon(const QColor &background, bool inEditor) const +{ + Q_UNUSED(inEditor); + return QIcon(iconPath(background) + "blur.svg"); +} +QString BlurTool::name() const { return tr("Blur"); } + +QString BlurTool::nameID() { return QLatin1String(""); } + +QString BlurTool::description() const +{ + return tr("Set Blur as the paint tool"); } -QIcon BlurTool::icon(const QColor &background, bool inEditor) const { - Q_UNUSED(inEditor); - return QIcon(iconPath(background) + "blur.svg"); -} -QString BlurTool::name() const { - return tr("Blur"); -} +CaptureTool *BlurTool::copy(QObject *parent) { return new BlurTool(parent); } -QString BlurTool::nameID() { - return QLatin1String(""); -} +void write_block(QImage &image, int x_start, int y_start, int pixel_size, + QRgb block_color) +{ + assert(x_start + pixel_size < image.width()); + assert(y_start + pixel_size < image.height()); -QString BlurTool::description() const { - return tr("Set Blur as the paint tool"); -} - -CaptureTool* BlurTool::copy(QObject *parent) { - return new BlurTool(parent); -} - -void BlurTool::process(QPainter &painter, const QPixmap &pixmap, bool recordUndo) { - if (recordUndo) { - updateBackup(pixmap); + for (auto x = x_start; x < x_start + pixel_size; x++) + { + for (auto y = y_start; y < y_start + pixel_size; y++) + { + image.setPixel(x, y, block_color); } - QPoint &p0 = m_points.first; - QPoint &p1 = m_points.second; - auto pixelRatio = pixmap.devicePixelRatio(); - - QRect selection = QRect(p0, p1).normalized(); - QRect selectionScaled = QRect(p0 * pixelRatio, p1 * pixelRatio).normalized(); - - QGraphicsBlurEffect *blur = new QGraphicsBlurEffect; - blur->setBlurRadius(10); - QGraphicsPixmapItem *item = new QGraphicsPixmapItem ( - pixmap.copy(selectionScaled)); - item->setGraphicsEffect(blur); - - QGraphicsScene scene; - scene.addItem(item); - - scene.render(&painter, selection, QRectF()); - blur->setBlurRadius(12); - scene.render(&painter, selection, QRectF()); - scene.render(&painter, selection, QRectF()); + } } -void BlurTool::paintMousePreview(QPainter &painter, const CaptureContext &context) { - Q_UNUSED(context); - Q_UNUSED(painter); +QRgb calculate_block_averge(QImage &image, int x_start, int y_start, + int pixel_size) +{ + assert(x_start + pixel_size < image.width()); + assert(y_start + pixel_size < image.height()); + + auto red_count = 0; + auto blue_count = 0; + auto green_count = 0; + auto pixel_count = 0; + for (auto x = x_start; x < x_start + pixel_size; x++) + { + for (auto y = y_start; y < y_start + pixel_size; y++) + { + auto pixel = image.pixel(x, y); + + red_count += qRed(pixel); + green_count += qGreen(pixel); + blue_count += qBlue(pixel); + pixel_count++; + } + } + return (qRgb(red_count / pixel_count, green_count / pixel_count, + blue_count / pixel_count)); +} +void BlurTool::process(QPainter &painter, const QPixmap &pixmap, + bool recordUndo) +{ + if (recordUndo) + { + updateBackup(pixmap); + } + QPoint &p0 = m_points.first; + QPoint &p1 = m_points.second; + auto pixelRatio = pixmap.devicePixelRatio(); + + QRect selection = QRect(p0, p1).normalized(); + QRect selectionScaled = QRect(p0 * pixelRatio, p1 * pixelRatio).normalized(); + + QPixmap *source = new QPixmap(pixmap.copy(selectionScaled)); + + QImage original_image{source->toImage()}; + QImage imageResult{source->toImage()}; + unsigned int pixel_size = m_thickness; + + const unsigned int width = source->width(); + const unsigned int height = source->height(); + + // Don't start pixelating until the region is at least as big as the pixel + if ((width > pixel_size) && (height > pixel_size)) + { + for (unsigned int x = 0; x < (width - pixel_size); x += pixel_size) + { + for (unsigned int y = 0; y < (height - pixel_size); y += pixel_size) + { + auto block_color = + calculate_block_averge(original_image, x, y, pixel_size); + write_block(imageResult, x, y, pixel_size, block_color); + } + } + } + QPixmap result{QPixmap::fromImage(imageResult)}; + + QGraphicsScene scene; + scene.addPixmap(result); + + scene.render(&painter, selection, QRectF()); } -void BlurTool::drawStart(const CaptureContext &context) { - m_thickness = context.thickness; - m_points.first = context.mousePos; - m_points.second = context.mousePos; +void BlurTool::paintMousePreview(QPainter &painter, + const CaptureContext &context) +{ + Q_UNUSED(context); + Q_UNUSED(painter); } -void BlurTool::pressed(const CaptureContext &context) { - Q_UNUSED(context); +void BlurTool::drawStart(const CaptureContext &context) +{ + m_thickness = context.thickness; + m_points.first = context.mousePos; + m_points.second = context.mousePos; } + +void BlurTool::pressed(const CaptureContext &context) { Q_UNUSED(context); }