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.

ImageLoaderTestCase.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.image.loader.batik;
  19. import java.awt.image.Raster;
  20. import java.awt.image.RenderedImage;
  21. import java.io.File;
  22. import org.junit.Ignore;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertFalse;
  26. import static org.junit.Assert.assertNotNull;
  27. import org.apache.xmlgraphics.image.loader.Image;
  28. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  29. import org.apache.xmlgraphics.image.loader.ImageInfo;
  30. import org.apache.xmlgraphics.image.loader.ImageManager;
  31. import org.apache.xmlgraphics.image.loader.XMLNamespaceEnabledImageFlavor;
  32. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  33. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  34. import org.apache.xmlgraphics.image.writer.ImageWriterUtil;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.apps.FopFactory;
  37. import org.apache.fop.apps.FopFactoryBuilder;
  38. /**
  39. * Tests for bundled ImageLoader implementations.
  40. */
  41. @Ignore("Batik fails big time")
  42. public class ImageLoaderTestCase {
  43. private static final File DEBUG_TARGET_DIR = null;
  44. private FopFactory fopFactory;
  45. public ImageLoaderTestCase() {
  46. FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
  47. builder.setSourceResolution(72);
  48. builder.setTargetResolution(300);
  49. fopFactory = builder.build();
  50. }
  51. @Test
  52. public void testSVG() throws Exception {
  53. String uri = "test/resources/images/img-w-size.svg";
  54. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  55. ImageManager manager = fopFactory.getImageManager();
  56. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  57. assertNotNull("ImageInfo must not be null", info);
  58. Image img = manager.getImage(info, XMLNamespaceEnabledImageFlavor.SVG_DOM,
  59. userAgent.getImageSessionContext());
  60. assertNotNull("Image must not be null", img);
  61. assertEquals(XMLNamespaceEnabledImageFlavor.SVG_DOM, img.getFlavor());
  62. ImageXMLDOM imgDom = (ImageXMLDOM)img;
  63. assertNotNull(imgDom.getDocument());
  64. assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace());
  65. info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image
  66. assertEquals(16000, info.getSize().getWidthMpt());
  67. assertEquals(16000, info.getSize().getHeightMpt());
  68. img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  69. userAgent.getImageSessionContext());
  70. assertNotNull("Image must not be null", img);
  71. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  72. ImageRendered imgRed = (ImageRendered)img;
  73. assertNotNull(imgRed.getRenderedImage());
  74. if (DEBUG_TARGET_DIR != null) {
  75. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  76. (int)userAgent.getTargetResolution(),
  77. new File(DEBUG_TARGET_DIR, "out.svg.png"));
  78. }
  79. assertEquals(67, imgRed.getRenderedImage().getWidth());
  80. assertEquals(67, imgRed.getRenderedImage().getHeight());
  81. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  82. assertEquals(16000, info.getSize().getWidthMpt());
  83. assertEquals(16000, info.getSize().getHeightMpt());
  84. }
  85. @Test
  86. public void testSVGNoViewbox() throws Exception {
  87. String uri = "test/resources/images/circles.svg";
  88. FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
  89. builder.setSourceResolution(96);
  90. builder.setTargetResolution(300);
  91. FopFactory ff = builder.build();
  92. FOUserAgent userAgent = ff.newFOUserAgent();
  93. ImageManager manager = ff.getImageManager();
  94. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  95. assertNotNull("ImageInfo must not be null", info);
  96. Image img = manager.getImage(info, XMLNamespaceEnabledImageFlavor.SVG_DOM,
  97. userAgent.getImageSessionContext());
  98. assertNotNull("Image must not be null", img);
  99. assertEquals(XMLNamespaceEnabledImageFlavor.SVG_DOM, img.getFlavor());
  100. ImageXMLDOM imgDom = (ImageXMLDOM)img;
  101. assertNotNull(imgDom.getDocument());
  102. assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace());
  103. info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image
  104. assertEquals(96, info.getSize().getDpiHorizontal(), 0);
  105. assertEquals(340158, info.getSize().getWidthMpt());
  106. assertEquals(340158, info.getSize().getHeightMpt());
  107. assertEquals(454, info.getSize().getWidthPx());
  108. assertEquals(454, info.getSize().getHeightPx());
  109. img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  110. userAgent.getImageSessionContext());
  111. assertNotNull("Image must not be null", img);
  112. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  113. ImageRendered imgRed = (ImageRendered)img;
  114. assertNotNull(imgRed.getRenderedImage());
  115. if (DEBUG_TARGET_DIR != null) {
  116. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  117. (int)userAgent.getTargetResolution(),
  118. new File(DEBUG_TARGET_DIR, "circles.svg.png"));
  119. }
  120. assertEquals(1418, imgRed.getRenderedImage().getWidth());
  121. assertEquals(1418, imgRed.getRenderedImage().getHeight());
  122. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  123. assertEquals(340158, info.getSize().getWidthMpt());
  124. assertEquals(340158, info.getSize().getHeightMpt());
  125. }
  126. @Test
  127. public void testWMF() throws Exception {
  128. String uri = "test/resources/images/testChart.wmf";
  129. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  130. ImageManager manager = fopFactory.getImageManager();
  131. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  132. assertNotNull("ImageInfo must not be null", info);
  133. Image img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  134. userAgent.getImageSessionContext());
  135. assertNotNull("Image must not be null", img);
  136. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  137. ImageRendered imgRed = (ImageRendered)img;
  138. assertNotNull(imgRed.getRenderedImage());
  139. if (DEBUG_TARGET_DIR != null) {
  140. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  141. (int)userAgent.getTargetResolution(),
  142. new File(DEBUG_TARGET_DIR, "out.wmf.png"));
  143. }
  144. assertEquals(3300, imgRed.getRenderedImage().getWidth());
  145. assertEquals(2550, imgRed.getRenderedImage().getHeight());
  146. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  147. assertEquals(792000, info.getSize().getWidthMpt());
  148. assertEquals(612000, info.getSize().getHeightMpt());
  149. }
  150. @Test
  151. public void testSVGWithReferences() throws Exception {
  152. String uri = "test/resources/fop/svg/images.svg";
  153. FopFactory ff = FopFactory.newInstance(new File(".").toURI());
  154. FOUserAgent userAgent = ff.newFOUserAgent();
  155. ImageManager manager = ff.getImageManager();
  156. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  157. assertNotNull("ImageInfo must not be null", info);
  158. Image img = manager.getImage(info, XMLNamespaceEnabledImageFlavor.SVG_DOM,
  159. userAgent.getImageSessionContext());
  160. assertNotNull("Image must not be null", img);
  161. assertEquals(XMLNamespaceEnabledImageFlavor.SVG_DOM, img.getFlavor());
  162. ImageXMLDOM imgDom = (ImageXMLDOM)img;
  163. assertNotNull(imgDom.getDocument());
  164. assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace());
  165. info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image
  166. assertEquals(400000, info.getSize().getWidthMpt());
  167. assertEquals(400000, info.getSize().getHeightMpt());
  168. assertEquals(400, info.getSize().getWidthPx());
  169. assertEquals(400, info.getSize().getHeightPx());
  170. img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  171. userAgent.getImageSessionContext());
  172. assertNotNull("Image must not be null", img);
  173. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  174. ImageRendered imgRed = (ImageRendered)img;
  175. RenderedImage renImg = imgRed.getRenderedImage();
  176. assertNotNull(renImg);
  177. if (DEBUG_TARGET_DIR != null) {
  178. ImageWriterUtil.saveAsPNG(renImg,
  179. (int)userAgent.getTargetResolution(),
  180. new File(DEBUG_TARGET_DIR, "images.svg.png"));
  181. }
  182. assertEquals(400, renImg.getWidth());
  183. assertEquals(400, renImg.getHeight());
  184. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  185. assertEquals(400000, info.getSize().getWidthMpt());
  186. assertEquals(400000, info.getSize().getHeightMpt());
  187. Raster raster = renImg.getData();
  188. // This pixel is white
  189. int[] pixel1 = raster.getPixel(1, 1, (int[])null);
  190. // This pixel is from the embedded JPG and is not white
  191. int[] pixel80 = raster.getPixel(80, 80, (int[]) null);
  192. assertEquals(pixel1.length, pixel80.length);
  193. boolean same = true;
  194. for (int i = 0; i < pixel1.length; i++) {
  195. same &= (pixel1[i] == pixel80[i]);
  196. }
  197. assertFalse("Embedding JPG into SVG failed", same);
  198. }
  199. }