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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = ((ImageRawPNG) 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. try {
  61. if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
  62. // means we have Gray, RGB, or Palette
  63. IOUtils.copy(in, out);
  64. } else {
  65. // means we have Gray + alpha or RGB + alpha
  66. int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
  67. int numColumns = image.getSize().getWidthPx();
  68. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  69. DataInputStream dataStream = new DataInputStream(infStream);
  70. int offset = 0;
  71. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  72. int filter;
  73. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
  74. // channel and then deflate the RGB channels back again
  75. // TODO: not using the baos below and using the original out instead (as happens in PDF)
  76. // would be preferable but that does not work with the rest of the postscript code; this
  77. // needs to be revisited
  78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  79. DeflaterOutputStream dos = new DeflaterOutputStream(/* out */baos, new Deflater());
  80. while ((filter = dataStream.read()) != -1) {
  81. byte[] bytes = new byte[bytesPerRow];
  82. dataStream.readFully(bytes, 0, bytesPerRow);
  83. dos.write((byte) filter);
  84. for (int j = 0; j < numColumns; j++) {
  85. dos.write(bytes, offset, numBytes);
  86. offset += numberOfInterleavedComponents;
  87. }
  88. offset = 0;
  89. }
  90. dos.close();
  91. IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out);
  92. }
  93. } finally {
  94. IOUtils.closeQuietly(in);
  95. }
  96. }
  97. /** {@inheritDoc} */
  98. public String getImplicitFilter() {
  99. String filter = "<< /Predictor 15 /Columns " + image.getSize().getWidthPx();
  100. filter += " /Colors " + (numberOfInterleavedComponents > 2 ? 3 : 1);
  101. filter += " /BitsPerComponent " + image.getBitDepth() + " >> /FlateDecode";
  102. return filter;
  103. }
  104. }