Exemplo n.º 1
0
    void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const
    {
        const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8);
        RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8);
        float r, g, b, gray;
        while (nPixels > 0) {

            r = SCALE_TO_FLOAT(src->red);
            g = SCALE_TO_FLOAT(src->green);
            b = SCALE_TO_FLOAT(src->blue);

            // http://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/
            switch(m_type) {
            case 0: // lightness
            {
                gray = (qMax(qMax(r, g), b) + qMin(qMin(r, g), b)) / 2;
                break;
            }
            case 1: // luminosity BT 709
            {
                gray = r * 0.2126 + g * 0.7152 + b * 0.0722;
                break;

            }

            case 2: // luminosity BT 601
            {
                gray = r * 0.299 + g * 0.587 + b * 0.114;
                break;

            }
            case 3: // average
            {
                gray = (r + g + b) / 2;
                break;
            }
            case 4: // min
            {
                gray = qMin(qMin(r, g), b);
                break;
            }
            case 5: // min
            {
                gray = qMax(qMax(r, g), b);
                break;
            }

            default:
                gray = 0;
            }
            dst->red = SCALE_FROM_FLOAT(gray);
            dst->green = SCALE_FROM_FLOAT(gray);
            dst->blue = SCALE_FROM_FLOAT(gray);
            dst->alpha = src->alpha;

            --nPixels;
            ++src;
            ++dst;
        }
    }
void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const
{
    KisColorBalanceMath *bal = new KisColorBalanceMath();
    const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8);
    RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8);
    float value_red, value_green, value_blue, hue, saturation, lightness;

    while(nPixels > 0) {

        float red = SCALE_TO_FLOAT(src->red);
        float green = SCALE_TO_FLOAT(src->green);
        float blue = SCALE_TO_FLOAT(src->blue);
        RGBToHSL(red, green, blue, &hue, &saturation, &lightness);

        value_red = bal->colorBalanceTransform(red, lightness, m_cyan_shadows, m_cyan_midtones, m_cyan_highlights);
        value_green = bal->colorBalanceTransform(green, lightness, m_magenta_shadows, m_magenta_midtones, m_magenta_highlights);
        value_blue = bal->colorBalanceTransform(blue, lightness, m_yellow_shadows, m_yellow_midtones, m_yellow_highlights);

        if(m_preserve_luminosity)
        {
            float h1, s1, l1, h2, s2, l2;
            RGBToHSL(SCALE_TO_FLOAT(src->red), SCALE_TO_FLOAT(src->green), SCALE_TO_FLOAT(src->blue), &h1, &s1, &l1);
            RGBToHSL(value_red, value_green, value_blue, &h2, &s2, &l2);
            HSLToRGB(h2, s2, l1, &value_red, &value_green, &value_blue);
        }
        dst->red = SCALE_FROM_FLOAT(value_red);
        dst->green = SCALE_FROM_FLOAT(value_green);
        dst->blue = SCALE_FROM_FLOAT(value_blue);
        dst->alpha = src->alpha;

        --nPixels;
        ++src;
        ++dst;
    }
}
Exemplo n.º 3
0
    void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const
    {

        //if (m_model="RGBA" || m_colorize) {
        /*It'd be nice to have LCH automatically selector for LAB in the future, but I don't know how to select LAB 
         * */
            const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8);
            RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8);
            float h, s, v, r, g, b;
            qreal lumaR, lumaG, lumaB;
            //Default to rec 709 when there's no coefficients given//
            if (m_lumaRed<=0 || m_lumaGreen<=0 || m_lumaBlue<=0) {
                lumaR   = 0.2126;
                lumaG   = 0.7152;
                lumaB   = 0.0722;
            } else {
                lumaR   = m_lumaRed;
                lumaG   = m_lumaGreen;
                lumaB   = m_lumaBlue;
            }
            while (nPixels > 0) {

                if (m_colorize) {
                    h = m_adj_h * 360;
                    if (h >= 360.0) h = 0;

                    s = m_adj_s;

                    r = SCALE_TO_FLOAT(src->red);
                    g = SCALE_TO_FLOAT(src->green);
                    b = SCALE_TO_FLOAT(src->blue);

                    float luminance = r * lumaR + g * lumaG + b * lumaB;

                    if (m_adj_v > 0) {
                        luminance *= (1.0 - m_adj_v);
                        luminance += 1.0 - (1.0 - m_adj_v);
                    }
                    else if (m_adj_v < 0 ){
                        luminance *= (m_adj_v + 1.0);
                    }
                    v = luminance;
                    HSLToRGB(h, s, v, &r, &g, &b);

                }
                else {

                    if (m_type == 0) {
                        RGBToHSV(SCALE_TO_FLOAT(src->red), SCALE_TO_FLOAT(src->green), SCALE_TO_FLOAT(src->blue), &h, &s, &v);
                        h += m_adj_h * 180;
                        if (h > 360) h -= 360;
                        if (h < 0) h += 360;
                        s += m_adj_s;
                        v += m_adj_v;
                        HSVToRGB(h, s, v, &r, &g, &b);
                    } else if (m_type == 1) {

                        RGBToHSL(SCALE_TO_FLOAT(src->red), SCALE_TO_FLOAT(src->green), SCALE_TO_FLOAT(src->blue), &h, &s, &v);

                        h += m_adj_h * 180;
                        if (h > 360) h -= 360;
                        if (h < 0) h += 360;

                        s *= (m_adj_s + 1.0);
                        if (s < 0.0) s = 0.0;
                        if (s > 1.0) s = 1.0;

                        if (m_adj_v < 0)
                            v *= (m_adj_v + 1.0);
                        else
                            v += (m_adj_v * (1.0 - v));


                        HSLToRGB(h, s, v, &r, &g, &b);
                    } else if (m_type == 2){

                        qreal red = SCALE_TO_FLOAT(src->red);
                        qreal green = SCALE_TO_FLOAT(src->green);
                        qreal blue = SCALE_TO_FLOAT(src->blue);
                        qreal hue, sat, intensity;
                        RGBToHCI(red, green, blue, &hue, &sat, &intensity);

                        hue *=360.0;
                        hue += m_adj_h * 180;
                        //if (intensity+m_adj_v>1.0){hue+=180.0;}
                        if (hue < 0) hue += 360;
                        hue = fmod(hue, 360.0);

                        sat *= (m_adj_s + 1.0);
                        //sat = qBound(0.0, sat, 1.0);
                        
                        intensity += (m_adj_v);

                        HCIToRGB(hue/360.0, sat, intensity, &red, &green, &blue);

                        r = red;
                        g = green;
                        b = blue;
                    } else if (m_type == 3){

                        qreal red = SCALE_TO_FLOAT(src->red);
                        qreal green = SCALE_TO_FLOAT(src->green);
                        qreal blue = SCALE_TO_FLOAT(src->blue);
                        qreal hue, sat, luma;
                        RGBToHCY(red, green, blue, &hue, &sat, &luma, lumaR, lumaG, lumaB);

                        hue *=360.0;
                        hue += m_adj_h * 180;
                        //if (luma+m_adj_v>1.0){hue+=180.0;}
                        if (hue < 0) hue += 360;
                        hue = fmod(hue, 360.0);

                        sat *= (m_adj_s + 1.0);
                        //sat = qBound(0.0, sat, 1.0);

                        luma += m_adj_v;


                        HCYToRGB(hue/360.0, sat, luma, &red, &green, &blue, lumaR, lumaG, lumaB);
                        r = red;
                        g = green;
                        b = blue;
                        
                    } else if (m_type == 4){

                        qreal red = SCALE_TO_FLOAT(src->red);
                        qreal green = SCALE_TO_FLOAT(src->green);
                        qreal blue = SCALE_TO_FLOAT(src->blue);
                        qreal y, cb, cr;
                        RGBToYUV(red, green, blue, &y, &cb, &cr, lumaR, lumaG, lumaB);

                        cb *= (m_adj_h + 1.0);
                        //cb = qBound(0.0, cb, 1.0);

                        cr *= (m_adj_s + 1.0);
                        //cr = qBound(0.0, cr, 1.0);

                        
                        
                        y += (m_adj_v);


                        YUVToRGB(y, cb, cr, &red, &green, &blue, lumaR, lumaG, lumaB);
                        r = red;
                        g = green;
                        b = blue;
                    }
                }

                clamp< _channel_type_ >(&r, &g, &b);
                dst->red = SCALE_FROM_FLOAT(r);
                dst->green = SCALE_FROM_FLOAT(g);
                dst->blue = SCALE_FROM_FLOAT(b);
                dst->alpha = src->alpha;

                --nPixels;
                ++src;
                ++dst;
            }
        /*} else if (m_model="LABA"){
            const LABPixel* src = reinterpret_cast<const LABPixel*>(srcU8);
            LABPixel* dst = reinterpret_cast<LABPixel*>(dstU8);
            qreal lightness = SCALE_TO_FLOAT(src->L);
            qreal a = SCALE_TO_FLOAT(src->a);
            qreal b = SCALE_TO_FLOAT(src->b);
            qreal L, C, H;
            
            while (nPixels > 0) {
                if (m_type = 4) {
                    a *= (m_adj_h + 1.0);
                    a = qBound(0.0, a, 1.0);

                    b *= (m_adj_s + 1.0);
                    b = qBound(0.0, b, 1.0);

                    if (m_adj_v < 0)
                        lightness *= (m_adj_v + 1.0);
                    else
                        lightness += (m_adj_v * (1.0 - lightness));
                } else {//lch
                    LABToLCH(lightness, a, b, &L, &C, &H);
                    H *=360;
                    H += m_adj_h * 180;
                    if (H > 360) h -= 360;
                    if (H < 0) h += 360;
                    C += m_adj_s;
                    C = qBound(0.0,C,1.0);
                    L += m_adj_v;
                    L = qBound(0.0,L,1.0);
                    LCHToLAB(L, C, H/360.0, &lightness, &a, &b);
                }
                clamp< _channel_type_ >(&lightness, &a, &b);
                dst->L = SCALE_FROM_FLOAT(lightness);
                dst->a = SCALE_FROM_FLOAT(a);
                dst->b = SCALE_FROM_FLOAT(b);
                dst->alpha = src->alpha;

                --nPixels;
                ++src;
                ++dst;
                }
        }*/
    }