X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FImageUtils.java;h=496d2ead7155b8cd21f0989a17e9e98e43737faf;hp=9f1e06f36806d57c3876dc38b6400d4d4b371412;hb=4b7d32e77cf31ca44c5f93f08d2729357c86b93f;hpb=217a3310efed6da8b1765738e6386825b3002570 diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index 9f1e06f..496d2ea 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -120,13 +120,32 @@ public class ImageUtils { return fromStream(in); } + /** + * A shorthand method to create an {@link ImageText} and return its output. + * + * @param image + * the source {@link Image} + * @param size + * the final text size to target + * @param mode + * the mode of conversion + * @param invert + * TRUE to invert colours rendering + * + * @return the text image + */ + static public String toAscii(Image image, Dimension size, Mode mode, + boolean invert) { + return new ImageText(image, size, mode, invert).toString(); + } + /** * Convert the given {@link InputStream} (which should allow calls to * {@link InputStream#reset()} for better perfs) into an {@link Image} * object, respecting the EXIF transformations if any. * * @param in - * the 'resetable' {@link InputStream} + * the {@link InputStream} * * @return the {@link Image} object * @@ -137,21 +156,49 @@ public class ImageUtils { MarkableFileInputStream tmpIn = null; File tmp = null; - boolean repack = !in.markSupported(); - if (!repack) { + boolean resetable = in.markSupported(); + if (resetable) { try { in.reset(); } catch (IOException e) { - repack = true; + resetable = false; } } - if (repack) { - tmp = File.createTempFile(".tmp-image", ".tmp"); - tmp.deleteOnExit(); + if (resetable) { + return fromResetableStream(in); + } + + tmp = File.createTempFile(".tmp-image", ".tmp"); + try { IOUtils.write(in, tmp); tmpIn = new MarkableFileInputStream(new FileInputStream(tmp)); + return fromResetableStream(tmpIn); + } finally { + try { + if (tmpIn != null) { + tmpIn.close(); + } + } finally { + tmp.delete(); + } } + } + + /** + * Convert the given resetable {@link InputStream} into an {@link Image} + * object, respecting the EXIF transformations if any. + * + * @param in + * the 'resetable' (this is mandatory) {@link InputStream} + * + * @return the {@link Image} object + * + * @throws IOException + * in case of IO error + */ + static private BufferedImage fromResetableStream(InputStream in) + throws IOException { int orientation; try { @@ -165,10 +212,6 @@ public class ImageUtils { BufferedImage image = ImageIO.read(in); if (image == null) { - if (tmp != null) { - tmp.delete(); - tmpIn.close(); - } throw new IOException("Failed to convert input to image"); } @@ -230,33 +273,9 @@ public class ImageUtils { } // - if (tmp != null) { - tmp.delete(); - tmpIn.close(); - } - return image; } - /** - * A shorthand method to create an {@link ImageText} and return its output. - * - * @param image - * the source {@link Image} - * @param size - * the final text size to target - * @param mode - * the mode of conversion - * @param invert - * TRUE to invert colours rendering - * - * @return the text image - */ - static public String toAscii(Image image, Dimension size, Mode mode, - boolean invert) { - return new ImageText(image, size, mode, invert).toString(); - } - /** * Return the EXIF transformation flag of this image if any. *