aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/apache/fop/render/ps/ImageEncoderPNGTestCase.java
blob: 53654d76af2125ae399c07ad05acc79e4b5fc8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * 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 java.awt.image.ComponentColorModel;
import java.awt.image.IndexColorModel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.xmlgraphics.image.loader.ImageSize;
import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;

import org.apache.fop.render.RawPNGTestUtil;

public class ImageEncoderPNGTestCase {

    @Test
    public void testWriteToWithRGBPNG() throws IOException {
        testWriteToWithGRGBAPNG(-1, 128, 128, 128, -1);
    }

    @Test
    public void testWriteToWithGPNG() throws IOException {
        testWriteToWithGRGBAPNG(128, -1, -1, -1, -1);
    }

    @Test
    public void testWriteToWithRGBAPNG() throws IOException {
        testWriteToWithGRGBAPNG(-1, 128, 128, 128, 128);
    }

    @Test
    public void testWriteToWithGAPNG() throws IOException {
        testWriteToWithGRGBAPNG(128, -1, -1, -1, 128);
    }

    private void testWriteToWithGRGBAPNG(int gray, int red, int green, int blue, int alpha)
            throws IOException {
        int numComponents = (gray > -1 ? 1 : 3) + (alpha > -1 ? 1 : 0);
        ImageSize is = RawPNGTestUtil.getImageSize();
        ComponentColorModel cm = mock(ComponentColorModel.class);
        when(cm.getNumComponents()).thenReturn(numComponents);
        ImageRawPNG irpng = mock(ImageRawPNG.class);
        when(irpng.getColorModel()).thenReturn(cm);
        when(irpng.getSize()).thenReturn(is);
        ImageEncoderPNG iepng = new ImageEncoderPNG(irpng);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] data = RawPNGTestUtil.buildGRGBAData(gray, red, green, blue, alpha);
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        when(irpng.createInputStream()).thenReturn(bais);
        iepng.writeTo(baos);
        if (alpha > -1) {
            byte[] expected = RawPNGTestUtil.buildGRGBAData(gray, red, green, blue, -1);
            assertArrayEquals(expected, baos.toByteArray());
        } else {
            assertArrayEquals(data, baos.toByteArray());
        }
    }

    @Test
    public void testWriteToWithPalettePNG() throws IOException {
        ImageSize is = RawPNGTestUtil.getImageSize();
        IndexColorModel cm = mock(IndexColorModel.class);
        ImageRawPNG irpng = mock(ImageRawPNG.class);
        when(irpng.getColorModel()).thenReturn(cm);
        when(irpng.getSize()).thenReturn(is);
        ImageEncoderPNG iepng = new ImageEncoderPNG(irpng);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] data = RawPNGTestUtil.buildGRGBAData(128, -1, -1, -1, -1);
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        when(irpng.createInputStream()).thenReturn(bais);
        iepng.writeTo(baos);
        assertArrayEquals(data, baos.toByteArray());
    }

    @Test
    public void testGetImplicitFilterWithIndexColorModel() {
        ImageSize is = RawPNGTestUtil.getImageSize();
        IndexColorModel cm = mock(IndexColorModel.class);
        ImageRawPNG irpng = mock(ImageRawPNG.class);
        when(irpng.getColorModel()).thenReturn(cm);
        when(irpng.getBitDepth()).thenReturn(8);
        when(irpng.getSize()).thenReturn(is);
        ImageEncoderPNG iepng = new ImageEncoderPNG(irpng);

        String expectedFilter = "<< /Predictor 15 /Columns 32 /Colors 1 /BitsPerComponent 8 >> /FlateDecode";
        assertEquals(expectedFilter, iepng.getImplicitFilter());
    }

    @Test
    public void testGetImplicitFilterWithComponentColorModel() {
        ImageSize is = RawPNGTestUtil.getImageSize();
        ComponentColorModel cm = mock(ComponentColorModel.class);
        when(cm.getNumComponents()).thenReturn(3);
        ImageRawPNG irpng = mock(ImageRawPNG.class);
        when(irpng.getColorModel()).thenReturn(cm);
        when(irpng.getBitDepth()).thenReturn(8);
        when(irpng.getSize()).thenReturn(is);
        ImageEncoderPNG iepng = new ImageEncoderPNG(irpng);

        String expectedFilter = "<< /Predictor 15 /Columns 32 /Colors 3 /BitsPerComponent 8 >> /FlateDecode";
        assertEquals(expectedFilter, iepng.getImplicitFilter());
    }

}