1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.comic; |
17 | |
18 | import java.awt.image.RenderedImage; |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | import java.util.Iterator; |
22 | |
23 | import javax.imageio.ImageIO; |
24 | import javax.imageio.ImageWriter; |
25 | import javax.imageio.stream.ImageOutputStream; |
26 | |
27 | import net.sf.jomic.tools.AbstractTask; |
28 | import net.sf.jomic.tools.FileTools; |
29 | import net.sf.jomic.tools.CopyFileTask; |
30 | import net.sf.jomic.tools.ImageTools; |
31 | import net.sf.jomic.tools.ProgressChangeListener; |
32 | import net.sf.jomic.tools.Task; |
33 | |
34 | /** |
35 | * Task to convert a source image to a target image according to a <code>Conversion</code>. |
36 | * |
37 | * @author Thomas Aglassinger |
38 | * @see Conversion |
39 | */ |
40 | public class ConvertImageTask extends AbstractTask implements ProgressChangeListener |
41 | { |
42 | private static final int STEP_COUNT = 3; |
43 | private Conversion conversion; |
44 | private FileTools fileTools; |
45 | private ImageTools imageTools; |
46 | private File sourceImageFile; |
47 | private File targetImageFile; |
48 | |
49 | public ConvertImageTask(File newSourceImageFile, File newTargetImageFile, Conversion newConversion) { |
50 | super(); |
51 | assert newSourceImageFile != null; |
52 | assert newTargetImageFile != null; |
53 | assert newConversion != null; |
54 | fileTools = FileTools.instance(); |
55 | imageTools = ImageTools.instance(); |
56 | sourceImageFile = newSourceImageFile; |
57 | targetImageFile = newTargetImageFile; |
58 | conversion = newConversion; |
59 | setMaxProgress(sourceImageFile.length()); |
60 | } |
61 | |
62 | private ImageWriter getWriterForSuffix(String suffix) { |
63 | assert suffix != null; |
64 | Iterator writers = ImageIO.getImageWritersByFormatName(suffix); |
65 | ImageWriter result = (ImageWriter) writers.next(); |
66 | |
67 | return result; |
68 | } |
69 | |
70 | /** |
71 | * In case the image just needs to be copied, set progress to value from <code>CopyFileTask</code> |
72 | * . |
73 | * |
74 | * @see CopyFileTask |
75 | */ |
76 | public void progressChanged(Task source) { |
77 | setProgress(source.getProgress()); |
78 | } |
79 | |
80 | public void start() |
81 | throws IOException { |
82 | RenderedImage image = null; |
83 | String writerSuffix = fileTools.getSuffix(sourceImageFile); |
84 | // TODO: if no writer can be found, use PNG |
85 | // TODO: dispose both ImageWriters properly (using finally) |
86 | ImageWriter imageWriter = getWriterForSuffix(writerSuffix); |
87 | boolean justCopy = true; |
88 | |
89 | setProgress(0); |
90 | if (conversion.isLimitImageSize()) { |
91 | image = imageTools.readImage(sourceImageFile); |
92 | setProgress(getMaxProgress() / STEP_COUNT); |
93 | |
94 | boolean needsResize = false; |
95 | int maxImageHeight = conversion.getMaxImageHeight(); |
96 | |
97 | if (image.getHeight() > maxImageHeight) { |
98 | needsResize = true; |
99 | } |
100 | int maxImageWidth = conversion.getMaxImageWidth(); |
101 | |
102 | if (image.getWidth() > maxImageWidth) { |
103 | needsResize = true; |
104 | } |
105 | if (needsResize) { |
106 | image = imageTools.getSqueezed(image, maxImageWidth, maxImageHeight, ImageTools.SCALE_FIT); |
107 | setProgress(2 * getMaxProgress() / STEP_COUNT); |
108 | justCopy = false; |
109 | } |
110 | } |
111 | String targetSuffix = conversion.getImageFormat(); |
112 | |
113 | if (!targetSuffix.equals(Conversion.IMAGE_FORMAT_PRESERVE)) { |
114 | ImageWriter requestedSuffixWriter = getWriterForSuffix(targetSuffix); |
115 | |
116 | if (!requestedSuffixWriter.equals(imageWriter)) { |
117 | imageWriter.dispose(); |
118 | imageWriter = requestedSuffixWriter; |
119 | justCopy = false; |
120 | } else { |
121 | requestedSuffixWriter.dispose(); |
122 | } |
123 | } |
124 | if (justCopy) { |
125 | CopyFileTask copyFileTask = new CopyFileTask(sourceImageFile, targetImageFile); |
126 | |
127 | copyFileTask.addProgressChangeListener(this); |
128 | try { |
129 | copyFileTask.start(); |
130 | } finally { |
131 | copyFileTask.removeProgressChangeListener(this); |
132 | } |
133 | } else { |
134 | if (image == null) { |
135 | image = imageTools.readImage(sourceImageFile); |
136 | setProgress(getMaxProgress() / 2); |
137 | } |
138 | try { |
139 | writeImage(image, imageWriter); |
140 | } finally { |
141 | imageWriter.dispose(); |
142 | } |
143 | } |
144 | setProgress(getMaxProgress()); |
145 | } |
146 | |
147 | private void writeImage(RenderedImage image, ImageWriter imageWriter) |
148 | throws IOException { |
149 | ImageOutputStream ios = ImageIO.createImageOutputStream(targetImageFile); |
150 | |
151 | try { |
152 | imageWriter.setOutput(ios); |
153 | imageWriter.write(image); |
154 | } finally { |
155 | ios.close(); |
156 | } |
157 | } |
158 | } |