View Javadoc
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  package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.imaging.ImagingException;
25  import org.apache.commons.imaging.common.ImageBuilder;
26  import org.apache.commons.imaging.formats.tiff.photometricinterpreters.PhotometricInterpreterLogLuv.TristimulusValues;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  public class PhotometricInterpreterLogLuvTest {
31  
32      private PhotometricInterpreterLogLuv p;
33  
34      private final int samplesPerPixel = 8;
35      private final int[] bitsPerSample = { 1, 2, 3 };
36      private final int predictor = 1;
37      private final int width = 800;
38      private final int height = 600;
39  
40      @BeforeEach
41      public void setUp() {
42          p = new PhotometricInterpreterLogLuv(samplesPerPixel, bitsPerSample, predictor, width, height);
43      }
44  
45      @Test
46      public void testConstructor() {
47          assertEquals(samplesPerPixel, p.samplesPerPixel);
48          for (int i = 0; i < bitsPerSample.length; i++) {
49              assertEquals(bitsPerSample[i], p.getBitsPerSample(i));
50          }
51          assertEquals(predictor, p.predictor);
52          assertEquals(width, p.width);
53          assertEquals(height, p.height);
54      }
55  
56      @Test
57      public void testGetRgbValues() {
58          // any value equals 0 will have its pow(N, 3) equal to 0
59          final TristimulusValues triValues = new TristimulusValues();
60          triValues.x = 0;
61          triValues.y = 0;
62          triValues.z = 0;
63          assertEquals(0, p.getRgbValues(triValues).r);
64          assertEquals(0, p.getRgbValues(triValues).g);
65          assertEquals(0, p.getRgbValues(triValues).b);
66          triValues.x = 1;
67          triValues.y = 1;
68          triValues.z = 1;
69          assertEquals(28, p.getRgbValues(triValues).r);
70          assertEquals(24, p.getRgbValues(triValues).g);
71          assertEquals(23, p.getRgbValues(triValues).b);
72      }
73  
74      @Test
75      public void testGetTristimulusValues() {
76          // any value equals 0 will have its pow(N, 3) equal to 0
77          assertEquals(0.0d, p.getTristimulusValues(0, 0, 0).x, 0.001d);
78          assertEquals(0.0d, p.getTristimulusValues(0, 0, 0).y, 0.001d);
79          assertEquals(0.0d, p.getTristimulusValues(0, 0, 0).z, 0.001d);
80          // values under the threshold used in the if statements
81          assertEquals(0.04126d, p.getTristimulusValues(1, 0, 0).x, 0.001d);
82          assertEquals(0.04341d, p.getTristimulusValues(1, 0, 0).y, 0.001d);
83          assertEquals(0.04727d, p.getTristimulusValues(1, 0, 0).z, 0.001d);
84          // values under the threshold used in the if statements
85          assertEquals(29.36116d, p.getTristimulusValues(100, 100, 50).x, 0.001d);
86          assertEquals(10.78483d, p.getTristimulusValues(100, 100, 50).y, 0.001d);
87          assertEquals(1.25681d, p.getTristimulusValues(100, 100, 50).z, 0.001d);
88      }
89  
90      @Test
91      public void testInterpretPixel() throws ImagingException, IOException {
92          final ImageBuilder imgBuilder = new ImageBuilder(600, 400, /* alpha */ true);
93          final int x = 10;
94          final int y = 20;
95          p.interpretPixel(imgBuilder, new int[] { 100, (byte) 32, (byte) 2 }, x, y);
96          assertEquals(-7584166, imgBuilder.getRgb(x, y));
97      }
98  
99      @Test
100     public void testInterpretPixelEmptySamples() {
101         assertThrows(ImagingException.class, () -> p.interpretPixel(null, new int[] {}, 0, 0));
102     }
103 
104     @Test
105     public void testInterpretPixelNullSamples() {
106         assertThrows(ImagingException.class, () -> p.interpretPixel(null, null, 0, 0));
107     }
108 }