1 #define __NR_ARENA_IMAGE_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <libnr/nr-compose-transform.h>
16 #include "../prefs-utils.h"
17 #include "nr-arena-image.h"
19 int nr_arena_image_x_sample = 1;
20 int nr_arena_image_y_sample = 1;
22 /*
23 * NRArenaCanvasImage
24 *
25 */
27 static void nr_arena_image_class_init (NRArenaImageClass *klass);
28 static void nr_arena_image_init (NRArenaImage *image);
29 static void nr_arena_image_finalize (NRObject *object);
31 static unsigned int nr_arena_image_update (NRArenaItem *item, NRRectL *area, NRGC *gc, unsigned int state, unsigned int reset);
32 static unsigned int nr_arena_image_render (NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
33 static NRArenaItem *nr_arena_image_pick (NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
35 static NRArenaItemClass *parent_class;
37 NRType
38 nr_arena_image_get_type (void)
39 {
40 static NRType type = 0;
41 if (!type) {
42 type = nr_object_register_type (NR_TYPE_ARENA_ITEM,
43 "NRArenaImage",
44 sizeof (NRArenaImageClass),
45 sizeof (NRArenaImage),
46 (void (*) (NRObjectClass *)) nr_arena_image_class_init,
47 (void (*) (NRObject *)) nr_arena_image_init);
48 }
49 return type;
50 }
52 static void
53 nr_arena_image_class_init (NRArenaImageClass *klass)
54 {
55 NRObjectClass *object_class;
56 NRArenaItemClass *item_class;
58 object_class = (NRObjectClass *) klass;
59 item_class = (NRArenaItemClass *) klass;
61 parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
63 object_class->finalize = nr_arena_image_finalize;
64 object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaImage>;
66 item_class->update = nr_arena_image_update;
67 item_class->render = nr_arena_image_render;
68 item_class->pick = nr_arena_image_pick;
69 }
71 static void
72 nr_arena_image_init (NRArenaImage *image)
73 {
74 image->px = NULL;
76 image->pxw = image->pxh = image->pxrs = 0;
77 image->x = image->y = 0.0;
78 image->width = 256.0;
79 image->height = 256.0;
81 nr_matrix_set_identity (&image->grid2px);
82 }
84 static void
85 nr_arena_image_finalize (NRObject *object)
86 {
87 NRArenaImage *image = NR_ARENA_IMAGE (object);
89 image->px = NULL;
91 ((NRObjectClass *) parent_class)->finalize (object);
92 }
94 static unsigned int
95 nr_arena_image_update (NRArenaItem *item, NRRectL *area, NRGC *gc, unsigned int state, unsigned int reset)
96 {
97 NRMatrix grid2px;
99 NRArenaImage *image = NR_ARENA_IMAGE (item);
101 /* Request render old */
102 nr_arena_item_request_render (item);
104 /* Copy affine */
105 nr_matrix_invert (&grid2px, &gc->transform);
106 double hscale, vscale; // todo: replace with NR::scale
107 if (image->px) {
108 hscale = image->pxw / image->width;
109 vscale = image->pxh / image->height;
110 } else {
111 hscale = 1.0;
112 vscale = 1.0;
113 }
115 image->grid2px[0] = grid2px.c[0] * hscale;
116 image->grid2px[2] = grid2px.c[2] * hscale;
117 image->grid2px[4] = grid2px.c[4] * hscale;
118 image->grid2px[1] = grid2px.c[1] * vscale;
119 image->grid2px[3] = grid2px.c[3] * vscale;
120 image->grid2px[5] = grid2px.c[5] * vscale;
122 image->grid2px[4] -= image->x * hscale;
123 image->grid2px[5] -= image->y * vscale;
125 /* Calculate bbox */
126 if (image->px) {
127 NRRect bbox;
129 bbox.x0 = image->x;
130 bbox.y0 = image->y;
131 bbox.x1 = image->x + image->width;
132 bbox.y1 = image->y + image->height;
133 nr_rect_d_matrix_transform (&bbox, &bbox, &gc->transform);
135 item->bbox.x0 = (int) floor (bbox.x0);
136 item->bbox.y0 = (int) floor (bbox.y0);
137 item->bbox.x1 = (int) ceil (bbox.x1);
138 item->bbox.y1 = (int) ceil (bbox.y1);
139 } else {
140 item->bbox.x0 = (int) gc->transform[4];
141 item->bbox.y0 = (int) gc->transform[5];
142 item->bbox.x1 = item->bbox.x0 - 1;
143 item->bbox.y1 = item->bbox.y0 - 1;
144 }
146 nr_arena_item_request_render (item);
148 return NR_ARENA_ITEM_STATE_ALL;
149 }
151 #define FBITS 12
152 #define b2i (image->grid2px)
154 static unsigned int
155 nr_arena_image_render (NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
156 {
157 nr_arena_image_x_sample = prefs_get_int_attribute ("options.bitmapoversample", "value", 1);
158 nr_arena_image_y_sample = nr_arena_image_x_sample;
160 NRArenaImage *image = NR_ARENA_IMAGE (item);
162 if (!image->px) return item->state;
164 guint32 Falpha = item->opacity;
165 if (Falpha < 1) return item->state;
167 unsigned char * dpx = NR_PIXBLOCK_PX (pb);
168 const int drs = pb->rs;
169 const int dw = pb->area.x1 - pb->area.x0;
170 const int dh = pb->area.y1 - pb->area.y0;
172 unsigned char * spx = image->px;
173 const int srs = image->pxrs;
174 const int sw = image->pxw;
175 const int sh = image->pxh;
177 NR::Matrix d2s;
179 d2s[0] = b2i[0];
180 d2s[1] = b2i[1];
181 d2s[2] = b2i[2];
182 d2s[3] = b2i[3];
183 d2s[4] = b2i[0] * pb->area.x0 + b2i[2] * pb->area.y0 + b2i[4];
184 d2s[5] = b2i[1] * pb->area.x0 + b2i[3] * pb->area.y0 + b2i[5];
186 if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8) {
187 /* fixme: This is not implemented yet (Lauris) */
188 /* nr_R8G8B8_R8G8B8_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample); */
189 } else if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
190 nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample);
191 } else if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) {
193 //FIXME: The _N_N_N_ version gives a gray border around images, see bug 906376
194 // This mode is only used when exporting, screen rendering always has _P_P_P_, so I decided to simply replace it for now
195 // Feel free to propose a better fix
197 //nr_R8G8B8A8_N_R8G8B8A8_N_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample);
198 nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample);
199 }
201 pb->empty = FALSE;
203 return item->state;
204 }
206 static NRArenaItem *
207 nr_arena_image_pick (NRArenaItem *item, NR::Point p, double delta, unsigned int sticky)
208 {
209 NRArenaImage *image = NR_ARENA_IMAGE (item);
211 if (!image->px) return NULL;
213 unsigned char * const pixels = image->px;
214 const int width = image->pxw;
215 const int height = image->pxh;
216 const int rowstride = image->pxrs;
217 NR::Point tp = p * image->grid2px;
218 const int ix = (int)(tp[NR::X]);
219 const int iy = (int)(tp[NR::Y]);
221 if ((ix < 0) || (iy < 0) || (ix >= width) || (iy >= height))
222 return NULL;
224 unsigned char *pix_ptr = pixels + iy * rowstride + ix * 4;
225 // is the alpha not transparent?
226 return (pix_ptr[3] > 0) ? item : NULL;
227 }
229 /* Utility */
231 void
232 nr_arena_image_set_pixels (NRArenaImage *image, const unsigned char *px, unsigned int pxw, unsigned int pxh, unsigned int pxrs)
233 {
234 nr_return_if_fail (image != NULL);
235 nr_return_if_fail (NR_IS_ARENA_IMAGE (image));
237 image->px = (unsigned char *) px;
238 image->pxw = pxw;
239 image->pxh = pxh;
240 image->pxrs = pxrs;
242 nr_arena_item_request_update (NR_ARENA_ITEM (image), NR_ARENA_ITEM_STATE_ALL, FALSE);
243 }
245 void
246 nr_arena_image_set_geometry (NRArenaImage *image, double x, double y, double width, double height)
247 {
248 nr_return_if_fail (image != NULL);
249 nr_return_if_fail (NR_IS_ARENA_IMAGE (image));
251 image->x = x;
252 image->y = y;
253 image->width = width;
254 image->height = height;
256 nr_arena_item_request_update (NR_ARENA_ITEM (image), NR_ARENA_ITEM_STATE_ALL, FALSE);
257 }