sp-image.cpp revision 11b2f376ecd9f48b2d862df617d252aa6c6a654f
/*
* SVG <image> implementation
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Edward Flick (EAF)
*
* Copyright (C) 1999-2005 Authors
* Copyright (C) 2000-2001 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
// This has to be included prior to anything that includes setjmp.h, it croaks otherwise
#include <png.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>
#include <libnr/nr-convert2geom.h>
//#define GDK_PIXBUF_ENABLE_BACKEND 1
//#include <gdk-pixbuf/gdk-pixbuf-io.h>
#include "display/nr-arena-image.h"
//Added for preserveAspectRatio support -- EAF
#include "enums.h"
#include "attributes.h"
#include "print.h"
#include "brokenimage.xpm"
#include "document.h"
#include "sp-image.h"
#include "sp-clippath.h"
#include "snap-candidate.h"
#include "libnr/nr-matrix-fns.h"
#if ENABLE_LCMS
#include "color-profile-fns.h"
#include "color-profile.h"
//#define DEBUG_LCMS
#ifdef DEBUG_LCMS
#define DEBUG_MESSAGE(key, ...)\
{\
g_message( __VA_ARGS__ );\
}
#include "preferences.h"
#include <gtk/gtkmessagedialog.h>
#endif // DEBUG_LCMS
#endif // ENABLE_LCMS
/*
* SPImage
*/
// TODO: give these constants better names:
#define MAGIC_EPSILON 1e-9
#define MAGIC_EPSILON_TOO 1e-18
// TODO: also check if it is correct to be using two different epsilon values
static Inkscape::XML::Node *sp_image_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
static void sp_image_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
static void sp_image_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
static NRArenaItem *sp_image_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
static GdkPixbuf *sp_image_repr_read_image( time_t& modTime, gchar*& pixPath, const gchar *href, const gchar *absref, const gchar *base );
static SPItemClass *parent_class;
extern "C"
{
}
#ifdef DEBUG_LCMS
extern guint update_in_progress;
#define DEBUG_MESSAGE_SCISLAC(key, ...) \
{\
if ( dump )\
{\
g_message( __VA_ARGS__ );\
\
}\
if ( dumpD )\
{\
);\
dialog); \
}\
}
#endif // DEBUG_LCMS
namespace Inkscape {
namespace IO {
class PushPull
{
public:
fp(0),
scratch(0),
size(0),
used(0),
offset(0),
loader(0) {};
{
if ( offset )
{
offset = 0;
}
{
if ( got )
{
if ( loader )
{
//g_message( " __read %d bytes", (int)got );
{
//g_message("_error writing pixbuf data");
}
}
}
else
{
}
}
return good;
}
{
}
{
{
}
{
offset = 0;
used = 0;
}
return giving;
}
void clear()
{
offset = 0;
used = 0;
}
private:
};
{
// g_message( "user_read_data(%d)", length );
{
{
}
}
// g_message("things out");
}
{
//g_message( "user_write_data(%d)", length );
}
{
//g_message( "user_flush_data" );
}
{
bool good = true;
//g_message( " png? %s", (isPng ? "Yes":"No") );
if ( isPng ) {
0, //(png_voidp)user_error_ptr,
0, //user_error_fn,
0 //user_warning_fn
);
// libpng calls longjmp to return here if an error occurs.
good = false;
}
if (good) {
//g_message( "In" );
//png_read_info( pngPtr, infoPtr );
//g_message("out");
/*
if ( png_get_valid( pngPtr, infoPtr, PNG_INFO_pHYs ) )
{
g_message("pHYs chunk now valid" );
}
if ( png_get_valid( pngPtr, infoPtr, PNG_INFO_sCAL ) )
{
g_message("sCAL chunk now valid" );
}
*/
png_uint_32 res_x = 0;
png_uint_32 res_y = 0;
int unit_type = 0;
// g_message( "pHYs yes (%d, %d) %d (%s)", (int)res_x, (int)res_y, unit_type,
// (unit_type == 1? "per meter" : "unknown")
// );
// g_message( " dpi: (%d, %d)",
// (int)(0.5 + ((double)res_x)/39.37),
// (int)(0.5 + ((double)res_y)/39.37) );
if ( unit_type == PNG_RESOLUTION_METER )
{
// TODO come up with a more accurate DPI setting
}
} else {
// g_message( "pHYs no" );
}
/*
double width = 0;
double height = 0;
int unit = 0;
if ( png_get_sCAL(pngPtr, infoPtr, &unit, &width, &height) )
{
gchar* vals[] = {
"unknown", // PNG_SCALE_UNKNOWN
"meter", // PNG_SCALE_METER
"radian", // PNG_SCALE_RADIAN
"last", //
NULL
};
g_message( "sCAL: (%f, %f) %d (%s)",
width, height, unit,
((unit >= 0 && unit < 3) ? vals[unit]:"???")
);
}
*/
#if defined(PNG_sRGB_SUPPORTED)
{
int intent = 0;
// 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;
// g_message("Found a cHRM png chunk");
}
}
#endif // defined(PNG_cHRM_SUPPORTED)
#if defined(PNG_gAMA_SUPPORTED)
{
double file_gamma = 0;
// 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;
// g_message("Found an iCCP chunk named [%s] with %d bytes and comp %d", name, proflen, compression_type);
}
}
#endif // defined(PNG_iCCP_SUPPORTED)
}
} else {
g_message("Error when creating PNG read struct");
}
// now clean it up.
pngPtr = 0;
infoPtr = 0;
} else if (pngPtr) {
png_destroy_read_struct( &pngPtr, 0, 0 );
pngPtr = 0;
}
} else {
good = false; // Was not a png file
}
return good;
}
static GdkPixbuf* pixbuf_new_from_file( const char *filename, time_t &modTime, gchar*& pixPath, GError **/*error*/ )
{
modTime = 0;
if ( pixPath ) {
pixPath = 0;
}
//buf = gdk_pixbuf_new_from_file( filename, error );
if ( fp )
{
{
if ( !val ) {
}
}
if ( loader )
{
// short buffer
{
//g_message( "First data chunk" );
{
// TODO set the dpi to be read elsewhere
}
} else if ( !latter ) {
//g_message(" READing latter");
}
// Now clear out the buffer so we can read more.
// (dumping out unused)
}
}
if ( ok ) {
if ( buf ) {
if ( dpiX ) {
if ( tmp ) {
//g_message("Need to set DpiX: %s", tmp);
//gdk_pixbuf_set_option( buf, "Inkscape::DpiX", tmp );
}
}
if ( dpiY ) {
if ( tmp ) {
//g_message("Need to set DpiY: %s", tmp);
//gdk_pixbuf_set_option( buf, "Inkscape::DpiY", tmp );
}
}
}
} else {
// do something
g_message("error loading pixbuf at close");
}
} else {
g_message("error when creating pixbuf loader");
}
fp = 0;
} else {
}
/*
if ( buf )
{
const gchar* bloop = gdk_pixbuf_get_option( buf, "Inkscape::DpiX" );
if ( bloop )
{
g_message("DPI X is [%s]", bloop);
}
bloop = gdk_pixbuf_get_option( buf, "Inkscape::DpiY" );
if ( bloop )
{
g_message("DPI Y is [%s]", bloop);
}
}
*/
return buf;
}
{
if (pixPath) {
}
return result;
}
}
}
sp_image_get_type (void)
{
static GType image_type = 0;
if (!image_type) {
GTypeInfo image_info = {
sizeof (SPImageClass),
NULL, /* base_init */
NULL, /* base_finalize */
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (SPImage),
16, /* n_preallocs */
NULL, /* value_table */
};
}
return image_type;
}
static void
{
}
{
image->trimheight = 0;
image->viewheight = 0;
#if ENABLE_LCMS
image->color_profile = 0;
#endif // ENABLE_LCMS
}
static void
{
}
/* Register */
}
static void
{
if (SP_OBJECT_DOCUMENT (object)) {
/* Unregister ourselves */
}
}
}
#if ENABLE_LCMS
if (image->color_profile) {
}
#endif // ENABLE_LCMS
}
}
}
}
static void
{
switch (key) {
case SP_ATTR_XLINK_HREF:
break;
case SP_ATTR_X:
/* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
}
break;
case SP_ATTR_Y:
/* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
}
break;
case SP_ATTR_WIDTH:
/* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
}
break;
case SP_ATTR_HEIGHT:
/* fixme: em, ex, % are probably valid, but require special treatment (Lauris) */
}
break;
/* Do setup before, so we can use break to escape */
if (value) {
int len;
gchar c[256];
const gchar *p, *e;
p = value;
while (*p && *p == 32) p += 1;
if (!*p) break;
e = p;
while (*e && *e != 32) e += 1;
len = e - p;
if (len > 8) break;
c[len] = 0;
/* Now the actual part */
if (!strcmp (c, "none")) {
} else if (!strcmp (c, "xMinYMin")) {
} else if (!strcmp (c, "xMidYMin")) {
} else if (!strcmp (c, "xMaxYMin")) {
} else if (!strcmp (c, "xMinYMid")) {
} else if (!strcmp (c, "xMidYMid")) {
} else if (!strcmp (c, "xMaxYMid")) {
} else if (!strcmp (c, "xMinYMax")) {
} else if (!strcmp (c, "xMidYMax")) {
} else if (!strcmp (c, "xMaxYMax")) {
} else {
break;
}
while (*e && *e == 32) e += 1;
if (*e) {
if (!strcmp (e, "meet")) {
} else if (!strcmp (e, "slice")) {
} else {
break;
}
}
}
break;
#if ENABLE_LCMS
case SP_PROP_COLOR_PROFILE:
if ( image->color_profile ) {
}
#ifdef DEBUG_LCMS
if ( value ) {
} else {
}
#endif // DEBUG_LCMS
// TODO check on this HREF_MODIFIED flag
break;
#endif // ENABLE_LCMS
default:
break;
}
}
static void
{
}
if (flags & SP_IMAGE_HREF_MODIFIED_FLAG) {
}
}
if (pixbuf) {
// BLIP
#if ENABLE_LCMS
if ( image->color_profile )
{
if ( px ) {
#ifdef DEBUG_LCMS
#endif // DEBUG_LCMS
image->color_profile );
if ( prof ) {
if ( profileClass != icSigNamedColorClass ) {
int intent = INTENT_PERCEPTUAL;
switch ( profIntent ) {
break;
break;
break;
case Inkscape::RENDERING_INTENT_UNKNOWN:
case Inkscape::RENDERING_INTENT_AUTO:
default:
}
intent, 0 );
if ( transf ) {
for ( int y = 0; y < imageheight; y++ ) {
// Since the types are the same size, we can do the transformation in-place
}
}
#ifdef DEBUG_LCMS
else
{
}
#endif // DEBUG_LCMS
}
#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
{
}
#endif // DEBUG_LCMS
}
}
#endif // ENABLE_LCMS
}
}
}
// preserveAspectRatio calculate bounds / clipping rectangle -- EAF
int imagewidth, imageheight;
double x,y;
switch (image->aspect_align) {
case SP_ASPECT_XMIN_YMIN:
x = 0.0;
y = 0.0;
break;
case SP_ASPECT_XMID_YMIN:
x = 0.5;
y = 0.0;
break;
case SP_ASPECT_XMAX_YMIN:
x = 1.0;
y = 0.0;
break;
case SP_ASPECT_XMIN_YMID:
x = 0.0;
y = 0.5;
break;
case SP_ASPECT_XMID_YMID:
x = 0.5;
y = 0.5;
break;
case SP_ASPECT_XMAX_YMID:
x = 1.0;
y = 0.5;
break;
case SP_ASPECT_XMIN_YMAX:
x = 0.0;
y = 1.0;
break;
case SP_ASPECT_XMID_YMAX:
x = 0.5;
y = 1.0;
break;
case SP_ASPECT_XMAX_YMAX:
x = 1.0;
y = 1.0;
break;
default:
x = 0.0;
y = 0.0;
break;
}
// Pixels aspect is wider than bounding box
image->trimwidth = static_cast<int>(static_cast<double>(imageheight) * image->width.computed / image->height.computed);
} else {
// Pixels aspect is taller than bounding box
image->trimheight = static_cast<int>(static_cast<double>(imagewidth) * image->height.computed / image->width.computed);
}
} else {
// Otherwise, assume SP_ASPECT_MEET
// Pixels aspect is wider than bounding boz
} else {
// Pixels aspect is taller than bounding box
}
}
}
}
static void
{
}
if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
}
}
}
sp_image_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
}
/* fixme: Reset attribute if needed (Lauris) */
}
}
}
}
#if ENABLE_LCMS
if (image->color_profile) {
}
#endif // ENABLE_LCMS
}
return repr;
}
static void
sp_image_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
{
}
}
static void
{
int pixskip = gdk_pixbuf_get_n_channels(image->pixbuf) * gdk_pixbuf_get_bits_per_sample(image->pixbuf) / 8;
/* fixme: (Lauris) */
t = s * tp;
t = ti * t;
} else { // preserveAspectRatio
t = s * tp;
t = ti * t;
}
} else { // preserveAspectRatio
sp_print_image_R8G8B8A8_N(ctx, px + image->trimx*pixskip + image->trimy*rs, image->trimwidth, image->trimheight, rs, &t, SP_OBJECT_STYLE(item));
}
}
}
static gchar *
{
char *href_desc;
? g_strdup(_("embedded"))
} else {
g_warning("Attempting to call strncmp() with a null pointer.");
}
: g_strdup_printf(_("<b>Image</b> %d × %d: %s"),
href_desc) );
return ret;
}
static NRArenaItem *
{
#if 0
int pixskip = gdk_pixbuf_get_n_channels(image->pixbuf) * gdk_pixbuf_get_bits_per_sample(image->pixbuf) / 8;
rs);
} else { // preserveAspectRatio
rs);
}
#endif
} else {
}
// TODO: reenable preserveAspectRatio
//if (image->aspect_align == SP_ASPECT_NONE) {
nr_arena_image_set_geometry(NR_ARENA_IMAGE(ai), image->x.computed, image->y.computed, image->width.computed, image->height.computed);
//} else { // preserveAspectRatio
// nr_arena_image_set_geometry(NR_ARENA_IMAGE(ai), image->viewx, image->viewy, image->viewwidth, image->viewheight);
//}
return ai;
}
/*
* utility function to try loading image from href
*
* absolute_src
*
*/
GdkPixbuf *sp_image_repr_read_image( time_t& modTime, char*& pixPath, const gchar *href, const gchar *absref, const gchar *base )
{
modTime = 0;
if ( pixPath ) {
pixPath = 0;
}
if (fullname) {
// TODO check this. Was doing a UTF-8 to filename conversion here.
return pixbuf;
}
}
/* data URI - embedded image */
filename += 5;
return pixbuf;
}
} else {
if (!g_path_is_absolute (filename)) {
/* try to load from relative pos combined with document base*/
if (!docbase) {
docbase = ".";
}
// 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
return pixbuf;
}
}
}
/* try filename as absolute */
return pixbuf;
}
}
}
}
/* at last try to load from sp absolute path name */
// using absref is outside of SVG rules, so we must at least warn the user
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);
}
return pixbuf;
}
}
/* Nope: We do not find any valid pixmap file :-( */
/* It should be included xpm, so if it still does not does load, */
/* our libraries are broken */
return pixbuf;
}
static GdkPixbuf *
{
if (gdk_pixbuf_get_has_alpha(pixbuf)) {
} else {
}
return result;
}
/* We assert that realpixbuf is either NULL or identical size to pixbuf */
static void
{
/* fixme: We are slightly violating spec here (Lauris) */
}
}
}
int pixskip = gdk_pixbuf_get_n_channels(image->pixbuf) * gdk_pixbuf_get_bits_per_sample(image->pixbuf) / 8;
rs);
} else { // preserveAspectRatio
rs);
}
}
}
static void sp_image_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const */*snapprefs*/)
{
/* 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.
*/
//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
//TODO Do return snappoints, but only when within visual bounding box
} else {
// The image has not been clipped: return its corners, which might be rotated for example
p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(x0, y0) * i2d, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(x0, y1) * i2d, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(x1, y1) * i2d, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(x1, y0) * i2d, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
}
}
/*
* Initially we'll do:
* Transform x, y, set x, y, clear translation
*/
{
/* Calculate position in parent coords. */
/* This function takes care of translation and scaling, we return whatever parts we can't
handle. */
} else {
ret[0] = 1.0;
}
} else {
}
/* Find position in item coords */
return ret;
}
static GdkPixbuf *
{
gint data_is_image = 0;
gint data_is_base64 = 0;
while (*data) {
/* base64-encoding */
data_is_base64 = 1;
data_is_image = 1; // Illustrator produces embedded images without MIME type, so we assume it's image no matter what
data += 6;
}
/* PNG image */
data_is_image = 1;
data += 9;
}
/* JPEG image */
data_is_image = 1;
data += 9;
}
/* JPEG image */
data_is_image = 1;
data += 10;
}
else { /* unrecognized option; skip it */
while (*data) {
break;
}
data++;
}
}
if ((*data) == ';') {
data++;
continue;
}
if ((*data) == ',') {
data++;
break;
}
}
}
return pixbuf;
}
static GdkPixbuf *
{
if (loader) {
bool eos = false;
bool failed = false;
while (!eos) {
for (gint j = 0; j < 19; j++) {
for (gint k = 0; k < 4; k++) {
if ((*btr) == '\0') break;
btr++;
}
if (eos) {
ud[k] = 0;
continue;
}
eos = true;
ud[k] = 0;
continue;
}
ud[k] = 64;
break;
}
}
eos = true;
ud[k] = 0;
continue;
}
btr++;
len++;
}
if (len > 2) {
}
if (len > 3) {
}
}
failed = true;
break;
}
}
if (!failed) {
}
}
return pixbuf;
}
static void
{
//create a curve at the image's boundary for snapping
if ((image->height.computed < MAGIC_EPSILON_TOO) || (image->width.computed < MAGIC_EPSILON_TOO) || (image->clip_ref->getObject())) {
}
} else {
}
if (c) {
c->unref();
}
}
}
/**
* Return duplicate of curve (if any exists) or NULL if there is no curve
*/
SPCurve *
{
}
return result;
}
void
{
format = "jpeg";
} else {
format = "png";
}
// Save base64 encoded data in image node
// this formula taken from Glib docs
}
{
// It *might* change
if ( !val ) {
// stat call worked. Check time now
unsigned int flags = SP_IMAGE_HREF_MODIFIED_FLAG;
}
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :