1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.metadata.IIOInvalidTreeException ; 11 import javax.imageio.metadata.IIOMetadataNode ; 12 import javax.imageio.stream.ImageOutputStream ; 13 14 import java.io.IOException ; 15 16 import org.w3c.dom.Node ; 17 import org.w3c.dom.NamedNodeMap ; 18 19 26 class MarkerSegment implements Cloneable { 27 protected static final int LENGTH_SIZE = 2; int tag; int length; 31 byte [] data = null; boolean unknown = false; 34 38 MarkerSegment(JPEGBuffer buffer) throws IOException { 39 40 buffer.loadBuf(3); tag = buffer.buf[buffer.bufPtr++] & 0xff; 42 length = (buffer.buf[buffer.bufPtr++] & 0xff) << 8; 43 length |= buffer.buf[buffer.bufPtr++] & 0xff; 44 length -= 2; buffer.bufAvail -= 3; 46 buffer.loadBuf(length); 49 } 50 51 55 MarkerSegment(int tag) { 56 this.tag = tag; 57 length = 0; 58 } 59 60 63 MarkerSegment(Node node) throws IIOInvalidTreeException { 64 tag = getAttributeValue(node, 67 null, 68 "MarkerTag", 69 0, 255, 70 true); 71 length = 0; 72 if (node instanceof IIOMetadataNode ) { 74 IIOMetadataNode iioNode = (IIOMetadataNode ) node; 75 try { 76 data = (byte []) iioNode.getUserObject(); 77 } catch (Exception e) { 78 IIOInvalidTreeException newGuy = 79 new IIOInvalidTreeException 80 ("Can't get User Object", node); 81 newGuy.initCause(e); 82 throw newGuy; 83 } 84 } else { 85 throw new IIOInvalidTreeException 86 ("Node must have User Object", node); 87 } 88 } 89 90 93 protected Object clone() { 94 MarkerSegment newGuy = null; 95 try { 96 newGuy = (MarkerSegment) super.clone(); 97 } catch (CloneNotSupportedException e) {} if (this.data != null) { 99 newGuy.data = (byte[]) data.clone(); 100 } 101 return newGuy; 102 } 103 104 108 void loadData(JPEGBuffer buffer) throws IOException { 109 data = new byte[length]; 110 buffer.readData(data); 111 } 112 113 IIOMetadataNode getNativeNode() { 114 IIOMetadataNode node = new IIOMetadataNode ("unknown"); 115 node.setAttribute("MarkerTag", Integer.toString(tag)); 116 node.setUserObject(data); 117 118 return node; 119 } 120 121 static int getAttributeValue(Node node, 122 NamedNodeMap attrs, 123 String name, 124 int min, 125 int max, 126 boolean required) 127 throws IIOInvalidTreeException { 128 if (attrs == null) { 129 attrs = node.getAttributes(); 130 } 131 String valueString = attrs.getNamedItem(name).getNodeValue(); 132 int value = -1; 133 if (valueString == null) { 134 if (required) { 135 throw new IIOInvalidTreeException 136 (name + " attribute not found", node); 137 } 138 } else { 139 value = Integer.parseInt(valueString); 140 if ((value < min) || (value > max)) { 141 throw new IIOInvalidTreeException 142 (name + " attribute out of range", node); 143 } 144 } 145 return value; 146 } 147 148 153 void writeTag(ImageOutputStream ios) throws IOException { 154 ios.write(0xff); 155 ios.write(tag); 156 write2bytes(ios, length); 157 } 158 159 163 void write(ImageOutputStream ios) throws IOException { 164 length = 2 + ((data != null) ? data.length : 0); 165 writeTag(ios); 166 if (data != null) { 167 ios.write(data); 168 } 169 } 170 171 static void write2bytes(ImageOutputStream ios, 172 int value) throws IOException { 173 ios.write((value >> 8) & 0xff); 174 ios.write(value & 0xff); 175 176 } 177 178 void printTag(String prefix) { 179 System.out.println(prefix + " marker segment - marker = 0x" 180 + Integer.toHexString(tag)); 181 System.out.println("length: " + length); 182 } 183 184 void print() { 185 printTag("Unknown"); 186 if (length > 10) { 187 System.out.print("First 5 bytes:"); 188 for (int i=0;i<5;i++) { 189 System.out.print(" Ox" 190 + Integer.toHexString((int)data[i])); 191 } 192 System.out.print("\nLast 5 bytes:"); 193 for (int i=data.length-5;i<data.length;i++) { 194 System.out.print(" Ox" 195 + Integer.toHexString((int)data[i])); 196 } 197 } else { 198 System.out.print("Data:"); 199 for (int i=0;i<data.length;i++) { 200 System.out.print(" Ox" 201 + Integer.toHexString((int)data[i])); 202 } 203 } 204 System.out.println(); 205 } 206 } 207 208 | Popular Tags |