title: ImageJ API: Getting RGB values from image
body: <p>I am trying to obtain RGB values from an <code>ImagePlus</code> object. I am getting an exception error when I attempt to do this:</p>
  
  <pre><code>import ij.IJ;
  import ij.ImagePlus;
  import ij.plugin.filter.PlugInFilter;
  import ij.process.ColorProcessor;
  import ij.process.ImageProcessor;
  import java.awt.image.IndexColorModel;
  
  public class ImageHelper implements PlugInFilter {
  
      public int setup(String arg, ImagePlus img) {
          return DOES_8G + NO_CHANGES;
      }
  
      public void run(ImageProcessor ip) {
  
          final int r = 0;
          final int g = 1;
          final int b = 2;
  
          int w = ip.getWidth();
          int h = ip.getHeight();
  
          ImagePlus ips = new ImagePlus("C:\\Lena.jpg");
          int width = ips.getWidth();
          int height = ips.getHeight();
          System.out.println("width of image: " + width + " pixels");
          System.out.println("height of image: " + height + " pixels");
  
          // retrieve the lookup tables (maps) for R,G,B
          IndexColorModel icm = (IndexColorModel) ip.getColorModel();
  
          int mapSize = icm.getMapSize();
          byte[] Rmap = new byte[mapSize];
          icm.getReds(Rmap);
          byte[] Gmap = new byte[mapSize];
          icm.getGreens(Gmap);
          byte[] Bmap = new byte[mapSize];
          icm.getBlues(Bmap);
  
          // create new 24-bit RGB image
          ColorProcessor cp = new ColorProcessor(w, h);
          int[] RGB = new int[3];
          for (int v = 0; v &lt; h; v++) {
              for (int u = 0; u &lt; w; u++) {
                  int idx = ip.getPixel(u, v);
                  RGB[r] = Rmap[idx];
                  RGB[g] = Gmap[idx];
                  RGB[b] = Bmap[idx];
                  cp.putPixel(u, v, RGB);
              }
          }
          ImagePlus cwin = new ImagePlus("RGB Image", cp);
          cwin.show();
      }
  }
  </code></pre>
  
  <p>The exception is comming from this line:</p>
  
  <pre><code> IndexColorModel icm = (IndexColorModel) ip.getColorModel();
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException:
    java.awt.image.DirectColorModel cannot be cast to
    java.awt.image.IndexColorModel</p>
  </blockquote>
  
  <p>...Any ideas? ^_^</p>