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 SOFMarkerSegment extends MarkerSegment { 25 int samplePrecision; 26 int numLines; 27 int samplesPerLine; 28 ComponentSpec [] componentSpecs; 30 SOFMarkerSegment(boolean wantProg, 31 boolean wantExtended, 32 boolean willSubsample, 33 byte[] componentIDs, 34 int numComponents) { 35 super(wantProg ? JPEG.SOF2 36 : wantExtended ? JPEG.SOF1 37 : JPEG.SOF0); 38 samplePrecision = 8; 39 numLines = 0; 40 samplesPerLine = 0; 41 componentSpecs = new ComponentSpec[numComponents]; 42 for(int i = 0; i < numComponents; i++) { 43 int factor = 1; 44 int qsel = 0; 45 if (willSubsample) { 46 factor = 2; 47 if ((i == 1) || (i == 2)) { 48 factor = 1; 49 qsel = 1; 50 } 51 } 52 componentSpecs[i] = new ComponentSpec(componentIDs[i], factor, qsel); 53 } 54 } 55 56 SOFMarkerSegment(JPEGBuffer buffer) throws IOException { 57 super(buffer); 58 samplePrecision = buffer.buf[buffer.bufPtr++]; 59 numLines = (buffer.buf[buffer.bufPtr++] & 0xff) << 8; 60 numLines |= buffer.buf[buffer.bufPtr++] & 0xff; 61 samplesPerLine = (buffer.buf[buffer.bufPtr++] & 0xff) << 8; 62 samplesPerLine |= buffer.buf[buffer.bufPtr++] & 0xff; 63 int numComponents = buffer.buf[buffer.bufPtr++]; 64 componentSpecs = new ComponentSpec [numComponents]; 65 for (int i = 0; i < numComponents; i++) { 66 componentSpecs[i] = new ComponentSpec(buffer); 67 } 68 buffer.bufAvail -= length; 69 } 70 71 SOFMarkerSegment(Node node) throws IIOInvalidTreeException { 72 super(JPEG.SOF0); 74 samplePrecision = 8; 75 numLines = 0; 76 samplesPerLine = 0; 77 updateFromNativeNode(node, true); 78 } 79 80 protected Object clone() { 81 SOFMarkerSegment newGuy = (SOFMarkerSegment) super.clone(); 82 if (componentSpecs != null) { 83 newGuy.componentSpecs = (ComponentSpec []) componentSpecs.clone(); 84 for (int i = 0; i < componentSpecs.length; i++) { 85 newGuy.componentSpecs[i] = 86 (ComponentSpec) componentSpecs[i].clone(); 87 } 88 } 89 return newGuy; 90 } 91 92 IIOMetadataNode getNativeNode() { 93 IIOMetadataNode node = new IIOMetadataNode ("sof"); 94 node.setAttribute("process", Integer.toString(tag-JPEG.SOF0)); 95 node.setAttribute("samplePrecision", 96 Integer.toString(samplePrecision)); 97 node.setAttribute("numLines", 98 Integer.toString(numLines)); 99 node.setAttribute("samplesPerLine", 100 Integer.toString(samplesPerLine)); 101 node.setAttribute("numFrameComponents", 102 Integer.toString(componentSpecs.length)); 103 for (int i = 0; i < componentSpecs.length; i++) { 104 node.appendChild(componentSpecs[i].getNativeNode()); 105 } 106 107 return node; 108 } 109 110 void updateFromNativeNode(Node node, boolean fromScratch) 111 throws IIOInvalidTreeException { 112 NamedNodeMap attrs = node.getAttributes(); 113 int value = getAttributeValue(node, attrs, "process", 0, 2, false); 114 tag = (value != -1) ? value+JPEG.SOF0 : tag; 115 value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false); 118 value = getAttributeValue(node, attrs, "numLines", 0, 65535, false); 119 numLines = (value != -1) ? value : numLines; 120 value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false); 121 samplesPerLine = (value != -1) ? value : samplesPerLine; 122 int numComponents = getAttributeValue(node, attrs, "numFrameComponents", 123 1, 4, false); 124 NodeList children = node.getChildNodes(); 125 if (children.getLength() != numComponents) { 126 throw new IIOInvalidTreeException 127 ("numFrameComponents must match number of children", node); 128 } 129 componentSpecs = new ComponentSpec [numComponents]; 130 for (int i = 0; i < numComponents; i++) { 131 componentSpecs[i] = new ComponentSpec(children.item(i)); 132 } 133 } 134 135 139 void write(ImageOutputStream ios) throws IOException { 140 } 142 143 void print () { 144 printTag("SOF"); 145 System.out.print("Sample precision: "); 146 System.out.println(samplePrecision); 147 System.out.print("Number of lines: "); 148 System.out.println(numLines); 149 System.out.print("Samples per line: "); 150 System.out.println(samplesPerLine); 151 System.out.print("Number of components: "); 152 System.out.println(componentSpecs.length); 153 for(int i = 0; i<componentSpecs.length; i++) { 154 componentSpecs[i].print(); 155 } 156 } 157 158 int getIDencodedCSType () { 159 for (int i = 0; i < componentSpecs.length; i++) { 160 if (componentSpecs[i].componentId < 'A') { 161 return JPEG.JCS_UNKNOWN; 162 } 163 } 164 switch(componentSpecs.length) { 165 case 3: 166 if ((componentSpecs[0].componentId == 'R') 167 &&(componentSpecs[0].componentId == 'G') 168 &&(componentSpecs[0].componentId == 'B')) { 169 return JPEG.JCS_RGB; 170 } 171 if ((componentSpecs[0].componentId == 'Y') 172 &&(componentSpecs[0].componentId == 'C') 173 &&(componentSpecs[0].componentId == 'c')) { 174 return JPEG.JCS_YCC; 175 } 176 break; 177 case 4: 178 if ((componentSpecs[0].componentId == 'R') 179 &&(componentSpecs[0].componentId == 'G') 180 &&(componentSpecs[0].componentId == 'B') 181 &&(componentSpecs[0].componentId == 'A')) { 182 return JPEG.JCS_RGBA; 183 } 184 if ((componentSpecs[0].componentId == 'Y') 185 &&(componentSpecs[0].componentId == 'C') 186 &&(componentSpecs[0].componentId == 'c') 187 &&(componentSpecs[0].componentId == 'A')) { 188 return JPEG.JCS_YCCA; 189 } 190 } 191 192 return JPEG.JCS_UNKNOWN; 193 } 194 195 ComponentSpec getComponentSpec(byte id, int factor, int qSelector) { 196 return new ComponentSpec(id, factor, qSelector); 197 } 198 199 202 class ComponentSpec implements Cloneable { 203 int componentId; 204 int HsamplingFactor; 205 int VsamplingFactor; 206 int QtableSelector; 207 208 ComponentSpec(byte id, int factor, int qSelector) { 209 componentId = id; 210 HsamplingFactor = factor; 211 VsamplingFactor = factor; 212 QtableSelector = qSelector; 213 } 214 215 ComponentSpec(JPEGBuffer buffer) { 216 componentId = buffer.buf[buffer.bufPtr++]; 218 HsamplingFactor = buffer.buf[buffer.bufPtr] >>> 4; 219 VsamplingFactor = buffer.buf[buffer.bufPtr++] & 0xf; 220 QtableSelector = buffer.buf[buffer.bufPtr++]; 221 } 222 223 ComponentSpec(Node node) throws IIOInvalidTreeException { 224 NamedNodeMap attrs = node.getAttributes(); 225 componentId = getAttributeValue(node, attrs, "componentId", 0, 255, true); 226 HsamplingFactor = getAttributeValue(node, attrs, "HsamplingFactor", 227 1, 255, true); 228 VsamplingFactor = getAttributeValue(node, attrs, "VsamplingFactor", 229 1, 255, true); 230 QtableSelector = getAttributeValue(node, attrs, "QtableSelector", 231 0, 3, true); 232 } 233 234 protected Object clone() { 235 try { 236 return super.clone(); 237 } catch (CloneNotSupportedException e) {} return null; 239 } 240 241 IIOMetadataNode getNativeNode() { 242 IIOMetadataNode node = new IIOMetadataNode ("componentSpec"); 243 node.setAttribute("componentId", 244 Integer.toString(componentId)); 245 node.setAttribute("HsamplingFactor", 246 Integer.toString(HsamplingFactor)); 247 node.setAttribute("VsamplingFactor", 248 Integer.toString(VsamplingFactor)); 249 node.setAttribute("QtableSelector", 250 Integer.toString(QtableSelector)); 251 return node; 252 } 253 254 void print () { 255 System.out.print("Component ID: "); 256 System.out.println(componentId); 257 System.out.print("H sampling factor: "); 258 System.out.println(HsamplingFactor); 259 System.out.print("V sampling factor: "); 260 System.out.println(VsamplingFactor); 261 System.out.print("Q table selector: "); 262 System.out.println(QtableSelector); 263 } 264 } 265 266 } 267 | Popular Tags |