testing some new stuff!
This commit is contained in:
@@ -3,16 +3,16 @@ package io.swtc.proccessing;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.function.Function;
|
||||
|
||||
/*
|
||||
*
|
||||
* Now here its getting rather interesting! this class processes some
|
||||
* important stuff!
|
||||
*
|
||||
* */
|
||||
|
||||
/**
|
||||
* Enhanced CameraPanel with support for custom image processors
|
||||
*/
|
||||
public class CameraPanel extends JPanel {
|
||||
private BufferedImage currentImage;
|
||||
private BufferedImage processedImage;
|
||||
|
||||
// Basic transform effects
|
||||
private boolean mirror = false;
|
||||
private boolean flip = false;
|
||||
private boolean rotate = false;
|
||||
@@ -21,150 +21,100 @@ public class CameraPanel extends JPanel {
|
||||
private int brightness = 0;
|
||||
private float contrast = 1.0f;
|
||||
|
||||
// Custom processor for advanced effects
|
||||
private Function<BufferedImage, BufferedImage> imageProcessor;
|
||||
|
||||
public CameraPanel() {
|
||||
setBackground(Color.BLACK);
|
||||
setPreferredSize(new Dimension(640, 480));
|
||||
}
|
||||
|
||||
public void setImage(BufferedImage img) {
|
||||
this.currentImage = img;
|
||||
this.repaint();
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public BufferedImage getCurrentImage() {
|
||||
return currentImage;
|
||||
public void setImageProcessor(Function<BufferedImage, BufferedImage> processor) {
|
||||
this.imageProcessor = processor;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public BufferedImage getCurrentProcessedImage() {
|
||||
private void processImage() {
|
||||
if (currentImage == null) {
|
||||
return null;
|
||||
processedImage = null;
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedImage processed = currentImage;
|
||||
BufferedImage result = currentImage;
|
||||
|
||||
// apply color effects
|
||||
if (grayscale || invert || brightness != 0 || contrast != 1.0f) {
|
||||
processed = applyColorEffects(processed);
|
||||
// Apply basic transforms first
|
||||
result = applyBasicEffects(result);
|
||||
|
||||
// Apply custom processor if set
|
||||
if (imageProcessor != null) {
|
||||
result = imageProcessor.apply(result);
|
||||
}
|
||||
|
||||
// apply transform.
|
||||
if (mirror || flip || rotate) {
|
||||
processed = applyTransforms(processed);
|
||||
}
|
||||
|
||||
return processed;
|
||||
processedImage = result;
|
||||
}
|
||||
|
||||
/* Helper Methods ... its the same boilerplate shit over and over again, im sick of this. */
|
||||
public void setMirror(boolean mirror) {
|
||||
this.mirror = mirror;
|
||||
this.repaint();
|
||||
}
|
||||
private BufferedImage applyBasicEffects(BufferedImage img) {
|
||||
int width = img.getWidth();
|
||||
int height = img.getHeight();
|
||||
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
public void setFlip(boolean flip) {
|
||||
this.flip = flip;
|
||||
this.repaint();
|
||||
}
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
// Handle mirror/flip/rotate
|
||||
int srcX = x;
|
||||
int srcY = y;
|
||||
|
||||
public void setRotate(boolean rotate) {
|
||||
this.rotate = rotate;
|
||||
this.repaint();
|
||||
}
|
||||
if (mirror) srcX = width - 1 - x;
|
||||
if (flip) srcY = height - 1 - y;
|
||||
if (rotate) {
|
||||
int temp = srcX;
|
||||
srcX = width - 1 - srcY;
|
||||
srcY = temp;
|
||||
}
|
||||
|
||||
public void setGrayscale(boolean grayscale) {
|
||||
this.grayscale = grayscale;
|
||||
this.repaint();
|
||||
}
|
||||
// Ensure coordinates are in bounds
|
||||
srcX = Math.max(0, Math.min(width - 1, srcX));
|
||||
srcY = Math.max(0, Math.min(height - 1, srcY));
|
||||
|
||||
public void setInvert(boolean invert) {
|
||||
this.invert = invert;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void setBrightness(int brightness) {
|
||||
this.brightness = brightness;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void setContrast(float contrast) {
|
||||
this.contrast = contrast;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void resetEffects() {
|
||||
mirror = flip = rotate = grayscale = invert = false;
|
||||
brightness = 0;
|
||||
contrast = 1.0f;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (currentImage != null) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
|
||||
BufferedImage processedImage = currentImage;
|
||||
|
||||
// effects
|
||||
if (grayscale || invert || brightness != 0 || contrast != 1.0f) {
|
||||
processedImage = applyColorEffects(processedImage);
|
||||
}
|
||||
|
||||
// transforms
|
||||
int w = getWidth(), h = getHeight();
|
||||
|
||||
if (rotate) {
|
||||
g2.translate(w / 2, h / 2);
|
||||
g2.rotate(Math.PI);
|
||||
g2.translate(-w / 2, -h / 2);
|
||||
}
|
||||
|
||||
|
||||
// here we have if, cause we need to do the stuff, yk?
|
||||
if (mirror && flip) {
|
||||
g2.drawImage(processedImage, w, h, -w, -h, null);
|
||||
} else if (mirror) {
|
||||
g2.drawImage(processedImage, w, 0, -w, h, null);
|
||||
} else if (flip) {
|
||||
g2.drawImage(processedImage, 0, h, w, -h, null);
|
||||
} else {
|
||||
g2.drawImage(processedImage, 0, 0, w, h, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private BufferedImage applyColorEffects(BufferedImage img) {
|
||||
BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
for (int y = 0; y < img.getHeight(); y++) {
|
||||
for (int x = 0; x < img.getWidth(); x++) {
|
||||
int rgb = img.getRGB(x, y);
|
||||
int rgb = img.getRGB(srcX, srcY);
|
||||
int r = (rgb >> 16) & 0xFF;
|
||||
int g = (rgb >> 8) & 0xFF;
|
||||
int b = rgb & 0xFF;
|
||||
|
||||
// Apply grayscale
|
||||
if (grayscale) {
|
||||
int gray = (r + g + b) / 3;
|
||||
r = g = b = gray;
|
||||
}
|
||||
|
||||
// this is fun, this regulates the brightness or the contrast!
|
||||
// this is some real java, the other stuff is just UI design, which i am bad at,
|
||||
// but this! This is some real shit
|
||||
r = (int) ((r - 128) * contrast + 128 + brightness);
|
||||
g = (int) ((g - 128) * contrast + 128 + brightness);
|
||||
b = (int) ((b - 128) * contrast + 128 + brightness);
|
||||
// Apply brightness
|
||||
if (brightness != 0) {
|
||||
r = clamp(r + brightness);
|
||||
g = clamp(g + brightness);
|
||||
b = clamp(b + brightness);
|
||||
}
|
||||
|
||||
// invert the colors!
|
||||
// Apply contrast
|
||||
if (contrast != 1.0f) {
|
||||
r = clamp((int)((r - 128) * contrast + 128));
|
||||
g = clamp((int)((g - 128) * contrast + 128));
|
||||
b = clamp((int)((b - 128) * contrast + 128));
|
||||
}
|
||||
|
||||
// Apply invert
|
||||
if (invert) {
|
||||
r = 255 - r;
|
||||
g = 255 - g;
|
||||
b = 255 - b;
|
||||
}
|
||||
|
||||
// clamp so we dont get into weird color grades, or any weird looking spaces
|
||||
// if we dont do this we cant really do stuff which looks bearable
|
||||
r = Math.max(0, Math.min(255, r));
|
||||
g = Math.max(0, Math.min(255, g));
|
||||
b = Math.max(0, Math.min(255, b));
|
||||
|
||||
result.setRGB(x, y, (r << 16) | (g << 8) | b);
|
||||
}
|
||||
}
|
||||
@@ -172,33 +122,95 @@ public class CameraPanel extends JPanel {
|
||||
return result;
|
||||
}
|
||||
|
||||
private BufferedImage applyTransforms(BufferedImage img) {
|
||||
int width = img.getWidth();
|
||||
int height = img.getHeight();
|
||||
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
private int clamp(int value) {
|
||||
return Math.max(0, Math.min(255, value));
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
int sourceX = x;
|
||||
int sourceY = y;
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
if (mirror) {
|
||||
sourceX = width - 1 - x;
|
||||
}
|
||||
if (flip) {
|
||||
sourceY = height - 1 - y;
|
||||
}
|
||||
if (rotate) {
|
||||
int tempX = width - 1 - sourceX;
|
||||
int tempY = height - 1 - sourceY;
|
||||
sourceX = tempX;
|
||||
sourceY = tempY;
|
||||
}
|
||||
if (processedImage != null) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
|
||||
result.setRGB(x, y, img.getRGB(sourceX, sourceY));
|
||||
}
|
||||
// Scale image to fit panel while maintaining aspect ratio
|
||||
int panelWidth = getWidth();
|
||||
int panelHeight = getHeight();
|
||||
int imgWidth = processedImage.getWidth();
|
||||
int imgHeight = processedImage.getHeight();
|
||||
|
||||
double scaleX = (double) panelWidth / imgWidth;
|
||||
double scaleY = (double) panelHeight / imgHeight;
|
||||
double scale = Math.min(scaleX, scaleY);
|
||||
|
||||
int scaledWidth = (int) (imgWidth * scale);
|
||||
int scaledHeight = (int) (imgHeight * scale);
|
||||
|
||||
int x = (panelWidth - scaledWidth) / 2;
|
||||
int y = (panelHeight - scaledHeight) / 2;
|
||||
|
||||
g2d.drawImage(processedImage, x, y, scaledWidth, scaledHeight, null);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
public BufferedImage getCurrentProcessedImage() {
|
||||
return processedImage;
|
||||
}
|
||||
|
||||
// Basic effect setters
|
||||
public void setMirror(boolean mirror) {
|
||||
this.mirror = mirror;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setFlip(boolean flip) {
|
||||
this.flip = flip;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setRotate(boolean rotate) {
|
||||
this.rotate = rotate;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setGrayscale(boolean grayscale) {
|
||||
this.grayscale = grayscale;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setInvert(boolean invert) {
|
||||
this.invert = invert;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setBrightness(int brightness) {
|
||||
this.brightness = brightness;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setContrast(float contrast) {
|
||||
this.contrast = contrast;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void resetEffects() {
|
||||
this.mirror = false;
|
||||
this.flip = false;
|
||||
this.rotate = false;
|
||||
this.grayscale = false;
|
||||
this.invert = false;
|
||||
this.brightness = 0;
|
||||
this.contrast = 1.0f;
|
||||
this.imageProcessor = null;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user