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.common;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.awt.image.BufferedImage;
25  import java.awt.image.ColorModel;
26  import java.awt.image.RasterFormatException;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Provides unit tests for the ImageBuilder class.
32   */
33  public class ImageBuilderTest {
34  
35      void executeBadBounds(final ImageBuilder imageBuilder, final int x, final int y, final int w, final int h) {
36          try {
37              final ImageBuilder sub = imageBuilder.getSubset(x, y, w, h);
38              fail("Failed to detect bad bounds " + x + ", " + y + ", " + w + ", " + h);
39          } catch (final RasterFormatException rfe) {
40              // success, no action required
41          }
42      }
43  
44      void executeBadConstructor(final int w, final int h) {
45          try {
46              final ImageBuilder iBuilder = new ImageBuilder(w, h, true);
47              fail("Failed to detect bad constructor " + w + ", " + h);
48          } catch (final RasterFormatException rfe) {
49              // success, no action required
50          }
51      }
52  
53      void populate(final ImageBuilder imageBuilder) {
54          for (int x = 0; x < 100; x++) {
55              for (int y = 0; y < 100; y++) {
56                  final int k = y * 100 + x;
57                  final int rgb = 0xff000000 | k;
58                  imageBuilder.setRgb(x, y, rgb);
59              }
60          }
61      }
62  
63      /**
64       * Test of bad bounds in sub-image
65       */
66      @Test
67      public void testBoundsCheck() {
68  
69          final ImageBuilder imageBuilder = new ImageBuilder(100, 100, false);
70  
71          executeBadBounds(imageBuilder, -1, 0, 50, 50);
72          executeBadBounds(imageBuilder, 0, -1, 50, 50);
73          executeBadBounds(imageBuilder, 0, 0, 0, 50);
74          executeBadBounds(imageBuilder, 0, 0, 50, 0);
75          executeBadBounds(imageBuilder, 90, 0, 50, 50);
76          executeBadBounds(imageBuilder, 0, 90, 50, 50);
77      }
78  
79      /**
80       * Test of bad dimensions in constructor
81       */
82      @Test
83      public void testConstructorBounds() {
84          executeBadConstructor(0, 10);
85          executeBadConstructor(10, 0);
86      }
87  
88      /**
89       * Test whether color model is properly applied to buffered images
90       */
91      @Test
92      void testImageColorModel() {
93          ImageBuilder imageBuilder;
94          BufferedImage bImage;
95          ColorModel model;
96          imageBuilder = new ImageBuilder(100, 100, false);
97          bImage = imageBuilder.getBufferedImage();
98          model = bImage.getColorModel();
99          assertFalse(model.hasAlpha(), "Output image has alpha where not specified");
100 
101         imageBuilder = new ImageBuilder(100, 100, true, false);
102         bImage = imageBuilder.getBufferedImage();
103         model = bImage.getColorModel();
104         assertTrue(model.hasAlpha(), "Output image does not have alpha where specified");
105         assertFalse(model.isAlphaPremultiplied(), "Output image has alpha pre-multiplied where not specified");
106 
107         imageBuilder = new ImageBuilder(100, 100, true, true);
108         bImage = imageBuilder.getBufferedImage();
109         model = bImage.getColorModel();
110         assertTrue(model.hasAlpha(), "Output image does not have alpha where specified");
111         assertTrue(model.isAlphaPremultiplied(), "Output image does not have alpha pre-multiplied where specified");
112     }
113 
114     /**
115      * Test whether sub-image is consistent with source
116      */
117     @Test
118     public void testSubimageAccess() {
119         final ImageBuilder imageBuilder = new ImageBuilder(100, 100, false);
120         populate(imageBuilder);
121         final BufferedImage bImage = imageBuilder.getSubimage(25, 25, 25, 25);
122         final int w = bImage.getWidth();
123         final int h = bImage.getHeight();
124         assertEquals(w, 25, "Width of subimage does not match");
125         assertEquals(h, 25, "Height of subimage does not match");
126 
127         for (int x = 25; x < 50; x++) {
128             for (int y = 25; y < 50; y++) {
129                 final int k = bImage.getRGB(x - 25, y - 25);
130                 final int rgb = imageBuilder.getRgb(x, y);
131                 assertEquals(k, rgb, "Invalid buffered image subpixel at " + x + ", " + y);
132             }
133         }
134 
135         final ImageBuilder testBuilder = imageBuilder.getSubset(25, 25, 25, 25);
136         for (int x = 25; x < 50; x++) {
137             for (int y = 25; y < 50; y++) {
138                 final int k = testBuilder.getRgb(x - 25, y - 25);
139                 final int rgb = imageBuilder.getRgb(x, y);
140                 assertEquals(k, rgb, "Invalid image builder subpixel at " + x + ", " + y);
141             }
142         }
143     }
144 
145 }