Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / helper / png-write.cpp
1 /*
2  * PNG file format utilities
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Whoever wrote this example in libpng documentation
7  *   Jon A. Cruz <jon@joncruz.org>
8  *   Abhishek Sharma
9  *
10  * Copyright (C) 1999-2002 authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include <interface.h>
20 #include <libnr/nr-pixops.h>
21 #include <libnr/nr-translate-scale-ops.h>
22 #include <2geom/rect.h>
23 #include <glib/gmessages.h>
24 #include <png.h>
25 #include "png-write.h"
26 #include "io/sys.h"
27 #include <display/nr-arena-item.h>
28 #include <display/nr-arena.h>
29 #include <document.h>
30 #include <sp-item.h>
31 #include <sp-root.h>
32 #include <sp-defs.h>
33 #include "preferences.h"
34 #include "rdf.h"
36 /* This is an example of how to use libpng to read and write PNG files.
37  * The file libpng.txt is much more verbose then this.  If you have not
38  * read it, do so first.  This was designed to be a starting point of an
39  * implementation.  This is not officially part of libpng, and therefore
40  * does not require a copyright notice.
41  *
42  * This file does not currently compile, because it is missing certain
43  * parts, like allocating memory to hold an image.  You will have to
44  * supply these parts to get it to compile.  For an example of a minimal
45  * working PNG reader/writer, see pngtest.c, included in this distribution.
46  */
48 static unsigned int const MAX_STRIPE_SIZE = 1024*1024;
50 struct SPEBP {
51     unsigned long int width, height, sheight;
52     guchar r, g, b, a;
53     NRArenaItem *root; // the root arena item to show; it is assumed that all unneeded items are hidden
54     guchar *px;
55     unsigned (*status)(float, void *);
56     void *data;
57 };
59 /* write a png file */
61 typedef struct SPPNGBD {
62     guchar const *px;
63     int rowstride;
64 } SPPNGBD;
66 /**
67  * A simple wrapper to list png_text.
68  */
69 class PngTextList {
70 public:
71     PngTextList() : count(0), textItems(0) {}
72     ~PngTextList();
74     void add(gchar const* key, gchar const* text);
75     gint getCount() {return count;}
76     png_text* getPtext() {return textItems;}
78 private:
79     gint count;
80     png_text* textItems;
81 };
83 PngTextList::~PngTextList() {
84     for (gint i = 0; i < count; i++) {
85         if (textItems[i].key) {
86             g_free(textItems[i].key);
87         }
88         if (textItems[i].text) {
89             g_free(textItems[i].text);
90         }
91     }
92 }
94 void PngTextList::add(gchar const* key, gchar const* text)
95 {
96     if (count < 0) {
97         count = 0;
98         textItems = 0;
99     }
100     png_text* tmp = (count > 0) ? g_try_renew(png_text, textItems, count + 1): g_try_new(png_text, 1);
101     if (tmp) {
102         textItems = tmp;
103         count++;
105         png_text* item = &(textItems[count - 1]);
106         item->compression = PNG_TEXT_COMPRESSION_NONE;
107         item->key = g_strdup(key);
108         item->text = g_strdup(text);
109         item->text_length = 0;
110 #ifdef PNG_iTXt_SUPPORTED
111         item->itxt_length = 0;
112         item->lang = 0;
113         item->lang_key = 0;
114 #endif // PNG_iTXt_SUPPORTED
115     } else {
116         g_warning("Unable to allocate arrary for %d PNG text data.", count);
117         textItems = 0;
118         count = 0;
119     }
122 static bool
123 sp_png_write_rgba_striped(SPDocument *doc,
124                           gchar const *filename, unsigned long int width, unsigned long int height, double xdpi, double ydpi,
125                           int (* get_rows)(guchar const **rows, int row, int num_rows, void *data),
126                           void *data)
128     struct SPEBP *ebp = (struct SPEBP *) data;
129     FILE *fp;
130     png_structp png_ptr;
131     png_infop info_ptr;
132     png_color_8 sig_bit;
133     png_uint_32 r;
135     g_return_val_if_fail(filename != NULL, false);
136     g_return_val_if_fail(data != NULL, false);
138     /* open the file */
140     Inkscape::IO::dump_fopen_call(filename, "M");
141     fp = Inkscape::IO::fopen_utf8name(filename, "wb");
142     g_return_val_if_fail(fp != NULL, false);
144     /* Create and initialize the png_struct with the desired error handler
145      * functions.  If you want to use the default stderr and longjump method,
146      * you can supply NULL for the last three parameters.  We also check that
147      * the library version is compatible with the one used at compile time,
148      * in case we are using dynamically linked libraries.  REQUIRED.
149      */
150     png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
152     if (png_ptr == NULL) {
153         fclose(fp);
154         return false;
155     }
157     /* Allocate/initialize the image information data.  REQUIRED */
158     info_ptr = png_create_info_struct(png_ptr);
159     if (info_ptr == NULL) {
160         fclose(fp);
161         png_destroy_write_struct(&png_ptr, NULL);
162         return false;
163     }
165     /* Set error handling.  REQUIRED if you aren't supplying your own
166      * error hadnling functions in the png_create_write_struct() call.
167      */
168     if (setjmp(png_ptr->jmpbuf)) {
169         /* If we get here, we had a problem reading the file */
170         fclose(fp);
171         png_destroy_write_struct(&png_ptr, &info_ptr);
172         return false;
173     }
175     /* set up the output control if you are using standard C streams */
176     png_init_io(png_ptr, fp);
178     /* Set the image information here.  Width and height are up to 2^31,
179      * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
180      * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
181      * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
182      * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
183      * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
184      * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
185      */
186     png_set_IHDR(png_ptr, info_ptr,
187                  width,
188                  height,
189                  8, /* bit_depth */
190                  PNG_COLOR_TYPE_RGB_ALPHA,
191                  PNG_INTERLACE_NONE,
192                  PNG_COMPRESSION_TYPE_BASE,
193                  PNG_FILTER_TYPE_BASE);
195     /* otherwise, if we are dealing with a color image then */
196     sig_bit.red = 8;
197     sig_bit.green = 8;
198     sig_bit.blue = 8;
199     /* if the image has an alpha channel then */
200     sig_bit.alpha = 8;
201     png_set_sBIT(png_ptr, info_ptr, &sig_bit);
203     PngTextList textList;
205     textList.add("Software", "www.inkscape.org"); // Made by Inkscape comment
206     {
207         const gchar* pngToDc[] = {"Title", "title",
208                                "Author", "creator",
209                                "Description", "description",
210                                //"Copyright", "",
211                                "Creation Time", "date",
212                                //"Disclaimer", "",
213                                //"Warning", "",
214                                "Source", "source"
215                                //"Comment", ""
216         };
217         for (size_t i = 0; i < G_N_ELEMENTS(pngToDc); i += 2) {
218             struct rdf_work_entity_t * entity = rdf_find_entity ( pngToDc[i + 1] );
219             if (entity) {
220                 gchar const* data = rdf_get_work_entity(doc, entity);
221                 if (data && *data) {
222                     textList.add(pngToDc[i], data);
223                 }
224             } else {
225                 g_warning("Unable to find entity [%s]", pngToDc[i + 1]);
226             }
227         }
230         struct rdf_license_t *license =  rdf_get_license(doc);
231         if (license) {
232             if (license->name && license->uri) {
233                 gchar* tmp = g_strdup_printf("%s %s", license->name, license->uri);
234                 textList.add("Copyright", tmp);
235                 g_free(tmp);
236             } else if (license->name) {
237                 textList.add("Copyright", license->name);
238             } else if (license->uri) {
239                 textList.add("Copyright", license->uri);
240             }
241         }
242     }
243     if (textList.getCount() > 0) {
244         png_set_text(png_ptr, info_ptr, textList.getPtext(), textList.getCount());
245     }
247     /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
248     /* note that if sRGB is present the cHRM chunk must be ignored
249      * on read and must be written in accordance with the sRGB profile */
250     png_set_pHYs(png_ptr, info_ptr, unsigned(xdpi / 0.0254 + 0.5), unsigned(ydpi / 0.0254 + 0.5), PNG_RESOLUTION_METER);
252     /* Write the file header information.  REQUIRED */
253     png_write_info(png_ptr, info_ptr);
255     /* Once we write out the header, the compression type on the text
256      * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
257      * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
258      * at the end.
259      */
261     /* set up the transformations you want.  Note that these are
262      * all optional.  Only call them if you want them.
263      */
265     /* --- CUT --- */
267     /* The easiest way to write the image (you may have a different memory
268      * layout, however, so choose what fits your needs best).  You need to
269      * use the first method if you aren't handling interlacing yourself.
270      */
272     png_bytep* row_pointers = new png_bytep[ebp->sheight];
274     r = 0;
275     while (r < static_cast< png_uint_32 > (height) ) {
276         int n = get_rows((unsigned char const **) row_pointers, r, height-r, data);
277         if (!n) break;
278         png_write_rows(png_ptr, row_pointers, n);
279         r += n;
280     }
282     delete[] row_pointers;
284     /* You can write optional chunks like tEXt, zTXt, and tIME at the end
285      * as well.
286      */
288     /* It is REQUIRED to call this to finish writing the rest of the file */
289     png_write_end(png_ptr, info_ptr);
291     /* if you allocated any text comments, free them here */
293     /* clean up after the write, and free any memory allocated */
294     png_destroy_write_struct(&png_ptr, &info_ptr);
296     /* close the file */
297     fclose(fp);
299     /* that's it */
300     return true;
304 /**
305  *
306  */
307 static int
308 sp_export_get_rows(guchar const **rows, int row, int num_rows, void *data)
310     struct SPEBP *ebp = (struct SPEBP *) data;
312     if (ebp->status) {
313         if (!ebp->status((float) row / ebp->height, ebp->data)) return 0;
314     }
316     num_rows = MIN(num_rows, static_cast<int>(ebp->sheight));
317     num_rows = MIN(num_rows, static_cast<int>(ebp->height - row));
319     /* Set area of interest */
320     // bbox is now set to the entire image to prevent discontinuities
321     // in the image when blur is used (the borders may still be a bit
322     // off, but that's less noticeable).
323     NRRectL bbox;
324     bbox.x0 = 0;
325     bbox.y0 = row;
326     bbox.x1 = ebp->width;
327     bbox.y1 = row + num_rows;
328     /* Update to renderable state */
329     NRGC gc(NULL);
330     gc.transform.setIdentity();
332     nr_arena_item_invoke_update(ebp->root, &bbox, &gc,
333            NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
335     NRPixBlock pb;
336     nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
337                              bbox.x0, bbox.y0, bbox.x1, bbox.y1,
338                              ebp->px, 4 * ebp->width, FALSE, FALSE);
340     for (int r = 0; r < num_rows; r++) {
341         guchar *p = NR_PIXBLOCK_PX(&pb) + r * pb.rs;
342         for (int c = 0; c < static_cast<int>(ebp->width); c++) {
343             *p++ = ebp->r;
344             *p++ = ebp->g;
345             *p++ = ebp->b;
346             *p++ = ebp->a;
347         }
348     }
350     /* Render */
351     nr_arena_item_invoke_render(NULL, ebp->root, &bbox, &pb, 0);
353     for (int r = 0; r < num_rows; r++) {
354         rows[r] = NR_PIXBLOCK_PX(&pb) + r * pb.rs;
355     }
357     nr_pixblock_release(&pb);
359     return num_rows;
362 /**
363  * Hide all items that are not listed in list, recursively, skipping groups and defs.
364  */
365 static void hide_other_items_recursively(SPObject *o, GSList *list, unsigned dkey)
367     if ( SP_IS_ITEM(o)
368          && !SP_IS_DEFS(o)
369          && !SP_IS_ROOT(o)
370          && !SP_IS_GROUP(o)
371          && !g_slist_find(list, o) )
372     {
373         SP_ITEM(o)->invoke_hide(dkey);
374     }
376     // recurse
377     if (!g_slist_find(list, o)) {
378         for ( SPObject *child = o->firstChild() ; child; child = child->getNext() ) {
379             hide_other_items_recursively(child, list, dkey);
380         }
381     }
385 /**
386  * Export the given document as a Portable Network Graphics (PNG) file.
387  *
388  * \return true if succeeded (or if no action was taken), false if an error occurred.
389  */
390 bool sp_export_png_file (SPDocument *doc, gchar const *filename,
391                    double x0, double y0, double x1, double y1,
392                    unsigned long int width, unsigned long int height, double xdpi, double ydpi,
393                    unsigned long bgcolor,
394                    unsigned int (*status) (float, void *),
395                    void *data, bool force_overwrite,
396                    GSList *items_only)
398     return sp_export_png_file(doc, filename, Geom::Rect(Geom::Point(x0,y0),Geom::Point(x1,y1)),
399                               width, height, xdpi, ydpi, bgcolor, status, data, force_overwrite, items_only);
401 bool
402 sp_export_png_file(SPDocument *doc, gchar const *filename,
403                    Geom::Rect const &area,
404                    unsigned long width, unsigned long height, double xdpi, double ydpi,
405                    unsigned long bgcolor,
406                    unsigned (*status)(float, void *),
407                    void *data, bool force_overwrite,
408                    GSList *items_only)
410     g_return_val_if_fail(doc != NULL, false);
411     g_return_val_if_fail(filename != NULL, false);
412     g_return_val_if_fail(width >= 1, false);
413     g_return_val_if_fail(height >= 1, false);
414     g_return_val_if_fail(!area.hasZeroArea(), false);
416     if (!force_overwrite && !sp_ui_overwrite_file(filename)) {
417         /* Remark: We return true so as not to invoke an error dialog in case export is cancelled
418            by the user; currently this is safe because the callers only act when false is returned.
419            If this changes in the future we need better distinction of return types (e.g., use int)
420         */
421         return true;
422     }
424     doc->ensureUpToDate();
426     /* Calculate translation by transforming to document coordinates (flipping Y)*/
427     Geom::Point translation = Geom::Point(-area[Geom::X][0], area[Geom::Y][1] - doc->getHeight());
429     /*  This calculation is only valid when assumed that (x0,y0)= area.corner(0) and (x1,y1) = area.corner(2)
430      * 1) a[0] * x0 + a[2] * y1 + a[4] = 0.0
431      * 2) a[1] * x0 + a[3] * y1 + a[5] = 0.0
432      * 3) a[0] * x1 + a[2] * y1 + a[4] = width
433      * 4) a[1] * x0 + a[3] * y0 + a[5] = height
434      * 5) a[1] = 0.0;
435      * 6) a[2] = 0.0;
436      *
437      * (1,3) a[0] * x1 - a[0] * x0 = width
438      * a[0] = width / (x1 - x0)
439      * (2,4) a[3] * y0 - a[3] * y1 = height
440      * a[3] = height / (y0 - y1)
441      * (1) a[4] = -a[0] * x0
442      * (2) a[5] = -a[3] * y1
443      */
445     Geom::Matrix const affine(Geom::Translate(translation)
446                             * Geom::Scale(width / area.width(),
447                                         height / area.height()));
449     //SP_PRINT_MATRIX("SVG2PNG", &affine);
451     struct SPEBP ebp;
452     ebp.width  = width;
453     ebp.height = height;
454     ebp.r      = NR_RGBA32_R(bgcolor);
455     ebp.g      = NR_RGBA32_G(bgcolor);
456     ebp.b      = NR_RGBA32_B(bgcolor);
457     ebp.a      = NR_RGBA32_A(bgcolor);
459     /* Create new arena */
460     NRArena *const arena = NRArena::create();
461     // export with maximum blur rendering quality
462     nr_arena_set_renderoffscreen(arena);
463     unsigned const dkey = SPItem::display_key_new(1);
465     /* Create ArenaItems and set transform */
466     ebp.root = SP_ITEM(doc->getRoot())->invoke_show(arena, dkey, SP_ITEM_SHOW_DISPLAY);
467     nr_arena_item_set_transform(NR_ARENA_ITEM(ebp.root), affine);
469     // We show all and then hide all items we don't want, instead of showing only requested items,
470     // because that would not work if the shown item references something in defs
471     if (items_only) {
472         hide_other_items_recursively(doc->getRoot(), items_only, dkey);
473     }
475     ebp.status = status;
476     ebp.data   = data;
478     bool write_status;
479     if ((width < 256) || ((width * height) < 32768)) {
480         ebp.px = nr_pixelstore_64K_new(FALSE, 0);
481         ebp.sheight = 65536 / (4 * width);
482         write_status = sp_png_write_rgba_striped(doc, filename, width, height, xdpi, ydpi, sp_export_get_rows, &ebp);
483         nr_pixelstore_64K_free(ebp.px);
484     } else {
485         ebp.sheight = 64;
486         ebp.px = g_try_new(guchar, 4 * ebp.sheight * width);
487         write_status = sp_png_write_rgba_striped(doc, filename, width, height, xdpi, ydpi, sp_export_get_rows, &ebp);
488         g_free(ebp.px);
489     }
491     // Hide items, this releases arenaitem
492     SP_ITEM(doc->getRoot())->invoke_hide(dkey);
494     /* Free arena */
495     nr_object_unref((NRObject *) arena);
497     return write_status;
501 /*
502   Local Variables:
503   mode:c++
504   c-file-style:"stroustrup"
505   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
506   indent-tabs-mode:nil
507   fill-column:99
508   End:
509 */
510 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :