KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > jpeg > DHTMarkerSegment


1 /*
2  * @(#)DHTMarkerSegment.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.jpeg;
9
10 import javax.imageio.metadata.IIOInvalidTreeException JavaDoc;
11 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
12 import javax.imageio.stream.ImageOutputStream JavaDoc;
13 import javax.imageio.plugins.jpeg.JPEGHuffmanTable JavaDoc;
14
15 import java.io.IOException JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23
24 /**
25  * A DHT (Define Huffman Table) marker segment.
26  */

27 class DHTMarkerSegment extends MarkerSegment {
28     List JavaDoc tables = new ArrayList JavaDoc();
29
30     DHTMarkerSegment(boolean needFour) {
31         super(JPEG.DHT);
32         tables.add(new Htable(JPEGHuffmanTable.StdDCLuminance, true, 0));
33         if (needFour) {
34             tables.add(new Htable(JPEGHuffmanTable.StdDCChrominance, true, 1));
35         }
36         tables.add(new Htable(JPEGHuffmanTable.StdACLuminance, false, 0));
37         if (needFour) {
38             tables.add(new Htable(JPEGHuffmanTable.StdACChrominance, false, 1));
39         }
40     }
41
42     DHTMarkerSegment(JPEGBuffer buffer) throws IOException JavaDoc {
43         super(buffer);
44         int count = length;
45         while (count > 0) {
46             Htable newGuy = new Htable(buffer);
47             tables.add(newGuy);
48             count -= 1 + 16 + newGuy.values.length;
49         }
50         buffer.bufAvail -= length;
51     }
52
53     DHTMarkerSegment(JPEGHuffmanTable JavaDoc[] dcTables,
54                      JPEGHuffmanTable JavaDoc[] acTables) {
55         super(JPEG.DHT);
56         for (int i = 0; i < dcTables.length; i++) {
57             tables.add(new Htable(dcTables[i], true, i));
58         }
59         for (int i = 0; i < acTables.length; i++) {
60             tables.add(new Htable(acTables[i], false, i));
61         }
62     }
63
64     DHTMarkerSegment(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc {
65         super(JPEG.DHT);
66         NodeList JavaDoc children = node.getChildNodes();
67         int size = children.getLength();
68         if ((size < 1) || (size > 4)) {
69             throw new IIOInvalidTreeException JavaDoc("Invalid DHT node", node);
70         }
71         for (int i = 0; i < size; i++) {
72             tables.add(new Htable(children.item(i)));
73         }
74     }
75
76     protected Object JavaDoc clone() {
77         DHTMarkerSegment newGuy = (DHTMarkerSegment) super.clone();
78         newGuy.tables = new ArrayList JavaDoc(tables.size());
79         Iterator JavaDoc iter = tables.iterator();
80         while (iter.hasNext()) {
81             Htable table = (Htable) iter.next();
82             newGuy.tables.add(table.clone());
83         }
84         return newGuy;
85     }
86
87     IIOMetadataNode JavaDoc getNativeNode() {
88         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("dht");
89         for (int i= 0; i<tables.size(); i++) {
90             Htable table = (Htable) tables.get(i);
91             node.appendChild(table.getNativeNode());
92         }
93         return node;
94     }
95
96     /**
97      * Writes the data for this segment to the stream in
98      * valid JPEG format.
99      */

100     void write(ImageOutputStream JavaDoc ios) throws IOException JavaDoc {
101         // We don't write DHT segments; the IJG library does.
102
}
103
104     void print() {
105         printTag("DHT");
106         System.out.println("Num tables: "
107                            + Integer.toString(tables.size()));
108         for (int i= 0; i<tables.size(); i++) {
109             Htable table = (Htable) tables.get(i);
110             table.print();
111         }
112         System.out.println();
113             
114     }
115
116     Htable getHtableFromNode(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc {
117         return new Htable(node);
118     }
119
120     void addHtable(JPEGHuffmanTable JavaDoc table, boolean isDC, int id) {
121         tables.add(new Htable(table, isDC, id));
122     }
123
124     /**
125      * A Huffman table within a DHT marker segment.
126      */

127     class Htable implements Cloneable JavaDoc {
128         int tableClass; // 0 == DC, 1 == AC
129
int tableID; // 0 - 4
130
private static final int NUM_LENGTHS = 16;
131         // # of codes of each length
132
short [] numCodes = new short[NUM_LENGTHS];
133         short [] values;
134
135         Htable(JPEGBuffer buffer) {
136             tableClass = buffer.buf[buffer.bufPtr] >>> 4;
137             tableID = buffer.buf[buffer.bufPtr++] & 0xf;
138             for (int i = 0; i < NUM_LENGTHS; i++) {
139                 numCodes[i] = (short) (buffer.buf[buffer.bufPtr++] & 0xff);
140             }
141
142             int numValues = 0;
143             for (int i = 0; i < NUM_LENGTHS; i++) {
144                 numValues += numCodes[i];
145             }
146             values = new short[numValues];
147             for (int i = 0; i < numValues; i++) {
148                 values[i] = (short) (buffer.buf[buffer.bufPtr++] & 0xff);
149             }
150         }
151
152         Htable(JPEGHuffmanTable JavaDoc table, boolean isDC, int id) {
153             tableClass = isDC ? 0 : 1;
154             tableID = id;
155             numCodes = table.getLengths();
156             values = table.getValues();
157         }
158
159         Htable(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc {
160             if (node.getNodeName().equals("dhtable")) {
161                 NamedNodeMap JavaDoc attrs = node.getAttributes();
162                 int count = attrs.getLength();
163                 if (count != 2) {
164                     throw new IIOInvalidTreeException JavaDoc
165                         ("dhtable node must have 2 attributes", node);
166                 }
167                 tableClass = getAttributeValue(node, attrs, "class", 0, 1, true);
168                 tableID = getAttributeValue(node, attrs, "htableId", 0, 3, true);
169                 if (node instanceof IIOMetadataNode JavaDoc) {
170                     IIOMetadataNode JavaDoc ourNode = (IIOMetadataNode JavaDoc) node;
171                     JPEGHuffmanTable JavaDoc table =
172                         (JPEGHuffmanTable JavaDoc) ourNode.getUserObject();
173                     if (table == null) {
174                         throw new IIOInvalidTreeException JavaDoc
175                             ("dhtable node must have user object", node);
176                     }
177                     numCodes = table.getLengths();
178                     values = table.getValues();
179                 } else {
180                     throw new IIOInvalidTreeException JavaDoc
181                         ("dhtable node must have user object", node);
182                 }
183             } else {
184                 throw new IIOInvalidTreeException JavaDoc
185                     ("Invalid node, expected dqtable", node);
186             }
187             
188         }
189
190         protected Object JavaDoc clone() {
191             Htable newGuy = null;
192             try {
193                 newGuy = (Htable) super.clone();
194             } catch (CloneNotSupportedException JavaDoc e) {} // won't happen
195
if (numCodes != null) {
196                 newGuy.numCodes = (short []) numCodes.clone();
197             }
198             if (values != null) {
199                 newGuy.values = (short []) values.clone();
200             }
201             return newGuy;
202         }
203
204         IIOMetadataNode JavaDoc getNativeNode() {
205             IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("dhtable");
206             node.setAttribute("class", Integer.toString(tableClass));
207             node.setAttribute("htableId", Integer.toString(tableID));
208
209             node.setUserObject(new JPEGHuffmanTable JavaDoc(numCodes, values));
210
211             return node;
212         }
213
214
215         void print() {
216             System.out.println("Huffman Table");
217             System.out.println("table class: "
218                                + ((tableClass == 0) ? "DC":"AC"));
219             System.out.println("table id: " + Integer.toString(tableID));
220
221             (new JPEGHuffmanTable JavaDoc(numCodes, values)).toString();
222             /*
223               System.out.print("Lengths:");
224               for (int i=0; i<16; i++) {
225               System.out.print(" " + Integer.toString(numCodes[i]));
226               }
227               int count = 0;
228               if (values.length > 16) {
229               System.out.println("\nFirst 16 Values:");
230               count = 16;
231               } else {
232               System.out.println("\nValues:");
233               count = values.length;
234               }
235               for (int i=0; i<count; i++) {
236               System.out.println(Integer.toString(values[i]&0xff));
237               }
238             */

239         }
240     }
241
242 }
243
Popular Tags