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.

PSImageHandlerRawJPEG.java 4.2KB

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