commit 0eccd55570f34853180ebda08d28dc18f5ddbdaa
parent 3c5129b4e685e8e098026434ea5e5807050dc18e
Author: lumidify <nobody@lumidify.org>
Date:   Tue,  5 May 2020 18:06:09 +0200
Allow to resize the image with space bar
Diffstat:
| M | README |  |  | 29 | ++++++++++++++++------------- | 
| M | croptool.c |  |  | 27 | ++++++++++++++++++++++----- | 
2 files changed, 38 insertions(+), 18 deletions(-)
diff --git a/README b/README
@@ -20,21 +20,24 @@ in the middle, you can move it. Clicking outside creates a new box.
 I don't know if all of the collision logic is entirely correct, so
 tell me if you notice any problems.
 
-Four keys are recognized: enter/return, delete, right arrow, and left arrow.
-Enter and right arrow both go to the next image, but enter copies the
-selection box from the current image and uses it for the next picture,
-while right arrow just goes to the next image and only displays a
-selection box if it already had one. This is so that lots of pages
-of a digitized book can be cropped quickly since the selection box
-needs to be tweaked occasionally (since my digitizing equipment, if it
-can be called that, isn't exactly very professional). Left arrow
-just goes to the last picture. Delete removes the selection for the
-current image (this is then also not printed out at the end).
+Several keys are recognized:
+* Enter and right arrow both go to the next image, but enter copies the
+  selection box from the current image and uses it for the next picture,
+  while right arrow just goes to the next image and only displays a
+  selection box if it already had one. This is so that lots of pages
+  of a digitized book can be cropped quickly since the selection box
+  needs to be tweaked occasionally (my digitizing equipment, if it
+  can be called that, isn't exactly very professional).
+* Left arrow just goes to the previous picture.
+* Delete removes the selection for the current image (this is then
+  also not printed out at the end).
+* Space bar resizes the image if the window was resized.
 
 Note that resizing the window currently does not resize the images.
-It will only take effect if you move to another image. There may be
-bugs lurking here as well since the actual cropping box needs to be
-scaled according to how much the image was scaled for display.
+It will only take effect if you move to another image or press
+space bar. There may be bugs lurking here as well since the actual
+cropping box needs to be rescaled according to how much the image
+was scaled for display.
 
 When the window is closed, the ImageMagick command (mogrify -crop...)
 for cropping each of the pictures that had a selection box defined
diff --git a/croptool.c b/croptool.c
@@ -91,6 +91,7 @@ static void last_picture(GtkWidget *area, struct State *state);
 static GdkPixbuf *load_pixbuf(char *filename, int w, int h, int *actual_w, int *actual_h);
 static void print_selection(struct Selection *sel, const char *filename);
 static void clear_selection(GtkWidget *area, struct State *state);
+static void resize_manual(GtkWidget *area, struct State *state);
 
 int main(int argc, char *argv[]) {
 	GtkWidget *window;
@@ -456,18 +457,18 @@ change_picture(
 	} else if (!sel) {
 		/* Just fill it with -200 so we can check later if it has been used yet */
 		sel = create_selection(-200, -200, -200, -200, orig_w, orig_h, actual_w, actual_h);
-	} else if (sel->rect.x0 != -200) {
+	} else if (sel->rect.x0 != -200 && actual_w != sel->scaled_w) {
 		/* If there is a selection, we need to convert it to the new scale.
-		 * This only takes width into account because the
-		 * aspect ratio should have been preserved anyways */
+		 * This only takes width into account because the aspect ratio
+		 * should have been preserved anyways */
 		double scale = (double)actual_w / sel->scaled_w;
 		sel->rect.x0 = round(sel->rect.x0 * scale);
 		sel->rect.y0 = round(sel->rect.y0 * scale);
 		sel->rect.x1 = round(sel->rect.x1 * scale);
 		sel->rect.y1 = round(sel->rect.y1 * scale);
-		sel->scaled_w = actual_w;
-		sel->scaled_h = actual_h;
 	}
+	sel->scaled_w = actual_w;
+	sel->scaled_h = actual_h;
 	state->selections[state->cur_selection] = sel;
 	gtk_widget_queue_draw(area);
 }
@@ -520,6 +521,19 @@ clear_selection(GtkWidget *area, struct State *state) {
 	gtk_widget_queue_draw(area);
 }
 
+static void
+resize_manual(GtkWidget *area, struct State *state) {
+	if (state->cur_selection < 0 || !state->selections[state->cur_selection])
+		return;
+	int orig_w, orig_h;
+	GdkPixbuf *tmp_pixbuf = load_pixbuf(
+	    state->filenames[state->cur_selection],
+	    state->window_w, state->window_h, &orig_w, &orig_h);
+	if (!tmp_pixbuf)
+		return;
+	change_picture(area, tmp_pixbuf, state->cur_selection, orig_w, orig_h, state, FALSE);
+}
+
 static gboolean
 key_press(GtkWidget *area, GdkEventKey *event, gpointer data) {
 	struct State *state = (struct State *)data;
@@ -536,6 +550,9 @@ key_press(GtkWidget *area, GdkEventKey *event, gpointer data) {
 	case GDK_KEY_Delete:
 		clear_selection(area, state);
 		break;
+	case GDK_KEY_space:
+		resize_manual(area, state);
+		break;
 	}
 	return FALSE;
 }