Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Manually recoloring buffered images

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Adelaide, Australia
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Manually recoloring buffered images

    Hi all,

    I posted a topic similar to this some months ago but it was never resolved so I thought I might try again.

    I have a series of monochrome images that I have to dynamically recolor in my application. There are about
    20 images, and a total of 8 possible color schemes for all of them- meaning either I recolor in Java or create
    160 images in total, which I'd rather not do if I need to change the colors for whatever reason.

    The following code shows the method I'm using, with a printout showing where things go wrong:

    //Our image has been loaded from file and displays fine as is, and the color is an sRGB value extracted from a Color object. It's
    //values are tested and work correctly.
    protected final void colorizeImage(BufferedImage image, int color){
            for(int i = 0; i < image.getWidth(); i++){
                for(int j = 0; j < image.getHeight(); j++){
                    if(image.getRGB(i,j) != 0){ //0 represents a transparent pixel, and should not be painted
                        image.setRGB(i, j, color);
                        if(image.getRGB(i, j) != color)
                            System.out.println("Incorrect- Expected " + (new Color(color)) + ", but found " + (new Color(image.getRGB(i, j))));
                    }
                }
            }
        }

    I find when running this code at present the call will happen quite frequently, usually uniformly for one image indicating
    one color is incorrect. The color for the whole image is the same, but not the color I specified. I cannot seem to determine
    any consistency from the errors occurring either; some colors will work fine in certain circumstances and others will be
    completely incorrect.

    I expect the problem has to do either with the way I'm using/working with buffered images, but I really can't tell.
    If anyone reading this post has a bit of experience working with dynamically recoloring images, please let me know.
    I'm more than happy to provide the images or additional source code if it would help the problem.

    Thanks for reading,
    Nitrogen Fingers


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Manually recoloring buffered images

    The API documentation for BufferedImage.setRGB explains why getRGB might give you a different value from the one you supply in setRGB. Could that apply in your case?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Adelaide, Australia
    Posts
    9
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Manually recoloring buffered images

    Thanks for your response, Sean4u.

    The entry you mentioned suggests the index colour may change it if that colour does not fit the IndexColorModel. I've attempted to counter this
    by ensuring each picture I have contains at least one pixel with the colour I'm attempting to dynamically set, but the issue persists. I think this
    probably is the issue- can you or anyone else suggest a solution?

    Thanks again

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Manually recoloring buffered images

    I had similar problem that I solved using filters. LookupOp (Java 2 Platform SE v1.4.2)
    This is much faster than the pixel by pixel method you are using. I have a white image with transperent background that I pass through a LookupOp filter to color it. It needs a bit more work to setup the bytelookuptable used for constructing the LookupOp object.

Similar Threads

  1. help me with my buffered reader please! whats wrong?
    By jeremanalaotao in forum Loops & Control Statements
    Replies: 10
    Last Post: September 6th, 2011, 08:09 PM
  2. Double buffered Image is flickering
    By jay-wo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2011, 12:21 AM
  3. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Java Swing
    Replies: 2
    Last Post: July 27th, 2010, 04:28 AM
  4. Buffered Reader to J Option
    By jk_0821 in forum Collections and Generics
    Replies: 13
    Last Post: July 19th, 2010, 04:14 PM
  5. setting cookies manually
    By dotanguy in forum Java Networking
    Replies: 2
    Last Post: July 2nd, 2010, 10:51 AM