Code

hide item instead of unreffing, and unref arena to prevent memory leak
[inkscape.git] / src / helper / pixbuf-ops.cpp
1 #define __SP_PIXBUF_OPS_C__
3 /*
4  * Helpers for SPItem -> gdk_pixbuf related stuff
5  *
6  * Authors:
7  *   John Cliff <simarilius@yahoo.com>
8  *
9  * Copyright (C) 2008 John Cliff
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include <interface.h>
19 #include <libnr/nr-pixops.h>
20 #include <glib.h>
21 #include <glib/gmessages.h>
22 #include <png.h>
23 #include "png-write.h"
24 #include <display/nr-arena-item.h>
25 #include <display/nr-arena.h>
26 #include <document.h>
27 #include <sp-item.h>
28 #include <sp-root.h>
29 #include <sp-defs.h>
30 #include "unit-constants.h"
32 #include "libnr/nr-matrix-translate-ops.h"
33 #include "libnr/nr-scale-ops.h"
34 #include "libnr/nr-scale-translate-ops.h"
35 #include "libnr/nr-translate-matrix-ops.h"
36 #include "libnr/nr-translate-scale-ops.h"
38 #include "pixbuf-ops.h"
40 /**
41  * Hide all items that are not listed in list, recursively, skipping groups and defs.
42  */
43 static void
44 hide_other_items_recursively(SPObject *o, GSList *list, unsigned dkey)
45 {
46     if ( SP_IS_ITEM(o)
47          && !SP_IS_DEFS(o)
48          && !SP_IS_ROOT(o)
49          && !SP_IS_GROUP(o)
50          && !g_slist_find(list, o) )
51     {
52         sp_item_invoke_hide(SP_ITEM(o), dkey);
53     }
55     // recurse
56     if (!g_slist_find(list, o)) {
57         for (SPObject *child = sp_object_first_child(o) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
58             hide_other_items_recursively(child, list, dkey);
59         }
60     }
61 }
64 // The following is a mutation of the flood fill code, the marker preview, and random other samplings.
65 // The dpi settings dont do anything yet, but I want them to, and was wanting to keep reasonably close
66 // to the call for the interface to the png writing.
68 bool
69 sp_export_jpg_file(SPDocument *doc, gchar const *filename,
70                    double x0, double y0, double x1, double y1,
71                    unsigned width, unsigned height, double xdpi, double ydpi,
72                    unsigned long bgcolor, double quality,GSList *items)
74 {
77       GdkPixbuf* pixbuf;
78       pixbuf = sp_generate_internal_bitmap(doc, filename, x0, y0, x1, y1,
79                                          width, height, xdpi, ydpi,
80                                          bgcolor, items );
83      gchar c[32];
84      g_snprintf(c, 32, "%f", quality);
85      gboolean saved = gdk_pixbuf_save (pixbuf, filename, "jpeg", NULL, "quality", c, NULL);
86      g_free(c);
87      gdk_pixbuf_unref (pixbuf);
88      if (saved) return true;
89      else return false;
90 }
92 GdkPixbuf*
93 sp_generate_internal_bitmap(SPDocument *doc, gchar const */*filename*/,
94                             double x0, double y0, double x1, double y1,
95                             unsigned width, unsigned height, double xdpi, double ydpi,
96                             unsigned long bgcolor,
97                             GSList *items_only)
99 {
102      /* Create new arena */
103      NRArena *arena = NRArena::create();
104      unsigned dkey = sp_item_display_key_new(1);
106      sp_document_ensure_up_to_date (doc);
108      Geom::Rect screen=Geom::Rect(Geom::Point(x0,y0), Geom::Point(x1, y1));
110      double padding = 1.0;
112      Geom::Point origin(screen.min()[Geom::X],
113                       sp_document_height(doc) - screen[Geom::Y].extent() - screen.min()[Geom::Y]);
115      origin[Geom::X] = origin[Geom::X] + (screen[Geom::X].extent() * ((1 - padding) / 2));
116      origin[Geom::Y] = origin[Geom::Y] + (screen[Geom::Y].extent() * ((1 - padding) / 2));
118      Geom::Scale scale( (xdpi / PX_PER_IN),   (ydpi / PX_PER_IN));
119      Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
121      /* Create ArenaItems and set transform */
122      NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(doc)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
123      nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
125      NRGC gc(NULL);
126      gc.transform.set_identity();
128      // We show all and then hide all items we don't want, instead of showing only requested items,
129      // because that would not work if the shown item references something in defs
130      if (items_only) {
131          hide_other_items_recursively(sp_document_root(doc), items_only, dkey);
132      }
134      NRRectL final_bbox;
135      final_bbox.x0 = 0;
136      final_bbox.y0 = 0;//row;
137      final_bbox.x1 = width;
138      final_bbox.y1 = height;//row + num_rows;
140      nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
142      guchar *px = g_new(guchar, 4 * width * height);
144      NRPixBlock B;
145      nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
146                                final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
147                                px, 4 * width, FALSE, FALSE );
149      unsigned char dtc[4];
150      dtc[0] = NR_RGBA32_R(bgcolor);
151      dtc[1] = NR_RGBA32_G(bgcolor);
152      dtc[2] = NR_RGBA32_B(bgcolor);
153      dtc[3] = NR_RGBA32_A(bgcolor);
155      for (unsigned int fy = 0; fy < height; fy++) {
156          guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
157          for (unsigned int fx = 0; fx < width; fx++) {
158              for (int i = 0; i < 4; i++) {
159                  *p++ = dtc[i];
160              }
161          }
162      }
164      nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
166      GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB,
167                                           TRUE,
168                                           8, width, height, width * 4,
169                                           (GdkPixbufDestroyNotify)g_free,
170                                           NULL);
172      sp_item_invoke_hide (SP_ITEM(sp_document_root(doc)), dkey);
173      nr_object_unref((NRObject *) arena);
175 //    gdk_pixbuf_save (pixbuf, "C:\\temp\\internal.jpg", "jpeg", NULL, "quality","100", NULL);
177     return pixbuf;
180 /*
181   Local Variables:
182   mode:c++
183   c-file-style:"stroustrup"
184   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
185   indent-tabs-mode:nil
186   fill-column:99
187   End:
188 */
189 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :