Skip to content

Instantly share code, notes, and snippets.

@JoJoDeveloping
Created June 18, 2019 23:48
Show Gist options
  • Save JoJoDeveloping/011307e635cc8e1a6d8de6fa356f3ca3 to your computer and use it in GitHub Desktop.
Save JoJoDeveloping/011307e635cc8e1a6d8de6fa356f3ca3 to your computer and use it in GitHub Desktop.
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageCombiner {
public static void main(String[] args) throws IOException {
if(args.length < 2){
System.out.println("Usage: java -jar <jar> src out -s");
}
boolean flip = args.length == 3 && args[2].equals("-s");
BufferedImage im = ImageIO.read(new File(args[0]));
if(im.getWidth()!=im.getHeight())
throw new IllegalArgumentException("Image must be quadratic");
BufferedImage res = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_ARGB);
for(int y = 0; y < res.getWidth(); y++){
double yd = (y+0.5)/res.getWidth();
for(int x = 0; x < res.getHeight(); x++){
Color c=null;
double xd = (x+0.5)/res.getHeight();
if(xd <= 1-yd){
double t2l = xd/(xd+yd) - 0.5;
c=colorAverage(4*t2l*t2l*t2l+0.5, im.getRGB(x,y), im.getRGB(flip?im.getWidth()-y:y,x));//im.getRGB(flip?im.getWidth()-y:y,x));
}else{
double r2b = (1-xd)/(2-xd-yd) - 0.5;
c=colorAverage(4*r2b*r2b*r2b+0.5, im.getRGB(x,y), im.getRGB(flip?im.getWidth()-y:y,x));//im.getRGB(x,y));
}
res.setRGB(x,y,c.getRGB());
}
}
File of = new File(args[1]);
String[] ss = of.getName().split(".");
String s = ss.length != 0 ? ss[ss.length-1].toLowerCase():"png";
ImageIO.write(res, s, of);
System.out.println("Wrote "+s+" to "+of.getAbsolutePath());
}
public static Color colorAverage(double mix, int rgb1, int rgb2){
Color c1 = new Color(rgb1), c2 = new Color(rgb2);
double l = mix, r=1-mix;
return new Color((int)Math.sqrt(l*c1.getRed()*c1.getRed()+r*c2.getRed()*c2.getRed()),
(int)Math.sqrt(l*c1.getGreen()*c1.getGreen()+r*c2.getGreen()*c2.getGreen()),
(int)Math.sqrt(l*c1.getBlue()*c1.getBlue()+r*c2.getBlue()*c2.getBlue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment