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-use.h>
30 #include <sp-defs.h>
31 #include "unit-constants.h"
33 #include "libnr/nr-matrix-translate-ops.h"
34 #include "libnr/nr-scale-ops.h"
35 #include "libnr/nr-scale-translate-ops.h"
36 #include "libnr/nr-translate-matrix-ops.h"
37 #include "libnr/nr-translate-scale-ops.h"
39 #include "pixbuf-ops.h"
41 /**
42 * Hide all items that are not listed in list, recursively, skipping groups and defs.
43 */
44 static void
45 hide_other_items_recursively(SPObject *o, GSList *list, unsigned dkey)
46 {
47 if ( SP_IS_ITEM(o)
48 && !SP_IS_DEFS(o)
49 && !SP_IS_ROOT(o)
50 && !SP_IS_GROUP(o)
51 && !SP_IS_USE(o)
52 && !g_slist_find(list, o) )
53 {
54 sp_item_invoke_hide(SP_ITEM(o), dkey);
55 }
57 // recurse
58 if (!g_slist_find(list, o)) {
59 for (SPObject *child = sp_object_first_child(o) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
60 hide_other_items_recursively(child, list, dkey);
61 }
62 }
63 }
66 // The following is a mutation of the flood fill code, the marker preview, and random other samplings.
67 // The dpi settings dont do anything yet, but I want them to, and was wanting to keep reasonably close
68 // to the call for the interface to the png writing.
70 bool
71 sp_export_jpg_file(SPDocument *doc, gchar const *filename,
72 double x0, double y0, double x1, double y1,
73 unsigned width, unsigned height, double xdpi, double ydpi,
74 unsigned long bgcolor, double quality,GSList *items)
76 {
79 GdkPixbuf* pixbuf;
80 pixbuf = sp_generate_internal_bitmap(doc, filename, x0, y0, x1, y1,
81 width, height, xdpi, ydpi,
82 bgcolor, items );
85 gchar c[32];
86 g_snprintf(c, 32, "%f", quality);
87 gboolean saved = gdk_pixbuf_save (pixbuf, filename, "jpeg", NULL, "quality", c, NULL);
88 g_free(c);
89 gdk_pixbuf_unref (pixbuf);
90 if (saved) return true;
91 else return false;
92 }
94 GdkPixbuf*
95 sp_generate_internal_bitmap(SPDocument *doc, gchar const */*filename*/,
96 double x0, double y0, double x1, double y1,
97 unsigned width, unsigned height, double xdpi, double ydpi,
98 unsigned long bgcolor,
99 GSList *items_only)
101 {
103 GdkPixbuf* pixbuf = NULL;
104 /* Create new arena for offscreen rendering*/
105 NRArena *arena = NRArena::create();
106 nr_arena_set_renderoffscreen(arena);
107 unsigned dkey = sp_item_display_key_new(1);
109 sp_document_ensure_up_to_date (doc);
111 Geom::Rect screen=Geom::Rect(Geom::Point(x0,y0), Geom::Point(x1, y1));
113 double padding = 1.0;
115 Geom::Point origin(screen.min()[Geom::X],
116 sp_document_height(doc) - screen[Geom::Y].extent() - screen.min()[Geom::Y]);
118 origin[Geom::X] = origin[Geom::X] + (screen[Geom::X].extent() * ((1 - padding) / 2));
119 origin[Geom::Y] = origin[Geom::Y] + (screen[Geom::Y].extent() * ((1 - padding) / 2));
121 Geom::Scale scale( (xdpi / PX_PER_IN), (ydpi / PX_PER_IN));
122 Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
124 /* Create ArenaItems and set transform */
125 NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(doc)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
126 nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
128 NRGC gc(NULL);
129 gc.transform.setIdentity();
131 // We show all and then hide all items we don't want, instead of showing only requested items,
132 // because that would not work if the shown item references something in defs
133 if (items_only) {
134 hide_other_items_recursively(sp_document_root(doc), items_only, dkey);
135 }
137 NRRectL final_bbox;
138 final_bbox.x0 = 0;
139 final_bbox.y0 = 0;//row;
140 final_bbox.x1 = width;
141 final_bbox.y1 = height;//row + num_rows;
143 nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
145 guchar *px = NULL;
146 guint64 size = 4L * (guint64)width * (guint64)height;
147 if(size < (guint64)G_MAXSIZE) {
148 // g_try_new is limited to g_size type which is defined as unisgned int. Need to test for very large nubers
149 px = g_try_new(guchar, size);
150 }
152 if(px != NULL)
153 {
155 NRPixBlock B;
156 //g_warning("sp_generate_internal_bitmap: nr_pixblock_setup_extern.");
157 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
158 final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
159 px, 4 * width, FALSE, FALSE );
161 unsigned char dtc[4];
162 dtc[0] = NR_RGBA32_R(bgcolor);
163 dtc[1] = NR_RGBA32_G(bgcolor);
164 dtc[2] = NR_RGBA32_B(bgcolor);
165 dtc[3] = NR_RGBA32_A(bgcolor);
167 for (gsize fy = 0; fy < height; fy++) {
168 guchar *p = NR_PIXBLOCK_PX(&B) + fy * (gsize)B.rs;
169 for (unsigned int fx = 0; fx < width; fx++) {
170 for (int i = 0; i < 4; i++) {
171 *p++ = dtc[i];
172 }
173 }
174 }
177 nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
179 pixbuf = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB,
180 TRUE,
181 8, width, height, width * 4,
182 (GdkPixbufDestroyNotify)g_free,
183 NULL);
184 }
185 else
186 {
187 g_warning("sp_generate_internal_bitmap: not enough memory to create pixel buffer. Need %lld.", size);
188 }
189 sp_item_invoke_hide (SP_ITEM(sp_document_root(doc)), dkey);
190 nr_object_unref((NRObject *) arena);
192 // gdk_pixbuf_save (pixbuf, "C:\\temp\\internal.jpg", "jpeg", NULL, "quality","100", NULL);
194 return pixbuf;
195 }
197 /*
198 Local Variables:
199 mode:c++
200 c-file-style:"stroustrup"
201 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
202 indent-tabs-mode:nil
203 fill-column:99
204 End:
205 */
206 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :