1 /* 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.imageio.plugins.jpeg; 27 28 //import javax.imageio.IIOException; 29 import javax.imageio.metadata.IIOInvalidTreeException; 30 import javax.imageio.metadata.IIOMetadataNode; 31 import javax.imageio.stream.ImageOutputStream; 32 33 import java.io.IOException; 34 35 import org.w3c.dom.Node; 36 import org.w3c.dom.NodeList; 37 import org.w3c.dom.NamedNodeMap; 38 39 /** 40 * An SOF (Start Of Frame) marker segment. 41 */ 42 class SOFMarkerSegment extends MarkerSegment { 43 int samplePrecision; 44 int numLines; 45 int samplesPerLine; 46 ComponentSpec [] componentSpecs; // Array size is num components 47 48 SOFMarkerSegment(boolean wantProg, 49 boolean wantExtended, 50 boolean willSubsample, 51 byte[] componentIDs, 52 int numComponents) { 53 super(wantProg ? JPEG.SOF2 54 : wantExtended ? JPEG.SOF1 55 : JPEG.SOF0); 56 samplePrecision = 8; 57 numLines = 0; 58 samplesPerLine = 0; 59 componentSpecs = new ComponentSpec[numComponents]; 60 for(int i = 0; i < numComponents; i++) { 61 int factor = 1; 62 int qsel = 0; 63 if (willSubsample) { 64 factor = 2; 65 if ((i == 1) || (i == 2)) { 66 factor = 1; 67 qsel = 1; 68 } 69 } 70 componentSpecs[i] = new ComponentSpec(componentIDs[i], factor, qsel); 71 } 72 } 73 74 SOFMarkerSegment(JPEGBuffer buffer) throws IOException{ 75 super(buffer); 76 samplePrecision = buffer.buf[buffer.bufPtr++]; 77 numLines = (buffer.buf[buffer.bufPtr++] & 0xff) << 8; 78 numLines |= buffer.buf[buffer.bufPtr++] & 0xff; 79 samplesPerLine = (buffer.buf[buffer.bufPtr++] & 0xff) << 8; 80 samplesPerLine |= buffer.buf[buffer.bufPtr++] & 0xff; 81 int numComponents = buffer.buf[buffer.bufPtr++] & 0xff; 82 componentSpecs = new ComponentSpec [numComponents]; 83 for (int i = 0; i < numComponents; i++) { 84 componentSpecs[i] = new ComponentSpec(buffer); 85 } 86 buffer.bufAvail -= length; 87 } 88 89 SOFMarkerSegment(Node node) throws IIOInvalidTreeException { 90 // All attributes are optional, so setup defaults first 91 super(JPEG.SOF0); 92 samplePrecision = 8; 93 numLines = 0; 94 samplesPerLine = 0; 95 updateFromNativeNode(node, true); 96 } 97 98 protected Object clone() { 99 SOFMarkerSegment newGuy = (SOFMarkerSegment) super.clone(); 100 if (componentSpecs != null) { 101 newGuy.componentSpecs = componentSpecs.clone(); 102 for (int i = 0; i < componentSpecs.length; i++) { 103 newGuy.componentSpecs[i] = 104 (ComponentSpec) componentSpecs[i].clone(); 105 } 106 } 107 return newGuy; 108 } 109 110 IIOMetadataNode getNativeNode() { 111 IIOMetadataNode node = new IIOMetadataNode("sof"); 112 node.setAttribute("process", Integer.toString(tag-JPEG.SOF0)); 113 node.setAttribute("samplePrecision", 114 Integer.toString(samplePrecision)); 115 node.setAttribute("numLines", 116 Integer.toString(numLines)); 117 node.setAttribute("samplesPerLine", 118 Integer.toString(samplesPerLine)); 119 node.setAttribute("numFrameComponents", 120 Integer.toString(componentSpecs.length)); 121 for (int i = 0; i < componentSpecs.length; i++) { 122 node.appendChild(componentSpecs[i].getNativeNode()); 123 } 124 125 return node; 126 } 127 128 void updateFromNativeNode(Node node, boolean fromScratch) 129 throws IIOInvalidTreeException { 130 NamedNodeMap attrs = node.getAttributes(); 131 int value = getAttributeValue(node, attrs, "process", 0, 2, false); 132 tag = (value != -1) ? value+JPEG.SOF0 : tag; 133 // If samplePrecision is present, it must be 8. 134 // This just checks. We don't bother to assign the value. 135 value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false); 136 value = getAttributeValue(node, attrs, "numLines", 0, 65535, false); 137 numLines = (value != -1) ? value : numLines; 138 value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false); 139 samplesPerLine = (value != -1) ? value : samplesPerLine; 140 int numComponents = getAttributeValue(node, attrs, "numFrameComponents", 141 1, 4, false); 142 NodeList children = node.getChildNodes(); 143 if (children.getLength() != numComponents) { 144 throw new IIOInvalidTreeException 145 ("numFrameComponents must match number of children", node); 146 } 147 componentSpecs = new ComponentSpec [numComponents]; 148 for (int i = 0; i < numComponents; i++) { 149 componentSpecs[i] = new ComponentSpec(children.item(i)); 150 } 151 } 152 153 /** 154 * Writes the data for this segment to the stream in 155 * valid JPEG format. 156 */ 157 void write(ImageOutputStream ios) throws IOException { 158 // We don't write SOF segments; the IJG library does. 159 } 160 161 void print () { 162 printTag("SOF"); 163 System.out.print("Sample precision: "); 164 System.out.println(samplePrecision); 165 System.out.print("Number of lines: "); 166 System.out.println(numLines); 167 System.out.print("Samples per line: "); 168 System.out.println(samplesPerLine); 169 System.out.print("Number of components: "); 170 System.out.println(componentSpecs.length); 171 for(int i = 0; i<componentSpecs.length; i++) { 172 componentSpecs[i].print(); 173 } 174 } 175 176 int getIDencodedCSType () { 177 for (int i = 0; i < componentSpecs.length; i++) { 178 if (componentSpecs[i].componentId < 'A') { 179 return JPEG.JCS_UNKNOWN; 180 } 181 } 182 switch(componentSpecs.length) { 183 case 3: 184 if ((componentSpecs[0].componentId == 'R') 185 &&(componentSpecs[1].componentId == 'G') 186 &&(componentSpecs[2].componentId == 'B')) { 187 return JPEG.JCS_RGB; 188 } 189 break; 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 /** 200 * A component spec within an SOF marker segment. 201 */ 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 // Parent already did a loadBuf 217 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) {} // won't happen 238 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 }