From f8582ccea124871817a4e782e7433b1835774243 Mon Sep 17 00:00:00 2001 From: Hanno Schwalm Date: Sat, 10 May 2025 13:38:28 +0200 Subject: [PATCH 1/2] dt_dev_get_processed_size() returns a gboolean In some cases we would like to know if the reported dimensions are from current port/pipe of rounded from preview pipe data. --- src/develop/develop.c | 11 ++++++----- src/develop/develop.h | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/develop/develop.c b/src/develop/develop.c index 590313ee9554..785716ff6c12 100644 --- a/src/develop/develop.c +++ b/src/develop/develop.c @@ -2475,21 +2475,21 @@ gboolean dt_dev_get_preview_size(const dt_develop_t *dev, return *wd >= 1.f && *ht >= 1.f; } -void dt_dev_get_processed_size(dt_dev_viewport_t *port, - int *procw, - int *proch) +gboolean dt_dev_get_processed_size(dt_dev_viewport_t *port, + int *procw, + int *proch) { // no processed pipes, lets return 0 size *procw = *proch = 0; - if(!port) return; + if(!port) return FALSE; // if pipe is processed, lets return its size if(port->pipe && port->pipe->processed_width) { *procw = port->pipe->processed_width; *proch = port->pipe->processed_height; - return; + return TRUE; } dt_develop_t *dev = darktable.develop; @@ -2501,6 +2501,7 @@ void dt_dev_get_processed_size(dt_dev_viewport_t *port, *procw = scale * dev->preview_pipe->processed_width; *proch = scale * dev->preview_pipe->processed_height; } + return FALSE; } static float _calculate_new_scroll_zoom_tscale(const int up, diff --git a/src/develop/develop.h b/src/develop/develop.h index f7206f9dae9e..0c4babadebb4 100644 --- a/src/develop/develop.h +++ b/src/develop/develop.h @@ -420,9 +420,10 @@ void dt_dev_reprocess_preview(dt_develop_t *dev); gboolean dt_dev_get_preview_size(const dt_develop_t *dev, float *wd, float *ht); -void dt_dev_get_processed_size(dt_dev_viewport_t *port, - int *procw, - int *proch); +// return TRUE in case we got it from current port instead of preview +gboolean dt_dev_get_processed_size(dt_dev_viewport_t *port, + int *procw, + int *proch); gboolean dt_dev_get_zoom_bounds(dt_dev_viewport_t *port, float *zoom_x, float *zoom_y, From 2b703e7caf244f878276c1cb22004a8cf7a483c9 Mon Sep 17 00:00:00 2001 From: Hanno Schwalm Date: Sat, 10 May 2025 15:54:16 +0200 Subject: [PATCH 2/2] Better calculation of cropped output dimension While setting the crops we get a feedback about it's resulting width & height. In modify_roi_out() and modify_roi_in() the position and width are set to the floored values to guarantee we stay inside available data range. Let's report the floored value in expose instead of what we did, also via -d verbose log. --- src/iop/crop.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/iop/crop.c b/src/iop/crop.c index 0099d37e5bd3..2a96557c2ef3 100644 --- a/src/iop/crop.c +++ b/src/iop/crop.c @@ -1418,25 +1418,26 @@ void gui_post_expose(dt_iop_module_t *self, if(dimmed) return; // draw cropping window dimensions if first mouse button is pressed - if(darktable.control->button_down && darktable.control->button_down_which == 1) + if(darktable.control->button_down && darktable.control->button_down_which == GDK_BUTTON_PRIMARY) { - char dimensions[16]; + char dimensions[64]; dimensions[0] = '\0'; PangoLayout *layout; PangoRectangle ext; PangoFontDescription *desc = pango_font_description_copy_static(darktable.bauhaus->pango_font_desc); pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD); - pango_font_description_set_absolute_size - (desc, - DT_PIXEL_APPLY_DPI(16) * PANGO_SCALE / zoom_scale); + pango_font_description_set_absolute_size(desc, DT_PIXEL_APPLY_DPI(16) * PANGO_SCALE / zoom_scale); layout = pango_cairo_create_layout(cr); pango_layout_set_font_description(layout, desc); int procw, proch; - dt_dev_get_processed_size(&dev->full, &procw, &proch); - snprintf(dimensions, sizeof(dimensions), - "%i x %i", (int)(0.5f + procw * g->clip_w), (int)(0.5f + proch * g->clip_h)); + const gboolean exact = dt_dev_get_processed_size(&dev->full, &procw, &proch); + const int dx = floorf(procw * g->clip_w); + const int dy = floorf(proch * g->clip_h); + snprintf(dimensions, sizeof(dimensions), "%s%d x %d", exact ? "" : "~ ", dx, dy); + dt_print(DT_DEBUG_VERBOSE, "reported crop dimension: %s%d x %d", + exact ? "" : "~ ", dx, dy); pango_layout_set_text(layout, dimensions, -1); pango_layout_get_pixel_extents(layout, NULL, &ext); @@ -1454,8 +1455,7 @@ void gui_post_expose(dt_iop_module_t *self, yp = CLAMP(yp, y1 + 2.0 * margin, y2 - text_h - 2.0 * margin); cairo_set_source_rgba(cr, .5, .5, .5, .9); - dt_gui_draw_rounded_rectangle - (cr, text_w + 2 * margin, text_h + 2 * margin, xp - margin, yp - margin); + dt_gui_draw_rounded_rectangle(cr, text_w + 2 * margin, text_h + 2 * margin, xp - margin, yp - margin); cairo_set_source_rgb(cr, .7, .7, .7); cairo_move_to(cr, xp, yp); pango_cairo_show_layout(cr, layout); @@ -1537,7 +1537,7 @@ int mouse_moved(dt_iop_module_t *self, _set_max_clip(self); - if(darktable.control->button_down && darktable.control->button_down_which == 1) + if(darktable.control->button_down && darktable.control->button_down_which == GDK_BUTTON_PRIMARY) { // draw a light gray frame, to show it's not stored yet: // first mouse button, adjust cropping frame, but what do we do? @@ -1548,14 +1548,12 @@ int mouse_moved(dt_iop_module_t *self, { /* moving the crop window */ if(!g->shift_hold) - g->clip_x - = fminf(g->clip_max_w + g->clip_max_x - g->clip_w, - fmaxf(g->clip_max_x, g->handle_x + pzx - bzx)); + g->clip_x = MIN(g->clip_max_w + g->clip_max_x - g->clip_w, + MAX(g->clip_max_x, g->handle_x + pzx - bzx)); if(!g->ctrl_hold) - g->clip_y - = fminf(g->clip_max_h + g->clip_max_y - g->clip_h, - fmaxf(g->clip_max_y, g->handle_y + pzy - bzy)); + g->clip_y = MIN(g->clip_max_h + g->clip_max_y - g->clip_h, + MAX(g->clip_max_y, g->handle_y + pzy - bzy)); } else if(g->cropping == GRAB_NONE) return 0; @@ -1734,10 +1732,10 @@ int button_pressed(dt_iop_module_t *self, if(!g->preview_ready) return 0; // avoid unexpected back to lt mode: - if(type == GDK_2BUTTON_PRESS && which == 1) + if(type == GDK_2BUTTON_PRESS && which == GDK_BUTTON_PRIMARY) return 1; - if(which == 1) + if(which == GDK_BUTTON_PRIMARY) { float wd, ht; dt_dev_get_preview_size(self->dev, &wd, &ht); @@ -1779,7 +1777,7 @@ int button_pressed(dt_iop_module_t *self, return 1; } - else if(which == 3) + else if(which == GDK_BUTTON_SECONDARY) { // we reset cropping g->clip_x = 0.0f;