From c4b45efb9aa79554f591f2156c291bde8dce3b6c Mon Sep 17 00:00:00 2001 From: Stefan Jahn Date: Sat, 26 Nov 2022 21:22:50 +0100 Subject: [PATCH] Removed off-by-one error in zoomed histogram --- histogram.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/histogram.cc b/histogram.cc index 2e68c20..bebfde2 100644 --- a/histogram.cc +++ b/histogram.cc @@ -19,7 +19,7 @@ GtkWidget *histogram_da = NULL; int histogram[3][HISTOGRAM_WIDTH]; // 6* hist_width min, max, min , max..... int histogram_max; // max vlaue int histogram_zoom_start = 0; // first zoom value -int histogram_zoom_stop = HISTOGRAM_WIDTH; // second zoom value +int histogram_zoom_stop = HISTOGRAM_WIDTH-1; // second zoom value int histogram_x; // current mouse x-coordinate int histogram_pressed_x; // window x-coordinate when pressing a button int histogram_released_x; // window x-coordinate when releasing a button @@ -68,9 +68,10 @@ void cb_histogramda_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer da } gdk_cairo_set_source_rgba(cr, &color); - int histogram_width = histogram_zoom_stop - histogram_zoom_start; + int histogram_width = histogram_zoom_stop - histogram_zoom_start + 1; + int histogram_scale = histogram_width - 1 > 0 ? histogram_width - 1 : 1; for (int i = 0; i < histogram_width; i++) { - px = HISTOGRAM_MARGIN + ((clientw-HISTOGRAM_MARGIN*2) * i) / histogram_width; + px = HISTOGRAM_MARGIN + ((clientw-HISTOGRAM_MARGIN*2) * i) / histogram_scale; py2 = (clienth-HISTOGRAM_MARGIN) - ((clienth-HISTOGRAM_MARGIN*2) * log10(histogram[chan][histogram_zoom_start+i]+1) / log10(histogram_max)); if (i == 0) cairo_move_to(cr, px, py2); @@ -183,7 +184,7 @@ void cb_histogramda_btnpress (GtkWidget *widget, gpointer data) { } else { histogram_zoomed = 0; histogram_zoom_start = 0; - histogram_zoom_stop = HISTOGRAM_WIDTH; + histogram_zoom_stop = HISTOGRAM_WIDTH-1; } }; @@ -202,13 +203,13 @@ void cb_histogramda_btnrelease (GtkWidget *widget, gpointer data) { // translate first coordinate histogram_zoom_start = (histogram_pressed_x - HISTOGRAM_MARGIN) * HISTOGRAM_WIDTH / (clientw - HISTOGRAM_MARGIN*2); if (histogram_zoom_start < 0) histogram_zoom_start = 0; - if (histogram_zoom_start > HISTOGRAM_WIDTH) histogram_zoom_start = HISTOGRAM_WIDTH; + if (histogram_zoom_start >= HISTOGRAM_WIDTH) histogram_zoom_start = HISTOGRAM_WIDTH-1; // translate second coordinate histogram_zoom_stop = (histogram_released_x - HISTOGRAM_MARGIN) * HISTOGRAM_WIDTH / (clientw - HISTOGRAM_MARGIN*2) + 1; // limit the values if (histogram_zoom_stop < 0) histogram_zoom_stop = 0; - if (histogram_zoom_stop > HISTOGRAM_WIDTH) histogram_zoom_stop = HISTOGRAM_WIDTH; + if (histogram_zoom_stop >= HISTOGRAM_WIDTH) histogram_zoom_stop = HISTOGRAM_WIDTH-1; // exchange the values if necessary if (histogram_zoom_start > histogram_zoom_stop) {