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.

ImageHandlingTestCase.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.fop.apps.FOUserAgent;
  26. import org.apache.fop.render.intermediate.IFContext;
  27. import org.apache.xmlgraphics.ps.DSCConstants;
  28. import org.apache.xmlgraphics.ps.PSResource;
  29. import org.apache.xmlgraphics.ps.dsc.DSCException;
  30. import org.apache.xmlgraphics.ps.dsc.DSCParser;
  31. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage;
  32. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages;
  33. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentTitle;
  34. import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
  35. import org.junit.Test;
  36. /**
  37. * Tests the image handling in PostScript output.
  38. */
  39. public class ImageHandlingTestCase extends AbstractPostScriptTestCase {
  40. /**
  41. * Tests JPEG handling.
  42. * @throws Exception if an error occurs
  43. */
  44. @Test
  45. public void testJPEGImageLevel3() throws Exception {
  46. innerTestJPEGImage(3);
  47. }
  48. /**
  49. * Tests JPEG handling.
  50. * @throws Exception if an error occurs
  51. */
  52. @Test
  53. public void testJPEGImageLevel2() throws Exception {
  54. innerTestJPEGImage(2);
  55. }
  56. private void innerTestJPEGImage(int level) throws Exception {
  57. FOUserAgent ua = fopFactory.newFOUserAgent();
  58. PSDocumentHandler handler = new PSDocumentHandler();
  59. handler.setContext(new IFContext(ua));
  60. PSRenderingUtil psUtil = handler.getPSUtil();
  61. psUtil.setLanguageLevel(level);
  62. psUtil.setOptimizeResources(true);
  63. ua.setDocumentHandlerOverride(handler);
  64. // Prepare output file
  65. File outputFile = renderFile(ua, "ps-jpeg-image.fo", "-if-l" + psUtil.getLanguageLevel());
  66. verifyPostScriptFile(outputFile, psUtil.getLanguageLevel());
  67. }
  68. private void verifyPostScriptFile(File psFile, int level)
  69. throws IOException, DSCException {
  70. InputStream in = new java.io.FileInputStream(psFile);
  71. in = new java.io.BufferedInputStream(in);
  72. try {
  73. DSCParser parser = new DSCParser(in);
  74. DSCCommentPages pages = (DSCCommentPages)gotoDSCComment(parser, DSCConstants.PAGES);
  75. assertEquals(1, pages.getPageCount());
  76. //Skip procsets and encoding
  77. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  78. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  79. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  80. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  81. PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2");
  82. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2);
  83. DSCCommentTitle title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  84. assertEquals("image/jpeg test/resources/images/bgimg300dpi.jpg", title.getTitle());
  85. String resourceContent = getResourceContent(parser);
  86. if (level == 3) {
  87. assertContains(resourceContent, "/FOPForm:2");
  88. assertContains(resourceContent, "/DCTDecode filter");
  89. assertContains(resourceContent, "/ReusableStreamDecode filter");
  90. } else {
  91. assertContains(resourceContent, "/FOPForm:2");
  92. assertContains(resourceContent, "/DCTDecode filter");
  93. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  94. }
  95. //---=== Page 1 ===---
  96. DSCCommentPage page = (DSCCommentPage)gotoDSCComment(parser, DSCConstants.PAGE);
  97. assertEquals(1, page.getPagePosition());
  98. PSResource form1 = new PSResource(PSResource.TYPE_FORM, "FOPForm:1");
  99. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form1);
  100. title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  101. assertEquals("image/jpeg test/resources/images/bgimg72dpi.jpg", title.getTitle());
  102. resourceContent = getResourceContent(parser);
  103. if (level == 3) {
  104. assertContains(resourceContent, "/FOPForm:1");
  105. assertContains(resourceContent, "/DCTDecode filter");
  106. assertContains(resourceContent, "/ReusableStreamDecode filter");
  107. } else {
  108. assertContains(resourceContent, "/FOPForm:1");
  109. assertContains(resourceContent, "/DCTDecode filter");
  110. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  111. }
  112. } finally {
  113. IOUtils.closeQuietly(in);
  114. }
  115. }
  116. private void assertContains(String text, String searchString) {
  117. assertTrue("Text doesn't contain '" + searchString + "'", text.indexOf(searchString) >= 0);
  118. }
  119. private void assertAbsent(String text, String searchString) {
  120. assertTrue("Text contains '" + searchString + "'", text.indexOf(searchString) < 0);
  121. }
  122. private String getResourceContent(DSCParser parser) throws IOException, DSCException {
  123. StringBuffer sb = new StringBuffer();
  124. while (parser.hasNext()) {
  125. DSCEvent event = parser.nextEvent();
  126. if (event.isLine()) {
  127. sb.append(event.asLine().getLine()).append('\n');
  128. } else if (event.isDSCComment()) {
  129. if (DSCConstants.END_RESOURCE.equals(event.asDSCComment().getName())) {
  130. break;
  131. }
  132. }
  133. }
  134. return sb.toString();
  135. }
  136. }