# HG changeset patch # User František Kučera # Date 1416257960 -3600 # Node ID 505031965440f4c7012baf0dfb04e734805543dc # Parent 4634176c6602820cfe562a3fb15daff678f34bb5 preparation for later support of NativeSingleImageResizer diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -0,0 +1,127 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Pattern; +import javax.imageio.ImageIO; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class JavaSingleImageResizer implements SingleImageResizer { + + private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName()); + + @Override + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException { + try { + BufferedImage image = readImage(input); + if (shouldResize(image, size)) { + BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType()); + ImageIO.write(resized, outputFormat.getFormat(), output); + return true; + } else { + return false; + } + } catch (IOException e) { + throw new ResizeException("Error while resizing image", e); + } + } + + private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) { + + int width; + int height; + float aspectRatio = (float) original.getWidth() / (float) original.getHeight(); + + if (aspectRatio > 1) { + width = maxWidth; + height = Math.max(Math.round(maxWidth / aspectRatio), 1); + } else { + width = Math.max(Math.round(maxHeight * aspectRatio), 1); + height = maxHeight; + } + + if (type == BufferedImage.TYPE_CUSTOM) { + log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB"); + type = BufferedImage.TYPE_INT_ARGB; + } + + BufferedImage resized = new BufferedImage(width, height, type); + Graphics2D g = resized.createGraphics(); + g.drawImage(original, 0, 0, width, height, null); + g.dispose(); + + return resized; + } + + private static BufferedImage readImage(InputStream input) throws ResizeException { + try { + return ImageIO.read(input); + } catch (IOException e) { + throw new ResizeException("Unable to read image from stream", e); + } + } + + private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { + if (requested.isResizeSmaller()) { + return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); + } else { + return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); + } + } + + public static enum ImageFormat { + + JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)), + PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)), + GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE)); + + private final String format; + private final Pattern regex; + + private ImageFormat(String format, Pattern regex) { + this.format = format; + this.regex = regex; + } + + /** + * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File) + */ + public String getFormat() { + return format; + } + + public static ImageFormat getMatching(String fileName) { + for (ImageFormat type : values()) { + if (type.regex.matcher(fileName).matches()) { + return type; + } + } + return null; + } + } +} diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/NativeSingleImageResizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/NativeSingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -0,0 +1,39 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class NativeSingleImageResizer implements SingleImageResizer{ + + @Override + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, JavaSingleImageResizer.ImageFormat outputFormat) throws ResizeException { + /** + * TODO: call native tool like convert from ImageMagick + */ + throw new UnsupportedOperationException("Not implemented yet."); + } + + + +} diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 21:58:52 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -17,7 +17,7 @@ */ package cz.frantovo.copyImageResizer; -import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat; +import cz.frantovo.copyImageResizer.JavaSingleImageResizer.ImageFormat; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -38,7 +38,10 @@ private final Counters counters = new Counters(); - private final SingleImageResizer resizer = new SingleImageResizer(); + /** + * TODO: support plugins – several implementations of SingleImageResizer + */ + private final SingleImageResizer resizer = new JavaSingleImageResizer(); private final RecursiveOptions options; diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 21:58:52 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -17,23 +17,14 @@ */ package cz.frantovo.copyImageResizer; -import java.awt.Graphics2D; -import java.awt.image.BufferedImage; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Pattern; -import javax.imageio.ImageIO; /** * * @author Ing. František Kučera (frantovo.cz) */ -public class SingleImageResizer { - - private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName()); +public interface SingleImageResizer { /** * @@ -46,92 +37,5 @@ * }. * @throws ResizeException */ - public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException { - try { - BufferedImage image = readImage(input); - if (shouldResize(image, size)) { - BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType()); - ImageIO.write(resized, outputFormat.getFormat(), output); - return true; - } else { - return false; - } - } catch (IOException e) { - throw new ResizeException("Error while resizing image", e); - } - } - - private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) { - - int width; - int height; - float aspectRatio = (float) original.getWidth() / (float) original.getHeight(); - - if (aspectRatio > 1) { - width = maxWidth; - height = Math.max(Math.round(maxWidth / aspectRatio), 1); - } else { - width = Math.max(Math.round(maxHeight * aspectRatio), 1); - height = maxHeight; - } - - if (type == BufferedImage.TYPE_CUSTOM) { - log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB"); - type = BufferedImage.TYPE_INT_ARGB; - } - - BufferedImage resized = new BufferedImage(width, height, type); - Graphics2D g = resized.createGraphics(); - g.drawImage(original, 0, 0, width, height, null); - g.dispose(); - - return resized; - } - - private static BufferedImage readImage(InputStream input) throws ResizeException { - try { - return ImageIO.read(input); - } catch (IOException e) { - throw new ResizeException("Unable to read image from stream", e); - } - } - - private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { - if (requested.isResizeSmaller()) { - return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); - } else { - return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); - } - } - - public static enum ImageFormat { - - JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)), - PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)), - GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE)); - - private final String format; - private final Pattern regex; - - private ImageFormat(String format, Pattern regex) { - this.format = format; - this.regex = regex; - } - - /** - * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File) - */ - public String getFormat() { - return format; - } - - public static ImageFormat getMatching(String fileName) { - for (ImageFormat type : values()) { - if (type.regex.matcher(fileName).matches()) { - return type; - } - } - return null; - } - } + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, JavaSingleImageResizer.ImageFormat outputFormat) throws ResizeException; }