I was recommended to separate this question from another one. Here is the original:
How to change white background for black
After some image processes i get a binary image, but the borders are so hard so i apply a gaussian filter to get a soft one. The result is a grayscale image. I need to apply this image as a mask for another one, none of them has an alpha channel so mix of colors need to be done without this value. I am using Marvin Framework to do the job but Marvin does not has a plugin for it, so i code one, this is it:
@Override public void process(MarvinImage _imageIn, MarvinImage _imageOut, MarvinAttributes attrOut, MarvinImageMask _mask, boolean previewMode) {
MarvinImage mask = _imageIn;
MarvinImage image = _imageOut;
for(int y=0; y<mask.getHeight(); y++){
for(int x=0; x<mask.getWidth(); x++){
//ya que está en grayscale, los 3 valores son los mismos
int r1 = mask.getIntComponent0(x, y);
int g1 = mask.getIntComponent1(x, y);
int b1 = mask.getIntComponent2(x, y);
int r2 = image.getIntComponent0(x, y);
int g2 = image.getIntComponent1(x, y);
int b2 = image.getIntComponent2(x, y);
//al color de salida, le asignamos la luminicencia de la imagen mascara
int r = 0, g = 0, b = 0;
if(r1 > 0 || r2 > 0){
r = r1*r2/Math.max(r1, r2);
}
if(g1 > 0 || g2 > 0){
g = g1*g2/Math.max(g1, g2);
}
if(b1 > 0 || b2 > 0){
b = b1*b2/Math.max(b1, b2);
}
image.setIntColor(x, y, r, g, b);
}
}
}
But this code has a little bug that i couldn't resolve. When the rgb image has a white, the color result is not combined very well, it does not get darker. What i am looking for is something one can achieve using gimp, where there is a 2 layer image, the bottom one is the rgb image and the upper one is the grayscale mask, then in this layer we use the function color to alpha, using the white color as a target. The result is the next:
Whit the algorithm is the next:
The difference is pretty obious. Here are the two original images for testing:
(source: imgsafe.org)