diff options
Diffstat (limited to 'test/java')
12 files changed, 579 insertions, 52 deletions
diff --git a/test/java/org/apache/fop/StandardTestSuite.java b/test/java/org/apache/fop/StandardTestSuite.java index caf75b4b4..61a9bdc5e 100644 --- a/test/java/org/apache/fop/StandardTestSuite.java +++ b/test/java/org/apache/fop/StandardTestSuite.java @@ -26,12 +26,14 @@ import org.apache.fop.fonts.DejaVuLGCSerifTest; import org.apache.fop.image.loader.batik.ImageLoaderTestCase; import org.apache.fop.image.loader.batik.ImagePreloaderTestCase; import org.apache.fop.intermediate.IFMimickingTestCase; +import org.apache.fop.render.afp.AFPTestSuite; import org.apache.fop.render.extensions.prepress.PageBoundariesTest; import org.apache.fop.render.extensions.prepress.PageScaleTest; import org.apache.fop.render.pdf.PDFAConformanceTestCase; import org.apache.fop.render.pdf.PDFCMapTestCase; import org.apache.fop.render.pdf.PDFEncodingTestCase; import org.apache.fop.render.pdf.PDFsRGBSettingsTestCase; +import org.apache.fop.render.ps.PSTestSuite; import org.apache.fop.render.rtf.RichTextFormatTestSuite; import org.apache.fop.traits.MinOptMaxTest; @@ -54,6 +56,8 @@ public class StandardTestSuite { suite.addTest(new TestSuite(PDFCMapTestCase.class)); suite.addTest(new TestSuite(PDFsRGBSettingsTestCase.class)); suite.addTest(new TestSuite(DejaVuLGCSerifTest.class)); + suite.addTest(AFPTestSuite.suite()); + suite.addTest(PSTestSuite.suite()); suite.addTest(RichTextFormatTestSuite.suite()); suite.addTest(new TestSuite(ImageLoaderTestCase.class)); suite.addTest(new TestSuite(ImagePreloaderTestCase.class)); diff --git a/test/java/org/apache/fop/UtilityCodeTestSuite.java b/test/java/org/apache/fop/UtilityCodeTestSuite.java index fed9f0f71..004b8f3c3 100644 --- a/test/java/org/apache/fop/UtilityCodeTestSuite.java +++ b/test/java/org/apache/fop/UtilityCodeTestSuite.java @@ -25,6 +25,7 @@ import junit.framework.TestSuite; import org.apache.fop.events.BasicEventTestCase; import org.apache.fop.pdf.PDFObjectTestCase; import org.apache.fop.traits.BorderPropsTestCase; +import org.apache.fop.util.BitmapImageUtilTestCase; import org.apache.fop.util.ColorUtilTestCase; import org.apache.fop.util.ElementListUtilsTestCase; import org.apache.fop.util.PDFNumberTestCase; @@ -51,6 +52,7 @@ public class UtilityCodeTestSuite { suite.addTest(new TestSuite(BasicEventTestCase.class)); suite.addTest(new TestSuite(XMLResourceBundleTestCase.class)); suite.addTest(new TestSuite(URIResolutionTestCase.class)); + suite.addTest(new TestSuite(BitmapImageUtilTestCase.class)); //$JUnit-END$ return suite; } diff --git a/test/java/org/apache/fop/render/AbstractRenderingTestCase.java b/test/java/org/apache/fop/render/AbstractRenderingTestCase.java new file mode 100644 index 000000000..daaa94e41 --- /dev/null +++ b/test/java/org/apache/fop/render/AbstractRenderingTestCase.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render; + +import java.io.File; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import java.util.MissingResourceException; + +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; + +/** + * Abstract base class for rendering (output) verification tests. + */ +public abstract class AbstractRenderingTestCase extends TestCase { + + private static final Map<String, String> MIME_MAP = new java.util.HashMap<String, String>(); + + static { + MIME_MAP.put(MimeConstants.MIME_PDF, ".pdf"); + MIME_MAP.put(MimeConstants.MIME_POSTSCRIPT, ".ps"); + MIME_MAP.put(MimeConstants.MIME_AFP, ".afp"); + } + + /** the JAXP TransformerFactory */ + protected TransformerFactory tFactory = TransformerFactory.newInstance(); + /** the FopFactory */ + protected FopFactory fopFactory = FopFactory.newInstance(); + + /** + * Renders a test file. + * @param ua the user agent (with override set!) + * @param resourceName the resource name for the FO file + * @param suffix a suffix for the output filename + * @param outputFormat MIME type of the requested output format + * @return the output file + * @throws Exception if an error occurs + */ + protected File renderFile(FOUserAgent ua, String resourceName, String suffix, + String outputFormat) throws Exception { + String extension = MIME_MAP.get(outputFormat); + assert extension != null; + File outputFile = new File("build/test-results/" + resourceName + suffix + extension); + File outputDir = outputFile.getParentFile(); + FileUtils.forceMkdir(outputDir); + + // Prepare input file + InputStream in = getClass().getResourceAsStream(resourceName); + if (in == null) { + throw new MissingResourceException(resourceName + " not found in resources", + getClass().getName(), null); + } + try { + Source src = new StreamSource(in); + + // Create output file + OutputStream out = new java.io.FileOutputStream(outputFile); + out = new java.io.BufferedOutputStream(out); + try { + Fop fop = fopFactory.newFop(outputFormat, ua, out); + SAXResult res = new SAXResult(fop.getDefaultHandler()); + + Transformer transformer = tFactory.newTransformer(); + transformer.transform(src, res); + } finally { + IOUtils.closeQuietly(out); + } + } finally { + IOUtils.closeQuietly(in); + } + return outputFile; + } + +} diff --git a/test/java/org/apache/fop/render/afp/AFPTestSuite.java b/test/java/org/apache/fop/render/afp/AFPTestSuite.java new file mode 100644 index 000000000..3f12746bf --- /dev/null +++ b/test/java/org/apache/fop/render/afp/AFPTestSuite.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.afp; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite for FOP's AFP output. + */ +public class AFPTestSuite { + + /** + * Builds the test suite + * @return the test suite + */ + public static Test suite() { + TestSuite suite = new TestSuite( + "Test suite for AFP output"); + //$JUnit-BEGIN$ + suite.addTest(new TestSuite(NoOperationTestCase.class)); + //$JUnit-END$ + return suite; + } +} diff --git a/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java b/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java new file mode 100644 index 000000000..7f081a4d0 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.afp; + +import java.io.File; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.MimeConstants; +import org.apache.fop.render.AbstractRenderingTestCase; + +/** + * Abstract base class for AFP verification tests. + */ +abstract class AbstractAFPTestCase extends AbstractRenderingTestCase { + + /** + * Renders a test file. + * @param ua the user agent (with override set!) + * @param resourceName the resource name for the FO file + * @param suffix a suffix for the output filename + * @return the output file + * @throws Exception if an error occurs + */ + protected File renderFile(FOUserAgent ua, String resourceName, String suffix) + throws Exception { + return renderFile(ua, resourceName, suffix, MimeConstants.MIME_AFP); + } + + +} diff --git a/test/java/org/apache/fop/render/afp/NoOperationTestCase.java b/test/java/org/apache/fop/render/afp/NoOperationTestCase.java new file mode 100644 index 000000000..56e4551d1 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/NoOperationTestCase.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.afp; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import junit.framework.Assert; + +import org.apache.commons.io.IOUtils; + +import org.apache.fop.afp.AFPConstants; +import org.apache.fop.afp.parser.MODCAParser; +import org.apache.fop.afp.parser.UnparsedStructuredField; +import org.apache.fop.apps.FOUserAgent; + +/** + * Tests generation of afp:no-operation (NOPs). + */ +public class NoOperationTestCase extends AbstractAFPTestCase { + + /** + * Tests afp:no-operation. + * @throws Exception if an error occurs + */ + public void testNoOperation() throws Exception { + FOUserAgent ua = fopFactory.newFOUserAgent(); + File outputFile = renderFile(ua, "nops.fo", ""); + + InputStream in = new java.io.FileInputStream(outputFile); + try { + MODCAParser parser = new MODCAParser(in); + UnparsedStructuredField field = skipTo(parser, 0xD3A8A8); //Begin Document + + //NOP in fo:declarations + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:declarations", getNopText(field)); + + field = parser.readNextStructuredField(); + assertEquals(0xD3A8AD, field.getSfTypeID()); //Begin Named Page Group + + //NOPs in fo:page-sequence + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:page-sequence: start", getNopText(field)); + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:page-sequence: end", getNopText(field)); + + field = parser.readNextStructuredField(); + assertEquals(0xD3A8AF, field.getSfTypeID()); //Begin Page + + field = skipTo(parser, 0xD3A9C9); //End Active Environment Group + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:simple-page-master: first", getNopText(field)); + + field = skipTo(parser, 0xD3A9C9); //End Active Environment Group + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:simple-page-master: rest", getNopText(field)); + } finally { + IOUtils.closeQuietly(in); + } + + int counter = 0; + in = new java.io.FileInputStream(outputFile); + try { + MODCAParser parser = new MODCAParser(in); + while (true) { + UnparsedStructuredField field = parser.readNextStructuredField(); + if (field == null) { + break; + } + if (field.getSfTypeID() == 0xD3EEEE) { + counter++; + } + } + } finally { + IOUtils.closeQuietly(in); + } + assertEquals(6, counter); //decl, 2 * ps, 3 * page/spm + } + + private String getNopText(UnparsedStructuredField field) throws UnsupportedEncodingException { + byte[] data = field.getData(); + String text = new String(data, AFPConstants.EBCIDIC_ENCODING); + return text; + } + + private UnparsedStructuredField skipTo(MODCAParser parser, int typeID) throws IOException { + UnparsedStructuredField field = null; + do { + field = parser.readNextStructuredField(); + if (field.getSfTypeID() == typeID) { + return field; + } + } while (field != null); + Assert.fail("Structured field not found: " + Integer.toHexString(typeID)); + return null; + } + +} diff --git a/test/java/org/apache/fop/render/afp/nops.fo b/test/java/org/apache/fop/render/afp/nops.fo new file mode 100644 index 000000000..96c6e0d24 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/nops.fo @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" + xmlns:afp="http://xmlgraphics.apache.org/fop/extensions/afp"> + <fo:layout-master-set> + <fo:simple-page-master master-name="first" page-height="10.5cm" page-width="14.85cm" margin="2cm"> + <afp:no-operation name="spm">fo:simple-page-master: first</afp:no-operation> + <fo:region-body margin-top="2em "/> + <fo:region-before extent="2em"/> + </fo:simple-page-master> + <fo:simple-page-master master-name="rest" page-height="10.5cm" page-width="14.85cm" margin="2cm"> + <afp:no-operation name="spm">fo:simple-page-master: rest</afp:no-operation> + <fo:region-body margin-top="2em "/> + <fo:region-before extent="2em"/> + </fo:simple-page-master> + <fo:page-sequence-master master-name="main"> + <fo:repeatable-page-master-alternatives> + <fo:conditional-page-master-reference master-reference="first" page-position="first"/> + <fo:conditional-page-master-reference master-reference="rest"/> + </fo:repeatable-page-master-alternatives> + </fo:page-sequence-master> + </fo:layout-master-set> + + <fo:declarations> + <afp:no-operation name="declarations">fo:declarations</afp:no-operation> + </fo:declarations> + + <fo:page-sequence master-reference="main" id="doc1"> + <afp:no-operation name="ps">fo:page-sequence: start</afp:no-operation> + <fo:flow flow-name="xsl-region-body"> + <fo:block>Page 1</fo:block> + <fo:block>Page 1</fo:block> + <fo:block break-after="page"></fo:block> + <fo:block>Page 2</fo:block> + <fo:block>Page 2</fo:block> + <fo:block break-after="page"></fo:block> + <fo:block>Page 3</fo:block> + <fo:block>Page 3</fo:block> + </fo:flow> + <afp:no-operation name="ps">fo:page-sequence: end</afp:no-operation> + </fo:page-sequence> +</fo:root> diff --git a/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java b/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java index 522d193ed..30e499846 100644 --- a/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java +++ b/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java @@ -21,20 +21,6 @@ package org.apache.fop.render.ps; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.MissingResourceException; - -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.stream.StreamSource; - -import junit.framework.TestCase; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.xmlgraphics.ps.PSResource; import org.apache.xmlgraphics.ps.dsc.DSCException; @@ -44,19 +30,13 @@ import org.apache.xmlgraphics.ps.dsc.events.DSCComment; import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; import org.apache.fop.apps.FOUserAgent; -import org.apache.fop.apps.Fop; -import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; +import org.apache.fop.render.AbstractRenderingTestCase; /** * Abstract base class for PostScript verification tests. */ -public abstract class AbstractPostScriptTestCase extends TestCase { - - /** the JAXP TransformerFactory */ - protected TransformerFactory tFactory = TransformerFactory.newInstance(); - /** the FopFactory */ - protected FopFactory fopFactory = FopFactory.newInstance(); +public abstract class AbstractPostScriptTestCase extends AbstractRenderingTestCase { /** * Renders a test file. @@ -68,35 +48,7 @@ public abstract class AbstractPostScriptTestCase extends TestCase { */ protected File renderFile(FOUserAgent ua, String resourceName, String suffix) throws Exception { - File outputFile = new File("build/test-results/" + resourceName + suffix + ".ps"); - File outputDir = outputFile.getParentFile(); - FileUtils.forceMkdir(outputDir); - - // Prepare input file - InputStream in = getClass().getResourceAsStream(resourceName); - if (in == null) { - throw new MissingResourceException(resourceName + " not found in resources", - getClass().getName(), null); - } - try { - Source src = new StreamSource(in); - - // Create PostScript - OutputStream out = new java.io.FileOutputStream(outputFile); - out = new java.io.BufferedOutputStream(out); - try { - Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, ua, out); - SAXResult res = new SAXResult(fop.getDefaultHandler()); - - Transformer transformer = tFactory.newTransformer(); - transformer.transform(src, res); - } finally { - IOUtils.closeQuietly(out); - } - } finally { - IOUtils.closeQuietly(in); - } - return outputFile; + return renderFile(ua, resourceName, suffix, MimeConstants.MIME_POSTSCRIPT); } /** diff --git a/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java b/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java index 72c677a0c..c127d82a5 100644 --- a/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java +++ b/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java @@ -86,6 +86,7 @@ public class ImageHandlingTestCase extends AbstractPostScriptTestCase { gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); + gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2"); checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2); diff --git a/test/java/org/apache/fop/render/ps/PSTestSuite.java b/test/java/org/apache/fop/render/ps/PSTestSuite.java new file mode 100644 index 000000000..05ba2dac7 --- /dev/null +++ b/test/java/org/apache/fop/render/ps/PSTestSuite.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.ps; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite for FOP's PostScript output. + */ +public class PSTestSuite { + + /** + * Builds the test suite + * @return the test suite + */ + public static Test suite() { + TestSuite suite = new TestSuite( + "Test suite for PostScript output"); + //$JUnit-BEGIN$ + suite.addTest(new TestSuite(ImageHandlingTestCase.class)); + suite.addTest(new TestSuite(ResourceOptimizationTestCase.class)); + //$JUnit-END$ + return suite; + } +} diff --git a/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java b/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java index 862ad5205..327c86548 100644 --- a/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java +++ b/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java @@ -90,7 +90,7 @@ public class ResourceOptimizationTestCase extends AbstractPostScriptTestCase { = (DSCCommentDocumentSuppliedResources)gotoDSCComment(parser, DSCConstants.DOCUMENT_SUPPLIED_RESOURCES); Set resources = supplied.getResources(); - assertEquals(4, resources.size()); + assertEquals(5, resources.size()); assertTrue(resources.contains(form1)); assertTrue("Expected barcode.eps as supplied resource", resources.contains(new PSResource(PSResource.TYPE_FILE, diff --git a/test/java/org/apache/fop/util/BitmapImageUtilTestCase.java b/test/java/org/apache/fop/util/BitmapImageUtilTestCase.java new file mode 100644 index 000000000..55a9fe4e4 --- /dev/null +++ b/test/java/org/apache/fop/util/BitmapImageUtilTestCase.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.util; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.awt.image.RenderedImage; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +import junit.framework.TestCase; + +import org.apache.commons.io.IOUtils; + +import org.apache.xmlgraphics.image.writer.ImageWriterUtil; +import org.apache.xmlgraphics.util.WriterOutputStream; +import org.apache.xmlgraphics.util.io.ASCIIHexOutputStream; + +import org.apache.fop.util.bitmap.BitmapImageUtil; +import org.apache.fop.util.bitmap.MonochromeBitmapConverter; + +/** + * Tests {@link BitmapImageUtil}. + */ +public class BitmapImageUtilTestCase extends TestCase { + + private static final boolean DEBUG = true; + private static final boolean TEST_PIXELS = false; + + /** + * Tests the convertTo* methods. + * @throws Exception if an error occurs + */ + public void testConvertToMono() throws Exception { + BufferedImage testImage = createTestImage(); + saveAsPNG(testImage, "test-image"); + + RenderedImage img; + Dimension scaled = new Dimension(320, 240); + + img = BitmapImageUtil.convertToGrayscale(testImage, null); + saveAsPNG(img, "out-gray"); + assertEquals(1, img.getColorModel().getNumComponents()); + assertEquals(8, img.getColorModel().getPixelSize()); + assertEquals(640, img.getWidth()); + assertEquals(480, img.getHeight()); + assertPixels("5757575757575757575757FFFFFFFFFF", img, 220, 34, 16); + + img = BitmapImageUtil.convertToGrayscale(testImage, scaled); + saveAsPNG(img, "out-gray-scaled"); + assertEquals(1, img.getColorModel().getNumComponents()); + assertEquals(8, img.getColorModel().getPixelSize()); + assertEquals(320, img.getWidth()); + assertEquals(240, img.getHeight()); + + img = BitmapImageUtil.convertToMonochrome(testImage, null); + saveAsPNG(img, "out-mono"); + assertEquals(1, img.getColorModel().getPixelSize()); + assertEquals(640, img.getWidth()); + assertEquals(480, img.getHeight()); + assertPixels("00000000000000000000000101010101", img, 220, 34, 16); + + if (isJAIAvailable()) { + img = BitmapImageUtil.convertToMonochrome(testImage, null, 0.5f); + saveAsPNG(img, "out-mono-jai-0.5"); + assertEquals(1, img.getColorModel().getPixelSize()); + assertEquals(640, img.getWidth()); + assertEquals(480, img.getHeight()); + assertPixels("00010000000100000001000101010101", img, 220, 34, 16); + + img = BitmapImageUtil.convertToMonochrome(testImage, null, 1.0f); + saveAsPNG(img, "out-mono-jai-1.0"); + assertEquals(1, img.getColorModel().getPixelSize()); + assertEquals(640, img.getWidth()); + assertEquals(480, img.getHeight()); + assertPixels("01000001000001000001000101010101", img, 220, 34, 16); + } + } + + private void assertPixels(String expected, RenderedImage img, int x, int y, int w) + throws IOException { + if (TEST_PIXELS) { + byte[] byteArray = (byte[])img.getData().getDataElements(x, y, w, 1, new byte[w]); + assertEquals(expected, toHex(byteArray)); + } + } + + private boolean isJAIAvailable() { + MonochromeBitmapConverter converter + = BitmapImageUtil.createDefaultMonochromeBitmapConverter(); + return converter.getClass().getName().contains("JAI"); + } + + private void saveAsPNG(RenderedImage img, String name) throws IOException { + if (DEBUG) { + File baseDir = new File("./build/test-results/bitmap-conversion"); + baseDir.mkdirs(); + ImageWriterUtil.saveAsPNG(img, new File(baseDir, name + ".png")); + } + } + + private String toHex(byte[] byteArray) throws IOException { + InputStream in = new java.io.ByteArrayInputStream(byteArray); + StringWriter writer = new StringWriter(); + WriterOutputStream wo = new WriterOutputStream(writer, "US-ASCII"); + ASCIIHexOutputStream hex = new ASCIIHexOutputStream(wo); + IOUtils.copyLarge(in, hex); + return writer.toString(); + } + + private BufferedImage createTestImage() { + BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = buf.createGraphics(); + g2d.setBackground(Color.WHITE); + g2d.clearRect(0, 0, buf.getWidth(), buf.getHeight()); + + //A few rectangles rotated and with different color + Graphics2D copy = (Graphics2D)g2d.create(); + copy.translate(170, 170); + int c = 12; + for (int i = 0; i < c; i++) { + float f = ((i + 1) / (float)c); + Color col = new Color(0.0f, 1 - f, 0.0f); + copy.setColor(col); + copy.fillRect(0, 0, 120, 120); + copy.rotate(-2 * Math.PI / c); + } + copy.dispose(); + + //the same in gray scales + copy = (Graphics2D)g2d.create(); + copy.translate(470, 310); + c = 12; + for (int i = 0; i < c; i++) { + float f = ((i + 1) / (float)c); + Color col = new Color(f, f, f); + copy.setColor(col); + copy.fillRect(0, 0, 120, 120); + copy.rotate(-2 * Math.PI / c); + } + copy.dispose(); + return buf; + } + +} |