DSL for Clojure Image Synthesis

I am experimenting with creating a small library / DSL for image synthesis in Clojure. Basically, the idea is to allow library users to compose sets of mathematical functions for procedurally creating interesting images.

Functions must work with double values ​​and take the form of converting a location vector to a color value, for example. (x, y, z) β†’ (r, g, b, a)

However, I came across several interesting design decisions:

  • Inputs can have 1,2,3 or maybe even 4 dimensions (x, y, z plus time)
  • It would be nice to provide vector mathematical operations (point products, addition, multiplication, etc.).
  • It would be useful to compose functions with operations such as rotation, scaling, etc.
  • For performance reasons, it is important to use primitive double maths at all stages (i.e. avoid creating separate double blocks separately). Thus, a function that should return red, green, and blue components might need to be three separate functions that return primitive red, green, and blue values, respectively.

Any ideas on how this kind of DSL can be reasonably achieved in Clojure (1.4 beta)?

+6
source share
2 answers

OK, so I finally figured out a good way to do this.

The trick was to represent functions as a code vector (in the sense of β€œcode - data,” for example

[(Math/sin (* 10 x)) (Math/cos (* 12 y)) (Math/cos (+ (* 5 x) (* 8 y)))] 

Then it can be β€œcompiled” to create 3 objects that implement the Java interface, with the following method:

 public double calc(double x, double y, double z, double t) { ..... } 

And these functional objects can be called using primitive values ​​to get the values ​​of red, green and blue for each pixel. The results look something like this:

enter image description here

Finally, it is possible to compose functions using a simple DSL, for example. to increase the texture you can do:

 (vscale 10 some-function-vector) 

I have published all the code on GitHub for anyone interested in this:

https://github.com/mikera/clisk

0
source

Check out ImageMagick's awesome tools at http://www.imagemagick.org to give you an idea of ​​what operations you can expect from such a library.

You may see that you will not need to flush vector math if you replicate the default IM toolkit.

+1
source

Source: https://habr.com/ru/post/915760/


All Articles