Is there a way to implement “Duotone” effect in Java?
A good example of what I’d like to do is here or here
I guess BandCombineOp might help.
To me I should convert it to grays first and then apply smth like threshold effect.
But I didn’t manage to achieve good output.
Also I don’t understand how can I set up colors for this effect.
float[][] grayMatrix = new float[][]
{
new float[] {0.3f, 0.3f, 0.3f},
new float[] {0.3f, 0.3f, 0.3f},
new float[] {0.3f, 0.3f, 0.3f},
};
float[][] duoToneMatrix = new float[][]
{
new float[] {0.1f, 0.1f, 0.1f},
new float[] {0.2f, 0.2f, 0.2f},
new float[] {0.1f, 0.1f, 0.1f},
};
BufferedImage src = ImageIO.read(new File("X:\\photoshop_image_effects.jpg"));
WritableRaster srcRaster = src.getRaster();
// make it gray
BandCombineOp bco = new BandCombineOp(grayMatrix, null);
WritableRaster dstRaster = bco.createCompatibleDestRaster(srcRaster);
bco.filter(srcRaster, dstRaster);
// apply duotone
BandCombineOp duoToneBco = new BandCombineOp(duoToneMatrix, null);
WritableRaster dstRaster2 = bco.createCompatibleDestRaster(dstRaster);
duoToneBco.filter(dstRaster, dstRaster2);
BufferedImage result = new BufferedImage(src.getColorModel(), dstRaster2, src.getColorModel().isAlphaPremultiplied(), null);
ImageIO.write(result, "png", new File("X:\\result_duotone.png"));
My output 
From what I can tell you are trying to change the colouring of an image without changing its luminosity. Note the difference from luminance.
Regardless of whether you are aiming for luminance or luminosity, your problem boils down to varying the relative contributions of B, G, and R without changing their weighted sum. Your first matrix converts to greyscale by setting B,G,R to the same value and only slightly changing their luminance (.3+.3+.3 = .9). To use luminosity instead use
Then you want to change their relative weighting without changing their weighted sum. First, note that since after greyscale conversion your B,G,R values are the same you could replace your matrix with
and it would be equivalent. To conserve luminance you need to choose 3 factors such that their sum is 1. Those three factors can be applied in the duoTone Matrix. The larger a factor is, the more tinted the image will be with that colour. To preserve luminosity you need 3 factors fb,fg,fr such that
You can choose your factors fb, fg, fr to find the tint of your choosing.
Also, note that you can do this with one matrix. Just combine the two matrices you already have.
just compute the product of duoToneMatrix and greyMatrix (in that order) and process in one step.