Skip to content

Instantly share code, notes, and snippets.

@kwolbachia
Created November 9, 2021 17:04
Show Gist options
  • Save kwolbachia/80dc3f2a6ee8cc02de37d314e11ed633 to your computer and use it in GitHub Desktop.
Save kwolbachia/80dc3f2a6ee8cc02de37d314e11ed633 to your computer and use it in GitHub Desktop.
gamma adjustment on LUTs with cursor
//Kevin Terretaz 061021
import ij.*
import ij.gui.*
import ij.process.*
import java.awt.image.*
// gammaLUT avec curseur
//possible pour les hyperstacks d'ajuster autant de curseurs que de channels en même temps?
imp = IJ.getImage()
luts = new LUT[0]
luts = imp.getLuts()
*/
imp = IJ.getImage()
ip = imp.getProcessor()
if (imp.isComposite()) original_lut = imp.getChannelLut()
else original_lut = ip.getColorModel()
//Dialog
gd = new GenericDialog("Gamma LUT Adjuster")
gd.addSlider("Gamma:", 0.05, 3.0, 1)
gd.addDialogListener(new DialogListener() {
boolean dialogItemChanged(GenericDialog gd,java.awt.AWTEvent e) {
gamma = gd.getNextNumber()
applyLUT(original_lut)
gammaLUT(gamma)
return true
}
})
gd.showDialog()
if (gd.wasCanceled()) {
applyLUT(original_lut)
imp.updateAndDraw()
}
def gammaLUT(gamma){
imp = IJ.getImage()
ip = imp.getProcessor()
//get colormodel aka lut
if (imp.isComposite()) cm = imp.getChannelLut()
else cm = ip.getColorModel()
//get reds greens and blues as byte arrays
reds = new byte[256]
greens = new byte[256]
blues = new byte[256]
cm.getReds(reds)
cm.getGreens(greens)
cm.getBlues(blues)
//apply gamma on LUT arrays
float_Gam = new float[256]
int_Gam = new int [256]
newReds = new byte[256]
newGreens = new byte[256]
newBlues = new byte[256]
for (i=0; i<256; i++) float_Gam[i] = Math.pow(i, gamma)
scale = 255/float_Gam[255]
for (i=0; i<256; i++) int_Gam[i] = Math.round(float_Gam[i] * scale)
for (i=0; i<256; i++) {
j = int_Gam[i];
newReds [i] = reds [j]
newGreens[i] = greens[j]
newBlues [i] = blues [j]
}
//create new gammaLUT and apply
lut = new LUT(8, 256,newReds,newGreens,newBlues)
applyLUT(lut)
imp.updateAndDraw()
}
def applyLUT(lut){
if (imp.isComposite()) imp.setChannelLut(lut)
else ip.setColorModel(lut)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment