1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.metadata.IIOInvalidTreeException ; 11 import javax.imageio.metadata.IIOMetadataNode ; 12 import javax.imageio.stream.ImageOutputStream ; 13 import javax.imageio.plugins.jpeg.JPEGHuffmanTable ; 14 15 import java.io.IOException ; 16 import java.util.List ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 import org.w3c.dom.NamedNodeMap ; 23 24 27 class DHTMarkerSegment extends MarkerSegment { 28 List tables = new ArrayList (); 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 { 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 [] dcTables, 54 JPEGHuffmanTable [] 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 node) throws IIOInvalidTreeException { 65 super(JPEG.DHT); 66 NodeList children = node.getChildNodes(); 67 int size = children.getLength(); 68 if ((size < 1) || (size > 4)) { 69 throw new IIOInvalidTreeException ("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 clone() { 77 DHTMarkerSegment newGuy = (DHTMarkerSegment) super.clone(); 78 newGuy.tables = new ArrayList (tables.size()); 79 Iterator 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 getNativeNode() { 88 IIOMetadataNode node = new IIOMetadataNode ("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 100 void write(ImageOutputStream ios) throws IOException { 101 } 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 node) throws IIOInvalidTreeException { 117 return new Htable(node); 118 } 119 120 void addHtable(JPEGHuffmanTable table, boolean isDC, int id) { 121 tables.add(new Htable(table, isDC, id)); 122 } 123 124 127 class Htable implements Cloneable { 128 int tableClass; int tableID; private static final int NUM_LENGTHS = 16; 131 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 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 node) throws IIOInvalidTreeException { 160 if (node.getNodeName().equals("dhtable")) { 161 NamedNodeMap attrs = node.getAttributes(); 162 int count = attrs.getLength(); 163 if (count != 2) { 164 throw new IIOInvalidTreeException 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 ) { 170 IIOMetadataNode ourNode = (IIOMetadataNode ) node; 171 JPEGHuffmanTable table = 172 (JPEGHuffmanTable ) ourNode.getUserObject(); 173 if (table == null) { 174 throw new IIOInvalidTreeException 175 ("dhtable node must have user object", node); 176 } 177 numCodes = table.getLengths(); 178 values = table.getValues(); 179 } else { 180 throw new IIOInvalidTreeException 181 ("dhtable node must have user object", node); 182 } 183 } else { 184 throw new IIOInvalidTreeException 185 ("Invalid node, expected dqtable", node); 186 } 187 188 } 189 190 protected Object clone() { 191 Htable newGuy = null; 192 try { 193 newGuy = (Htable) super.clone(); 194 } catch (CloneNotSupportedException e) {} 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 getNativeNode() { 205 IIOMetadataNode node = new IIOMetadataNode ("dhtable"); 206 node.setAttribute("class", Integer.toString(tableClass)); 207 node.setAttribute("htableId", Integer.toString(tableID)); 208 209 node.setUserObject(new JPEGHuffmanTable (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 (numCodes, values)).toString(); 222 239 } 240 } 241 242 } 243 | Popular Tags |