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.

AFPImageHandlerRawJPEG.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Rectangle;
  20. import java.awt.color.ColorSpace;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import org.apache.commons.io.IOUtils;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.image.loader.Image;
  27. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  28. import org.apache.xmlgraphics.image.loader.ImageSize;
  29. import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG;
  30. import org.apache.xmlgraphics.image.loader.impl.JPEGConstants;
  31. import org.apache.xmlgraphics.util.MimeConstants;
  32. import org.apache.fop.afp.AFPDataObjectInfo;
  33. import org.apache.fop.afp.AFPImageObjectInfo;
  34. import org.apache.fop.afp.AFPObjectAreaInfo;
  35. import org.apache.fop.afp.AFPPaintingState;
  36. import org.apache.fop.afp.AFPResourceInfo;
  37. import org.apache.fop.afp.AFPResourceManager;
  38. import org.apache.fop.afp.ioca.ImageContent;
  39. import org.apache.fop.afp.modca.ResourceObject;
  40. import org.apache.fop.render.ImageHandler;
  41. import org.apache.fop.render.RenderingContext;
  42. /**
  43. * {@link ImageHandler} implementation which handles ImageRawJPEG instances. JPEG data is
  44. * embedded directly (not decoded) into IOCA images (FS11 or FS45).
  45. */
  46. public class AFPImageHandlerRawJPEG extends AFPImageHandler implements ImageHandler {
  47. /** logging instance */
  48. private final Log log = LogFactory.getLog(AFPImageHandlerRawJPEG.class);
  49. private void setDefaultResourceLevel(AFPImageObjectInfo imageObjectInfo,
  50. AFPResourceManager resourceManager) {
  51. AFPResourceInfo resourceInfo = imageObjectInfo.getResourceInfo();
  52. if (!resourceInfo.levelChanged()) {
  53. resourceInfo.setLevel(resourceManager.getResourceLevelDefaults()
  54. .getDefaultResourceLevel(ResourceObject.TYPE_IMAGE));
  55. }
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. protected AFPDataObjectInfo createDataObjectInfo() {
  60. return new AFPImageObjectInfo();
  61. }
  62. /** {@inheritDoc} */
  63. public int getPriority() {
  64. return 150;
  65. }
  66. /** {@inheritDoc} */
  67. public Class<?> getSupportedImageClass() {
  68. return ImageRawJPEG.class;
  69. }
  70. /** {@inheritDoc} */
  71. public ImageFlavor[] getSupportedImageFlavors() {
  72. return new ImageFlavor[] {ImageFlavor.RAW_JPEG};
  73. }
  74. /** {@inheritDoc} */
  75. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  76. throws IOException {
  77. AFPRenderingContext afpContext = (AFPRenderingContext)context;
  78. AFPDataObjectInfo info = createDataObjectInfo();
  79. assert (info instanceof AFPImageObjectInfo);
  80. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo) info;
  81. AFPPaintingState paintingState = afpContext.getPaintingState();
  82. // set resource information
  83. imageObjectInfo.setResourceInfo(createResourceInformation(
  84. image.getInfo().getOriginalURI(),
  85. afpContext.getForeignAttributes()));
  86. setDefaultResourceLevel(imageObjectInfo, afpContext.getResourceManager());
  87. // Positioning
  88. imageObjectInfo.setObjectAreaInfo(createObjectAreaInfo(paintingState, pos));
  89. updateIntrinsicSize(imageObjectInfo, paintingState, image.getSize());
  90. // Image content
  91. ImageRawJPEG jpeg = (ImageRawJPEG)image;
  92. imageObjectInfo.setCompression(ImageContent.COMPID_JPEG);
  93. ColorSpace cs = jpeg.getColorSpace();
  94. switch (cs.getType()) {
  95. case ColorSpace.TYPE_GRAY:
  96. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
  97. imageObjectInfo.setColor(false);
  98. imageObjectInfo.setBitsPerPixel(8);
  99. break;
  100. case ColorSpace.TYPE_RGB:
  101. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
  102. imageObjectInfo.setColor(true);
  103. imageObjectInfo.setBitsPerPixel(24);
  104. break;
  105. case ColorSpace.TYPE_CMYK:
  106. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
  107. imageObjectInfo.setColor(true);
  108. imageObjectInfo.setBitsPerPixel(32);
  109. break;
  110. default:
  111. throw new IllegalStateException(
  112. "Color space of JPEG image not supported: " + cs);
  113. }
  114. boolean included = afpContext.getResourceManager().tryIncludeObject(imageObjectInfo);
  115. if (!included) {
  116. log.debug("Embedding undecoded JPEG as IOCA image...");
  117. InputStream inputStream = jpeg.createInputStream();
  118. try {
  119. imageObjectInfo.setData(IOUtils.toByteArray(inputStream));
  120. } finally {
  121. IOUtils.closeQuietly(inputStream);
  122. }
  123. // Create image
  124. afpContext.getResourceManager().createObject(imageObjectInfo);
  125. }
  126. }
  127. private void updateIntrinsicSize(AFPImageObjectInfo imageObjectInfo,
  128. AFPPaintingState paintingState, ImageSize targetSize) {
  129. //Update image object info
  130. imageObjectInfo.setDataHeightRes((int)Math.round(
  131. targetSize.getDpiHorizontal() * 10));
  132. imageObjectInfo.setDataWidthRes((int)Math.round(
  133. targetSize.getDpiVertical() * 10));
  134. imageObjectInfo.setDataHeight(targetSize.getHeightPx());
  135. imageObjectInfo.setDataWidth(targetSize.getWidthPx());
  136. // set object area info
  137. int resolution = paintingState.getResolution();
  138. AFPObjectAreaInfo objectAreaInfo = imageObjectInfo.getObjectAreaInfo();
  139. objectAreaInfo.setResolution(resolution);
  140. }
  141. /** {@inheritDoc} */
  142. public boolean isCompatible(RenderingContext targetContext, Image image) {
  143. if (!(targetContext instanceof AFPRenderingContext)) {
  144. return false; //AFP-specific image handler
  145. }
  146. AFPRenderingContext context = (AFPRenderingContext)targetContext;
  147. AFPPaintingState paintingState = context.getPaintingState();
  148. if (!paintingState.canEmbedJpeg()) {
  149. return false;
  150. }
  151. if (paintingState.getBitsPerPixel() < 8) {
  152. return false; //This would stand in the way of dithering and cause exceptions
  153. }
  154. if (image == null) {
  155. return true; //Don't know the image format, yet
  156. }
  157. if (image instanceof ImageRawJPEG) {
  158. ImageRawJPEG jpeg = (ImageRawJPEG)image;
  159. ColorSpace cs = jpeg.getColorSpace();
  160. switch (cs.getType()) {
  161. case ColorSpace.TYPE_GRAY:
  162. case ColorSpace.TYPE_RGB:
  163. //ok
  164. break;
  165. case ColorSpace.TYPE_CMYK:
  166. if (!paintingState.isCMYKImagesSupported()) {
  167. return false; //CMYK is disabled
  168. //Note: you may need to disable this image handler through configuration
  169. //if you want to paint a CMYK JPEG on 24bit and less configurations.
  170. }
  171. break;
  172. default:
  173. return false; //not supported
  174. }
  175. if (jpeg.getSOFType() != JPEGConstants.SOF0) {
  176. return false; //We'll let only baseline DCT through.
  177. }
  178. return true;
  179. }
  180. return false;
  181. }
  182. }