70 lines
2.6 KiB
Java
70 lines
2.6 KiB
Java
package io.swtc.proccessing;
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
public class SharpnessProccessor {
|
|
|
|
public int[] process(int[] srcPixels, int width, int height, float amount, boolean edgeEnhance) {
|
|
if (amount == 0 && !edgeEnhance) return srcPixels;
|
|
|
|
int[] dstPixels = new int[srcPixels.length];
|
|
|
|
// Normalization setup
|
|
float centerWeight = edgeEnhance ? 5f : 9f;
|
|
float neighborWeight = -1f;
|
|
float strength = (amount / 100f);
|
|
// Adjust strength scaling to match your original "amount - 1 / 8f" logic if needed,
|
|
// but typically sharpness is 0.0 to 1.0.
|
|
// Adapting to your specific previous math:
|
|
float weightFactor = (amount / 100f - 1) / 8f;
|
|
|
|
// Parallel loop skipping borders
|
|
IntStream.range(1, height - 1).parallel().forEach(y -> {
|
|
int yOffset = y * width;
|
|
for (int x = 1; x < width - 1; x++) {
|
|
int i = yOffset + x;
|
|
|
|
float rAcc = 0, gAcc = 0, bAcc = 0;
|
|
|
|
// Center
|
|
int pC = srcPixels[i];
|
|
float wC = centerWeight * weightFactor + 1.0f;
|
|
rAcc += ((pC >> 16) & 0xFF) * wC;
|
|
gAcc += ((pC >> 8) & 0xFF) * wC;
|
|
bAcc += (pC & 0xFF) * wC;
|
|
|
|
// Neighbors (North, South, East, West)
|
|
int[] neighbors = {
|
|
srcPixels[i - width], srcPixels[i + width],
|
|
srcPixels[i - 1], srcPixels[i + 1]
|
|
};
|
|
|
|
float wN = neighborWeight * weightFactor;
|
|
for(int p : neighbors) {
|
|
rAcc += ((p >> 16) & 0xFF) * wN;
|
|
gAcc += ((p >> 8) & 0xFF) * wN;
|
|
bAcc += (p & 0xFF) * wN;
|
|
}
|
|
|
|
// Diagonals (only if not edge enhance mode, per your original code)
|
|
if (!edgeEnhance) {
|
|
int[] diags = {
|
|
srcPixels[i - width - 1], srcPixels[i - width + 1],
|
|
srcPixels[i + width - 1], srcPixels[i + width + 1]
|
|
};
|
|
for(int p : diags) {
|
|
rAcc += ((p >> 16) & 0xFF) * wN;
|
|
gAcc += ((p >> 8) & 0xFF) * wN;
|
|
bAcc += (p & 0xFF) * wN;
|
|
}
|
|
}
|
|
|
|
dstPixels[i] = (ImageUtils.clamp((int)rAcc) << 16) |
|
|
(ImageUtils.clamp((int)gAcc) << 8) |
|
|
ImageUtils.clamp((int)bAcc);
|
|
}
|
|
});
|
|
|
|
return dstPixels;
|
|
}
|
|
} |