Add option to preserve alpha channel in export (closes #661)

Default off cause it being on by default had confused quite a few people.
This commit is contained in:
Jonas Herzig
2022-03-02 16:39:30 +01:00
parent d9da2e135c
commit 7615499cef
8 changed files with 36 additions and 11 deletions

View File

@@ -19,9 +19,11 @@ import java.util.Map;
public class PNGWriter implements FrameConsumer<BitmapFrame> {
private final Path outputFolder;
private final boolean keepAlpha;
public PNGWriter(Path outputFolder) throws IOException {
public PNGWriter(Path outputFolder, boolean keepAlpha) throws IOException {
this.outputFolder = outputFolder;
this.keepAlpha = keepAlpha;
Files.createDirectories(outputFolder);
}
@@ -47,6 +49,7 @@ public class PNGWriter implements FrameConsumer<BitmapFrame> {
}
private void withImage(BitmapFrame frame, IOConsumer<Image> consumer) throws IOException {
byte alphaMask = (byte) (keepAlpha ? 0 : 0xff);
ByteBuffer buffer = frame.getByteBuffer();
ReadableDimension size = frame.getSize();
int width = size.getWidth();
@@ -58,7 +61,7 @@ public class PNGWriter implements FrameConsumer<BitmapFrame> {
byte g = buffer.get();
byte r = buffer.get();
byte a = buffer.get();
image.setRGBA(x, y, r, g, b, a);
image.setRGBA(x, y, r, g, b, a | alphaMask);
}
}
consumer.accept(image);