Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movie Producer Tool #151

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>sc.fiji</groupId>
<artifactId>bigdataviewer-core</artifactId>
<version>10.4.6-SNAPSHOT</version>
<version>10.4.7-SNAPSHOT</version>

<name>BigDataViewer Core</name>
<description>BigDataViewer core classes with minimal dependencies.</description>
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/bdv/BigDataViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
package bdv;

import bdv.tools.PreferencesDialog;
import bdv.tools.movie.ProduceMovieDialog;
import bdv.ui.UIUtils;
import bdv.ui.keymap.Keymap;
import bdv.ui.keymap.KeymapManager;
Expand Down Expand Up @@ -127,6 +128,8 @@ public class BigDataViewer

protected final RecordMovieDialog movieDialog;

protected final ProduceMovieDialog produceMovieDialog;

protected final RecordMaxProjectionDialog movieMaxProjectDialog;

protected final VisibilityAndGroupingDialog activeSourcesDialog;
Expand Down Expand Up @@ -398,6 +401,7 @@ public BigDataViewer(
cropDialog = ( spimData == null ) ? null : new CropDialog( viewerFrame, viewer, spimData.getSequenceDescription() );

movieDialog = new RecordMovieDialog( viewerFrame, viewer, progressWriter );
produceMovieDialog = new ProduceMovieDialog(viewerFrame,viewer,progressWriter);
// this is just to get updates of window size:
viewer.getDisplay().overlays().add( movieDialog );

Expand Down Expand Up @@ -502,6 +506,10 @@ public boolean accept( final File f )
miMovie.setText( "Record Movie" );
menu.add( miMovie );

final JMenuItem miMovieProducer = new JMenuItem(actionMap.get(BigDataViewerActions.PRODUCE_MOVIE));
miMovieProducer.setText("Produce Movie");
menu.add(miMovieProducer);

final JMenuItem miMaxProjectMovie = new JMenuItem( actionMap.get( BigDataViewerActions.RECORD_MAX_PROJECTION_MOVIE ) );
miMaxProjectMovie.setText( "Record Max-Projection Movie" );
menu.add( miMaxProjectMovie );
Expand Down Expand Up @@ -775,7 +783,7 @@ public void collapseCardPanel()

public static void main( final String[] args )
{
final String fn = "/Users/pietzsch/workspace/data/111010_weber_resave.xml";
final String fn = "/Users/zouinkhim/Desktop/unfinishedProjects/grid-3d-stitched-h5/dataset.xml";
try
{
System.setProperty( "apple.laf.useScreenMenuBar", "true" );
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/bdv/BigDataViewerActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class BigDataViewerActions extends Actions
public static final String LOAD_SETTINGS = "load settings";
public static final String EXPAND_CARDS = "expand and focus cards panel";
public static final String COLLAPSE_CARDS = "collapse cards panel";
public static final String PRODUCE_MOVIE = "produce movie";
public static final String RECORD_MOVIE = "record movie";
public static final String RECORD_MAX_PROJECTION_MOVIE = "record max projection movie";
public static final String SET_BOOKMARK = "set bookmark";
Expand All @@ -78,6 +79,7 @@ public class BigDataViewerActions extends Actions
public static final String[] EXPAND_CARDS_KEYS = new String[] { "P" };
public static final String[] COLLAPSE_CARDS_KEYS = new String[] { "shift P", "shift ESCAPE" };
public static final String[] RECORD_MOVIE_KEYS = new String[] { "F10" };
public static final String[] PRODUCE_MOVIE_KEYS = new String[] { "F7" };
public static final String[] RECORD_MAX_PROJECTION_MOVIE_KEYS = new String[] { "F8" };
public static final String[] SET_BOOKMARK_KEYS = new String[] { "shift B" };
public static final String[] GO_TO_BOOKMARK_KEYS = new String[] { "B" };
Expand Down Expand Up @@ -108,6 +110,7 @@ public void getCommandDescriptions( final CommandDescriptions descriptions )
descriptions.add( EXPAND_CARDS, EXPAND_CARDS_KEYS, "Expand and focus the BigDataViewer card panel" );
descriptions.add( COLLAPSE_CARDS, COLLAPSE_CARDS_KEYS, "Collapse the BigDataViewer card panel" );
descriptions.add( RECORD_MOVIE, RECORD_MOVIE_KEYS, "Show the Record Movie dialog." );
descriptions.add( PRODUCE_MOVIE, PRODUCE_MOVIE_KEYS, "Show the Produce Movie dialog." );
descriptions.add( RECORD_MAX_PROJECTION_MOVIE, RECORD_MAX_PROJECTION_MOVIE_KEYS, "Show the Record Max Projection Movie dialog." );
descriptions.add( SET_BOOKMARK, SET_BOOKMARK_KEYS, "Set a labeled bookmark at the current location." );
descriptions.add( GO_TO_BOOKMARK, GO_TO_BOOKMARK_KEYS, "Retrieve a labeled bookmark location." );
Expand All @@ -132,6 +135,7 @@ public static void install( final Actions actions, final BigDataViewer bdv )
toggleDialogAction( actions, bdv.helpDialog, SHOW_HELP, SHOW_HELP_KEYS );
toggleDialogAction( actions, bdv.cropDialog, CROP, CROP_KEYS );
toggleDialogAction( actions, bdv.movieDialog, RECORD_MOVIE, RECORD_MOVIE_KEYS );
toggleDialogAction( actions, bdv.produceMovieDialog, PRODUCE_MOVIE, PRODUCE_MOVIE_KEYS );
toggleDialogAction( actions, bdv.movieMaxProjectDialog, RECORD_MAX_PROJECTION_MOVIE, RECORD_MAX_PROJECTION_MOVIE_KEYS );
toggleDialogAction( actions, bdv.preferencesDialog, PREFERENCES_DIALOG, PREFERENCES_DIALOG_KEYS );
bookmarks( actions, bdv.bookmarkEditor );
Expand Down
207 changes: 207 additions & 0 deletions src/main/java/bdv/tools/movie/MovieProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@

/**
* License: GPL
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License 2
* as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package bdv.tools.movie;

import bdv.cache.CacheControl;
import bdv.export.ProgressWriter;
import bdv.viewer.ViewerPanel;
import bdv.viewer.ViewerState;
import bdv.viewer.animate.SimilarityTransformAnimator;
import bdv.viewer.overlay.MultiBoxOverlayRenderer;
import bdv.viewer.overlay.ScaleBarOverlayRenderer;
import bdv.viewer.render.MultiResolutionRenderer;
import bdv.viewer.render.PainterThread;
import bdv.viewer.render.RenderTarget;
import bdv.viewer.render.awt.BufferedImageRenderResult;
import net.imglib2.realtransform.AffineTransform3D;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
* @author Stephan Saalfeld &lt;[email protected]&gt;
* Code was copied from org.janelia.saalfeldlab.hotknife.VNCMovie;
* Modified and adapted by Marwan Zouinkhi
*/

public class MovieProducer {

public static class Target implements RenderTarget<BufferedImageRenderResult> {

public BufferedImageRenderResult renderResult = new BufferedImageRenderResult();

private final int width;
private final int height;

public Target(final int width, final int height) {
this.width = width;
this.height = height;
}

@Override
public BufferedImageRenderResult getReusableRenderResult() {
return renderResult;
}

@Override
public BufferedImageRenderResult createRenderResult() {
return new BufferedImageRenderResult();
}

@Override
public void setRenderResult(final BufferedImageRenderResult renderResult) {
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}
}

/**
* Cosine shape of linear [0,1]
*/
protected static double cos(final double x) {

return 0.5 - 0.5 * Math.cos(Math.PI * x);
}

/**
* Acceleration function for a t in [0,1]:
* <p>
* types
* 0 symmetric
* 1 slow start
* 2 slow end
* 3 soft symmetric
* 4 soft slow start
* 5 soft slow end
* t = current frame / nb frames
*/
public static double accel(final double t, final int type) {

switch (type) {
case 1: // slow start
return cos(t * t);
case 2: // slow end
return 1.0 - cos(Math.pow(1.0 - t, 2));
case 3: // soft symmetric
return cos(cos(t));
case 4: // soft slow start
return cos(cos(t * t));
case 5: // soft slow end
return 1.0 - cos(cos(Math.pow(1.0 - t, 2)));
default: // symmetric
return cos(t);
}
}

public static void recordMovie(
final ViewerPanel viewer,
final AffineTransform3D[] transforms,
final int[] frames,
final int[] accel,
int width,
int height,
final String dir,
ProgressWriter progressWriter) throws IOException {

final ViewerState renderState = viewer.state();
final ScaleBarOverlayRenderer scalebar = new ScaleBarOverlayRenderer();

int screenWidth = viewer.getDisplayComponent().getWidth();
int screenHeight = viewer.getDisplayComponent().getHeight();
double ratio = Math.min(width * 1.0 / screenWidth, height * 1.0 / screenHeight);

final AffineTransform3D viewerScale = new AffineTransform3D();

viewerScale.set(
ratio, 0, 0, 0,
0, ratio, 0, 0,
0, 0, 1.0, 0);

final MultiBoxOverlayRenderer box = new MultiBoxOverlayRenderer(width, height);

final Target target = new Target(width, height);

final MultiResolutionRenderer renderer = new MultiResolutionRenderer(
target,
new PainterThread(null),
new double[]{1.0},
0l,
12,
null,
false,
viewer.getOptionValues().getAccumulateProjectorFactory(),
new CacheControl.Dummy());

int i = 0;

for (int k = 1; k < transforms.length; ++k) {
progressWriter.setProgress((k*1.0/transforms.length));
final SimilarityTransformAnimator animator = new SimilarityTransformAnimator(
transforms[k - 1],
transforms[k],
0,
0,
0);

for (int d = 0; d < frames[k]; ++d) {
final AffineTransform3D tkd = animator.get(accel((double) d / (double) frames[k], accel[k]));
tkd.preConcatenate(viewerScale);
viewer.state().setViewerTransform(tkd);
renderState.setViewerTransform(tkd);
renderer.requestRepaint();
try {
renderer.paint(renderState);
} catch (final Exception e) {
e.printStackTrace();
return;
}

final BufferedImage bi = target.renderResult.getBufferedImage();

final Graphics2D g2 = bi.createGraphics();
g2.drawImage(bi, 0, 0, null);

/* scalebar */
g2.setClip(0, 0, width, height);
scalebar.setViewerState(renderState);
scalebar.paint(g2);
box.setViewerState(renderState);
box.paint(g2);

/* save image */
ImageIO.write(bi, "png", new File(String.format("%s/img-%04d.png", dir, i++)));

System.out.println(String.format("%s/img-%04d.png", dir, i));
}
}
}



}
Loading