/imageio/imageio-batik/src/main/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderSpi.java

https://github.com/conceptboard/TwelveMonkeys · Java · 125 lines · 71 code · 16 blank · 38 comment · 19 complexity · 02081fbf19ba1e48a57ef795955aaa00 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, Harald Kuhr
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name "TwelveMonkeys" nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package com.twelvemonkeys.imageio.plugins.tiff;
  29. import com.twelvemonkeys.imageio.spi.ProviderInfo;
  30. import com.twelvemonkeys.lang.SystemUtil;
  31. import com.twelvemonkeys.imageio.util.IIOUtil;
  32. import javax.imageio.ImageReader;
  33. import javax.imageio.spi.ImageReaderSpi;
  34. import javax.imageio.spi.ServiceRegistry;
  35. import javax.imageio.stream.ImageInputStream;
  36. import java.io.IOException;
  37. import java.util.Locale;
  38. /**
  39. * TIFFImageReaderSpi
  40. * <p/>
  41. *
  42. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  43. * @version $Id: TIFFImageReaderSpi.java,v 1.1 2003/12/02 16:45:00 wmhakur Exp $
  44. */
  45. public class TIFFImageReaderSpi extends ImageReaderSpi {
  46. final static boolean TIFF_CLASSES_AVAILABLE = SystemUtil.isClassAvailable("com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader");
  47. /**
  48. * Creates a {@code TIFFImageReaderSpi}.
  49. */
  50. public TIFFImageReaderSpi() {
  51. this(IIOUtil.getProviderInfo(TIFFImageReaderSpi.class));
  52. }
  53. private TIFFImageReaderSpi(final ProviderInfo pProviderInfo) {
  54. super(
  55. pProviderInfo.getVendorName(), // Vendor name
  56. pProviderInfo.getVersion(), // Version
  57. TIFF_CLASSES_AVAILABLE ? new String[]{"tiff", "TIFF"} : new String[] {""}, // Names
  58. TIFF_CLASSES_AVAILABLE ? new String[]{"tiff", "tif"} : null, // Suffixes
  59. TIFF_CLASSES_AVAILABLE ? new String[]{"image/tiff", "image/x-tiff"} : null, // Mime-types
  60. "com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader", // Writer class name..?
  61. ImageReaderSpi.STANDARD_INPUT_TYPE, // Output types
  62. new String[]{"com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi"}, // Writer SPI names
  63. true, // Supports standard stream metadata format
  64. null, // Native stream metadata format name
  65. null, // Native stream metadata format class name
  66. null, // Extra stream metadata format names
  67. null, // Extra stream metadata format class names
  68. true, // Supports standard image metadata format
  69. null, // Native image metadata format name
  70. null, // Native image metadata format class name
  71. null, // Extra image metadata format names
  72. null // Extra image metadata format class names
  73. );
  74. }
  75. public boolean canDecodeInput(Object source) throws IOException {
  76. return source instanceof ImageInputStream && TIFF_CLASSES_AVAILABLE && canDecode((ImageInputStream) source);
  77. }
  78. static boolean canDecode(ImageInputStream pInput) throws IOException {
  79. try {
  80. pInput.mark();
  81. int byte0 = pInput.read(); // Byte order 1 (M or I)
  82. int byte1 = pInput.read(); // Byte order 2 (always same as 1)
  83. int byte2 = pInput.read(); // Version number 1 (M: 0, I: 42)
  84. int byte3 = pInput.read(); // Version number 2 (M: 42, I: 0)
  85. // Test for Motorola or Intel byte order, and version number == 42
  86. if ((byte0 == 'M' && byte1 == 'M' && byte2 == 0 && byte3 == 42)
  87. || (byte0 == 'I' && byte1 == 'I' && byte2 == 42 && byte3 == 0)) {
  88. return true;
  89. }
  90. }
  91. finally {
  92. pInput.reset();
  93. }
  94. return false;
  95. }
  96. public ImageReader createReaderInstance(Object extension) throws IOException {
  97. return new TIFFImageReader(this);
  98. }
  99. public String getDescription(Locale locale) {
  100. return "Tagged Image File Format (TIFF) image reader";
  101. }
  102. @SuppressWarnings({"deprecation"})
  103. @Override
  104. public void onRegistration(ServiceRegistry registry, Class<?> category) {
  105. if (!TIFF_CLASSES_AVAILABLE) {
  106. IIOUtil.deregisterProvider(registry, this, category);
  107. }
  108. }
  109. }