Skip to content

Commit

Permalink
v1.2 - Added Percentage based Cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
frak0d committed Mar 27, 2024
1 parent 47d6c6c commit 7fe2a1f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 57 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
cmake_minimum_required(VERSION 3.5)

project(smol_img VERSION 1.1 LANGUAGES CXX)
project(smol_img VERSION 1.2 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down
37 changes: 33 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <cmath>
#include <chrono>

using namespace std::literals;
Expand Down Expand Up @@ -50,15 +49,45 @@ int main(int argc, char* argv[])
img_view.scale2fit(state == Qt::Unchecked);
});

QObject::connect(ui.crop_in_percent, &QCheckBox::stateChanged, [&](int state)
{
if (input_img.isNull()) return;

if (state == Qt::Checked)
{
// absolute to percentage
ui.crop_top->setValue(ui.crop_top->value()*100.f / input_img.height());
ui.crop_left->setValue(ui.crop_left->value()*100.f / input_img.width());
ui.crop_right->setValue(ui.crop_right->value()*100.f / input_img.width());
ui.crop_bottom->setValue(ui.crop_bottom->value()*100.f / input_img.height());
}
else
{
// percentage to absolute
ui.crop_top->setValue(ui.crop_top->value()/100.f * input_img.height());
ui.crop_left->setValue(ui.crop_left->value()/100.f * input_img.width());
ui.crop_right->setValue(ui.crop_right->value()/100.f * input_img.width());
ui.crop_bottom->setValue(ui.crop_bottom->value()/100.f * input_img.height());
}
});

bool png_mode = false;

auto compute_output = [&]
{
QImage img = input_img;
int quality = png_mode ? 0 : ui.img_quality->sliderPosition(); // max compression for png

// crop
auto img = input_img.copy(QRect{QPoint{ui.crop_left->value(), ui.crop_top->value()},
QPoint{input_img.width() - ui.crop_right->value(), input_img.height() - ui.crop_bottom->value()}});
if (ui.crop_in_percent->isChecked())
{
img = img.copy(QRect{QPoint( ui.crop_left->value()/100.f * img.width(), ui.crop_top->value() /100.f * img.height()),
QPoint((1-ui.crop_right->value()/100.f) * img.width(), (1-ui.crop_bottom->value()/100.f) * img.height())});
}
else // crop values in pixels
{
img = img.copy(QRect{QPoint{ui.crop_left->value(), ui.crop_top->value()},
QPoint{img.width() - ui.crop_right->value(), img.height() - ui.crop_bottom->value()}});
}

// rotate
img = img.transformed(QTransform{}.rotate(rotation));
Expand Down
112 changes: 63 additions & 49 deletions main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<x>0</x>
<y>0</y>
<width>1000</width>
<height>650</height>
<height>685</height>
</rect>
</property>
<property name="windowTitle">
<string>smol_img tool v1.1 by frakod</string>
<string>smol_img tool v1.2 by frakod</string>
</property>
<property name="windowIcon">
<iconset theme="zoom-fit-best"/>
Expand All @@ -28,8 +28,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>666</width>
<height>630</height>
<width>648</width>
<height>661</height>
</rect>
</property>
</widget>
Expand Down Expand Up @@ -133,35 +133,16 @@
</property>
</widget>
</item>
<item row="1" column="3" alignment="Qt::AlignLeft|Qt::AlignVCenter">
<widget class="QPushButton" name="rotr_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Rotate Clockwise</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="object-rotate-right"/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QSpinBox" name="crop_left">
<item row="2" column="2" colspan="2">
<widget class="QSpinBox" name="crop_bottom">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Crop Left</string>
<string>Crop Bottom</string>
</property>
<property name="accelerated">
<bool>true</bool>
Expand Down Expand Up @@ -190,16 +171,16 @@
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QSpinBox" name="crop_bottom">
<item row="0" column="2" colspan="2">
<widget class="QSpinBox" name="crop_top">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Crop Bottom</string>
<string>Crop Top</string>
</property>
<property name="accelerated">
<bool>true</bool>
Expand All @@ -209,16 +190,45 @@
</property>
</widget>
</item>
<item row="0" column="2" colspan="2">
<widget class="QSpinBox" name="crop_top">
<item row="1" column="3" alignment="Qt::AlignLeft|Qt::AlignVCenter">
<widget class="QPushButton" name="rotr_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Rotate Clockwise</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="object-rotate-right"/>
</property>
</widget>
</item>
<item row="3" column="0" colspan="6" alignment="Qt::AlignHCenter">
<widget class="QCheckBox" name="crop_in_percent">
<property name="text">
<string>Crop Values in Percentage</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QSpinBox" name="crop_left">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Crop Top</string>
<string>Crop Left</string>
</property>
<property name="accelerated">
<bool>true</bool>
Expand Down Expand Up @@ -548,7 +558,7 @@
<item row="0" column="0" colspan="2" alignment="Qt::AlignHCenter">
<widget class="QCheckBox" name="preview_original_sz">
<property name="text">
<string>Preview in Original Size</string>
<string>Preview in Actual Size</string>
</property>
<property name="checked">
<bool>false</bool>
Expand Down Expand Up @@ -581,6 +591,9 @@
</widget>
</widget>
<tabstops>
<tabstop>rotl_btn</tabstop>
<tabstop>rotr_btn</tabstop>
<tabstop>crop_in_percent</tabstop>
<tabstop>crop_top</tabstop>
<tabstop>crop_left</tabstop>
<tabstop>crop_right</tabstop>
Expand All @@ -596,25 +609,10 @@
<tabstop>copy_btn</tabstop>
<tabstop>save_btn</tabstop>
<tabstop>image_frame</tabstop>
<tabstop>phony_btn</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>crop_left</sender>
<signal>valueChanged(int)</signal>
<receiver>phony_btn</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>779</x>
<y>113</y>
</hint>
<hint type="destinationlabel">
<x>834</x>
<y>123</y>
</hint>
</hints>
</connection>
<connection>
<sender>crop_top</sender>
<signal>valueChanged(int)</signal>
Expand Down Expand Up @@ -759,5 +757,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>crop_left</sender>
<signal>valueChanged(int)</signal>
<receiver>phony_btn</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>779</x>
<y>113</y>
</hint>
<hint type="destinationlabel">
<x>834</x>
<y>123</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 7fe2a1f

Please sign in to comment.