Code

BUG: #273767: crash on ...; resolved crash on several pixbuf operations
[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      GdkPixbuf* pixbuf = NULL;
103      /* Create new arena for offscreen rendering*/
104      NRArena *arena = NRArena::create();
105      nr_arena_set_renderoffscreen(arena);
106      unsigned dkey = sp_item_display_key_new(1);
108      sp_document_ensure_up_to_date (doc);
110      Geom::Rect screen=Geom::Rect(Geom::Point(x0,y0), Geom::Point(x1, y1));
112      double padding = 1.0;
114      Geom::Point origin(screen.min()[Geom::X],
115                       sp_document_height(doc) - screen[Geom::Y].extent() - screen.min()[Geom::Y]);
117      origin[Geom::X] = origin[Geom::X] + (screen[Geom::X].extent() * ((1 - padding) / 2));
118      origin[Geom::Y] = origin[Geom::Y] + (screen[Geom::Y].extent() * ((1 - padding) / 2));
120      Geom::Scale scale( (xdpi / PX_PER_IN),   (ydpi / PX_PER_IN));
121      Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
123      /* Create ArenaItems and set transform */
124      NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(doc)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
125      nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
127      NRGC gc(NULL);
128      gc.transform.setIdentity();
130      // We show all and then hide all items we don't want, instead of showing only requested items,
131      // because that would not work if the shown item references something in defs
132      if (items_only) {
133          hide_other_items_recursively(sp_document_root(doc), items_only, dkey);
134      }
136      NRRectL final_bbox;
137      final_bbox.x0 = 0;
138      final_bbox.y0 = 0;//row;
139      final_bbox.x1 = width;
140      final_bbox.y1 = height;//row + num_rows;
142      nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
144      guchar *px = g_try_new(guchar, 4L * width * height);
146     if(px != NULL)
147     {
149          NRPixBlock B;
150         g_warning("sp_generate_internal_bitmap: nr_pixblock_setup_extern.");
151          nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
152                                    final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
153                                    px, 4 * width, FALSE, FALSE );
155          unsigned char dtc[4];
156          dtc[0] = NR_RGBA32_R(bgcolor);
157          dtc[1] = NR_RGBA32_G(bgcolor);
158          dtc[2] = NR_RGBA32_B(bgcolor);
159          dtc[3] = NR_RGBA32_A(bgcolor);
161          for (unsigned int fy = 0; fy < height; fy++) {
162              guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
163              for (unsigned int fx = 0; fx < width; fx++) {
164                  for (int i = 0; i < 4; i++) {
165                      *p++ = dtc[i];
166                  }
167              }
168          }
169      
171          nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
173          pixbuf = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB,
174                                               TRUE,
175                                               8, width, height, width * 4,
176                                               (GdkPixbufDestroyNotify)g_free,
177                                               NULL);
178     }
179     else
180     {
181         g_warning("sp_generate_internal_bitmap: not enough memory to create pixel buffer. Need %ld.", 4L * width * height);
182     }
183      sp_item_invoke_hide (SP_ITEM(sp_document_root(doc)), dkey);
184      nr_object_unref((NRObject *) arena);
186 //    gdk_pixbuf_save (pixbuf, "C:\\temp\\internal.jpg", "jpeg", NULL, "quality","100", NULL);
188     return pixbuf;
191 /*
192   Local Variables:
193   mode:c++
194   c-file-style:"stroustrup"
195   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
196   indent-tabs-mode:nil
197   fill-column:99
198   End:
199 */
200 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :