use of org.jwildfire.image.Pixel in project JWildfire by thargor6.
the class UgrGradientReader method parsePalette.
private void parsePalette(RGBPalette pPalette, String pUgr) {
Map<Integer, RGBColor> colors = new HashMap<Integer, RGBColor>();
StringTokenizer tokenizer = new StringTokenizer(pUgr, " \n\r");
Pixel pixel = new Pixel();
while (tokenizer.hasMoreElements()) {
String token = tokenizer.nextToken();
String nextToken;
if (token.startsWith("title=")) {
StringTokenizer lineTokenizer = new StringTokenizer(token, "\"");
if (lineTokenizer.hasMoreElements()) {
lineTokenizer.nextElement();
if (lineTokenizer.hasMoreElements()) {
pPalette.setFlam3Name((String) lineTokenizer.nextElement());
}
}
} else if (token.startsWith("index=") && tokenizer.hasMoreElements() && (nextToken = tokenizer.nextToken()).startsWith("color=")) {
int index = Integer.parseInt(token.substring(6, token.length()));
int colorValue = Integer.parseInt(nextToken.substring(6, nextToken.length()));
pixel.setARGBValue(colorValue);
RGBColor color = new RGBColor(pixel.b, pixel.g, pixel.r);
colors.put(index, color);
}
}
boolean doInterpolate = colors.size() < 256;
boolean doSmooth = pUgr.contains("smooth=yes");
final int maxIdx = 399;
if (doInterpolate) {
if (colors.get(0) == null) {
colors.put(0, new RGBColor(0, 0, 0));
}
if (colors.get(maxIdx) == null) {
colors.put(maxIdx, new RGBColor(0, 0, 0));
}
} else {
RGBColor lastColor = new RGBColor(0, 0, 0);
for (int i = 0; i <= maxIdx; i++) {
RGBColor color = colors.get(i);
if (color == null) {
colors.put(i, lastColor);
} else {
lastColor = color;
}
}
}
pPalette.setColors(colors, doInterpolate, doSmooth);
}
use of org.jwildfire.image.Pixel in project JWildfire by thargor6.
the class ImgPaletteReader method readPaletteFromMapData.
public List<RGBPalette> readPaletteFromMapData(String pMapData, String pFilename) throws Exception {
List<RGBPalette> res = new ArrayList<RGBPalette>();
RGBPalette gradient = new RGBPalette();
res.add(gradient);
gradient.setFlam3Name(new File(pFilename).getName());
SimpleImage img = new ImageReader().loadImage(pFilename);
if (img.getImageWidth() > 0 && img.getImageHeight() > 0) {
Map<Integer, RGBColor> colors = new HashMap<Integer, RGBColor>();
Pixel rgbPixel = new Pixel();
for (int i = 0; i < img.getImageWidth(); i++) {
rgbPixel.setARGBValue(img.getARGBValue(i, 0));
RGBColor color = new RGBColor(rgbPixel.r, rgbPixel.g, rgbPixel.b);
colors.put(i, color);
}
gradient.setColors(colors, false, false);
}
return res;
}
use of org.jwildfire.image.Pixel in project JWildfire by thargor6.
the class PixelizeTransformer method performPixelTransformation.
@Override
protected void performPixelTransformation(WFImage pImg) {
// Init
SimpleImage img = (SimpleImage) pImg;
int width = pImg.getImageWidth();
int height = pImg.getImageHeight();
int gridSize = this.gridSize;
Grid grid = (gridSize < 1) ? Grid.OFF : this.grid;
Pixel pixel = new Pixel();
int pw = this.width;
int ph = this.height;
if (pw < 1)
pw = 1;
else if (pw > width)
pw = width;
if (ph < 1)
ph = 1;
else if (ph > height)
ph = height;
if ((pw == 1) && (ph == 1))
return;
int pwo = pw;
int pho = ph;
int hc = width / pw;
if ((width % pw) != 0)
hc += 2;
int vc = height / ph;
if ((height % ph) != 0)
vc += 2;
int[] pwa = new int[hc];
int[] pha = new int[vc];
int gSize = 0;
if (grid != Grid.OFF) {
gSize = gridSize;
if ((gSize > pwo) || (gSize > pho)) {
if (pwo > pho)
gSize = pho;
else
gSize = pwo;
}
}
int[] grUp = null, ggUp = null, gbUp = null;
int[] grDown = null, ggDown = null, gbDown = null;
int[] grLeft = null, ggLeft = null, gbLeft = null;
int[] grRight = null, ggRight = null, gbRight = null;
if (((grid) != Grid.OFF) && (gSize >= 1)) {
if ((gSize % 2) == 0)
gSize++;
int s = (gSize / 2 + 1);
if (gSize > 1) {
grUp = new int[s];
ggUp = new int[s];
gbUp = new int[s];
grDown = new int[s];
ggDown = new int[s];
gbDown = new int[s];
grLeft = new int[s];
ggLeft = new int[s];
gbLeft = new int[s];
grRight = new int[s];
ggRight = new int[s];
gbRight = new int[s];
}
}
//
if ((width % pw) == 0) {
for (int i = 0; i < hc; i++) pwa[i] = pw;
} else {
if (this.centre) {
pwa[0] = (width % pw) / 2;
if (pwa[0] == 0)
pwa[0] = 1;
for (int i = 1; i < (hc - 1); i++) pwa[i] = pw;
pwa[hc - 1] = width - pwa[0] - (hc - 2) * pw;
if (pwa[hc - 1] == 0)
hc--;
} else {
hc--;
for (int i = 0; i < (hc - 1); i++) pwa[i] = pw;
pwa[hc - 1] = width - (hc - 1) * pw;
}
}
if ((height % ph) == 0) {
for (int i = 0; i < vc; i++) pha[i] = ph;
} else {
if (this.centre) {
pha[0] = (height % ph) / 2;
if (pha[0] == 0)
pha[0] = 1;
for (int i = 1; i < (vc - 1); i++) pha[i] = ph;
pha[vc - 1] = height - pha[0] - (vc - 2) * ph;
if (pha[vc - 1] == 0)
vc--;
} else {
vc--;
for (int i = 0; i < (vc - 1); i++) pha[i] = ph;
pha[vc - 1] = height - (vc - 1) * ph;
}
}
{
int topOffset = 0;
for (int i = 0; i < vc; i++) {
ph = pha[i];
int leftOffset = 0;
for (int j = 0; j < hc; j++) {
pw = pwa[j];
int pc = pw * ph;
/* compute the average-color */
int ra = 0, ga = 0, ba = 0;
for (int k = 0; k < ph; k++) {
for (int l = 0; l < pw; l++) {
int x = leftOffset + l;
int y = topOffset + k;
pixel.setARGBValue(srcImg.getARGBValue(x, y));
ra += pixel.r;
ga += pixel.g;
ba += pixel.b;
}
}
ra = Tools.limitColor(ra / pc);
ga = Tools.limitColor(ga / pc);
ba = Tools.limitColor(ba / pc);
// set this color in the current box
for (int k = 0; k < ph; k++) {
for (int l = 0; l < pw; l++) {
int x = leftOffset + l;
int y = topOffset + k;
img.setRGB(x, y, pixel);
}
}
leftOffset += pw;
}
topOffset += ph;
}
}
if ((grid != Grid.OFF) && (gSize >= 1)) {
double gFallOffUp = this.gridFallOffTop;
double gFallOffDown = this.gridFallOffBottom;
double gFallOffLeft = this.gridFallOffLeft;
double gFallOffRight = this.gridFallOffRight;
if (gFallOffUp < 0.0)
gFallOffUp = 0.0;
if (gFallOffDown < 0.0)
gFallOffDown = 0.0;
if (gFallOffLeft < 0.0)
gFallOffLeft = 0.0;
if (gFallOffRight < 0.0)
gFallOffRight = 0.0;
int s = (gSize / 2 + 1);
int n = s - 1;
if (gSize > 1) {
if ((gFallOffUp < 0.0001) && (gFallOffDown < 0.0001) && (gFallOffLeft < 0.0001) && (gFallOffRight < 0.0001)) {
for (int k = 0; k < s; k++) {
grUp[k] = grDown[k] = grLeft[k] = grRight[k] = this.gridColor.getRed();
ggUp[k] = ggDown[k] = ggLeft[k] = ggRight[k] = this.gridColor.getGreen();
gbUp[k] = gbDown[k] = gbLeft[k] = gbRight[k] = this.gridColor.getBlue();
}
} else {
calcCArray(grUp, s, gFallOffUp, this.gridColor.getRed());
calcCArray(ggUp, s, gFallOffUp, this.gridColor.getGreen());
calcCArray(gbUp, s, gFallOffUp, this.gridColor.getBlue());
calcCArray(grDown, s, gFallOffDown, this.gridColor.getRed());
calcCArray(ggDown, s, gFallOffDown, this.gridColor.getGreen());
calcCArray(gbDown, s, gFallOffDown, this.gridColor.getBlue());
calcCArray(grLeft, s, gFallOffLeft, this.gridColor.getRed());
calcCArray(ggLeft, s, gFallOffLeft, this.gridColor.getGreen());
calcCArray(gbLeft, s, gFallOffLeft, this.gridColor.getBlue());
calcCArray(grRight, s, gFallOffRight, this.gridColor.getRed());
calcCArray(ggRight, s, gFallOffRight, this.gridColor.getGreen());
calcCArray(gbRight, s, gFallOffRight, this.gridColor.getBlue());
}
} else
n = 0;
// horizontal grid
if ((grid == Grid.XY) || (grid == Grid.X)) {
int topOffset = 0;
int r, g, b;
for (int i = 0; i < (vc - 1); i++) {
ph = pha[i];
topOffset += ph;
// base grid
r = this.gridColor.getRed();
g = this.gridColor.getGreen();
b = this.gridColor.getBlue();
for (int j = 0; j < width; j++) {
img.setRGB(j, topOffset, r, g, b);
}
// up
for (int kk = 1; kk <= n; kk++) {
r = grUp[kk];
g = ggUp[kk];
b = gbUp[kk];
for (int j = 0; j < width; j++) {
int y = topOffset - kk;
if (y >= 0)
img.setRGB(j, y, r, g, b);
}
}
// down
for (int kk = 1; kk <= n; kk++) {
r = grDown[kk];
g = ggDown[kk];
b = gbDown[kk];
for (int j = 0; j < width; j++) {
int y = topOffset + kk;
if (y < height)
img.setRGB(j, y, r, g, b);
}
}
}
}
// vertical grid
if ((grid == Grid.XY) || (grid == Grid.Y)) {
int r, g, b;
int leftOffset = 0;
for (int i = 0; i < (hc - 1); i++) {
pw = pwa[i];
leftOffset += pw;
// base grid
r = this.gridColor.getRed();
g = this.gridColor.getGreen();
b = this.gridColor.getBlue();
for (int j = 0; j < height; j++) {
img.setRGB(leftOffset, j, r, g, b);
}
// left
for (int kk = 1; kk <= n; kk++) {
r = grLeft[kk];
g = ggLeft[kk];
b = gbLeft[kk];
for (int j = 0; j < height; j++) {
int x = leftOffset - kk;
if (x >= 0)
img.setRGB(x, j, r, g, b);
}
}
// right
for (int kk = 1; kk <= n; kk++) {
r = grRight[kk];
g = ggRight[kk];
b = gbRight[kk];
for (int j = 0; j < height; j++) {
int x = leftOffset + kk;
if (x < width)
img.setRGB(x, j, r, g, b);
}
}
}
}
// connections
if (grid == Grid.XY) {
int topOffset = 0;
for (int i = 0; i < (vc - 1); i++) {
ph = pha[i];
topOffset += ph;
int leftOffset = 0;
for (int j = 0; j < (hc - 1); j++) {
pw = pwa[j];
leftOffset += pw;
int r, g, b;
// base grid
r = this.gridColor.getRed();
g = this.gridColor.getGreen();
b = this.gridColor.getBlue();
for (int kk = -n; kk <= +n; kk++) {
int x = leftOffset + kk;
int y = topOffset;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
img.setRGB(x, y, r, g, b);
}
// up
for (int kk = 1; kk <= n; kk++) {
r = grUp[kk];
g = ggUp[kk];
b = gbUp[kk];
for (int ll = kk; ll <= n; ll++) {
int x = leftOffset - ll;
int y = topOffset - kk;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
img.setRGB(x, y, r, g, b);
x = leftOffset + ll;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
img.setRGB(x, y, r, g, b);
}
}
// down
for (int kk = 1; kk <= n; kk++) {
r = grDown[kk];
g = ggDown[kk];
b = gbDown[kk];
for (int ll = kk; ll <= n; ll++) {
int x = leftOffset - ll;
int y = topOffset + kk;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
img.setRGB(x, y, r, g, b);
x = leftOffset + ll;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
img.setRGB(x, y, r, g, b);
}
}
}
}
}
}
}
use of org.jwildfire.image.Pixel in project JWildfire by thargor6.
the class RefractTransformer method performPixelTransformation.
@Override
protected void performPixelTransformation(WFImage pImg) {
SimpleImage img = (SimpleImage) pImg;
int width = pImg.getImageWidth();
int height = pImg.getImageHeight();
double xscl = this.scaleX;
double yscl = this.scaleY;
double w1 = (double) width - 1.0;
double h1 = (double) height - 1.0;
Pixel pPixel = new Pixel();
for (int pY = 0; pY < height; pY++) {
for (int pX = 0; pX < width; pX++) {
pPixel.setARGBValue(img.getARGBValue(pX, pY));
int argb = srcImg.getARGBValue(pX, pY);
toolPixel.setARGBValue(argb);
double gint = (double) (toolPixel.r - avg);
double x = (double) pX - xscl * gint;
double y = (double) pY - yscl * gint;
/* color-wrapping */
if (this.wrap) {
while (x >= ((double) width - 0.5)) x -= (double) (width - 1);
while ((int) x < 0.5) x += (double) (width - 1);
while (y >= ((double) height - 0.5)) y -= (double) (height - 1);
while ((int) y < 0.5) y += (double) (height - 1);
}
/* render it */
double xi = Tools.fmod33(x);
double yi = Tools.fmod33(y);
if ((x < 0.0) || (x > w1) || (y < 0.0) || (y > h1)) {
pPixel.r = pPixel.g = pPixel.b = 0;
} else {
readSrcPixels(x, y);
pPixel.r = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.r) + xi * (srcQ.r)) + yi * ((1.0 - xi) * (srcR.r) + xi * (srcS.r))));
pPixel.g = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.g) + xi * (srcQ.g)) + yi * ((1.0 - xi) * (srcR.g) + xi * (srcS.g))));
pPixel.b = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.b) + xi * (srcQ.b)) + yi * ((1.0 - xi) * (srcR.b) + xi * (srcS.b))));
}
img.setRGB(pX, pY, pPixel.r, pPixel.g, pPixel.b);
}
}
}
use of org.jwildfire.image.Pixel in project JWildfire by thargor6.
the class RotateTransformer method performPixelTransformation.
@Override
protected void performPixelTransformation(WFImage pImg) {
SimpleImage img = (SimpleImage) pImg;
int width = pImg.getImageWidth();
int height = pImg.getImageHeight();
Pixel pPixel = new Pixel();
double alpha = (0.0 - this.amount) * Math.PI / 180.0;
double cosa = Math.cos(alpha);
double sina = Math.sin(alpha);
double cx = this.centreX - 0.5;
double cy = this.centreY - 0.5;
double zoom = this.zoom;
if (zoom < 0.01)
zoom = 0.01;
zoom = 1.0 / zoom;
double radius = this.radius * this.radius;
double w1 = (double) width - 1.0;
double h1 = (double) height - 1.0;
for (int i = 0; i < height; i++) {
double dyq = (double) i - cy;
double y0 = dyq * zoom;
dyq *= dyq;
for (int j = 0; j < width; j++) {
/* transform the point */
double x0 = (double) j - cx;
double rr = x0 * x0 + dyq;
if (rr <= radius) {
x0 *= zoom;
double x = cosa * x0 + sina * y0 + cx;
double y = -sina * x0 + cosa * y0 + cy;
/* render it */
/* render it */
double xi = Tools.fmod33(x);
double yi = Tools.fmod33(y);
if ((x < 0.0) || (x > w1) || (y < 0.0) || (y > h1)) {
pPixel.r = pPixel.g = pPixel.b = 0;
} else {
readSrcPixels(x, y);
pPixel.r = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.r) + xi * (srcQ.r)) + yi * ((1.0 - xi) * (srcR.r) + xi * (srcS.r))));
pPixel.g = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.g) + xi * (srcQ.g)) + yi * ((1.0 - xi) * (srcR.g) + xi * (srcS.g))));
pPixel.b = roundColor(((1.0 - yi) * ((1.0 - xi) * (srcP.b) + xi * (srcQ.b)) + yi * ((1.0 - xi) * (srcR.b) + xi * (srcS.b))));
}
img.setRGB(j, i, pPixel.r, pPixel.g, pPixel.b);
}
}
}
applySmoothing(img, smoothing);
}
Aggregations