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