/** Gamma Adjust Dave Bollinger, Dec 2006 http://www.davebollinger.com for Processing 0123 Beta Drag slider to adjust gamma */ PImage src, dst; float gamma = 1.0; HScrollbar hsg; int [] gammatable; void setup() { size(200,200,P3D); src = loadImage("eames.jpg"); dst = src.get(); PFont font = loadFont("small-6.vlw"); hsg = new HScrollbar(10, height-8, 180, 8, 5, "GAMMA", font); hsg.setRange(0.1,4.0); hsg.setValue(gamma); gammatable = new int[256]; } void draw() { hsg.update(); float newgamma = hsg.getValue(); if (newgamma != gamma) { gamma = newgamma; gammaAdjustImage(src,dst,gamma); } background(dst); hsg.draw(); } void gammaAdjustImage(PImage src, PImage dst, float gamma) { float rgamma = 1f / gamma; for (int i=0; i<256; i++) gammatable[i] = (int)(pow((float)(i) / 255f, rgamma) * 255f); int [] sp = src.pixels, dp = dst.pixels; for (int i=dp.length-1; i>=0; i--) { int c = sp[i]; dp[i] = (c & 0xFF000000) | (gammatable[(c&0xFF0000)>>16]<<16) | (gammatable[(c&0xFF00)>>8]<<8) | (gammatable[(c&0xFF)]) ; } }