Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Saturation and ColorFilter to image simultaneously

The following function changes contrast and brightness in the picture successfully.

Bitmap bmp;
ImageView alteredImageView;
...
public void drawAlteredImage(float contr,float bright) {
    Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();

    cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });

    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Matrix matrix = new Matrix();
    canvas.drawBitmap(bmp, matrix, paint);
    alteredImageView.setImageBitmap(alteredBitmap);
}

But when I added setSaturation method to ColorMatrix the contrast and brightness altering ceased to work. The code:

public void drawAlteredImage(float contr,float bright,float satur) {
    Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();

    cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });
    cm.setSaturation(satur);
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Matrix matrix = new Matrix();
    canvas.drawBitmap(bmp, matrix, paint);
    alteredImageView.setImageBitmap(alteredBitmap);
}

Only the saturation effect is applying in this case. Why does this problem happening? How can I fix it?

like image 902
gabin Avatar asked Dec 28 '22 14:12

gabin


2 Answers

The setSaturation(...) method replaces all the values in the matrix. You can either create a matrix that combines both operations, or do the operations with two separate drawBitmap(...) calls.

Try:

cm.setSaturation(satur);
final float m[] = cm.getArray();
final float c = contr;
cm.set(new float[] { 
        m[ 0] * c, m[ 1] * c, m[ 2] * c, m[ 3] * c, m[ 4] * c + bright, 
        m[ 5] * c, m[ 6] * c, m[ 7] * c, m[ 8] * c, m[ 9] * c + bright, 
        m[10] * c, m[11] * c, m[12] * c, m[13] * c, m[14] * c + bright, 
        m[15]    , m[16]    , m[17]    , m[18]    , m[19] }); 
like image 167
Mats Avatar answered Mar 24 '23 03:03

Mats


Another option is to create a new ColorMatrix, set the saturation in this new ColorMatrix, then call postConcat on the Brightness/Contrast ColorMatrix. Something like:

 public void drawAlteredImage(float contr,float bright,float satur) {
        Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        Canvas canvas = new Canvas(alteredBitmap);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();

        cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });

        ColorMatrix saturationCM = new ColorMatrix();
        saturationCM.setSaturation(satur);
        cm.postConcat(saturationCM);

        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        Matrix matrix = new Matrix();
        canvas.drawBitmap(bmp, matrix, paint);
        alteredImageView.setImageBitmap(alteredBitmap);
}
like image 32
Diego Bezerra Avatar answered Mar 24 '23 03:03

Diego Bezerra