/*
* Helper functions to use cairo with inkscape
*
* Copyright (C) 2007 bulia byak
* Copyright (C) 2008 Johan Engelen
*
* Released under GNU GPL
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "display/cairo-utils.h"
#include <stdexcept>
#include <glibmm/fileutils.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "color.h"
#include "style.h"
#include "helper/geom-curves.h"
#include "display/cairo-templates.h"
/**
* Key for cairo_surface_t to keep track of current color interpolation value
* Only the address of the structure is used, it is never initialized. See:
* http://www.cairographics.org/manual/cairo-Types.html#cairo-user-data-key-t
*/
namespace Inkscape {
CairoGroup::~CairoGroup() {
if (pushed) {
}
}
pushed = true;
}
pushed = true;
}
if (pushed) {
pushed = false;
return ret;
} else {
}
}
if (pushed) {
pushed = false;
return retmm;
} else {
}
}
if (pushed) {
pushed = false;
}
}
{}
{
}
{
}
{
}
{
return ret;
}
/* The class below implement the following hack:
*
* The pixels formats of Cairo and GdkPixbuf are different.
* GdkPixbuf accesses pixels as bytes, alpha is not premultiplied,
* and successive bytes of a single pixel contain R, G, B and A components.
* Cairo accesses pixels as 32-bit ints, alpha is premultiplied,
* and each int contains as 0xAARRGGBB, accessed with bitwise operations.
*
* In other words, on a little endian system, a GdkPixbuf will contain:
* char *data = "rgbargbargba...."
* int *data = { 0xAABBGGRR, 0xAABBGGRR, 0xAABBGGRR, ... }
* while a Cairo image surface will contain:
* char *data = "bgrabgrabgra...."
* int *data = { 0xAARRGGBB, 0xAARRGGBB, 0xAARRGGBB, ... }
*
* It is possible to convert between these two formats (almost) losslessly.
* Some color information from partially transparent regions of the image
* is lost, but the result when displaying this image will remain the same.
*
* The class allows interoperation between GdkPixbuf
* and Cairo surfaces without creating a copy of the image.
* This is implemented by creating a GdkPixbuf and a Cairo image surface
* which share their data. Depending on what is needed at a given time,
* the pixels are converted in place to the Cairo or the GdkPixbuf format.
*/
/** Create a pixbuf from a Cairo surface.
* The constructor takes ownership of the passed surface,
* so it should not be destroyed. */
, _surface(s)
, _mod_time(0)
, _cairo_store(true)
{}
/** Create a pixbuf from a GdkPixbuf.
* The constructor takes ownership of the passed GdkPixbuf reference,
* so it should not be unrefed. */
, _surface(0)
, _mod_time(0)
, _cairo_store(false)
{
_forceAlpha();
}
, _cairo_store(false)
{}
{
if (_cairo_store) {
} else {
}
}
{
bool data_is_image = false;
bool data_is_base64 = false;
while (*data) {
/* base64-encoding */
data_is_base64 = true;
data_is_image = true; // Illustrator produces embedded images without MIME type, so we assume it's image no matter what
data += 6;
}
/* PNG image */
data_is_image = true;
data += 9;
}
/* JPEG image */
data_is_image = true;
data += 9;
}
/* JPEG image */
data_is_image = true;
data += 10;
}
/* JPEG2000 image */
data_is_image = true;
data += 9;
}
else { /* unrecognized option; skip it */
while (*data) {
break;
}
data++;
}
}
if ((*data) == ';') {
data++;
continue;
}
if ((*data) == ',') {
data++;
break;
}
}
gsize decoded_len = 0;
if (buf) {
} else {
}
} else {
}
}
return pixbuf;
}
{
// test correctness of filename
return NULL;
}
return NULL;
}
// we need to load the entire file into memory,
// since we'll store it as MIME data
if (buf) {
} else {
}
// TODO: we could also read DPI, ICC profile, gamma correction, and other information
// from the file. This can be done by using format-specific libraries e.g. libpng.
} else {
return NULL;
}
return pb;
}
/**
* Converts the pixbuf to GdkPixbuf pixel format.
* The returned pixbuf can be used e.g. in calls to gdk_pixbuf_save().
*/
{
if (convert_format) {
}
return _pixbuf;
}
/**
* Converts the pixbuf to Cairo pixel format and returns an image surface
* which can be used as a source.
*
* The returned surface is owned by the GdkPixbuf and should not be freed.
* Calling this function causes the pixbuf to be unsuitable for use
* with GTK drawing functions until ensurePixelFormat(Pixbuf::PIXEL_FORMAT_PIXBUF) is called.
*/
{
if (convert_format) {
}
return _surface;
}
* when compiling with G_DISABLE_DEPRECATED, as we do in non-release builds.
* at the top.
*
* Since we don't really use gdkmm, do not define this function for now. */
/*
Glib::RefPtr<Gdk::Pixbuf> Pixbuf::getPixbuf(bool convert_format = true)
{
g_object_ref(_pixbuf);
Glib::RefPtr<Gdk::Pixbuf> p(getPixbuf(convert_format));
return p;
}
*/
{
return p;
}
/** Retrieves the original compressed data for the surface, if any.
* The returned data belongs to the object and should not be freed. */
{
for (guint i = 0; i < mimetypes_len; ++i) {
unsigned long len_long = 0;
cairo_surface_get_mime_data(const_cast<cairo_surface_t*>(_surface), mimetypes[i], &data, &len_long);
break;
}
}
return data;
}
}
}
}
}
return gdk_pixbuf_get_pixels(_pixbuf);
}
}
{
if (gdk_pixbuf_get_has_alpha(_pixbuf)) return;
}
{
if (format == "jpeg") {
} else if (format == "jpeg2000") {
} else if (format == "png") {
}
//g_message("Setting Cairo MIME data: %s", mimetype);
} else {
//g_message("Not setting Cairo MIME data: unknown format %s", name.c_str());
}
}
{
if (_pixel_format == PF_GDK) {
return;
}
_pixel_format = fmt;
return;
}
}
if (_pixel_format == PF_CAIRO) {
_pixel_format = fmt;
return;
}
return;
}
}
}
} // namespace Inkscape
/*
* Can be called recursively.
* If optimize_stroke == false, the view Rect is not used.
*/
static void
feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Affine const & trans, Geom::Rect view, bool optimize_stroke)
{
using Geom::X;
using Geom::Y;
unsigned order = 0;
}
// handle the three typical curve cases
switch (order) {
case 1:
{
if (!optimize_stroke) {
} else {
} else {
}
}
}
break;
case 2:
{
// degree-elevate to cubic Bezier, since Cairo doesn't do quadratic Beziers
if (!optimize_stroke) {
} else {
} else {
}
}
}
break;
case 3:
{
//points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
if (!optimize_stroke) {
cairo_curve_to(cr, points[1][X], points[1][Y], points[2][X], points[2][Y], points[3][X], points[3][Y]);
} else {
cairo_curve_to(cr, points[1][X], points[1][Y], points[2][X], points[2][Y], points[3][X], points[3][Y]);
} else {
}
}
}
break;
default:
{
//if (!optimize_stroke || a->boundsFast().intersects(view)) {
// Apply the transformation to the current context
cairo_save(cr);
// Draw the circle
if (a->sweep()) {
} else {
}
// Revert the current context
//} else {
// Geom::Point f = a->finalPoint() * trans;
// cairo_move_to(cr, f[X], f[Y]);
//}
} else {
// handles sbasis as well as all other curve types
// this is very slow
// recurse to convert the new path resulting from the sbasis to svgd
}
}
}
break;
}
}
/** Feeds path-creating calls to the cairo context translating them from the Path */
static void
{
return;
feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
}
}
}
/** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
static void
feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
if (!area)
return;
return;
// Transform all coordinates to coords within "area"
// Pass transformation to feed_curve, so that we don't need to create a whole new path.
}
if (!optimize_stroke) {
} else {
/* We cannot use cairo_close_path(ct) here because some parts of the path may have been
clipped and not drawn (maybe the before last segment was outside view area), which
would result in closing the "subpath" after the last interruption, not the entire path.
However, according to cairo documentation:
The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
there is a line join connecting the final and initial segments of the sub-path.
The correct fix will be possible when cairo introduces methods for moving without
see bug 168129
*/
}
}
}
/** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
* One must have done cairo_new_path(ct); before calling this function. */
void
feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
if (!area)
return;
return;
}
}
/** Feeds path-creating calls to the cairo context translating them from the PathVector
* One must have done cairo_new_path(ct); before calling this function. */
void
{
return;
}
}
} else {
return SP_CSS_COLOR_INTERPOLATION_AUTO;
}
}
/** Set the color_interpolation_value for a Cairo surface.
* Transform the surface between sRGB and linearRGB if necessary. */
void
if( ci_in == SP_CSS_COLOR_INTERPOLATION_SRGB &&
}
if( ci_in == SP_CSS_COLOR_INTERPOLATION_LINEARRGB &&
}
}
}
void
cairo_surface_set_user_data(out, &ink_color_interpolation_key, cairo_surface_get_user_data(in, &ink_color_interpolation_key), NULL);
}
void
{
cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
}
void
{
}
{
}
{
}
void
{
ink_matrix_to_cairo(cm, m);
}
void
{
ink_matrix_to_cairo(cm, m);
}
/**
* Create an exact copy of a surface.
* Creates a surface that has the same type, content type, dimensions and contents
* as the specified surface.
*/
{
if (cairo_surface_get_type(s) == CAIRO_SURFACE_TYPE_IMAGE) {
// use memory copy instead of using a Cairo context
int h = cairo_image_surface_get_height(s);
} else {
// generic implementation
cairo_set_source_surface(ct, s, 0, 0);
}
return ns;
}
/**
* Create a surface that differs only in pixel content.
* Creates a surface that has the same type, content type and dimensions
* as the specified surface. Pixel contents are not copied.
*/
{
cairo_surface_set_user_data(ns, &ink_color_interpolation_key, cairo_surface_get_user_data(s, &ink_color_interpolation_key), NULL);
return ns;
}
{
return ns;
}
/**
* Extract the alpha channel into a new surface.
* Creates a surface with a content type of CAIRO_CONTENT_ALPHA that contains
* the alpha values of pixels from @a s.
*/
{
cairo_set_source_surface(ct, s, 0, 0);
return alpha;
}
{
} else {
}
return out;
}
void
{
{
// use memory copy instead of using a Cairo context
int h = cairo_image_surface_get_height(src);
} else {
// generic implementation
}
}
int
{
// For now only image surface is handled.
// Later add others, e.g. cairo-gl
return cairo_image_surface_get_width(surface);
}
int
{
return cairo_image_surface_get_height(surface);
}
static int ink_cairo_surface_average_color_internal(cairo_surface_t *surface, double &rf, double &gf, double &bf, double &af)
{
/* TODO convert this to OpenMP somehow */
for (int x = 0; x < width; ++x) {
EXTRACT_ARGB32(px, a,r,g,b)
rf += r / 255.0;
gf += g / 255.0;
bf += b / 255.0;
af += a / 255.0;
}
}
}
{
ASSEMBLE_ARGB32(px, a,r,g,b);
return px;
}
void ink_cairo_surface_average_color(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
r /= a;
g /= a;
b /= a;
a /= count;
}
void ink_cairo_surface_average_color_premul(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
r /= count;
g /= count;
b /= count;
a /= count;
}
if( cc < 0.04045 ) {
cc /= 12.92;
} else {
}
cc *= 255.0;
return premul_alpha( c2, a );
}
if( cc < 0.0031308 ) {
cc *= 12.92;
} else {
}
cc *= 255.0;
return premul_alpha( c2, a );
}
struct SurfaceSrgbToLinear {
if( a != 0 ) {
r = srgb_to_linear( r, a );
g = srgb_to_linear( g, a );
b = srgb_to_linear( b, a );
}
ASSEMBLE_ARGB32(out, a,r,g,b);
return out;
}
private:
/* None */
};
{
// int stride = cairo_image_surface_get_stride(surface);
// unsigned char *data = cairo_image_surface_get_data(surface);
/* TODO convert this to OpenMP somehow */
// for (int y = 0; y < height; ++y, data += stride) {
// for (int x = 0; x < width; ++x) {
// guint32 px = *reinterpret_cast<guint32*>(data + 4*x);
// EXTRACT_ARGB32(px, a,r,g,b) ; // Unneeded semi-colon for indenting
// if( a != 0 ) {
// r = srgb_to_linear( r, a );
// g = srgb_to_linear( g, a );
// b = srgb_to_linear( b, a );
// }
// ASSEMBLE_ARGB32(px2, a,r,g,b);
// *reinterpret_cast<guint32*>(data + 4*x) = px2;
// }
// }
}
struct SurfaceLinearToSrgb {
if( a != 0 ) {
r = linear_to_srgb( r, a );
g = linear_to_srgb( g, a );
b = linear_to_srgb( b, a );
}
ASSEMBLE_ARGB32(out, a,r,g,b);
return out;
}
private:
/* None */
};
{
// int stride = cairo_image_surface_get_stride(surface);
// unsigned char *data = cairo_image_surface_get_data(surface);
// /* TODO convert this to OpenMP somehow */
// for (int y = 0; y < height; ++y, data += stride) {
// for (int x = 0; x < width; ++x) {
// guint32 px = *reinterpret_cast<guint32*>(data + 4*x);
// EXTRACT_ARGB32(px, a,r,g,b) ; // Unneeded semi-colon for indenting
// if( a != 0 ) {
// r = linear_to_srgb( r, a );
// g = linear_to_srgb( g, a );
// b = linear_to_srgb( b, a );
// }
// ASSEMBLE_ARGB32(px2, a,r,g,b);
// *reinterpret_cast<guint32*>(data + 4*x) = px2;
// }
// }
}
{
int const w = 6;
int const h = 6;
cairo_rectangle(ct, 0, 0, w, h);
cairo_rectangle(ct, w, h, w, h);
cairo_fill(ct);
return p;
}
/**
* Converts the Cairo surface to a GdkPixbuf pixel format,
* without allocating extra memory.
*
* This function is intended mainly for creating previews displayed by GTK.
* For loading images for display on the canvas, use the Inkscape::Pixbuf object.
*
* The returned GdkPixbuf takes ownership of the passed surface reference,
* so it should NOT be freed after calling this function.
*/
{
int w = cairo_image_surface_get_width(s);
int h = cairo_image_surface_get_height(s);
w, h, rs, ink_cairo_pixbuf_cleanup, s);
return pb;
}
/**
* Cleanup function for GdkPixbuf.
* This function should be passed as the GdkPixbufDestroyNotify parameter
* to gdk_pixbuf_new_from_data when creating a GdkPixbuf backed by
* a Cairo surface.
*/
{
}
/* The following two functions use "from" instead of "to", because when you write:
val1 = argb32_from_pixbuf(val1);
the name of the format is closer to the value in that format. */
{
guint32 o = 0;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#else
guint32 a = (c & 0x000000ff);
#endif
if (a != 0) {
// extract color components
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
guint32 r = (c & 0x000000ff);
#else
#endif
// premultiply
r = premul_alpha(r, a);
b = premul_alpha(b, a);
g = premul_alpha(g, a);
// combine into output
o = (a << 24) | (r << 16) | (g << 8) | (b);
}
return o;
}
{
if (a == 0) return 0;
// extract color components
guint32 b = (c & 0x000000ff);
// unpremultiply; adding a/2 gives correct rounding
// (taken from Cairo sources)
r = (r * 255 + a/2) / a;
b = (b * 255 + a/2) / a;
g = (g * 255 + a/2) / a;
// combine into output
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#else
#endif
return o;
}
/**
* Convert pixel data from GdkPixbuf format to ARGB.
* This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
* This involves premultiplying alpha and shuffling around the channels.
* Pixbuf data must have an alpha channel, otherwise the results are undefined
* (usually a segfault).
*/
void
{
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
++px;
}
}
}
/**
* Convert pixel data from ARGB to GdkPixbuf format.
* This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
* This involves premultiplying alpha and shuffling around the channels.
*/
void
{
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
++px;
}
}
}
/**
* Converts GdkPixbuf's data to premultiplied ARGB.
* This function will convert a GdkPixbuf in place into Cairo's native pixel format.
* Note that this is a hack intended to save memory. When the pixbuf is in Cairo's format,
* using it with GTK will result in corrupted drawings.
*/
void
{
// nothing to do
return;
}
}
/**
* Converts GdkPixbuf's data back to its native format.
* Once this is done, the pixbuf can be used with GTK again.
*/
void
{
// nothing to do
return;
}
}
{
guint32 r, g, b, a;
a = (in & 0x000000ff);
ASSEMBLE_ARGB32(px, a, r, g, b)
return px;
}
/*
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:fileencoding=utf-8:textwidth=99 :