Pixels array, HSB, PImage.save(), and Unexpected Behavior

Hi all, I'm working on a generative art project. For this project it is in the best interests of flexibility to work in HSB mode. Given that this involves working with large image files, I hit upon the idea of stuffing HSB info into the RGB field, saving it, and then processing that altered version. Here is my process.

1) Read image, loadpixels. 2) convert rgb data to hsv data in pixels array 3) update pixels 4) validate hsv data ranges in pixels array 5) save file

Once I've started my program I read the file, convert to HSV/HSB, and show the resulting bizarre image on screen via image(). Just to be sure I was doing it right, I wrote a routine to convert the HSV/HSB data in the pixels array back to RGB. I can go back and forth flawlessly.

Before saving the image to disk using the PImage.save(), I run a validate routine to check on the ranges of all the HSV values in the pixels array. The output from that is as follows:

HSV validation: hue range=** 0 to 359** Saturation range= **0 to 94**  Brightness range= **7 to 100**

This is what I expect to see so I save the file and end the session.

I restart the program and load that newly saved image. Displaying the image it does display correctly, that is as a weird image - which is no surprise as the image data is being interpreted as 0-255 RGB values.

However, when I run my validate routine against the values in the pixels array, it returns the following.

   HSV validation: hue range= **0 to 746** Saturation range= **0 to 127**  Brightness range= **0 to 127**

The values in this new pixels array have changed substantially yet I have done nothing to the data between the time I saved it and opened it. Further when I attempt to convert the HSB values back to RGB the result is garbage and all kinds of "range out of bounds" error messages that I incorporated into my conversion utilities.

I am guessing that there is some sort of behind the scenes processing that is going on with respect to what Processing is doing when it is reading and writing the pixels array. OR... a thought that has crossed my mind is that the PImage.save does not save the contents of the pixels array but instead relies on saving an RGB version of what appears on screen.

So, perhaps my question should be this: how can I insure that the contents of the pixels array is what is being saved?

PS - the reason I suspect that Processing is at fault is because I have to lie in order to do HSB. RGB values are all in the 0-255 range associated with 8 bits per channel of storage. In order to save the HSB data, whose range is 360,100,100, I use only 7 bits for S and B, and 10 bits for H.

Any light that anyone can shed on this would be immensely appreciated. Thanks.


  • ... And the mystery deepens.

    The above post was based on working with JPEG images.

    On a lark, I tried the same workflow with a BMP image and my program worked precisely as expected. Following up I tried again using a PNG image and again the program worked correctly.

    So why would the program work with PNG and BMP but not JPEG? I'm guessing it must have something to do with the image compression associated with the JPEG format.


Sign In or Register to comment.