Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/component/PageImageWriterTest.java

raw
1 /*
2 * Copyright 2016 by Eduard Weissmann ([email protected]).
3 *
4 * This file is part of the Sejda source code
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.sejda.impl.sambox.component;
20
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.*;
24 import static org.sejda.TestUtils.encryptedAtRest;
25 import static org.sejda.core.service.BaseTaskTest.customNonPdfInput;
26 import static org.sejda.core.service.BaseTaskTest.customNonPdfInputAsFileSource;
27
28 import java.io.IOException;
29
30 import org.apache.commons.io.IOUtils;
31 import org.junit.Test;
32 import org.sejda.model.exception.TaskIOException;
33 import org.sejda.sambox.pdmodel.graphics.color.PDDeviceRGB;
34 import org.sejda.sambox.pdmodel.graphics.image.PDImageXObject;
35 import org.sejda.sambox.pdmodel.graphics.image.UnsupportedImageFormatException;
36 import org.sejda.sambox.util.filetypedetector.FileType;
37
38 public class PageImageWriterTest {
39
40 @Test
41 public void testJpeg() throws TaskIOException, IOException {
42
43 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/large.jpg"));
44 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
45 assertThat(result.getHeight(), is(3840));
46 assertThat(result.getWidth(), is(5760));
47
48 assertTrue("Original bytes should be used", IOUtils.contentEquals(result.getCOSObject().getFilteredStream(),
49 customNonPdfInput("image/large.jpg").getSource()));
50 }
51
52 @Test
53 public void testHeif_unsupported() throws TaskIOException {
54 Exception ex = assertThrows(TaskIOException.class, () -> {
55 PageImageWriter.toPDXImageObject(customNonPdfInput("image/sample_heic.jpg"));
56 });
57
58 Throwable cause = ex.getCause();
59 assertEquals(cause.getClass(), UnsupportedImageFormatException.class);
60 UnsupportedImageFormatException uife = (UnsupportedImageFormatException) cause;
61
62 assertEquals(uife.getFileType(), FileType.HEIF);
63 assertEquals(uife.getFilename(), "sample_heic.jpg");
64 }
65
66 @Test
67 public void testPng() throws TaskIOException, IOException {
68
69 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/draft.png"));
70 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
71 assertThat(result.getHeight(), is(103));
72 assertThat(result.getWidth(), is(248));
73 }
74
75 @Test
76 public void testTiffWithAlphaToPDXImageObject() throws TaskIOException {
77 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/draft.tiff"));
78 assertThat(result.getHeight(), is(103));
79 }
80
81 @Test
82 public void test_CMYK_jpeg() throws TaskIOException, IOException {
83 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/cmyk.jpg"));
84 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
85 assertThat(result.getHeight(), is(560));
86 assertThat(result.getWidth(), is(1400));
87
88 assertFalse("Original bytes should not be used; the image should be converted from CMYK to RGB",
89 IOUtils.contentEquals(result.getCOSObject().getFilteredStream(),
90 customNonPdfInput("image/cmyk.jpg").getSource()));
91 }
92
93 @Test
94 public void test_CMYK_jpeg_wrong_extension() throws TaskIOException, IOException {
95 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/cmyk.jpg", "cmyk.png"));
96 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
97 assertThat(result.getHeight(), is(560));
98 assertThat(result.getWidth(), is(1400));
99
100 assertFalse("Original bytes should not be used; the image should be converted from CMYK to RGB",
101 IOUtils.contentEquals(result.getCOSObject().getFilteredStream(),
102 customNonPdfInput("image/cmyk.jpg").getSource()));
103 }
104
105 @Test
106 public void test_Gray_ICC_png() throws TaskIOException, IOException {
107 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/icc_profile_gray.png"));
108 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
109
110 assertFalse("Original bytes should not be used; the image should be converted from ICC Gray to RGB",
111 IOUtils.contentEquals(result.getCOSObject().getFilteredStream(),
112 customNonPdfInput("image/icc_profile_gray.png").getSource()));
113 }
114
115 @Test
116 public void encryptedAtRestTest_stream() throws TaskIOException, IOException {
117
118 PDImageXObject result = PageImageWriter.toPDXImageObject(encryptedAtRest(customNonPdfInput("image/large.jpg")));
119 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
120 assertThat(result.getHeight(), is(3840));
121
122 assertTrue("Decrypted bytes should be used", IOUtils.contentEquals(result.getCOSObject().getFilteredStream(),
123 customNonPdfInput("image/large.jpg").getSource()));
124 }
125
126 @Test
127 public void encryptedAtRestTest_file() throws TaskIOException, IOException {
128
129 PDImageXObject result = PageImageWriter
130 .toPDXImageObject(encryptedAtRest(customNonPdfInputAsFileSource("image/draft.png")));
131 assertThat(result.getColorSpace(), is(PDDeviceRGB.INSTANCE));
132 assertThat(result.getHeight(), is(103));
133 }
134
135 @Test
136 public void testExifRotated() throws TaskIOException {
137
138 PDImageXObject result = PageImageWriter.toPDXImageObject(customNonPdfInput("image/with_exif_orientation.JPG"));
139 assertThat(result.getHeight(), is(3264));
140 assertThat(result.getWidth(), is(2448));
141 }
142 }