KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AdobeMarkerSegment.java 1.5 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 JavaDoc;
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.NamedNodeMap JavaDoc;
19
20 /**
21  * An Adobe APP14 (Application-Specific) marker segment.
22  */

23 class AdobeMarkerSegment extends MarkerSegment {
24     int version;
25     int flags0;
26     int flags1;
27     int transform;
28     private static final int ID_SIZE = 5;
29
30     AdobeMarkerSegment(int transform) {
31         super(JPEG.APP14);
32         version = 101;
33         flags0 = 0;
34         flags1 = 0;
35         this.transform = transform;
36     }
37
38     AdobeMarkerSegment(JPEGBuffer buffer) throws IOException JavaDoc {
39         super(buffer);
40         buffer.bufPtr += ID_SIZE; // Skip the id
41
version = (buffer.buf[buffer.bufPtr++] & 0xff) << 8;
42         version |= buffer.buf[buffer.bufPtr++] & 0xff;
43         flags0 = (buffer.buf[buffer.bufPtr++] & 0xff) << 8;
44         flags0 |= buffer.buf[buffer.bufPtr++] & 0xff;
45         flags1 = (buffer.buf[buffer.bufPtr++] & 0xff) << 8;
46         flags1 |= buffer.buf[buffer.bufPtr++] & 0xff;
47         transform = buffer.buf[buffer.bufPtr++] & 0xff;
48         buffer.bufAvail -= length;
49     }
50
51     AdobeMarkerSegment(Node JavaDoc node) throws IIOInvalidTreeException JavaDoc {
52         this(0); // default transform will be changed
53
updateFromNativeNode(node, true);
54     }
55
56     IIOMetadataNode JavaDoc getNativeNode() {
57         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("app14Adobe");
58         node.setAttribute("version", Integer.toString(version));
59         node.setAttribute("flags0", Integer.toString(flags0));
60         node.setAttribute("flags1", Integer.toString(flags1));
61         node.setAttribute("transform", Integer.toString(transform));
62
63         return node;
64     }
65
66     void updateFromNativeNode(Node JavaDoc node, boolean fromScratch)
67         throws IIOInvalidTreeException JavaDoc {
68         // Only the transform is required
69
NamedNodeMap JavaDoc attrs = node.getAttributes();
70         transform = getAttributeValue(node, attrs, "transform", 0, 2, true);
71         int count = attrs.getLength();
72         if (count > 4) {
73             throw new IIOInvalidTreeException JavaDoc
74                 ("Adobe APP14 node cannot have > 4 attributes", node);
75         }
76         if (count > 1) {
77             int value = getAttributeValue(node, attrs, "version",
78                                           100, 255, false);
79             version = (value != -1) ? value : version;
80             value = getAttributeValue(node, attrs, "flags0", 0, 65535, false);
81             flags0 = (value != -1) ? value : flags0;
82             value = getAttributeValue(node, attrs, "flags1", 0, 65535, false);
83             flags1 = (value != -1) ? value : flags1;
84         }
85     }
86
87     /**
88      * Writes the data for this segment to the stream in
89      * valid JPEG format.
90      */

91     void write(ImageOutputStream JavaDoc ios) throws IOException JavaDoc {
92         length = 14;
93         writeTag(ios);
94         byte [] id = {0x41, 0x64, 0x6F, 0x62, 0x65};
95         ios.write(id);
96         write2bytes(ios, version);
97         write2bytes(ios, flags0);
98         write2bytes(ios, flags1);
99         ios.write(transform);
100     }
101
102     static void writeAdobeSegment(ImageOutputStream JavaDoc ios, int transform)
103         throws IOException JavaDoc {
104         (new AdobeMarkerSegment(transform)).write(ios);
105     }
106
107     void print () {
108         printTag("Adobe APP14");
109         System.out.print("Version: ");
110         System.out.println(version);
111         System.out.print("Flags0: 0x");
112         System.out.println(Integer.toHexString(flags0));
113         System.out.print("Flags1: 0x");
114         System.out.println(Integer.toHexString(flags1));
115         System.out.print("Transform: ");
116         System.out.println(transform);
117     }
118 }
119
120
Popular Tags