1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.InputStream ; 56 import java.io.OutputStream ; 57 58 import com.lowagie.text.Image; 59 60 63 64 public class PdfImage extends PdfStream { 65 66 static final int TRANSFERSIZE = 4096; 67 69 70 protected PdfName name = null; 71 72 74 81 82 public PdfImage(Image image, String name, PdfIndirectReference maskRef) throws BadPdfFormatException { 83 super(); 84 this.name = new PdfName(name); 85 put(PdfName.TYPE, PdfName.XOBJECT); 86 put(PdfName.SUBTYPE, PdfName.IMAGE); 87 put(PdfName.WIDTH, new PdfNumber(image.getWidth())); 88 put(PdfName.HEIGHT, new PdfNumber(image.getHeight())); 89 if (image.getLayer() != null) 90 put(PdfName.OC, image.getLayer().getRef()); 91 if (image.isMask() && (image.getBpc() == 1 || image.getBpc() > 0xff)) 92 put(PdfName.IMAGEMASK, PdfBoolean.PDFTRUE); 93 if (maskRef != null) { 94 if (image.isSmask()) 95 put(PdfName.SMASK, maskRef); 96 else 97 put(PdfName.MASK, maskRef); 98 } 99 if (image.isMask() && image.isInverted()) 100 put(PdfName.DECODE, new PdfLiteral("[1 0]")); 101 if (image.isInterpolation()) 102 put(PdfName.INTERPOLATE, PdfBoolean.PDFTRUE); 103 InputStream is = null; 104 try { 105 106 if (image.isImgRaw()) { 108 int colorspace = image.getColorspace(); 110 int transparency[] = image.getTransparency(); 111 if (transparency != null && !image.isMask() && maskRef == null) { 112 String s = "["; 113 for (int k = 0; k < transparency.length; ++k) 114 s += transparency[k] + " "; 115 s += "]"; 116 put(PdfName.MASK, new PdfLiteral(s)); 117 } 118 bytes = image.getRawData(); 119 put(PdfName.LENGTH, new PdfNumber(bytes.length)); 120 int bpc = image.getBpc(); 121 if (bpc > 0xff) { 122 if (!image.isMask()) 123 put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); 124 put(PdfName.BITSPERCOMPONENT, new PdfNumber(1)); 125 put(PdfName.FILTER, PdfName.CCITTFAXDECODE); 126 int k = bpc - Image.CCITTG3_1D; 127 PdfDictionary decodeparms = new PdfDictionary(); 128 if (k != 0) 129 decodeparms.put(PdfName.K, new PdfNumber(k)); 130 if ((colorspace & Image.CCITT_BLACKIS1) != 0) 131 decodeparms.put(PdfName.BLACKIS1, PdfBoolean.PDFTRUE); 132 if ((colorspace & Image.CCITT_ENCODEDBYTEALIGN) != 0) 133 decodeparms.put(PdfName.ENCODEDBYTEALIGN, PdfBoolean.PDFTRUE); 134 if ((colorspace & Image.CCITT_ENDOFLINE) != 0) 135 decodeparms.put(PdfName.ENDOFLINE, PdfBoolean.PDFTRUE); 136 if ((colorspace & Image.CCITT_ENDOFBLOCK) != 0) 137 decodeparms.put(PdfName.ENDOFBLOCK, PdfBoolean.PDFFALSE); 138 decodeparms.put(PdfName.COLUMNS, new PdfNumber(image.getWidth())); 139 decodeparms.put(PdfName.ROWS, new PdfNumber(image.getHeight())); 140 put(PdfName.DECODEPARMS, decodeparms); 141 } 142 else { 143 switch(colorspace) { 144 case 1: 145 put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); 146 if (image.isInverted()) 147 put(PdfName.DECODE, new PdfLiteral("[1 0]")); 148 break; 149 case 3: 150 put(PdfName.COLORSPACE, PdfName.DEVICERGB); 151 if (image.isInverted()) 152 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0]")); 153 break; 154 case 4: 155 default: 156 put(PdfName.COLORSPACE, PdfName.DEVICECMYK); 157 if (image.isInverted()) 158 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]")); 159 } 160 PdfDictionary additional = image.getAdditional(); 161 if (additional != null) 162 putAll(additional); 163 if (image.isMask() && (image.getBpc() == 1 || image.getBpc() > 8)) 164 remove(PdfName.COLORSPACE); 165 put(PdfName.BITSPERCOMPONENT, new PdfNumber(image.getBpc())); 166 if (image.isDeflated()) 167 put(PdfName.FILTER, PdfName.FLATEDECODE); 168 else { 169 flateCompress(); 170 } 171 } 172 return; 173 } 174 175 String errorID; 177 if (image.getRawData() == null){ 178 is = image.getUrl().openStream(); 179 errorID = image.getUrl().toString(); 180 } 181 else{ 182 is = new java.io.ByteArrayInputStream (image.getRawData()); 183 errorID = "Byte array"; 184 } 185 switch(image.type()) { 186 case Image.JPEG: 187 put(PdfName.FILTER, PdfName.DCTDECODE); 188 switch(image.getColorspace()) { 189 case 1: 190 put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); 191 break; 192 case 3: 193 put(PdfName.COLORSPACE, PdfName.DEVICERGB); 194 break; 195 default: 196 put(PdfName.COLORSPACE, PdfName.DEVICECMYK); 197 if (image.isInverted()) { 198 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]")); 199 } 200 } 201 put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); 202 if (image.getRawData() != null){ 203 bytes = image.getRawData(); 204 put(PdfName.LENGTH, new PdfNumber(bytes.length)); 205 return; 206 } 207 streamBytes = new ByteArrayOutputStream (); 208 transferBytes(is, streamBytes, -1); 209 break; 210 default: 211 throw new BadPdfFormatException(errorID + " is an unknown Image format."); 212 } 213 put(PdfName.LENGTH, new PdfNumber(streamBytes.size())); 214 } 215 catch(IOException ioe) { 216 throw new BadPdfFormatException(ioe.getMessage()); 217 } 218 finally { 219 if (is != null) { 220 try{ 221 is.close(); 222 } 223 catch (Exception ee) { 224 } 226 } 227 } 228 } 229 230 235 236 public PdfName name() { 237 return name; 238 } 239 240 static void transferBytes(InputStream in, OutputStream out, int len) throws IOException { 241 byte buffer[] = new byte[TRANSFERSIZE]; 242 if (len < 0) 243 len = 0x7ffffff; 244 int size; 245 while (len != 0) { 246 size = in.read(buffer, 0, Math.min(len, TRANSFERSIZE)); 247 if (size < 0) 248 return; 249 out.write(buffer, 0, size); 250 len -= size; 251 } 252 } 253 254 protected void importAll(PdfImage dup) { 255 name = dup.name; 256 compressed = dup.compressed; 257 streamBytes = dup.streamBytes; 258 bytes = dup.bytes; 259 hashMap = dup.hashMap; 260 } 261 } 262 | Popular Tags |