Skip to content

Instantly share code, notes, and snippets.

View hageldave's full-sized avatar

David Hägele hageldave

  • University of Stuttgart
  • Germany / Stuttgart
View GitHub Profile
@hageldave
hageldave / RGB2XYZ2LAB and back
Last active April 21, 2016 19:08
convsion from RGB to CIE XYZ and further to CIE Lab and all the way back
public static void main(String[] args) throws MalformedURLException, IOException {
// String imgUrl = "http://sipi.usc.edu/database/preview/misc/4.1.07.png";
String imgUrl = "http://sipi.usc.edu/database/preview/misc/boat.512.png";
Img img = Img.createRemoteImg(ImageLoader.loadImage(new URL(imgUrl).openStream(),BufferedImage.TYPE_INT_ARGB));
System.out.println("start");
// to CIE XYZ colorspace (bound to [0..255] missing the factor 1/0.17697)
img.forEachParallel(px->{
px.setARGB( px.a(),
@hageldave
hageldave / scalar field visualization with surface normals
Last active July 8, 2016 15:57
mini shader (scalar field vis)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.function.BiConsumer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public static void main(String[] args) {
Img img = new Img(2048,2048);
img.fill(0xff000000);
Random r = new Random();
img.forEach(px->{px.setG(r.nextInt(256));});
Img copy = img.copy();
int kernelSize = 21;
int minus = kernelSize/2;
int plus = kernelSize-minus;
@hageldave
hageldave / Blending Demo
Created October 2, 2016 15:57
little gui for testing the different blending functions of imagingkit
public static void main(String[] args) throws IOException {
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BorderLayout());
ImagePanel imgpanel = new ImagePanel();
imgpanel.setPreferredSize(new Dimension(200, 200));
JComboBox<Blending> blendselector = new JComboBox<>(Blending.values());
final JLabel renderlabel = new JLabel();
blendselector.setRenderer((list,blend,idx,selected,focused)->{
renderlabel.setText(blend.name());
@hageldave
hageldave / LAB gammut in RGB demo
Created October 3, 2016 22:10
demo app showing the RGB representable colors of LAB
public static void main(String[] args) throws IOException {
Img[] gammut = new Img[256];
Consumer<Pixel> lab2rgb = (px)->{px.setValue(transformLAB2RGB(px.getValue()));};
for(int l = 0; l < 256; l++){
int L = l;
Img img = new Img(256, 256);
img.forEach(px->{px.setRGB(L, px.getX(), px.getY());});
img.forEach(lab2rgb);
static int hotColdMapping(double f){
f*=Math.PI;
if(f < Math.PI/2){
float r = (float) Math.cos(f);
float g = (float) (1-Math.sin(f));
return Pixel.rgb_fromNormalized(r, g, 0);
} else {
float b = (float) -Math.cos(f);
float g = (float) (1-Math.sin(f));
return Pixel.rgb_fromNormalized(0, g, b);
@hageldave
hageldave / gui selfie
Last active November 25, 2016 00:52
make a screenshot of your swing or awt GUI
JFrame frame = new JFrame();
JTextField textField = new JTextField(new File("selfie.png").getAbsolutePath());
JButton btn = new JButton("selfie!");
btn.addActionListener((e)->{
Img img = new Img(frame.getWidth(), frame.getHeight());
img.draw(g->{
frame.paintAll(g);
});
System.out.println("flash!");
try{
@hageldave
hageldave / converted pixel streaming
Last active December 22, 2016 23:46
convert pixel to anything else e.g float[] and stream it
public static void main(String[] args) {
Img img = new Img(200,200);
img.fill(0xff000000);
Spliterator<float[]> split = new ConvertedPixelSpliterator<>(
img.spliterator(),
()->{return new float[6];},
testmain::pixel2vec,
testmain::vec2Pixel);
@hageldave
hageldave / Pixel with cached x,y (not beneficial)
Created January 18, 2017 15:49
performance test for Pixel with cached x,y values using a box blur with large neighbourhood (51x51 ~ 2600 calls to getX,getY per pixel)
public static void main(String[] args) {
Img img1 = new Img(4000, 4000);
Img img2 = new Img(4000, 4000) {
@Override
public Pixel getPixel() {
return new Pixol(this, 0);
}
@hageldave
hageldave / Slices in Java
Last active January 21, 2017 14:42
wrapper for arrays
package misc;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Slice<T> implements Iterable<Slice.ArrayAccessor<T>>{