You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageEncoderPNG.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.ps;
  19. import java.awt.image.ColorModel;
  20. import java.awt.image.IndexColorModel;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.DataInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.util.zip.Deflater;
  28. import java.util.zip.DeflaterOutputStream;
  29. import java.util.zip.Inflater;
  30. import java.util.zip.InflaterInputStream;
  31. import org.apache.commons.io.IOUtils;
  32. import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
  33. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  34. import org.apache.xmlgraphics.ps.ImageEncoder;
  35. /**
  36. * ImageEncoder implementation for PNG images.
  37. */
  38. public class ImageEncoderPNG implements ImageEncoder {
  39. private final ImageRawPNG image;
  40. private int numberOfInterleavedComponents;
  41. /**
  42. * Main constructor
  43. * @param image the PNG image
  44. */
  45. public ImageEncoderPNG(ImageRawPNG image) {
  46. this.image = image;
  47. ColorModel cm = this.image.getColorModel();
  48. if (cm instanceof IndexColorModel) {
  49. numberOfInterleavedComponents = 1;
  50. } else {
  51. // this can be 1 (gray), 2 (gray + alpha), 3 (rgb) or 4 (rgb + alpha)
  52. // numberOfInterleavedComponents = (cm.hasAlpha() ? 1 : 0) + cm.getNumColorComponents();
  53. numberOfInterleavedComponents = cm.getNumComponents();
  54. }
  55. }
  56. /** {@inheritDoc} */
  57. public void writeTo(OutputStream out) throws IOException {
  58. // TODO: refactor this code with equivalent PDF code
  59. InputStream in = ((ImageRawStream) image).createInputStream();
  60. InflaterInputStream infStream = null;
  61. DataInputStream dataStream = null;
  62. ByteArrayOutputStream baos = null;
  63. DeflaterOutputStream dos = null;
  64. try {
  65. if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
  66. // means we have Gray, RGB, or Palette
  67. IOUtils.copy(in, out);
  68. } else {
  69. // means we have Gray + alpha or RGB + alpha
  70. int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
  71. int numColumns = image.getSize().getWidthPx();
  72. infStream = new InflaterInputStream(in, new Inflater());
  73. dataStream = new DataInputStream(infStream);
  74. int offset = 0;
  75. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  76. int filter;
  77. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
  78. // channel and then deflate the RGB channels back again
  79. // TODO: not using the baos below and using the original out instead (as happens in PDF)
  80. // would be preferable but that does not work with the rest of the postscript code; this
  81. // needs to be revisited
  82. baos = new ByteArrayOutputStream();
  83. dos = new DeflaterOutputStream(/* out */baos, new Deflater());
  84. while ((filter = dataStream.read()) != -1) {
  85. byte[] bytes = new byte[bytesPerRow];
  86. dataStream.readFully(bytes, 0, bytesPerRow);
  87. dos.write((byte) filter);
  88. for (int j = 0; j < numColumns; j++) {
  89. dos.write(bytes, offset, numBytes);
  90. offset += numberOfInterleavedComponents;
  91. }
  92. offset = 0;
  93. }
  94. dos.close();
  95. IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out);
  96. }
  97. } finally {
  98. IOUtils.closeQuietly(dos);
  99. IOUtils.closeQuietly(baos);
  100. IOUtils.closeQuietly(dataStream);
  101. IOUtils.closeQuietly(infStream);
  102. IOUtils.closeQuietly(in);
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. public String getImplicitFilter() {
  107. String filter = "<< /Predictor 15 /Columns " + image.getSize().getWidthPx();
  108. filter += " /Colors " + (numberOfInterleavedComponents > 2 ? 3 : 1);
  109. filter += " /BitsPerComponent " + image.getBitDepth() + " >> /FlateDecode";
  110. return filter;
  111. }
  112. }