Code

replace text strings by ints for tools/bounding_box
[inkscape.git] / src / sp-image.cpp
index 631eb6a9c36f8544637be22497926f09e569c2ac..95e104b7652774de37739cd4266a38ea20e179d1 100644 (file)
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
-#include <libnr/nr-matrix-fns.h>
 
+#include <cstring>
+#include <string>
+#include <libnr/nr-matrix-fns.h>
+#include <libnr/nr-matrix-ops.h>
+#include <libnr/nr-translate-matrix-ops.h>
+#include <libnr/nr-scale-translate-ops.h>
 //#define GDK_PIXBUF_ENABLE_BACKEND 1
 //#include <gdk-pixbuf/gdk-pixbuf-io.h>
 #include "display/nr-arena-image.h"
+#include <display/curve.h>
 
 //Added for preserveAspectRatio support -- EAF
 #include "enums.h"
 #include "brokenimage.xpm"
 #include "document.h"
 #include "sp-image.h"
+#include "sp-clippath.h"
 #include <glibmm/i18n.h>
 #include "xml/quote.h"
 #include <xml/repr.h>
 
+#include "libnr/nr-matrix-fns.h"
+
 #include "io/sys.h"
 #include <png.h>
-
+#if ENABLE_LCMS
+#include "color-profile-fns.h"
+#include "color-profile.h"
+//#define DEBUG_LCMS
+#ifdef DEBUG_LCMS
+#include "prefs-utils.h"
+#include <gtk/gtkmessagedialog.h>
+#endif // DEBUG_LCMS
+#endif // ENABLE_LCMS
 /*
  * SPImage
  */
@@ -57,8 +74,10 @@ static gchar * sp_image_description (SPItem * item);
 static void sp_image_snappoints(SPItem const *item, SnapPointsIter p);
 static NRArenaItem *sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
 static NR::Matrix sp_image_set_transform (SPItem *item, NR::Matrix const &xform);
+static void sp_image_set_curve(SPImage *image);
+
 
-GdkPixbuf * sp_image_repr_read_image (Inkscape::XML::Node * repr);
+GdkPixbuf *sp_image_repr_read_image (const gchar *href, const gchar *absref, const gchar *base);
 static GdkPixbuf *sp_image_pixbuf_force_rgba (GdkPixbuf * pixbuf);
 static void sp_image_update_canvas_image (SPImage *image);
 static GdkPixbuf * sp_image_repr_read_dataURI (const gchar * uri_data);
@@ -75,6 +94,36 @@ extern "C"
 
 }
 
+
+#ifdef DEBUG_LCMS
+extern guint update_in_progress;
+#define DEBUG_MESSAGE(key, ...) \
+{\
+    gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
+    gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
+    gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
+    dumpD &= ( (update_in_progress == 0) || dumpD2 );\
+    if ( dump )\
+    {\
+        g_message( __VA_ARGS__ );\
+\
+    }\
+    if ( dumpD )\
+    {\
+        GtkWidget *dialog = gtk_message_dialog_new(NULL,\
+                                                   GTK_DIALOG_DESTROY_WITH_PARENT, \
+                                                   GTK_MESSAGE_INFO,    \
+                                                   GTK_BUTTONS_OK,      \
+                                                   __VA_ARGS__          \
+                                                   );\
+        g_signal_connect_swapped(dialog, "response",\
+                                 G_CALLBACK(gtk_widget_destroy),        \
+                                 dialog);                               \
+        gtk_widget_show_all( dialog );\
+    }\
+}
+#endif // DEBUG_LCMS
+
 namespace Inkscape {
 namespace IO {
 
@@ -187,17 +236,17 @@ void user_read_data( png_structp png_ptr, png_bytep data, png_size_t length )
 //    g_message("things out");
 }
 
-void user_write_data( png_structp png_ptr, png_bytep data, png_size_t length )
+void user_write_data( png_structp /*png_ptr*/, png_bytep /*data*/, png_size_t /*length*/ )
 {
     //g_message( "user_write_data(%d)", length );
 }
 
-void user_flush_data( png_structp png_ptr )
+void user_flush_data( png_structp /*png_ptr*/ )
 {
     //g_message( "user_flush_data" );
 }
 
-GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **error )
+GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **/*error*/ )
 {
     GdkPixbuf* buf = NULL;
     PushPull youme;
@@ -317,12 +366,64 @@ GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **error )
                                 }
 */
 
+#if defined(PNG_sRGB_SUPPORTED)
+                                {
+                                    int intent = 0;
+                                    if ( png_get_sRGB(pngPtr, infoPtr, &intent) ) {
+//                                         g_message("Found an sRGB png chunk");
+                                    }
+                                }
+#endif // defined(PNG_sRGB_SUPPORTED)
+
+#if defined(PNG_cHRM_SUPPORTED)
+                                {
+                                    double white_x = 0;
+                                    double white_y = 0;
+                                    double red_x = 0;
+                                    double red_y = 0;
+                                    double green_x = 0;
+                                    double green_y = 0;
+                                    double blue_x = 0;
+                                    double blue_y = 0;
+
+                                    if ( png_get_cHRM(pngPtr, infoPtr,
+                                                      &white_x, &white_y,
+                                                      &red_x, &red_y,
+                                                      &green_x, &green_y,
+                                                      &blue_x, &blue_y) ) {
+//                                         g_message("Found a cHRM png chunk");
+                                    }
+                                }
+#endif // defined(PNG_cHRM_SUPPORTED)
+
+#if defined(PNG_gAMA_SUPPORTED)
+                                {
+                                    double file_gamma = 0;
+                                    if ( png_get_gAMA(pngPtr, infoPtr, &file_gamma) ) {
+//                                         g_message("Found a gAMA png chunk");
+                                    }
+                                }
+#endif // defined(PNG_gAMA_SUPPORTED)
+
+#if defined(PNG_iCCP_SUPPORTED)
+                                {
+                                    char* name = 0;
+                                    int compression_type = 0;
+                                    char* profile = 0;
+                                    png_uint_32 proflen = 0;
+                                    if ( png_get_iCCP(pngPtr, infoPtr, &name, &compression_type, &profile, &proflen) ) {
+//                                         g_message("Found an iCCP chunk named [%s] with %d bytes and comp %d", name, proflen, compression_type);
+                                    }
+                                }
+#endif // defined(PNG_iCCP_SUPPORTED)
+
+
                                 // now clean it up.
                                 png_destroy_read_struct( &pngPtr, &infoPtr, NULL );//&endPtr );
                             }
                             else
                             {
-                                g_message("Error when creating PNG read struct");
+//                                 g_message("Error when creating PNG read struct");
                             }
                         }
                     }
@@ -350,6 +451,7 @@ GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **error )
                         gchar *tmp = g_strdup_printf( "%d", dpiX );
                         if ( tmp )
                         {
+//                             g_message("Need to set DpiX: %s", tmp);
                             //gdk_pixbuf_set_option( buf, "Inkscape::DpiX", tmp );
                             g_free( tmp );
                         }
@@ -359,6 +461,7 @@ GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **error )
                         gchar *tmp = g_strdup_printf( "%d", dpiY );
                         if ( tmp )
                         {
+//                             g_message("Need to set DpiY: %s", tmp);
                             //gdk_pixbuf_set_option( buf, "Inkscape::DpiY", tmp );
                             g_free( tmp );
                         }
@@ -382,7 +485,7 @@ GdkPixbuf*  pixbuf_new_from_file( const char *filename, GError **error )
     }
     else
     {
-        g_warning ("unable to open file: %s", filename);
+        g_warning ("Unable to open linked file: %s", filename);
     }
 
 /*
@@ -464,6 +567,7 @@ sp_image_init (SPImage *image)
        image->width.unset();
        image->height.unset();
        image->aspect_align = SP_ASPECT_NONE;
+       image->curve = NULL;
 }
 
 static void
@@ -478,6 +582,7 @@ sp_image_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *rep
        sp_object_read_attr (object, "width");
        sp_object_read_attr (object, "height");
        sp_object_read_attr (object, "preserveAspectRatio");
+       sp_object_read_attr (object, "color-profile");
 
        /* Register */
        sp_document_add_resource (document, "image", object);
@@ -505,6 +610,17 @@ sp_image_release (SPObject *object)
                image->pixbuf = NULL;
        }
 
+#if ENABLE_LCMS
+       if (image->color_profile) {
+               g_free (image->color_profile);
+               image->color_profile = NULL;
+       }
+#endif // ENABLE_LCMS
+
+    if (image->curve) {
+               image->curve = sp_curve_unref (image->curve);
+       }
+
        if (((SPObjectClass *) parent_class)->release)
                ((SPObjectClass *) parent_class)->release (object);
 }
@@ -608,19 +724,39 @@ sp_image_set (SPObject *object, unsigned int key, const gchar *value)
                        image->aspect_clip = clip;
                }
                break;
+#if ENABLE_LCMS
+        case SP_PROP_COLOR_PROFILE:
+                if ( image->color_profile ) {
+                    g_free (image->color_profile);
+                }
+                image->color_profile = (value) ? g_strdup (value) : NULL;
+#ifdef DEBUG_LCMS
+                if ( value ) {
+                    DEBUG_MESSAGE( lcmsFour, "<image> color-profile set to '%s'", value );
+                } else {
+                    DEBUG_MESSAGE( lcmsFour, "<image> color-profile cleared" );
+                }
+#endif // DEBUG_LCMS
+                // TODO check on this HREF_MODIFIED flag
+                object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_IMAGE_HREF_MODIFIED_FLAG);
+                break;
+#endif // ENABLE_LCMS
        default:
                if (((SPObjectClass *) (parent_class))->set)
                        ((SPObjectClass *) (parent_class))->set (object, key, value);
                break;
        }
+       
+       sp_image_set_curve(image); //creates a curve at the image's boundary for snapping
 }
 
 static void
 sp_image_update (SPObject *object, SPCtx *ctx, unsigned int flags)
 {
-       SPImage *image;
+    SPImage *image;
 
-       image = (SPImage *) object;
+    image = (SPImage *) object;
+    SPDocument *doc = SP_OBJECT_DOCUMENT(object);
 
        if (((SPObjectClass *) (parent_class))->update)
                ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
@@ -632,9 +768,89 @@ sp_image_update (SPObject *object, SPCtx *ctx, unsigned int flags)
                }
                if (image->href) {
                        GdkPixbuf *pixbuf;
-                       pixbuf = sp_image_repr_read_image (object->repr);
+                       pixbuf = sp_image_repr_read_image (
+                    object->repr->attribute("xlink:href"),
+                    object->repr->attribute("sodipodi:absref"),
+                    doc->base);
                        if (pixbuf) {
                                pixbuf = sp_image_pixbuf_force_rgba (pixbuf);
+// BLIP
+#if ENABLE_LCMS
+                                if ( image->color_profile )
+                                {
+                                    int imagewidth = gdk_pixbuf_get_width( pixbuf );
+                                    int imageheight = gdk_pixbuf_get_height( pixbuf );
+                                    int rowstride = gdk_pixbuf_get_rowstride( pixbuf );
+                                    guchar* px = gdk_pixbuf_get_pixels( pixbuf );
+
+                                    if ( px ) {
+#ifdef DEBUG_LCMS
+                                        DEBUG_MESSAGE( lcmsFive, "in <image>'s sp_image_update. About to call colorprofile_get_handle()" );
+#endif // DEBUG_LCMS
+                                        guint profIntent = Inkscape::RENDERING_INTENT_UNKNOWN;
+                                        cmsHPROFILE prof = Inkscape::colorprofile_get_handle( SP_OBJECT_DOCUMENT( object ),
+                                                                                              &profIntent,
+                                                                                              image->color_profile );
+                                        if ( prof ) {
+                                            icProfileClassSignature profileClass = cmsGetDeviceClass( prof );
+                                            if ( profileClass != icSigNamedColorClass ) {
+                                                int intent = INTENT_PERCEPTUAL;
+                                                switch ( profIntent ) {
+                                                    case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
+                                                        intent = INTENT_RELATIVE_COLORIMETRIC;
+                                                        break;
+                                                    case Inkscape::RENDERING_INTENT_SATURATION:
+                                                        intent = INTENT_SATURATION;
+                                                        break;
+                                                    case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
+                                                        intent = INTENT_ABSOLUTE_COLORIMETRIC;
+                                                        break;
+                                                    case Inkscape::RENDERING_INTENT_PERCEPTUAL:
+                                                    case Inkscape::RENDERING_INTENT_UNKNOWN:
+                                                    case Inkscape::RENDERING_INTENT_AUTO:
+                                                    default:
+                                                        intent = INTENT_PERCEPTUAL;
+                                                }
+                                                cmsHPROFILE destProf = cmsCreate_sRGBProfile();
+                                                cmsHTRANSFORM transf = cmsCreateTransform( prof,
+                                                                                           TYPE_RGBA_8,
+                                                                                           destProf,
+                                                                                           TYPE_RGBA_8,
+                                                                                           intent, 0 );
+                                                if ( transf ) {
+                                                    guchar* currLine = px;
+                                                    for ( int y = 0; y < imageheight; y++ ) {
+                                                        // Since the types are the same size, we can do the transformation in-place
+                                                        cmsDoTransform( transf, currLine, currLine, imagewidth );
+                                                        currLine += rowstride;
+                                                    }
+
+                                                    cmsDeleteTransform( transf );
+                                                }
+#ifdef DEBUG_LCMS
+                                                else
+                                                {
+                                                    DEBUG_MESSAGE( lcmsSix, "in <image>'s sp_image_update. Unable to create LCMS transform." );
+                                                }
+#endif // DEBUG_LCMS
+                                                cmsCloseProfile( destProf );
+                                            }
+#ifdef DEBUG_LCMS
+                                            else
+                                            {
+                                                DEBUG_MESSAGE( lcmsSeven, "in <image>'s sp_image_update. Profile type is named color. Can't transform." );
+                                            }
+#endif // DEBUG_LCMS
+                                        }
+#ifdef DEBUG_LCMS
+                                        else
+                                        {
+                                            DEBUG_MESSAGE( lcmsEight, "in <image>'s sp_image_update. No profile found." );
+                                        }
+#endif // DEBUG_LCMS
+                                    }
+                                }
+#endif // ENABLE_LCMS
                                image->pixbuf = pixbuf;
                        }
                }
@@ -729,7 +945,6 @@ sp_image_update (SPObject *object, SPCtx *ctx, unsigned int flags)
                                }
                        }
        }
-
        sp_image_update_canvas_image ((SPImage *) object);
 }
 
@@ -741,7 +956,8 @@ sp_image_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
        image = SP_IMAGE (object);
 
        if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-               repr = sp_repr_new ("svg:image");
+                Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+               repr = xml_doc->createElement("svg:image");
        }
 
        repr->setAttribute("xlink:href", image->href);
@@ -751,6 +967,9 @@ sp_image_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
        if (image->width._set) sp_repr_set_svg_double(repr, "width", image->width.computed);
        if (image->height._set) sp_repr_set_svg_double(repr, "height", image->height.computed);
        repr->setAttribute("preserveAspectRatio", object->repr->attribute("preserveAspectRatio"));
+#if ENABLE_LCMS
+        if (image->color_profile) repr->setAttribute("color-profile", image->color_profile);
+#endif // ENABLE_LCMS
 
        if (((SPObjectClass *) (parent_class))->write)
                ((SPObjectClass *) (parent_class))->write (object, repr, flags);
@@ -759,7 +978,7 @@ sp_image_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
 }
 
 static void
-sp_image_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
+sp_image_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
 {
        SPImage const &image = *SP_IMAGE(item);
 
@@ -780,7 +999,6 @@ static void
 sp_image_print (SPItem *item, SPPrintContext *ctx)
 {
        SPImage *image;
-       NRMatrix tp, ti, s, t;
        guchar *px;
        int w, h, rs, pixskip;
 
@@ -795,20 +1013,22 @@ sp_image_print (SPItem *item, SPPrintContext *ctx)
        rs = gdk_pixbuf_get_rowstride (image->pixbuf);
        pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
 
+    NR::Matrix t;
        if (image->aspect_align == SP_ASPECT_NONE) {
                /* fixme: (Lauris) */
-               nr_matrix_set_translate (&tp, image->x.computed, image->y.computed);
-               nr_matrix_set_scale (&s, image->width.computed, -image->height.computed);
-               nr_matrix_set_translate (&ti, 0.0, -1.0);
+        NR::translate tp = NR::translate(image->x.computed, image->y.computed);
+        NR::scale s = NR::scale(image->width.computed, -image->height.computed);
+        NR::translate ti = NR::translate(0.0, -1.0);
+           t = s * tp;
+           t = ti * t;
        } else { // preserveAspectRatio
-               nr_matrix_set_translate (&tp, image->viewx, image->viewy);
-               nr_matrix_set_scale (&s, image->viewwidth, -image->viewheight);
-               nr_matrix_set_translate (&ti, 0.0, -1.0);
+        NR::translate tp = NR::translate(image->viewx, image->viewy);
+        NR::scale s = NR::scale(image->viewwidth, -image->viewheight);
+        NR::translate ti = NR::translate(0.0, -1.0);
+           t = s * tp;
+           t = ti * t;
        }
 
-       nr_matrix_multiply (&t, &s, &tp);
-       nr_matrix_multiply (&t, &ti, &t);
-
        if (image->aspect_align == SP_ASPECT_NONE)
                sp_print_image_R8G8B8A8_N (ctx, px, w, h, rs, &t, SP_OBJECT_STYLE (item));
        else // preserveAspectRatio
@@ -826,7 +1046,7 @@ sp_image_description(SPItem *item)
                 : xml_quote_strdup(image->href);
         } else {
             g_warning("Attempting to call strncmp() with a null pointer.");
-            href_desc = g_strdup(_("(null_pointer)")); // we call g_free() on href_desc
+            href_desc = g_strdup("(null_pointer)"); // we call g_free() on href_desc
         }
 
        char *ret = ( image->pixbuf == NULL
@@ -840,7 +1060,7 @@ sp_image_description(SPItem *item)
 }
 
 static NRArenaItem *
-sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
+sp_image_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
 {
        int pixskip, rs;
        SPImage * image;
@@ -853,6 +1073,7 @@ sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flag
        if (image->pixbuf) {
                pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
                rs = gdk_pixbuf_get_rowstride (image->pixbuf);
+                nr_arena_image_set_style(NR_ARENA_IMAGE(ai), SP_OBJECT_STYLE(SP_OBJECT(item)));
                if (image->aspect_align == SP_ASPECT_NONE)
                        nr_arena_image_set_pixels (NR_ARENA_IMAGE (ai),
                                           gdk_pixbuf_get_pixels (image->pixbuf),
@@ -885,55 +1106,72 @@ sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flag
  */
 
 GdkPixbuf *
-sp_image_repr_read_image (Inkscape::XML::Node * repr)
+sp_image_repr_read_image (const gchar *href, const gchar *absref, const gchar *base)
 {
-       const gchar * filename, * docbase;
-       gchar * fullname;
-       GdkPixbuf * pixbuf;
-
-       filename = repr->attribute("xlink:href");
-       if (filename == NULL) filename = repr->attribute("href"); /* FIXME */
-       if (filename != NULL) {
-               if (strncmp (filename,"file:",5) == 0) {
-                       fullname = g_filename_from_uri(filename, NULL, NULL);
-                       if (fullname) {
-                               // TODO check this. Was doing a UTF-8 to filename conversion here.
-                               pixbuf = Inkscape::IO::pixbuf_new_from_file (fullname, NULL);
-                               if (pixbuf != NULL) return pixbuf;
-                       }
-               } else if (strncmp (filename,"data:",5) == 0) {
-                       /* data URI - embedded image */
-                       filename += 5;
-                       pixbuf = sp_image_repr_read_dataURI (filename);
-                       if (pixbuf != NULL) return pixbuf;
-               } else if (!g_path_is_absolute (filename)) {
-                       /* try to load from relative pos */
-                       docbase = sp_repr_document_root (sp_repr_document (repr))->attribute("sodipodi:docbase");
-                       if (!docbase) docbase = ".";
-                       fullname = g_build_filename(docbase, filename, NULL);
-                       pixbuf = Inkscape::IO::pixbuf_new_from_file( fullname, NULL );
-                       g_free (fullname);
-                       if (pixbuf != NULL) return pixbuf;
-               } else {
-                       /* try absolute filename */
-                       pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
-                       if (pixbuf != NULL) return pixbuf;
-               }
-       }
-       /* at last try to load from sp absolute path name */
-       filename = repr->attribute("sodipodi:absref");
-       if (filename != NULL) {
-               pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
-               if (pixbuf != NULL) return pixbuf;
-       }
-       /* Nope: We do not find any valid pixmap file :-( */
-       pixbuf = gdk_pixbuf_new_from_xpm_data ((const gchar **) brokenimage_xpm);
+    const gchar *filename, *docbase;
+    gchar *fullname;
+    GdkPixbuf *pixbuf;
+
+    filename = href;
+    if (filename != NULL) {
+        if (strncmp (filename,"file:",5) == 0) {
+            fullname = g_filename_from_uri(filename, NULL, NULL);
+            if (fullname) {
+                // TODO check this. Was doing a UTF-8 to filename conversion here.
+                pixbuf = Inkscape::IO::pixbuf_new_from_file (fullname, NULL);
+                if (pixbuf != NULL) return pixbuf;
+            }
+        } else if (strncmp (filename,"data:",5) == 0) {
+            /* data URI - embedded image */
+            filename += 5;
+            pixbuf = sp_image_repr_read_dataURI (filename);
+            if (pixbuf != NULL) return pixbuf;
+        } else {
+
+            if (!g_path_is_absolute (filename)) {
+                /* try to load from relative pos combined with document base*/
+                docbase = base;
+                if (!docbase) docbase = ".";
+                fullname = g_build_filename(docbase, filename, NULL);
+
+                // document base can be wrong (on the temporary doc when importing bitmap from a
+                // different dir) or unset (when doc is not saved yet), so we check for base+href existence first,
+                // and if it fails, we also try to use bare href regardless of its g_path_is_absolute
+                if (g_file_test (fullname, G_FILE_TEST_EXISTS) && !g_file_test (fullname, G_FILE_TEST_IS_DIR)) {
+                    pixbuf = Inkscape::IO::pixbuf_new_from_file( fullname, NULL );
+                    g_free (fullname);
+                    if (pixbuf != NULL) return pixbuf;
+                }
+            }
 
-       /* It should be included xpm, so if it still does not does load, */
-       /* our libraries are broken */
-       g_assert (pixbuf != NULL);
+            /* try filename as absolute */
+            if (g_file_test (filename, G_FILE_TEST_EXISTS) && !g_file_test (filename, G_FILE_TEST_IS_DIR)) {
+                pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
+                if (pixbuf != NULL) return pixbuf;
+            }
+        }
+    }
 
-       return pixbuf;
+    /* at last try to load from sp absolute path name */
+    filename = absref;
+    if (filename != NULL) {
+        // using absref is outside of SVG rules, so we must at least warn the user
+        if ( base != NULL && href != NULL )
+               g_warning ("<image xlink:href=\"%s\"> did not resolve to a valid image file (base dir is %s), now trying sodipodi:absref=\"%s\"", href, base, absref);
+               else
+                   g_warning ("xlink:href did not resolve to a valid image file, now trying sodipodi:absref=\"%s\"", absref);
+
+        pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
+        if (pixbuf != NULL) return pixbuf;
+    }
+    /* Nope: We do not find any valid pixmap file :-( */
+    pixbuf = gdk_pixbuf_new_from_xpm_data ((const gchar **) brokenimage_xpm);
+
+    /* It should be included xpm, so if it still does not does load, */
+    /* our libraries are broken */
+    g_assert (pixbuf != NULL);
+
+    return pixbuf;
 }
 
 static GdkPixbuf *
@@ -973,6 +1211,7 @@ sp_image_update_canvas_image (SPImage *image)
        for (v = item->display; v != NULL; v = v->next) {
                pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
                rs = gdk_pixbuf_get_rowstride (image->pixbuf);
+                nr_arena_image_set_style (NR_ARENA_IMAGE(v->arenaitem), SP_OBJECT_STYLE(SP_OBJECT(image)));
                if (image->aspect_align == SP_ASPECT_NONE) {
                        nr_arena_image_set_pixels (NR_ARENA_IMAGE (v->arenaitem),
                                           gdk_pixbuf_get_pixels (image->pixbuf),
@@ -997,9 +1236,30 @@ sp_image_update_canvas_image (SPImage *image)
 
 static void sp_image_snappoints(SPItem const *item, SnapPointsIter p)
 {
-     if (((SPItemClass *) parent_class)->snappoints) {
-         ((SPItemClass *) parent_class)->snappoints (item, p);
-     }
+    /* An image doesn't have any nodes to snap, but still we want to be able snap one image 
+    to another. Therefore we will create some snappoints at the corner, similar to a rect. If
+    the image is rotated, then the snappoints will rotate with it. Again, just like a rect.
+    */
+     
+    g_assert(item != NULL);
+    g_assert(SP_IS_IMAGE(item));
+
+    if (item->clip_ref->getObject()) {
+        //We are looking at a clipped image: do not return any snappoints, as these might be
+        //far far away from the visible part from the clipped image
+    } else {
+        // The image has not been clipped: return its corners, which might be rotated for example
+        SPImage &image = *SP_IMAGE(item);
+        double const x0 = image.x.computed;
+               double const y0 = image.y.computed;
+               double const x1 = x0 + image.width.computed;
+               double const y1 = y0 + image.height.computed;
+               NR::Matrix const i2d (sp_item_i2d_affine (item));
+               *p = NR::Point(x0, y0) * i2d;
+        *p = NR::Point(x0, y1) * i2d;
+        *p = NR::Point(x1, y1) * i2d;
+        *p = NR::Point(x1, y0) * i2d;
+    }
 }
 
 /*
@@ -1188,6 +1448,45 @@ sp_image_repr_read_b64 (const gchar * uri_data)
        return pixbuf;
 }
 
+static void
+sp_image_set_curve(SPImage *image) 
+{
+    //create a curve at the image's boundary for snapping
+    if ((image->height.computed < 1e-18) || (image->width.computed < 1e-18) || (image->clip_ref->getObject())) {
+        if (image->curve) {
+            image->curve = sp_curve_unref(image->curve);
+        }
+        return;
+    }
+    
+    NRRect rect;
+       sp_image_bbox(image, &rect, NR::identity(), 0);
+       NR::Maybe<NR::Rect> rect2 = rect.upgrade();
+       SPCurve *c = sp_curve_new_from_rect(rect2);
+        
+    if (image->curve) {
+        image->curve = sp_curve_unref(image->curve);
+    }
+    
+    if (c) {
+        image->curve = sp_curve_ref(c);
+    }
+    
+    sp_curve_unref(c);    
+}
+
+/**
+ * Return duplicate of curve (if any exists) or NULL if there is no curve
+ */
+SPCurve *
+sp_image_get_curve (SPImage *image)
+{
+       if (image->curve) {
+               return sp_curve_copy(image->curve);
+       }
+       return NULL;
+}
+
 /*
   Local Variables:
   mode:c++