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