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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.xmlgraphics.image.codec.tiff;
20
import java.awt.color.ColorSpace;
21
import java.awt.image.ColorModel;
22
import java.awt.image.IndexColorModel;
23
import java.awt.image.RenderedImage;
25
import org.junit.Before;
26
import org.junit.Test;
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;
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;
43
public class ImageInfoTestCase {
45
private ColorSpace colorSpace;
46
private ColorModel colorModel;
47
private RenderedImage image;
48
private TIFFEncodeParam params;
52
colorSpace = mock(ColorSpace.class);
53
colorModel = new TestColorModel(colorSpace, true);
54
image = mock(RenderedImage.class);
55
params = mock(TIFFEncodeParam.class);
59
public void testNullColorModel() {
60
testImageInfo(ImageInfo.newInstance(image, 1, 1, null, params),
61
BILEVEL_BLACK_IS_ZERO, 0, null, 0, UNSPECIFIED);
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);
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);
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);
82
testImageInfo(ImageInfo.newInstance(image, 1, 1, colorModel, params),
83
expectedType, 0, null, 0, UNSPECIFIED);
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);
93
private void testWithNumOfBandsGreaterThan1(int colorSpaceType, ImageType type, int numBands,
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);
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());
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);
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);
124
public void testTileWidthHeight() {
125
when(params.getWriteTiled()).thenReturn(true);
127
when(image.getWidth()).thenReturn(10);
128
when(image.getHeight()).thenReturn(10);
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;
137
testTileOnImageInfo(ImageInfo.newInstance(image, 1, 1, colorModel, params),
138
i, i, numTiles, bytesPerRow, bytesPerTile);
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());
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]);
158
// Assert that getColormap() returns a defensive copy
159
assertEquals(0, sut.getColormap()[0]);
162
private static final class TestColorModel extends ColorModel {
164
protected TestColorModel(ColorSpace cspace, boolean isAlphaPremultiplied) {
165
super(1, new int[] {1, 1}, cspace, isAlphaPremultiplied, isAlphaPremultiplied, 1, 1);
169
public int getRed(int pixel) {
174
public int getGreen(int pixel) {
179
public int getBlue(int pixel) {
184
public int getAlpha(int pixel) {