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.

PDFImageXObjectTestCase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.pdf;
  19. import java.awt.image.IndexColorModel;
  20. import java.awt.image.RenderedImage;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.when;
  26. import org.apache.xmlgraphics.image.loader.ImageInfo;
  27. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  28. import org.apache.fop.render.pdf.ImageRenderedAdapter;
  29. import org.apache.fop.render.pdf.ImageRenderedAdapterTestCase;
  30. public class PDFImageXObjectTestCase {
  31. /**
  32. * FOP-2847: tests whether images with index color model returns a valid color key mask</p>
  33. */
  34. @Test
  35. public void testPDFImageXObjectHasCorrectMaskForSemiTransparentIndexColorModel() {
  36. RenderedImage ri = ImageRenderedAdapterTestCase.createRenderedImageWithIndexColorModel(false);
  37. ImageRendered ir = mock(ImageRendered.class);
  38. when(ir.getRenderedImage()).thenReturn(ri);
  39. ImageInfo ii = mock(ImageInfo.class);
  40. when(ir.getInfo()).thenReturn(ii);
  41. ImageRenderedAdapter ira = new ImageRenderedAdapter(ir, "mock");
  42. PDFDocument doc = ImageRenderedAdapterTestCase.createPDFDocumentFromRenderedImage();
  43. ira.setup(doc);
  44. AbstractPDFStream pdfImageXObject = new PDFImageXObject(0, ira);
  45. pdfImageXObject.populateStreamDict(null);
  46. /*
  47. * Currently FOP may generate a color key mask (/Mask) as well
  48. * as the more flexible (/SMask) both for a single transparent image.
  49. * That seems and actually is redundant, but it may help limited
  50. * PDF viewers to show at least the fully transparent parts (/Mask),
  51. * while omitting the translucent ones (/SMask).
  52. *
  53. * If it contains a /Mask, then make sure it has only length 2.
  54. * Length 2 actually means it holds the two bounds (min/max) as
  55. * single 8-bit values see section 8.9.6.4 color key masking
  56. * of PDF Spec 1.7.
  57. */
  58. assertTrue(ri.getColorModel() instanceof IndexColorModel);
  59. Object obj = pdfImageXObject.getDictionary().get("Mask");
  60. if (obj != null && obj instanceof PDFArray) {
  61. assertEquals(2, ((PDFArray) obj).length());
  62. }
  63. }
  64. /**
  65. * FOP-2847: tests whether images with index color model return a valid color key mask</p>
  66. */
  67. @Test
  68. public void testPDFImageXObjectHasCorrectMaskForFullyTransparentIndexColorModel() {
  69. RenderedImage ri = ImageRenderedAdapterTestCase.createRenderedImageWithIndexColorModel(true);
  70. ImageRendered ir = mock(ImageRendered.class);
  71. when(ir.getRenderedImage()).thenReturn(ri);
  72. ImageInfo ii = mock(ImageInfo.class);
  73. when(ir.getInfo()).thenReturn(ii);
  74. ImageRenderedAdapter ira = new ImageRenderedAdapter(ir, "mock");
  75. PDFDocument doc = ImageRenderedAdapterTestCase.createPDFDocumentFromRenderedImage();
  76. ira.setup(doc);
  77. AbstractPDFStream pdfImageXObject = new PDFImageXObject(0, ira);
  78. pdfImageXObject.populateStreamDict(null);
  79. assertTrue(ri.getColorModel() instanceof IndexColorModel);
  80. Object obj = pdfImageXObject.getDictionary().get("Mask");
  81. if (obj != null && obj instanceof PDFArray) {
  82. assertEquals(2, ((PDFArray) obj).length());
  83. }
  84. }
  85. }