1087
1087
gr->color = g_new(guchar, 4 * NCOLORS);
1090
for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1091
guint32 color = gr->vector.stops[i].color.toRGBA32( gr->vector.stops[i].opacity );
1092
gint r0 = (color >> 24) & 0xff;
1093
gint g0 = (color >> 16) & 0xff;
1094
gint b0 = (color >> 8) & 0xff;
1095
gint a0 = color & 0xff;
1096
color = gr->vector.stops[i + 1].color.toRGBA32( gr->vector.stops[i + 1].opacity );
1097
gint r1 = (color >> 24) & 0xff;
1098
gint g1 = (color >> 16) & 0xff;
1099
gint b1 = (color >> 8) & 0xff;
1100
gint a1 = color & 0xff;
1101
gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1102
gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1104
gint dr = ((r1 - r0) << 16) / (o1 - o0);
1105
gint dg = ((g1 - g0) << 16) / (o1 - o0);
1106
gint db = ((b1 - b0) << 16) / (o1 - o0);
1107
gint da = ((a1 - a0) << 16) / (o1 - o0);
1112
for (int j = o0; j < o1 + 1; j++) {
1113
gr->color[4 * j] = r >> 16;
1114
gr->color[4 * j + 1] = g >> 16;
1115
gr->color[4 * j + 2] = b >> 16;
1116
gr->color[4 * j + 3] = a >> 16;
1090
// This assumes that gr->vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1091
// This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1092
// Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1094
double remainder[4] = {0,0,0,0};
1095
double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1096
switch(gr->spread) {
1097
case SP_GRADIENT_SPREAD_PAD:
1098
remainder[0] = 0.5*gr->vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1099
remainder[1] = 0.5*gr->vector.stops[0].color.v.c[1];
1100
remainder[2] = 0.5*gr->vector.stops[0].color.v.c[2];
1101
remainder[3] = 0.5*gr->vector.stops[0].opacity;
1102
remainder_for_end[0] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[0]; // Half of the first cell uses the color of the last stop
1103
remainder_for_end[1] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[1];
1104
remainder_for_end[2] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[2];
1105
remainder_for_end[3] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].opacity;
1107
case SP_GRADIENT_SPREAD_REFLECT:
1108
case SP_GRADIENT_SPREAD_REPEAT:
1109
// These two are handled differently, see below.
1112
g_error("Spread type not supported!");
1114
for (unsigned int i = 0; i < gr->vector.stops.size() - 1; i++) {
1115
double r0 = gr->vector.stops[i].color.v.c[0];
1116
double g0 = gr->vector.stops[i].color.v.c[1];
1117
double b0 = gr->vector.stops[i].color.v.c[2];
1118
double a0 = gr->vector.stops[i].opacity;
1119
double r1 = gr->vector.stops[i+1].color.v.c[0];
1120
double g1 = gr->vector.stops[i+1].color.v.c[1];
1121
double b1 = gr->vector.stops[i+1].color.v.c[2];
1122
double a1 = gr->vector.stops[i+1].opacity;
1123
double o0 = gr->vector.stops[i].offset * (NCOLORS-1);
1124
double o1 = gr->vector.stops[i + 1].offset * (NCOLORS-1);
1125
unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1126
unsigned int oe = (unsigned int) floor(o1+.5); // These need to be computed the same to ensure that ob will be covered by the next interval if oe==ob
1129
// Simple case, this interval starts and stops within one cell
1130
// The contribution of this interval is:
1131
// (o1-o0)*(c(o0)+c(o1))/2
1132
// = (o1-o0)*(c0+c1)/2
1133
double dt = 0.5*(o1-o0);
1134
remainder[0] += dt*(r0 + r1);
1135
remainder[1] += dt*(g0 + g1);
1136
remainder[2] += dt*(b0 + b1);
1137
remainder[3] += dt*(a0 + a1);
1139
// First compute colors for the cells which are fully covered by the current interval.
1140
// The prefiltered values are equal to the midpoint of each cell here.
1141
// f = (j-o0)/(o1-o0)
1142
// = j*(1/(o1-o0)) - o0/(o1-o0)
1143
double f = (ob-o0) / (o1-o0);
1144
double df = 1. / (o1-o0);
1145
for (unsigned int j = ob+1; j < oe; j++) {
1147
gr->color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1148
gr->color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1149
gr->color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1150
gr->color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1153
// Now handle the beginning
1154
// The contribution of the last point is already in remainder.
1155
// The contribution of this point is:
1156
// (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1157
// = (ob+.5-o0)*c((o0+ob+.5)/2)
1158
// = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1159
// = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1160
double dt = ob+.5-o0;
1162
if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REFLECT) {
1163
// The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1164
gr->color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1165
gr->color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1166
gr->color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1167
gr->color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1168
} else if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REPEAT) {
1169
// The first cell is the same as the last cell, so save whatever is in the second half here and deal with the rest later.
1170
remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1171
remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1172
remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1173
remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1175
// The first half of the cell was already in remainder.
1176
gr->color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1177
gr->color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1178
gr->color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1179
gr->color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1182
// Now handle the end, which should end up in remainder
1183
// The contribution of this point is:
1184
// (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1185
// = (o1-oe+.5)*c((o1+oe-.5)/2)
1186
// = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1188
f = (0.5*(o1+oe-.5)-o0)*df;
1189
remainder[0] = dt*(r0 + f*(r1-r0));
1190
remainder[1] = dt*(g0 + f*(g1-g0));
1191
remainder[2] = dt*(b0 + f*(b1-b0));
1192
remainder[3] = dt*(a0 + f*(a1-a0));
1195
switch(gr->spread) {
1196
case SP_GRADIENT_SPREAD_PAD:
1197
gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1198
gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1199
gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1200
gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1202
case SP_GRADIENT_SPREAD_REFLECT:
1203
// The second half is the same as the first half, so multiply by 2.
1204
gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1205
gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1206
gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1207
gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1209
case SP_GRADIENT_SPREAD_REPEAT:
1210
// The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1211
gr->color[0] = gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1212
gr->color[1] = gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1213
gr->color[2] = gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1214
gr->color[3] = gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1255
sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1348
sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1257
1350
if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1258
return ( NR::scale(bbox.dimensions())
1259
* NR::translate(bbox.min())
1351
return ( Geom::Scale(bbox.dimensions())
1352
* Geom::Translate(bbox.min())
1353
* Geom::Matrix(ctm) );
1267
sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1360
sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1269
1362
if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1270
1363
return ( gr->gradientTransform
1271
* NR::scale(bbox.dimensions())
1272
* NR::translate(bbox.min())
1364
* Geom::Scale(bbox.dimensions())
1365
* Geom::Translate(bbox.min())
1366
* Geom::Matrix(ctm) );
1275
1368
return gr->gradientTransform * ctm;
1280
sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1281
NR::Rect const &bbox, NR::Matrix const &gs2d)
1373
sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1374
Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1283
gr->gradientTransform = gs2d / ctm;
1376
gr->gradientTransform = gs2d * ctm.inverse();
1284
1377
if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1285
1378
gr->gradientTransform = ( gr->gradientTransform
1286
/ NR::translate(bbox.min())
1287
/ NR::scale(bbox.dimensions()) );
1379
* Geom::Translate(-bbox.min())
1380
* Geom::Scale(bbox.dimensions()).inverse() );
1289
1382
gr->gradientTransform_set = TRUE;
1494
1586
* or something similar. Originally I had 1023.9999 here - not sure
1495
1587
* whether we have really to cut out ceil int (Lauris).
1497
NR::Matrix color2norm(NR::identity());
1498
NR::Matrix color2px;
1589
Geom::Matrix color2norm(Geom::identity());
1590
Geom::Matrix color2px;
1499
1591
if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1500
NR::Matrix norm2pos(NR::identity());
1592
Geom::Matrix norm2pos(Geom::identity());
1502
1594
/* BBox to user coordinate system */
1503
NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1595
Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1505
NR::Matrix color2pos = color2norm * norm2pos;
1506
NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1507
NR::Matrix color2user = color2tpos * bbox2user;
1597
Geom::Matrix color2pos = color2norm * norm2pos;
1598
Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1599
Geom::Matrix color2user = color2tpos * bbox2user;
1508
1600
color2px = color2user * full_transform;
1511
1603
/* Problem: What to do, if we have mixed lengths and percentages? */
1512
1604
/* Currently we do ignore percentages at all, but that is not good (lauris) */
1514
NR::Matrix norm2pos(NR::identity());
1515
NR::Matrix color2pos = color2norm * norm2pos;
1516
NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1606
Geom::Matrix norm2pos(Geom::identity());
1607
Geom::Matrix color2pos = color2norm * norm2pos;
1608
Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1517
1609
color2px = color2tpos * full_transform;
1522
color2px.copyto(&v2px);
1524
nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
1612
// TODO: remove color2px_nr after converting to 2geom
1613
NR::Matrix color2px_nr = from_2geom(color2px);
1614
nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &color2px_nr,
1525
1615
lg->x1.computed, lg->y1.computed,
1526
1616
lg->x2.computed, lg->y2.computed);