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

fix: better image reading errors #2632

Merged
merged 1 commit into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -53,7 +53,7 @@ public HeightBrush(
try {
heightMap = ScalableHeightMap.fromPNG(stream);
} catch (IOException e) {
throw new FaweException(Caption.of("fawe.worldedit.brush.brush.height.invalid"));
throw new FaweException(Caption.of("fawe.worldedit.brush.brush.height.invalid", e.getMessage()));
}
} else if (clipboard != null) {
heightMap = ScalableHeightMap.fromClipboard(clipboard, minY, maxY);
Expand Down
Expand Up @@ -38,6 +38,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -70,6 +72,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -519,8 +522,22 @@ public static File copyFile(File sourceFile, File destFile) throws IOException {
return destFile;
}

public static BufferedImage readImage(InputStream in) throws IOException {
return MainUtil.toRGB(ImageIO.read(in));
public static BufferedImage readImage(InputStream stream) throws IOException {
Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
if (!iter.hasNext()) {
throw new IOException("Could not get image reader from stream.");
}
ImageReader reader = iter.next();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(stream, true, true);
BufferedImage bi;
try {
bi = reader.read(0, param);
} finally {
reader.dispose();
stream.close();
}
return MainUtil.toRGB(bi);
}

public static BufferedImage readImage(URL url) throws IOException {
Expand Down