EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [ExtractPdfImagesTask.java]

nameclass, %method, %block, %line, %
ExtractPdfImagesTask.java100% (1/1)100% (4/4)80%  (280/352)87%  (68.6/79)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ExtractPdfImagesTask100% (1/1)100% (4/4)80%  (280/352)87%  (68.6/79)
start (): void 100% (1/1)69%  (158/229)78%  (36.6/47)
ExtractPdfImagesTask (): void 100% (1/1)95%  (19/20)99%  (5/5)
ExtractPdfImagesTask (File, File): void 100% (1/1)100% (16/16)100% (5/5)
getTargetImageNames (List): List 100% (1/1)100% (87/87)100% (22/22)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 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.tools;
17 
18import java.io.File;
19import java.io.IOException;
20import java.text.DecimalFormat;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24import java.util.Map;
25 
26import org.pdfbox.pdmodel.PDDocument;
27import org.pdfbox.pdmodel.PDPage;
28import org.pdfbox.pdmodel.PDResources;
29import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
30 
31import net.sf.wraplog.Logger;
32 
33/**
34 *  Task to extract all images from a PDF file to a specified target folder. The images will be
35 *  named "00.jpg", "01.jpg" and so on, with the actual number of digits depending on the number of
36 *  images in the PDF.
37 *
38 * @author    Thomas Aglassinger
39 */
40public class ExtractPdfImagesTask extends AbstractTask
41{
42    private static final double WEIGHT_IMAGE_NAMES = 0.10;
43    private static final double WEIGHT_LOAD = 0.10;
44    private FileTools fileTools;
45 
46    private Logger logger;
47    private File pdfFile;
48    private StringTools stringTools;
49    private File targetFolder;
50 
51    public ExtractPdfImagesTask(File newPdfFile, File newTargetFolder) {
52        this();
53        pdfFile = newPdfFile;
54        targetFolder = newTargetFolder;
55        setMaxProgress(pdfFile.length() + 1);
56    }
57 
58    private ExtractPdfImagesTask() {
59        super();
60        logger = Logger.getLogger(ExtractPdfImagesTask.class);
61        fileTools = FileTools.instance();
62        stringTools = StringTools.instance();
63    }
64 
65    private List getTargetImageNames(List pages)
66        throws IOException {
67        List result = new ArrayList();
68        Iterator pageRider = pages.iterator();
69 
70        while (pageRider.hasNext()) {
71            PDPage page = (PDPage) pageRider.next();
72            PDResources resources = page.getResources();
73            Map images = resources.getImages();
74 
75            if (images != null) {
76                Iterator imageRider = images.keySet().iterator();
77 
78                while (imageRider.hasNext()) {
79                    String key = (String) imageRider.next();
80                    PDXObjectImage image = (PDXObjectImage) images.get(key);
81                    String suffix = image.getSuffix();
82 
83                    result.add(suffix);
84                }
85            }
86        }
87 
88        // Assign numeric names to all images, but preserve the original suffix.
89        int imageCount = result.size();
90 
91        if (imageCount > 0) {
92            DecimalFormat format = stringTools.getLeadingZeroFormat(imageCount);
93 
94            for (int i = 0; i < imageCount; i += 1) {
95                String name = format.format(i) + "." + result.get(i);
96 
97                result.set(i, name);
98            }
99        }
100        return result;
101    }
102 
103    public void start()
104        throws Exception {
105        setProgress(0);
106 
107        boolean allFilesExtracted = false;
108        List targetImageNames = null;
109        PDDocument pdf = PDDocument.load(pdfFile);
110 
111        setProgress(Math.round(WEIGHT_LOAD * getMaxProgress()));
112 
113        try {
114            List pages = pdf.getDocumentCatalog().getAllPages();
115 
116            targetImageNames = getTargetImageNames(pages);
117 
118            int imageCount = targetImageNames.size();
119 
120            setProgress(Math.round((WEIGHT_LOAD + WEIGHT_IMAGE_NAMES) * getMaxProgress()));
121 
122            long progressWhenStartingToExtract = getProgress();
123            long maxExtractProgress = (getMaxProgress() - progressWhenStartingToExtract);
124            int imageIndex = 0;
125            Iterator pageRider = pages.iterator();
126 
127            fileTools.mkdirs(targetFolder);
128            while (pageRider.hasNext()) {
129                PDPage page = (PDPage) pageRider.next();
130                PDResources resources = page.getResources();
131                Map images = resources.getImages();
132 
133                if (images != null) {
134                    Iterator imageRider = images.keySet().iterator();
135 
136                    while (imageRider.hasNext()) {
137                        String key = (String) imageRider.next();
138                        PDXObjectImage image = (PDXObjectImage) images.get(key);
139                        File imageFile = new File(targetFolder, (String) targetImageNames.get(imageIndex));
140                        String name = imageFile.getAbsolutePath();
141 
142                        if (logger.isInfoEnabled()) {
143                            logger.info("extracting image: " + stringTools.sourced(name));
144                        }
145                        image.write2file(fileTools.getWithoutLastSuffix(name));
146                        imageIndex += 1;
147 
148                        double imagesProcessedRatio = ((double) imageIndex) / imageCount;
149 
150                        setProgress(progressWhenStartingToExtract
151                                + Math.round(imagesProcessedRatio * maxExtractProgress));
152                    }
153                }
154                allFilesExtracted = !isInterrupted();
155            }
156        } finally {
157            pdf.close();
158            if (!allFilesExtracted && (targetImageNames != null)) {
159                if (logger.isInfoEnabled()) {
160                    logger.info("removing files extracted so far");
161                }
162                Iterator fileRider = targetImageNames.iterator();
163 
164                // TODO: Also remove folder structure generated by extracted files.
165                while (fileRider.hasNext()) {
166                    String fileNameToDelete = (String) fileRider.next();
167                    File fileToDelete = new File(targetFolder, fileNameToDelete);
168 
169                    fileToDelete.delete();
170                }
171 
172            }
173        }
174        setProgress(getMaxProgress());
175    }
176}

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