10 package com.generalrobotix.ui.view.tdview;
17 import javax.media.control.*;
18 import javax.media.protocol.*;
19 import javax.media.datasink.*;
20 import javax.media.format.*;
21 import javax.media.util.ImageToBuffer;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.swt.widgets.Display;
71 public class ImageToMovie implements ControllerListener, DataSinkListener {
73 public static String
QUICKTIME=FileTypeDescriptor.QUICKTIME;
74 public static String
MSVIDEO=FileTypeDescriptor.MSVIDEO;
76 private ImageDataSource
ids_;
92 static MediaLocator createMediaLocator(String url) {
96 if (url.indexOf(
":") > 0 && (ml =
new MediaLocator(url)) !=
null)
99 if (url.startsWith(
File.separator)) {
100 if ((ml =
new MediaLocator(
"file:" + url)) !=
null)
104 "file:" + System.getProperty(
"user.dir") +
File.separator + url;
105 if ((ml =
new MediaLocator(file)) !=
null)
return ml;
121 outML_ = createMediaLocator(outputUrl);
122 if (debugFlag_) System.out.println(
"create:"+outML_);
126 ids_ =
new ImageDataSource(width, height, frameRate);
132 "- create processor for the image datasource ..." 135 p_ = Manager.createProcessor(ids_);
136 }
catch (Exception e) {
138 "Yikes! Cannot create a processor from the data source." 144 p_.addControllerListener(
this);
149 if (!_waitForState(p_, Processor.Configured)) {
150 System.err.println(
"Failed to configure the processor.");
155 p_.setContentDescriptor(
new ContentDescriptor(fileType));
160 TrackControl tcs[] = p_.getTrackControls();
161 return tcs[0].getSupportedFormats();
166 TrackControl tcs[] = p_.getTrackControls();
167 tcs[0].setFormat(format);
173 if (!_waitForState(p_, Controller.Realized)) {
174 System.err.println(
"Failed to realize the processor.");
180 "Failed to create a DataSink for the given output " +
181 "MediaLocator: " + outML_
185 dsink_.addDataSinkListener(
this);
188 if (debugFlag_) System.err.println(
"start processing...");
194 }
catch (IOException e) {
195 System.err.println(
"IO error during processing");
204 ids_.pushImage(image);
209 return ids_.getImageStackSize();
221 }
catch (Exception ex) {}
222 p_.removeControllerListener(
this);
224 if (debugFlag_) System.err.println(
"...done processing.");
234 if ((ds = p.getDataOutput()) ==
null) {
236 "Something is really wrong: the processor does not have " +
237 "an output DataSource" 246 System.err.println(
"- create DataSink for: " + outML);
248 dsink = Manager.createDataSink(ds, outML);
250 }
catch (Exception e) {
251 System.err.println(
"Cannot create the DataSink: " + e);
252 Display display = Display.getDefault();
253 if (display !=
null && !display.isDisposed()) {
254 display.syncExec(
new Runnable(){
269 boolean _waitForState(Processor p,
int state) {
274 }
catch (Exception e) {}
283 if (evt instanceof ConfigureCompleteEvent ||
284 evt instanceof RealizeCompleteEvent ||
285 evt instanceof PrefetchCompleteEvent) {
287 stateTransitionOK_ =
true;
288 waitSync_.notifyAll();
290 }
else if (evt instanceof ResourceUnavailableEvent) {
292 stateTransitionOK_ =
false;
293 waitSync_.notifyAll();
295 }
else if (evt instanceof EndOfMediaEvent) {
296 evt.getSourceController().stop();
297 evt.getSourceController().close();
305 boolean _waitForFileDone() {
309 waitFileSync_.wait();
310 }
catch (Exception e) {}
319 if (evt instanceof EndOfStreamEvent) {
322 waitFileSync_.notifyAll();
324 }
else if (evt instanceof DataSinkErrorEvent) {
327 fileSuccess_ =
false;
328 waitFileSync_.notifyAll();
337 class ImageDataSource
extends PullBufferDataSource {
339 ImageSourceStream streams_[];
341 ImageDataSource(
int width,
int height,
float frameRate) {
342 streams_ =
new ImageSourceStream[1];
343 streams_[0] =
new ImageSourceStream(width, height, frameRate);
347 streams_[0].pushImage(image);
352 return streams_[0].getImageStackSize();
356 public void endImage(){
357 streams_[0].endImage();
360 public void setLocator(MediaLocator source) {
363 public MediaLocator getLocator() {
369 public String getContentType() {
370 return ContentDescriptor.RAW;
373 public void connect() {
376 public void disconnect() {
379 public void start() {
385 public PullBufferStream[] getStreams() {
389 public javax.media.Time getDuration() {
390 return DURATION_UNKNOWN;
393 public Object[] getControls() {
394 return new Object[0];
397 public Object getControl(String
type) {
404 class ImageSourceStream
implements PullBufferStream {
410 Vector<Buffer> imageStack_;
411 boolean ending_ =
false;
412 boolean ended_ =
false;
414 public ImageSourceStream(
int width,
int height,
float frameRate) {
417 frameRate_=frameRate;
419 imageStack_=
new Vector<Buffer>();
423 new Dimension(width, height),
424 Format.NOT_SPECIFIED,
436 imageStack_.add(ImageToBuffer.createBuffer(image, frameRate_));
440 public void endImage(){
446 return imageStack_.size();
451 public boolean willReadBlock() {
453 return (ending_==
false && imageStack_.isEmpty());
459 public void read(Buffer
buf)
throws IOException {
462 if (ending_ && imageStack_.isEmpty()) {
464 System.err.println(
"Done reading all images.");
471 }
else if (!(imageStack_.isEmpty())){
473 buf.copy((Buffer)imageStack_.remove(0));
474 buf.setFlags(buf.getFlags() | Buffer.FLAG_KEY_FRAME);
475 buf.setFormat(format_);
488 buf.setFlags(Buffer.FLAG_DISCARD );
492 public Format getFormat() {
496 public ContentDescriptor getContentDescriptor() {
497 return new ContentDescriptor(ContentDescriptor.RAW);
500 public long getContentLength() {
504 public boolean endOfStream() {
508 public Object[] getControls() {
509 return new Object[0];
512 public Object getControl(String
type) {
static final String get(String key)
png_infop png_charp png_int_32 png_int_32 int * type
ImageToMovie(int width, int height, float frameRate, String outputUrl, String fileType)
#define null
our own NULL pointer
void dataSinkUpdate(DataSinkEvent evt)
void setFormat(Format format)
void controllerUpdate(ControllerEvent evt)
png_infop png_uint_32 * width
static Shell getCurrentShell()
png_infop png_uint_32 png_uint_32 * height
DataSink _createDataSink(Processor p, MediaLocator outML)
Format[] getSupportedFormats()
boolean stateTransitionOK_
void pushImage(Image image)