Skip to content

Instantly share code, notes, and snippets.

@Computeiful
Created February 14, 2021 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Computeiful/ae856786bd4b587a16b175d82e139c24 to your computer and use it in GitHub Desktop.
Save Computeiful/ae856786bd4b587a16b175d82e139c24 to your computer and use it in GitHub Desktop.
Represent an RGB image as a float array
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Image {
public static Image read(String name) throws IOException {
return new Image(ImageIO.read(new File(name)));
}
public static void save(Image image, String name) throws IOException {
ImageIO.write(image.toBufferedImage(), "png", new File(name));
}
private final float[] image;
private final int w, h;
public Image(int w, int h) {
this.w = w;
this.h = h;
this.image = new float[w * h * 3];
}
public Image(BufferedImage image) {
this(image.getWidth(), image.getHeight());
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
setRGB(x, y, image.getRGB(x, y));
}
}
}
public Image(int w, int h, float[] image) {
this.w = w;
this.h = h;
this.image = image;
}
private int indexOf(int x, int y) {
return ((w * y) + x) * 3;
}
public void setRGB(int x, int y, final int RGB) {
float R = (((RGB >> 16) & 0xFF) / 255.0f);
float G = (((RGB >> 8) & 0xFF) / 255.0f);
float B = ((RGB & 0xFF) / 255.0f);
setR(x, y, R);
setG(x, y, G);
setB(x, y, B);
}
private void set(int index, float n) {
this.image[index] = n;
}
public void setR(int x, int y, float n) {
set(indexOf(x, y), n);
}
public void setG(int x, int y, float n) {
set(indexOf(x, y) + 1, n);
}
public void setB(int x, int y, float n) {
set(indexOf(x, y) + 2, n);
}
public int getRGB(int x, int y) {
int R = (int) (getR(x, y) * 255.0f);
int G = (int) (getG(x, y) * 255.0f);
int B = (int) (getB(x, y) * 255.0f);
return 0xFF000000 | (R << 16) | (G << 8) | B;
}
private float get(int index) {
return this.image[index];
}
public float getR(int x, int y) {
return get(indexOf(x, y));
}
public float getG(int x, int y) {
return get(indexOf(x, y) + 1);
}
public float getB(int x, int y) {
return get(indexOf(x, y) + 2);
}
public int getW() {
return w;
}
public int getH() {
return h;
}
public int getSize() {
return (w * h);
}
public float[] asArray() {
return image;
}
public BufferedImage toBufferedImage() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
image.setRGB(x, y, this.getRGB(x, y));
}
}
return image;
}
public float diff(final Image that) {
final int w = Math.min(this.getW(), that.getW());
final int h = Math.min(this.getH(), that.getH());
return diff(that, 0, 0, w, h);
}
public float diff(final Image that, int x, int y, int w, int h) {
final int size = (w * h);
float s = 0;
for(int yy = 0; yy < h; yy++) {
for(int xx = 0; xx < w; xx++) {
final float d = this.diff(that, x + xx, y + yy);
s += (d * d);
}
}
return (float) Math.sqrt(s / size);
}
private float diff(final Image that, int x, int y) {
final float R = this.getR(x, y) - that.getR(x, y);
final float G = this.getG(x, y) - that.getG(x, y);
final float B = this.getB(x, y) - that.getB(x, y);
final float s = (R * R) + (G * G) + (B * B);
return (float) Math.sqrt(s / 3.0f); // RMSE
}
}
@DoogeTube
Copy link

download this, it makes such beautiful looks on the screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment