Logo Search packages:      
Sourcecode: inkscape version File versions  Download package

void SPGradient::ensureColors (  ) [inherited]

Ensures that color array is populated

The gradient's color array is newly created and set up from vector.

Todo:
Where is the memory freed?

Definition at line 1127 of file sp-gradient.cpp.

References SPGradient::color, NCOLORS, SPGradient::rebuildVector(), SPGradient::spread, and SPGradient::vector.

Referenced by SPGradientImpl::modified(), SPRGPainter::painter_new(), SPLGPainter::painter_new(), sp_gradient_render_vector_line_rgba(), sp_lg_fill(), and sp_rg_fill().

{
    if (!vector.built) {
        rebuildVector();
    }
    g_return_if_fail(!vector.stops.empty());

    /// \todo Where is the memory freed?
    if (!color) {
        color = g_new(guchar, 4 * NCOLORS);
    }

    // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
    // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
    // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".

    double remainder[4] = {0,0,0,0};
    double remainder_for_end[4] = {0,0,0,0}; // Used at the end
    switch(spread) {
    case SP_GRADIENT_SPREAD_PAD:
        remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
        remainder[1] = 0.5*vector.stops[0].color.v.c[1];
        remainder[2] = 0.5*vector.stops[0].color.v.c[2];
        remainder[3] = 0.5*vector.stops[0].opacity;
        remainder_for_end[0] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[0]; // Half of the first cell uses the color of the last stop
        remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
        remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
        remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
        break;
    case SP_GRADIENT_SPREAD_REFLECT:
    case SP_GRADIENT_SPREAD_REPEAT:
        // These two are handled differently, see below.
        break;
    default:
        g_error("Spread type not supported!");
    };
    for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
        double r0 = vector.stops[i].color.v.c[0];
        double g0 = vector.stops[i].color.v.c[1];
        double b0 = vector.stops[i].color.v.c[2];
        double a0 = vector.stops[i].opacity;
        double r1 = vector.stops[i+1].color.v.c[0];
        double g1 = vector.stops[i+1].color.v.c[1];
        double b1 = vector.stops[i+1].color.v.c[2];
        double a1 = vector.stops[i+1].opacity;
        double o0 = vector.stops[i].offset * (NCOLORS-1);
        double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
        unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
        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

        if (oe == ob) {
            // Simple case, this interval starts and stops within one cell
            // The contribution of this interval is:
            //    (o1-o0)*(c(o0)+c(o1))/2
            //  = (o1-o0)*(c0+c1)/2
            double dt = 0.5*(o1-o0);
            remainder[0] += dt*(r0 + r1);
            remainder[1] += dt*(g0 + g1);
            remainder[2] += dt*(b0 + b1);
            remainder[3] += dt*(a0 + a1);
        } else {
            // First compute colors for the cells which are fully covered by the current interval.
            // The prefiltered values are equal to the midpoint of each cell here.
            //  f = (j-o0)/(o1-o0)
            //    = j*(1/(o1-o0)) - o0/(o1-o0)
            double f = (ob-o0) / (o1-o0);
            double df = 1. / (o1-o0);
            for (unsigned int j = ob+1; j < oe; j++) {
                f += df;
                color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
                color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
                color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
                color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
            }

            // Now handle the beginning
            // The contribution of the last point is already in remainder.
            // The contribution of this point is:
            //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
            //  = (ob+.5-o0)*c((o0+ob+.5)/2)
            //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
            //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
            double dt = ob+.5-o0;
            f = 0.5*dt*df;
            if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
                // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
                color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
                color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
                color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
                color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
            } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
                // 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.
                remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
                remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
                remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
                remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
            } else {
                // The first half of the cell was already in remainder.
                color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
                color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
                color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
                color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
            }

            // Now handle the end, which should end up in remainder
            // The contribution of this point is:
            //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
            //  = (o1-oe+.5)*c((o1+oe-.5)/2)
            //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
            dt = o1-oe+.5;
            f = (0.5*(o1+oe-.5)-o0)*df;
            remainder[0] = dt*(r0 + f*(r1-r0));
            remainder[1] = dt*(g0 + f*(g1-g0));
            remainder[2] = dt*(b0 + f*(b1-b0));
            remainder[3] = dt*(a0 + f*(a1-a0));
        }
    }
    switch(spread) {
    case SP_GRADIENT_SPREAD_PAD:
        color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
        color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
        color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
        color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
        break;
    case SP_GRADIENT_SPREAD_REFLECT:
        // The second half is the same as the first half, so multiply by 2.
        color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
        color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
        color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
        color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
        break;
    case SP_GRADIENT_SPREAD_REPEAT:
        // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
        color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
        color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
        color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
        color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
        break;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index