X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FDataLoader.java;h=25300130116214d8202bc88e1acc83de98addf3d;hp=2fd59d89764783cd9622fcc0249039dc012f1043;hb=2a25f7814eec9854022f1c9dee188bfbdb955591;hpb=62c63b0724f4bc45999cb2e7186b4b3ada479a0a diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index 2fd59d8..2530013 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -228,16 +229,48 @@ public class DataLoader { public void saveAsImage(URL url, File target) throws IOException { InputStream in = open(url, null, true); try { - ImageIO.write(ImageUtils.fromStream(in), Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), - target); - } catch (IOException e) { - throw new IOException("Cannot write image " + url, e); + saveAsImage(ImageUtils.fromStream(in), target); } finally { in.close(); } } + /** + * Save the given resource as an image on disk using the default image + * format for content. + * + * @param image + * the resource + * @param target + * the target file + * + * @throws IOException + * in case of I/O error + */ + public void saveAsImage(BufferedImage image, File target) + throws IOException { + try { + String format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); + boolean ok = ImageIO.write(image, format, target); + if (!ok) { + // Some formats are not reliable + // Second change: PNG + if (!format.equals("png")) { + ok = ImageIO.write(image, "png", target); + } + + if (!ok) { + throw new IOException( + "Cannot find a writer for this image and format: " + + format); + } + } + } catch (IOException e) { + throw new IOException("Cannot write image to " + target, e); + } + } + /** * Manually add this item to the cache. *