java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 18:17:13 +0100
changeset 11 f517bafcf812
parent 10 a5a723467a39
child 12 27e41d7f5e8d
permissions -rw-r--r--
stateful RecursiveImageResizer
     1 /**
     2  * copy-image-resizer
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.copyImageResizer;
    19 
    20 import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
    21 import java.awt.image.BufferedImage;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.FileNotFoundException;
    25 import java.io.FileOutputStream;
    26 import java.io.IOException;
    27 import java.io.InputStream;
    28 import java.nio.channels.FileChannel;
    29 import java.util.logging.Level;
    30 import java.util.logging.Logger;
    31 import javax.imageio.ImageIO;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class RecursiveImageResizer {
    38 
    39 	private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
    40 
    41 	private final SingleImageResizer resizer = new SingleImageResizer();
    42 
    43 	private final RecursiveOptions options;
    44 
    45 	public RecursiveImageResizer(RecursiveOptions options) {
    46 		this.options = options;
    47 	}
    48 
    49 	public void resize() throws RecursiveException, ResizeException {
    50 		resizeDirectory(options.getInput());
    51 	}
    52 
    53 	private void resizeFile(File inputFile) throws ResizeException {
    54 		File inputFileRelative = relativize(options.getInput(), inputFile);
    55 		log.log(Level.FINER, "Resizing file: {0}", inputFileRelative);
    56 
    57 		ImageFormat format = ImageFormat.getMatching(inputFile.getName());
    58 
    59 		if (format == null) {
    60 			log.log(Level.FINER, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
    61 		} else {
    62 			try {
    63 				for (SizeSpecification size : options.getSizes()) {
    64 					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    65 					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    66 					try (FileInputStream input = new FileInputStream(inputFile)) {
    67 						BufferedImage image = readImage(input);
    68 						if (shouldResize(image, size)) {
    69 							try (FileOutputStream output = new FileOutputStream(outputFile)) {
    70 								resizer.resize(image, output, size, format);
    71 							}
    72 						} else {
    73 							log.log(Level.FINER, "File: {0} has already required size → just copy", inputFileRelative);
    74 							justCopy(inputFile, outputFile);
    75 						}
    76 					}
    77 				}
    78 			} catch (FileNotFoundException e) {
    79 				throw new ResizeException("Error while opening stream", e);
    80 			} catch (IOException e) {
    81 				throw new ResizeException("Error while closing stream", e);
    82 			}
    83 		}
    84 	}
    85 
    86 	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    87 		if (requested.isResizeSmaller()) {
    88 			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    89 		} else {
    90 			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    91 		}
    92 	}
    93 
    94 	private static BufferedImage readImage(InputStream input) throws ResizeException {
    95 		try {
    96 			return ImageIO.read(input);
    97 		} catch (IOException e) {
    98 			throw new ResizeException("Unable to read image from stream", e);
    99 		}
   100 	}
   101 
   102 	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
   103 		try {
   104 
   105 			if (!outputFile.exists()) {
   106 				outputFile.createNewFile();
   107 			}
   108 
   109 			try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
   110 				try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
   111 					output.transferFrom(input, 0, input.size());
   112 				}
   113 			}
   114 
   115 		} catch (IOException e) {
   116 			throw new ResizeException("Unable copy stream/channel", e);
   117 		}
   118 
   119 	}
   120 
   121 	private void resizeDirectory(File directory) throws ResizeException {
   122 
   123 		log.log(Level.FINE, "Resizing directory: {0}", directory);
   124 
   125 		for (SizeSpecification size : options.getSizes()) {
   126 			File relative = relativize(options.getInput(), directory);
   127 			File sizeRoot = new File(options.getOutput(), size.getDirectory());
   128 			File dir = new File(sizeRoot, relative.getPath());
   129 			dir.mkdirs();
   130 		}
   131 
   132 		for (File entry : directory.listFiles()) {
   133 			if (entry.isDirectory()) {
   134 				resizeDirectory(entry);
   135 			} else {
   136 				resizeFile(entry);
   137 			}
   138 		}
   139 
   140 	}
   141 
   142 	private static File relativize(File root, File child) {
   143 		return root.toPath().relativize(child.toPath()).toFile();
   144 	}
   145 
   146 }