Affects the original image for some reason. Can somebody take a look at this?
PImage rocks; PImage rocksBrightened; int rockBytes[][]; int rockBrights[][]; void setup() { rocks = loadImage("rocks.jpg"); rocksBrightened = loadImage("notrocks.jpg"); rocks.loadPixels(); int h = rocks.height; size(rocks.width*2, rocks.height*2); image(rocks, 0, 0); rockBytes = new int[rocks.width][rocks.height]; rockBrights = new int[rocks.width][rocks.height]; for (int i = 0; i < rocks.width; i++) { for (int j = 0; j < rocks.height; j++) { rockBytes[i][j] = rocks.get(i, j); } } for (int i = 0; i < rocks.width; i++) { for (int j = 0; j < rocks.height; j++) { rockBrights[i][j] = rocksBrightened.get(i, j); } } drawHistogram(rocks, computeHistogram (rocks)); brighten(rockBrights); makeImage(rocksBrightened, rockBrights); image(rocksBrightened, 0, h); } void loadPixelArray(PImage anImage) { anImage.loadPixels(); //load pixels into pixels[] array } int[] computeHistogram (PImage anImage) { int histogram[] = new int[256]; loadPixelArray(anImage); int histMax = 0; for (int x = 0; x < anImage.width; x++) { for (int y = 0; y < anImage.height; y++ ) { // Calculate the 1D pixel location int loc = x + y*anImage.width; histogram[int(red(anImage.pixels[loc]))]++; } } for (int i = 0; i < histogram.length; i++) { if (histogram[i] > histMax) { histMax = histogram[i]; } } for (int i = 0; i < histogram.length; i++) { histogram[i] = histogram[i]*100/histMax; } return histogram; }
void brighten(int anyArray[][]) { for (int i = 0; i < anyArray.length; i++){ for (int j = 0; j < anyArray[0].length; j++) { anyArray[i][j] = int(-0.004436*anyArray[i][j]) + int(2.1308*anyArray[i][j]); } } }
void drawHistogram(PImage anImage, int histogram[]) { noStroke(); fill(0); rect(anImage.width+1, 0, anImage.width, anImage.height); for (int i = 0; i < 256; i++) { stroke(255); line(anImage.width + i, anImage.height, anImage.width + i, anImage.height - histogram[i]); } }
void makeImage(PImage anImage, int imgArray[][]) { anImage.loadPixels(); for (int i = 0; i < imgArray.length; i++) { for (int j = 0; j < imgArray[0].length; j++) { int loc = i + j*imgArray.length; set(i, j, color(imgArray[i][j])); } } anImage.updatePixels(); }
I have an assignment for school that involves displaying a base colour and then a darker version of that colour. Do I actually have to get the RGB values of the base colour and alter them individually to prevent the value wrapping around when it hits 0, or is there a quick and dirty way to do this? (This is my first programming class ever so speak slowly and use little words!)