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.ByteArrayInputStream; |
20 | import java.io.ByteArrayOutputStream; |
21 | import java.io.File; |
22 | import java.io.FileInputStream; |
23 | import java.io.FileOutputStream; |
24 | import java.io.IOException; |
25 | import java.io.InputStream; |
26 | import java.util.Map; |
27 | import java.util.zip.Deflater; |
28 | import java.util.zip.ZipEntry; |
29 | import java.util.zip.ZipException; |
30 | import java.util.zip.ZipOutputStream; |
31 | |
32 | import javax.imageio.ImageIO; |
33 | import javax.imageio.stream.ImageInputStream; |
34 | |
35 | import net.sf.jomic.tools.FileTools; |
36 | import net.sf.jomic.tools.ImageInfo; |
37 | import net.sf.jomic.tools.ImageTools; |
38 | import net.sf.jomic.tools.StringTools; |
39 | |
40 | import org.apache.commons.logging.Log; |
41 | import org.apache.commons.logging.LogFactory; |
42 | |
43 | /** |
44 | * Task to create a CBZ comic from a set of image files. |
45 | * |
46 | * @author Thomas Aglassinger |
47 | */ |
48 | // TODO: Use antzip to encode zipped filenames using ISO-Latin1. |
49 | public class CreateCbzComicTask extends AbstractCreateComicTask |
50 | { |
51 | private static final double STEP_WEIGHT_READ_IMAGE = 0.333; |
52 | private static final double STEP_WEIGHT_TRIM_IMAGE = 0.667; |
53 | |
54 | private FileTools fileTools; |
55 | private ImageTools imageTools; |
56 | private Log logger; |
57 | private ZipOutputStream out; |
58 | private StringTools stringTools; |
59 | |
60 | /** |
61 | * Create a new task to create a CBZ comic. |
62 | * |
63 | * @param newSourceBaseDir the base directory where the source files are located |
64 | * @param newSourceFileNames the names of the files relative to <code>newSourceBaseDir</code> |
65 | * that should be included in the archive |
66 | * @param newComicInfoMap Description of the parameter |
67 | * @param newTargetZipFile the ZIP file to create |
68 | * @param newConversion a conversion specifying the format and image manipulations of the |
69 | * new comic; if null, the default will be a conversion creating a CBZ including all the |
70 | * images specified. |
71 | */ |
72 | public CreateCbzComicTask(File newSourceBaseDir, String[] newSourceFileNames, |
73 | Map newComicInfoMap, File newTargetZipFile, Conversion newConversion) { |
74 | super(newSourceBaseDir, newSourceFileNames, newComicInfoMap, newTargetZipFile, newConversion); |
75 | assert newTargetZipFile != null; |
76 | assert newSourceBaseDir != null; |
77 | assert newSourceFileNames != null; |
78 | assert newSourceFileNames.length > 0; |
79 | logger = LogFactory.getLog(CreateCbzComicTask.class); |
80 | fileTools = FileTools.instance(); |
81 | imageTools = ImageTools.instance(); |
82 | stringTools = StringTools.instance(); |
83 | } |
84 | |
85 | protected void setUpComic() |
86 | throws Exception { |
87 | out = new ZipOutputStream(new FileOutputStream(getTargetComicFile())); |
88 | } |
89 | |
90 | protected void addImageFile(String outImageName, File sourceImageFile) |
91 | throws Exception { |
92 | assert out != null; |
93 | assert outImageName != null; |
94 | assert sourceImageFile != null; |
95 | ImageInputStream sourceImageStream = ImageIO.createImageInputStream(sourceImageFile); |
96 | int compressionLevel = Deflater.DEFAULT_COMPRESSION; |
97 | |
98 | if (sourceImageStream != null) { |
99 | try { |
100 | ImageInfo imageInfo = getImageInfo(sourceImageFile); |
101 | long baseProgress = getProgress(); |
102 | |
103 | if (getConversion().isAddable(imageInfo)) { |
104 | String sourceImageFormat = imageInfo.getFormat(); |
105 | String targetImageFormat = getConversion().getImageFormatName(sourceImageFormat); |
106 | boolean needsNewFormat = !sourceImageFormat.equals(targetImageFormat); |
107 | boolean needsTrim = getConversion().needsTrim(sourceImageFile); |
108 | long inFileLength = sourceImageFile.length(); |
109 | |
110 | if (logger.isInfoEnabled()) { |
111 | logger.info("add " + stringTools.sourced(sourceImageFile) + " as " |
112 | + stringTools.sourced(outImageName)); |
113 | } |
114 | |
115 | InputStream in; |
116 | |
117 | if (needsTrim || needsNewFormat) { |
118 | RenderedImage image = imageTools.readImage(sourceImageFile); |
119 | |
120 | setProgress(baseProgress + Math.round(STEP_WEIGHT_READ_IMAGE * inFileLength)); |
121 | if (needsTrim) { |
122 | image = getConversion().getTrimmed(image); |
123 | } |
124 | in = createInputStream(image, targetImageFormat); |
125 | setProgress(baseProgress + Math.round(STEP_WEIGHT_TRIM_IMAGE * inFileLength)); |
126 | } else { |
127 | in = new FileInputStream(sourceImageFile); |
128 | } |
129 | if (imageTools.isCompressedImageFormat(targetImageFormat)) { |
130 | compressionLevel = Deflater.NO_COMPRESSION; |
131 | } |
132 | out.setLevel(compressionLevel); |
133 | addZipEntry(outImageName, sourceImageFile, in); |
134 | } else { |
135 | if (logger.isInfoEnabled()) { |
136 | if (imageInfo.hasError()) { |
137 | logger.info("ignoring broken image: " + stringTools.sourced(sourceImageFile), |
138 | imageInfo.getError()); |
139 | } else { |
140 | logger.info("skipping non-image: " + stringTools.sourced(sourceImageFile) |
141 | + "; format=" + imageInfo.getFormat() + ", size=" + imageInfo.getSize()); |
142 | } |
143 | } |
144 | } |
145 | setProgress(baseProgress + sourceImageFile.length() + 1); |
146 | } finally { |
147 | sourceImageStream.close(); |
148 | } |
149 | } |
150 | } |
151 | |
152 | protected void cleanUpComic() |
153 | throws Exception { |
154 | if (out != null) { |
155 | try { |
156 | out.close(); |
157 | } catch (ZipException error) { |
158 | logger.warn("ignoring attempt to close zip file without entries", error); |
159 | } finally { |
160 | out = null; |
161 | } |
162 | } |
163 | } |
164 | |
165 | private void addZipEntry(String outName, File sourceImageFile, InputStream in) |
166 | throws IOException { |
167 | try { |
168 | ZipEntry zipEntry = new ZipEntry(outName); |
169 | |
170 | zipEntry.setTime(sourceImageFile.lastModified()); |
171 | out.putNextEntry(zipEntry); |
172 | fileTools.copy(in, out); |
173 | enableAddedAtLeastOneImage(); |
174 | } finally { |
175 | in.close(); |
176 | } |
177 | out.closeEntry(); |
178 | } |
179 | |
180 | private InputStream createInputStream(RenderedImage image, String imageFormat) |
181 | throws IOException { |
182 | InputStream result; |
183 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
184 | |
185 | try { |
186 | ImageIO.write(image, imageFormat, byteStream); |
187 | |
188 | byte[] imageData = byteStream.toByteArray(); |
189 | |
190 | result = new ByteArrayInputStream(imageData); |
191 | } finally { |
192 | byteStream.close(); |
193 | } |
194 | return result; |
195 | } |
196 | } |