KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > documents > contents > entities > JpegImage


1 /*
2   Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

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 JavaDoc;
44
45 /**
46   JPEG image object [ISO 10918-1;JFIF:1.02].
47 */

48 public class JpegImage
49   extends Image
50 {
51   // <class>
52
// <dynamic>
53
// <constructors>
54
JpegImage(
55     IInputStream stream
56     )
57   {
58     super(stream);
59
60     load();
61   }
62   // </constructors>
63

64   // <interface>
65
// <public>
66
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     // Get the content buffer!
101
IBuffer buffer = context.getBuffer();
102
103     // Insert the image inside the content buffer!
104
buffer.append("BI\r");
105     // Header.
106
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     // Body.
114
buffer.append("ID\r");
115     buffer.append(getStream());
116     buffer.append("\rEI\r");
117   }
118   // </public>
119

120   // <private>
121
private void load(
122     )
123   {
124     /*
125       NOTE: Big-endian data expected.
126     */

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         // Frame header?
143
if(markerBytes[0] == (byte)0xFF
144           && markerBytes[1] == (byte)0xC0)
145         {
146           stream.skip(2);
147           // Get the image bits per color component (sample precision)!
148
setBitsPerComponent(stream.readUnsignedByte());
149           // Get the image size!
150
setHeight((stream.readUnsignedByte() << 8) + stream.readUnsignedByte());
151           setWidth((stream.readUnsignedByte() << 8) + stream.readUnsignedByte());
152
153           break;
154         }
155       }
156     }
157     catch(Exception JavaDoc e)
158     {throw new RuntimeException JavaDoc(e);}
159   }
160   // </private>
161
// </interface>
162
// </dynamic>
163
// </class>
164
}
Popular Tags