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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)createDataObjectInfo();
  79. AFPPaintingState paintingState = afpContext.getPaintingState();
  80. // set resource information
  81. setResourceInformation(imageObjectInfo,
  82. image.getInfo().getOriginalURI(),
  83. afpContext.getForeignAttributes());
  84. setDefaultResourceLevel(imageObjectInfo, afpContext.getResourceManager());
  85. // Positioning
  86. imageObjectInfo.setObjectAreaInfo(createObjectAreaInfo(paintingState, pos));
  87. updateIntrinsicSize(imageObjectInfo, paintingState, image.getSize());
  88. // Image content
  89. ImageRawJPEG jpeg = (ImageRawJPEG)image;
  90. imageObjectInfo.setCompression(ImageContent.COMPID_JPEG);
  91. ColorSpace cs = jpeg.getColorSpace();
  92. switch (cs.getType()) {
  93. case ColorSpace.TYPE_GRAY:
  94. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
  95. imageObjectInfo.setColor(false);
  96. imageObjectInfo.setBitsPerPixel(8);
  97. break;
  98. case ColorSpace.TYPE_RGB:
  99. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
  100. imageObjectInfo.setColor(true);
  101. imageObjectInfo.setBitsPerPixel(24);
  102. break;
  103. case ColorSpace.TYPE_CMYK:
  104. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
  105. imageObjectInfo.setColor(true);
  106. imageObjectInfo.setBitsPerPixel(32);
  107. break;
  108. default:
  109. throw new IllegalStateException(
  110. "Color space of JPEG image not supported: " + cs);
  111. }
  112. boolean included = afpContext.getResourceManager().tryIncludeObject(imageObjectInfo);
  113. if (!included) {
  114. log.debug("Embedding undecoded JPEG as IOCA image...");
  115. InputStream inputStream = jpeg.createInputStream();
  116. try {
  117. imageObjectInfo.setData(IOUtils.toByteArray(inputStream));
  118. } finally {
  119. IOUtils.closeQuietly(inputStream);
  120. }
  121. // Create image
  122. afpContext.getResourceManager().createObject(imageObjectInfo);
  123. }
  124. }
  125. private void updateIntrinsicSize(AFPImageObjectInfo imageObjectInfo,
  126. AFPPaintingState paintingState, ImageSize targetSize) {
  127. //Update image object info
  128. imageObjectInfo.setDataHeightRes((int)Math.round(
  129. targetSize.getDpiHorizontal() * 10));
  130. imageObjectInfo.setDataWidthRes((int)Math.round(
  131. targetSize.getDpiVertical() * 10));
  132. imageObjectInfo.setDataHeight(targetSize.getHeightPx());
  133. imageObjectInfo.setDataWidth(targetSize.getWidthPx());
  134. // set object area info
  135. int resolution = paintingState.getResolution();
  136. AFPObjectAreaInfo objectAreaInfo = imageObjectInfo.getObjectAreaInfo();
  137. objectAreaInfo.setResolution(resolution);
  138. }
  139. /** {@inheritDoc} */
  140. public boolean isCompatible(RenderingContext targetContext, Image image) {
  141. if (!(targetContext instanceof AFPRenderingContext)) {
  142. return false; //AFP-specific image handler
  143. }
  144. AFPRenderingContext context = (AFPRenderingContext)targetContext;
  145. AFPPaintingState paintingState = context.getPaintingState();
  146. if (!paintingState.canEmbedJpeg()) {
  147. return false;
  148. }
  149. if (paintingState.getBitsPerPixel() < 8) {
  150. return false; //This would stand in the way of dithering and cause exceptions
  151. }
  152. if (image == null) {
  153. return true; //Don't know the image format, yet
  154. }
  155. if (image instanceof ImageRawJPEG) {
  156. ImageRawJPEG jpeg = (ImageRawJPEG)image;
  157. ColorSpace cs = jpeg.getColorSpace();
  158. switch (cs.getType()) {
  159. case ColorSpace.TYPE_GRAY:
  160. case ColorSpace.TYPE_RGB:
  161. //ok
  162. break;
  163. case ColorSpace.TYPE_CMYK:
  164. if (!paintingState.isCMYKImagesSupported()) {
  165. return false; //CMYK is disabled
  166. //Note: you may need to disable this image handler through configuration
  167. //if you want to paint a CMYK JPEG on 24bit and less configurations.
  168. }
  169. break;
  170. default:
  171. return false; //not supported
  172. }
  173. if (jpeg.getSOFType() != JPEGConstants.SOF0) {
  174. return false; //We'll let only baseline DCT through.
  175. }
  176. return true;
  177. }
  178. return false;
  179. }
  180. }