Skip to content

Commit

Permalink
Issue #488 bitmap chart export alpha channel
Browse files Browse the repository at this point in the history
  • Loading branch information
timmolter committed Jul 20, 2020
1 parent 6cff58e commit 6c0b4b8
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.knowm.xchart;

import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.*;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
Expand Down Expand Up @@ -56,11 +65,8 @@ public static String addFileExtension(String fileName, BitmapFormat bitmapFormat
public static void saveBitmap(Chart chart, String fileName, BitmapFormat bitmapFormat)
throws IOException {

OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat));
try {
try (OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat)); ) {
saveBitmap(chart, out, bitmapFormat);
} finally {
out.close();
}
}

Expand Down Expand Up @@ -99,11 +105,8 @@ public static void saveBitmap(
BitmapEncoder.BitmapFormat bitmapFormat)
throws IOException {

OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat));
try {
try (OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat))) {
saveBitmap(charts, rows, cols, out, bitmapFormat);
} finally {
out.close();
}
}

Expand All @@ -127,11 +130,10 @@ public static void saveBitmap(
BitmapEncoder.BitmapFormat bitmapFormat)
throws IOException {

List<BufferedImage> chartImages = new LinkedList<BufferedImage>();
List<BufferedImage> chartImages = new LinkedList<>();
for (Chart c : charts) chartImages.add(getBufferedImage(c));

BufferedImage bufferedImage = mergeImages(chartImages, rows, cols);

ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream);
}

Expand Down Expand Up @@ -182,14 +184,12 @@ public static void saveBitmapWithDPI(
setDPI(metadata, DPI);

File file = new File(addFileExtension(fileName, bitmapFormat));
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, metadata);
try {

try (FileImageOutputStream output = new FileImageOutputStream(file)) {
writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, metadata);
writer.write(null, image, iwp);
writer.dispose();
} finally {
output.close();
}
}
}
Expand Down Expand Up @@ -242,14 +242,12 @@ public static void saveJPGWithQuality(Chart chart, String fileName, float qualit
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
File file = new File(fileName);
FileImageOutputStream output = new FileImageOutputStream(file);
try {

try (FileImageOutputStream output = new FileImageOutputStream(file)) {
writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
} finally {
output.close();
}
}

Expand All @@ -266,21 +264,18 @@ public static byte[] getBitmapBytes(Chart chart, BitmapFormat bitmapFormat) thro

byte[] imageInBytes;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos);
try {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ) {
ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos);
baos.flush();
imageInBytes = baos.toByteArray();
} finally {
baos.close();
}
return imageInBytes;
}

public static BufferedImage getBufferedImage(Chart chart) {

BufferedImage bufferedImage =
new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB);
new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = bufferedImage.createGraphics();
chart.paint(graphics2D, chart.getWidth(), chart.getHeight());
return bufferedImage;
Expand Down

0 comments on commit 6c0b4b8

Please sign in to comment.