ImagePane.java

Go to the documentation of this file.
00001 package theba.core.gui;
00002 
00003 import java.awt.Dimension;
00004 import java.awt.Graphics;
00005 import java.awt.event.MouseEvent;
00006 import java.awt.event.MouseMotionListener;
00007 import java.awt.image.BufferedImage;
00008 import java.awt.image.IndexColorModel;
00009 
00010 import javax.swing.JOptionPane;
00011 import javax.swing.JPanel;
00012 import javax.swing.JTabbedPane;
00013 
00014 import theba.core.Stack;
00015 import theba.core.io.SliceReader;
00016 import theba.core.io.VolumeReader;
00017 
00018 
00026 public class ImagePane extends JPanel {
00030         private static final long serialVersionUID = 1L;
00031 
00032         private VolumeReader reader;
00033 
00034         private int[] tmp;
00035 
00036         ThebaGUI control;
00037 
00038         boolean drawFibers = false;
00039 
00040         BufferedImage img;
00041 
00042         JTabbedPane tabPane = new JTabbedPane();
00043 
00044         private int[] colors;
00045 
00046         public ImagePane(ThebaGUI f) {
00047                 this.control = f;
00048                 generateColorTable();
00049                 addMouseMotionListener(new MouseMotionListener() {
00050                         public void mouseDragged(MouseEvent e) {
00051                         }
00052 
00053                         public void mouseMoved(MouseEvent e) {
00054                                 control.setPointerLabel(e.getPoint());
00055                         }
00056                 });
00057         }
00058 
00059         public ImagePane(ThebaGUI forsk, SliceReader reader2) {
00060                 reader = reader2;
00061                 this.control = forsk;
00062                 generateColorTable();
00063                 addMouseMotionListener(new MouseMotionListener() {
00064                         public void mouseDragged(MouseEvent e) {
00065                         }
00066 
00067                         public void mouseMoved(MouseEvent e) {
00068                                 control.setPointerLabel(e.getPoint());
00069                         }
00070                 });
00071         }
00072 
00076         public IndexColorModel getIndexedColorModel() {
00077                 byte ff = (byte) 0xff;
00078                 byte[] r = new byte[256];
00079                 byte[] g = new byte[256];
00080                 byte[] b = new byte[256];
00081                 ;
00082                 r[255] = ff;
00083                 g[255] = ff;
00084                 b[255] = ff;
00085                 for (int i = 1; i < 255; i++) {
00086                         b[i] = (byte) (100 + ((i * 4) % 15) * 10);
00087                         g[i] = (byte) (50 + ((i * 4) % 14) * 10);
00088                         r[i] = (byte) (0 + (((i * 5)) % 10) * 20);
00089                 }
00090                 r[7] = ff;
00091                 g[7] = 0;
00092                 b[7] = 0;
00093                 IndexColorModel cm = new IndexColorModel(8, 256, r, g, b);
00094                 return cm;
00095         }
00096 
00097         public VolumeReader getSliceReader() {
00098                 return reader;
00099         }
00100 
00101         public Stack getVolume() {
00102                 return new Stack(reader);
00103         }
00104 
00105         public boolean isDrawFibers() {
00106                 return drawFibers;
00107         }
00108 
00109         @Override
00110         public void paintComponent(Graphics g) {
00111                 super.paintComponent(g);
00112                 if (img != null) {
00113                         g.drawImage(img, 0, 0, this);
00114                 }
00115         }
00116 
00120         public void generateColorTable() {
00121                 colors = new int[control.MAX_FIBERS];
00122                 for (int i = 0; i < colors.length; i++) {
00123                         colors[i] = (((i * 4) % 15) * 10) << 16
00124                         | (100 + ((i * 4) % 14) * 10) << 8
00125                         | (100 + (((i * 5)) % 10) * 20);
00126                 }
00127 
00128                 colors[control.INVALID] = 0xff0000;
00129         }
00130 
00136         public int getColor(short val) {
00137                 if (val < 0)
00138                         return 0; // invalid color
00139                 int index = val % colors.length; // make sure color is inside
00140                 // lookup table size
00141                 return colors[index];
00142         }
00143 
00144         public void setData(short[] data, int w, int h) {
00145                 if (control.isMinimized()) {
00146                         return;
00147                 }
00148                 if (tmp == null || tmp.length != w * h)
00149                         tmp = new int[w * h];
00150                 
00151                 for (int i = 0; i < tmp.length; i++) {
00152                         if (data[i] <= 255) {
00153                                 tmp[i] = data[i] | data[i] << 8 | data[i] << 16;
00154                         } else {
00155                                 tmp[i] = getColor(data[i]);
00156                         }
00157                 }
00158                 if (data == null)
00159                         return;
00160                 short id = control.getCurrentTracker().getCurrentId();
00161                 if (control.hideWhite().isSelected()) {
00162                         for (int i = 0; i < tmp.length; i++) {
00163                                 if (data[i] == id && id != 0) {
00164                                         tmp[i] = 0xffff00;
00165                                 } else if (data[i] <= 255) {
00166                                         tmp[i] = 0xff000000;
00167                                 }
00168                         }
00169                 }
00170                 if (img == null || w != img.getWidth() || h != img.getHeight()) {
00171                         img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
00172                         setPreferredSize(new Dimension(w, h));
00173                 }
00174                 img.getRaster().setDataElements(0, 0, w, h, tmp);
00175         }
00176 
00177         public void setDrawFibers(boolean drawFibers) {
00178                 this.drawFibers = drawFibers;
00179         }
00180 
00181         public void setReader(VolumeReader reader) {
00182                 this.reader = reader;
00183 
00184         }
00185 
00186         public void showSlice(int index) {
00187                 short[] pixels = reader.getSlice(index);
00188                 if (reader != null)
00189                         setData(pixels, control.width, control.height);
00190                 else
00191                         JOptionPane.showMessageDialog(null,
00192                         "No reader attached to this view");
00193                 repaint();
00194         }
00195 
00196         public void updateData(int slice) {
00197                 if (reader == null)
00198                         System.err.println("No reader attached to this pane!");
00199                 else {
00200                         setData(reader.getSlice(slice), reader.getWidth(), reader
00201                                         .getHeight());
00202                         repaint();
00203                 }
00204         }
00205 
00206         public void setInput(SliceReader writer) {
00207                 reader.destroy();
00208                 reader = writer;
00209 
00210         }
00211 }

Generated on Fri Nov 13 08:57:07 2009 for Theba by  doxygen 1.6.1