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.

AFPImageHandlerRenderedImage.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.afp;
  19. import java.awt.image.RenderedImage;
  20. import java.io.IOException;
  21. import org.apache.commons.io.output.ByteArrayOutputStream;
  22. import org.apache.fop.afp.AFPDataObjectInfo;
  23. import org.apache.fop.afp.AFPImageObjectInfo;
  24. import org.apache.fop.afp.AFPObjectAreaInfo;
  25. import org.apache.fop.afp.AFPPaintingState;
  26. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  27. import org.apache.xmlgraphics.image.loader.impl.ImageBuffered;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  29. import org.apache.xmlgraphics.ps.ImageEncodingHelper;
  30. import org.apache.xmlgraphics.util.MimeConstants;
  31. /**
  32. * PDFImageHandler implementation which handles RenderedImage instances.
  33. */
  34. public class AFPImageHandlerRenderedImage extends AFPImageHandler {
  35. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  36. ImageFlavor.BUFFERED_IMAGE,
  37. ImageFlavor.RENDERED_IMAGE
  38. };
  39. private static final Class[] CLASSES = new Class[] {
  40. ImageBuffered.class,
  41. ImageRendered.class
  42. };
  43. /** {@inheritDoc} */
  44. public AFPDataObjectInfo generateDataObjectInfo(
  45. AFPRendererImageInfo rendererImageInfo) throws IOException {
  46. AFPImageObjectInfo imageObjectInfo
  47. = (AFPImageObjectInfo)super.generateDataObjectInfo(rendererImageInfo);
  48. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
  49. AFPObjectAreaInfo objectAreaInfo = imageObjectInfo.getObjectAreaInfo();
  50. AFPRendererContext rendererContext
  51. = (AFPRendererContext)rendererImageInfo.getRendererContext();
  52. AFPInfo afpInfo = rendererContext.getInfo();
  53. AFPPaintingState paintingState = afpInfo.getPaintingState();
  54. int resolution = paintingState.getResolution();
  55. objectAreaInfo.setWidthRes(resolution);
  56. objectAreaInfo.setHeightRes(resolution);
  57. ImageRendered imageRendered = (ImageRendered) rendererImageInfo.img;
  58. RenderedImage renderedImage = imageRendered.getRenderedImage();
  59. int dataHeight = renderedImage.getHeight();
  60. imageObjectInfo.setDataHeight(dataHeight);
  61. int dataWidth = renderedImage.getWidth();
  62. imageObjectInfo.setDataWidth(dataWidth);
  63. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64. ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, baos);
  65. byte[] imageData = baos.toByteArray();
  66. boolean colorImages = paintingState.isColorImages();
  67. imageObjectInfo.setColor(colorImages);
  68. // convert to grayscale
  69. if (!colorImages) {
  70. baos.reset();
  71. int bitsPerPixel = paintingState.getBitsPerPixel();
  72. imageObjectInfo.setBitsPerPixel(bitsPerPixel);
  73. ImageEncodingHelper.encodeRGBAsGrayScale(
  74. imageData, dataWidth, dataHeight, bitsPerPixel, baos);
  75. imageData = baos.toByteArray();
  76. }
  77. imageObjectInfo.setData(imageData);
  78. return imageObjectInfo;
  79. }
  80. /** {@inheritDoc} */
  81. protected AFPDataObjectInfo createDataObjectInfo() {
  82. return new AFPImageObjectInfo();
  83. }
  84. /** {@inheritDoc} */
  85. public int getPriority() {
  86. return 300;
  87. }
  88. /** {@inheritDoc} */
  89. public Class[] getSupportedImageClasses() {
  90. return CLASSES;
  91. }
  92. /** {@inheritDoc} */
  93. public ImageFlavor[] getSupportedImageFlavors() {
  94. return FLAVORS;
  95. }
  96. }