Skip to content

Commit

Permalink
BAM Converter: add new output filter "Overlay BAM output"
Browse files Browse the repository at this point in the history
Combines split BAM animations, such as huge creature animations
(dragons, etc.) into a single BAM file.
  • Loading branch information
Argent77 committed Sep 9, 2024
1 parent 99fe6f7 commit 5569579
Show file tree
Hide file tree
Showing 22 changed files with 1,539 additions and 95 deletions.
5 changes: 3 additions & 2 deletions src/org/infinity/gui/converter/BamFilterBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ public void close() {
* Modifies the specified BufferedImage object to reflect the current settings of the filter.<br>
* <b>Note:</b> For optimization purposes, prevent creating a new BufferedImage object if possible.
*
* @param frame The PseudoBamFrameEntry object to modify.
* @param frameIndex Frame index in the global frame list of the current BAM animation.
* @param frame The PseudoBamFrameEntry object to modify.
* @return The updated PseudoBamFrameEntry object.
*/
public abstract PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry frame);
public abstract PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry frame);

/**
* Adds a ChangeListener to the listener list. ChangeListeners will be notified whenever the filter settings change.
Expand Down
41 changes: 41 additions & 0 deletions src/org/infinity/gui/converter/BamFilterBaseOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.nio.file.Path;
import java.util.Arrays;

import org.infinity.resource.graphics.DxtEncoder;
import org.infinity.resource.graphics.PseudoBamDecoder;
import org.infinity.util.Logger;

/**
* The base class for filters that output the current state of the BAM structure to disk.
Expand Down Expand Up @@ -53,4 +56,42 @@ public static int[] retrievePalette(PseudoBamDecoder decoder) {
}
return retVal;
}

/**
* Converts animation data from the specified decoder into the target BAM format and saves it to disk.
*
* @param converter The global {@link ConvertToBam} instance.
* @param outFileName Output path of the resulting BAM file.
* @param decoder {@link PseudoBamDecoder} instance with animation data to convert.
* @return {@code true} if the conversion completed successfully, {@code false} otherwise.
* @throws Exception if an unrecoverable error occurs during the conversion process.
*/
public static boolean convertBam(ConvertToBam converter, Path outFileName, PseudoBamDecoder decoder)
throws Exception {
if (converter != null && outFileName != null && decoder != null) {
if (converter.isBamV1Selected()) {
// convert to BAM v1
decoder.setOption(PseudoBamDecoder.OPTION_INT_RLEINDEX, converter.getPaletteDialog().getRleIndex());
decoder.setOption(PseudoBamDecoder.OPTION_BOOL_COMPRESSED, converter.isBamV1Compressed());
try {
return decoder.exportBamV1(outFileName, converter.getProgressMonitor(), converter.getProgressMonitorStage());
} catch (Exception e) {
Logger.error(e);
throw e;
}
} else {
// convert to BAM v2
DxtEncoder.DxtType dxtType = converter.getDxtType();
int pvrzIndex = converter.getPvrzIndex();
try {
return decoder.exportBamV2(outFileName, dxtType, pvrzIndex, converter.getProgressMonitor(),
converter.getProgressMonitorStage());
} catch (Exception e) {
Logger.error(e);
throw e;
}
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorBCG.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorHSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorInvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterColorSwap.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public BufferedImage process(BufferedImage frame) throws Exception {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
if (entry != null) {
entry.setFrame(applyEffect(entry.getFrame()));
}
Expand Down
6 changes: 6 additions & 0 deletions src/org/infinity/gui/converter/BamFilterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class BamFilterFactory {
BamFilterColorSwap.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterColorInvert.getFilterName(), BamFilterColorInvert.getFilterDesc(),
BamFilterColorInvert.class));

FILTER_INFO_LIST.add(new FilterInfo(BamFilterTransformResize.getFilterName(),
BamFilterTransformResize.getFilterDesc(), BamFilterTransformResize.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterTransformRotate.getFilterName(),
Expand All @@ -39,6 +40,7 @@ public class BamFilterFactory {
BamFilterTransformTrim.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterTransformCenter.getFilterName(),
BamFilterTransformCenter.getFilterDesc(), BamFilterTransformCenter.class));

FILTER_INFO_LIST.add(new FilterInfo(BamFilterOutputDefault.getFilterName(), BamFilterOutputDefault.getFilterDesc(),
BamFilterOutputDefault.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterOutputCombine.getFilterName(), BamFilterOutputCombine.getFilterDesc(),
Expand All @@ -49,6 +51,10 @@ public class BamFilterFactory {
BamFilterOutputImage.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterOutputGif.getFilterName(), BamFilterOutputGif.getFilterDesc(),
BamFilterOutputGif.class));
FILTER_INFO_LIST.add(new FilterInfo(BamFilterOutputOverlay.getFilterName(), BamFilterOutputOverlay.getFilterDesc(),
BamFilterOutputOverlay.class));

FILTER_INFO_LIST.sort((a, b) -> a.getName().compareTo(b.getName()));
}

/** Returns the number of registered BAM filters. */
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterOutputCombine.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean setConfiguration(String config) {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry frame) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry frame) {
// does not modify the source image
return frame;
}
Expand Down
39 changes: 2 additions & 37 deletions src/org/infinity/gui/converter/BamFilterOutputDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.nio.file.Path;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.infinity.gui.ViewerUtil;
import org.infinity.resource.graphics.DxtEncoder;
import org.infinity.resource.graphics.PseudoBamDecoder;
import org.infinity.resource.graphics.PseudoBamDecoder.PseudoBamFrameEntry;
import org.infinity.util.Logger;

/**
* The default BAM output filter.
Expand All @@ -43,11 +40,11 @@ public BamFilterOutputDefault(ConvertToBam parent) {

@Override
public boolean process(PseudoBamDecoder decoder) throws Exception {
return applyEffect(decoder);
return BamFilterBaseOutput.convertBam(getConverter(), getConverter().getBamOutput(), decoder);
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry entry) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry entry) {
// does not modify the source image
return entry;
}
Expand Down Expand Up @@ -75,36 +72,4 @@ protected JPanel loadControls() {

return panel;
}

private boolean applyEffect(PseudoBamDecoder decoder) throws Exception {
if (getConverter() != null && decoder != null) {
Path outFile = getConverter().getBamOutput();

if (getConverter().isBamV1Selected()) {
// convert to BAM v1
decoder.setOption(PseudoBamDecoder.OPTION_INT_RLEINDEX,
getConverter().getPaletteDialog().getRleIndex());
decoder.setOption(PseudoBamDecoder.OPTION_BOOL_COMPRESSED, getConverter().isBamV1Compressed());
try {
return decoder.exportBamV1(outFile, getConverter().getProgressMonitor(),
getConverter().getProgressMonitorStage());
} catch (Exception e) {
Logger.error(e);
throw e;
}
} else {
// convert to BAM v2
DxtEncoder.DxtType dxtType = getConverter().getDxtType();
int pvrzIndex = getConverter().getPvrzIndex();
try {
return decoder.exportBamV2(outFile, dxtType, pvrzIndex, getConverter().getProgressMonitor(),
getConverter().getProgressMonitorStage());
} catch (Exception e) {
Logger.error(e);
throw e;
}
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterOutputGif.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean setConfiguration(String config) {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry frame) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry frame) {
return frame;
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/gui/converter/BamFilterOutputImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public boolean setConfiguration(String config) {
}

@Override
public PseudoBamFrameEntry updatePreview(PseudoBamFrameEntry frame) {
public PseudoBamFrameEntry updatePreview(int frameIndex, PseudoBamFrameEntry frame) {
return frame;
}

Expand Down
Loading

0 comments on commit 5569579

Please sign in to comment.