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
|
/*
Copyright 2006 Jerry Huxtable
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.jhlabs.image;
import java.awt.image.*;
import java.util.*;
public class SparkleFilter extends PointFilter implements java.io.Serializable {
static final long serialVersionUID = 1692413049411710802L;
private int rays = 50;
private int radius = 25;
private int amount = 50;
private int color = 0xffffffff;
private int randomness = 25;
private int width, height;
private int centreX, centreY;
private long seed = 371;
private float[] rayLengths;
private Random randomNumbers = new Random();
public SparkleFilter() {
}
public void setColor(int color) {
this.color = color;
}
public int getColor() {
return color;
}
public void setRandomness(int randomness) {
this.randomness = randomness;
}
public int getRandomness() {
return randomness;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setRays(int rays) {
this.rays = rays;
}
public int getRays() {
return rays;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setDimensions(int width, int height) {
this.width = width;
this.height = height;
centreX = width/2;
centreY = height/2;
super.setDimensions(width, height);
randomNumbers.setSeed(seed);
rayLengths = new float[rays];
for (int i = 0; i < rays; i++)
rayLengths[i] = radius + randomness / 100.0f * radius * (float)randomNumbers.nextGaussian();
}
public int filterRGB(int x, int y, int rgb) {
float dx = x-centreX;
float dy = y-centreY;
float distance = dx*dx+dy*dy;
float angle = (float)Math.atan2(dy, dx);
float d = (angle+ImageMath.PI) / (ImageMath.TWO_PI) * rays;
int i = (int)d;
float f = d - i;
if (radius != 0) {
float length = ImageMath.lerp(f, rayLengths[i % rays], rayLengths[(i+1) % rays]);
float g = length*length / (distance+0.0001f);
g = (float)Math.pow(g, (100-amount) / 50.0);
f -= 0.5f;
// f *= amount/50.0f;
f = 1 - f*f;
f *= g;
}
f = ImageMath.clamp(f, 0, 1);
return ImageMath.mixColors(f, rgb, color);
}
public String toString() {
return "Stylize/Sparkle...";
}
}
|