40 import java.io.IOException; 41 42 import javax.imageio.IIOImage; 43 import javax.imageio.IIOException; 44 import javax.imageio.ImageTypeSpecifier; 45 import javax.imageio.ImageWriteParam; 46 import javax.imageio.ImageWriter; 47 import javax.imageio.metadata.IIOMetadata; 48 import javax.imageio.metadata.IIOMetadataFormatImpl; 49 import javax.imageio.metadata.IIOInvalidTreeException; 50 import javax.imageio.spi.ImageWriterSpi; 51 import javax.imageio.stream.ImageOutputStream; 52 53 import com.sun.imageio.plugins.common.I18N; 54 55 /** 56 * The Java Image IO plugin writer for encoding a binary RenderedImage into 57 * a WBMP format. 58 * 59 * The encoding process may clip, subsample using the parameters 60 * specified in the <code>ImageWriteParam</code>. 61 * 62 * @see com.sun.media.imageio.plugins.WBMPImageWriteParam 63 */ 64 public class WBMPImageWriter extends ImageWriter { 65 /** The output stream to write into */ 66 private ImageOutputStream stream = null; 67 68 // Get the number of bits required to represent an int. 69 private static int getNumBits(int intValue) { 70 int numBits = 32; 71 int mask = 0x80000000; 72 while(mask != 0 && (intValue & mask) == 0) { 73 numBits--; 74 mask >>>= 1; 75 } 76 return numBits; 77 } 78 79 // Convert an int value to WBMP multi-byte format. 80 private static byte[] intToMultiByte(int intValue) { 81 int numBitsLeft = getNumBits(intValue); 82 byte[] multiBytes = new byte[(numBitsLeft + 6)/7]; 83 84 int maxIndex = multiBytes.length - 1; 85 for(int b = 0; b <= maxIndex; b++) { 86 multiBytes[b] = (byte)((intValue >>> ((maxIndex - b)*7))&0x7f); 87 if(b != maxIndex) { 88 multiBytes[b] |= (byte)0x80; 89 } 90 } 91 92 return multiBytes; 93 } 94 95 /** Constructs <code>WBMPImageWriter</code> based on the provided 96 * <code>ImageWriterSpi</code>. 97 */ 98 public WBMPImageWriter(ImageWriterSpi originator) { 99 super(originator); 100 } 101 102 public void setOutput(Object output) { 103 super.setOutput(output); // validates output 104 if (output != null) { 105 if (!(output instanceof ImageOutputStream)) 106 throw new IllegalArgumentException(I18N.getString("WBMPImageWriter")); 107 this.stream = (ImageOutputStream)output; 108 } else 109 this.stream = null; 110 } 111 112 public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) { 113 return null; 114 } 115 116 public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, | 40 import java.io.IOException; 41 42 import javax.imageio.IIOImage; 43 import javax.imageio.IIOException; 44 import javax.imageio.ImageTypeSpecifier; 45 import javax.imageio.ImageWriteParam; 46 import javax.imageio.ImageWriter; 47 import javax.imageio.metadata.IIOMetadata; 48 import javax.imageio.metadata.IIOMetadataFormatImpl; 49 import javax.imageio.metadata.IIOInvalidTreeException; 50 import javax.imageio.spi.ImageWriterSpi; 51 import javax.imageio.stream.ImageOutputStream; 52 53 import com.sun.imageio.plugins.common.I18N; 54 55 /** 56 * The Java Image IO plugin writer for encoding a binary RenderedImage into 57 * a WBMP format. 58 * 59 * The encoding process may clip, subsample using the parameters 60 * specified in the {@code ImageWriteParam}. 61 * 62 * @see com.sun.media.imageio.plugins.WBMPImageWriteParam 63 */ 64 public class WBMPImageWriter extends ImageWriter { 65 /** The output stream to write into */ 66 private ImageOutputStream stream = null; 67 68 // Get the number of bits required to represent an int. 69 private static int getNumBits(int intValue) { 70 int numBits = 32; 71 int mask = 0x80000000; 72 while(mask != 0 && (intValue & mask) == 0) { 73 numBits--; 74 mask >>>= 1; 75 } 76 return numBits; 77 } 78 79 // Convert an int value to WBMP multi-byte format. 80 private static byte[] intToMultiByte(int intValue) { 81 int numBitsLeft = getNumBits(intValue); 82 byte[] multiBytes = new byte[(numBitsLeft + 6)/7]; 83 84 int maxIndex = multiBytes.length - 1; 85 for(int b = 0; b <= maxIndex; b++) { 86 multiBytes[b] = (byte)((intValue >>> ((maxIndex - b)*7))&0x7f); 87 if(b != maxIndex) { 88 multiBytes[b] |= (byte)0x80; 89 } 90 } 91 92 return multiBytes; 93 } 94 95 /** Constructs {@code WBMPImageWriter} based on the provided 96 * {@code ImageWriterSpi}. 97 */ 98 public WBMPImageWriter(ImageWriterSpi originator) { 99 super(originator); 100 } 101 102 public void setOutput(Object output) { 103 super.setOutput(output); // validates output 104 if (output != null) { 105 if (!(output instanceof ImageOutputStream)) 106 throw new IllegalArgumentException(I18N.getString("WBMPImageWriter")); 107 this.stream = (ImageOutputStream)output; 108 } else 109 this.stream = null; 110 } 111 112 public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) { 113 return null; 114 } 115 116 public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, |