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. package com.sun.imageio.plugins.png;
  8. import java.awt.image.ColorModel;
  9. import java.awt.image.IndexColorModel;
  10. import java.awt.image.SampleModel;
  11. import java.util.Locale;
  12. import javax.imageio.ImageWriter;
  13. import javax.imageio.ImageTypeSpecifier;
  14. import javax.imageio.metadata.IIOMetadataFormat;
  15. import javax.imageio.metadata.IIOMetadataFormatImpl;
  16. import javax.imageio.spi.ImageWriterSpi;
  17. /**
  18. * @version 0.5
  19. */
  20. public class PNGImageWriterSpi extends ImageWriterSpi {
  21. private static final String vendorName = "Sun Microsystems, Inc.";
  22. private static final String version = "1.0";
  23. private static final String[] names = { "png", "PNG" };
  24. private static final String[] suffixes = { "png", "PNG" };
  25. private static final String[] MIMETypes = { "image/png", "image/x-png" };
  26. private static final String writerClassName =
  27. "com.sun.imageio.plugins.png.PNGImageWriter";
  28. private static final String[] readerSpiNames = {
  29. "com.sun.imageio.plugins.png.PNGImageReaderSpi"
  30. };
  31. public PNGImageWriterSpi() {
  32. super(vendorName,
  33. version,
  34. names,
  35. suffixes,
  36. MIMETypes,
  37. writerClassName,
  38. STANDARD_OUTPUT_TYPE,
  39. readerSpiNames,
  40. false,
  41. null, null,
  42. null, null,
  43. true,
  44. PNGMetadata.nativeMetadataFormatName,
  45. "com.sun.imageio.plugins.png.PNGMetadataFormat",
  46. null, null
  47. );
  48. }
  49. public boolean canEncodeImage(ImageTypeSpecifier type) {
  50. SampleModel sampleModel = type.getSampleModel();
  51. ColorModel colorModel = type.getColorModel();
  52. // Find the maximum bit depth across all channels
  53. int[] sampleSize = sampleModel.getSampleSize();
  54. int bitDepth = sampleSize[0];
  55. for (int i = 1; i < sampleSize.length; i++) {
  56. if (sampleSize[i] > bitDepth) {
  57. bitDepth = sampleSize[i];
  58. }
  59. }
  60. // Ensure bitDepth is between 1 and 16
  61. if (bitDepth < 1 || bitDepth > 16) {
  62. return false;
  63. }
  64. // Check number of bands, alpha
  65. int numBands = sampleModel.getNumBands();
  66. if (numBands < 1 || numBands > 4) {
  67. return false;
  68. }
  69. boolean hasAlpha = colorModel.hasAlpha();
  70. // Fix 4464413: PNGTransparency reg-test was failing
  71. // because for IndexColorModels that have alpha,
  72. // numBands == 1 && hasAlpha == true, thus causing
  73. // the check below to fail and return false.
  74. if (colorModel instanceof IndexColorModel) {
  75. return true;
  76. }
  77. if ((numBands == 1 || numBands == 3) && hasAlpha) {
  78. return false;
  79. }
  80. if ((numBands == 2 || numBands == 4) && !hasAlpha) {
  81. return false;
  82. }
  83. return true;
  84. }
  85. public String getDescription(Locale locale) {
  86. return "Standard PNG image writer";
  87. }
  88. public ImageWriter createWriterInstance(Object extension) {
  89. return new PNGImageWriter(this);
  90. }
  91. }