示例#1
0
Surface pp::choose(cinder::Surface& imageA, cinder::Surface& imageB, cinder::Surface& errorA, cinder::Surface& secondWeight, float threshold)
{
    // for each target pixel find one in source!
    float width = std::min(imageA.getWidth(), imageB.getWidth());
    float height = std::min(imageB.getHeight(), imageB.getHeight());

    Surface result(width, height, false);
    ivec2 v(0, 0);
    for (v.y = 0; v.y < height; v.y++)
        for (v.x = 0; v.x < width; v.x++)
        {
            // is errorA a local maximum?
            bool swap = true;
            Color8u eA = errorA.getPixel(v);
            float errA = (eA.r-127)*(eA.r-127) + (eA.g-127)*(eA.g-127) + (eA.b-127)*(eA.b-127);
            float alternative = secondWeight.getPixel(v).r;
            if (std::sqrt(errA)*alternative <= threshold*(3*127*127))
                swap = false;
            else
                for (int i = 0; i < 3 && swap; i++)
                    for (int j = 0; j < 3 && swap; j++)
                        if (i != 1 || j != 1)
                        {
                            ivec2 offset(i-1, j-1);
                            Color8u eO = errorA.getPixel(v+offset);
                            float errOther = (eO.r-127)*(eO.r-127) + (eO.g-127)*(eO.g-127) + (eO.b-127)*(eO.b-127);
                            if (errOther >= errA)
                                swap = false;
                        }

            if (swap)
                result.setPixel(v, imageB.getPixel(v));
            else
                result.setPixel(v, imageA.getPixel(v));
        }

    return result;
}
示例#2
0
void pp::getColors(cinder::Surface& source, Palette& result)
{
    result.clear();
    ivec2 v(0, 0);
    int width = source.getWidth();
    int height = source.getHeight();
    for (v.x = 0; v.x < width; v.x++)
        for (v.y = 0; v.y < height; v.y++)
        {
            Color8u pxl = source.getPixel(v);
            bool found = false;
            for (Palette::iterator it = result.begin(); it != result.end(); it++)
                if (distance2(Color8u(it->r, it->g, it->b), pxl) == 0)  // found
                    found = true;
            if (!found)
                result.push_back(pxl);
        }
}