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

https://github.com/conceptboard/TwelveMonkeys · Java · 145 lines · 73 code · 21 blank · 51 comment · 18 complexity · 985271836acd6110d29cd1d3ef04c5c8 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.image.ImageUtil;
  30. import com.twelvemonkeys.imageio.ImageWriterBase;
  31. import com.twelvemonkeys.imageio.util.IIOUtil;
  32. import org.apache.batik.ext.awt.image.codec.ImageEncodeParam;
  33. import org.apache.batik.ext.awt.image.codec.tiff.TIFFEncodeParam;
  34. import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageEncoder;
  35. import javax.imageio.IIOImage;
  36. import javax.imageio.ImageTypeSpecifier;
  37. import javax.imageio.ImageWriteParam;
  38. import javax.imageio.metadata.IIOMetadata;
  39. import javax.imageio.spi.ImageWriterSpi;
  40. import java.awt.image.BufferedImage;
  41. import java.awt.image.RenderedImage;
  42. import java.io.IOException;
  43. /**
  44. * TIFFImageWriter class description.
  45. *
  46. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  47. * @author last modified by $Author: haku $
  48. * @version $Id: TIFFImageWriter.java,v 1.0 29.jul.2004 12:52:54 haku Exp $
  49. */
  50. public class TIFFImageWriter extends ImageWriterBase {
  51. private TIFFImageEncoder encoder;
  52. protected TIFFImageWriter(final ImageWriterSpi pProvider) {
  53. super(pProvider);
  54. }
  55. @Override
  56. public void setOutput(final Object output) {
  57. encoder = null;
  58. super.setOutput(output);
  59. }
  60. public IIOMetadata getDefaultImageMetadata(final ImageTypeSpecifier imageType, final ImageWriteParam param) {
  61. throw new UnsupportedOperationException("Method getDefaultImageMetadata not implemented");// TODO: Implement
  62. }
  63. public IIOMetadata convertImageMetadata(final IIOMetadata inData, final ImageTypeSpecifier imageType, final ImageWriteParam param) {
  64. throw new UnsupportedOperationException("Method convertImageMetadata not implemented");// TODO: Implement
  65. }
  66. public void write(final IIOMetadata pStreamMetadata, final IIOImage pImage, final ImageWriteParam pParam) throws IOException {
  67. RenderedImage renderedImage = pImage.getRenderedImage();
  68. init();
  69. ImageEncodeParam param;
  70. if (pParam != null) {
  71. param = new TIFFEncodeParam();
  72. // TODO: Convert params
  73. encoder.setParam(param);
  74. }
  75. BufferedImage image;
  76. // FIX: TIFFEnocder chokes on a any of the TYPE_INT_* types...
  77. // (The TIFFEncoder expects int types to have 1 sample of size 32
  78. // while there actually is 4 samples of size 8, according to the
  79. // SampleModel...)
  80. if (renderedImage instanceof BufferedImage && (
  81. ((BufferedImage) renderedImage).getType() == BufferedImage.TYPE_INT_ARGB
  82. || ((BufferedImage) renderedImage).getType() == BufferedImage.TYPE_INT_ARGB_PRE)) {
  83. image = ImageUtil.toBuffered(renderedImage, BufferedImage.TYPE_4BYTE_ABGR);
  84. }
  85. else if (renderedImage instanceof BufferedImage && (
  86. ((BufferedImage) renderedImage).getType() == BufferedImage.TYPE_INT_BGR
  87. || ((BufferedImage) renderedImage).getType() == BufferedImage.TYPE_INT_RGB)) {
  88. image = ImageUtil.toBuffered(renderedImage, BufferedImage.TYPE_3BYTE_BGR);
  89. }
  90. else {
  91. image = ImageUtil.toBuffered(renderedImage);
  92. }
  93. image = fakeAOI(image, pParam);
  94. image = ImageUtil.toBuffered(fakeSubsampling(image, pParam));
  95. /*
  96. System.out.println("Image: " + pImage);
  97. SampleModel sampleModel = pImage.getSampleModel();
  98. System.out.println("SampleModel: " + sampleModel);
  99. int sampleSize[] = sampleModel.getSampleSize();
  100. System.out.println("Samples: " + sampleSize.length);
  101. for (int i = 0; i < sampleSize.length; i++) {
  102. System.out.println("SampleSize[" + i + "]: " + sampleSize[i]);
  103. }
  104. int dataType = sampleModel.getDataType();
  105. System.out.println("DataType: " + dataType);
  106. */
  107. processImageStarted(0);
  108. encoder.encode(image);
  109. imageOutput.flush();
  110. processImageComplete();
  111. }
  112. public void dispose() {
  113. super.dispose();
  114. encoder = null;
  115. }
  116. private synchronized void init() {
  117. if (encoder == null) {
  118. if (imageOutput == null) {
  119. throw new IllegalStateException("output == null");
  120. }
  121. encoder = new TIFFImageEncoder(IIOUtil.createStreamAdapter(imageOutput), null);
  122. }
  123. }
  124. }