EMMA Coverage Report (generated Sat Oct 08 11:41:37 CEST 2011)
[all classes][net.sf.jomic.comic]

COVERAGE SUMMARY FOR SOURCE FILE [CreatePdfComicTask.java]

nameclass, %method, %block, %line, %
CreatePdfComicTask.java100% (1/1)100% (6/6)87%  (245/282)96%  (52.6/55)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreatePdfComicTask100% (1/1)100% (6/6)87%  (245/282)96%  (52.6/55)
cleanUpComic (): void 100% (1/1)79%  (23/29)93%  (6.5/7)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
CreatePdfComicTask (File, String [], Map, File, Conversion): void 100% (1/1)80%  (20/25)96%  (4.8/5)
createJpegStream (RenderedImage): InputStream 100% (1/1)81%  (22/27)97%  (6.8/7)
addImageFile (String, File): void 100% (1/1)90%  (162/180)96%  (31.7/33)
setUpComic (): void 100% (1/1)100% (6/6)100% (2/2)

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/>.
16package net.sf.jomic.comic;
17 
18import java.awt.image.RenderedImage;
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.Map;
26 
27import javax.imageio.ImageIO;
28 
29import net.sf.jomic.tools.ImageTools;
30import net.sf.jomic.tools.StringTools;
31 
32import org.apache.commons.logging.Log;
33import org.apache.commons.logging.LogFactory;
34import org.apache.pdfbox.pdmodel.PDDocument;
35import org.apache.pdfbox.pdmodel.PDPage;
36import org.apache.pdfbox.pdmodel.common.PDRectangle;
37import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
38import 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 */
45public 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}

[all classes][net.sf.jomic.comic]
EMMA 2.0.4217 (C) Vladimir Roubtsov