KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > png > PNGImageWriterSpi


1 /*
2  * @(#)PNGImageWriterSpi.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.png;
9
10 import java.awt.image.ColorModel JavaDoc;
11 import java.awt.image.IndexColorModel JavaDoc;
12 import java.awt.image.SampleModel JavaDoc;
13 import java.util.Locale JavaDoc;
14 import javax.imageio.ImageWriter JavaDoc;
15 import javax.imageio.ImageTypeSpecifier JavaDoc;
16 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
17 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
18 import javax.imageio.spi.ImageWriterSpi JavaDoc;
19
20 /**
21  * @version 0.5
22  */

23 public class PNGImageWriterSpi extends ImageWriterSpi JavaDoc {
24
25     private static final String JavaDoc vendorName = "Sun Microsystems, Inc.";
26     
27     private static final String JavaDoc version = "1.0";
28     
29     private static final String JavaDoc[] names = { "png", "PNG" };
30     
31     private static final String JavaDoc[] suffixes = { "png", "PNG" };
32     
33     private static final String JavaDoc[] MIMETypes = { "image/png", "image/x-png" };
34     
35     private static final String JavaDoc writerClassName =
36         "com.sun.imageio.plugins.png.PNGImageWriter";
37     
38     private static final String JavaDoc[] 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 JavaDoc type) {
62         SampleModel JavaDoc sampleModel = type.getSampleModel();
63         ColorModel JavaDoc colorModel = type.getColorModel();
64
65         // Find the maximum bit depth across all channels
66
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         // Ensure bitDepth is between 1 and 16
75
if (bitDepth < 1 || bitDepth > 16) {
76             return false;
77         }
78
79         // Check number of bands, alpha
80
int numBands = sampleModel.getNumBands();
81         if (numBands < 1 || numBands > 4) {
82             return false;
83         }
84
85         boolean hasAlpha = colorModel.hasAlpha();
86         // Fix 4464413: PNGTransparency reg-test was failing
87
// because for IndexColorModels that have alpha,
88
// numBands == 1 && hasAlpha == true, thus causing
89
// the check below to fail and return false.
90
if (colorModel instanceof IndexColorModel JavaDoc) {
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 JavaDoc getDescription(Locale JavaDoc locale) {
104         return "Standard PNG image writer";
105     }
106
107     public ImageWriter JavaDoc createWriterInstance(Object JavaDoc extension) {
108         return new PNGImageWriter(this);
109     }
110 }
111
Popular Tags