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 3 of 3

Thread: Setting individual sRGB pixel values on BufferedImages

  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 Setting individual sRGB pixel values on BufferedImages

    Hi all, this is my first technical post on the forums, it's for a small project I'm working on that I hope to recycle in the future.

    My issue is to do with performing color manipulation on BufferedImage objects (imported from file). Using a ColorModel, I'm able to determine the RGB values of each pixel on an image, however the setRGB method demands I submit an sRGB color value. I'm not familiar with the sRGB system, so my question is how can I convert an RGB value to sRGB, or is there another method I can use in manipulating image pixels?

    If I can solve this problem, I'll be able to create several procedurally generated "skins" for individual images, which I think could be used to add variety and visual indicators to Java interfaces.

    Thanks for any help in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Setting individual sRGB pixel values on BufferedImages

    Quote Originally Posted by nitrogenFingers View Post
    I'm not familiar with the sRGB system
    I bet you are and just don't know it. Check out the API for Color. What happened when you tried to use the setRGB method? An SSCCE would be nice to see.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  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: Setting individual sRGB pixel values on BufferedImages

    Gave it a quick lookover, and have had success with the getRGB method in the Color API, although the values are a bit foreign I think I have a better understanding for how they work.

    I ran into some issues with the setRGB method for image, and have developed a workaround for simple cases. I've noticed the method does not successfully convert sRGB values to those provided typically, but if that particular color is already present in the image it is successful. So a simple workaround, if you know the colors you'll be providing is to just ensure at least one pixel per region on your image contains one of the colors you need, and if necessary paint it over in the colorizing method.

    Although this is fine for the project I'm attempting to include it in, especially for more complex user interfaces I had hoped colors could be chosen and set within a Java application itself, if for example the user wanted to specify which colors to use via a JColorChooser or similar.This wouldn't work with this workaround, so any ideas into how this could be achieved would be very welcome.

    Before I forget, here's an SSCCE, hopefully it helps explain.

    //Not a real class in my project, for demonstration's sake
     
    public ImageColorizer(Color color) throws IOException{
           BufferedImage image = ImageIO.read(new File("example.png"));
           int sRGBColor = color.getRGB();
           colorizeImage(image, sRGBColor);
    }
     
    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- these images are monochrome
                        image.setRGB(i, j, color);
                    }
                }
            }
        }

    This is a very simplified version of this part of the problem. Were this code to be run, with the colors method having a sRGB value not found anywhere in the image, the setRGB method in this case would either set it to one already found in the image (thus far only tested with monochrome images), or matt black. If there were some way any color could be passed to the ImageColorizer and be effective in recolorizing the image, it would achieve the goal I'm after.

Similar Threads

  1. Getting length of individual token?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 10th, 2011, 02:48 AM
  2. Get Pixel
    By Ophe in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2011, 04:24 PM
  3. XOR'ing BufferedImages onto a Graphics2D object
    By nemo in forum AWT / Java Swing
    Replies: 6
    Last Post: November 14th, 2010, 03:33 PM
  4. Controlling individual threads
    By youngstorm in forum Threads
    Replies: 4
    Last Post: October 19th, 2010, 03:28 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM