1 26 27 package it.stefanochizzolini.clown.documents.contents.entities; 28 29 import it.stefanochizzolini.clown.bytes.Buffer; 30 import it.stefanochizzolini.clown.bytes.IBuffer; 31 import it.stefanochizzolini.clown.bytes.IInputStream; 32 import it.stefanochizzolini.clown.documents.contents.composition.ContentBuilder; 33 import it.stefanochizzolini.clown.documents.contents.xObjects.ImageXObject; 34 import it.stefanochizzolini.clown.documents.contents.xObjects.XObject; 35 import it.stefanochizzolini.clown.documents.Document; 36 import it.stefanochizzolini.clown.objects.PdfDictionary; 37 import it.stefanochizzolini.clown.objects.PdfDirectObject; 38 import it.stefanochizzolini.clown.objects.PdfInteger; 39 import it.stefanochizzolini.clown.objects.PdfName; 40 import it.stefanochizzolini.clown.objects.PdfReference; 41 import it.stefanochizzolini.clown.objects.PdfStream; 42 43 import java.io.EOFException ; 44 45 48 public class JpegImage 49 extends Image 50 { 51 JpegImage( 55 IInputStream stream 56 ) 57 { 58 super(stream); 59 60 load(); 61 } 62 64 public XObject createXObject( 67 Document context 68 ) 69 { 70 return new ImageXObject( 71 context, 72 new PdfStream( 73 new PdfDictionary( 74 new PdfName[] 75 { 76 PdfName.Width, 77 PdfName.Height, 78 PdfName.BitsPerComponent, 79 PdfName.ColorSpace, 80 PdfName.Filter 81 }, 82 new PdfDirectObject[] 83 { 84 new PdfInteger(getWidth()), 85 new PdfInteger(getHeight()), 86 new PdfInteger(getBitsPerComponent()), 87 PdfName.DeviceRGB, 88 PdfName.DCTDecode 89 } 90 ), 91 new Buffer(getStream().toByteArray()) 92 ) 93 ); 94 } 95 96 public void showInline( 97 ContentBuilder context 98 ) 99 { 100 IBuffer buffer = context.getBuffer(); 102 103 buffer.append("BI\r"); 105 buffer.append( 107 PdfName.toPdf("W") + " " + getWidth() + "\r" 108 + PdfName.toPdf("H") + " " + getHeight() + "\r" 109 + PdfName.toPdf("CS") + " " + PdfName.toPdf("RGB") + "\r" 110 + PdfName.toPdf("BPC") + " " + getBitsPerComponent() + "\r" 111 + PdfName.toPdf("F") + " " + PdfName.toPdf("DCT") + "\r" 112 ); 113 buffer.append("ID\r"); 115 buffer.append(getStream()); 116 buffer.append("\rEI\r"); 117 } 118 120 private void load( 122 ) 123 { 124 127 IInputStream stream = getStream(); 128 129 try 130 { 131 int index = 4; 132 stream.seek(index); 133 byte[] markerBytes = new byte[2]; 134 while(true) 135 { 136 index += stream.readUnsignedShort(); 137 stream.seek(index); 138 139 stream.read(markerBytes); 140 index += 2; 141 142 if(markerBytes[0] == (byte)0xFF 144 && markerBytes[1] == (byte)0xC0) 145 { 146 stream.skip(2); 147 setBitsPerComponent(stream.readUnsignedByte()); 149 setHeight((stream.readUnsignedByte() << 8) + stream.readUnsignedByte()); 151 setWidth((stream.readUnsignedByte() << 8) + stream.readUnsignedByte()); 152 153 break; 154 } 155 } 156 } 157 catch(Exception e) 158 {throw new RuntimeException (e);} 159 } 160 } | Popular Tags |