Login  Register

Java codes for RGB images does not work for ImageJ 2

Posted by LIM Soon Yew John (IMB) on Apr 06, 2015; 3:50am
URL: http://imagej.273.s1.nabble.com/Java-codes-for-RGB-images-does-not-work-for-ImageJ-2-tp5012360.html

Dear All,

I used the Java below as part of the codes for white balance. The codes works fine in ImageJ 1.48 but it does not for ImageJ 2. Any advice on how to change it for ImageJ 2 will be greatly appreciated. Thanks. John

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.PlugInFilter;

public class RGB_Color_Shift implements PlugInFilter {

        public int setup(String arg, ImagePlus imp) {
                return DOES_RGB;
        }

        public void run(ImageProcessor ip) {
                int w=ip.getWidth();
                int h=ip.getHeight();
               
                GenericDialog gd = new GenericDialog("Color Shift");
                gd.addNumericField("Red: ", 0, 0);
                gd.addNumericField("Green: ", 0, 0);
                gd.addNumericField("Blue: ", 0, 0);
                gd.showDialog();
                int red=(int) gd.getNextNumber();
                int green=(int) gd.getNextNumber();
                int blue=(int) gd.getNextNumber();
               
                int[] pixels=(int[]) ip.getPixels();

                for (int i=0; i<h; i++) {
                        int offset=i*w;
                        for (int j=0; j<w; j++) {
                                int pos=offset+j;
                                int c=pixels[pos];
                                int r=(c & 0xff0000)>>16;
                                int g=(c & 0x00ff00)>>8;
                                int b=(c & 0x0000ff);

                                r=r+red;
                                if (r<0) {r=0;}
                                if (r>255) {r=255;}
                                g=g+green;
                                if (g<0) {g=0;}
                                if (g>255) {g=255;}
                                b=b+blue;
                                if (b<0) {b=0;}
                                if (b>255) {b=255;}
                                pixels[pos]=((r  & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
                                }
                        }
                }
}

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html