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.

PSImageHandlerRawPNG.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Dimension;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.ColorModel;
  23. import java.io.IOException;
  24. import org.apache.xmlgraphics.image.loader.Image;
  25. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  26. import org.apache.xmlgraphics.image.loader.ImageInfo;
  27. import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
  28. import org.apache.xmlgraphics.ps.FormGenerator;
  29. import org.apache.xmlgraphics.ps.ImageEncoder;
  30. import org.apache.xmlgraphics.ps.ImageFormGenerator;
  31. import org.apache.xmlgraphics.ps.PSGenerator;
  32. import org.apache.xmlgraphics.ps.PSImageUtils;
  33. import org.apache.fop.render.RenderingContext;
  34. /**
  35. * Image handler implementation which handles raw (not decoded) PNG images for PostScript output.
  36. */
  37. public class PSImageHandlerRawPNG implements PSImageHandler {
  38. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {ImageFlavor.RAW_PNG};
  39. /** {@inheritDoc} */
  40. public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException {
  41. PSRenderingContext psContext = (PSRenderingContext) context;
  42. PSGenerator gen = psContext.getGenerator();
  43. ImageRawPNG png = (ImageRawPNG) image;
  44. float x = (float) pos.getX() / 1000f;
  45. float y = (float) pos.getY() / 1000f;
  46. float w = (float) pos.getWidth() / 1000f;
  47. float h = (float) pos.getHeight() / 1000f;
  48. Rectangle2D targetRect = new Rectangle2D.Float(x, y, w, h);
  49. ImageEncoder encoder = new ImageEncoderPNG(png);
  50. ImageInfo info = image.getInfo();
  51. Dimension imgDim = info.getSize().getDimensionPx();
  52. String imgDescription = image.getClass().getName();
  53. ColorModel cm = png.getColorModel();
  54. PSImageUtils.writeImage(encoder, imgDim, imgDescription, targetRect, cm, gen);
  55. }
  56. /** {@inheritDoc} */
  57. public void generateForm(RenderingContext context, Image image, PSImageFormResource form)
  58. throws IOException {
  59. PSRenderingContext psContext = (PSRenderingContext) context;
  60. PSGenerator gen = psContext.getGenerator();
  61. ImageRawPNG png = (ImageRawPNG) image;
  62. ImageInfo info = image.getInfo();
  63. String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
  64. ImageEncoder encoder = new ImageEncoderPNG(png);
  65. FormGenerator formGen = new ImageFormGenerator(form.getName(), imageDescription, info.getSize()
  66. .getDimensionPt(), info.getSize().getDimensionPx(), encoder, png.getColorSpace(),
  67. false);
  68. formGen.generate(gen);
  69. }
  70. /** {@inheritDoc} */
  71. public int getPriority() {
  72. return 200;
  73. }
  74. /** {@inheritDoc} */
  75. public Class<ImageRawPNG> getSupportedImageClass() {
  76. return ImageRawPNG.class;
  77. }
  78. /** {@inheritDoc} */
  79. public ImageFlavor[] getSupportedImageFlavors() {
  80. return FLAVORS;
  81. }
  82. /** {@inheritDoc} */
  83. public boolean isCompatible(RenderingContext targetContext, Image image) {
  84. if (targetContext instanceof PSRenderingContext) {
  85. PSRenderingContext psContext = (PSRenderingContext) targetContext;
  86. // The filters required for this implementation need PS level 2 or higher
  87. if (psContext.getGenerator().getPSLevel() >= 2) {
  88. return (image == null || image instanceof ImageRawPNG);
  89. }
  90. }
  91. return false;
  92. }
  93. }