Skip to content

Commit

Permalink
Ensure valid thumb->zoom_100
Browse files Browse the repository at this point in the history
In a few cornercases the calculation of thumb->zoom_100 fails. We can assume this
to be valid only if we have valid thumb->img_width > 0. (same fore height)

As we keep a thumbs zoom_100 for it's lifetime we don't write it so it will be
updated if valid data are present.
  • Loading branch information
jenshannoschwalm authored and TurboGit committed Dec 2, 2023
1 parent b9edf68 commit add8d82
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/dtgtk/thumbnail.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,12 @@ static gboolean _event_image_draw(GtkWidget *widget,
cairo_surface_t *img_surf = NULL;
if(thumb->zoomable)
{
if(thumb->zoom > 1.0f)
thumb->zoom = MIN(thumb->zoom, dt_thumbnail_get_zoom100(thumb));
const float zoom100 = dt_thumbnail_get_zoom100(thumb);
// Any zoom100 > 1 is ensured to be correct

if(thumb->zoom > 1.0f && zoom100 > 1.0f)
thumb->zoom = MIN(thumb->zoom, zoom100);

res = dt_view_image_get_surface(thumb->imgid,
image_w * thumb->zoom,
image_h * thumb->zoom,
Expand Down Expand Up @@ -2259,11 +2263,18 @@ float dt_thumbnail_get_zoom100(dt_thumbnail_t *thumb)
(float)(thumb->height - thumb->img_margin->top - thumb->img_margin->bottom);
const float used_w =
(float)(thumb->width - thumb->img_margin->left - thumb->img_margin->right);
thumb->zoom_100 = fmaxf((float)w / used_w, (float)h / used_h);
if(thumb->zoom_100 < 1.0f) thumb->zoom_100 = 1.0f;
const float zoom100 = fmaxf((float)w / used_w, (float)h / used_h);

/* the thumb->zoom_100 value is kept for the thumbs lifetime so
we have to make sure this is only done if valid even in cornercases.
If not safe we won't keep it.
*/
const gboolean safe = zoom100 >= 1.0f && thumb->img_width > 0;
if(safe)
thumb->zoom_100 = MAX(zoom100, 1.0f);
}

return thumb->zoom_100;
return MAX(1.0f, thumb->zoom_100);
}

float dt_thumbnail_get_zoom_ratio(dt_thumbnail_t *thumb)
Expand Down

0 comments on commit add8d82

Please sign in to comment.