1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.IIOException ; 11 import javax.imageio.metadata.IIOInvalidTreeException ; 12 import javax.imageio.metadata.IIOMetadataNode ; 13 import javax.imageio.stream.ImageOutputStream ; 14 15 import java.io.IOException ; 16 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NamedNodeMap ; 19 20 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 { 39 super(buffer); 40 buffer.bufPtr += ID_SIZE; 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 node) throws IIOInvalidTreeException { 52 this(0); updateFromNativeNode(node, true); 54 } 55 56 IIOMetadataNode getNativeNode() { 57 IIOMetadataNode node = new IIOMetadataNode ("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 node, boolean fromScratch) 67 throws IIOInvalidTreeException { 68 NamedNodeMap 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 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 91 void write(ImageOutputStream ios) throws IOException { 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 ios, int transform) 103 throws IOException { 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 |