aboutsummaryrefslogtreecommitdiff
path: root/fiv-jpegcrop.c
blob: fad0c22af6da3f6bbbd6aaea0be296d9429264a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//
// fiv-jpegcrop.c: lossless JPEG cropper
//
// Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

#include <gtk/gtk.h>
#include <turbojpeg.h>

#include "config.h"

// --- Utilities ---------------------------------------------------------------

static void exit_fatal(const char *format, ...) G_GNUC_PRINTF(1, 2);

static void
exit_fatal(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);

	gchar *format_nl = g_strdup_printf("%s\n", format);
	vfprintf(stderr, format_nl, ap);
	free(format_nl);

	va_end(ap);
	exit(EXIT_FAILURE);
}

// --- Main --------------------------------------------------------------------

struct {
	GFile *location;
	gchar *data;
	gsize len;
	int width, height, subsampling, colorspace;
	int mcu_width, mcu_height;
	cairo_surface_t *surface;

	int top, left, right, bottom;

	GtkWidget *label;
	GtkWidget *window;
	GtkWidget *scrolled;
	GtkWidget *view;
} g;

static void
show_error_dialog(GError *error)
{
	GtkWidget *dialog =
		gtk_message_dialog_new(GTK_WINDOW(g.window), GTK_DIALOG_MODAL,
			GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
	g_error_free(error);
}

static gboolean
on_draw(G_GNUC_UNUSED GtkWidget *widget, cairo_t *cr,
	G_GNUC_UNUSED gpointer user_data)
{
	cairo_set_source_surface(cr, g.surface, 1, 1);
	cairo_paint(cr);

	cairo_rectangle(cr,
		1 + g.left - 0.5,
		1 + g.top - 0.5,
		g.right - g.left + 1,
		g.bottom - g.top + 1);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_set_line_width(cr, 1);
	cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
	cairo_stroke(cr);

	cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
	cairo_rectangle(cr, 1, 1, g.width, g.height);
	cairo_rectangle(
		cr, g.left, g.top, g.right - g.left + 2, g.bottom - g.top + 2);
	cairo_clip(cr);
	cairo_set_source_rgba(cr, 0, 0, 0, 0.5);
	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	cairo_paint(cr);
	return TRUE;
}

static GFile *
run_chooser(GtkWidget *dialog)
{
	GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
	gtk_file_chooser_set_local_only(chooser, FALSE);

	GtkFileFilter *jpeg = gtk_file_filter_new();
	gtk_file_filter_add_mime_type(jpeg, "image/jpeg");
	gtk_file_filter_add_pattern(jpeg, "*.jpg");
	gtk_file_filter_add_pattern(jpeg, "*.jpeg");
	gtk_file_filter_add_pattern(jpeg, "*.jpe");
	gtk_file_filter_set_name(jpeg, "JPEG");
	gtk_file_chooser_add_filter(chooser, jpeg);

	GtkFileFilter *all = gtk_file_filter_new();
	gtk_file_filter_add_pattern(all, "*");
	gtk_file_filter_set_name(all, "All files");
	gtk_file_chooser_add_filter(chooser, all);

	GFile *file = NULL;
	switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
	default:
		gtk_widget_destroy(dialog);
		// Fall-through.
	case GTK_RESPONSE_NONE:
		return file;
	case GTK_RESPONSE_ACCEPT:
		file = gtk_file_chooser_get_file(chooser);
		gtk_widget_destroy(dialog);
		return file;
	}
}

static GFile *
choose_file_to_open(void)
{
	GtkWidget *dialog = gtk_file_chooser_dialog_new("Open image",
		NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
		"_Cancel", GTK_RESPONSE_CANCEL,
		"_Open", GTK_RESPONSE_ACCEPT, NULL);
	return run_chooser(dialog);
}

static GFile *
choose_file_to_save(void)
{
	GtkWidget *dialog = gtk_file_chooser_dialog_new("Saved cropped image as",
		GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_SAVE,
		"_Cancel", GTK_RESPONSE_CANCEL,
		"_Save", GTK_RESPONSE_ACCEPT, NULL);

	GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
	gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
	(void) gtk_file_chooser_set_file(chooser, g.location, NULL);
	return run_chooser(dialog);
}

static void
on_save_as(G_GNUC_UNUSED GtkButton *button, G_GNUC_UNUSED gpointer user_data)
{
	tjhandle h = tjInitTransform();
	if (!h) {
		show_error_dialog(g_error_new_literal(
			G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h)));
		return;
	}

	// Convert up front, because the target is in memory.
	tjtransform t = {
		.r.x = g.left,
		.r.y = g.top,
		.r.w = g.right - g.left,
		.r.h = g.bottom - g.top,
		.op = TJXOP_NONE,
		.options = TJXOPT_CROP | TJXOPT_PROGRESSIVE | TJXOPT_PERFECT,
	};

	guchar *data = NULL;
	gulong len = 0;
	if (tjTransform(h, (const guchar *) g.data, g.len, 1, &data, &len, &t, 0)) {
		show_error_dialog(g_error_new_literal(
			G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h)));
		goto out;
	}

	GFile *file = choose_file_to_save();
	GError *error = NULL;
	if (file &&
		!g_file_replace_contents(file, (const char *) data, len, NULL, FALSE,
			G_FILE_CREATE_NONE, NULL, NULL, &error)) {
		show_error_dialog(error);
		goto out;
	}

	g_clear_object(&file);
	tjFree(data);
out:
	if (tjDestroy(h)) {
		show_error_dialog(g_error_new_literal(
			G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h)));
	}
}

static void
update_label(void)
{
	gchar *text = g_strdup_printf("(%d, %d) × (%d, %d)", g.left, g.top,
		g.right - g.left, g.bottom - g.top);
	gtk_label_set_label(GTK_LABEL(g.label), text);
	g_free(text);
}

static void
update(void)
{
	update_label();
	gtk_widget_queue_draw(g.view);
}

static void
on_reset(G_GNUC_UNUSED GtkButton *button, G_GNUC_UNUSED gpointer user_data)
{
	g.top = 0;
	g.left = 0;
	g.right = g.width;
	g.bottom = g.height;
	update();
}

static gboolean
on_mouse(guint state, guint button, gdouble x, gdouble y)
{
	if (state != 0)
		return FALSE;

	switch (button) {
	case GDK_BUTTON_PRIMARY:
		g.left = CLAMP((int) (x - 1), 0, g.right) / g.mcu_width * g.mcu_width;
		g.top = CLAMP((int) (y - 1), 0, g.bottom) / g.mcu_height * g.mcu_height;
		update();
		return TRUE;
	case GDK_BUTTON_SECONDARY:
		// Inclusive of pointer position.
		g.right = CLAMP(x, g.left, g.width);
		g.bottom = CLAMP(y, g.top, g.height);
		update();
		return TRUE;
	default:
		return FALSE;
	}
}

static gboolean
on_press(G_GNUC_UNUSED GtkWidget *self, GdkEventButton *event,
	G_GNUC_UNUSED gpointer user_data)
{
	return on_mouse(event->state, event->button, event->x, event->y);
}

static gboolean
on_motion(G_GNUC_UNUSED GtkWidget *self, GdkEventMotion *event,
	G_GNUC_UNUSED gpointer user_data)
{
	switch (event->state) {
	case GDK_BUTTON1_MASK:
		return on_mouse(0, GDK_BUTTON_PRIMARY, event->x, event->y);
	case GDK_BUTTON3_MASK:
		return on_mouse(0, GDK_BUTTON_SECONDARY, event->x, event->y);
	}
	return FALSE;
}

static void
on_drag_begin(
	GtkGestureDrag *self, gdouble start_x, gdouble start_y, gpointer user_data)
{
	// The middle mouse button will never be triggered by touch screens,
	// so there is only the NULL sequence to care about.
	gtk_gesture_set_state(GTK_GESTURE(self), GTK_EVENT_SEQUENCE_CLAIMED);

	GdkWindow *window = gtk_widget_get_window(g.view);
	GdkCursor *cursor =
		gdk_cursor_new_from_name(gdk_window_get_display(window), "grabbing");
	gdk_window_set_cursor(window, cursor);
	g_object_unref(cursor);

	double *last = user_data;
	last[0] = start_x;
	last[1] = start_y;
}

static void
on_drag_update(GtkGestureDrag *self, gdouble offset_x, gdouble offset_y,
	gpointer user_data)
{
	double start_x = 0, start_y = 0;
	gtk_gesture_drag_get_start_point(self, &start_x, &start_y);

	double *last = user_data,
		diff_x = (start_x + offset_x) - last[0],
		diff_y = (start_y + offset_y) - last[1];

	last[0] = start_x + offset_x;
	last[1] = start_y + offset_y;

	GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(g.scrolled);
	GtkAdjustment *h = gtk_scrolled_window_get_hadjustment(sw);
	GtkAdjustment *v = gtk_scrolled_window_get_vadjustment(sw);
	if (diff_x)
		gtk_adjustment_set_value(h, gtk_adjustment_get_value(h) - diff_x);
	if (diff_y)
		gtk_adjustment_set_value(v, gtk_adjustment_get_value(v) - diff_y);
}

static void
on_drag_end(G_GNUC_UNUSED GtkGestureDrag *self, G_GNUC_UNUSED gdouble start_x,
	G_GNUC_UNUSED gdouble start_y, G_GNUC_UNUSED gpointer user_data)
{
	// Cursors follow the widget hierarchy.
	gdk_window_set_cursor(gtk_widget_get_window(g.view), NULL);
}

static gboolean
open_jpeg(const char *data, gsize len, GError **error)
{
	tjhandle h = tjInitDecompress();
	if (!h) {
		g_set_error_literal(
			error, G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h));
		return FALSE;
	}

	if (tjDecompressHeader3(h, (const guint8 *) data, len, &g.width, &g.height,
			&g.subsampling, &g.colorspace)) {
		g_set_error_literal(
			error, G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h));
		tjDestroy(h);
		return FALSE;
	}

	g.top = 0;
	g.left = 0;
	g.right = g.width;
	g.bottom = g.height;

	g.mcu_width = tjMCUWidth[g.subsampling];
	g.mcu_height = tjMCUHeight[g.subsampling];

	if (tjDestroy(h)) {
		g_set_error_literal(
			error, G_IO_ERROR, G_IO_ERROR_FAILED, tjGetErrorStr2(h));
		return FALSE;
	}

	// TODO(p): Eventually, convert to using fiv-io.c directly,
	// which will pull in most of fiv's dependencies,
	// but also enable correct color management, even for CMYK.
	// NOTE: It's possible to include this as a mode of the main binary.
	GInputStream *is = g_memory_input_stream_new_from_data(data, len, NULL);
	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(is, NULL, error);
	g_object_unref(is);
	if (!pixbuf)
		return FALSE;

	const char *orientation = gdk_pixbuf_get_option(pixbuf, "orientation");
	if (orientation && strlen(orientation) == 1) {
		int n = *orientation - '0';
		if (n >= 1 && n <= 8) {
			// TODO(p): Apply this to the view, somehow.
		}
	}

	g.surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, 1, NULL);
	cairo_status_t surface_status = cairo_surface_status(g.surface);
	if (surface_status != CAIRO_STATUS_SUCCESS) {
		g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_FAILED,
			cairo_status_to_string(surface_status));
		g_clear_pointer(&g.surface, cairo_surface_destroy);
		g_object_unref(pixbuf);
		return FALSE;
	}
	return TRUE;
}

int
main(int argc, char *argv[])
{
	gboolean show_version = FALSE;
	gchar **args = NULL;

	const GOptionEntry options[] = {
		{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args,
			NULL, "[FILE | URI]"},
		{"version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
			&show_version, "Output version information and exit", NULL},
		{},
	};

	GError *error = NULL;
	gboolean initialized = gtk_init_with_args(
		&argc, &argv, " - Lossless JPEG cropper", options, NULL, &error);
	if (show_version) {
		printf("fiv-jpegcrop " PROJECT_VERSION "\n");
		return 0;
	}
	if (!initialized)
		exit_fatal("%s", error->message);

	gtk_window_set_default_icon_name(PROJECT_NAME);

	// TODO(p): Rather use G_OPTION_ARG_CALLBACK with G_OPTION_FLAG_FILENAME.
	// Alternatively, GOptionContext with gtk_get_option_group(TRUE).
	// Then we can show the help string here instead (in fiv as well).
	if (args && args[1])
		exit_fatal("Too many arguments");
	else if (args)
		g.location = g_file_new_for_commandline_arg(args[0]);
	else if (!(g.location = choose_file_to_open()))
		exit(EXIT_SUCCESS);

	g.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(g.window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	GFileInfo *info = g_file_query_info(g.location,
		G_FILE_ATTRIBUTE_STANDARD_NAME
		"," G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
		G_FILE_QUERY_INFO_NONE, NULL, &error);
	if (!info ||
		!g_file_load_contents(
			g.location, NULL, &g.data, &g.len, NULL, &error) ||
		!open_jpeg(g.data, g.len, &error)) {
		show_error_dialog(error);
		exit(EXIT_FAILURE);
	}

	GtkWidget *header = gtk_header_bar_new();
	gtk_window_set_titlebar(GTK_WINDOW(g.window), header);
	gtk_header_bar_set_title(
		GTK_HEADER_BAR(header), g_file_info_get_display_name(info));
	gtk_header_bar_set_subtitle(GTK_HEADER_BAR(header),
		"Use L/R mouse buttons to adjust the crop region.");
	gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header), TRUE);

	g.label = gtk_label_new(NULL);
	gtk_header_bar_pack_start(GTK_HEADER_BAR(header), g.label);
	update_label();

	GtkWidget *save = gtk_button_new_from_icon_name(
		"document-save-as-symbolic", GTK_ICON_SIZE_BUTTON);
	gtk_widget_set_tooltip_text(save, "Save as...");
	g_signal_connect(save, "clicked", G_CALLBACK(on_save_as), NULL);
	gtk_header_bar_pack_end(GTK_HEADER_BAR(header), save);

	GtkWidget *reset = gtk_button_new_with_mnemonic("_Reset");
	gtk_widget_set_tooltip_text(reset, "Reset the crop region");
	g_signal_connect(reset, "clicked", G_CALLBACK(on_reset), NULL);
	gtk_header_bar_pack_end(GTK_HEADER_BAR(header), reset);

	g.view = gtk_drawing_area_new();
	gtk_widget_set_size_request(g.view, g.width + 2, g.height + 2);
	gtk_widget_add_events(g.view,
		GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
			GDK_POINTER_MOTION_MASK);
	g_signal_connect(g.view, "draw",
		G_CALLBACK(on_draw), NULL);
	g_signal_connect(g.view, "button-press-event",
		G_CALLBACK(on_press), NULL);
	g_signal_connect(g.view, "motion-notify-event",
		G_CALLBACK(on_motion), NULL);

	g.scrolled = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_overlay_scrolling(
		GTK_SCROLLED_WINDOW(g.scrolled), FALSE);
	gtk_scrolled_window_set_propagate_natural_width(
		GTK_SCROLLED_WINDOW(g.scrolled), TRUE);
	gtk_scrolled_window_set_propagate_natural_height(
		GTK_SCROLLED_WINDOW(g.scrolled), TRUE);

	GtkGesture *drag = gtk_gesture_drag_new(g.scrolled);
	gtk_event_controller_set_propagation_phase(
		GTK_EVENT_CONTROLLER(drag), GTK_PHASE_CAPTURE);
	gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(drag), GDK_BUTTON_MIDDLE);

	double last_drag_point[2] = {};
	g_signal_connect(drag, "drag-begin",
		G_CALLBACK(on_drag_begin), last_drag_point);
	g_signal_connect(drag, "drag-update",
		G_CALLBACK(on_drag_update), last_drag_point);
	g_signal_connect(drag, "drag-end",
		G_CALLBACK(on_drag_end), last_drag_point);

	gtk_container_add(GTK_CONTAINER(g.scrolled), g.view);
	gtk_container_add(GTK_CONTAINER(g.window), g.scrolled);
	gtk_window_set_default_size(GTK_WINDOW(g.window), 800, 600);
	gtk_widget_show_all(g.window);

	// It probably needs to be realized.
	GdkWindow *window = gtk_widget_get_window(g.scrolled);
	GdkCursor *cursor =
		gdk_cursor_new_from_name(gdk_window_get_display(window), "crosshair");
	gdk_window_set_cursor(window, cursor);
	g_object_unref(cursor);

	gtk_main();

	g_free(g.data);
	g_object_unref(g.location);
	g_object_unref(info);
	return 0;
}