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.IOException; |
24 | import java.io.InputStream; |
25 | import java.util.Map; |
26 | |
27 | import javax.imageio.ImageIO; |
28 | |
29 | import net.sf.jomic.tools.ImageTools; |
30 | import net.sf.jomic.tools.StringTools; |
31 | |
32 | import org.apache.commons.logging.Log; |
33 | import org.apache.commons.logging.LogFactory; |
34 | import org.apache.pdfbox.pdmodel.PDDocument; |
35 | import org.apache.pdfbox.pdmodel.PDPage; |
36 | import org.apache.pdfbox.pdmodel.common.PDRectangle; |
37 | import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; |
38 | import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; |
39 | |
40 | /** |
41 | * Task to create a PDF comic from a set of image files. |
42 | * |
43 | * @author Thomas Aglassinger |
44 | */ |
45 | public class CreatePdfComicTask extends AbstractCreateComicTask |
46 | { |
47 | private static final double WEIGHT_READ = 0.3333; |
48 | private static final double WEIGHT_READ_AND_CONVERT = 0.6667; |
49 | |
50 | private ImageTools imageTools; |
51 | private Log logger; |
52 | private PDDocument pdfDocument; |
53 | private StringTools stringTools; |
54 | |
55 | public CreatePdfComicTask(File newSourceBaseDir, String[] newSourceFileNames, |
56 | Map newImageInfoMap, File newTargetPdfFile, Conversion newConversion) { |
57 | super(newSourceBaseDir, newSourceFileNames, newImageInfoMap, newTargetPdfFile, newConversion); |
58 | |
59 | logger = LogFactory.getLog(CreatePdfComicTask.class); |
60 | imageTools = ImageTools.instance(); |
61 | stringTools = StringTools.instance(); |
62 | } |
63 | |
64 | protected void setUpComic() |
65 | throws Exception { |
66 | pdfDocument = new PDDocument(); |
67 | } |
68 | |
69 | protected void addImageFile(String outName, File imageFile) |
70 | throws Exception { |
71 | assert outName != null; |
72 | assert imageFile != null; |
73 | |
74 | long imageFileLength = imageFile.length(); |
75 | long baseProgress = getProgress(); |
76 | String imageFormat = imageTools.getImageFormat(imageFile); |
77 | |
78 | if (imageFormat == null) { |
79 | if (logger.isInfoEnabled()) { |
80 | logger.info("ignore non-image: " + imageFile); |
81 | } |
82 | } else { |
83 | InputStream imageStream; |
84 | |
85 | if (logger.isInfoEnabled()) { |
86 | logger.info("add " + stringTools.sourced(imageFile)); |
87 | } |
88 | enableAddedAtLeastOneImage(); |
89 | |
90 | boolean isJpegImage = imageFormat.equals("jpeg"); |
91 | boolean needsTrim = getConversion().needsTrim(imageFile); |
92 | |
93 | if (!needsTrim && isJpegImage) { |
94 | // Use original JFIF/JPEG image. |
95 | imageStream = new FileInputStream(imageFile); |
96 | } else { |
97 | RenderedImage image = imageTools.readImage(imageFile); |
98 | |
99 | setProgress(baseProgress + Math.round(WEIGHT_READ * imageFileLength)); |
100 | if (needsTrim) { |
101 | image = getConversion().getTrimmed(image); |
102 | } |
103 | imageStream = createJpegStream(image); |
104 | } |
105 | setProgress(baseProgress + Math.round(WEIGHT_READ_AND_CONVERT * imageFileLength)); |
106 | |
107 | try { |
108 | PDPage page = new PDPage(); |
109 | PDJpeg jpeg = new PDJpeg(pdfDocument, imageStream); |
110 | PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page); |
111 | |
112 | try { |
113 | contentStream.drawImage(jpeg, 0, 0); |
114 | } finally { |
115 | contentStream.close(); |
116 | } |
117 | page.setMediaBox(new PDRectangle(jpeg.getWidth(), jpeg.getHeight())); |
118 | pdfDocument.addPage(page); |
119 | } finally { |
120 | imageStream.close(); |
121 | } |
122 | setProgress(baseProgress + imageFileLength + 1); |
123 | } |
124 | } |
125 | |
126 | protected void cleanUpComic() |
127 | throws Exception { |
128 | if (pdfDocument != null) { |
129 | try { |
130 | if (!isInterrupted() && hasAddedAtLeastOneImage()) { |
131 | pdfDocument.save(getTargetComicFile().getAbsolutePath()); |
132 | } |
133 | pdfDocument.close(); |
134 | } finally { |
135 | pdfDocument = null; |
136 | } |
137 | } |
138 | } |
139 | |
140 | private InputStream createJpegStream(RenderedImage image) |
141 | throws IOException { |
142 | InputStream result; |
143 | ByteArrayOutputStream jpegStream = new ByteArrayOutputStream(); |
144 | |
145 | try { |
146 | ImageIO.write(image, "jpeg", jpegStream); |
147 | |
148 | byte[] imageData = jpegStream.toByteArray(); |
149 | |
150 | result = new ByteArrayInputStream(imageData); |
151 | } finally { |
152 | jpegStream.close(); |
153 | } |
154 | return result; |
155 | } |
156 | } |