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.

ImageRawPNGAdapterTestCase.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.image.ComponentColorModel;
  20. import java.awt.image.IndexColorModel;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.util.zip.Deflater;
  25. import java.util.zip.DeflaterOutputStream;
  26. import org.junit.Test;
  27. import org.apache.xmlgraphics.image.loader.ImageSize;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
  29. import org.apache.fop.pdf.FlateFilter;
  30. import org.apache.fop.pdf.PDFAMode;
  31. import org.apache.fop.pdf.PDFDocument;
  32. import org.apache.fop.pdf.PDFProfile;
  33. import org.apache.fop.render.RawPNGTestUtil;
  34. import static org.junit.Assert.assertArrayEquals;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. public class ImageRawPNGAdapterTestCase {
  39. @Test
  40. public void testSetupWithIndexColorModel() {
  41. IndexColorModel cm = mock(IndexColorModel.class);
  42. ImageRawPNG irpng = mock(ImageRawPNG.class);
  43. PDFDocument doc = mock(PDFDocument.class);
  44. PDFProfile profile = mock(PDFProfile.class);
  45. ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
  46. ImageSize is = RawPNGTestUtil.getImageSize();
  47. when(irpng.getColorModel()).thenReturn(cm);
  48. // when(cm.hasAlpha()).thenReturn(false);
  49. when(doc.getProfile()).thenReturn(profile);
  50. when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
  51. when(irpng.getSize()).thenReturn(is);
  52. irpnga.setup(doc);
  53. FlateFilter filter = (FlateFilter) irpnga.getPDFFilter();
  54. assertEquals(1, filter.getColors());
  55. }
  56. @Test
  57. public void testSetupWithComponentColorModel() throws IOException {
  58. ComponentColorModel cm = mock(ComponentColorModel.class);
  59. ImageRawPNG irpng = mock(ImageRawPNG.class);
  60. PDFDocument doc = mock(PDFDocument.class);
  61. PDFProfile profile = mock(PDFProfile.class);
  62. ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
  63. ImageSize is = RawPNGTestUtil.getImageSize();
  64. when(irpng.getColorModel()).thenReturn(cm);
  65. when(cm.getNumComponents()).thenReturn(3);
  66. // when(cm.hasAlpha()).thenReturn(false);
  67. when(doc.getProfile()).thenReturn(profile);
  68. when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
  69. when(irpng.getSize()).thenReturn(is);
  70. irpnga.setup(doc);
  71. FlateFilter filter = (FlateFilter) irpnga.getPDFFilter();
  72. assertEquals(3, filter.getColors());
  73. }
  74. @Test
  75. public void testOutputContentsWithRGBPNG() throws IOException {
  76. testOutputContentsWithGRGBAPNG(-1, 128, 128, 128, -1);
  77. }
  78. @Test
  79. public void testOutputContentsWithRGBAPNG() throws IOException {
  80. testOutputContentsWithGRGBAPNG(-1, 128, 128, 128, 128);
  81. }
  82. @Test
  83. public void testOutputContentsWithGPNG() throws IOException {
  84. testOutputContentsWithGRGBAPNG(128, -1, -1, -1, -1);
  85. }
  86. @Test
  87. public void testOutputContentsWithGAPNG() throws IOException {
  88. testOutputContentsWithGRGBAPNG(128, -1, -1, -1, 128);
  89. }
  90. private void testOutputContentsWithGRGBAPNG(int gray, int red, int green, int blue, int alpha)
  91. throws IOException {
  92. int numColorComponents = gray > -1 ? 1 : 3;
  93. int numComponents = numColorComponents + (alpha > -1 ? 1 : 0);
  94. ComponentColorModel cm = mock(ComponentColorModel.class);
  95. ImageRawPNG irpng = mock(ImageRawPNG.class);
  96. PDFDocument doc = mock(PDFDocument.class);
  97. PDFProfile profile = mock(PDFProfile.class);
  98. ImageRawPNGAdapter irpnga = new ImageRawPNGAdapter(irpng, "mock");
  99. ImageSize is = RawPNGTestUtil.getImageSize();
  100. when(irpng.getColorModel()).thenReturn(cm);
  101. when(cm.getNumComponents()).thenReturn(numComponents);
  102. // when(cm.hasAlpha()).thenReturn(false);
  103. when(doc.getProfile()).thenReturn(profile);
  104. when(profile.getPDFAMode()).thenReturn(PDFAMode.PDFA_1A);
  105. when(irpng.getSize()).thenReturn(is);
  106. irpnga.setup(doc);
  107. FlateFilter filter = (FlateFilter) irpnga.getPDFFilter();
  108. assertEquals(numColorComponents, filter.getColors());
  109. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  110. byte[] data = RawPNGTestUtil.buildGRGBAData(gray, red, green, blue, alpha);
  111. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  112. when(irpng.createInputStream()).thenReturn(bais);
  113. irpnga.outputContents(baos);
  114. if (alpha > -1) {
  115. byte[] expected = RawPNGTestUtil.buildGRGBAData(gray, red, green, blue, -1);
  116. assertArrayEquals(expected, baos.toByteArray());
  117. } else {
  118. assertArrayEquals(data, baos.toByteArray());
  119. }
  120. }
  121. }