1. /*
  2. * @(#)JPEGImageWriterSpi.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.imageio.plugins.jpeg;
  8. import javax.imageio.spi.ImageWriterSpi;
  9. import javax.imageio.spi.ServiceRegistry;
  10. import javax.imageio.spi.IIORegistry;
  11. import javax.imageio.ImageWriter;
  12. import javax.imageio.ImageTypeSpecifier;
  13. import javax.imageio.IIOException;
  14. import java.awt.image.ColorModel;
  15. import java.awt.image.IndexColorModel;
  16. import java.awt.image.SampleModel;
  17. import java.util.Locale;
  18. public class JPEGImageWriterSpi extends ImageWriterSpi {
  19. private static String [] readerSpiNames =
  20. {"com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi"};
  21. private boolean registered = false;
  22. public JPEGImageWriterSpi() {
  23. super(JPEG.vendor,
  24. JPEG.version,
  25. JPEG.names,
  26. JPEG.suffixes,
  27. JPEG.MIMETypes,
  28. "com.sun.imageio.plugins.jpeg.JPEGImageWriter",
  29. STANDARD_OUTPUT_TYPE,
  30. readerSpiNames,
  31. true,
  32. JPEG.nativeStreamMetadataFormatName,
  33. JPEG.nativeStreamMetadataFormatClassName,
  34. null, null,
  35. true,
  36. JPEG.nativeImageMetadataFormatName,
  37. JPEG.nativeImageMetadataFormatClassName,
  38. null, null
  39. );
  40. }
  41. public String getDescription(Locale locale) {
  42. return "Standard JPEG Image Writer";
  43. }
  44. public void onRegistration(ServiceRegistry registry,
  45. Class category) {
  46. if (registered) {
  47. return;
  48. }
  49. try {
  50. java.security.AccessController.doPrivileged(
  51. new sun.security.action.LoadLibraryAction("jpeg"));
  52. } catch (Throwable e) { // Fail on any Throwable
  53. // if it can't be loaded, deregister and return
  54. registry.deregisterServiceProvider(this);
  55. return;
  56. }
  57. registered = true;
  58. }
  59. public boolean isFormatLossless() {
  60. return false;
  61. }
  62. public boolean canEncodeImage(ImageTypeSpecifier type) {
  63. SampleModel sampleModel = type.getSampleModel();
  64. // Find the maximum bit depth across all channels
  65. int[] sampleSize = sampleModel.getSampleSize();
  66. int bitDepth = sampleSize[0];
  67. for (int i = 1; i < sampleSize.length; i++) {
  68. if (sampleSize[i] > bitDepth) {
  69. bitDepth = sampleSize[i];
  70. }
  71. }
  72. // 4450894: Ensure bitDepth is between 1 and 8
  73. if (bitDepth < 1 || bitDepth > 8) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. public ImageWriter createWriterInstance(Object extension)
  79. throws IIOException {
  80. return new JPEGImageWriter(this);
  81. }
  82. }