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.

PNGImage.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.image;
  19. import java.io.IOException;
  20. import org.apache.xmlgraphics.image.codec.png.PNGRed;
  21. import org.apache.xmlgraphics.image.codec.png.PNGDecodeParam;
  22. import org.apache.xmlgraphics.image.codec.util.SeekableStream;
  23. import org.apache.xmlgraphics.image.rendered.CachableRed;
  24. import org.apache.commons.io.IOUtils;
  25. /**
  26. * FopImage object using PNG
  27. *
  28. * @see AbstractFopImage
  29. * @see FopImage
  30. */
  31. public class PNGImage extends XmlGraphicsCommonsImage {
  32. /**
  33. * Constructs a new PNGImage instance.
  34. * @param imgReader basic metadata for the image
  35. */
  36. public PNGImage(FopImage.ImageInfo imgReader) {
  37. super(imgReader);
  38. this.loaded = 0; //TODO The PNGReader cannot read the resolution, yet.
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. protected CachableRed decodeImage(SeekableStream stream) throws IOException {
  44. PNGDecodeParam param = new PNGDecodeParam();
  45. param.setPerformGammaCorrection(true);
  46. param.setDisplayExponent(2.2f); // sRGB gamma
  47. PNGRed red = new PNGRed(stream, param);
  48. String unit = (String)red.getProperty("pixel_units");
  49. if ("Meters".equals(unit)) {
  50. this.dpiHorizontal = ((Integer)red.getProperty("x_pixels_per_unit")).intValue()
  51. * 25.4f / 1000f;
  52. this.dpiVertical = ((Integer)red.getProperty("y_pixels_per_unit")).intValue()
  53. * 25.4f / 1000f;
  54. }
  55. return red;
  56. }
  57. /**
  58. * Load the original PNG data.
  59. * This loads the original PNG data as is into memory.
  60. *
  61. * @return true if loaded false for any error
  62. */
  63. protected boolean loadOriginalData() {
  64. try {
  65. seekableInput.seek(0);
  66. this.raw = IOUtils.toByteArray(seekableInput);
  67. } catch (java.io.IOException ex) {
  68. log.error("Error while loading raw image: " + ex.getMessage(), ex);
  69. return false;
  70. } finally {
  71. IOUtils.closeQuietly(inputStream);
  72. inputStream = null;
  73. }
  74. return true;
  75. }
  76. }