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 9.8KB

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