1 7 8 package com.sun.imageio.plugins.png; 9 10 import java.awt.image.ColorModel ; 11 import java.awt.image.IndexColorModel ; 12 import java.awt.image.SampleModel ; 13 import java.util.Locale ; 14 import javax.imageio.ImageWriter ; 15 import javax.imageio.ImageTypeSpecifier ; 16 import javax.imageio.metadata.IIOMetadataFormat ; 17 import javax.imageio.metadata.IIOMetadataFormatImpl ; 18 import javax.imageio.spi.ImageWriterSpi ; 19 20 23 public class PNGImageWriterSpi extends ImageWriterSpi { 24 25 private static final String vendorName = "Sun Microsystems, Inc."; 26 27 private static final String version = "1.0"; 28 29 private static final String [] names = { "png", "PNG" }; 30 31 private static final String [] suffixes = { "png", "PNG" }; 32 33 private static final String [] MIMETypes = { "image/png", "image/x-png" }; 34 35 private static final String writerClassName = 36 "com.sun.imageio.plugins.png.PNGImageWriter"; 37 38 private static final String [] readerSpiNames = { 39 "com.sun.imageio.plugins.png.PNGImageReaderSpi" 40 }; 41 42 public PNGImageWriterSpi() { 43 super(vendorName, 44 version, 45 names, 46 suffixes, 47 MIMETypes, 48 writerClassName, 49 STANDARD_OUTPUT_TYPE, 50 readerSpiNames, 51 false, 52 null, null, 53 null, null, 54 true, 55 PNGMetadata.nativeMetadataFormatName, 56 "com.sun.imageio.plugins.png.PNGMetadataFormat", 57 null, null 58 ); 59 } 60 61 public boolean canEncodeImage(ImageTypeSpecifier type) { 62 SampleModel sampleModel = type.getSampleModel(); 63 ColorModel colorModel = type.getColorModel(); 64 65 int[] sampleSize = sampleModel.getSampleSize(); 67 int bitDepth = sampleSize[0]; 68 for (int i = 1; i < sampleSize.length; i++) { 69 if (sampleSize[i] > bitDepth) { 70 bitDepth = sampleSize[i]; 71 } 72 } 73 74 if (bitDepth < 1 || bitDepth > 16) { 76 return false; 77 } 78 79 int numBands = sampleModel.getNumBands(); 81 if (numBands < 1 || numBands > 4) { 82 return false; 83 } 84 85 boolean hasAlpha = colorModel.hasAlpha(); 86 if (colorModel instanceof IndexColorModel ) { 91 return true; 92 } 93 if ((numBands == 1 || numBands == 3) && hasAlpha) { 94 return false; 95 } 96 if ((numBands == 2 || numBands == 4) && !hasAlpha) { 97 return false; 98 } 99 100 return true; 101 } 102 103 public String getDescription(Locale locale) { 104 return "Standard PNG image writer"; 105 } 106 107 public ImageWriter createWriterInstance(Object extension) { 108 return new PNGImageWriter(this); 109 } 110 } 111 | Popular Tags |