static void sp_gradient_image_update (SPGradientImage *image) { GtkAllocation *allocation; if (!image->px) return; allocation = &((GtkWidget *) image)->allocation; if (image->gradient) { nr_render_checkerboard_rgb (image->px, allocation->width, VBLOCK, 3 * allocation->width, 0, 0); sp_gradient_render_vector_block_rgb (image->gradient, image->px, allocation->width, VBLOCK, 3 * allocation->width, 0, allocation->width, TRUE); } else { NRPixBlock pb; nr_pixblock_setup_extern (&pb, NR_PIXBLOCK_MODE_R8G8B8, 0, 0, allocation->width, VBLOCK, image->px, 3 * allocation->width, TRUE, FALSE); nr_pixblock_render_gray_noise (&pb, NULL); nr_pixblock_release (&pb); } if (GTK_WIDGET_DRAWABLE (image)) { gtk_widget_queue_draw (GTK_WIDGET (image)); } }
FilterSlot::~FilterSlot() { for (int i = 0 ; i < _slot_count ; i++) { if (_slot[i]) { nr_pixblock_release(_slot[i]); delete _slot[i]; } } delete[] _slot; delete[] _slot_number; }
void nr_gdk_draw_rgba32_solid (GdkDrawable *drawable, GdkGC *gc, gint x, gint y, gint w, gint h, guint32 rgba) { NRPixBlock pb; nr_pixblock_setup_fast (&pb, NR_PIXBLOCK_MODE_R8G8B8A8N, 0, 0, w, h, FALSE); nr_render_rgba32_rgb (NR_PIXBLOCK_PX (&pb), w, h, pb.rs, x, y, rgba); gdk_draw_rgb_image (drawable, gc, x, y, w, h, GDK_RGB_DITHER_MAX, NR_PIXBLOCK_PX (&pb), pb.rs); nr_pixblock_release (&pb); }
void nr_gdk_draw_gray_garbage (GdkDrawable *drawable, GdkGC *gc, gint x, gint y, gint w, gint h) { gint xx, yy; for (yy = y; yy < y + h; yy += 64) { for (xx = x; xx < x + w; xx += 64) { NRPixBlock pb; gint ex, ey; ex = MIN (xx + 64, x + w); ey = MIN (yy + 64, y + h); nr_pixblock_setup_fast (&pb, NR_PIXBLOCK_MODE_R8G8B8, xx, yy, ex, ey, FALSE); nr_pixblock_render_gray_noise (&pb, NULL); gdk_draw_rgb_image (drawable, gc, xx, yy, ex - xx, ey - yy, GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs); nr_pixblock_release (&pb); } } }
void nr_pixblock_draw_line_rgba32 (NRPixBlock *d, long x0, long y0, long x1, long y1, short /*first*/, unsigned long rgba) { long deltax, deltay, xinc1, xinc2, yinc1, yinc2; long den, num, numadd, numpixels; long x, y, curpixel; /* Pixblock */ int dbpp; NRPixBlock spb; unsigned char *spx; if (x1 >= x0) { deltax = x1 - x0; xinc1 = 1; xinc2 = 1; } else { deltax = x0 - x1; xinc1 = -1; xinc2 = -1; } if (y1 >= y0) { deltay = y1 - y0; yinc1 = 1; yinc2 = 1; } else { deltay = y0 - y1; yinc1 = -1; yinc2 = -1; } if (deltax >= deltay) { xinc1 = 0; yinc2 = 0; den = deltax; num = deltax / 2; numadd = deltay; numpixels = deltax; } else { xinc2 = 0; yinc1 = 0; den = deltay; num = deltay / 2; numadd = deltax; numpixels = deltay; } /* We can be quite sure 1x1 pixblock is TINY */ nr_pixblock_setup_fast (&spb, NR_PIXBLOCK_MODE_R8G8B8A8N, 0, 0, 1, 1, 0); spb.empty = 0; spx = NR_PIXBLOCK_PX (&spb); spx[0] = NR_RGBA32_R (rgba); spx[1] = NR_RGBA32_G (rgba); spx[2] = NR_RGBA32_B (rgba); spx[3] = NR_RGBA32_A (rgba); dbpp = NR_PIXBLOCK_BPP (d); x = x0; y = y0; for (curpixel = 0; curpixel <= numpixels; curpixel++) { if ((x >= d->area.x0) && (y >= d->area.y0) && (x < d->area.x1) && (y < d->area.y1)) { nr_compose_pixblock_pixblock_pixel (d, NR_PIXBLOCK_PX (d) + (y - d->area.y0) * d->rs + (x - d->area.x0) * dbpp, &spb, spx); } num += numadd; if (num >= den) { num -= den; x += xinc1; y += yinc1; } x += xinc2; y += yinc2; } nr_pixblock_release (&spb); }
static void sp_canvas_arena_render (SPCanvasItem *item, SPCanvasBuf *buf) { SPCanvasArena *arena; gint bw, bh, sw, sh; gint x, y; arena = SP_CANVAS_ARENA (item); nr_arena_item_invoke_update (arena->root, NULL, &arena->gc, NR_ARENA_ITEM_STATE_BBOX | NR_ARENA_ITEM_STATE_RENDER, NR_ARENA_ITEM_STATE_NONE); if (buf->is_bg) { sp_canvas_clear_buffer (buf); buf->is_bg = FALSE; buf->is_buf = TRUE; } bw = buf->rect.x1 - buf->rect.x0; bh = buf->rect.y1 - buf->rect.y0; if ((bw < 1) || (bh < 1)) return; /* 65536 is max cached buffer and we need 4 channels */ if (bw * bh < 16384) { /* We can go with single buffer */ sw = bw; sh = bh; } else if (bw <= 2048) { /* Go with row buffer */ sw = bw; sh = 16384 / bw; } else if (bh <= 2048) { /* Go with column buffer */ sw = 16384 / bh; sh = bh; } else { sw = 128; sh = 128; } /* fixme: RGB transformed bitmap blit is not implemented (Lauris) */ /* And even if it would be, unless it uses MMX there is little reason to go RGB */ #define STRICT_RGBA for (y = buf->rect.y0; y < buf->rect.y1; y += sh) { for (x = buf->rect.x0; x < buf->rect.x1; x += sw) { NRRectL area; #ifdef STRICT_RGBA NRPixBlock pb; #endif NRPixBlock cb; area.x0 = x; area.y0 = y; area.x1 = MIN (x + sw, buf->rect.x1); area.y1 = MIN (y + sh, buf->rect.y1); #ifdef STRICT_RGBA nr_pixblock_setup_fast (&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, area.x0, area.y0, area.x1, area.y1, TRUE); /* fixme: */ pb.empty = FALSE; #endif nr_pixblock_setup_extern (&cb, NR_PIXBLOCK_MODE_R8G8B8, area.x0, area.y0, area.x1, area.y1, buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + 3 * (x - buf->rect.x0), buf->buf_rowstride, FALSE, FALSE); #ifdef STRICT_RGBA nr_arena_item_invoke_render (arena->root, &area, &pb, 0); nr_blit_pixblock_pixblock (&cb, &pb); nr_pixblock_release (&pb); #else nr_arena_item_invoke_render (arena->root, &area, &cb, 0); #endif nr_pixblock_release (&cb); } } }
void FilterSlot::set(int slot_nr, NRPixBlock *pb) { /* Unnamed slot is for saving filter primitive results, when parameter * 'result' is not set. Only the filter immediately after this one * can access unnamed results, so we don't have to worry about overwriting * previous results in filter chain. On the other hand, we may not * overwrite any other image with this one, because they might be * accessed later on. */ int index = ((slot_nr != NR_FILTER_SLOT_NOT_SET) ? _get_index(slot_nr) : _get_index(NR_FILTER_UNNAMED_SLOT)); assert(index >= 0); // Unnamed slot is only for Inkscape::Filters::FilterSlot internal use. assert(slot_nr != NR_FILTER_UNNAMED_SLOT); assert(slot_nr == NR_FILTER_SLOT_NOT_SET ||_slot_number[index] == slot_nr); if (slot_nr == NR_FILTER_SOURCEGRAPHIC || slot_nr == NR_FILTER_BACKGROUNDIMAGE) { Geom::Matrix trans = units.get_matrix_display2pb(); if (fabs(trans[1]) > 1e-6 || fabs(trans[2]) > 1e-6) { NRPixBlock *trans_pb = new NRPixBlock; int x0 = pb->area.x0; int y0 = pb->area.y0; int x1 = pb->area.x1; int y1 = pb->area.y1; int min_x = _min4(trans[0] * x0 + trans[2] * y0 + trans[4], trans[0] * x0 + trans[2] * y1 + trans[4], trans[0] * x1 + trans[2] * y0 + trans[4], trans[0] * x1 + trans[2] * y1 + trans[4]); int max_x = _max4(trans[0] * x0 + trans[2] * y0 + trans[4], trans[0] * x0 + trans[2] * y1 + trans[4], trans[0] * x1 + trans[2] * y0 + trans[4], trans[0] * x1 + trans[2] * y1 + trans[4]); int min_y = _min4(trans[1] * x0 + trans[3] * y0 + trans[5], trans[1] * x0 + trans[3] * y1 + trans[5], trans[1] * x1 + trans[3] * y0 + trans[5], trans[1] * x1 + trans[3] * y1 + trans[5]); int max_y = _max4(trans[1] * x0 + trans[3] * y0 + trans[5], trans[1] * x0 + trans[3] * y1 + trans[5], trans[1] * x1 + trans[3] * y0 + trans[5], trans[1] * x1 + trans[3] * y1 + trans[5]); nr_pixblock_setup_fast(trans_pb, pb->mode, min_x, min_y, max_x, max_y, true); if (trans_pb->size != NR_PIXBLOCK_SIZE_TINY && trans_pb->data.px == NULL) { /* TODO: this gets hit occasionally. Worst case scenario: * images are exported in horizontal stripes. One stripe * is not too high, but can get thousands of pixels wide. * Rotate this 45 degrees -> _huge_ image */ g_warning("Memory allocation failed in Inkscape::Filters::FilterSlot::set (transform)"); return; } if (filterquality == FILTER_QUALITY_BEST) { NR::transform_bicubic(trans_pb, pb, trans); } else { NR::transform_nearest(trans_pb, pb, trans); } nr_pixblock_release(pb); delete pb; pb = trans_pb; } else if (fabs(trans[0] - 1) > 1e-6 || fabs(trans[3] - 1) > 1e-6) { NRPixBlock *trans_pb = new NRPixBlock; int x0 = pb->area.x0; int y0 = pb->area.y0; int x1 = pb->area.x1; int y1 = pb->area.y1; int min_x = _min2(trans[0] * x0 + trans[4], trans[0] * x1 + trans[4]); int max_x = _max2(trans[0] * x0 + trans[4], trans[0] * x1 + trans[4]); int min_y = _min2(trans[3] * y0 + trans[5], trans[3] * y1 + trans[5]); int max_y = _max2(trans[3] * y0 + trans[5], trans[3] * y1 + trans[5]); nr_pixblock_setup_fast(trans_pb, pb->mode, min_x, min_y, max_x, max_y, true); if (trans_pb->size != NR_PIXBLOCK_SIZE_TINY && trans_pb->data.px == NULL) { g_warning("Memory allocation failed in Inkscape::Filters::FilterSlot::set (scaling)"); return; } NR::scale_bicubic(trans_pb, pb); nr_pixblock_release(pb); delete pb; pb = trans_pb; } } if(_slot[index]) { nr_pixblock_release(_slot[index]); delete _slot[index]; } _slot[index] = pb; _last_out = index; }
static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event) { SPDropperContext *dc = (SPDropperContext *) event_context; int ret = FALSE; SPDesktop *desktop = event_context->desktop; Inkscape::Preferences *prefs = Inkscape::Preferences::get(); int pick = prefs->getInt("/tools/dropper/pick", SP_DROPPER_PICK_VISIBLE); bool setalpha = prefs->getBool("/tools/dropper/setalpha", true); switch (event->type) { case GDK_BUTTON_PRESS: if (event->button.button == 1 && !event_context->space_panning) { dc->centre = Geom::Point(event->button.x, event->button.y); dc->dragging = TRUE; ret = TRUE; } break; case GDK_MOTION_NOTIFY: if (event->motion.state & GDK_BUTTON2_MASK) { // pass on middle-drag ret = FALSE; break; } else if (!event_context->space_panning) { // otherwise, constantly calculate color no matter is any button pressed or not double rw = 0.0; double W(0), R(0), G(0), B(0), A(0); if (dc->dragging) { // calculate average // radius rw = std::min(Geom::L2(Geom::Point(event->button.x, event->button.y) - dc->centre), 400.0); if (rw == 0) { // happens sometimes, little idea why... break; } Geom::Point const cd = desktop->w2d(dc->centre); Geom::Matrix const w2dt = desktop->w2d(); const double scale = rw * w2dt.descrim(); Geom::Matrix const sm( Geom::Scale(scale, scale) * Geom::Translate(cd) ); sp_canvas_item_affine_absolute(dc->area, sm); sp_canvas_item_show(dc->area); /* Get buffer */ const int x0 = (int) floor(dc->centre[Geom::X] - rw); const int y0 = (int) floor(dc->centre[Geom::Y] - rw); const int x1 = (int) ceil(dc->centre[Geom::X] + rw); const int y1 = (int) ceil(dc->centre[Geom::Y] + rw); if ((x1 > x0) && (y1 > y0)) { NRPixBlock pb; nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE); /* fixme: (Lauris) */ sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb); for (int y = y0; y < y1; y++) { const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs; for (int x = x0; x < x1; x++) { const double dx = x - dc->centre[Geom::X]; const double dy = y - dc->centre[Geom::Y]; const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw)); W += w; R += w * s[0]; G += w * s[1]; B += w * s[2]; A += w * s[3]; s += 4; } } nr_pixblock_release(&pb); R = (R + 0.001) / (255.0 * W); G = (G + 0.001) / (255.0 * W); B = (B + 0.001) / (255.0 * W); A = (A + 0.001) / (255.0 * W); R = CLAMP(R, 0.0, 1.0); G = CLAMP(G, 0.0, 1.0); B = CLAMP(B, 0.0, 1.0); A = CLAMP(A, 0.0, 1.0); } } else { // pick single pixel NRPixBlock pb; int x = (int) floor(event->button.x); int y = (int) floor(event->button.y); nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE); sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb); const unsigned char *s = NR_PIXBLOCK_PX(&pb); R = s[0] / 255.0; G = s[1] / 255.0; B = s[2] / 255.0; A = s[3] / 255.0; } if (pick == SP_DROPPER_PICK_VISIBLE) { // compose with page color guint32 bg = sp_desktop_namedview(desktop)->pagecolor; R = R + (SP_RGBA32_R_F(bg)) * (1 - A); G = G + (SP_RGBA32_G_F(bg)) * (1 - A); B = B + (SP_RGBA32_B_F(bg)) * (1 - A); A = 1.0; } else { // un-premultiply color channels if (A > 0) { R /= A; G /= A; B /= A; } } if (fabs(A) < 1e-4) { A = 0; // suppress exponentials, CSS does not allow that } // remember color dc->R = R; dc->G = G; dc->B = B; dc->alpha = A; // status message double alpha_to_set = setalpha? dc->alpha : 1.0; guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set); gchar c[64]; sp_svg_write_color(c, sizeof(c), c32); // alpha of color under cursor, to show in the statusbar // locale-sensitive printf is OK, since this goes to the UI, not into SVG gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set); // where the color is picked, to show in the statusbar gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor")); // message, to show in the statusbar const gchar *message = dc->dragging ? _("<b>Release mouse</b> to set color.") : _("<b>Click</b> to set fill, <b>Shift+click</b> to set stroke; <b>drag</b> to average color in area; with <b>Alt</b> to pick inverse color; <b>Ctrl+C</b> to copy the color under mouse to clipboard"); event_context->defaultMessageContext()->setF( Inkscape::NORMAL_MESSAGE, "<b>%s%s</b>%s. %s", c, (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha, where, message ); g_free(where); g_free(alpha); ret = TRUE; } break; case GDK_BUTTON_RELEASE: if (event->button.button == 1 && !event_context->space_panning) { sp_canvas_item_hide(dc->area); dc->dragging = FALSE; double alpha_to_set = setalpha? dc->alpha : 1.0; // do the actual color setting sp_desktop_set_color(desktop, (event->button.state & GDK_MOD1_MASK)? ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set), false, !(event->button.state & GDK_SHIFT_MASK)); // REJON: set aux. toolbar input to hex color! if (!(sp_desktop_selection(desktop)->isEmpty())) { sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER, _("Set picked color")); } ret = TRUE; } break; case GDK_KEY_PRESS: switch (get_group0_keyval(&event->key)) { case GDK_Up: case GDK_Down: case GDK_KP_Up: case GDK_KP_Down: // prevent the zoom field from activation if (!MOD__CTRL_ONLY) { ret = TRUE; } break; case GDK_Escape: sp_desktop_selection(desktop)->clear(); default: break; } break; default: break; } if (!ret) { if (((SPEventContextClass *) parent_class)->root_handler) { ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event); } } return ret; }
GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const Geom::Rect& dbox, unsigned psize) { NRGC gc(NULL); Geom::Matrix t(Geom::Scale(scale_factor, scale_factor)); nr_arena_item_set_transform(root, t); gc.transform.setIdentity(); nr_arena_item_invoke_update( root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE ); /* Item integer bbox in points */ NRRectL ibox; ibox.x0 = (int) floor(scale_factor * dbox.min()[Geom::X] + 0.5); ibox.y0 = (int) floor(scale_factor * dbox.min()[Geom::Y] + 0.5); ibox.x1 = (int) floor(scale_factor * dbox.max()[Geom::X] + 0.5); ibox.y1 = (int) floor(scale_factor * dbox.max()[Geom::Y] + 0.5); /* Find visible area */ int width = ibox.x1 - ibox.x0; int height = ibox.y1 - ibox.y0; int dx = psize; int dy = psize; dx = (dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative dy = (dy - height)/2; NRRectL area; area.x0 = ibox.x0 - dx; area.y0 = ibox.y0 - dy; area.x1 = area.x0 + psize; area.y1 = area.y0 + psize; /* Actual renderable area */ NRRectL ua; ua.x0 = std::max(ibox.x0, area.x0); ua.y0 = std::max(ibox.y0, area.y0); ua.x1 = std::min(ibox.x1, area.x1); ua.y1 = std::min(ibox.y1, area.y1); /* Set up pixblock */ guchar *px = g_new(guchar, 4 * psize * psize); memset(px, 0x00, 4 * psize * psize); /* Render */ NRPixBlock B; nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N, ua.x0, ua.y0, ua.x1, ua.y1, px + 4 * psize * (ua.y0 - area.y0) + 4 * (ua.x0 - area.x0), 4 * psize, FALSE, FALSE ); nr_arena_item_invoke_render(NULL, root, &ua, &B, NR_ARENA_ITEM_RENDER_NO_CACHE ); nr_pixblock_release(&B); GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, psize, psize, psize * 4, (GdkPixbufDestroyNotify)g_free, NULL); return pixbuf; }