KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SOFMarkerSegment.java 1.6 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.IIOException;
11
import javax.imageio.metadata.IIOInvalidTreeException JavaDoc;
12 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
13 import javax.imageio.stream.ImageOutputStream JavaDoc;
14
15 import java.io.IOException JavaDoc;
16
17 import org.w3c.dom.Node JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19 import org.w3c.dom.NamedNodeMap JavaDoc;
20
21 /**
22  * An SOF (Start Of Frame) marker segment.
23  */

24 class SOFMarkerSegment extends MarkerSegment {
25     int samplePrecision;
26     int numLines;
27     int samplesPerLine;
28     ComponentSpec [] componentSpecs; // Array size is num components
29

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 JavaDoc{
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 JavaDoc node) throws IIOInvalidTreeException JavaDoc {
72         // All attributes are optional, so setup defaults first
73
super(JPEG.SOF0);
74         samplePrecision = 8;
75         numLines = 0;
76         samplesPerLine = 0;
77         updateFromNativeNode(node, true);
78     }
79
80     protected Object JavaDoc 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 JavaDoc getNativeNode() {
93         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("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 JavaDoc node, boolean fromScratch)
111         throws IIOInvalidTreeException JavaDoc {
112         NamedNodeMap JavaDoc attrs = node.getAttributes();
113         int value = getAttributeValue(node, attrs, "process", 0, 2, false);
114         tag = (value != -1) ? value+JPEG.SOF0 : tag;
115         // If samplePrecision is present, it must be 8.
116
// This just checks. We don't bother to assign the value.
117
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 JavaDoc children = node.getChildNodes();
125         if (children.getLength() != numComponents) {
126             throw new IIOInvalidTreeException JavaDoc
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     /**
136      * Writes the data for this segment to the stream in
137      * valid JPEG format.
138      */

139     void write(ImageOutputStream JavaDoc ios) throws IOException JavaDoc {
140         // We don't write SOF segments; the IJG library does.
141
}
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     /**
200      * A component spec within an SOF marker segment.
201      */

202     class ComponentSpec implements Cloneable JavaDoc {
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 JavaDoc node) throws IIOInvalidTreeException JavaDoc {
224             NamedNodeMap JavaDoc 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 JavaDoc clone() {
235             try {
236                 return super.clone();
237             } catch (CloneNotSupportedException JavaDoc e) {} // won't happen
238
return null;
239         }
240
241         IIOMetadataNode JavaDoc getNativeNode() {
242             IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("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