Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AFPImageRenderedFactory.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.impl.ImageRendered;
  27. import org.apache.xmlgraphics.ps.ImageEncodingHelper;
  28. import org.apache.xmlgraphics.util.MimeConstants;
  29. /**
  30. * A buffered image data object info factory
  31. */
  32. public class AFPImageRenderedFactory extends AFPDataObjectInfoFactory {
  33. /**
  34. * Main constructor
  35. *
  36. * @param state the AFP painting state
  37. */
  38. public AFPImageRenderedFactory(AFPPaintingState state) {
  39. super(state);
  40. }
  41. /** {@inheritDoc} */
  42. public AFPDataObjectInfo create(AFPRendererImageInfo rendererImageInfo) throws IOException {
  43. AFPImageObjectInfo imageObjectInfo
  44. = (AFPImageObjectInfo)super.create(rendererImageInfo);
  45. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
  46. AFPObjectAreaInfo objectAreaInfo = imageObjectInfo.getObjectAreaInfo();
  47. int resolution = state.getResolution();
  48. objectAreaInfo.setWidthRes(resolution);
  49. objectAreaInfo.setHeightRes(resolution);
  50. ImageRendered imageRendered = (ImageRendered) rendererImageInfo.img;
  51. RenderedImage renderedImage = imageRendered.getRenderedImage();
  52. int dataHeight = renderedImage.getHeight();
  53. imageObjectInfo.setDataHeight(dataHeight);
  54. int dataWidth = renderedImage.getWidth();
  55. imageObjectInfo.setDataWidth(dataWidth);
  56. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  57. ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, baos);
  58. byte[] imageData = baos.toByteArray();
  59. boolean colorImages = state.isColorImages();
  60. imageObjectInfo.setColor(colorImages);
  61. // convert to grayscale
  62. if (!colorImages) {
  63. baos.reset();
  64. int bitsPerPixel = state.getBitsPerPixel();
  65. imageObjectInfo.setBitsPerPixel(bitsPerPixel);
  66. ImageEncodingHelper.encodeRGBAsGrayScale(
  67. imageData, dataWidth, dataHeight, bitsPerPixel, baos);
  68. imageData = baos.toByteArray();
  69. }
  70. imageObjectInfo.setData(imageData);
  71. return imageObjectInfo;
  72. }
  73. /** {@inheritDoc} */
  74. protected AFPDataObjectInfo createDataObjectInfo() {
  75. return new AFPImageObjectInfo();
  76. }
  77. }