Skip to content

Commit

Permalink
clarify resize and stretch logic
Browse files Browse the repository at this point in the history
cppcheck (via Codacy) rightly noted that the construct ((!!w ^ !!h)) was confusing.

Rearrange the code a bit, note some things must be true without testing them, and we can eliminate two variables, make the code faster than the "cute optimization" and make it FAR easier to read.
  • Loading branch information
GregoryLundberg committed Jan 28, 2018
1 parent 36e9290 commit 5e0c305
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/gui/core/canvas.cpp
Expand Up @@ -1105,33 +1105,30 @@ void image_shape::draw(surface& canvas,
surface surf;

// Test whether we need to scale and do the scaling if needed.
if(w || h) {
bool done = false;
bool stretch_image = (resize_mode_ == stretch) && (!!w ^ !!h);
if(!w) {
if(stretch_image) {
if ((w == 0) && (h == 0)) {
surf = image_;
}
else { // assert((w != 0) || (h != 0))
if(w == 0) { // assert(h != 0)
if(resize_mode_ == stretch) {
DBG_GUI_D << "Image: vertical stretch from " << image_->w << ','
<< image_->h << " to a height of " << h << ".\n";

surf = stretch_surface_vertical(image_, h);
done = true;
}
w = image_->w;
}

if(!h) {
if(stretch_image) {
else if(h == 0) { // assert(w != 0)
if(resize_mode_ == stretch) {
DBG_GUI_D << "Image: horizontal stretch from " << image_->w
<< ',' << image_->h << " to a width of " << w
<< ".\n";

surf = stretch_surface_horizontal(image_, w);
done = true;
}
h = image_->h;
}

if(!done) {
else { // assert((w != 0) && (h != 0))

if(resize_mode_ == tile) {
DBG_GUI_D << "Image: tiling from " << image_->w << ','
Expand All @@ -1157,8 +1154,6 @@ void image_shape::draw(surface& canvas,
}
src_clip.w = w;
src_clip.h = h;
} else {
surf = image_;
}

if(vertical_mirror_(local_variables)) {
Expand Down

0 comments on commit 5e0c305

Please sign in to comment.