1 #define __SP_IMAGE_C__
3 /*
4 * SVG <image> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Edward Flick (EAF)
9 *
10 * Copyright (C) 1999-2005 Authors
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19 #include <libnr/nr-matrix-fns.h>
21 //#define GDK_PIXBUF_ENABLE_BACKEND 1
22 //#include <gdk-pixbuf/gdk-pixbuf-io.h>
23 #include "display/nr-arena-image.h"
25 //Added for preserveAspectRatio support -- EAF
26 #include "enums.h"
27 #include "attributes.h"
29 #include "print.h"
30 #include "brokenimage.xpm"
31 #include "document.h"
32 #include "sp-image.h"
33 #include <glibmm/i18n.h>
34 #include "xml/quote.h"
35 #include <xml/repr.h>
37 #include "io/sys.h"
38 #include <png.h>
39 #if ENABLE_LCMS
40 #include "color-profile-fns.h"
41 #include "color-profile.h"
42 //#define DEBUG_LCMS
43 #ifdef DEBUG_LCMS
44 #include "prefs-utils.h"
45 #include <gtk/gtkmessagedialog.h>
46 #endif // DEBUG_LCMS
47 #endif // ENABLE_LCMS
48 /*
49 * SPImage
50 */
53 static void sp_image_class_init (SPImageClass * klass);
54 static void sp_image_init (SPImage * image);
56 static void sp_image_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
57 static void sp_image_release (SPObject * object);
58 static void sp_image_set (SPObject *object, unsigned int key, const gchar *value);
59 static void sp_image_update (SPObject *object, SPCtx *ctx, unsigned int flags);
60 static Inkscape::XML::Node *sp_image_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
62 static void sp_image_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
63 static void sp_image_print (SPItem * item, SPPrintContext *ctx);
64 static gchar * sp_image_description (SPItem * item);
65 static void sp_image_snappoints(SPItem const *item, SnapPointsIter p);
66 static NRArenaItem *sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
67 static NR::Matrix sp_image_set_transform (SPItem *item, NR::Matrix const &xform);
69 GdkPixbuf * sp_image_repr_read_image (Inkscape::XML::Node * repr);
70 static GdkPixbuf *sp_image_pixbuf_force_rgba (GdkPixbuf * pixbuf);
71 static void sp_image_update_canvas_image (SPImage *image);
72 static GdkPixbuf * sp_image_repr_read_dataURI (const gchar * uri_data);
73 static GdkPixbuf * sp_image_repr_read_b64 (const gchar * uri_data);
75 static SPItemClass *parent_class;
78 extern "C"
79 {
80 void user_read_data( png_structp png_ptr, png_bytep data, png_size_t length );
81 void user_write_data( png_structp png_ptr, png_bytep data, png_size_t length );
82 void user_flush_data( png_structp png_ptr );
84 }
87 #ifdef DEBUG_LCMS
88 extern guint update_in_progress;
89 #define DEBUG_MESSAGE(key, ...) \
90 {\
91 gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
92 gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
93 gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
94 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
95 if ( dump )\
96 {\
97 g_message( __VA_ARGS__ );\
98 \
99 }\
100 if ( dumpD )\
101 {\
102 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
103 GTK_DIALOG_DESTROY_WITH_PARENT, \
104 GTK_MESSAGE_INFO, \
105 GTK_BUTTONS_OK, \
106 __VA_ARGS__ \
107 );\
108 g_signal_connect_swapped(dialog, "response",\
109 G_CALLBACK(gtk_widget_destroy), \
110 dialog); \
111 gtk_widget_show_all( dialog );\
112 }\
113 }
114 #endif // DEBUG_LCMS
116 namespace Inkscape {
117 namespace IO {
119 class PushPull
120 {
121 public:
122 gboolean first;
123 FILE* fp;
124 guchar* scratch;
125 gsize size;
126 gsize used;
127 gsize offset;
128 GdkPixbufLoader *loader;
130 PushPull() : first(TRUE),
131 fp(0),
132 scratch(0),
133 size(0),
134 used(0),
135 offset(0),
136 loader(0) {};
138 gboolean readMore()
139 {
140 gboolean good = FALSE;
141 if ( offset )
142 {
143 g_memmove( scratch, scratch + offset, used - offset );
144 used -= offset;
145 offset = 0;
146 }
147 if ( used < size )
148 {
149 gsize space = size - used;
150 gsize got = fread( scratch + used, 1, space, fp );
151 if ( got )
152 {
153 if ( loader )
154 {
155 GError *err = NULL;
156 //g_message( " __read %d bytes", (int)got );
157 if ( !gdk_pixbuf_loader_write( loader, scratch + used, got, &err ) )
158 {
159 //g_message("_error writing pixbuf data");
160 }
161 }
163 used += got;
164 good = TRUE;
165 }
166 else
167 {
168 good = FALSE;
169 }
170 }
171 return good;
172 }
174 gsize available() const
175 {
176 return (used - offset);
177 }
179 gsize readOut( gpointer data, gsize length )
180 {
181 gsize giving = available();
182 if ( length < giving )
183 {
184 giving = length;
185 }
186 g_memmove( data, scratch + offset, giving );
187 offset += giving;
188 if ( offset >= used )
189 {
190 offset = 0;
191 used = 0;
192 }
193 return giving;
194 }
196 void clear()
197 {
198 offset = 0;
199 used = 0;
200 }
202 private:
203 PushPull& operator = (const PushPull& other);
204 PushPull(const PushPull& other);
205 };
207 void user_read_data( png_structp png_ptr, png_bytep data, png_size_t length )
208 {
209 // g_message( "user_read_data(%d)", length );
211 PushPull* youme = (PushPull*)png_get_io_ptr(png_ptr);
213 gsize filled = 0;
214 gboolean canRead = TRUE;
216 while ( filled < length && canRead )
217 {
218 gsize some = youme->readOut( data + filled, length - filled );
219 filled += some;
220 if ( filled < length )
221 {
222 canRead &= youme->readMore();
223 }
224 }
225 // g_message("things out");
226 }
228 void user_write_data( png_structp png_ptr, png_bytep data, png_size_t length )
229 {
230 //g_message( "user_write_data(%d)", length );
231 }
233 void user_flush_data( png_structp png_ptr )
234 {
235 //g_message( "user_flush_data" );
236 }
238 GdkPixbuf* pixbuf_new_from_file( const char *filename, GError **error )
239 {
240 GdkPixbuf* buf = NULL;
241 PushPull youme;
242 gint dpiX = 0;
243 gint dpiY = 0;
245 //buf = gdk_pixbuf_new_from_file( filename, error );
246 dump_fopen_call( filename, "pixbuf_new_from_file" );
247 FILE* fp = fopen_utf8name( filename, "r" );
248 if ( fp )
249 {
250 GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
251 if ( loader )
252 {
253 GError *err = NULL;
255 // short buffer
256 guchar scratch[1024];
257 gboolean latter = FALSE;
258 gboolean isPng = FALSE;
259 png_structp pngPtr = NULL;
260 png_infop infoPtr = NULL;
261 //png_infop endPtr = NULL;
263 youme.fp = fp;
264 youme.scratch = scratch;
265 youme.size = sizeof(scratch);
266 youme.used = 0;
267 youme.offset = 0;
268 youme.loader = loader;
270 while ( !feof(fp) )
271 {
272 if ( youme.readMore() )
273 {
274 if ( youme.first )
275 {
276 //g_message( "First data chunk" );
277 youme.first = FALSE;
278 isPng = !png_sig_cmp( scratch + youme.offset, 0, youme.available() );
279 //g_message( " png? %s", (isPng ? "Yes":"No") );
280 if ( isPng )
281 {
282 pngPtr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
283 NULL,//(png_voidp)user_error_ptr,
284 NULL,//user_error_fn,
285 NULL//user_warning_fn
286 );
287 if ( pngPtr )
288 {
289 infoPtr = png_create_info_struct( pngPtr );
290 //endPtr = png_create_info_struct( pngPtr );
292 png_set_read_fn( pngPtr, &youme, user_read_data );
293 //g_message( "In" );
295 //png_read_info( pngPtr, infoPtr );
296 png_read_png( pngPtr, infoPtr, PNG_TRANSFORM_IDENTITY, NULL );
298 //g_message("out");
300 //png_read_end(pngPtr, endPtr);
302 /*
303 if ( png_get_valid( pngPtr, infoPtr, PNG_INFO_pHYs ) )
304 {
305 g_message("pHYs chunk now valid" );
306 }
307 if ( png_get_valid( pngPtr, infoPtr, PNG_INFO_sCAL ) )
308 {
309 g_message("sCAL chunk now valid" );
310 }
311 */
313 png_uint_32 res_x = 0;
314 png_uint_32 res_y = 0;
315 int unit_type = 0;
316 if ( png_get_pHYs( pngPtr, infoPtr, &res_x, &res_y, &unit_type) )
317 {
318 // g_message( "pHYs yes (%d, %d) %d (%s)", (int)res_x, (int)res_y, unit_type,
319 // (unit_type == 1? "per meter" : "unknown")
320 // );
322 // g_message( " dpi: (%d, %d)",
323 // (int)(0.5 + ((double)res_x)/39.37),
324 // (int)(0.5 + ((double)res_y)/39.37) );
325 if ( unit_type == PNG_RESOLUTION_METER )
326 {
327 // TODO come up with a more accurate DPI setting
328 dpiX = (int)(0.5 + ((double)res_x)/39.37);
329 dpiY = (int)(0.5 + ((double)res_y)/39.37);
330 }
331 }
332 else
333 {
334 // g_message( "pHYs no" );
335 }
337 /*
338 double width = 0;
339 double height = 0;
340 int unit = 0;
341 if ( png_get_sCAL(pngPtr, infoPtr, &unit, &width, &height) )
342 {
343 gchar* vals[] = {
344 "unknown", // PNG_SCALE_UNKNOWN
345 "meter", // PNG_SCALE_METER
346 "radian", // PNG_SCALE_RADIAN
347 "last", //
348 NULL
349 };
351 g_message( "sCAL: (%f, %f) %d (%s)",
352 width, height, unit,
353 ((unit >= 0 && unit < 3) ? vals[unit]:"???")
354 );
355 }
356 */
358 // now clean it up.
359 png_destroy_read_struct( &pngPtr, &infoPtr, NULL );//&endPtr );
360 }
361 else
362 {
363 g_message("Error when creating PNG read struct");
364 }
365 }
366 }
367 else if ( !latter )
368 {
369 latter = TRUE;
370 //g_message(" READing latter");
371 }
372 // Now clear out the buffer so we can read more.
373 // (dumping out unused)
374 youme.clear();
375 }
376 }
378 gboolean ok = gdk_pixbuf_loader_close(loader, &err);
379 if ( ok )
380 {
381 buf = gdk_pixbuf_loader_get_pixbuf( loader );
382 if ( buf )
383 {
384 g_object_ref(buf);
386 if ( dpiX )
387 {
388 gchar *tmp = g_strdup_printf( "%d", dpiX );
389 if ( tmp )
390 {
391 //gdk_pixbuf_set_option( buf, "Inkscape::DpiX", tmp );
392 g_free( tmp );
393 }
394 }
395 if ( dpiY )
396 {
397 gchar *tmp = g_strdup_printf( "%d", dpiY );
398 if ( tmp )
399 {
400 //gdk_pixbuf_set_option( buf, "Inkscape::DpiY", tmp );
401 g_free( tmp );
402 }
403 }
404 }
405 }
406 else
407 {
408 // do something
409 g_message("error loading pixbuf at close");
410 }
412 g_object_unref(loader);
413 }
414 else
415 {
416 g_message("error when creating pixbuf loader");
417 }
418 fclose( fp );
419 fp = NULL;
420 }
421 else
422 {
423 g_warning ("Unable to open linked file: %s", filename);
424 }
426 /*
427 if ( buf )
428 {
429 const gchar* bloop = gdk_pixbuf_get_option( buf, "Inkscape::DpiX" );
430 if ( bloop )
431 {
432 g_message("DPI X is [%s]", bloop);
433 }
434 bloop = gdk_pixbuf_get_option( buf, "Inkscape::DpiY" );
435 if ( bloop )
436 {
437 g_message("DPI Y is [%s]", bloop);
438 }
439 }
440 */
442 return buf;
443 }
445 }
446 }
448 GType
449 sp_image_get_type (void)
450 {
451 static GType image_type = 0;
452 if (!image_type) {
453 GTypeInfo image_info = {
454 sizeof (SPImageClass),
455 NULL, /* base_init */
456 NULL, /* base_finalize */
457 (GClassInitFunc) sp_image_class_init,
458 NULL, /* class_finalize */
459 NULL, /* class_data */
460 sizeof (SPImage),
461 16, /* n_preallocs */
462 (GInstanceInitFunc) sp_image_init,
463 NULL, /* value_table */
464 };
465 image_type = g_type_register_static (sp_item_get_type (), "SPImage", &image_info, (GTypeFlags)0);
466 }
467 return image_type;
468 }
470 static void
471 sp_image_class_init (SPImageClass * klass)
472 {
473 GObjectClass * gobject_class;
474 SPObjectClass * sp_object_class;
475 SPItemClass * item_class;
477 gobject_class = (GObjectClass *) klass;
478 sp_object_class = (SPObjectClass *) klass;
479 item_class = (SPItemClass *) klass;
481 parent_class = (SPItemClass*)g_type_class_ref (sp_item_get_type ());
483 sp_object_class->build = sp_image_build;
484 sp_object_class->release = sp_image_release;
485 sp_object_class->set = sp_image_set;
486 sp_object_class->update = sp_image_update;
487 sp_object_class->write = sp_image_write;
489 item_class->bbox = sp_image_bbox;
490 item_class->print = sp_image_print;
491 item_class->description = sp_image_description;
492 item_class->show = sp_image_show;
493 item_class->snappoints = sp_image_snappoints;
494 item_class->set_transform = sp_image_set_transform;
495 }
497 static void
498 sp_image_init (SPImage *image)
499 {
500 image->x.unset();
501 image->y.unset();
502 image->width.unset();
503 image->height.unset();
504 image->aspect_align = SP_ASPECT_NONE;
505 }
507 static void
508 sp_image_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
509 {
510 if (((SPObjectClass *) parent_class)->build)
511 ((SPObjectClass *) parent_class)->build (object, document, repr);
513 sp_object_read_attr (object, "xlink:href");
514 sp_object_read_attr (object, "x");
515 sp_object_read_attr (object, "y");
516 sp_object_read_attr (object, "width");
517 sp_object_read_attr (object, "height");
518 sp_object_read_attr (object, "preserveAspectRatio");
519 sp_object_read_attr (object, "color-profile");
521 /* Register */
522 sp_document_add_resource (document, "image", object);
523 }
525 static void
526 sp_image_release (SPObject *object)
527 {
528 SPImage *image;
530 image = SP_IMAGE (object);
532 if (SP_OBJECT_DOCUMENT (object)) {
533 /* Unregister ourselves */
534 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "image", SP_OBJECT (object));
535 }
537 if (image->href) {
538 g_free (image->href);
539 image->href = NULL;
540 }
542 if (image->pixbuf) {
543 gdk_pixbuf_unref (image->pixbuf);
544 image->pixbuf = NULL;
545 }
547 #if ENABLE_LCMS
548 if (image->color_profile) {
549 g_free (image->color_profile);
550 image->color_profile = NULL;
551 }
552 #endif // ENABLE_LCMS
554 if (((SPObjectClass *) parent_class)->release)
555 ((SPObjectClass *) parent_class)->release (object);
556 }
558 static void
559 sp_image_set (SPObject *object, unsigned int key, const gchar *value)
560 {
561 SPImage *image;
563 image = SP_IMAGE (object);
565 switch (key) {
566 case SP_ATTR_XLINK_HREF:
567 g_free (image->href);
568 image->href = (value) ? g_strdup (value) : NULL;
569 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_IMAGE_HREF_MODIFIED_FLAG);
570 break;
571 case SP_ATTR_X:
572 if (!image->x.readAbsolute(value)) {
573 /* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
574 image->x.unset();
575 }
576 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
577 break;
578 case SP_ATTR_Y:
579 if (!image->y.readAbsolute(value)) {
580 /* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
581 image->y.unset();
582 }
583 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
584 break;
585 case SP_ATTR_WIDTH:
586 if (!image->width.readAbsolute(value)) {
587 /* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
588 image->width.unset();
589 }
590 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
591 break;
592 case SP_ATTR_HEIGHT:
593 if (!image->height.readAbsolute(value)) {
594 /* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
595 image->height.unset();
596 }
597 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
598 break;
599 case SP_ATTR_PRESERVEASPECTRATIO:
600 /* Do setup before, so we can use break to escape */
601 image->aspect_align = SP_ASPECT_NONE;
602 image->aspect_clip = SP_ASPECT_MEET;
603 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
604 if (value) {
605 int len;
606 gchar c[256];
607 const gchar *p, *e;
608 unsigned int align, clip;
609 p = value;
610 while (*p && *p == 32) p += 1;
611 if (!*p) break;
612 e = p;
613 while (*e && *e != 32) e += 1;
614 len = e - p;
615 if (len > 8) break;
616 memcpy (c, value, len);
617 c[len] = 0;
618 /* Now the actual part */
619 if (!strcmp (c, "none")) {
620 align = SP_ASPECT_NONE;
621 } else if (!strcmp (c, "xMinYMin")) {
622 align = SP_ASPECT_XMIN_YMIN;
623 } else if (!strcmp (c, "xMidYMin")) {
624 align = SP_ASPECT_XMID_YMIN;
625 } else if (!strcmp (c, "xMaxYMin")) {
626 align = SP_ASPECT_XMAX_YMIN;
627 } else if (!strcmp (c, "xMinYMid")) {
628 align = SP_ASPECT_XMIN_YMID;
629 } else if (!strcmp (c, "xMidYMid")) {
630 align = SP_ASPECT_XMID_YMID;
631 } else if (!strcmp (c, "xMaxYMid")) {
632 align = SP_ASPECT_XMAX_YMID;
633 } else if (!strcmp (c, "xMinYMax")) {
634 align = SP_ASPECT_XMIN_YMAX;
635 } else if (!strcmp (c, "xMidYMax")) {
636 align = SP_ASPECT_XMID_YMAX;
637 } else if (!strcmp (c, "xMaxYMax")) {
638 align = SP_ASPECT_XMAX_YMAX;
639 } else {
640 break;
641 }
642 clip = SP_ASPECT_MEET;
643 while (*e && *e == 32) e += 1;
644 if (e) {
645 if (!strcmp (e, "meet")) {
646 clip = SP_ASPECT_MEET;
647 } else if (!strcmp (e, "slice")) {
648 clip = SP_ASPECT_SLICE;
649 } else {
650 break;
651 }
652 }
653 image->aspect_align = align;
654 image->aspect_clip = clip;
655 }
656 break;
657 #if ENABLE_LCMS
658 case SP_PROP_COLOR_PROFILE:
659 if ( image->color_profile ) {
660 g_free (image->color_profile);
661 }
662 image->color_profile = (value) ? g_strdup (value) : NULL;
663 #ifdef DEBUG_LCMS
664 if ( value ) {
665 DEBUG_MESSAGE( lcmsFour, "<image> color-profile set to '%s'", value );
666 } else {
667 DEBUG_MESSAGE( lcmsFour, "<image> color-profile cleared" );
668 }
669 #endif // DEBUG_LCMS
670 // TODO check on this HREF_MODIFIED flag
671 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_IMAGE_HREF_MODIFIED_FLAG);
672 break;
673 #endif // ENABLE_LCMS
674 default:
675 if (((SPObjectClass *) (parent_class))->set)
676 ((SPObjectClass *) (parent_class))->set (object, key, value);
677 break;
678 }
679 }
681 static void
682 sp_image_update (SPObject *object, SPCtx *ctx, unsigned int flags)
683 {
684 SPImage *image;
686 image = (SPImage *) object;
688 if (((SPObjectClass *) (parent_class))->update)
689 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
691 if (flags & SP_IMAGE_HREF_MODIFIED_FLAG) {
692 if (image->pixbuf) {
693 gdk_pixbuf_unref (image->pixbuf);
694 image->pixbuf = NULL;
695 }
696 if (image->href) {
697 GdkPixbuf *pixbuf;
698 pixbuf = sp_image_repr_read_image (object->repr);
699 if (pixbuf) {
700 pixbuf = sp_image_pixbuf_force_rgba (pixbuf);
701 // BLIP
702 #if ENABLE_LCMS
703 if ( image->color_profile )
704 {
705 int imagewidth = gdk_pixbuf_get_width( pixbuf );
706 int imageheight = gdk_pixbuf_get_height( pixbuf );
707 int rowstride = gdk_pixbuf_get_rowstride( pixbuf );
708 guchar* px = gdk_pixbuf_get_pixels( pixbuf );
710 if ( px ) {
711 #ifdef DEBUG_LCMS
712 DEBUG_MESSAGE( lcmsFive, "in <image>'s sp_image_update. About to call colorprofile_get_handle()" );
713 #endif // DEBUG_LCMS
714 guint profIntent = Inkscape::RENDERING_INTENT_UNKNOWN;
715 cmsHPROFILE prof = Inkscape::colorprofile_get_handle( SP_OBJECT_DOCUMENT( object ),
716 &profIntent,
717 image->color_profile );
718 if ( prof ) {
719 icProfileClassSignature profileClass = cmsGetDeviceClass( prof );
720 if ( profileClass != icSigNamedColorClass ) {
721 int intent = INTENT_PERCEPTUAL;
722 switch ( profIntent ) {
723 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
724 intent = INTENT_RELATIVE_COLORIMETRIC;
725 break;
726 case Inkscape::RENDERING_INTENT_SATURATION:
727 intent = INTENT_SATURATION;
728 break;
729 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
730 intent = INTENT_ABSOLUTE_COLORIMETRIC;
731 break;
732 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
733 case Inkscape::RENDERING_INTENT_UNKNOWN:
734 case Inkscape::RENDERING_INTENT_AUTO:
735 default:
736 intent = INTENT_PERCEPTUAL;
737 }
738 cmsHPROFILE destProf = cmsCreate_sRGBProfile();
739 cmsHTRANSFORM transf = cmsCreateTransform( prof,
740 TYPE_RGBA_8,
741 destProf,
742 TYPE_RGBA_8,
743 intent, 0 );
744 if ( transf ) {
745 guchar* currLine = px;
746 for ( int y = 0; y < imageheight; y++ ) {
747 // Since the types are the same size, we can do the transformation in-place
748 cmsDoTransform( transf, currLine, currLine, imagewidth );
749 currLine += rowstride;
750 }
752 cmsDeleteTransform( transf );
753 }
754 #ifdef DEBUG_LCMS
755 else
756 {
757 DEBUG_MESSAGE( lcmsSix, "in <image>'s sp_image_update. Unable to create LCMS transform." );
758 }
759 #endif // DEBUG_LCMS
760 cmsCloseProfile( destProf );
761 }
762 #ifdef DEBUG_LCMS
763 else
764 {
765 DEBUG_MESSAGE( lcmsSeven, "in <image>'s sp_image_update. Profile type is named color. Can't transform." );
766 }
767 #endif // DEBUG_LCMS
768 }
769 #ifdef DEBUG_LCMS
770 else
771 {
772 DEBUG_MESSAGE( lcmsEight, "in <image>'s sp_image_update. No profile found." );
773 }
774 #endif // DEBUG_LCMS
775 }
776 }
777 #endif // ENABLE_LCMS
778 image->pixbuf = pixbuf;
779 }
780 }
781 }
782 // preserveAspectRatio calculate bounds / clipping rectangle -- EAF
783 if (image->pixbuf && (image->aspect_align != SP_ASPECT_NONE)) {
784 int imagewidth, imageheight;
785 double x,y;
787 imagewidth = gdk_pixbuf_get_width (image->pixbuf);
788 imageheight = gdk_pixbuf_get_height (image->pixbuf);
790 switch (image->aspect_align) {
791 case SP_ASPECT_XMIN_YMIN:
792 x = 0.0;
793 y = 0.0;
794 break;
795 case SP_ASPECT_XMID_YMIN:
796 x = 0.5;
797 y = 0.0;
798 break;
799 case SP_ASPECT_XMAX_YMIN:
800 x = 1.0;
801 y = 0.0;
802 break;
803 case SP_ASPECT_XMIN_YMID:
804 x = 0.0;
805 y = 0.5;
806 break;
807 case SP_ASPECT_XMID_YMID:
808 x = 0.5;
809 y = 0.5;
810 break;
811 case SP_ASPECT_XMAX_YMID:
812 x = 1.0;
813 y = 0.5;
814 break;
815 case SP_ASPECT_XMIN_YMAX:
816 x = 0.0;
817 y = 1.0;
818 break;
819 case SP_ASPECT_XMID_YMAX:
820 x = 0.5;
821 y = 1.0;
822 break;
823 case SP_ASPECT_XMAX_YMAX:
824 x = 1.0;
825 y = 1.0;
826 break;
827 default:
828 x = 0.0;
829 y = 0.0;
830 break;
831 }
833 if (image->aspect_clip == SP_ASPECT_SLICE) {
834 image->viewx = image->x.computed;
835 image->viewy = image->y.computed;
836 image->viewwidth = image->width.computed;
837 image->viewheight = image->height.computed;
838 if ((imagewidth*image->height.computed)>(image->width.computed*imageheight)) {
839 // Pixels aspect is wider than bounding box
840 image->trimheight = imageheight;
841 image->trimwidth = static_cast<int>(static_cast<double>(imageheight) * image->width.computed / image->height.computed);
842 image->trimy = 0;
843 image->trimx = static_cast<int>(static_cast<double>(imagewidth - image->trimwidth) * x);
844 } else {
845 // Pixels aspect is taller than bounding box
846 image->trimwidth = imagewidth;
847 image->trimheight = static_cast<int>(static_cast<double>(imagewidth) * image->height.computed / image->width.computed);
848 image->trimx = 0;
849 image->trimy = static_cast<int>(static_cast<double>(imageheight - image->trimheight) * y);
850 }
851 } else {
852 // Otherwise, assume SP_ASPECT_MEET
853 image->trimx = 0;
854 image->trimy = 0;
855 image->trimwidth = imagewidth;
856 image->trimheight = imageheight;
857 if ((imagewidth*image->height.computed)>(image->width.computed*imageheight)) {
858 // Pixels aspect is wider than bounding boz
859 image->viewwidth = image->width.computed;
860 image->viewheight = image->viewwidth * imageheight / imagewidth;
861 image->viewx=image->x.computed;
862 image->viewy=(image->height.computed - image->viewheight) * y + image->y.computed;
863 } else {
864 // Pixels aspect is taller than bounding box
865 image->viewheight = image->height.computed;
866 image->viewwidth = image->viewheight * imagewidth / imageheight;
867 image->viewy=image->y.computed;
868 image->viewx=(image->width.computed - image->viewwidth) * x + image->x.computed;
869 }
870 }
871 }
873 sp_image_update_canvas_image ((SPImage *) object);
874 }
876 static Inkscape::XML::Node *
877 sp_image_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
878 {
879 SPImage *image;
881 image = SP_IMAGE (object);
883 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
884 repr = sp_repr_new ("svg:image");
885 }
887 repr->setAttribute("xlink:href", image->href);
888 /* fixme: Reset attribute if needed (Lauris) */
889 if (image->x._set) sp_repr_set_svg_double(repr, "x", image->x.computed);
890 if (image->y._set) sp_repr_set_svg_double(repr, "y", image->y.computed);
891 if (image->width._set) sp_repr_set_svg_double(repr, "width", image->width.computed);
892 if (image->height._set) sp_repr_set_svg_double(repr, "height", image->height.computed);
893 repr->setAttribute("preserveAspectRatio", object->repr->attribute("preserveAspectRatio"));
894 #if ENABLE_LCMS
895 if (image->color_profile) repr->setAttribute("color-profile", image->color_profile);
896 #endif // ENABLE_LCMS
898 if (((SPObjectClass *) (parent_class))->write)
899 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
901 return repr;
902 }
904 static void
905 sp_image_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
906 {
907 SPImage const &image = *SP_IMAGE(item);
909 if ((image.width.computed > 0.0) && (image.height.computed > 0.0)) {
910 double const x0 = image.x.computed;
911 double const y0 = image.y.computed;
912 double const x1 = x0 + image.width.computed;
913 double const y1 = y0 + image.height.computed;
915 nr_rect_union_pt(bbox, NR::Point(x0, y0) * transform);
916 nr_rect_union_pt(bbox, NR::Point(x1, y0) * transform);
917 nr_rect_union_pt(bbox, NR::Point(x1, y1) * transform);
918 nr_rect_union_pt(bbox, NR::Point(x0, y1) * transform);
919 }
920 }
922 static void
923 sp_image_print (SPItem *item, SPPrintContext *ctx)
924 {
925 SPImage *image;
926 NRMatrix tp, ti, s, t;
927 guchar *px;
928 int w, h, rs, pixskip;
930 image = SP_IMAGE (item);
932 if (!image->pixbuf) return;
933 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
935 px = gdk_pixbuf_get_pixels (image->pixbuf);
936 w = gdk_pixbuf_get_width (image->pixbuf);
937 h = gdk_pixbuf_get_height (image->pixbuf);
938 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
939 pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
941 if (image->aspect_align == SP_ASPECT_NONE) {
942 /* fixme: (Lauris) */
943 nr_matrix_set_translate (&tp, image->x.computed, image->y.computed);
944 nr_matrix_set_scale (&s, image->width.computed, -image->height.computed);
945 nr_matrix_set_translate (&ti, 0.0, -1.0);
946 } else { // preserveAspectRatio
947 nr_matrix_set_translate (&tp, image->viewx, image->viewy);
948 nr_matrix_set_scale (&s, image->viewwidth, -image->viewheight);
949 nr_matrix_set_translate (&ti, 0.0, -1.0);
950 }
952 nr_matrix_multiply (&t, &s, &tp);
953 nr_matrix_multiply (&t, &ti, &t);
955 if (image->aspect_align == SP_ASPECT_NONE)
956 sp_print_image_R8G8B8A8_N (ctx, px, w, h, rs, &t, SP_OBJECT_STYLE (item));
957 else // preserveAspectRatio
958 sp_print_image_R8G8B8A8_N (ctx, px + image->trimx*pixskip + image->trimy*rs, image->trimwidth, image->trimheight, rs, &t, SP_OBJECT_STYLE (item));
959 }
961 static gchar *
962 sp_image_description(SPItem *item)
963 {
964 SPImage *image = SP_IMAGE(item);
965 char *href_desc;
966 if (image->href) {
967 href_desc = (strncmp(image->href, "data:", 5) == 0)
968 ? g_strdup(_("embedded"))
969 : xml_quote_strdup(image->href);
970 } else {
971 g_warning("Attempting to call strncmp() with a null pointer.");
972 href_desc = g_strdup(_("(null_pointer)")); // we call g_free() on href_desc
973 }
975 char *ret = ( image->pixbuf == NULL
976 ? g_strdup_printf(_("<b>Image with bad reference</b>: %s"), href_desc)
977 : g_strdup_printf(_("<b>Image</b> %d × %d: %s"),
978 gdk_pixbuf_get_width(image->pixbuf),
979 gdk_pixbuf_get_height(image->pixbuf),
980 href_desc) );
981 g_free(href_desc);
982 return ret;
983 }
985 static NRArenaItem *
986 sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
987 {
988 int pixskip, rs;
989 SPImage * image;
990 NRArenaItem *ai;
992 image = (SPImage *) item;
994 ai = NRArenaImage::create(arena);
996 if (image->pixbuf) {
997 pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
998 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
999 nr_arena_image_set_style(NR_ARENA_IMAGE(ai), SP_OBJECT_STYLE(SP_OBJECT(item)));
1000 if (image->aspect_align == SP_ASPECT_NONE)
1001 nr_arena_image_set_pixels (NR_ARENA_IMAGE (ai),
1002 gdk_pixbuf_get_pixels (image->pixbuf),
1003 gdk_pixbuf_get_width (image->pixbuf),
1004 gdk_pixbuf_get_height (image->pixbuf),
1005 rs);
1006 else // preserveAspectRatio
1007 nr_arena_image_set_pixels (NR_ARENA_IMAGE (ai),
1008 gdk_pixbuf_get_pixels (image->pixbuf) + image->trimx*pixskip + image->trimy*rs,
1009 image->trimwidth,
1010 image->trimheight,
1011 rs);
1012 } else {
1013 nr_arena_image_set_pixels (NR_ARENA_IMAGE (ai), NULL, 0, 0, 0);
1014 }
1015 if (image->aspect_align == SP_ASPECT_NONE)
1016 nr_arena_image_set_geometry (NR_ARENA_IMAGE (ai), image->x.computed, image->y.computed, image->width.computed, image->height.computed);
1017 else // preserveAspectRatio
1018 nr_arena_image_set_geometry (NR_ARENA_IMAGE (ai), image->viewx, image->viewy, image->viewwidth, image->viewheight);
1020 return ai;
1021 }
1023 /*
1024 * utility function to try loading image from href
1025 *
1026 * docbase/relative_src
1027 * absolute_src
1028 *
1029 */
1031 GdkPixbuf *
1032 sp_image_repr_read_image (Inkscape::XML::Node * repr)
1033 {
1034 const gchar * filename, * docbase;
1035 gchar * fullname;
1036 GdkPixbuf * pixbuf;
1038 filename = repr->attribute("xlink:href");
1039 if (filename == NULL) filename = repr->attribute("href"); /* FIXME */
1040 if (filename != NULL) {
1041 if (strncmp (filename,"file:",5) == 0) {
1042 fullname = g_filename_from_uri(filename, NULL, NULL);
1043 if (fullname) {
1044 // TODO check this. Was doing a UTF-8 to filename conversion here.
1045 pixbuf = Inkscape::IO::pixbuf_new_from_file (fullname, NULL);
1046 if (pixbuf != NULL) return pixbuf;
1047 }
1048 } else if (strncmp (filename,"data:",5) == 0) {
1049 /* data URI - embedded image */
1050 filename += 5;
1051 pixbuf = sp_image_repr_read_dataURI (filename);
1052 if (pixbuf != NULL) return pixbuf;
1053 } else if (!g_path_is_absolute (filename)) {
1054 /* try to load from relative pos */
1055 docbase = sp_repr_document_root (sp_repr_document (repr))->attribute("sodipodi:docbase");
1056 if (!docbase) docbase = ".";
1057 fullname = g_build_filename(docbase, filename, NULL);
1058 pixbuf = Inkscape::IO::pixbuf_new_from_file( fullname, NULL );
1059 g_free (fullname);
1060 if (pixbuf != NULL) return pixbuf;
1061 } else {
1062 /* try absolute filename */
1063 pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
1064 if (pixbuf != NULL) return pixbuf;
1065 }
1066 }
1067 /* at last try to load from sp absolute path name */
1068 filename = repr->attribute("sodipodi:absref");
1069 if (filename != NULL) {
1070 pixbuf = Inkscape::IO::pixbuf_new_from_file( filename, NULL );
1071 if (pixbuf != NULL) return pixbuf;
1072 }
1073 /* Nope: We do not find any valid pixmap file :-( */
1074 pixbuf = gdk_pixbuf_new_from_xpm_data ((const gchar **) brokenimage_xpm);
1076 /* It should be included xpm, so if it still does not does load, */
1077 /* our libraries are broken */
1078 g_assert (pixbuf != NULL);
1080 return pixbuf;
1081 }
1083 static GdkPixbuf *
1084 sp_image_pixbuf_force_rgba (GdkPixbuf * pixbuf)
1085 {
1086 GdkPixbuf * newbuf;
1088 if (gdk_pixbuf_get_has_alpha (pixbuf)) return pixbuf;
1090 newbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0);
1091 gdk_pixbuf_unref (pixbuf);
1093 return newbuf;
1094 }
1096 /* We assert that realpixbuf is either NULL or identical size to pixbuf */
1098 static void
1099 sp_image_update_canvas_image (SPImage *image)
1100 {
1101 int rs, pixskip;
1102 SPItem *item;
1103 SPItemView *v;
1105 item = SP_ITEM (image);
1107 if (image->pixbuf) {
1108 /* fixme: We are slightly violating spec here (Lauris) */
1109 if (!image->width._set) {
1110 image->width.computed = gdk_pixbuf_get_width (image->pixbuf);
1111 }
1112 if (!image->height._set) {
1113 image->height.computed = gdk_pixbuf_get_height (image->pixbuf);
1114 }
1115 }
1117 for (v = item->display; v != NULL; v = v->next) {
1118 pixskip = gdk_pixbuf_get_n_channels (image->pixbuf) * gdk_pixbuf_get_bits_per_sample (image->pixbuf) / 8;
1119 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
1120 nr_arena_image_set_style (NR_ARENA_IMAGE(v->arenaitem), SP_OBJECT_STYLE(SP_OBJECT(image)));
1121 if (image->aspect_align == SP_ASPECT_NONE) {
1122 nr_arena_image_set_pixels (NR_ARENA_IMAGE (v->arenaitem),
1123 gdk_pixbuf_get_pixels (image->pixbuf),
1124 gdk_pixbuf_get_width (image->pixbuf),
1125 gdk_pixbuf_get_height (image->pixbuf),
1126 rs);
1127 nr_arena_image_set_geometry (NR_ARENA_IMAGE (v->arenaitem),
1128 image->x.computed, image->y.computed,
1129 image->width.computed, image->height.computed);
1130 } else { // preserveAspectRatio
1131 nr_arena_image_set_pixels (NR_ARENA_IMAGE (v->arenaitem),
1132 gdk_pixbuf_get_pixels (image->pixbuf) + image->trimx*pixskip + image->trimy*rs,
1133 image->trimwidth,
1134 image->trimheight,
1135 rs);
1136 nr_arena_image_set_geometry (NR_ARENA_IMAGE (v->arenaitem),
1137 image->viewx, image->viewy,
1138 image->viewwidth, image->viewheight);
1139 }
1140 }
1141 }
1143 static void sp_image_snappoints(SPItem const *item, SnapPointsIter p)
1144 {
1145 if (((SPItemClass *) parent_class)->snappoints) {
1146 ((SPItemClass *) parent_class)->snappoints (item, p);
1147 }
1148 }
1150 /*
1151 * Initially we'll do:
1152 * Transform x, y, set x, y, clear translation
1153 */
1155 static NR::Matrix
1156 sp_image_set_transform(SPItem *item, NR::Matrix const &xform)
1157 {
1158 SPImage *image = SP_IMAGE(item);
1160 /* Calculate position in parent coords. */
1161 NR::Point pos( NR::Point(image->x.computed, image->y.computed) * xform );
1163 /* This function takes care of translation and scaling, we return whatever parts we can't
1164 handle. */
1165 NR::Matrix ret(NR::transform(xform));
1166 NR::Point const scale(hypot(ret[0], ret[1]),
1167 hypot(ret[2], ret[3]));
1168 if ( scale[NR::X] > 1e-9 ) {
1169 ret[0] /= scale[NR::X];
1170 ret[1] /= scale[NR::X];
1171 } else {
1172 ret[0] = 1.0;
1173 ret[1] = 0.0;
1174 }
1175 if ( scale[NR::Y] > 1e-9 ) {
1176 ret[2] /= scale[NR::Y];
1177 ret[3] /= scale[NR::Y];
1178 } else {
1179 ret[2] = 0.0;
1180 ret[3] = 1.0;
1181 }
1183 image->width = image->width.computed * scale[NR::X];
1184 image->height = image->height.computed * scale[NR::Y];
1186 /* Find position in item coords */
1187 pos = pos * ret.inverse();
1188 image->x = pos[NR::X];
1189 image->y = pos[NR::Y];
1191 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1193 return ret;
1194 }
1196 static GdkPixbuf *
1197 sp_image_repr_read_dataURI (const gchar * uri_data)
1198 { GdkPixbuf * pixbuf = NULL;
1200 gint data_is_image = 0;
1201 gint data_is_base64 = 0;
1203 const gchar * data = uri_data;
1205 while (*data) {
1206 if (strncmp (data,"base64",6) == 0) {
1207 /* base64-encoding */
1208 data_is_base64 = 1;
1209 data_is_image = 1; // Illustrator produces embedded images without MIME type, so we assume it's image no matter what
1210 data += 6;
1211 }
1212 else if (strncmp (data,"image/png",9) == 0) {
1213 /* PNG image */
1214 data_is_image = 1;
1215 data += 9;
1216 }
1217 else if (strncmp (data,"image/jpg",9) == 0) {
1218 /* JPEG image */
1219 data_is_image = 1;
1220 data += 9;
1221 }
1222 else if (strncmp (data,"image/jpeg",10) == 0) {
1223 /* JPEG image */
1224 data_is_image = 1;
1225 data += 10;
1226 }
1227 else { /* unrecognized option; skip it */
1228 while (*data) {
1229 if (((*data) == ';') || ((*data) == ',')) break;
1230 data++;
1231 }
1232 }
1233 if ((*data) == ';') {
1234 data++;
1235 continue;
1236 }
1237 if ((*data) == ',') {
1238 data++;
1239 break;
1240 }
1241 }
1243 if ((*data) && data_is_image && data_is_base64) {
1244 pixbuf = sp_image_repr_read_b64 (data);
1245 }
1247 return pixbuf;
1248 }
1250 static GdkPixbuf *
1251 sp_image_repr_read_b64 (const gchar * uri_data)
1252 { GdkPixbuf * pixbuf = NULL;
1253 GdkPixbufLoader * loader = NULL;
1255 gint j;
1256 gint k;
1257 gint l;
1258 gint b;
1259 gint len;
1260 gint eos = 0;
1261 gint failed = 0;
1263 guint32 bits;
1265 static const gchar B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1267 const gchar* btr = uri_data;
1269 gchar ud[4];
1271 guchar bd[57];
1273 loader = gdk_pixbuf_loader_new ();
1275 if (loader == NULL) return NULL;
1277 while (eos == 0) {
1278 l = 0;
1279 for (j = 0; j < 19; j++) {
1280 len = 0;
1281 for (k = 0; k < 4; k++) {
1282 while (isspace ((int) (*btr))) {
1283 if ((*btr) == '\0') break;
1284 btr++;
1285 }
1286 if (eos) {
1287 ud[k] = 0;
1288 continue;
1289 }
1290 if (((*btr) == '\0') || ((*btr) == '=')) {
1291 eos = 1;
1292 ud[k] = 0;
1293 continue;
1294 }
1295 ud[k] = 64;
1296 for (b = 0; b < 64; b++) { /* There must a faster way to do this... ?? */
1297 if (B64[b] == (*btr)) {
1298 ud[k] = (gchar) b;
1299 break;
1300 }
1301 }
1302 if (ud[k] == 64) { /* data corruption ?? */
1303 eos = 1;
1304 ud[k] = 0;
1305 continue;
1306 }
1307 btr++;
1308 len++;
1309 }
1310 bits = (guint32) ud[0];
1311 bits = (bits << 6) | (guint32) ud[1];
1312 bits = (bits << 6) | (guint32) ud[2];
1313 bits = (bits << 6) | (guint32) ud[3];
1314 bd[l++] = (guchar) ((bits & 0xff0000) >> 16);
1315 if (len > 2) {
1316 bd[l++] = (guchar) ((bits & 0xff00) >> 8);
1317 }
1318 if (len > 3) {
1319 bd[l++] = (guchar) (bits & 0xff);
1320 }
1321 }
1323 if (!gdk_pixbuf_loader_write (loader, (const guchar *) bd, (size_t) l, NULL)) {
1324 failed = 1;
1325 break;
1326 }
1327 }
1329 gdk_pixbuf_loader_close (loader, NULL);
1331 if (!failed) pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1333 return pixbuf;
1334 }
1336 /*
1337 Local Variables:
1338 mode:c++
1339 c-file-style:"stroustrup"
1340 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1341 indent-tabs-mode:nil
1342 fill-column:99
1343 End:
1344 */
1345 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :