1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.spi.ImageWriterSpi ; 11 import javax.imageio.spi.ServiceRegistry ; 12 import javax.imageio.spi.IIORegistry ; 13 import javax.imageio.ImageWriter ; 14 import javax.imageio.ImageTypeSpecifier ; 15 import javax.imageio.IIOException ; 16 17 import java.awt.image.ColorModel ; 18 import java.awt.image.IndexColorModel ; 19 import java.awt.image.SampleModel ; 20 import java.util.Locale ; 21 22 public class JPEGImageWriterSpi extends ImageWriterSpi { 23 24 private static String [] readerSpiNames = 25 {"com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi"}; 26 27 private boolean registered = false; 28 29 public JPEGImageWriterSpi() { 30 super(JPEG.vendor, 31 JPEG.version, 32 JPEG.names, 33 JPEG.suffixes, 34 JPEG.MIMETypes, 35 "com.sun.imageio.plugins.jpeg.JPEGImageWriter", 36 STANDARD_OUTPUT_TYPE, 37 readerSpiNames, 38 true, 39 JPEG.nativeStreamMetadataFormatName, 40 JPEG.nativeStreamMetadataFormatClassName, 41 null, null, 42 true, 43 JPEG.nativeImageMetadataFormatName, 44 JPEG.nativeImageMetadataFormatClassName, 45 null, null 46 ); 47 } 48 49 public String getDescription(Locale locale) { 50 return "Standard JPEG Image Writer"; 51 } 52 53 public void onRegistration(ServiceRegistry registry, 54 Class <?> category) { 55 if (registered) { 56 return; 57 } 58 try { 59 java.security.AccessController.doPrivileged( 60 new sun.security.action.LoadLibraryAction("jpeg")); 61 } catch (Throwable e) { registry.deregisterServiceProvider(this); 64 return; 65 } 66 67 registered = true; 68 } 69 70 public boolean isFormatLossless() { 71 return false; 72 } 73 74 public boolean canEncodeImage(ImageTypeSpecifier type) { 75 SampleModel sampleModel = type.getSampleModel(); 76 77 int[] sampleSize = sampleModel.getSampleSize(); 79 int bitDepth = sampleSize[0]; 80 for (int i = 1; i < sampleSize.length; i++) { 81 if (sampleSize[i] > bitDepth) { 82 bitDepth = sampleSize[i]; 83 } 84 } 85 86 if (bitDepth < 1 || bitDepth > 8) { 88 return false; 89 } 90 91 return true; 92 } 93 94 public ImageWriter createWriterInstance(Object extension) 95 throws IIOException { 96 return new JPEGImageWriter(this); 97 } 98 } 99 | Popular Tags |