1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.metadata.IIOInvalidTreeException ; 12 import javax.imageio.metadata.IIOMetadataNode ; 13 import javax.imageio.stream.ImageOutputStream ; 14 15 import java.io.IOException ; 16 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 import org.w3c.dom.NamedNodeMap ; 20 21 24 class SOSMarkerSegment extends MarkerSegment { 25 int startSpectralSelection; 26 int endSpectralSelection; 27 int approxHigh; 28 int approxLow; 29 ScanComponentSpec [] componentSpecs; 31 SOSMarkerSegment(boolean willSubsample, 32 byte[] componentIDs, 33 int numComponents) { 34 super(JPEG.SOS); 35 startSpectralSelection = 0; 36 endSpectralSelection = 63; 37 approxHigh = 0; 38 approxLow = 0; 39 componentSpecs = new ScanComponentSpec[numComponents]; 40 for (int i = 0; i < numComponents; i++) { 41 int tableSel = 0; 42 if (willSubsample) { 43 if ((i == 1) || (i == 2)) { 44 tableSel = 1; 45 } 46 } 47 componentSpecs[i] = new ScanComponentSpec(componentIDs[i], 48 tableSel); 49 } 50 } 51 52 SOSMarkerSegment(JPEGBuffer buffer) throws IOException { 53 super(buffer); 54 int numComponents = buffer.buf[buffer.bufPtr++]; 55 componentSpecs = new ScanComponentSpec[numComponents]; 56 for (int i = 0; i < numComponents; i++) { 57 componentSpecs[i] = new ScanComponentSpec(buffer); 58 } 59 startSpectralSelection = buffer.buf[buffer.bufPtr++]; 60 endSpectralSelection = buffer.buf[buffer.bufPtr++]; 61 approxHigh = buffer.buf[buffer.bufPtr] >> 4; 62 approxLow = buffer.buf[buffer.bufPtr++] &0xf; 63 buffer.bufAvail -= length; 64 } 65 66 SOSMarkerSegment(Node node) throws IIOInvalidTreeException { 67 super(JPEG.SOS); 68 startSpectralSelection = 0; 69 endSpectralSelection = 63; 70 approxHigh = 0; 71 approxLow = 0; 72 updateFromNativeNode(node, true); 73 } 74 75 protected Object clone () { 76 SOSMarkerSegment newGuy = (SOSMarkerSegment) super.clone(); 77 if (componentSpecs != null) { 78 newGuy.componentSpecs = 79 (ScanComponentSpec []) componentSpecs.clone(); 80 for (int i = 0; i < componentSpecs.length; i++) { 81 newGuy.componentSpecs[i] = 82 (ScanComponentSpec) componentSpecs[i].clone(); 83 } 84 } 85 return newGuy; 86 } 87 88 IIOMetadataNode getNativeNode() { 89 IIOMetadataNode node = new IIOMetadataNode ("sos"); 90 node.setAttribute("numScanComponents", 91 Integer.toString(componentSpecs.length)); 92 node.setAttribute("startSpectralSelection", 93 Integer.toString(startSpectralSelection)); 94 node.setAttribute("endSpectralSelection", 95 Integer.toString(endSpectralSelection)); 96 node.setAttribute("approxHigh", 97 Integer.toString(approxHigh)); 98 node.setAttribute("approxLow", 99 Integer.toString(approxLow)); 100 for (int i = 0; i < componentSpecs.length; i++) { 101 node.appendChild(componentSpecs[i].getNativeNode()); 102 } 103 104 return node; 105 } 106 107 void updateFromNativeNode(Node node, boolean fromScratch) 108 throws IIOInvalidTreeException { 109 NamedNodeMap attrs = node.getAttributes(); 110 int numComponents = getAttributeValue(node, attrs, "numScanComponents", 111 1, 4, true); 112 int value = getAttributeValue(node, attrs, "startSpectralSelection", 113 0, 63, false); 114 startSpectralSelection = (value != -1) ? value : startSpectralSelection; 115 value = getAttributeValue(node, attrs, "endSpectralSelection", 116 0, 63, false); 117 endSpectralSelection = (value != -1) ? value : endSpectralSelection; 118 value = getAttributeValue(node, attrs, "approxHigh", 0, 15, false); 119 approxHigh = (value != -1) ? value : approxHigh; 120 value = getAttributeValue(node, attrs, "approxLow", 0, 15, false); 121 approxLow = (value != -1) ? value : approxLow; 122 123 NodeList children = node.getChildNodes(); 125 if (children.getLength() != numComponents) { 126 throw new IIOInvalidTreeException 127 ("numScanComponents must match the number of children", node); 128 } 129 componentSpecs = new ScanComponentSpec[numComponents]; 130 for (int i = 0; i < numComponents; i++) { 131 componentSpecs[i] = new ScanComponentSpec(children.item(i)); 132 } 133 } 134 135 139 void write(ImageOutputStream ios) throws IOException { 140 } 142 143 void print () { 144 printTag("SOS"); 145 System.out.print("Start spectral selection: "); 146 System.out.println(startSpectralSelection); 147 System.out.print("End spectral selection: "); 148 System.out.println(endSpectralSelection); 149 System.out.print("Approx high: "); 150 System.out.println(approxHigh); 151 System.out.print("Approx low: "); 152 System.out.println(approxLow); 153 System.out.print("Num scan components: "); 154 System.out.println(componentSpecs.length); 155 for (int i = 0; i< componentSpecs.length; i++) { 156 componentSpecs[i].print(); 157 } 158 } 159 160 ScanComponentSpec getScanComponentSpec(byte componentSel, int tableSel) { 161 return new ScanComponentSpec(componentSel, tableSel); 162 } 163 164 167 class ScanComponentSpec implements Cloneable { 168 int componentSelector; 169 int dcHuffTable; 170 int acHuffTable; 171 172 ScanComponentSpec(byte componentSel, int tableSel) { 173 componentSelector = componentSel; 174 dcHuffTable = tableSel; 175 acHuffTable = tableSel; 176 } 177 178 ScanComponentSpec(JPEGBuffer buffer) { 179 componentSelector = buffer.buf[buffer.bufPtr++]; 181 dcHuffTable = buffer.buf[buffer.bufPtr] >> 4; 182 acHuffTable = buffer.buf[buffer.bufPtr++] & 0xf; 183 } 184 185 ScanComponentSpec(Node node) throws IIOInvalidTreeException { 186 NamedNodeMap attrs = node.getAttributes(); 187 componentSelector = getAttributeValue(node, attrs, "componentSelector", 188 0, 255, true); 189 dcHuffTable = getAttributeValue(node, attrs, "dcHuffTable", 190 0, 3, true); 191 acHuffTable = getAttributeValue(node, attrs, "acHuffTable", 192 0, 3, true); 193 } 194 195 protected Object clone() { 196 try { 197 return super.clone(); 198 } catch (CloneNotSupportedException e) {} return null; 200 } 201 202 IIOMetadataNode getNativeNode() { 203 IIOMetadataNode node = new IIOMetadataNode ("scanComponentSpec"); 204 node.setAttribute("componentSelector", 205 Integer.toString(componentSelector)); 206 node.setAttribute("dcHuffTable", 207 Integer.toString(dcHuffTable)); 208 node.setAttribute("acHuffTable", 209 Integer.toString(acHuffTable)); 210 return node; 211 } 212 213 void print () { 214 System.out.print("Component Selector: "); 215 System.out.println(componentSelector); 216 System.out.print("DC huffman table: "); 217 System.out.println(dcHuffTable); 218 System.out.print("AC huffman table: "); 219 System.out.println(acHuffTable); 220 } 221 } 222 223 } 224 | Popular Tags |