From f6a8e7ef02e6f449c2c3aa6186bf10d81c127dd3 Mon Sep 17 00:00:00 2001 From: Tom White Date: Mon, 29 Sep 2008 18:23:46 +0100 Subject: Add a scale bar to the diffraction patterns --- src/imagedisplay.c | 31 +++++++++++++++++++++++++++++++ src/mapping.c | 14 ++++++++++++++ src/mapping.h | 2 ++ 3 files changed, 47 insertions(+) diff --git a/src/imagedisplay.c b/src/imagedisplay.c index 4ca46a5..b6de26a 100644 --- a/src/imagedisplay.c +++ b/src/imagedisplay.c @@ -23,6 +23,7 @@ #include "imagedisplay.h" #include "utils.h" +#include "mapping.h" /* Free pixbuf data when reference count drops to zero */ static void imagedisplay_free_data(guchar *image_eightbit, ImageDisplay *imagedisplay) { @@ -167,6 +168,33 @@ void imagedisplay_close(ImageDisplay *imagedisplay) { } } +static void imagedisplay_add_scalebar(ImageDisplay *imagedisplay, GtkWidget *drawingarea, + double scale, double xoffs, double yoffs) { + + PangoLayout *layout; + double sb; + PangoRectangle rect; + int bwidth, bheight; + int view_height = imagedisplay->view_height; + + sb = mapping_scale_bar_length(&imagedisplay->imagerecord); + layout = gtk_widget_create_pango_layout(drawingarea, "1 nm^-1"); + pango_layout_get_pixel_extents(layout, &rect, NULL); + + bwidth = (sb*scale)+20; + bheight = rect.height+30; + if ( rect.width > bwidth ) bwidth = rect.width+20; + gdk_draw_rectangle(drawingarea->window, drawingarea->style->bg_gc[GTK_WIDGET_STATE(drawingarea)], TRUE, + xoffs+20, yoffs+view_height-20-bheight, + bwidth, bheight); + gdk_draw_line(drawingarea->window, drawingarea->style->fg_gc[GTK_WIDGET_STATE(drawingarea)], + xoffs+30, yoffs+view_height-30, + xoffs+30+(scale*sb), yoffs+view_height-30); + gdk_draw_layout(drawingarea->window, drawingarea->style->fg_gc[GTK_WIDGET_STATE(drawingarea)], + xoffs+30, yoffs+view_height-20-bheight+10, layout); + +} + #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))) @@ -202,6 +230,9 @@ static gboolean imagedisplay_redraw(GtkWidget *drawingarea, GdkEventExpose *even * tan(imagedisplay->imagerecord.omega)) * scale); } + /* Add scale bar */ + imagedisplay_add_scalebar(imagedisplay, drawingarea, scale, xoffs, yoffs); + /* NB This calls the function above, which sorts out stuff */ if ( imagedisplay->flags & IMAGEDISPLAY_SHOW_CENTRE ) { imagedisplay_draw_line(imagedisplay->gc_centre, diff --git a/src/mapping.c b/src/mapping.c index 54a7235..4897450 100644 --- a/src/mapping.c +++ b/src/mapping.c @@ -128,6 +128,20 @@ int mapping_scale(ImageFeature *refl, double *ddx, double *ddy) { } +/* Return the length of a 1 nm^-1 scale bar in the given image (in pixels) + * Result only strictly valid at the centre of the image */ +double mapping_scale_bar_length(ImageRecord *image) { + + switch ( image->fmode ) { + case FORMULATION_PIXELSIZE : return 1.0e9/image->pixel_size; + case FORMULATION_CLEN : return image->camera_len*image->lambda; + default : fprintf(stderr, "Unrecognised formulation mode in mapping_scale_bar_length.\n"); + } + + return 0.0; + +} + void mapping_map_features(ControlContext *ctx) { int i; diff --git a/src/mapping.h b/src/mapping.h index 6724499..82d2f04 100644 --- a/src/mapping.h +++ b/src/mapping.h @@ -18,11 +18,13 @@ #include "reflections.h" #include "control.h" +#include "image.h" extern void mapping_rotate(double x, double y, double z, double *ddx, double *ddy, double *ddz, double omega, double tilt); extern int mapping_map_to_space(ImageFeature *refl, double *ddx, double *ddy, double *ddz, double *twotheta); extern int mapping_scale(ImageFeature *refl, double *ddx, double *ddy); +extern double mapping_scale_bar_length(ImageRecord *image); extern void mapping_adjust_axis(ControlContext *ctx, double offset); extern void mapping_map_features(ControlContext *ctx); -- cgit v1.2.3