command line export functionality only

Discussion about writing code for Inkscape.
matheszabi
Posts: 4
Joined: Sun Sep 08, 2013 1:53 am

command line export functionality only

Postby matheszabi » Tue Oct 08, 2013 8:22 am

I would like to extract from sources all, what I don't need for the following command:

/Applications/Inkscape.app/Contents/Resources/bin/inkscape --export-png output.png -w 1024 -h 768 input.svg

I would like to launch or create a shared library with this functionality only.
The input file need to parse, and I don't know how is done the rendering, but I need only as png into a file.

Any hints?


So far:
from main.cpp is executed:

sp_main_console

inkscape_application_init
sp_process_file_list



SPDocument *doc = NULL;
try {
doc = Inkscape::Extension::open(NULL, filename);
} catch (Inkscape::Extension::Input::no_extension_found &e) {
doc = NULL;
} catch (Inkscape::Extension::Input::open_failed &e) {
doc = NULL;
}

inkscape_add_document

...
static int sp_do_export_png(SPDocument *doc)

...
sp_export_png_file

sp_png_write_rgba_striped - it will open the file finally...

http://bazaar.launchpad.net/~inkscape.d ... -write.cpp


Maybe this is the "headless rendering" part:

Code: Select all

 struct SPEBP ebp;
    ebp.width  = width;
    ebp.height = height;
    ebp.background = bgcolor;

    /* Create new drawing */
    Inkscape::Drawing drawing;
    drawing.setExact(true); // export with maximum blur rendering quality
    unsigned const dkey = SPItem::display_key_new(1);

    // Create ArenaItems and set transform
    drawing.setRoot(doc->getRoot()->invoke_show(drawing, dkey, SP_ITEM_SHOW_DISPLAY));
    drawing.root()->setTransform(affine);
    ebp.drawing = &drawing;



or this?

Code: Select all

static int
sp_export_get_rows(guchar const **rows, void **to_free, int row, int num_rows, void *data)
{
    struct SPEBP *ebp = (struct SPEBP *) data;

    if (ebp->status) {
        if (!ebp->status((float) row / ebp->height, ebp->data)) return 0;
    }

    num_rows = MIN(num_rows, static_cast<int>(ebp->sheight));
    num_rows = MIN(num_rows, static_cast<int>(ebp->height - row));

    /* Set area of interest */
    // bbox is now set to the entire image to prevent discontinuities
    // in the image when blur is used (the borders may still be a bit
    // off, but that's less noticeable).
    Geom::IntRect bbox = Geom::IntRect::from_xywh(0, row, ebp->width, num_rows);

    /* Update to renderable state */
    ebp->drawing->update(bbox);

    int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, ebp->width);
    unsigned char *px = g_new(guchar, num_rows * stride);

    cairo_surface_t *s = cairo_image_surface_create_for_data(
        px, CAIRO_FORMAT_ARGB32, ebp->width, num_rows, stride);
    Inkscape::DrawingContext ct(s, bbox.min());
    ct.setSource(ebp->background);
    ct.setOperator(CAIRO_OPERATOR_SOURCE);
    ct.paint();
    ct.setOperator(CAIRO_OPERATOR_OVER);

    /* Render */
    ebp->drawing->render(ct, bbox);
    cairo_surface_destroy(s);

    *to_free = px;

    // PNG stores data as unpremultiplied big-endian RGBA, which means
    // it's identical to the GdkPixbuf format.
    convert_pixels_argb32_to_pixbuf(px, ebp->width, num_rows, stride);

    for (int r = 0; r < num_rows; r++) {
        rows[r] = px + r * stride;
    }

    return num_rows;
}
Last edited by matheszabi on Tue Oct 08, 2013 9:37 am, edited 2 times in total.

v1nce
Posts: 696
Joined: Wed Jan 13, 2010 4:36 am

Re: command line export functionality only

Postby v1nce » Tue Oct 08, 2013 9:23 am

Didn't look at the sources how difficult it would be to extract the renderer part of inkscape.
It would be real nice to have.

OTOH you could use librsvg that only does rendering
http://stackoverflow.com/questions/1143 ... ng-streams
but I don't know how it compares to inkscape (filters...)

matheszabi
Posts: 4
Joined: Sun Sep 08, 2013 1:53 am

Re: command line export functionality only

Postby matheszabi » Tue Oct 08, 2013 9:50 am

I would like to run in Android NDK part :)

Not sure what need Cairo, but libcairo.2.dylib is 1.6 MB, which is an acceptable size.
Inkscape is 30MB, that is to big and I don't need all of his editing stuff at all.

http://en.wikipedia.org/wiki/Librsvg

hm, I need to try it, maybe I have a luck.

User avatar
ragstian
Posts: 1181
Joined: Thu Oct 11, 2012 2:44 am
Location: Stavanger-Norway

Re: command line export functionality only

Postby ragstian » Tue Oct 08, 2013 9:55 am

Hi.

Extracting the renderer part from the Inkscape source and compile with the right dependencies might not be trivial.

The Inkscape renderer uses the Cairo library (for display and PNG export).
You can easily use Cairo from Python.
( or C or C# ?? - I have not got the "brain-capacity" to learn C - so I stick to Python - Life is short - you need Python!)

Have a look at these;
https://pypi.python.org/pypi/CairoSVG
http://stackoverflow.com/questions/6589358/convert-svg-to-png-in-python
http://sachinpatil.com/blog/2013/03/26/python-tips/

Looking at your "Forum name" you might be interested in matplotlib as well; http://matplotlib.org/

Edit - Python for Android; http://code.google.com/p/python-for-android/

Good Luck.
RGDS
Ragnar
Good Luck!
( ͡° ͜ʖ ͡°)
RGDS
Ragnar


Return to “Programming”