java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:13:15 +0100
changeset 13 28aa5f597457
parent 12 27e41d7f5e8d
child 14 dec0dd934a64
permissions -rw-r--r--
increment counters
     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 Counters counters = new Counters();
    42 
    43 	private final SingleImageResizer resizer = new SingleImageResizer();
    44 
    45 	private final RecursiveOptions options;
    46 
    47 	public RecursiveImageResizer(RecursiveOptions options) {
    48 		this.options = options;
    49 	}
    50 
    51 	public Counters resize() throws RecursiveException, ResizeException {
    52 		resizeDirectory(options.getInput());
    53 		return counters;
    54 	}
    55 
    56 	private void resizeFile(File inputFile) throws ResizeException {
    57 		File inputFileRelative = relativize(options.getInput(), inputFile);
    58 		log.log(Level.FINER, "Resizing file: {0}", inputFileRelative);
    59 		counters.increment(Counters.COUNTER_TYPE.FILES);
    60 
    61 		ImageFormat format = ImageFormat.getMatching(inputFile.getName());
    62 
    63 		if (format == null) {
    64 			log.log(Level.FINER, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
    65 			counters.increment(Counters.COUNTER_TYPE.SKIPPED_UNKNOWN_EXTENSION);
    66 		} else {
    67 			try {
    68 				for (SizeSpecification size : options.getSizes()) {
    69 					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    70 					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    71 					try (FileInputStream input = new FileInputStream(inputFile)) {
    72 						BufferedImage image = readImage(input);
    73 						if (shouldResize(image, size)) {
    74 							try (FileOutputStream output = new FileOutputStream(outputFile)) {
    75 								resizer.resize(image, output, size, format);
    76 								counters.increment(Counters.COUNTER_TYPE.RESIZED);
    77 							}
    78 						} else {
    79 							log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative);
    80 							justCopy(inputFile, outputFile);
    81 							counters.increment(Counters.COUNTER_TYPE.JUST_COPIED);
    82 						}
    83 					}
    84 				}
    85 			} catch (FileNotFoundException e) {
    86 				throw new ResizeException("Error while opening stream", e);
    87 			} catch (IOException e) {
    88 				throw new ResizeException("Error while closing stream", e);
    89 			}
    90 		}
    91 	}
    92 
    93 	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    94 		if (requested.isResizeSmaller()) {
    95 			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    96 		} else {
    97 			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    98 		}
    99 	}
   100 
   101 	private static BufferedImage readImage(InputStream input) throws ResizeException {
   102 		try {
   103 			return ImageIO.read(input);
   104 		} catch (IOException e) {
   105 			throw new ResizeException("Unable to read image from stream", e);
   106 		}
   107 	}
   108 
   109 	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
   110 		try {
   111 
   112 			if (!outputFile.exists()) {
   113 				outputFile.createNewFile();
   114 			}
   115 
   116 			try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
   117 				try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
   118 					output.transferFrom(input, 0, input.size());
   119 				}
   120 			}
   121 
   122 		} catch (IOException e) {
   123 			throw new ResizeException("Unable copy stream/channel", e);
   124 		}
   125 
   126 	}
   127 
   128 	private void resizeDirectory(File directory) throws ResizeException {
   129 
   130 		log.log(Level.FINE, "Resizing directory: {0}", directory);
   131 		counters.increment(Counters.COUNTER_TYPE.DIRECTORIES);
   132 
   133 		for (SizeSpecification size : options.getSizes()) {
   134 			File relative = relativize(options.getInput(), directory);
   135 			File sizeRoot = new File(options.getOutput(), size.getDirectory());
   136 			File dir = new File(sizeRoot, relative.getPath());
   137 			dir.mkdirs();
   138 		}
   139 
   140 		for (File entry : directory.listFiles()) {
   141 			if (entry.isDirectory()) {
   142 				resizeDirectory(entry);
   143 			} else {
   144 				resizeFile(entry);
   145 			}
   146 		}
   147 
   148 	}
   149 
   150 	private static File relativize(File root, File child) {
   151 		return root.toPath().relativize(child.toPath()).toFile();
   152 	}
   153 
   154 }