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. * Copyright 2004-2006 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.image;
  18. import java.io.IOException;
  19. import org.apache.xmlgraphics.image.codec.png.PNGRed;
  20. import org.apache.xmlgraphics.image.codec.png.PNGDecodeParam;
  21. import org.apache.xmlgraphics.image.codec.util.SeekableStream;
  22. import org.apache.xmlgraphics.image.rendered.CachableRed;
  23. import org.apache.commons.io.IOUtils;
  24. /**
  25. * FopImage object using PNG
  26. * @author Eric SCHAEFFER
  27. * @see AbstractFopImage
  28. * @see FopImage
  29. */
  30. public class PNGImage extends XmlGraphicsCommonsImage {
  31. /**
  32. * Constructs a new PNGImage instance.
  33. * @param imgReader basic metadata for the image
  34. */
  35. public PNGImage(FopImage.ImageInfo imgReader) {
  36. super(imgReader);
  37. this.loaded = 0; //TODO The PNGReader cannot read the resolution, yet.
  38. }
  39. /**
  40. * @see org.apache.fop.image.XmlGraphicsCommonsImage#decodeImage(
  41. * org.apache.xmlgraphics.image.codec.util.SeekableStream)
  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. }