protected void performPixelTransformation(WFImage pImg) {
SimpleImage img = (SimpleImage) pImg;
int width = pImg.getImageWidth();
int height = pImg.getImageHeight();
final double brMAX = 256.0;
Pixel pixel = new Pixel();
/* saturation */
if (this.saturation != 0) {
int rs = 2990;
int gs = 5880;
int bs = 1130;
rs = (rs * Tools.VPREC) / 10000;
gs = (gs * Tools.VPREC) / 10000;
bs = (bs * Tools.VPREC) / 10000;
int scl = (int) ((double) this.saturation / 255.0 * 1024.0 + 0.5);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
int avg = (rs * pixel.r + gs * pixel.g + bs * pixel.b) >> Tools.SPREC;
pixel.r += ((pixel.r - avg) * scl) >> Tools.SPREC;
if (pixel.r < 0)
pixel.r = 0;
else if (pixel.r > 255)
pixel.r = 255;
pixel.g += ((pixel.g - avg) * scl) >> Tools.SPREC;
if (pixel.g < 0)
pixel.g = 0;
else if (pixel.g > 255)
pixel.g = 255;
pixel.b += ((pixel.b - avg) * scl) >> Tools.SPREC;
if (pixel.b < 0)
pixel.b = 0;
else if (pixel.b > 255)
pixel.b = 255;
img.setRGB(j, i, pixel);
}
}
}
/* gamma */
if (this.gamma != 0) {
double max = 255.0;
double g = (double) 512.0 / (512.0 + (double) this.gamma);
int gamma[] = new int[256];
double da, aa;
da = aa = 1.0 / 255.0;
gamma[0] = 0;
for (int i = 1; i < 256; i++) {
int val = (int) (max * Math.pow(aa, g) + 0.5);
aa += da;
gamma[i] = val;
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
pixel.r = gamma[pixel.r];
pixel.g = gamma[pixel.g];
pixel.b = gamma[pixel.b];
img.setRGB(j, i, pixel);
}
}
}
/* red */
if (this.red != 0) {
int tt = (int) ((double) this.red / (double) brMAX * (double) 255.0 + 0.5);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
pixel.r += tt;
if (pixel.r < 0)
pixel.r = 0;
else if (pixel.r > 255)
pixel.r = 255;
img.setRGB(j, i, pixel);
}
}
}
/* green */
if (this.green != 0) {
int tt = (int) ((double) this.green / (double) brMAX * (double) 255.0 + 0.5);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
pixel.g += tt;
if (pixel.g < 0)
pixel.g = 0;
else if (pixel.g > 255)
pixel.g = 255;
img.setRGB(j, i, pixel);
}
}
}
/* blue */
if (this.blue != 0) {
int tt = (int) ((double) this.blue / (double) brMAX * (double) 255.0 + 0.5);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
pixel.b += tt;
if (pixel.b < 0)
pixel.b = 0;
else if (pixel.b > 255)
pixel.b = 255;
img.setRGB(j, i, pixel);
}
}
}
/* brightness */
if (brightness != 0) {
int tt = (int) ((double) this.brightness / (double) brMAX * (double) 255.0 + 0.5);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
pixel.r += tt;
if (pixel.r < 0)
pixel.r = 0;
else if (pixel.r > 255)
pixel.r = 255;
pixel.g += tt;
if (pixel.g < 0)
pixel.g = 0;
else if (pixel.g > 255)
pixel.g = 255;
pixel.b += tt;
if (pixel.b < 0)
pixel.b = 0;
else if (pixel.b > 255)
pixel.b = 255;
img.setRGB(j, i, pixel);
}
}
}
/* contrast */
if (this.contrast != 0) {
double scale = (double) this.contrast / brMAX;
int sc = (int) (scale * (double) Tools.VPREC + 0.5);
int dc;
if (this.contrast > 0) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
dc = (int) (((int) (pixel.r - (int) 127) * sc) >> Tools.SPREC);
if (dc < (-255))
dc = (-255);
else if (dc > 255)
dc = 255;
pixel.r += dc;
if (pixel.r < 0)
pixel.r = 0;
else if (pixel.r > 255)
pixel.r = 255;
dc = (int) (((int) (pixel.g - (int) 127) * sc) >> Tools.SPREC);
if (dc < (-255))
dc = (-255);
else if (dc > 255)
dc = 255;
pixel.g += dc;
if (pixel.g < 0)
pixel.g = 0;
else if (pixel.g > 255)
pixel.g = 255;
dc = (int) ((int) ((pixel.b - (int) 127) * sc) >> Tools.SPREC);
if (dc < (-255))
dc = (-255);
else if (dc > 255)
dc = 255;
pixel.b += dc;
if (pixel.b < 0)
pixel.b = 0;
else if (pixel.b > 255)
pixel.b = 255;
img.setRGB(j, i, pixel);
}
}
}
else {
int val;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixel.setARGBValue(img.getARGBValue(j, i));
dc = (int) (((int) ((int) pixel.r - (int) 127) * sc) >> Tools.SPREC);
if (dc < (-255))
dc = (-255);
else if (dc > 255)
dc = 255;