Files
swt-cctv/src/main/java/io/swtc/proccessing/ImageUtils.java
2026-01-19 12:00:50 +01:00

36 lines
1.2 KiB
Java

package io.swtc.proccessing;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class ImageUtils {
public static int[] getPixels(BufferedImage img) {
return ((DataBufferInt) ensureIntRGB(img).getRaster().getDataBuffer()).getData();
}
public static BufferedImage createFromPixels(int[] pixels, int width, int height) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] dst = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
System.arraycopy(pixels, 0, dst, 0, pixels.length);
return img;
}
public static BufferedImage ensureIntRGB(BufferedImage img) {
if (img.getType() == BufferedImage.TYPE_INT_RGB) {
return img;
}
BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = newImg.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return newImg;
}
public static int clamp(int val) {
if (val < 0) return 0;
if (val > 255) return 255;
return val;
}
}