X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/f37c549b29baa06cc6931ce0434d90ab7f619d6f..64d557a287f0e695856d6bc579293b399b8a07ea:/src/vikgeoreflayer.c diff --git a/src/vikgeoreflayer.c b/src/vikgeoreflayer.c index 57915052..d152f5ca 100644 --- a/src/vikgeoreflayer.c +++ b/src/vikgeoreflayer.c @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include "icons/icons.h" @@ -128,6 +130,9 @@ struct _VikGeorefLayer { gdouble mpp_easting, mpp_northing; guint width, height; + GdkPixbuf *scaled; + guint32 scaled_width, scaled_height; + gint click_x, click_y; }; @@ -208,6 +213,9 @@ VikGeorefLayer *georef_layer_new ( ) vgl->pixbuf = NULL; vgl->click_x = -1; vgl->click_y = -1; + vgl->scaled = NULL; + vgl->scaled_width = 0; + vgl->scaled_height = 0; return vgl; } @@ -219,20 +227,48 @@ static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ) VikViewport *vp = VIK_VIEWPORT(data); struct UTM utm_middle; gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp); + GdkPixbuf *pixbuf = vgl->pixbuf; + guint layer_width = vgl->width; + guint layer_height = vgl->height; + vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle ); - if ( xmpp == vgl->mpp_easting && ympp == vgl->mpp_northing ) + /* scale the pixbuf if it doesn't match our dimensions */ + if ( xmpp != vgl->mpp_easting || ympp != vgl->mpp_northing ) { - guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp); - gint32 x, y; - vgl->corner.zone = utm_middle.zone; - vgl->corner.letter = utm_middle.letter; - VikCoord corner_coord; - vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) ); - vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y ); - if ( (x < 0 || x < width) && (y < 0 || y < height) && x+vgl->width > 0 && y+vgl->height > 0 ) - vik_viewport_draw_pixbuf ( vp, vgl->pixbuf, 0, 0, x, y, vgl->width, vgl->height ); /* todo: draw only what we need to. */ + layer_width = round(vgl->width * vgl->mpp_easting / xmpp); + layer_height = round(vgl->height * vgl->mpp_northing / ympp); + + /* rescale if necessary */ + if (layer_width == vgl->scaled_width && layer_height == vgl->scaled_height && vgl->scaled != NULL) + pixbuf = vgl->scaled; + else + { + pixbuf = gdk_pixbuf_scale_simple( + vgl->pixbuf, + layer_width, + layer_height, + GDK_INTERP_BILINEAR + ); + + if (vgl->scaled != NULL) + g_object_unref(vgl->scaled); + + vgl->scaled = pixbuf; + vgl->scaled_width = layer_width; + vgl->scaled_height = layer_height; + } } + + guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp); + gint32 x, y; + vgl->corner.zone = utm_middle.zone; + vgl->corner.letter = utm_middle.letter; + VikCoord corner_coord; + vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) ); + vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y ); + if ( (x < 0 || x < width) && (y < 0 || y < height) && x+layer_width > 0 && y+layer_height > 0 ) + vik_viewport_draw_pixbuf ( vp, pixbuf, 0, 0, x, y, layer_width, layer_height ); /* todo: draw only what we need to. */ } } @@ -240,6 +276,8 @@ static void georef_layer_free ( VikGeorefLayer *vgl ) { if ( vgl->image != NULL ) g_free ( vgl->image ); + if ( vgl->scaled != NULL ) + g_object_unref ( vgl->scaled ); } VikGeorefLayer *georef_layer_create ( VikViewport *vp ) @@ -260,6 +298,11 @@ static void georef_layer_load_image ( VikGeorefLayer *vgl ) if ( vgl->pixbuf ) g_object_unref ( G_OBJECT(vgl->pixbuf) ); + if ( vgl->scaled ) + { + g_object_unref ( G_OBJECT(vgl->scaled) ); + vgl->scaled = NULL; + } vgl->pixbuf = gdk_pixbuf_new_from_file ( vgl->image, &gx ); @@ -281,6 +324,11 @@ static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image ) { if ( vgl->image ) g_free ( vgl->image ); + if ( vgl->scaled ) + { + g_object_unref ( vgl->scaled ); + vgl->scaled = NULL; + } if ( image == NULL ) vgl->image = NULL; vgl->image = g_strdup ( image ); @@ -404,19 +452,19 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_button, FALSE, FALSE, 3 ); ce_label = gtk_label_new ( _("Corner pixel easting:") ); - ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 5 ), 1, 4 ); + ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 0 ), 1, 4 ); gtk_widget_set_tooltip_text ( GTK_WIDGET(ce_spin), _("the UTM \"easting\" value of the upper-right corner pixel of the map") ); cn_label = gtk_label_new ( _("Corner pixel northing:") ); - cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 5 ), 1, 4 ); + cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 0 ), 1, 4 ); gtk_widget_set_tooltip_text ( GTK_WIDGET(cn_spin), _("the UTM \"northing\" value of the upper-right corner pixel of the map") ); xlabel = gtk_label_new ( _("X (easting) scale (mpp): ")); ylabel = gtk_label_new ( _("Y (northing) scale (mpp): ")); - xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 ); + xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 ); gtk_widget_set_tooltip_text ( GTK_WIDGET(xspin), _("the scale of the map in the X direction (meters per pixel)") ); - yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 ); + yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 ); gtk_widget_set_tooltip_text ( GTK_WIDGET(yspin), _("the scale of the map in the Y direction (meters per pixel)") ); imagelabel = gtk_label_new ( _("Map Image:") );