1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| /**
*
* @author jdrigo
*
*/
public enum ImageFactory {
INSTANCE;
public BufferedImage readImage(String path) throws IOException {
return ImageIO.read(new File(path));
}
public BufferedImage readImage(URL url) throws IOException {
return ImageIO.read(url);
}
public BufferedImage createNoiseImage(int width, int height) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
double[][] noise = createNoise(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int g = (int) (256 * noise[x][y]);
Color color = new Color(g, g, g);
image.setRGB(x, y, color.getRGB());
}
}
return image;
}
public BufferedImage createClouds(int width, int height, int blue) {
if ( width<=0 || height<=0 ) throw new IllegalArgumentException("Size is out of range: " + width + " × " + height);
if ( blue<169 || blue>240 ) throw new IllegalArgumentException("Blue color is out of range: " + blue);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
double[][] noise = createNoise(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int l = 192 + (int)(turbulence(x, y, 64, noise)) / 3;
if ( l>255 ) {
l=255;
}
Color color = HSLToRGB(blue, 255, l);
image.setRGB(x, y, color.getRGB());
}
}
return image;
}
public Color HSLToRGB(int h, int s, int l) {
return HSLToRGB(h, 100*s/255f, 100*(l/255f), 1f);
}
public Color HSLToRGB(float h, float s, float l) {
return HSLToRGB(h, s, l, 1f);
}
public Color HSLToRGB(float h, float s, float l, float alpha) {
if (s <0.0f || s > 100.0f) {
throw new IllegalArgumentException( "Color parameter outside of expected range - Saturation: " + s);
}
if (l <0.0f || l > 100.0f) {
throw new IllegalArgumentException( "Color parameter outside of expected range - Luminance: " + l );
}
if (alpha <0.0f || alpha > 1.0f) {
throw new IllegalArgumentException( "Color parameter outside of expected range - Alpha: " + alpha );
}
h = h % 360.0f;
h /= 360f;
s /= 100f;
l /= 100f;
float q = 0;
if (l < 0.5)
q = l * (1 + s);
else
q = (l + s) - (s * l);
float p = 2 * l - q;
float r = Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)));
float g = Math.max(0, HueToRGB(p, q, h));
float b = Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)));
r = Math.min(r, 1.0f);
g = Math.min(g, 1.0f);
b = Math.min(b, 1.0f);
return new Color(r, g, b, alpha);
}
private float HueToRGB(float p, float q, float h) {
if (h < 0) h += 1;
if (h > 1 ) h -= 1;
if (6 * h < 1)
{
return p + ((q - p) * 6 * h);
}
if (2 * h < 1 )
{
return q;
}
if (3 * h < 2)
{
return p + ( (q - p) * 6 * ((2.0f / 3.0f) - h) );
}
return p;
}
private double[][] createNoise(int width, int height) {
Random random = new Random();
double[][] noise = new double[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
noise[x][y] = random.nextDouble();
}
}
return noise;
}
// zoom
private double smoothNoise(double x, double y, double[][] noise) {
int noiseWidth = noise.length;
int noiseHeight = noise[0].length;
// partie fractionnel
double fractX = x - (int) x;
double fractY = y - (int) y;
// bouclage
int x1 = ((int) x + noiseWidth) % noiseWidth;
int y1 = ((int) y + noiseHeight) % noiseHeight;
// valeurs voisines
int x2 = (x1 + noiseWidth - 1) % noiseWidth;
int y2 = (y1 + noiseHeight - 1) % noiseHeight;
// interpolation bilineaire
double value = 0.0;
value += fractX * fractY * noise[x1][y1];
value += fractX * (1 - fractY) * noise[x1][y2];
value += (1 - fractX) * fractY * noise[x2][y1];
value += (1 - fractX) * (1 - fractY) * noise[x2][y2];
return value;
}
private double turbulence(double x, double y, double size, double[][] noise) {
double value = 0.0, initialSize = size;
while (size >= 1) {
value += smoothNoise(x / size, y / size, noise) * size;
size /= 2.0;
}
return (128.0 * value / initialSize);
}
} |
Partager