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