Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalazscs committed Dec 15, 2024
1 parent 40b4711 commit 0f1d610
Show file tree
Hide file tree
Showing 186 changed files with 2,331 additions and 2,391 deletions.
33 changes: 15 additions & 18 deletions src/main/java/com/jhlabs/image/CausticsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.jhlabs.math.Noise;
import pixelitor.ThreadPool;

import java.awt.*;
import java.util.Random;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -215,17 +214,15 @@ public int getBgColor() {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
// sin = (float) Math.sin(0.1);
// cos = (float) Math.cos(0.1);

int outWidth = transformedSpace.width;
int outHeight = transformedSpace.height;
int index = 0;
int[] pixels = new int[outWidth * outHeight];
int[] pixels = new int[width * height];

for (int y = 0; y < outHeight; y++) {
for (int x = 0; x < outWidth; x++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[index++] = bgColor;
}
}
Expand All @@ -238,13 +235,13 @@ protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle tr
float rs = 1.0f / scale;
float d = 0.95f;

pt = createProgressTracker(outHeight);
pt = createProgressTracker(height);

Future<?>[] futures = new Future[outHeight];
for (int y = 0; y < outHeight; y++) {
Future<?>[] futures = new Future[height];
for (int y = 0; y < height; y++) {
int finalY = y;
int finalV = v;
Runnable lineTask = () -> calculateLine(outWidth, outHeight, pixels, finalV, rs, d, finalY);
Runnable lineTask = () -> calculateLine(width, height, pixels, finalV, rs, d, finalY);
futures[y] = ThreadPool.submit(lineTask);
}
ThreadPool.waitFor(futures, pt);
Expand All @@ -254,10 +251,10 @@ protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle tr
return pixels;
}

private void calculateLine(int outWidth, int outHeight, int[] pixels, int v, float rs, float d, int y) {
for (int x = 0; x < outWidth; x++) {
private void calculateLine(int width, int height, int[] pixels, int v, float rs, float d, int y) {
Random random = ThreadLocalRandom.current();
for (int x = 0; x < width; x++) {
for (int s = 0; s < samples; s++) {
Random random = ThreadLocalRandom.current();
float sx = x + random.nextFloat();
float sy = y + random.nextFloat();
float nx = sx * rs;
Expand All @@ -273,9 +270,9 @@ private void calculateLine(int outWidth, int outHeight, int[] pixels, int v, flo
float srcX = sx + scaleXFocusXca * xDisplacement;
float srcY = sy + scaleXFocusXca * yDisplacement;

if (srcX < 0 || srcX >= outWidth - 1 || srcY < 0 || srcY >= outHeight - 1) {
if (srcX < 0 || srcX >= width - 1 || srcY < 0 || srcY >= height - 1) {
} else {
int i = ((int) srcY) * outWidth + (int) srcX;
int i = ((int) srcY) * width + (int) srcX;
int rgb = pixels[i];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
Expand Down Expand Up @@ -303,9 +300,9 @@ private void calculateLine(int outWidth, int outHeight, int[] pixels, int v, flo
float srcX = sx + scale * focus * xDisplacement;
float srcY = sy + scale * focus * yDisplacement;

if (srcX < 0 || srcX >= outWidth - 1 || srcY < 0 || srcY >= outHeight - 1) {
if (srcX < 0 || srcX >= width - 1 || srcY < 0 || srcY >= height - 1) {
} else {
int i = ((int) srcY) * outWidth + (int) srcX;
int i = ((int) srcY) * width + (int) srcX;
int rgb = pixels[i];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jhlabs/image/CellularFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import pixelitor.ThreadPool;
import pixelitor.utils.CachedFloatRandom;

import java.awt.Rectangle;
import java.util.concurrent.Future;

/**
Expand Down Expand Up @@ -591,7 +590,7 @@ public int getPixel(int x, int y, int[] inPixels, int width, int height) {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
// float[] minmax = Noise.findRange(this, null);
// min = minmax[0];
// max = minmax[1];
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jhlabs/image/EdgeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;

/**
* An edge-detection filter.
*/
Expand Down Expand Up @@ -89,7 +87,7 @@ public float[] getHEdgeMatrix() {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
pt = createProgressTracker(height);

int index = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jhlabs/image/EmbossFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;

/**
* A class to emboss an image.
*/
Expand Down Expand Up @@ -65,7 +63,7 @@ public boolean getEmboss() {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
pt = createProgressTracker(height);

int index = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jhlabs/image/MedianFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;

/**
* A filter which performs a 3x3 median operation. Useful for removing dust and noise.
*/
Expand Down Expand Up @@ -68,7 +66,7 @@ private static int rgbMedian(int[] r, int[] g, int[] b) {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int index = 0;
int[] argb = new int[9];
int[] r = new int[9];
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jhlabs/image/OilFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import pixelitor.ThreadPool;

import java.awt.*;
import java.util.concurrent.Future;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ public int getLevels() {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int[] outPixels = new int[width * height];

pt = createProgressTracker(height);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jhlabs/image/PlasmaFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import pixelitor.utils.ProgressTracker;

import java.awt.Rectangle;
import java.util.SplittableRandom;

public class PlasmaFilter extends WholeImageFilter {
Expand Down Expand Up @@ -242,7 +241,7 @@ private boolean doPlasma(int x1, int y1, int x2, int y2, int[] pixels, int strid
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int[] outPixels = new int[width * height];

if (width == 1 && height == 1) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jhlabs/image/QuantizeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;

/**
* A filter which quantizes an image to a set number of colors - useful for producing
* images which are to be encoded using an index color model. The filter can perform
Expand Down Expand Up @@ -194,7 +192,7 @@ public void quantize(int[] inPixels, int[] outPixels, int width, int height, int
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int[] outPixels = new int[width * height];

quantize(inPixels, outPixels, width, height, numColors, dither, serpentine);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jhlabs/image/ReduceNoiseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;

/**
* A filter which performs reduces noise by looking at each pixel's 8 neighbours, and if it's a minimum or maximum,
* replacing it by the next minimum or maximum of the neighbours.
Expand Down Expand Up @@ -52,7 +50,7 @@ private static int smooth(int[] v) {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int index = 0;
int[] r = new int[9];
int[] g = new int[9];
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jhlabs/image/SmearFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import pixelitor.ThreadPool;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -98,7 +97,7 @@ public float getMix() {
}

@Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
protected int[] filterPixels(int width, int height, int[] inPixels) {
int[] outPixels = new int[width * height];

int i = 0;
Expand Down
38 changes: 7 additions & 31 deletions src/main/java/com/jhlabs/image/WholeImageFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.jhlabs.image;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

Expand All @@ -25,16 +24,6 @@
* to do their stuff.
*/
public abstract class WholeImageFilter extends AbstractBufferedImageOp {
/**
* The output image bounds.
*/
protected Rectangle transformedSpace;

/**
* The input image bounds.
*/
protected Rectangle originalSpace;

/**
* Construct a WholeImageFilter.
*/
Expand All @@ -49,42 +38,29 @@ public BufferedImage filter(BufferedImage src, BufferedImage dst) {
// int type = src.getType();
// WritableRaster srcRaster = src.getRaster();

originalSpace = new Rectangle(0, 0, width, height);
transformedSpace = new Rectangle(0, 0, width, height);
transformSpace(transformedSpace);

if (dst == null) {
ColorModel dstCM = src.getColorModel();
dst = new BufferedImage(dstCM, dstCM
.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM
.createCompatibleWritableRaster(width, height), dstCM
.isAlphaPremultiplied(), null);
}
// WritableRaster dstRaster = dst.getRaster();

int[] inPixels = getRGB(src, 0, 0, width, height, null);
inPixels = filterPixels(width, height, inPixels, transformedSpace);
setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);
inPixels = filterPixels(width, height, inPixels);
setRGB(dst, 0, 0, width, height, inPixels);

return dst;
}

/**
* Calculate output bounds for given input bounds.
*
* @param rect input and output rectangle
*/
protected void transformSpace(Rectangle rect) {
}

/**
* Actually filter the pixels.
*
* @param width the image width
* @param height the image height
* @param inPixels the image pixels
* @param transformedSpace the output bounds
* @param width the image width
* @param height the image height
* @param inPixels the image pixels
* @return the output pixels
*/
protected abstract int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace);
protected abstract int[] filterPixels(int width, int height, int[] inPixels);
}

Loading

0 comments on commit 0f1d610

Please sign in to comment.