| 1 |
// Copyright 2001-2005, FreeHEP |
| 2 |
package org.freehep.graphicsio.pdf; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.image.RenderedImage; |
| 6 |
import java.io.IOException; |
| 7 |
import java.util.HashMap; |
| 8 |
import java.util.Iterator; |
| 9 |
import java.util.LinkedList; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
|
| 13 |
/** |
| 14 |
* Delay <tt>Image</tt> objects for writing XObjects to the pdf file when the |
| 15 |
* pageStream is complete. Caches identical images to only write them once. |
| 16 |
* |
| 17 |
* @author Simon Fischer |
| 18 |
* @author Mark Donszelmann |
| 19 |
* @version $Id: freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFImageDelayQueue.java 2fa79ac3a135 2007/01/09 18:18:57 duns $ |
| 20 |
*/ |
| 21 |
public class PDFImageDelayQueue { |
| 22 |
|
| 23 |
private int currentNumber = 0; |
| 24 |
|
| 25 |
private class Entry { |
| 26 |
private RenderedImage image; |
| 27 |
|
| 28 |
private String name, maskName; |
| 29 |
|
| 30 |
private Color bkg; |
| 31 |
|
| 32 |
private String writeAs; |
| 33 |
|
| 34 |
private boolean written; |
| 35 |
|
| 36 |
private Entry(RenderedImage image, Color bkg, String writeAs) { |
| 37 |
this.image = image; |
| 38 |
this.bkg = bkg; |
| 39 |
this.writeAs = writeAs; |
| 40 |
this.name = "Img" + (currentNumber++); |
| 41 |
if (image.getColorModel().hasAlpha() && (bkg == null)) { |
| 42 |
maskName = name + "Mask"; |
| 43 |
} else { |
| 44 |
maskName = null; |
| 45 |
} |
| 46 |
this.written = false; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private Map/* <RenderedImage,Entry> */<RenderedImage, Entry>imageMap; |
| 51 |
|
| 52 |
private List/* <entry> */<Entry>imageList; |
| 53 |
|
| 54 |
private PDFWriter pdf; |
| 55 |
|
| 56 |
public PDFImageDelayQueue(PDFWriter pdf) { |
| 57 |
this.pdf = pdf; |
| 58 |
this.imageMap = new HashMap<RenderedImage, Entry>(); |
| 59 |
this.imageList = new LinkedList<Entry>(); |
| 60 |
} |
| 61 |
|
| 62 |
public PDFName delayImage(RenderedImage image, Color bkg, String writeAs) { |
| 63 |
Entry entry = imageMap.get(image); |
| 64 |
if (entry == null) { |
| 65 |
entry = new Entry(image, bkg, writeAs); |
| 66 |
imageMap.put(image, entry); |
| 67 |
imageList.add(entry); |
| 68 |
} |
| 69 |
|
| 70 |
return pdf.name(entry.name); |
| 71 |
} |
| 72 |
|
| 73 |
/** Creates a stream for every delayed image that is not written yet. */ |
| 74 |
public void processAll() throws IOException { |
| 75 |
for (Iterator<Entry> i = imageList.iterator(); i.hasNext();) { |
| 76 |
Entry entry = i.next(); |
| 77 |
|
| 78 |
if (!entry.written) { |
| 79 |
entry.written = true; |
| 80 |
|
| 81 |
PDFStream img = pdf.openStream(entry.name); |
| 82 |
img.entry("Subtype", pdf.name("Image")); |
| 83 |
if (entry.maskName != null) |
| 84 |
img.entry("SMask", pdf.ref(entry.maskName)); |
| 85 |
img.image(entry.image, entry.bkg, entry.writeAs); |
| 86 |
pdf.close(img); |
| 87 |
|
| 88 |
if (entry.maskName != null) { |
| 89 |
PDFStream mask = pdf.openStream(entry.maskName); |
| 90 |
mask.entry("Subtype", pdf.name("Image")); |
| 91 |
mask.imageMask(entry.image, entry.writeAs); |
| 92 |
pdf.close(mask); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Adds all names to the dictionary which should be the value of the |
| 100 |
* resources dicionrary's /XObject entry. |
| 101 |
*/ |
| 102 |
public int addXObjects() throws IOException { |
| 103 |
if (imageList.size() > 0) { |
| 104 |
PDFDictionary xobj = pdf.openDictionary("XObjects"); |
| 105 |
for (Iterator<Entry> i = imageList.iterator(); i.hasNext();) { |
| 106 |
Entry entry = i.next(); |
| 107 |
xobj.entry(entry.name, pdf.ref(entry.name)); |
| 108 |
if (entry.maskName != null) |
| 109 |
xobj.entry(entry.maskName, pdf.ref(entry.maskName)); |
| 110 |
} |
| 111 |
pdf.close(xobj); |
| 112 |
} |
| 113 |
return imageList.size(); |
| 114 |
} |
| 115 |
} |