Contrast and Sharpness Code using JAI

Posted on 2006-04-08
Last Modified: 2008-01-09
Plz tell me code of increase and decrease contrast and sharpen of image using JAI
i have write code for contrast it increase contrast accuratly but decrease code is not giving desired result
Question by:MuhammadAdil
    LVL 13

    Expert Comment

    You basically need to use a JAILookuptable, here's a function that helps you adjust the contrast and brightness of a picture:

    public static PlanarImage setColor(PlanarImage source,
    int red, int green, int blue,
    float contrast,
    float brightness)
    // Time the process for performance
    long beginTime = (new java.util.Date()).getTime();

    // Setup the parameter block for the source image and
    // the three parameters for the mean operation
    ParameterBlock mpb = new ParameterBlock();
    mpb.addSource(source); // The source image
    mpb.add(null); // null ROI means whole image
    mpb.add(1); // check every pixel horizontally
    mpb.add(1); // check every pixel vertically
    // Perform the mean operation on the source image
    PlanarImage meanImage = JAI.create("mean", mpb, null);
    // Retrieve the mean pixel value
    double[] mean = (double[])meanImage.getProperty("mean");
    // Average the mean of all bands
    double sum = 0.0D;
    for (int i=0; i < mean.length; i++)
    sum += mean;
    int average = (int) sum / mean.length;

    // Create the lookup table based on the average mean
    byte[][] lut = new byte[3][256];
    for (int i = 0; i < 256; i++ )
    lut[0] = clamp((average + (int)((i - average) * contrast)) + red + (int)brightness);
    lut[1] = clamp((average + (int)((i - average) * contrast)) + green + (int)brightness);
    lut[2] = clamp((average + (int)((i - average) * contrast)) + blue + (int)brightness);
    LookupTableJAI lookup = new LookupTableJAI(lut);
    // Setup the parameter block for the lookup operation
    ParameterBlock pb = new ParameterBlock();

    // Calculate processing time and report
    long endTime = (new java.util.Date()).getTime();
    System.out.println("Time to set colors was " + (endTime - beginTime) + " ms");

    // Return the resulting image
    return JAI.create("lookup", pb, null);
    LVL 13

    Accepted Solution

    Another function that could help you:

    public void processBrightContrast(int ipParam, Image pImage){

    Image mImage = pImage;
    //Create parameter block
    // java.awt.image.renderable.ParameterBlock mPB = new java.awt.image.renderable.ParameterBlock();
    //Add image source to parameter block
    // mPB.addSource(mImage);

    byte lutData[];
    lutData = new byte[256];
    for(int j = 0; j < 256; j++)
    lutData[j] = clampByte(j + ipParam); mLookup = new;

    // mPB.add(mLookup);

    java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();
    pb.add(mImage); // mImage is a normal image here
    PlanarImage im = (PlanarImage)JAI.create("awtImage", pb);

    try {
    // Process the brightness/ darkness mRenOp ="lookup", im); //mPB, null); <---- this is where the debug crashes

    src = this.toImage(mRenOp.getAsBufferedImage());
    catch(IllegalArgumentException e)

    Also, check out the JAI programmers guide here for more info:

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This video teaches viewers about errors in exception handling.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now