benji_r
benji_r

Reputation: 555

java image color formulas

I know the java code for grayscale is this( 0.2126 * red + 0.7152 * green + 0.0722 * blue( I was wondering if anyone knows how I can find more variety of coloring formulas, like if i want to make the picture old fashion way, more orange, make it brighter, or darker ... sharper and so on

 int pixel = image.getRGB(j, i);
 int red = (pixel) & 0xff;
 int green = (pixel >> 8) & 0xff;
 int blue = (pixel >> 16) & 0xff;
 int newPixel = (int) (0.2126 * red  + 0.7152 * green + 0.0722 * blue);
                    image1.setRGB(j, i, newPixel);

Upvotes: 1

Views: 1955

Answers (3)

posdef
posdef

Reputation: 6532

The old fashion way you mention is called "sepia" effect. Take a look at this question particularly this answer which points out to the following code snippet (note that I did not write this code, just helping out in finding answers to your question)

/**
*
* @param img Image to modify
* @param sepiaIntensity From 0-255, 30 produces nice results
* @throws Exception
*/
public static void applySepiaFilter(BufferedImage img, int
sepiaIntensity) throws Exception
{
// Play around with this. 20 works well and was recommended
// by another developer. 0 produces a grey image
int sepiaDepth = 20;

int w = img.getWidth();
int h = img.getHeight();

WritableRaster raster = img.getRaster();

// We need 3 integers (for R,G,B color values) per pixel.
int[] pixels = new int[w*h*3];
raster.getPixels(0, 0, w, h, pixels);

// Process 3 ints at a time for each pixel. Each pixel has 3 RGB
colors in array
for (int i=0;i<pixels.length; i+=3)
{
int r = pixels[i];
int g = pixels[i+1];
int b = pixels[i+2];

int gry = (r + g + b) / 3;
r = g = b = gry;
r = r + (sepiaDepth * 2);
g = g + sepiaDepth;

if (r>255) r=255;
if (g>255) g=255;
if (b>255) b=255;

// Darken blue color to increase sepia effect
b-= sepiaIntensity;

// normalize if out of bounds
if (b<0) b=0;
if (b>255) b=255;

pixels[i] = r;
pixels[i+1]= g;
pixels[i+2] = b;
}
raster.setPixels(0, 0, w, h, pixels);
}

Upvotes: 3

Gabriel Archanjo
Gabriel Archanjo

Reputation: 4597

You can manipulate the proportion between the color channels in order to change the scene "atmosphere". The images below were created using the ColorChannel plug-in.

enter image description here

The algorithm source code is presented below. The method getAttribute() gets the parameters (red,gree,blue) passed by the user. The methods getIntComponent0, getIntComponent1 and getIntComponent2 get each color channel (red, gree and blue). The method setIntColor sets back the value of each channel.

@Override
public void process
(
    MarvinImage imageIn,
    MarvinImage imageOut,
    MarvinAttributes attrOut,
    MarvinImageMask mask,
    boolean preview
) {

    int vr = (Integer)getAttribute("red");
    int vg = (Integer)getAttribute("green");
    int vb = (Integer)getAttribute("blue");

    double mr = 1+Math.abs((vr/100.0)*2.5);
    double mg = 1+Math.abs((vg/100.0)*2.5);
    double mb = 1+Math.abs((vb/100.0)*2.5);

    mr = (vr > 0? mr : 1.0/mr);
    mg = (vg > 0? mg : 1.0/mg);
    mb = (vb > 0? mb : 1.0/mb);

    int red,green,blue;
    for(int y=0; y<imageIn.getHeight(); y++){
        for(int x=0; x<imageIn.getWidth(); x++){
            red = imageIn.getIntComponent0(x, y);
            green = imageIn.getIntComponent1(x, y);
            blue = imageIn.getIntComponent2(x, y);

            red     = (int)Math.min(red * mr, 255);
            green   = (int)Math.min(green * mg, 255);
            blue    = (int)Math.min(blue * mb, 255);

            imageOut.setIntColor(x, y, 255, red, green, blue);
        }
    }
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533442

I would just play with the numbers.

more orange,

more red and a little more green (red + green = yellow)

brighter

increase all the factors

darker

decrease all the factors

sharper

This is specific filter which compare surrounding pixels to find edges. It not just a matter of playing with the colours.

BTW: You should add capping of the values. i.e. Math.min(255, Math.max(0, value))

Upvotes: 1

Related Questions