aboutsummaryrefslogtreecommitdiff
path: root/src/imagedisplay.c
blob: bc2fa53f27c9b01f6cb51e1d7deca62038e938fd (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
/*
 * imagedisplay.c
 *
 * Show raw and processed images
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "imagedisplay.h"
#include "utils.h"

/* Free pixbuf data when reference count drops to zero */
static void imagedisplay_free_data(guchar *image_eightbit, ImageDisplay *imagedisplay) {
	free(image_eightbit);
}

static void imagedisplay_rescale(ImageDisplay *imagedisplay, unsigned int v_w, unsigned int v_h) {

	unsigned int w, h;
	float aspect_image, aspect_window;
	
	w = imagedisplay->imagerecord.width;
	h = imagedisplay->imagerecord.height;
	
	/* Preserve aspect ratio */
	aspect_image = (float)w/h;
	aspect_window = (float)v_w/v_h;
	if ( aspect_window > aspect_image ) {
		v_w = aspect_image*v_h;
	} else {
		v_h = v_w/aspect_image;
	}
	
	if ( imagedisplay->pixbuf_scaled ) {
		g_object_unref(imagedisplay->pixbuf_scaled);
	}
	
	/* Create the scaled pixbuf from the 8-bit display data */
	imagedisplay->pixbuf_scaled = gdk_pixbuf_scale_simple(imagedisplay->pixbuf, v_w, v_h, GDK_INTERP_BILINEAR);
	imagedisplay->view_width = v_w;
	imagedisplay->view_height = v_h;
	
}

static gboolean imagedisplay_configure_event(GtkWidget *widget, GdkEventConfigure *event, ImageDisplay *imagedisplay) {

	imagedisplay->drawingarea_width = event->width;
	imagedisplay->drawingarea_height = event->height;
	imagedisplay_rescale(imagedisplay, event->width, event->height);
	
	return FALSE;

}

void imagedisplay_put_data(ImageDisplay *imagedisplay, ImageRecord imagerecord) {

	unsigned int x, y;
	unsigned int w, h;
	int min, max;
	double c, scale;
	int b;
	
	h = imagerecord.height;
	w = imagerecord.width;
	
	if ( imagedisplay->pixbuf ) {
		g_object_unref(imagedisplay->pixbuf);
	}
	
	min = 2<<15;  max = 0;
	b = (h+w)/20;	/* Number of pixels of ignored border */
	for ( y=b; y<h-b; y++ ) {
		for ( x=b; x<w-b; x++ ) {
			uint16_t val;
			val = imagerecord.image[x+w*y];
			if ( val > max ) max = val;
			if ( val < min ) min = val;
		}
	}
	
	c = 0.1;
	scale = 255.0 / log(1+c*(max-min));
	
	/* Turn 16-bit image data into 8-bit display data */
	imagedisplay->data = malloc(3*w*h);
	for ( y=0; y<h; y++ ) {
		for ( x=0; x<w; x++ ) {
		
			uint16_t val16, val8;
			
			val16 = imagerecord.image[x+w*y];
			val8 = scale * log(1+c*(val16-min));
			
			imagedisplay->data[3*( x+w*(h-1-y) )] = val8;
			imagedisplay->data[3*( x+w*(h-1-y) )+1] = val8;
			imagedisplay->data[3*( x+w*(h-1-y) )+2] = val8;
			
		}
	}
	
	memcpy(&imagedisplay->imagerecord, &imagerecord, sizeof(ImageRecord));
	
	/* Create the pixbuf from the 8-bit display data */
	imagedisplay->pixbuf = gdk_pixbuf_new_from_data(imagedisplay->data, GDK_COLORSPACE_RGB, FALSE, 8, w, h, w*3,
				(GdkPixbufDestroyNotify)imagedisplay_free_data, imagedisplay);
	
	if ( imagedisplay->realised ) {
		imagedisplay_force_redraw(imagedisplay);
	}

}

void imagedisplay_clear_marks(ImageDisplay *imagedisplay) {
	
	ImageDisplayMark *cur;
	
	cur = imagedisplay->marks;
	while ( cur ) {
		ImageDisplayMark *next = cur->next;
		free(cur);
		cur = next;
	}
	imagedisplay->marks = NULL;
	
}

static void imagedisplay_destroyed(GtkWidget *widget, ImageDisplay *imagedisplay) {

	imagedisplay_clear_marks(imagedisplay);
	
	if ( imagedisplay->flags & IMAGEDISPLAY_QUIT_IF_CLOSED ) {
		gtk_exit(0);
	}
	
	g_object_unref(G_OBJECT(imagedisplay->gc_centre));
	g_object_unref(G_OBJECT(imagedisplay->gc_tiltaxis));
	g_object_unref(G_OBJECT(imagedisplay->gc_marks_1));
	g_object_unref(G_OBJECT(imagedisplay->gc_marks_2));
	
	if ( imagedisplay->flags & IMAGEDISPLAY_FREE ) {
		free(imagedisplay->imagerecord.image);
	}

	free(imagedisplay);
	
}

void imagedisplay_close(ImageDisplay *imagedisplay) {
	imagedisplay->flags = (imagedisplay->flags | IMAGEDISPLAY_QUIT_IF_CLOSED)^IMAGEDISPLAY_QUIT_IF_CLOSED;
	if ( imagedisplay->window ) {
		gtk_widget_destroy(imagedisplay->window);
	}
}

#define imagedisplay_draw_line(gc,x1,y1,x2,y2) (gdk_draw_line(drawingarea->window,gc, \
						xoffs+(x1), yoffs+imagedisplay->view_height-1-(y1), \
						xoffs+(x2), yoffs+imagedisplay->view_height-1-(y2)))

static gboolean imagedisplay_redraw(GtkWidget *drawingarea, GdkEventExpose *event, ImageDisplay *imagedisplay) {
	
	double scale, xoffs, yoffs;
	ImageDisplayMark *cur;
	double max;
	
	xoffs = ((double)imagedisplay->drawingarea_width - imagedisplay->view_width) / 2;
	yoffs = ((double)imagedisplay->drawingarea_height - imagedisplay->view_height) / 2;
	scale = (double)imagedisplay->view_width/imagedisplay->imagerecord.width;
	
	gdk_draw_pixbuf(drawingarea->window, drawingarea->style->bg_gc[GTK_WIDGET_STATE(drawingarea)], imagedisplay->pixbuf_scaled,
			0, 0, xoffs, yoffs, imagedisplay->view_width, imagedisplay->view_height,
			GDK_RGB_DITHER_NONE, 0, 0);
	
	if ( imagedisplay->flags & IMAGEDISPLAY_SHOW_TILT_AXIS ) {
		/* This is nasty, but works */
		imagedisplay_draw_line(imagedisplay->gc_tiltaxis,
			imagedisplay->imagerecord.x_centre * scale,
			imagedisplay->imagerecord.y_centre * scale,
			(imagedisplay->imagerecord.x_centre + imagedisplay->imagerecord.width) * scale,
			(imagedisplay->imagerecord.y_centre + imagedisplay->imagerecord.width
					* tan(deg2rad(imagedisplay->imagerecord.omega))) * scale);
		imagedisplay_draw_line(imagedisplay->gc_tiltaxis,
			imagedisplay->imagerecord.x_centre * scale,
			imagedisplay->imagerecord.y_centre * scale,
			(imagedisplay->imagerecord.x_centre - imagedisplay->imagerecord.width) * scale,
			(imagedisplay->imagerecord.y_centre - imagedisplay->imagerecord.width
					* tan(deg2rad(imagedisplay->imagerecord.omega))) * scale);
	}
	
	/* NB This calls the function above, which sorts out stuff */
	if ( imagedisplay->flags & IMAGEDISPLAY_SHOW_CENTRE ) {
		imagedisplay_draw_line(imagedisplay->gc_centre,
			imagedisplay->imagerecord.x_centre * scale - 10,
			imagedisplay->imagerecord.y_centre * scale,
			imagedisplay->imagerecord.x_centre * scale + 10,
			imagedisplay->imagerecord.y_centre * scale);
		imagedisplay_draw_line(imagedisplay->gc_centre,
			imagedisplay->imagerecord.x_centre * scale,
			imagedisplay->imagerecord.y_centre * scale - 10,
			imagedisplay->imagerecord.x_centre * scale,
			imagedisplay->imagerecord.y_centre * scale + 10);
	}

	/* Find the maximum intensity */
	cur = imagedisplay->marks;
	max = 0.0;
	while ( cur ) {
	
		if ( cur->weight < 0.0 ) {
			printf("ID: Warning: ImageDisplayMark with negative weight\n");
			cur = cur->next;
			continue;
		}
		
		if ( log(1+0.1*cur->weight) > max ) max = log(1+0.1*cur->weight);
		cur = cur->next;
		
	}
	
	cur = imagedisplay->marks;
	while ( cur ) {
	
		GdkGC *gc;
		
		switch ( cur->type ) {
			case IMAGEDISPLAY_MARK_CIRCLE_1 : gc = imagedisplay->gc_marks_1; break;
			case IMAGEDISPLAY_MARK_CIRCLE_2 : gc = imagedisplay->gc_marks_2; break;
			case IMAGEDISPLAY_MARK_CIRCLE_3 : gc = imagedisplay->gc_marks_3; break;
			case IMAGEDISPLAY_MARK_LINE_1 : gc = imagedisplay->gc_marks_1; break;
			case IMAGEDISPLAY_MARK_LINE_2 : gc = imagedisplay->gc_marks_2; break;
			default : gc = imagedisplay->gc_marks_1; break;
		}
		
		if ( (cur->type == IMAGEDISPLAY_MARK_CIRCLE_1) || (cur->type == IMAGEDISPLAY_MARK_CIRCLE_2)
			 || (cur->type == IMAGEDISPLAY_MARK_CIRCLE_3) ) {
		
			double r;
		
			if ( cur->weight < 0.0 ) {
				cur = cur->next;
				continue;
			}
			
			if ( cur->type == IMAGEDISPLAY_MARK_CIRCLE_1 ) {
				r = 10.0;
			} else {
				r = 20.0 * (log(1+0.1*cur->weight)/max);
			}
			
			gdk_draw_arc(drawingarea->window, gc, FALSE,
						xoffs + cur->x*scale - r,
						yoffs + imagedisplay->view_height-1-cur->y*scale - r,
						2*r, 2*r, 0, 64*360);
			
		} else if ( (cur->type == IMAGEDISPLAY_MARK_LINE_1) || (cur->type == IMAGEDISPLAY_MARK_LINE_2) ) {
		
			gdk_draw_line(drawingarea->window, gc,
						xoffs + cur->x*scale,
						yoffs + imagedisplay->view_height-1-cur->y*scale,
						xoffs + cur->x2*scale,
						yoffs + imagedisplay->view_height-1-cur->y2*scale );
		
		}
		
		cur = cur->next;
	}

	return FALSE;

}

static gint imagedisplay_realize(GtkWidget *widget, ImageDisplay *imagedisplay) {

	GdkColor colour;
	
	imagedisplay->gc_centre = gdk_gc_new(imagedisplay->drawingarea->window);
	gdk_color_parse("yellow", &colour);
	gdk_gc_set_rgb_fg_color(imagedisplay->gc_centre, &colour);
	
	imagedisplay->gc_tiltaxis = gdk_gc_new(imagedisplay->drawingarea->window);
	gdk_color_parse("#6600dd", &colour);
	gdk_gc_set_rgb_fg_color(imagedisplay->gc_tiltaxis, &colour);

	imagedisplay->gc_marks_1 = gdk_gc_new(imagedisplay->drawingarea->window);
	gdk_color_parse("#dd0000", &colour);
	gdk_gc_set_rgb_fg_color(imagedisplay->gc_marks_1, &colour);

	imagedisplay->gc_marks_2 = gdk_gc_new(imagedisplay->drawingarea->window);
	gdk_color_parse("#00dd00", &colour);
	gdk_gc_set_rgb_fg_color(imagedisplay->gc_marks_2, &colour);

	imagedisplay->gc_marks_3 = gdk_gc_new(imagedisplay->drawingarea->window);
	gdk_color_parse("#00ddff", &colour);
	gdk_gc_set_rgb_fg_color(imagedisplay->gc_marks_3, &colour);

	imagedisplay->realised = TRUE;

	return 0;
	
}

ImageDisplay *imagedisplay_new_nowindow(ImageRecord imagerecord, ImageDisplayFlags flags, const char *message,
					GCallback mouse_click_func, gpointer callback_data) {

	ImageDisplay *imagedisplay;
	
	imagedisplay = malloc(sizeof(ImageDisplay));
	imagedisplay->imagerecord = imagerecord;
	imagedisplay->view_width = 512;
	imagedisplay->view_height = 512;
	imagedisplay->message = message;
	imagedisplay->mouse_click_func = mouse_click_func;
	imagedisplay->flags = flags;
	imagedisplay->marks = NULL;
	imagedisplay->pixbuf = NULL;
	imagedisplay->pixbuf_scaled = NULL;
	imagedisplay->realised = FALSE;
	imagedisplay->window = NULL;
	
	imagedisplay_put_data(imagedisplay, imagerecord);
	
	imagedisplay->vbox = gtk_vbox_new(FALSE, 0);
	
	if ( message ) {
		GtkWidget *label;
		label = gtk_label_new(message);
		gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
		gtk_box_pack_start(GTK_BOX(imagedisplay->vbox), label, FALSE, TRUE, 3);
	}
	
	imagedisplay->drawingarea = gtk_drawing_area_new();
	gtk_box_pack_start(GTK_BOX(imagedisplay->vbox), imagedisplay->drawingarea, TRUE, TRUE, 0);
	
	if ( imagedisplay->mouse_click_func ) {
		gtk_widget_add_events(GTK_WIDGET(imagedisplay->drawingarea), GDK_BUTTON_PRESS_MASK);
		g_signal_connect(GTK_OBJECT(imagedisplay->drawingarea), "button-press-event",
					G_CALLBACK(imagedisplay->mouse_click_func), callback_data);
	}
	
	g_signal_connect(GTK_OBJECT(imagedisplay->drawingarea), "realize", G_CALLBACK(imagedisplay_realize), imagedisplay);
	g_signal_connect(GTK_OBJECT(imagedisplay->drawingarea), "destroy", G_CALLBACK(imagedisplay_destroyed), imagedisplay);
	g_signal_connect(GTK_OBJECT(imagedisplay->drawingarea), "configure_event", G_CALLBACK(imagedisplay_configure_event), imagedisplay);
	g_signal_connect(GTK_OBJECT(imagedisplay->drawingarea), "expose-event", G_CALLBACK(imagedisplay_redraw), imagedisplay);
	
	return imagedisplay;

}

/* Display an image */
ImageDisplay *imagedisplay_open_with_message(ImageRecord imagerecord, const char *title, const char *message,
						ImageDisplayFlags flags, GCallback mouse_click_func, gpointer callback_data) {

	ImageDisplay *imagedisplay;
	GdkGeometry geom;
	
	imagedisplay = imagedisplay_new_nowindow(imagerecord, flags, message, mouse_click_func, callback_data);
	
	imagedisplay->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_add(GTK_CONTAINER(imagedisplay->window), imagedisplay->vbox);
	imagedisplay->title = strdup(title);
	gtk_window_set_title(GTK_WINDOW(imagedisplay->window), imagedisplay->title);
	
	geom.min_width = 128;	geom.min_height = 128;
	gtk_window_set_geometry_hints(GTK_WINDOW(imagedisplay->window), GTK_WIDGET(imagedisplay->drawingarea), &geom, GDK_HINT_MIN_SIZE);
	
	gtk_window_set_default_size(GTK_WINDOW(imagedisplay->window), 512, 512);
	
	gtk_widget_show_all(imagedisplay->window);
	
	return imagedisplay;
	
}

ImageDisplay *imagedisplay_open(ImageRecord image, const char *title, ImageDisplayFlags flags) {
	return imagedisplay_open_with_message(image, title, NULL, flags, NULL, NULL);
}

void imagedisplay_add_mark(ImageDisplay *imagedisplay, double x, double y, ImageDisplayMarkType type, double weight) {

	ImageDisplayMark *new;
	
	new = malloc(sizeof(ImageDisplayMark));
	new->x = x;  new->y = y;
	new->type = type;
	new->weight = weight;
	new->next = NULL;
	
	if ( !imagedisplay->marks ) {
		imagedisplay->marks = new;
	} else {
		ImageDisplayMark *cur = imagedisplay->marks;
		while ( cur->next ) {
			cur = cur->next;
		}
		cur->next = new;
	}

}

void imagedisplay_add_line(ImageDisplay *imagedisplay, double x1, double y1, double x2, double y2, ImageDisplayMarkType type) {

	ImageDisplayMark *new;
	
	new = malloc(sizeof(ImageDisplayMark));
	new->x = x1;  new->y = y1;
	new->x2 = x2;  new->y2 = y2;
	new->type = type;
	new->weight = 1.0;	/* This field makes little sense for a line */
	new->next = NULL;
	
	if ( !imagedisplay->marks ) {
		imagedisplay->marks = new;
	} else {
		ImageDisplayMark *cur = imagedisplay->marks;
		while ( cur->next ) {
			cur = cur->next;
		}
		cur->next = new;
	}

}

void imagedisplay_force_redraw(ImageDisplay *imagedisplay) {
	imagedisplay_rescale(imagedisplay, imagedisplay->drawingarea_width, imagedisplay->drawingarea_height);
	gtk_widget_queue_draw_area(imagedisplay->drawingarea, 0, 0, imagedisplay->drawingarea_width, imagedisplay->drawingarea_height);
}