|
Author |
Topic: example of a higher dynamic range (Read 1051 times) |
|
Charles Hinshaw
|
example of a higher dynamic range
« on: Mar 21st, 2004, 11:37pm » |
|
Code: /* I was noticing that when I did multiple, sequential modifications to colors in an image, I was getting unwanted results because my colors were always clamped to integers between 0 and 255 for each step. This was no good, so I made pix[][] for all pixel work. When it is done, and you are ready to write to the screen just use drawpix(). In this example, if mod1 and mod2 were actually done to the pixels[] array instead of pix[], the final image would not be the image loaded in the beginning. Hopefully this will help somebody. */ float pix[][]; void setup(){ size(200,200); BImage img; img = loadImage("color.jpg"); image(img, 0, 0); makepix(); mod1(); mod2(); drawpix(); } // ----- Make an array to store floating point values for red, green and blue based on pixels[] void makepix(){ pix = new float[pixels.length][3]; for (int i = 0; i < pixels.length; i++){ pix[i][0] = getR(pixels[i]); pix[i][1] = getG(pixels[i]); pix[i][2] = getB(pixels[i]); } } // ----- for each pixel, increase red by 25.3 and green by 37.8, and decrease blue by 200 void mod1(){ for (int i = 0; i < pixels.length; i++){ pix[i][0] += 25.3; pix[i][1] += 37.8; pix[i][2] -= 200; } } // ----- undo what was done in mod void mod2(){ for (int i = 0; i < pixels.length; i++){ pix[i][0] -= 25.3; pix[i][1] -= 37.8; pix[i][2] += 200; } } // ----- draw the pix[][] array into the pixels[] array void drawpix(){ int r,g,b; for (int i = 0; i < pixels.length; i++){ r = setColor(pix[i][0]); g = setColor(pix[i][1]); b = setColor(pix[i][2]); pixels[i] = r<<16 | g<<8 | b; } } // ----- getR, getG and getB will return floating point values from Hex pixel values float getR(int r){ return (float)((r >> 16) & 0xff); } float getG(int g){ return (float)((g >> 8) & 255); } float getB(int b){ return (float)(b & 255); } // ----- setColor will take a floating point number and return an integer clamped between 0 and 255 int setColor(float c){ return (int)(c<0 ? 0 : c>255 ? 255 : c); } |
|
|
Charles Hinshaw PHAERSE
http://www.phaerse.com
|
|
|
|