preparation for later support of NativeSingleImageResizer
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 21:59:20 +0100
changeset 17505031965440
parent 16 4634176c6602
child 18 a5a36526ff71
preparation for later support of NativeSingleImageResizer
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/NativeSingleImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java	Mon Nov 17 21:59:20 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/**
     1.5 + * copy-image-resizer
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.copyImageResizer;
    1.22 +
    1.23 +import java.awt.Graphics2D;
    1.24 +import java.awt.image.BufferedImage;
    1.25 +import java.io.IOException;
    1.26 +import java.io.InputStream;
    1.27 +import java.io.OutputStream;
    1.28 +import java.util.logging.Level;
    1.29 +import java.util.logging.Logger;
    1.30 +import java.util.regex.Pattern;
    1.31 +import javax.imageio.ImageIO;
    1.32 +
    1.33 +/**
    1.34 + *
    1.35 + * @author Ing. František Kučera (frantovo.cz)
    1.36 + */
    1.37 +public class JavaSingleImageResizer implements SingleImageResizer {
    1.38 +
    1.39 +	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
    1.40 +
    1.41 +	@Override
    1.42 +	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    1.43 +		try {
    1.44 +			BufferedImage image = readImage(input);
    1.45 +			if (shouldResize(image, size)) {
    1.46 +				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
    1.47 +				ImageIO.write(resized, outputFormat.getFormat(), output);
    1.48 +				return true;
    1.49 +			} else {
    1.50 +				return false;
    1.51 +			}
    1.52 +		} catch (IOException e) {
    1.53 +			throw new ResizeException("Error while resizing image", e);
    1.54 +		}
    1.55 +	}
    1.56 +
    1.57 +	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
    1.58 +
    1.59 +		int width;
    1.60 +		int height;
    1.61 +		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
    1.62 +
    1.63 +		if (aspectRatio > 1) {
    1.64 +			width = maxWidth;
    1.65 +			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
    1.66 +		} else {
    1.67 +			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
    1.68 +			height = maxHeight;
    1.69 +		}
    1.70 +
    1.71 +		if (type == BufferedImage.TYPE_CUSTOM) {
    1.72 +			log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
    1.73 +			type = BufferedImage.TYPE_INT_ARGB;
    1.74 +		}
    1.75 +
    1.76 +		BufferedImage resized = new BufferedImage(width, height, type);
    1.77 +		Graphics2D g = resized.createGraphics();
    1.78 +		g.drawImage(original, 0, 0, width, height, null);
    1.79 +		g.dispose();
    1.80 +
    1.81 +		return resized;
    1.82 +	}
    1.83 +
    1.84 +	private static BufferedImage readImage(InputStream input) throws ResizeException {
    1.85 +		try {
    1.86 +			return ImageIO.read(input);
    1.87 +		} catch (IOException e) {
    1.88 +			throw new ResizeException("Unable to read image from stream", e);
    1.89 +		}
    1.90 +	}
    1.91 +
    1.92 +	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    1.93 +		if (requested.isResizeSmaller()) {
    1.94 +			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    1.95 +		} else {
    1.96 +			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    1.97 +		}
    1.98 +	}
    1.99 +
   1.100 +	public static enum ImageFormat {
   1.101 +
   1.102 +		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
   1.103 +		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
   1.104 +		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
   1.105 +
   1.106 +		private final String format;
   1.107 +		private final Pattern regex;
   1.108 +
   1.109 +		private ImageFormat(String format, Pattern regex) {
   1.110 +			this.format = format;
   1.111 +			this.regex = regex;
   1.112 +		}
   1.113 +
   1.114 +		/**
   1.115 +		 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
   1.116 +		 */
   1.117 +		public String getFormat() {
   1.118 +			return format;
   1.119 +		}
   1.120 +
   1.121 +		public static ImageFormat getMatching(String fileName) {
   1.122 +			for (ImageFormat type : values()) {
   1.123 +				if (type.regex.matcher(fileName).matches()) {
   1.124 +					return type;
   1.125 +				}
   1.126 +			}
   1.127 +			return null;
   1.128 +		}
   1.129 +	}
   1.130 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/NativeSingleImageResizer.java	Mon Nov 17 21:59:20 2014 +0100
     2.3 @@ -0,0 +1,39 @@
     2.4 +/**
     2.5 + * copy-image-resizer
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.copyImageResizer;
    2.22 +
    2.23 +import java.io.InputStream;
    2.24 +import java.io.OutputStream;
    2.25 +
    2.26 +/**
    2.27 + *
    2.28 + * @author Ing. František Kučera (frantovo.cz)
    2.29 + */
    2.30 +public class NativeSingleImageResizer implements SingleImageResizer{
    2.31 +
    2.32 +	@Override
    2.33 +	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, JavaSingleImageResizer.ImageFormat outputFormat) throws ResizeException {
    2.34 +		/**
    2.35 +		 * TODO: call native tool like convert from ImageMagick
    2.36 +		 */
    2.37 +		throw new UnsupportedOperationException("Not implemented yet.");
    2.38 +	}
    2.39 +	
    2.40 +	
    2.41 +
    2.42 +}
     3.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 21:58:52 2014 +0100
     3.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 21:59:20 2014 +0100
     3.3 @@ -17,7 +17,7 @@
     3.4   */
     3.5  package cz.frantovo.copyImageResizer;
     3.6  
     3.7 -import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
     3.8 +import cz.frantovo.copyImageResizer.JavaSingleImageResizer.ImageFormat;
     3.9  import java.io.File;
    3.10  import java.io.FileInputStream;
    3.11  import java.io.FileNotFoundException;
    3.12 @@ -38,7 +38,10 @@
    3.13  
    3.14  	private final Counters counters = new Counters();
    3.15  
    3.16 -	private final SingleImageResizer resizer = new SingleImageResizer();
    3.17 +	/**
    3.18 +	 * TODO: support plugins – several implementations of SingleImageResizer
    3.19 +	 */
    3.20 +	private final SingleImageResizer resizer = new JavaSingleImageResizer();
    3.21  
    3.22  	private final RecursiveOptions options;
    3.23  
     4.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 21:58:52 2014 +0100
     4.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 21:59:20 2014 +0100
     4.3 @@ -17,23 +17,14 @@
     4.4   */
     4.5  package cz.frantovo.copyImageResizer;
     4.6  
     4.7 -import java.awt.Graphics2D;
     4.8 -import java.awt.image.BufferedImage;
     4.9 -import java.io.IOException;
    4.10  import java.io.InputStream;
    4.11  import java.io.OutputStream;
    4.12 -import java.util.logging.Level;
    4.13 -import java.util.logging.Logger;
    4.14 -import java.util.regex.Pattern;
    4.15 -import javax.imageio.ImageIO;
    4.16  
    4.17  /**
    4.18   *
    4.19   * @author Ing. František Kučera (frantovo.cz)
    4.20   */
    4.21 -public class SingleImageResizer {
    4.22 -
    4.23 -	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
    4.24 +public interface SingleImageResizer {
    4.25  
    4.26  	/**
    4.27  	 *
    4.28 @@ -46,92 +37,5 @@
    4.29  	 * }.
    4.30  	 * @throws ResizeException
    4.31  	 */
    4.32 -	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    4.33 -		try {
    4.34 -			BufferedImage image = readImage(input);
    4.35 -			if (shouldResize(image, size)) {
    4.36 -				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
    4.37 -				ImageIO.write(resized, outputFormat.getFormat(), output);
    4.38 -				return true;
    4.39 -			} else {
    4.40 -				return false;
    4.41 -			}
    4.42 -		} catch (IOException e) {
    4.43 -			throw new ResizeException("Error while resizing image", e);
    4.44 -		}
    4.45 -	}
    4.46 -
    4.47 -	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
    4.48 -
    4.49 -		int width;
    4.50 -		int height;
    4.51 -		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
    4.52 -
    4.53 -		if (aspectRatio > 1) {
    4.54 -			width = maxWidth;
    4.55 -			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
    4.56 -		} else {
    4.57 -			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
    4.58 -			height = maxHeight;
    4.59 -		}
    4.60 -
    4.61 -		if (type == BufferedImage.TYPE_CUSTOM) {
    4.62 -			log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
    4.63 -			type = BufferedImage.TYPE_INT_ARGB;
    4.64 -		}
    4.65 -
    4.66 -		BufferedImage resized = new BufferedImage(width, height, type);
    4.67 -		Graphics2D g = resized.createGraphics();
    4.68 -		g.drawImage(original, 0, 0, width, height, null);
    4.69 -		g.dispose();
    4.70 -
    4.71 -		return resized;
    4.72 -	}
    4.73 -
    4.74 -	private static BufferedImage readImage(InputStream input) throws ResizeException {
    4.75 -		try {
    4.76 -			return ImageIO.read(input);
    4.77 -		} catch (IOException e) {
    4.78 -			throw new ResizeException("Unable to read image from stream", e);
    4.79 -		}
    4.80 -	}
    4.81 -
    4.82 -	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    4.83 -		if (requested.isResizeSmaller()) {
    4.84 -			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    4.85 -		} else {
    4.86 -			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    4.87 -		}
    4.88 -	}
    4.89 -
    4.90 -	public static enum ImageFormat {
    4.91 -
    4.92 -		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
    4.93 -		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
    4.94 -		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
    4.95 -
    4.96 -		private final String format;
    4.97 -		private final Pattern regex;
    4.98 -
    4.99 -		private ImageFormat(String format, Pattern regex) {
   4.100 -			this.format = format;
   4.101 -			this.regex = regex;
   4.102 -		}
   4.103 -
   4.104 -		/**
   4.105 -		 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
   4.106 -		 */
   4.107 -		public String getFormat() {
   4.108 -			return format;
   4.109 -		}
   4.110 -
   4.111 -		public static ImageFormat getMatching(String fileName) {
   4.112 -			for (ImageFormat type : values()) {
   4.113 -				if (type.regex.matcher(fileName).matches()) {
   4.114 -					return type;
   4.115 -				}
   4.116 -			}
   4.117 -			return null;
   4.118 -		}
   4.119 -	}
   4.120 +	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, JavaSingleImageResizer.ImageFormat outputFormat) throws ResizeException;
   4.121  }