KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SOSMarkerSegment.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 SOS (Start Of Scan) marker segment.
23  */

24 class SOSMarkerSegment extends MarkerSegment {
25     int startSpectralSelection;
26     int endSpectralSelection;
27     int approxHigh;
28     int approxLow;
29     ScanComponentSpec [] componentSpecs; // Array size is numScanComponents
30

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 JavaDoc {
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 JavaDoc node) throws IIOInvalidTreeException JavaDoc {
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 JavaDoc 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 JavaDoc getNativeNode() {
89         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("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 JavaDoc node, boolean fromScratch)
108         throws IIOInvalidTreeException JavaDoc {
109         NamedNodeMap JavaDoc 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         // Now the children
124
NodeList JavaDoc children = node.getChildNodes();
125         if (children.getLength() != numComponents) {
126             throw new IIOInvalidTreeException JavaDoc
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     /**
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 SOS segments; the IJG library does.
141
}
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     /**
165      * A scan component spec within an SOS marker segment.
166      */

167     class ScanComponentSpec implements Cloneable JavaDoc {
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             // Parent already loaded the buffer
180
componentSelector = buffer.buf[buffer.bufPtr++];
181             dcHuffTable = buffer.buf[buffer.bufPtr] >> 4;
182             acHuffTable = buffer.buf[buffer.bufPtr++] & 0xf;
183         }
184
185         ScanComponentSpec(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc {
186             NamedNodeMap JavaDoc 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 JavaDoc clone() {
196             try {
197                 return super.clone();
198             } catch (CloneNotSupportedException JavaDoc e) {} // won't happen
199
return null;
200         }
201
202         IIOMetadataNode JavaDoc getNativeNode() {
203             IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("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