~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/image/codec/tiff/ImageInfoTestCase.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
18
package org.apache.xmlgraphics.image.codec.tiff;
 
19
 
 
20
import java.awt.color.ColorSpace;
 
21
import java.awt.image.ColorModel;
 
22
import java.awt.image.IndexColorModel;
 
23
import java.awt.image.RenderedImage;
 
24
 
 
25
import org.junit.Before;
 
26
import org.junit.Test;
 
27
 
 
28
import static org.junit.Assert.assertArrayEquals;
 
29
import static org.junit.Assert.assertEquals;
 
30
import static org.mockito.Mockito.mock;
 
31
import static org.mockito.Mockito.when;
 
32
 
 
33
import static org.apache.xmlgraphics.image.codec.tiff.ExtraSamplesType.UNSPECIFIED;
 
34
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.BILEVEL_BLACK_IS_ZERO;
 
35
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.BILEVEL_WHITE_IS_ZERO;
 
36
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.CIELAB;
 
37
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.CMYK;
 
38
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.GENERIC;
 
39
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.GRAY;
 
40
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.RGB;
 
41
import static org.apache.xmlgraphics.image.codec.tiff.ImageType.YCBCR;
 
42
 
 
43
public class ImageInfoTestCase {
 
44
 
 
45
    private ColorSpace colorSpace;
 
46
    private ColorModel colorModel;
 
47
    private RenderedImage image;
 
48
    private TIFFEncodeParam params;
 
49
 
 
50
    @Before
 
51
    public void setUp() {
 
52
        colorSpace = mock(ColorSpace.class);
 
53
        colorModel = new TestColorModel(colorSpace, true);
 
54
        image = mock(RenderedImage.class);
 
55
        params = mock(TIFFEncodeParam.class);
 
56
    }
 
57
 
 
58
    @Test
 
59
    public void testNullColorModel() {
 
60
        testImageInfo(ImageInfo.newInstance(image, 1, 1, null, params),
 
61
                BILEVEL_BLACK_IS_ZERO, 0, null, 0, UNSPECIFIED);
 
62
 
 
63
        for (int i = 2; i < 10; i += 2) {
 
64
            testImageInfo(ImageInfo.newInstance(image, 1, i, null, params),
 
65
                    GENERIC, i - 1, null, 0, UNSPECIFIED);
 
66
        }
 
67
    }
 
68
 
 
69
    @Test
 
70
    public void testNonIndexColorModel() {
 
71
        testTheColorSpaceType(ColorSpace.TYPE_CMYK, false, CMYK);
 
72
        testTheColorSpaceType(ColorSpace.TYPE_GRAY, false, GRAY);
 
73
        testTheColorSpaceType(ColorSpace.TYPE_RGB, true, YCBCR);
 
74
        testTheColorSpaceType(ColorSpace.TYPE_RGB, false, RGB);
 
75
    }
 
76
 
 
77
    private void testTheColorSpaceType(int colorSpaceType, boolean getJpegCompress, ImageType expectedType) {
 
78
        when(colorSpace.getType()).thenReturn(colorSpaceType);
 
79
        TIFFEncodeParam params = mock(TIFFEncodeParam.class);
 
80
        when(params.getJPEGCompressRGBToYCbCr()).thenReturn(getJpegCompress);
 
81
 
 
82
        testImageInfo(ImageInfo.newInstance(image, 1, 1, colorModel, params),
 
83
                expectedType, 0, null, 0, UNSPECIFIED);
 
84
    }
 
85
 
 
86
    @Test
 
87
    public void testNonIndexColorModelWithNumBandsGreaterThan1() {
 
88
        testWithNumOfBandsGreaterThan1(ColorSpace.TYPE_GRAY, GRAY, 3, 1);
 
89
        testWithNumOfBandsGreaterThan1(ColorSpace.TYPE_Lab, CIELAB, 6, 3);
 
90
        testWithNumOfBandsGreaterThan1(ColorSpace.TYPE_CMYK, CMYK, 5, 2);
 
91
    }
 
92
 
 
93
    private void testWithNumOfBandsGreaterThan1(int colorSpaceType, ImageType type, int numBands,
 
94
            int numComponents) {
 
95
        when(colorSpace.getType()).thenReturn(colorSpaceType);
 
96
        when(colorSpace.getNumComponents()).thenReturn(numComponents);
 
97
        testImageInfo(ImageInfo.newInstance(image, 2, numBands, colorModel, params),
 
98
                type, numBands - numComponents, null, 0, UNSPECIFIED);
 
99
    }
 
100
 
 
101
    private void testImageInfo(ImageInfo imageInfo, ImageType imageType, int numExtraSamples,
 
102
            char[] colormap, int colormapSize, ExtraSamplesType extraSamplesType) {
 
103
        assertEquals(imageType, imageInfo.getType());
 
104
        assertEquals(numExtraSamples, imageInfo.getNumberOfExtraSamples());
 
105
        assertArrayEquals(colormap, imageInfo.getColormap());
 
106
        assertEquals(colormapSize, imageInfo.getColormapSize());
 
107
        assertEquals(extraSamplesType, imageInfo.getExtraSamplesType());
 
108
    }
 
109
 
 
110
    @Test
 
111
    public void testIndexColorModel() {
 
112
        byte[] blackIsZero = new byte[] {0, (byte) 0xff};
 
113
        IndexColorModel icm = new IndexColorModel(1, 2, blackIsZero, blackIsZero, blackIsZero);
 
114
        testImageInfo(ImageInfo.newInstance(image, 1, 1, icm, params),
 
115
                BILEVEL_BLACK_IS_ZERO, 0, null, 0, UNSPECIFIED);
 
116
 
 
117
        byte[] whiteIsZero = new byte[] {(byte) 0xff, 0};
 
118
        icm = new IndexColorModel(1, 2, whiteIsZero, whiteIsZero, whiteIsZero);
 
119
        testImageInfo(ImageInfo.newInstance(image, 1, 1, icm, params),
 
120
                BILEVEL_WHITE_IS_ZERO, 0, null, 0, UNSPECIFIED);
 
121
    }
 
122
 
 
123
    @Test
 
124
    public void testTileWidthHeight() {
 
125
        when(params.getWriteTiled()).thenReturn(true);
 
126
 
 
127
        when(image.getWidth()).thenReturn(10);
 
128
        when(image.getHeight()).thenReturn(10);
 
129
 
 
130
        for (int i = 1; i < 10000; i += 200) {
 
131
            when(params.getTileWidth()).thenReturn(i);
 
132
            when(params.getTileHeight()).thenReturn(i);
 
133
            int numTiles = ((10 + i - 1) / i) * ((10 + i - 1) / i);
 
134
            long bytesPerRow = (long) Math.ceil((1 / 8.0) * i * 1);
 
135
            long bytesPerTile = bytesPerRow * i;
 
136
 
 
137
            testTileOnImageInfo(ImageInfo.newInstance(image, 1, 1, colorModel, params),
 
138
                    i, i, numTiles, bytesPerRow, bytesPerTile);
 
139
        }
 
140
    }
 
141
 
 
142
    private void testTileOnImageInfo(ImageInfo imageInfo, int tileWidth, int tileHeight,
 
143
            int numTiles, long bytesPerRow, long bytesPerTile) {
 
144
        assertEquals(tileWidth, imageInfo.getTileWidth());
 
145
        assertEquals(tileHeight, imageInfo.getTileHeight());
 
146
        assertEquals(numTiles, imageInfo.getNumTiles());
 
147
        assertEquals(bytesPerRow, imageInfo.getBytesPerRow());
 
148
        assertEquals(bytesPerTile, imageInfo.getBytesPerTile());
 
149
    }
 
150
 
 
151
    @Test
 
152
    public void testGetColormap() {
 
153
        ImageInfo sut = ImageInfo.newInstance(image, 1, 1,
 
154
                new IndexColorModel(1, 2, new byte[2], new byte[2], new byte[2], new byte[2]), params);
 
155
        char[] colormap = sut.getColormap();
 
156
        assertEquals(0, colormap[0]);
 
157
        colormap[0] = 1;
 
158
        //  Assert that getColormap() returns a defensive copy
 
159
        assertEquals(0, sut.getColormap()[0]);
 
160
    }
 
161
 
 
162
    private static final class TestColorModel extends ColorModel {
 
163
 
 
164
        protected TestColorModel(ColorSpace cspace, boolean isAlphaPremultiplied) {
 
165
            super(1, new int[] {1, 1}, cspace, isAlphaPremultiplied, isAlphaPremultiplied, 1, 1);
 
166
        }
 
167
 
 
168
        @Override
 
169
        public int getRed(int pixel) {
 
170
            return 0;
 
171
        }
 
172
 
 
173
        @Override
 
174
        public int getGreen(int pixel) {
 
175
            return 0;
 
176
        }
 
177
 
 
178
        @Override
 
179
        public int getBlue(int pixel) {
 
180
            return 0;
 
181
        }
 
182
 
 
183
        @Override
 
184
        public int getAlpha(int pixel) {
 
185
            return 0;
 
186
        }
 
187
    }
 
188
}