clojure2d.extra.glitch

Various glitching pixel filters or functions

Filter

Use following filters with filter-channels function.

  • Slitscan - x/y slitscan simulation based on wave functions
  • Shift-channels - just shift channels
  • Mirror - mirror image along different axes
  • Slitscan2 - slitscan simulation based on vector fields
  • Fold - apply vector field on the image
  • Pix2line - convert pixel into horizontal line

Machines

Short sketches operating on images/pixels.

  • Blend - compose two images in glitchy way

All filters are equiped with random configuration generator.

Code snippets

Save glitch

(defn save-result
  [f params & opts]
  (let [n (str "images/glitch/" (first opts) ".jpg")]
    (save f (str "docs/" n))
    (str "../" n)))

blend-machine

(blend-machine p1 p2)(blend-machine {:keys [switch? in1-cs in2-cs out-cs in1-to? in2-to? out-to? blend-ch1 blend-ch2 blend-ch3]} p1 p2)

Blend two Pixels based on configuration.

The idea is to compose channels separately in different color spaces.

Full flow does following steps:

  • convert inputs to (or from) selected color spaces
  • compose each channel separately using different method
  • convert output to (or from) selected color space

Parametrization:

  • :switch? - reverse input order
  • :in1-cs - color space for first image
  • :in2-cs - color space for second image
  • :out-cs - color space for output
  • :in1-to?, :in2-to?, :cs-to? - which conversion to select: to color space or from color space
  • :blend-ch1, :blend-ch2, :blend-ch3 - blend methods for each channel

Examples

Usage

(save-result (let [blend-conf {:blend-ch3 :hardmix,
                               :out-cs :Cubehelix,
                               :blend-ch2 :mlinearburn,
                               :switch? true,
                               :out-to? true,
                               :in1-cs :LUV,
                               :in1-to? true,
                               :in2-cs :XYZ,
                               :blend-ch1 :mdodge,
                               :in2-to? true}
                   p2l-conf {:nx 9,
                             :ny 3,
                             :scale 4.2,
                             :tolerance 37,
                             :nseed -1,
                             :whole true,
                             :shiftx 0.66,
                             :shifty 0.46}
                   p2 (p/filter-channels (pix2line p2l-conf) samurai)]
               (blend-machine blend-conf samurai p2))
             ...)

blend-machine-random-config

(blend-machine-random-config)

Random configuration for blend machine.

Examples

Usage

(blend-machine-random-config)
;;=> {:blend-ch1 :hardlight,
;;=>  :blend-ch2 :subtract,
;;=>  :blend-ch3 nil,
;;=>  :in1-cs :Gray,
;;=>  :in1-to? false,
;;=>  :in2-cs :Gray,
;;=>  :in2-to? false,
;;=>  :out-cs :Gray,
;;=>  :out-to? true,
;;=>  :switch? false}

fold

(fold {:keys [fields r]})(fold)

Folding filter based on vector fields.

Parameters:

  • :fields - vector fields configuration combine
  • :r - range value 1.0-3.0

Examples

Fold with random config

(save-result (p/filter-channels (fold) samurai) ...)

Provided config

(save-result
 (let [conf
       {:fields
        {:type :operation,
         :name :comp,
         :var1 {:type :variation,
                :name :pressure-wave,
                :amount 1.0,
                :config {:x-freq -0.27, :y-freq 5.92}},
         :var2
         {:type :variation, :name :besselj, :amount 1.0, :config {}},
         :amount 1.0},
        :r 4.0}]
   (p/filter-channels (fold conf) samurai))
 ...)

Provided config with different range value for each channel

(save-result
 (let [conf
       {:fields {:type :operation,
                 :name :comp,
                 :var1 {:type :variation,
                        :name :perlin2,
                        :amount 1.0,
                        :config {:seed 112, :scale -1.13, :octaves 4}},
                 :var2 {:type :variation,
                        :name :exponential,
                        :amount 1.0,
                        :config {}},
                 :amount 1.0},
        :r 2.0}]
   (p/filter-channels (fold (assoc conf :r 1.9))
                      (fold (assoc conf :r 2.0))
                      (fold (assoc conf :r 2.1))
                      nil
                      samurai))
 ...)

fold-random-config

Generate random configuration for vector fields slitscan.

  • r - field range (default 2.0)
  • d - fields configuration depth (default up to 3)

Examples

Usage

(fold-random-config)
;;=> {:fields
;;=>  {:amount 1.0,
;;=>   :name :comp,
;;=>   :type :operation,
;;=>   :var1 {:amount 1.0,
;;=>          :name :mult,
;;=>          :type :operation,
;;=>          :var1 {:amount -0.9565860811257152,
;;=>                 :name :add,
;;=>                 :type :operation,
;;=>                 :var1 {:amount 1.0054301219249075,
;;=>                        :config {},
;;=>                        :name :horseshoe,
;;=>                        :type :variation},
;;=>                 :var2 {:amount 0.7534850633473344,
;;=>                        :config {:line-up -0.5451656020068334,
;;=>                                 :pull -0.05994811514933285,
;;=>                                 :rotate 0.9873312807549997,
;;=>                                 :x 0.39938905673443736,
;;=>                                 :y 0.7597915833340503},
;;=>                        :name :kaleidoscope,
;;=>                        :type :variation}},
;;=>          :var2 {:amount 1.337984743682349,
;;=>                 :config {:space 1.5380896037698109,
;;=>                          :warp -1.244241226134092},
;;=>                 :name :stripes,
;;=>                 :type :variation}},
;;=>   :var2 {:amount 1.0, :config {}, :name :sinh, :type :variation}},
;;=>  :r 2.0}
(fold-random-config 3.1234)
;;=> {:fields
;;=>  {:amount 1.0,
;;=>   :name :comp,
;;=>   :type :operation,
;;=>   :var1 {:amount 1.0,
;;=>          :name :angles,
;;=>          :type :operation,
;;=>          :var1 {:amount 1.0,
;;=>                 :name :add,
;;=>                 :type :operation,
;;=>                 :var1 {:amount -0.5531213501491528,
;;=>                        :config {:amplitude -0.2923679032044754,
;;=>                                 :damping 0.0,
;;=>                                 :frequency-x -4.972665560331895,
;;=>                                 :frequency-y 4.249042452003008,
;;=>                                 :perturbation 1.5889373764439743,
;;=>                                 :separation 0.3817182731405231},
;;=>                        :name :oscilloscope2,
;;=>                        :type :variation},
;;=>                 :var2 {:amount 1.0864151559946866,
;;=>                        :config {:a 1.0957089672048363,
;;=>                                 :b -1.3255456393562028,
;;=>                                 :c -0.9483900016810627,
;;=>                                 :d 1.4616609210124647,
;;=>                                 :e -1.0882991818870866,
;;=>                                 :f 0.30890934577622486,
;;=>                                 :g 2.7849355720128504,
;;=>                                 :h -2.2341224405490308},
;;=>                        :name :rational3,
;;=>                        :type :variation}},
;;=>          :var2
;;=>          {:amount 1.0, :config {}, :name :disc, :type :variation}},
;;=>   :var2 {:amount 1.0, :config {}, :name :arcsech2, :type :variation}},
;;=>  :r 3.1234}
(fold-random-config 3.1234 0)
;;=> {:fields {:amount 1.0, :config {}, :name :cross, :type :variation},
;;=>  :r 3.1234}

imgslicer

(imgslicer pixels {:keys [distance threshold min-size mode], :or {distance v/dist, threshold 200, min-size 500, mode :color}, :as options})

mirror

(mirror t)(mirror)

Mirror image for given (or random) mirroring functions.

Examples

Random mirror

(save-result (p/filter-channels (mirror) samurai) ...)

Mirror R(ight) to left

(save-result (p/filter-channels (mirror :R) samurai) ...)

Mirror each channel separately

(save-result
 (p/filter-channels (mirror :L) (mirror :R) (mirror :U) nil samurai)
 ...)

mirror-random-config

(mirror-random-config)

Generate random mirroring functions.

Examples

Usage

(mirror-random-config)
;;=> :RDL
(mirror-random-config)
;;=> :DL2
(mirror-random-config)
;;=> :RDL

mirror-types

Map of names and mirroring functions

Examples

List of mirror function keys

(sort (keys mirror-types))
;;=> (:D :DL
;;=>     :DL2 :DR
;;=>     :DR2 :L
;;=>     :R :RDL
;;=>     :RDR :RUL
;;=>     :RUR :SDL
;;=>     :SDL2 :SDR
;;=>     :SDR2 :SUL
;;=>     :SUL2 :SUR
;;=>     :SUR2 :U
;;=>     :UL :UL2
;;=>     :UR :UR2)

pix2line

(pix2line)(pix2line {:keys [tolerance whole], :as config})

Pix2line effect. Convert pixels to lines.

Parametrization:

  • :nx, :ny - grid size
  • :scale - grid scaling factor
  • :tolerance - factor which regulates when start new line
  • :nseed - noise seed
  • :whole - skip lines or not
  • :shiftx, :shifty - noise shift

Examples

Random pix2line

(save-result (p/filter-channels (pix2line) samurai) ...)

With configuration, slightly modified for each channel

(save-result
 (let [conf {:nx 9,
             :ny 3,
             :scale 4.2,
             :tolerance 37,
             :nseed -1,
             :whole true,
             :shiftx 0.66,
             :shifty 0.46}]
   (p/filter-channels (pix2line conf)
                      (pix2line (assoc conf :scale 1))
                      (pix2line (assoc conf :scale 10 :tolerance 100))
                      nil
                      samurai))
 ...)

pix2line-random-config

(pix2line-random-config)

Make random config for pix2line.

Examples

Usage

(pix2line-random-config)
;;=> {:nseed -104559432,
;;=>  :nx 19,
;;=>  :ny 27,
;;=>  :scale 4.827626098091167,
;;=>  :shiftx 0.16595269492440257,
;;=>  :shifty 0.8787030351906517,
;;=>  :tolerance 17,
;;=>  :whole true}

shift-channels

(shift-channels)(shift-channels {:keys [x-shift y-shift], :or {x-shift 0.05, y-shift -0.05}})

Shift channels by given amount.

Parameters:

  • :x-shift - shift amount along x axis
  • :y-shift - shift amount along y axis

Examples

Shift red and blue channels

(save-result
 (p/filter-channels (shift-channels) nil (shift-channels) nil samurai)
 ...)

Shift hue and saturation channels

(save-result (let [filt (shift-channels {:x-shift -0.2, :y-shift 1.0})]
               (->> samurai
                    (p/filter-colors c/to-HSV*)
                    (p/filter-channels filt filt nil nil)
                    (p/filter-colors c/from-HSV*)))
             ...)

shift-channels-random-config

(shift-channels-random-config)(shift-channels-random-config spread)

Random shift values along x and y axes.

Optionally provide spread parameter to define maximum shift value.

Examples

Usage

(shift-channels-random-config)
;;=> {:x-shift -0.038265490877159095, :y-shift 0.09319188874560025}
(shift-channels-random-config 100)
;;=> {:x-shift -49.676209433839766, :y-shift -70.77799115763057}

slitscan

(slitscan)(slitscan {:keys [x y], :or {x [(make-random-wave)], y [(make-random-wave)]}})

Create slitscan filter funtion.

Config is a map each axis has it’s own list of maps defining waves. Each map contains:

  • :wave - oscillator name (see oscillators.
  • :freq - wave frequency
  • :amp - wave amplitude
  • :phase - wave phase (0-1).

Examples

Random slitscan

(save-result (p/filter-channels (slitscan) samurai) ...)

Slitscan with parameters

(save-result
 (p/filter-channels
  (slitscan {:x [{:wave :triangle, :freq 4, :amp 0.25, :phase 0.8}],
             :y [{:wave :sin, :freq 2, :amp 0.5, :phase 0.5}]})
  samurai)
 ...)

slitscan-random-config

(slitscan-random-config nx ny)(slitscan-random-config)

Create list of random waves for each axis separately.

Optionally you can pass number of waves to create for each axis.

Examples

Usage

(slitscan-random-config)
;;=> {:x
;;=>  ({:amp 0.25, :freq 4, :phase 0.4723322408370296, :wave :cut-triangle}
;;=>   {:amp 1.0, :freq 1, :phase 0.6781403854759053, :wave :square}
;;=>   {:amp 0.125, :freq 8, :phase 0.2842156593790839, :wave :square}
;;=>   {:amp 0.0625, :freq 16, :phase 0.3104935143630404, :wave :sin}),
;;=>  :y ({:amp 1.0, :freq 1, :phase 0.5188947439945767, :wave :saw}
;;=>      {:amp 1.0, :freq 1, :phase 0.15233255014353309, :wave :saw}
;;=>      {:amp 0.0078125, :freq 128, :phase 0.781138428970248, :wave :saw}
;;=>      {:amp 0.5, :freq 2, :phase 0.799247357791214, :wave :constant}
;;=>      {:amp 0.125, :freq 8, :phase 0.850275448299315, :wave :triangle})}
(slitscan-random-config 1 1)
;;=> {:x
;;=>  ({:amp 0.5, :freq 2, :phase 0.3666151313886057, :wave :cut-triangle}),
;;=>  :y
;;=>  ({:amp 0.5, :freq 2, :phase 0.1734761065837901, :wave :cut-triangle})}

slitscan2

(slitscan2 {:keys [fields r], :or {r 2.0, fields (var/random-configuration 0)}})(slitscan2)

Slitscan filter based on vector fields.

Parameters:

  • :fields - vector fields configuration combine
  • :r - range value 1.0-3.0

Examples

Slitscan with random config

(save-result (p/filter-channels (slitscan2) samurai) ...)

Provided config

(save-result
 (let [conf
       {:fields
        {:type :operation,
         :name :comp,
         :var1 {:type :variation,
                :name :pressure-wave,
                :amount 1.0,
                :config {:x-freq -0.27, :y-freq 5.92}},
         :var2
         {:type :variation, :name :besselj, :amount 1.0, :config {}},
         :amount 1.0},
        :r 4.0}]
   (p/filter-channels (slitscan2 conf) samurai))
 ...)

Provided config with different range value for each channel

(save-result
 (let [conf
       {:fields {:type :operation,
                 :name :comp,
                 :var1 {:type :variation,
                        :name :perlin2,
                        :amount 1.0,
                        :config {:seed 112, :scale -1.13, :octaves 4}},
                 :var2 {:type :variation,
                        :name :exponential,
                        :amount 1.0,
                        :config {}},
                 :amount 1.0},
        :r 2.0}]
   (p/filter-channels (slitscan2 (assoc conf :r 1.9))
                      (slitscan2 (assoc conf :r 2.0))
                      (slitscan2 (assoc conf :r 2.1))
                      nil
                      samurai))
 ...)

slitscan2-random-config

(slitscan2-random-config)(slitscan2-random-config r)(slitscan2-random-config r d)

Generate random configuration for vector fields slitscan.

  • r - field range (default 2.0)
  • d - fields configuration depth (default up to 3)

Examples

Usage

(slitscan2-random-config)
;;=> {:fields
;;=>  {:amount 1.0,
;;=>   :name :comp,
;;=>   :type :operation,
;;=>   :var1 {:amount 1.0,
;;=>          :name :add,
;;=>          :type :operation,
;;=>          :var1 {:amount -0.16158068340884846,
;;=>                 :name :add,
;;=>                 :type :operation,
;;=>                 :var1 {:amount 1.5201449797345497,
;;=>                        :config {:x 1.402412301294639,
;;=>                                 :y 0.7374344086056237},
;;=>                        :name :bent2,
;;=>                        :type :variation},
;;=>                 :var2 {:amount 0.973817987599169,
;;=>                        :config {:ai 1.4882385462604386,
;;=>                                 :ar -3.1499156781719533},
;;=>                        :name :plusrecip,
;;=>                        :type :variation}},
;;=>          :var2 {:amount -1.3946024576513403,
;;=>                 :config {},
;;=>                 :name :ellipticprecision,
;;=>                 :type :variation}},
;;=>   :var2 {:amount 1.0,
;;=>          :config {:x 0.5912859423990514, :y 0.10473447742540525},
;;=>          :name :modulus,
;;=>          :type :variation}},
;;=>  :r 2.0}
(slitscan2-random-config 3.1234)
;;=> {:fields {:amount 1.0,
;;=>           :name :angles,
;;=>           :type :operation,
;;=>           :var1 {:amount 1.0,
;;=>                  :config {:polarweight 1.2052381865090394,
;;=>                           :radius 1.020260533855468,
;;=>                           :s 0.9970439948928231,
;;=>                           :scale -0.13539037630990663,
;;=>                           :t 0.49176952333609997},
;;=>                  :name :octapol,
;;=>                  :type :variation},
;;=>           :var2 {:amount 1.0,
;;=>                  :config {:x -0.42914621055226654,
;;=>                           :y 1.365522836761052},
;;=>                  :name :rectangles,
;;=>                  :type :variation}},
;;=>  :r 3.1234}
(slitscan2-random-config 3.1234 0)
;;=> {:fields {:amount 1.0,
;;=>           :config {:circle -1.1627043560845067,
;;=>                    :sides 3,
;;=>                    :star 0.5283290262036706},
;;=>           :name :loonie2,
;;=>           :type :variation},
;;=>  :r 3.1234}