1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
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
81
82 @Test
83 public void testConstructorBounds() {
84 executeBadConstructor(0, 10);
85 executeBadConstructor(10, 0);
86 }
87
88
89
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
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 }