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.roundtrip;
18  
19  import java.awt.image.BufferedImage;
20  
21  final class TestImages {
22  
23      static BufferedImage createArgbBitmapImage(final int width, final int height) {
24          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
25          for (int x = 0; x < width; x++) {
26              for (int y = 0; y < height; y++) {
27                  // alternating black and white.
28                  final int modulator = y + 2; // make sure lines vary.
29                  final int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
30                  result.setRGB(x, y, argb);
31              }
32          }
33          return result;
34      }
35  
36      static BufferedImage createArgbGrayscaleImage(final int width, final int height) {
37          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
38          for (int x = 0; x < width; x++) {
39              for (int y = 0; y < height; y++) {
40                  final int value = 256 * (x + y) / (width + height);
41                  final int argb = 0xff << 24 | value << 16 | value << 8 | value << 0;
42  
43                  result.setRGB(x, y, argb);
44              }
45          }
46          return result;
47      }
48  
49      static BufferedImage createBitmapBitmapImage(final int width, final int height) {
50          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
51          for (int x = 0; x < width; x++) {
52              for (int y = 0; y < height; y++) {
53                  // alternating black and white.
54                  final int modulator = y + 2; // make sure lines vary.
55                  final int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
56                  result.setRGB(x, y, argb);
57              }
58          }
59          return result;
60      }
61  
62      static BufferedImage createFullColorImage(final int width, final int height) {
63          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
64          for (int x = 0; x < width; x++) {
65              for (int y = 0; y < height; y++) {
66                  final int red = x * 255 / width;
67                  final int green = y * 255 / height;
68                  final int blue = (x + y) * 255 / (width + height);
69                  final int argb = 0xff << 24 | red << 16 | green << 8 | blue << 0;
70                  result.setRGB(x, y, argb);
71              }
72          }
73          return result;
74      }
75  
76      static BufferedImage createGrayscaleGrayscaleImage(final int width, final int height) {
77          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
78          for (int x = 0; x < width; x++) {
79              for (int y = 0; y < height; y++) {
80                  final int value = 256 * (x + y) / (width + height);
81                  final int argb = 0xff << 24 | value << 16 | value << 8 | value << 0;
82  
83                  result.setRGB(x, y, argb);
84              }
85          }
86          return result;
87      }
88  
89      static BufferedImage createLimitedColorImage(final int width, final int height) {
90          final int[] colors = { 0xffffffff, 0xff000000, 0xfff00000, 0xff0000ff, 0xff123456, 0xfffefeff, 0xff7f817f, };
91  
92          final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
93          for (int x = 0; x < width; x++) {
94              for (int y = 0; y < height; y++) {
95                  final int argb = colors[(x + y) % colors.length];
96                  result.setRGB(x, y, argb);
97              }
98          }
99          return result;
100     }
101 
102     private TestImages() {
103     }
104 }