import ij.*; import ij.plugin.frame.*; import ij.process.*; import ij.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.media.*; import javax.media.control.FrameGrabbingControl; import javax.media.control.FramePositioningControl; import javax.media.format.VideoFormat; import javax.media.protocol.*; import javax.media.util.BufferToImage; public class JMF_Movie_Reader extends PlugInFrame implements ControllerListener, ActionListener { Player p; FramePositioningControl fpc; FrameGrabbingControl fgc; Object waitSync = new Object(); boolean stateTransitionOK = true; int totalFrames = FramePositioningControl.FRAME_UNKNOWN; BufferToImage frameConverter; String name; boolean grayscale = false; ImageStack stack; public JMF_Movie_Reader() { super("JMF Movie Reader"); } public void run(String arg) { IJ.showStatus(""); OpenDialog od = new OpenDialog("Open Movie...", ""); String dir = od.getDirectory(); name = od.getFileName(); if (name==null) return; String path = dir + name; String url = encodeURL("file://"+path); //IJ.write("path: "+path); //IJ.write("url: "+url); //url = "http://rsb.info.nih.gov/movie.avi"; //url = "file:///Macintosh HD/Desktop%20Folder/test.mov"; MediaLocator ml; if ((ml = new MediaLocator(url)) == null) { IJ.write("Cannot build media locator from: "); return; } DataSource ds = null; // Create a DataSource given the media locator. IJ.showStatus("creating JMF data source"); try { ds = Manager.createDataSource(ml); } catch (Exception e) { IJ.write("Cannot create DataSource from: " + ml); return; } openMovie(ds); } public String encodeURL(String url) { int index = 0; while (index>-1) { index = url.indexOf(' '); if (index>-1) url = url.substring(0,index)+"%20"+url.substring(index+1,url.length()); } return url; } public boolean openMovie(DataSource ds) { IJ.showStatus("opening: "+ds.getContentType()); try { p = Manager.createPlayer(ds); } catch (Exception e) { error("Failed to create a player from the given DataSource:\n \n" + e.getMessage()); return false; } p.addControllerListener(this); p.realize(); if (!waitForState(p.Realized)) { error("Failed to realize the JMF player."); return false; } // Try to retrieve a FramePositioningControl from the player. fpc = (FramePositioningControl)p.getControl("javax.media.control.FramePositioningControl"); if (fpc == null) { error("The player does not support FramePositioningControl."); return false; } // Try to retrieve a FrameGrabbingControl from the player. fgc = (FrameGrabbingControl)p.getControl("javax.media.control.FrameGrabbingControl"); if (fgc == null) { error("The player does not support FrameGrabbingControl."); return false; } Time duration = p.getDuration(); if (duration != Duration.DURATION_UNKNOWN) { //IJ.write("Movie duration: " + duration.getSeconds()); totalFrames = fpc.mapTimeToFrame(duration)+1; if (totalFrames==FramePositioningControl.FRAME_UNKNOWN) IJ.write("The FramePositioningControl does not support mapTimeToFrame."); } else { IJ.write("Movie duration: unknown"); } // Prefetch the player. p.prefetch(); if (!waitForState(p.Prefetched)) { error("Failed to prefetch the player."); return false; } // Display the visual & control component if there's one. setLayout(new FlowLayout()); Component vc; if ((vc = p.getVisualComponent()) != null) { add(vc); } Buffer frame = fgc.grabFrame(); VideoFormat format = (VideoFormat)frame.getFormat(); frameConverter = new BufferToImage(format); setVisible(true); //saveImage(1, frame); for (int i=0;i>16; g = (c&0xff00)>>8; b = c&0xff; if (r!=g || r!=b || g!=b) { grayscale = false; break; } } return grayscale; } }