public Image contrast(Image im, int width, int height, int contrast)
    {
        int pix[] = JIPTUtilities.getPixelArray(im);
        int new_pix[] = new int[width*height];
        int mean_luminance = 0;

        mean_luminance = getMeanLuminance(pix);

        for(int i = 0; i < pix.length; i++)
        {
            //////// Get pixels ///////////////////
            int alpha = (pix[i] >> 24) & 0xff;
	        int red   = (pix[i] >> 16) & 0xff;
	        int green = (pix[i] >>  8) & 0xff;
	        int blue  = (pix[i]      ) & 0xff;

            ////// Adjust for contrast /////////
            if(contrast <= 0)
            {
                red   = red   + (int) ((red   - mean_luminance)*(contrast/100.0));
                green = green + (int) ((green - mean_luminance)*(contrast/100.0));
                blue  = blue  + (int) ((blue  - mean_luminance)*(contrast/100.0));
            }
            else
            {

                if(red - mean_luminance > 0)
                    red = red +  (int) ((255-red)*contrast/100.0);
                else
                    red = red -  (int) (red*contrast/100.0);


                if(green - mean_luminance > 0)
                    green = green +  (int) ((255-green)*contrast/100.0);
                else
                    green = green -  (int) (green*contrast/100.0);


                if(blue - mean_luminance > 0)
                    blue = blue +  (int) ((255-blue)*contrast/100.0);
                else
                    blue = blue -  (int) (blue*contrast/100.0);



            }

            ///// Put in correct range (0...255) /////
            red   = putInRange(red);
            green = putInRange(green);
            blue  = putInRange(blue);

            new_pix[i] = 0;
    		new_pix[i] += (alpha         << 24);   // alpha
    		new_pix[i] += (red           << 16);   // r
    		new_pix[i] += (green         <<  8);   // g
    		new_pix[i] += (blue               );   // b

        }

        Image new_im = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, new_pix, 0, width));
        return new_im;
    }