Guard accesses to Crop::crop_ratios (#5701)

This commit is contained in:
Flössie
2020-04-05 11:26:47 +02:00
parent 2b230bfd1e
commit 15db8cdb57
2 changed files with 92 additions and 44 deletions

View File

@@ -16,6 +16,8 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>. * along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <vector>
#include "crop.h" #include "crop.h"
#include "options.h" #include "options.h"
@@ -46,9 +48,11 @@ inline void get_custom_ratio(int w, int h, double &rw, double &rh)
} // namespace } // namespace
Crop::Crop(): class Crop::CropRatios final
FoldableToolPanel(this, "crop", M("TP_CROP_LABEL"), false, true), {
crop_ratios{ public:
CropRatios() :
ratios{
{M("GENERAL_ASIMAGE"), 0.0}, {M("GENERAL_ASIMAGE"), 0.0},
{M("GENERAL_CURRENT"), -1.0}, {M("GENERAL_CURRENT"), -1.0},
{"3:2", 3.0 / 2.0}, // L1.5, P0.666... {"3:2", 3.0 / 2.0}, // L1.5, P0.666...
@@ -78,7 +82,48 @@ Crop::Crop():
{"17:22", 17.0 / 22.0}, // L1.294..., P0.772... {"17:22", 17.0 / 22.0}, // L1.294..., P0.772...
{"45:35 - ePassport", 45.0 / 35.0}, // L1.285,... P0.777... {"45:35 - ePassport", 45.0 / 35.0}, // L1.285,... P0.777...
{"64:27", 64.0 / 27.0}, // L2.370..., P0.421... {"64:27", 64.0 / 27.0}, // L2.370..., P0.421...
}, }
{
}
std::vector<Glib::ustring> getLabels() const
{
std::vector<Glib::ustring> res;
res.reserve(ratios.size());
for (const auto& ratio : ratios) {
res.push_back(ratio.label);
}
return res;
}
double getValue(std::size_t index) const
{
return
index < ratios.size()
? ratios[index].value
: ratios[0].value;
}
void updateCurrentRatio(double value)
{
ratios[1].value = value;
}
private:
struct CropRatio {
Glib::ustring label;
double value;
};
std::vector<CropRatio> ratios;
};
Crop::Crop():
FoldableToolPanel(this, "crop", M("TP_CROP_LABEL"), false, true),
crop_ratios(new CropRatios),
opt(0), opt(0),
wDirty(true), wDirty(true),
hDirty(true), hDirty(true),
@@ -229,8 +274,8 @@ Crop::Crop():
// ppigrid END // ppigrid END
// Populate the combobox // Populate the combobox
for (const auto& crop_ratio : crop_ratios) { for (const auto& label : crop_ratios->getLabels()) {
ratio->append (crop_ratio.label); ratio->append (label);
} }
ratio->set_active (0); ratio->set_active (0);
@@ -354,7 +399,10 @@ void Crop::read (const ProcParams* pp, const ParamsEdited* pedited)
setDimensions (pp->crop.x + pp->crop.w, pp->crop.y + pp->crop.h); setDimensions (pp->crop.x + pp->crop.w, pp->crop.y + pp->crop.h);
} }
const bool flip_orientation = pp->crop.fixratio && crop_ratios[ratio->get_active_row_number()].value > 0 && crop_ratios[ratio->get_active_row_number()].value < 1.0; const bool flip_orientation =
pp->crop.fixratio
&& crop_ratios->getValue(ratio->get_active_row_number()) > 0
&& crop_ratios->getValue(ratio->get_active_row_number()) < 1.0;
if (pp->crop.orientation == "Landscape") { if (pp->crop.orientation == "Landscape") {
orientation->set_active (flip_orientation ? 1 : 0); orientation->set_active (flip_orientation ? 1 : 0);
@@ -469,7 +517,10 @@ void Crop::write (ProcParams* pp, ParamsEdited* pedited)
} }
// for historical reasons we store orientation different if ratio is written as 2:3 instead of 3:2, but in GUI 'landscape' is always long side horizontal regardless of the ratio is written short or long side first. // for historical reasons we store orientation different if ratio is written as 2:3 instead of 3:2, but in GUI 'landscape' is always long side horizontal regardless of the ratio is written short or long side first.
const bool flip_orientation = fixr->get_active() && crop_ratios[ratio->get_active_row_number()].value > 0 && crop_ratios[ratio->get_active_row_number()].value < 1.0; const bool flip_orientation =
fixr->get_active()
&& crop_ratios->getValue(ratio->get_active_row_number()) > 0
&& crop_ratios->getValue(ratio->get_active_row_number()) < 1.0;
if (orientation->get_active_row_number() == 0) { if (orientation->get_active_row_number() == 0) {
pp->crop.orientation = flip_orientation ? "Portrait" : "Landscape"; pp->crop.orientation = flip_orientation ? "Portrait" : "Landscape";
@@ -1501,7 +1552,7 @@ double Crop::getRatio () const
return r; return r;
} }
r = crop_ratios[ratio->get_active_row_number()].value; r = crop_ratios->getValue(ratio->get_active_row_number());
if (!r) { if (!r) {
r = maxh <= maxw ? float(maxh)/float(maxw) : float(maxw)/float(maxh); r = maxh <= maxw ? float(maxh)/float(maxw) : float(maxw)/float(maxh);
} }
@@ -1539,5 +1590,5 @@ void Crop::updateCurrentRatio()
double rw, rh; double rw, rh;
get_custom_ratio(w->get_value(), h->get_value(), rw, rh); get_custom_ratio(w->get_value(), h->get_value(), rw, rh);
customRatioLabel->set_text(Glib::ustring::compose("%1:%2", rw, rh)); customRatioLabel->set_text(Glib::ustring::compose("%1:%2", rw, rh));
crop_ratios[1].value = double(w->get_value())/double(h->get_value()); crop_ratios->updateCurrentRatio(static_cast<double>(w->get_value()) / static_cast<double>(h->get_value()));
} }

View File

@@ -18,7 +18,7 @@
*/ */
#pragma once #pragma once
#include <vector> #include <memory>
#include <gtkmm.h> #include <gtkmm.h>
@@ -91,16 +91,13 @@ public:
void rotateCrop (int deg, bool hflip, bool vflip); void rotateCrop (int deg, bool hflip, bool vflip);
private: private:
struct CropRatio { class CropRatios;
Glib::ustring label;
double value;
};
std::vector<CropRatio> crop_ratios;
void adjustCropToRatio(); void adjustCropToRatio();
void updateCurrentRatio(); void updateCurrentRatio();
const std::unique_ptr<CropRatios> crop_ratios;
Gtk::CheckButton* fixr; Gtk::CheckButton* fixr;
MyComboBoxText* ratio; MyComboBoxText* ratio;
MyComboBoxText* orientation; MyComboBoxText* orientation;