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.

ImageRenderedAdapterTestCase.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.color.ICC_Profile;
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.IndexColorModel;
  23. import java.awt.image.RenderedImage;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertNotNull;
  26. import static org.mockito.ArgumentMatchers.nullable;
  27. import static org.mockito.Matchers.any;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.when;
  30. import org.apache.xmlgraphics.image.loader.ImageInfo;
  31. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  32. import org.apache.fop.pdf.PDFAMode;
  33. import org.apache.fop.pdf.PDFDictionary;
  34. import org.apache.fop.pdf.PDFDocument;
  35. import org.apache.fop.pdf.PDFFactory;
  36. import org.apache.fop.pdf.PDFICCBasedColorSpace;
  37. import org.apache.fop.pdf.PDFICCStream;
  38. import org.apache.fop.pdf.PDFImage;
  39. import org.apache.fop.pdf.PDFImageXObject;
  40. import org.apache.fop.pdf.PDFProfile;
  41. import org.apache.fop.pdf.PDFResourceContext;
  42. import org.apache.fop.pdf.PDFResources;
  43. public class ImageRenderedAdapterTestCase {
  44. /**
  45. * tests whether ARGB images return a soft mask
  46. */
  47. @Test
  48. public void testSetupWithARGBReturnsSoftMask() {
  49. RenderedImage ri = createRenderedImageWithRGBA();
  50. ImageRendered ir = mock(ImageRendered.class);
  51. when(ir.getRenderedImage()).thenReturn(ri);
  52. ImageInfo ii = mock(ImageInfo.class);
  53. when(ir.getInfo()).thenReturn(ii);
  54. ImageRenderedAdapter ira = new ImageRenderedAdapter(ir, "mock");
  55. PDFDocument doc = createPDFDocumentFromRenderedImage();
  56. PDFDictionary dict = new PDFDictionary();
  57. ira.setup(doc);
  58. ira.populateXObjectDictionary(dict);
  59. assertNotNull(ira.getSoftMaskReference());
  60. }
  61. /**
  62. * FOP-2847: tests whether images with index color model return a soft mask</p>
  63. */
  64. @Test
  65. public void testSetupWithIndexColorModelSemiTransparentReturnsSoftMask() {
  66. RenderedImage ri = createRenderedImageWithIndexColorModel(false);
  67. ImageRendered ir = mock(ImageRendered.class);
  68. when(ir.getRenderedImage()).thenReturn(ri);
  69. ImageInfo ii = mock(ImageInfo.class);
  70. when(ir.getInfo()).thenReturn(ii);
  71. ImageRenderedAdapter ira = new ImageRenderedAdapter(ir, "mock");
  72. PDFDocument doc = createPDFDocumentFromRenderedImage();
  73. PDFDictionary dict = new PDFDictionary();
  74. ira.setup(doc);
  75. ira.populateXObjectDictionary(dict);
  76. assertNotNull(ira.getSoftMaskReference());
  77. }
  78. /**
  79. * FOP-2847: tests whether images with index color model return a soft mask</p>
  80. */
  81. @Test
  82. public void testSetupWithIndexColorModelFullyTransparentReturnsSoftMask() {
  83. RenderedImage ri = createRenderedImageWithIndexColorModel(true);
  84. ImageRendered ir = mock(ImageRendered.class);
  85. when(ir.getRenderedImage()).thenReturn(ri);
  86. ImageInfo ii = mock(ImageInfo.class);
  87. when(ir.getInfo()).thenReturn(ii);
  88. ImageRenderedAdapter ira = new ImageRenderedAdapter(ir, "mock");
  89. PDFDocument doc = createPDFDocumentFromRenderedImage();
  90. PDFDictionary dict = new PDFDictionary();
  91. ira.setup(doc);
  92. ira.populateXObjectDictionary(dict);
  93. assertNotNull(ira.getSoftMaskReference());
  94. }
  95. /**
  96. * Creates a semi transparent 4x4 image in index color space.
  97. *
  98. * @param fullyTransparent true if image is supposed to have a fully
  99. * transparent color
  100. * @return RenderedImage
  101. */
  102. public static RenderedImage createRenderedImageWithIndexColorModel(boolean fullyTransparent) {
  103. /*
  104. * Define an index color model with just four colors. For reasons of
  105. * simplicity colors will be gray.
  106. */
  107. IndexColorModel cm;
  108. if (fullyTransparent) {
  109. byte[] i = {(byte)0x00, (byte)0x80, (byte)0xB0, (byte)0xF0};
  110. cm = new IndexColorModel(8, 4, i, i, i, i);
  111. } else {
  112. byte[] i = {(byte)0x10, (byte)0x80, (byte)0xB0, (byte)0xF0};
  113. cm = new IndexColorModel(8, 4, i, i, i, i);
  114. }
  115. // create a 4x4 image with just one uniform color
  116. BufferedImage ri = new BufferedImage(4, 4, BufferedImage.TYPE_BYTE_INDEXED, cm);
  117. for (int x = 0; x < 2; x++) {
  118. for (int y = 0; y < 2; y++) {
  119. Color c = new Color(128, 128, 128, 128);
  120. ri.setRGB(x, y, c.getRGB());
  121. }
  122. }
  123. return ri;
  124. }
  125. /**
  126. * creates a semi transparent 4x4 image in ABGR color space
  127. *
  128. * @return RenderedImage
  129. */
  130. static RenderedImage createRenderedImageWithRGBA() {
  131. // create a 4x4 image
  132. BufferedImage ri = new BufferedImage(4, 4, BufferedImage.TYPE_4BYTE_ABGR);
  133. for (int x = 0; x < 2; x++) {
  134. for (int y = 0; y < 2; y++) {
  135. Color c = new Color(128, 128, 128, 128);
  136. ri.setRGB(x, y, c.getRGB());
  137. }
  138. }
  139. return ri;
  140. }
  141. /**
  142. * Create a mocked PDF document from RenderedImage.
  143. *
  144. * @return
  145. */
  146. public static PDFDocument createPDFDocumentFromRenderedImage() {
  147. // mock PDFDocument
  148. PDFDocument doc = mock(PDFDocument.class);
  149. PDFResources resources = mock(PDFResources.class);
  150. when(doc.getResources()).thenReturn(resources);
  151. PDFProfile profile = mock(PDFProfile.class);
  152. when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_2A);
  153. PDFImageXObject pio = new PDFImageXObject(0, null);
  154. pio.setObjectNumber(0);
  155. when(doc.getProfile()).thenReturn(profile);
  156. when(doc.addImage(nullable(PDFResourceContext.class), any(PDFImage.class))).thenReturn(pio);
  157. // ICC Color info
  158. PDFFactory factory = mock(PDFFactory.class);
  159. PDFICCStream iccStream = mock(PDFICCStream.class);
  160. ICC_Profile iccProfile = mock(ICC_Profile.class);
  161. when(iccProfile.getNumComponents()).thenReturn(4);
  162. when(iccStream.getICCProfile()).thenReturn(iccProfile);
  163. when(factory.makePDFICCStream()).thenReturn(iccStream);
  164. PDFICCBasedColorSpace iccbcs = new PDFICCBasedColorSpace(null, iccStream);
  165. when(factory.makeICCBasedColorSpace(null, null, iccStream)).thenReturn(iccbcs);
  166. when(doc.getFactory()).thenReturn(factory);
  167. return doc;
  168. }
  169. }