Geometrize Haxe is a Haxe library for recreating images with geometric primitives
Ce script ne doit pas être installé directement. C'est une librairie destinée à être incluse dans d'autres scripts avec la méta-directive // @require https://update.greasyfork.org/scripts/585407/1866784/Geometrize%20Haxe%20Library.js
(function ($global, worker_code) {
'use strict';
$global['geometrize'] = $global['geometrize'] || {};
$global['geometrize']['bitmap'] = $global['geometrize']['bitmap'] || {};
$global['geometrize']['exporter'] = $global['geometrize']['exporter'] || {};
$global['geometrize']['runner'] = $global['geometrize']['runner'] || {};
$global['geometrize']['shape'] = $global['geometrize']['shape'] || {};
function $extend(from, fields) {
var proto = Object.create(from);
for (var name in fields) proto[name] = fields[name];
if (fields.toString !== Object.prototype.toString) proto.toString = fields.toString;
return proto;
}
function observableArray(onLengthChange, initial = []) {
const array = [...initial];
return new Proxy(array, {
set(target, prop, value) {
const oldLength = target.length;
const result = Reflect.set(target, prop, value);
if (target.length !== oldLength) {
onLengthChange(target.length, oldLength, target);
}
return result;
},
});
}
var CanvasTools = function () {};
CanvasTools.__name__ = true;
CanvasTools.downScaleCanvas = function (cv, scale) {
if (scale <= 0.0 || scale >= 1.0) {
throw haxe_Exception.thrown('Scale must be a positive number < 1');
}
var sqScale = scale * scale;
var sw = cv.width;
var sh = cv.height;
var tw = (sw * scale) | 0;
var th = (sh * scale) | 0;
var sx = 0;
var sy = 0;
var sIndex = 0;
var tx = 0;
var ty = 0;
var yIndex = 0;
var tIndex = 0;
var tX = 0;
var tY = 0;
var w = 0.0;
var nw = 0.0;
var wx = 0.0;
var nwx = 0.0;
var wy = 0.0;
var nwy = 0.0;
var crossX = false;
var crossY = false;
var sBuffer = cv.getContext('2d').getImageData(0, 0, sw, sh).data;
var this1 = new Float32Array(3 * tw * th);
var tBuffer = this1;
var sR = 0.0;
var sG = 0.0;
var sB = 0.0;
while (sy < sh) {
ty = (sy * scale) | 0;
tY = ty | 0;
yIndex = (3 * tY * tw) | 0;
crossY = tY != ((ty + scale) | 0);
if (crossY) {
wy = tY + 1 - ty;
nwy = ty + scale - tY - 1;
}
sx = 0;
while (sx < sw) {
tx = (sx * scale) | 0;
tX = tx | 0;
tIndex = (yIndex + tX * 3) | 0;
crossX = tX != Math.floor(tx + scale);
if (crossX) {
wx = tX + 1 - tx;
nwx = (tx + scale - tX - 1) | 0;
}
sR = sBuffer[sIndex];
sG = sBuffer[sIndex + 1];
sB = sBuffer[sIndex + 2];
if (!crossX && !crossY) {
tBuffer[tIndex] += sR * sqScale;
tBuffer[tIndex + 1] += sG * sqScale;
tBuffer[tIndex + 2] += sB * sqScale;
} else if (crossX && !crossY) {
w = wx * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
nw = nwx * scale;
tBuffer[tIndex + 3] += sR * nw;
tBuffer[tIndex + 4] += sG * nw;
tBuffer[tIndex + 5] += sB * nw;
} else if (crossY && !crossX) {
w = wy * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
nw = nwy * scale;
tBuffer[tIndex + 3 * tw] += sR * nw;
tBuffer[tIndex + 3 * tw + 1] += sG * nw;
tBuffer[tIndex + 3 * tw + 2] += sB * nw;
} else {
w = wx * wy;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
nw = nwx * wy;
tBuffer[tIndex + 3] += sR * nw;
tBuffer[tIndex + 4] += sG * nw;
tBuffer[tIndex + 5] += sB * nw;
nw = wx * nwy;
tBuffer[tIndex + 3 * tw] += sR * nw;
tBuffer[tIndex + 3 * tw + 1] += sG * nw;
tBuffer[tIndex + 3 * tw + 2] += sB * nw;
nw = nwx * nwy;
tBuffer[tIndex + 3 * tw + 3] += sR * nw;
tBuffer[tIndex + 3 * tw + 4] += sG * nw;
tBuffer[tIndex + 3 * tw + 5] += sB * nw;
}
sIndex += 4;
++sx;
}
++sy;
}
var result = window.document.createElement('canvas');
result.width = tw;
result.height = th;
var resultContext = result.getContext('2d');
var resultImage = resultContext.getImageData(0, 0, tw, th);
var tByteBuffer = resultImage.data;
var pxIndex = 0;
sIndex = 0;
tIndex = 0;
while (pxIndex < tw * th) {
tByteBuffer[tIndex] = Math.ceil(tBuffer[sIndex]);
tByteBuffer[tIndex + 1] = Math.ceil(tBuffer[sIndex + 1]);
tByteBuffer[tIndex + 2] = Math.ceil(tBuffer[sIndex + 2]);
tByteBuffer[tIndex + 3] = 255;
sIndex += 3;
tIndex += 4;
++pxIndex;
}
resultContext.putImageData(resultImage, 0, 0);
return result;
};
var GeometrizeWorkerInterface = function () {
var _gthis = this;
this.worker = new Worker(window.URL.createObjectURL(new Blob([worker_code], { type: 'text/javascript' })));
this.worker.onmessage = function (message) {
_gthis.onMessage(message.data);
};
};
GeometrizeWorkerInterface.__name__ = true;
GeometrizeWorkerInterface.prototype = {
postMessage: function (message) {
this.worker.postMessage(message);
},
terminate: function () {
this.worker.terminate();
},
onMessage: function (message) {},
};
var HxOverrides = function () {};
HxOverrides.__name__ = true;
HxOverrides.remove = function (a, obj) {
var i = a.indexOf(obj);
if (i == -1) {
return false;
}
a.splice(i, 1);
return true;
};
HxOverrides.now = function () {
return Date.now();
};
var Main = function () {
this.targetImage = null;
this.shapeJsonData = observableArray((newLength, oldLength, array) => {
window.dispatchEvent(new CustomEvent('geometrize-shape-json-data-changed', { detail: { newLength, oldLength, array } }));
});
this.shapeSvgData = [];
this.shapeMutationsPerStep = 100;
this.candidateShapesPerStep = 50;
this.initialBackgroundOpacity = 255;
this.shapeOpacity = 255;
this.shapeTypes = geometrize_ArraySet.create([5]);
this.maxInputImageSize = 768;
this.onWindowLoaded();
};
Main.__name__ = true;
Main.main = function () {
var main = new Main();
$global['geometrize']['main'] = main;
$global['geometrize']['getShapeJsonData'] = function () {
return main.shapeJsonData.slice();
};
$global['geometrize']['getShapeSvgData'] = function () {
return main.shapeSvgData.slice();
};
$global['geometrize']['getShapeCount'] = function () {
return main.get_shapeCount();
};
$global['geometrize']['setTargetCanvas'] = function (canvas) {
main.targetImage = main.canvasToBitmap(canvas);
main.onTargetImageChanged();
};
$global['geometrize']['setMaxShapeCount'] = function (limit) {
return main.set_maxShapeCountLimit(limit);
};
};
Main.prototype = {
get_shapeCount: function () {
return this.shapeSvgData.length;
},
get_maxShapeCountLimit: function () {
var text = Main.maxShapesCapTextEdit.value;
var value = Std.parseInt(text);
if (value != null) {
return value;
}
return 3000;
},
set_maxShapeCountLimit: function (limit) {
Main.maxShapesCapTextEdit.value = limit == null ? 'null' : '' + limit;
return limit;
},
onWindowLoaded: function () {
Main.circlesCheckbox.checked = true;
Main.linesCheckbox.checked = true;
var _gthis = this;
var _gthis1 = this;
Main.runPauseButton.addEventListener(
'click',
function () {
_gthis1.set_running(!_gthis1.running);
},
false
);
Main.openImageFileInput.addEventListener(
'change',
function (e) {
if (Main.openImageFileInput.files == null || Main.openImageFileInput.files.length == 0) {
return;
}
var file = Main.openImageFileInput.files[0];
var fileReader = new FileReader();
fileReader.onload = function (e) {
var image = new Image();
image.onload = function (e) {
var canvas = _gthis1.imageToCanvas(image);
while (canvas.width > _gthis1.maxInputImageSize || canvas.height > _gthis1.maxInputImageSize)
canvas = CanvasTools.downScaleCanvas(canvas, 0.5);
_gthis1.targetImage = _gthis1.canvasToBitmap(canvas);
_gthis1.onTargetImageChanged();
};
image.src = fileReader.result;
};
fileReader.readAsDataURL(file);
Main.openImageFileInput.files[0] = null;
},
false
);
Main.stepButton.addEventListener(
'click',
function () {
_gthis1.stepRunner();
},
false
);
Main.resetButton.addEventListener(
'click',
function () {
_gthis1.targetImage = _gthis1.targetImage;
_gthis1.shapeJsonData.length = 0;
_gthis1.shapeSvgData = [];
_gthis1.onTargetImageChanged();
},
false
);
var saveBlob = function (data, dataType, filename, anchor) {
var blob = new Blob([data], { type: dataType });
var navigator = window.navigator;
if (navigator.msSaveBlob != null) {
navigator.msSaveBlob(blob, filename);
} else {
var dataUrl = URL.createObjectURL(blob);
anchor.download = filename;
anchor.href = dataUrl;
}
};
var setShapeOption = function (option, enable) {
if (enable) {
geometrize_ArraySet.add(_gthis1.shapeTypes, option);
} else {
HxOverrides.remove(_gthis1.shapeTypes, option);
}
};
Main.circlesCheckbox.addEventListener(
'click',
function () {
setShapeOption(5, Main.circlesCheckbox.checked);
},
false
);
Main.linesCheckbox.addEventListener(
'click',
function () {
setShapeOption(6, Main.linesCheckbox.checked);
},
false
);
this.set_maxShapeCountLimit(2000);
if (this.worker != null) {
this.worker.terminate();
}
this.worker = new GeometrizeWorkerInterface();
this.worker.onMessage = $bind(this, this.onWorkerMessageReceived);
this.set_running(false);
},
checkStopConditions: function () {
if (this.get_shapeCount() >= this.get_maxShapeCountLimit()) {
this.set_running(false);
}
},
stepRunner: function () {
var options = {
shapeTypes: this.shapeTypes.length == 0 ? [2] : geometrize_ArraySet.toArray(this.shapeTypes),
alpha: this.shapeOpacity | 0,
candidateShapesPerStep: this.candidateShapesPerStep,
shapeMutationsPerStep: this.shapeMutationsPerStep,
};
this.worker.postMessage({ id: 'should_step', data: options });
},
onWorkerMessageReceived: function (message) {
switch (message.id) {
case 'did_set_target_image':
break;
case 'did_step':
this.shapeJsonData.push(message.jsonData);
this.appendSvgShapeData(message.svgData);
this.checkStopConditions();
break;
}
if (this.running) {
this.stepRunner();
}
},
appendSvgShapeData: function (data) {
this.shapeSvgData.push(data);
var tmp = this.get_shapeCount();
Main.shapesAddedText.innerHTML = Std.string(tmp);
var data = this.makeSvgData();
this.setSvgElement(data);
},
setSvgElement: function (svgCode) {
Main.currentSvgContainer.innerHTML = svgCode;
},
canvasToBitmap: function (canvas) {
var context = canvas.getContext('2d', null);
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
var bytesData = new haxe_io_Bytes(new ArrayBuffer(imageData.data.length));
var _g = 0;
var _g1 = bytesData.length;
while (_g < _g1) {
var i = _g++;
bytesData.b[i] = imageData.data[i];
}
var w = canvas.width;
var h = canvas.height;
var bitmap = new geometrize_bitmap_Bitmap();
if (bytesData == null) {
throw haxe_Exception.thrown('FAIL: bytes != null');
}
var actual = bytesData.length;
var expected = w * h * 4;
if (actual != expected) {
throw haxe_Exception.thrown('FAIL: values are not equal (expected: ' + expected + ', actual: ' + actual + ')');
}
bitmap.width = w;
bitmap.height = h;
var this1 = new Array((bytesData.length / 4) | 0);
bitmap.data = this1;
var i = 0;
var x = 0;
while (i < bytesData.length) {
var red = bytesData.b[i];
var green = bytesData.b[i + 1];
var blue = bytesData.b[i + 2];
var alpha = bytesData.b[i + 3];
bitmap.data[x] =
((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
i += 4;
++x;
}
var bitmap1 = bitmap;
return bitmap1;
},
imageToCanvas: function (image) {
var canvas = window.document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d', null);
context.drawImage(image, 0, 0);
return canvas;
},
onTargetImageChanged: function () {
this.shapeJsonData = [];
this.shapeSvgData = [];
Main.shapesAddedText.innerHTML = '0';
Main.currentSvgContainer.innerHTML = '';
if (this.worker != null) {
this.worker.terminate();
}
this.worker = new GeometrizeWorkerInterface();
this.worker.onMessage = $bind(this, this.onWorkerMessageReceived);
this.worker.postMessage({
id: 'should_set_target_image',
data: this.targetImage,
});
if (this.running) {
this.stepRunner();
}
},
makeSvgData: function () {
return (
geometrize_exporter_SvgExporter.getSvgPrelude() +
geometrize_exporter_SvgExporter.getSvgNodeOpen(this.targetImage.width, this.targetImage.height) +
Std.string(this.shapeSvgData) +
geometrize_exporter_SvgExporter.getSvgNodeClose()
);
},
set_running: function (running) {
Main.runPauseButton.innerHTML = running ? 'Pause' : 'Run';
var wasRunning = this.running;
this.running = running;
if (!wasRunning && this.running) {
this.stepRunner();
}
return this.running;
},
};
Math.__name__ = true;
var Std = function () {};
Std.__name__ = true;
Std.string = function (s) {
return js_Boot.__string_rec(s, '');
};
Std.parseInt = function (x) {
if (x != null) {
var _g = 0;
var _g1 = x.length;
while (_g < _g1) {
var i = _g++;
var c = x.charCodeAt(i);
if (c <= 8 || (c >= 14 && c != 32 && c != 45)) {
var nc = x.charCodeAt(i + 1);
var v = parseInt(x, nc == 120 || nc == 88 ? 16 : 10);
if (isNaN(v)) {
return null;
} else {
return v;
}
}
}
}
return null;
};
Std.random = function (x) {
if (x <= 0) {
return 0;
} else {
return Math.floor(Math.random() * x);
}
};
var StringTools = function () {};
StringTools.__name__ = true;
StringTools.replace = function (s, sub, by) {
return s.split(sub).join(by);
};
var UInt = {};
UInt.toFloat = function (this1) {
var int = this1;
if (int < 0) {
return 4294967296.0 + int;
} else {
return int + 0.0;
}
};
var geometrize_ArraySet = {};
geometrize_ArraySet.create = function (array) {
if (array == null) {
var this1 = [];
return this1;
}
return geometrize_ArraySet.toSet(array);
};
geometrize_ArraySet.add = function (this1, element) {
if (element == null) {
throw haxe_Exception.thrown('FAIL: element != null');
}
if (geometrize_ArraySet.contains(this1, element)) {
return false;
}
this1.push(element);
return true;
};
geometrize_ArraySet.contains = function (this1, element) {
var _g = 0;
while (_g < this1.length) {
var i = this1[_g];
++_g;
if (i == element) {
return true;
}
}
return false;
};
geometrize_ArraySet.toArray = function (this1) {
return this1.slice();
};
geometrize_ArraySet.toSet = function (array) {
var this1 = [];
var set = this1;
var _g = 0;
while (_g < array.length) {
var v = array[_g];
++_g;
geometrize_ArraySet.add(set, v);
}
return set;
};
var geometrize_Core = function () {};
geometrize_Core.__name__ = true;
geometrize_Core.computeColor = function (target, current, lines, alpha) {
if (target == null) {
throw haxe_Exception.thrown('FAIL: target != null');
}
if (current == null) {
throw haxe_Exception.thrown('FAIL: current != null');
}
if (lines == null) {
throw haxe_Exception.thrown('FAIL: lines != null');
}
if (alpha < 0) {
throw haxe_Exception.thrown('FAIL: alpha >= 0');
}
var totalRed = 0;
var totalGreen = 0;
var totalBlue = 0;
var count = 0;
var f = 65535 / alpha;
var a = f | 0;
var _g = 0;
while (_g < lines.length) {
var line = lines[_g];
++_g;
var y = line.y;
var _g1 = line.x1;
var _g2 = line.x2 + 1;
while (_g1 < _g2) {
var x = _g1++;
var t = target.data[target.width * y + x];
var c = current.data[current.width * y + x];
totalRed += (((t >> 24) & 255) - ((c >> 24) & 255)) * a + ((c >> 24) & 255) * 257;
totalGreen += (((t >> 16) & 255) - ((c >> 16) & 255)) * a + ((c >> 16) & 255) * 257;
totalBlue += (((t >> 8) & 255) - ((c >> 8) & 255)) * a + ((c >> 8) & 255) * 257;
++count;
}
}
if (count == 0) {
return 0;
}
var value = ((totalRed / count) | 0) >> 8;
var r = value < 0 ? 0 : value > 255 ? 255 : value;
var value = ((totalGreen / count) | 0) >> 8;
var g = value < 0 ? 0 : value > 255 ? 255 : value;
var value = ((totalBlue / count) | 0) >> 8;
var b = value < 0 ? 0 : value > 255 ? 255 : value;
return (
((r < 0 ? 0 : r > 255 ? 255 : r) << 24) +
((g < 0 ? 0 : g > 255 ? 255 : g) << 16) +
((b < 0 ? 0 : b > 255 ? 255 : b) << 8) +
(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha)
);
};
geometrize_Core.differenceFull = function (first, second) {
if (first == null) {
throw haxe_Exception.thrown('FAIL: first != null');
}
if (second == null) {
throw haxe_Exception.thrown('FAIL: second != null');
}
if (first.width == 0) {
throw haxe_Exception.thrown('FAIL: first.width != 0');
}
if (first.height == 0) {
throw haxe_Exception.thrown('FAIL: first.height != 0');
}
if (second.width == 0) {
throw haxe_Exception.thrown('FAIL: second.width != 0');
}
if (second.height == 0) {
throw haxe_Exception.thrown('FAIL: second.height != 0');
}
var actual = first.width;
var expected = second.width;
if (actual != expected) {
throw haxe_Exception.thrown('FAIL: values are not equal (expected: ' + expected + ', actual: ' + actual + ')');
}
var actual = first.height;
var expected = second.height;
if (actual != expected) {
throw haxe_Exception.thrown('FAIL: values are not equal (expected: ' + expected + ', actual: ' + actual + ')');
}
var total = 0;
var width = first.width;
var height = first.height;
var _g = 0;
var _g1 = height;
while (_g < _g1) {
var y = _g++;
var _g2 = 0;
var _g3 = width;
while (_g2 < _g3) {
var x = _g2++;
var f = first.data[first.width * y + x];
var s = second.data[second.width * y + x];
var dr = ((f >> 24) & 255) - ((s >> 24) & 255);
var dg = ((f >> 16) & 255) - ((s >> 16) & 255);
var db = ((f >> 8) & 255) - ((s >> 8) & 255);
var da = (f & 255) - (s & 255);
total += dr * dr + dg * dg + db * db + da * da;
}
}
var result = Math.sqrt(total / (width * height * 4.0)) / 255;
if (!isFinite(result)) {
throw haxe_Exception.thrown('FAIL: Math.isFinite(result)');
}
return result;
};
geometrize_Core.differencePartial = function (target, before, after, score, lines) {
if (target == null) {
throw haxe_Exception.thrown('FAIL: target != null');
}
if (before == null) {
throw haxe_Exception.thrown('FAIL: before != null');
}
if (after == null) {
throw haxe_Exception.thrown('FAIL: after != null');
}
if (lines == null) {
throw haxe_Exception.thrown('FAIL: lines != null');
}
if (lines.length == 0) {
throw haxe_Exception.thrown('FAIL: lines.length != 0');
}
var width = target.width;
var height = target.height;
var rgbaCount = width * height * 4;
var total = Math.pow(score * 255, 2) * rgbaCount;
var _g = 0;
while (_g < lines.length) {
var line = lines[_g];
++_g;
var y = line.y;
var _g1 = line.x1;
var _g2 = line.x2 + 1;
while (_g1 < _g2) {
var x = _g1++;
var t = target.data[target.width * y + x];
var b = before.data[before.width * y + x];
var a = after.data[after.width * y + x];
var dtbr = ((t >> 24) & 255) - ((b >> 24) & 255);
var dtbg = ((t >> 16) & 255) - ((b >> 16) & 255);
var dtbb = ((t >> 8) & 255) - ((b >> 8) & 255);
var dtba = (t & 255) - (b & 255);
var dtar = ((t >> 24) & 255) - ((a >> 24) & 255);
var dtag = ((t >> 16) & 255) - ((a >> 16) & 255);
var dtab = ((t >> 8) & 255) - ((a >> 8) & 255);
var dtaa = (t & 255) - (a & 255);
total -= dtbr * dtbr + dtbg * dtbg + dtbb * dtbb + dtba * dtba;
total += dtar * dtar + dtag * dtag + dtab * dtab + dtaa * dtaa;
}
}
var result = Math.sqrt(total / rgbaCount) / 255;
if (!isFinite(result)) {
throw haxe_Exception.thrown('FAIL: Math.isFinite(result)');
}
return result;
};
geometrize_Core.bestRandomState = function (shapes, alpha, n, target, current, buffer, lastScore) {
var bestEnergy = 0;
var bestState = null;
var _g = 0;
var _g1 = n;
while (_g < _g1) {
var i = _g++;
var state = new geometrize_State(
geometrize_shape_ShapeFactory.randomShapeOf(shapes, current.width, current.height),
alpha,
target,
current,
buffer
);
var energy = state.energy(lastScore);
if (i == 0 || energy < bestEnergy) {
bestEnergy = energy;
bestState = state;
}
}
return bestState;
};
geometrize_Core.bestHillClimbState = function (shapes, alpha, n, age, target, current, buffer, lastScore) {
var state = geometrize_Core.bestRandomState(shapes, alpha, n, target, current, buffer, lastScore);
state = geometrize_Core.hillClimb(state, age, lastScore);
return state;
};
geometrize_Core.hillClimb = function (state, maxAge, lastScore) {
if (state == null) {
throw haxe_Exception.thrown('FAIL: state != null');
}
if (maxAge < 0) {
throw haxe_Exception.thrown('FAIL: maxAge >= 0');
}
var state1 = state.clone();
var bestState = state1.clone();
var bestEnergy = state1.energy(lastScore);
var age = 0;
while (age < maxAge) {
var undo = state1.mutate();
var energy = state1.energy(lastScore);
if (energy >= bestEnergy) {
state1 = undo;
} else {
bestEnergy = energy;
bestState = state1.clone();
age = -1;
}
++age;
}
return bestState;
};
geometrize_Core.energy = function (shape, alpha, target, current, buffer, score) {
if (shape == null) {
throw haxe_Exception.thrown('FAIL: shape != null');
}
if (target == null) {
throw haxe_Exception.thrown('FAIL: target != null');
}
if (current == null) {
throw haxe_Exception.thrown('FAIL: current != null');
}
if (buffer == null) {
throw haxe_Exception.thrown('FAIL: buffer != null');
}
var lines = shape.rasterize();
if (lines == null) {
throw haxe_Exception.thrown('FAIL: lines != null');
}
if (lines.length == 0) {
throw haxe_Exception.thrown('FAIL: lines.length != 0');
}
var color = geometrize_Core.computeColor(target, current, lines, alpha);
geometrize_rasterizer_Rasterizer.copyLines(buffer, current, lines);
geometrize_rasterizer_Rasterizer.drawLines(buffer, color, lines);
return geometrize_Core.differencePartial(target, current, buffer, score, lines);
};
var geometrize_Model = function (target, backgroundColor) {
if (target == null) {
throw haxe_Exception.thrown('FAIL: target != null');
}
this.width = target.width;
this.height = target.height;
this.target = target;
var w = target.width;
var h = target.height;
var bitmap = new geometrize_bitmap_Bitmap();
bitmap.width = w;
bitmap.height = h;
var this1 = new Array(w * h);
bitmap.data = this1;
var i = 0;
while (i < bitmap.data.length) {
bitmap.data[i] = backgroundColor;
++i;
}
this.current = bitmap;
var w = target.width;
var h = target.height;
var bitmap = new geometrize_bitmap_Bitmap();
bitmap.width = w;
bitmap.height = h;
var this1 = new Array(w * h);
bitmap.data = this1;
var i = 0;
while (i < bitmap.data.length) {
bitmap.data[i] = backgroundColor;
++i;
}
this.buffer = bitmap;
this.score = geometrize_Core.differenceFull(target, this.current);
};
geometrize_Model.__name__ = true;
geometrize_Model.prototype = {
step: function (shapeTypes, alpha, n, age) {
var state = geometrize_Core.bestHillClimbState(
shapeTypes,
alpha,
n,
age,
this.target,
this.current,
this.buffer,
this.score
);
var results = [this.addShape(state.shape, state.alpha)];
return results;
},
addShape: function (shape, alpha) {
if (shape == null) {
throw haxe_Exception.thrown('FAIL: shape != null');
}
var _this = this.current;
var bitmap = new geometrize_bitmap_Bitmap();
bitmap.width = _this.width;
bitmap.height = _this.height;
var this1 = new Array(_this.data.length);
bitmap.data = this1;
var _g = 0;
var _g1 = _this.data.length;
while (_g < _g1) {
var i = _g++;
bitmap.data[i] = _this.data[i];
}
var before = bitmap;
var lines = shape.rasterize();
var color = geometrize_Core.computeColor(this.target, this.current, lines, alpha);
geometrize_rasterizer_Rasterizer.drawLines(this.current, color, lines);
this.score = geometrize_Core.differencePartial(this.target, before, this.current, this.score, lines);
var result = { score: this.score, color: color, shape: shape };
return result;
},
};
var geometrize_State = function (shape, alpha, target, current, buffer) {
if (shape == null) {
throw haxe_Exception.thrown('FAIL: shape != null');
}
this.shape = shape;
this.alpha = alpha;
this.score = -1;
this.target = target;
this.current = current;
this.buffer = buffer;
};
geometrize_State.__name__ = true;
geometrize_State.prototype = {
energy: function (lastScore) {
if (this.score < 0) {
this.score = geometrize_Core.energy(this.shape, this.alpha, this.target, this.current, this.buffer, lastScore);
}
return this.score;
},
mutate: function () {
var oldState = this.clone();
this.shape.mutate();
return oldState;
},
clone: function () {
return new geometrize_State(this.shape.clone(), this.alpha, this.target, this.current, this.buffer);
},
};
var geometrize_Util = function () {};
geometrize_Util.__name__ = true;
geometrize_Util.getAverageImageColor = function (image, alpha) {
if (alpha == null) {
alpha = 255;
}
if (image == null) {
throw haxe_Exception.thrown('FAIL: image != null');
}
var totalRed = 0;
var totalGreen = 0;
var totalBlue = 0;
var _g = 0;
var _g1 = image.width;
while (_g < _g1) {
var x = _g++;
var _g2 = 0;
var _g3 = image.height;
while (_g2 < _g3) {
var y = _g2++;
var pixel = image.data[image.width * y + x];
totalRed += (pixel >> 24) & 255;
totalGreen += (pixel >> 16) & 255;
totalBlue += (pixel >> 8) & 255;
}
}
var size = image.width * image.height;
var red = (totalRed / size) | 0;
var green = (totalGreen / size) | 0;
var blue = (totalBlue / size) | 0;
return (
((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha)
);
};
var geometrize_bitmap_Bitmap = ($global['geometrize']['bitmap']['Bitmap'] = function () {});
geometrize_bitmap_Bitmap.__name__ = true;
geometrize_bitmap_Bitmap.create = function (w, h, color) {
var bitmap = new geometrize_bitmap_Bitmap();
bitmap.width = w;
bitmap.height = h;
var this1 = new Array(w * h);
bitmap.data = this1;
var i = 0;
while (i < bitmap.data.length) {
bitmap.data[i] = color;
++i;
}
return bitmap;
};
geometrize_bitmap_Bitmap.createFromBytes = function (w, h, bytes) {
var bitmap = new geometrize_bitmap_Bitmap();
if (bytes == null) {
throw haxe_Exception.thrown('FAIL: bytes != null');
}
var actual = bytes.length;
var expected = w * h * 4;
if (actual != expected) {
throw haxe_Exception.thrown('FAIL: values are not equal (expected: ' + expected + ', actual: ' + actual + ')');
}
bitmap.width = w;
bitmap.height = h;
var this1 = new Array((bytes.length / 4) | 0);
bitmap.data = this1;
var i = 0;
var x = 0;
while (i < bytes.length) {
var red = bytes.b[i];
var green = bytes.b[i + 1];
var blue = bytes.b[i + 2];
var alpha = bytes.b[i + 3];
bitmap.data[x] =
((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
i += 4;
++x;
}
return bitmap;
};
geometrize_bitmap_Bitmap.createFromByteArray = function (w, h, bytes) {
var data = new haxe_io_Bytes(new ArrayBuffer(bytes.length));
var i = 0;
while (i < bytes.length) {
data.b[i] = bytes[i];
++i;
}
var bitmap = new geometrize_bitmap_Bitmap();
if (data == null) {
throw haxe_Exception.thrown('FAIL: bytes != null');
}
var actual = data.length;
var expected = w * h * 4;
if (actual != expected) {
throw haxe_Exception.thrown('FAIL: values are not equal (expected: ' + expected + ', actual: ' + actual + ')');
}
bitmap.width = w;
bitmap.height = h;
var this1 = new Array((data.length / 4) | 0);
bitmap.data = this1;
var i = 0;
var x = 0;
while (i < data.length) {
var red = data.b[i];
var green = data.b[i + 1];
var blue = data.b[i + 2];
var alpha = data.b[i + 3];
bitmap.data[x] =
((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
i += 4;
++x;
}
return bitmap;
};
geometrize_bitmap_Bitmap.prototype = {
getPixel: function (x, y) {
return this.data[this.width * y + x];
},
setPixel: function (x, y, color) {
this.data[this.width * y + x] = color;
},
clone: function () {
var bitmap = new geometrize_bitmap_Bitmap();
bitmap.width = this.width;
bitmap.height = this.height;
var this1 = new Array(this.data.length);
bitmap.data = this1;
var _g = 0;
var _g1 = this.data.length;
while (_g < _g1) {
var i = _g++;
bitmap.data[i] = this.data[i];
}
return bitmap;
},
fill: function (color) {
var idx = 0;
while (idx < this.data.length) {
this.data[idx] = (color >> 24) & 255;
this.data[idx + 1] = (color >> 16) & 255;
this.data[idx + 2] = (color >> 8) & 255;
this.data[idx + 3] = color & 255;
idx += 4;
}
},
getBytes: function () {
var bytes = new haxe_io_Bytes(new ArrayBuffer(this.data.length * 4));
var i = 0;
while (i < this.data.length) {
var idx = i * 4;
bytes.b[idx] = (this.data[i] >> 24) & 255;
bytes.b[idx + 1] = (this.data[i] >> 16) & 255;
bytes.b[idx + 2] = (this.data[i] >> 8) & 255;
bytes.b[idx + 3] = this.data[i] & 255;
++i;
}
return bytes;
},
};
var geometrize_exporter_ShapeJsonExporter = ($global['geometrize']['exporter']['ShapeJsonExporter'] = function () {});
geometrize_exporter_ShapeJsonExporter.__name__ = true;
geometrize_exporter_ShapeJsonExporter.export = function (shapes) {
return '[\n' + geometrize_exporter_ShapeJsonExporter.exportShapes(shapes) + '\n]';
};
geometrize_exporter_ShapeJsonExporter.exportShapes = function (shapes) {
var results = '';
var _g = 0;
var _g1 = shapes.length;
while (_g < _g1) {
var i = _g++;
results += geometrize_exporter_ShapeJsonExporter.exportShape(shapes[i]);
if (i != shapes.length - 1) {
results += ',\n';
}
}
return results;
};
geometrize_exporter_ShapeJsonExporter.exportShape = function (shape) {
var result = ' {\n';
var type = shape.shape.getType();
var data = shape.shape.getRawShapeData();
var color = shape.color;
var score = shape.score;
result += ' "type":' + type + ',\n';
result += ' "data":' + '[';
var _g = 0;
var _g1 = data.length;
while (_g < _g1) {
var item = _g++;
result += data[item];
if (item <= data.length - 2) {
result += ',';
}
}
result += '],\n';
result += ' "color":' + '[';
result += ((color >> 24) & 255) + ',';
result += ((color >> 16) & 255) + ',';
result += ((color >> 8) & 255) + ',';
result += color & 255;
result += '],\n';
result += ' "score":' + score + '\n';
result += ' }';
return result;
};
var geometrize_exporter_SvgExporter = ($global['geometrize']['exporter']['SvgExporter'] = function () {});
geometrize_exporter_SvgExporter.__name__ = true;
geometrize_exporter_SvgExporter.export = function (shapes, width, height) {
var results = geometrize_exporter_SvgExporter.getSvgPrelude();
results += geometrize_exporter_SvgExporter.getSvgNodeOpen(width, height);
results += geometrize_exporter_SvgExporter.exportShapes(shapes);
results += geometrize_exporter_SvgExporter.getSvgNodeClose();
return results;
};
geometrize_exporter_SvgExporter.exportShapes = function (shapes) {
var results = '';
var _g = 0;
var _g1 = shapes.length;
while (_g < _g1) {
var i = _g++;
results += geometrize_exporter_SvgExporter.exportShape(shapes[i]);
if (i != shapes.length - 1) {
results += '\n';
}
}
return results;
};
geometrize_exporter_SvgExporter.exportShape = function (shape) {
return StringTools.replace(
shape.shape.getSvgShapeData(),
geometrize_exporter_SvgExporter.SVG_STYLE_HOOK,
geometrize_exporter_SvgExporter.stylesForShape(shape)
);
};
geometrize_exporter_SvgExporter.getSvgPrelude = function () {
return '<?xml version="1.0" standalone="no"?>\n';
};
geometrize_exporter_SvgExporter.getSvgNodeOpen = function (width, height) {
return (
'<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="' +
width +
'" height="' +
height +
'">\n'
);
};
geometrize_exporter_SvgExporter.getSvgNodeClose = function () {
return '</svg>';
};
geometrize_exporter_SvgExporter.stylesForShape = function (shape) {
switch (shape.shape.getType()) {
case 6:
case 7:
return (
geometrize_exporter_SvgExporter.strokeForColor(shape.color) +
' stroke-width="1" fill="none" ' +
geometrize_exporter_SvgExporter.strokeOpacityForAlpha(shape.color & 255)
);
default:
return (
geometrize_exporter_SvgExporter.fillForColor(shape.color) +
' ' +
geometrize_exporter_SvgExporter.fillOpacityForAlpha(shape.color & 255)
);
}
};
geometrize_exporter_SvgExporter.rgbForColor = function (color) {
return 'rgb(' + ((color >> 24) & 255) + ',' + ((color >> 16) & 255) + ',' + ((color >> 8) & 255) + ')';
};
geometrize_exporter_SvgExporter.strokeForColor = function (color) {
return 'stroke="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
};
geometrize_exporter_SvgExporter.fillForColor = function (color) {
return 'fill="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
};
geometrize_exporter_SvgExporter.fillOpacityForAlpha = function (alpha) {
return 'fill-opacity="' + alpha / 255.0 + '"';
};
geometrize_exporter_SvgExporter.strokeOpacityForAlpha = function (alpha) {
return 'stroke-opacity="' + alpha / 255.0 + '"';
};
var geometrize_rasterizer_Rasterizer = function () {};
geometrize_rasterizer_Rasterizer.__name__ = true;
geometrize_rasterizer_Rasterizer.drawLines = function (image, c, lines) {
if (image == null) {
throw haxe_Exception.thrown('FAIL: image != null');
}
if (lines == null) {
throw haxe_Exception.thrown('FAIL: lines != null');
}
var sr = (c >> 24) & 255;
sr |= sr << 8;
sr *= c & 255;
sr = (sr / 255) | 0;
var sg = (c >> 16) & 255;
sg |= sg << 8;
sg *= c & 255;
sg = (sg / 255) | 0;
var sb = (c >> 8) & 255;
sb |= sb << 8;
sb *= c & 255;
sb = (sb / 255) | 0;
var sa = c & 255;
sa |= sa << 8;
var _g = 0;
while (_g < lines.length) {
var line = lines[_g];
++_g;
var y = line.y;
var ma = 65535;
var m = 65535;
var as = (m - sa * (ma / m)) * 257;
var a = as | 0;
var _g1 = line.x1;
var _g2 = line.x2 + 1;
while (_g1 < _g2) {
var x = _g1++;
var d = image.data[image.width * y + x];
var dr = (d >> 24) & 255;
var dg = (d >> 16) & 255;
var db = (d >> 8) & 255;
var da = d & 255;
var r = ((UInt.toFloat(dr * a + sr * ma) / UInt.toFloat(m)) | 0) >> 8;
var g = ((UInt.toFloat(dg * a + sg * ma) / UInt.toFloat(m)) | 0) >> 8;
var b = ((UInt.toFloat(db * a + sb * ma) / UInt.toFloat(m)) | 0) >> 8;
var a1 = ((UInt.toFloat(da * a + sa * ma) / UInt.toFloat(m)) | 0) >> 8;
image.data[image.width * y + x] =
((r < 0 ? 0 : r > 255 ? 255 : r) << 24) +
((g < 0 ? 0 : g > 255 ? 255 : g) << 16) +
((b < 0 ? 0 : b > 255 ? 255 : b) << 8) +
(a1 < 0 ? 0 : a1 > 255 ? 255 : a1);
}
}
};
geometrize_rasterizer_Rasterizer.copyLines = function (destination, source, lines) {
if (destination == null) {
throw haxe_Exception.thrown('FAIL: destination != null');
}
if (source == null) {
throw haxe_Exception.thrown('FAIL: source != null');
}
if (lines == null) {
throw haxe_Exception.thrown('FAIL: lines != null');
}
var _g = 0;
while (_g < lines.length) {
var line = lines[_g];
++_g;
var y = line.y;
var _g1 = line.x1;
var _g2 = line.x2 + 1;
while (_g1 < _g2) {
var x = _g1++;
destination.data[destination.width * y + x] = source.data[source.width * y + x];
}
}
};
geometrize_rasterizer_Rasterizer.bresenham = function (x1, y1, x2, y2) {
var dx = x2 - x1;
var ix = (dx > 0 ? 1 : 0) - (dx < 0 ? 1 : 0);
dx = (dx < 0 ? -dx : dx) << 1;
var dy = y2 - y1;
var iy = (dy > 0 ? 1 : 0) - (dy < 0 ? 1 : 0);
dy = (dy < 0 ? -dy : dy) << 1;
var points = [];
points.push({ x: x1, y: y1 });
if (dx >= dy) {
var error = dy - (dx >> 1);
while (x1 != x2) {
if (error >= 0 && (error != 0 || ix > 0)) {
error -= dx;
y1 += iy;
}
error += dy;
x1 += ix;
points.push({ x: x1, y: y1 });
}
} else {
var error = dx - (dy >> 1);
while (y1 != y2) {
if (error >= 0 && (error != 0 || iy > 0)) {
error -= dy;
x1 += ix;
}
error += dx;
y1 += iy;
points.push({ x: x1, y: y1 });
}
}
return points;
};
geometrize_rasterizer_Rasterizer.scanlinesForPolygon = function (points) {
var lines = [];
var edges = [];
var _g = 0;
var _g1 = points.length;
while (_g < _g1) {
var i = _g++;
var p1 = points[i];
var p2 = i == points.length - 1 ? points[0] : points[i + 1];
var p1p2 = geometrize_rasterizer_Rasterizer.bresenham(p1.x, p1.y, p2.x, p2.y);
edges = edges.concat(p1p2);
}
var yToXs = new haxe_ds_IntMap();
var _g = 0;
while (_g < edges.length) {
var point = edges[_g];
++_g;
var s = yToXs.h[point.y];
if (s != null) {
geometrize_ArraySet.add(s, point.x);
} else {
s = geometrize_ArraySet.create();
geometrize_ArraySet.add(s, point.x);
yToXs.h[point.y] = s;
}
}
var key = yToXs.keys();
while (key.hasNext()) {
var key1 = key.next();
var a = geometrize_ArraySet.toArray(yToXs.h[key1]);
var minMaxElements;
if (a == null || a.length == 0) {
minMaxElements = { x: 0, y: 0 };
} else {
var min = a[0];
var max = a[0];
var _g = 0;
while (_g < a.length) {
var value = a[_g];
++_g;
if (min > value) {
min = value;
}
if (max < value) {
max = value;
}
}
minMaxElements = { x: min, y: max };
}
lines.push(new geometrize_rasterizer_Scanline(key1, minMaxElements.x, minMaxElements.y));
}
return lines;
};
var geometrize_rasterizer_Scanline = function (y, x1, x2) {
this.y = y;
this.x1 = x1;
this.x2 = x2;
};
geometrize_rasterizer_Scanline.__name__ = true;
geometrize_rasterizer_Scanline.trim = function (scanlines, w, h) {
if (scanlines == null) {
throw haxe_Exception.thrown('FAIL: scanlines != null');
}
var w1 = w;
var h1 = h;
var f = function (line) {
if (line.y < 0 || line.y >= h1 || line.x1 >= w1 || line.x2 < 0) {
return false;
} else {
var value = line.x1;
var max = w1 - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
line.x1 = value < 0 ? 0 : value > max ? max : value;
var value = line.x2;
var max = w1 - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
line.x2 = value < 0 ? 0 : value > max ? max : value;
return line.x1 <= line.x2;
}
};
var _g = [];
var _g1 = 0;
var _g2 = scanlines;
while (_g1 < _g2.length) {
var v = _g2[_g1];
++_g1;
if (f(v)) {
_g.push(v);
}
}
return _g;
};
var geometrize_runner_ImageRunner = ($global['geometrize']['runner']['ImageRunner'] = function (
inputImage,
backgroundColor
) {
this.model = null;
this.model = new geometrize_Model(inputImage, backgroundColor);
});
geometrize_runner_ImageRunner.__name__ = true;
geometrize_runner_ImageRunner.prototype = {
step: function (options) {
var finalOptions_shapeTypes =
options.shapeTypes != null && options.shapeTypes.length != 0
? options.shapeTypes
: geometrize_runner_Default.options.shapeTypes;
var finalOptions_alpha = options.alpha != null ? options.alpha : geometrize_runner_Default.options.alpha;
var finalOptions_candidateShapesPerStep =
options.candidateShapesPerStep != null
? options.candidateShapesPerStep
: geometrize_runner_Default.options.candidateShapesPerStep;
var finalOptions_shapeMutationsPerStep =
options.shapeMutationsPerStep != null
? options.shapeMutationsPerStep
: geometrize_runner_Default.options.shapeMutationsPerStep;
return this.model.step(
finalOptions_shapeTypes,
finalOptions_alpha,
finalOptions_candidateShapesPerStep,
finalOptions_shapeMutationsPerStep
);
},
getImageData: function () {
if (this.model == null) {
throw haxe_Exception.thrown('FAIL: model != null');
}
return this.model.current;
},
};
var geometrize_runner_Default = function () {};
geometrize_runner_Default.__name__ = true;
var geometrize_shape_Ellipse = function (xBound, yBound) {
this.x = Std.random(xBound);
this.y = Std.random(yBound);
this.rx = Std.random(32) + 1;
this.ry = Std.random(32) + 1;
this.xBound = xBound;
this.yBound = yBound;
};
geometrize_shape_Ellipse.__name__ = true;
geometrize_shape_Ellipse.prototype = {
rasterize: function () {
var lines = [];
var aspect = this.rx / this.ry;
var w = this.xBound;
var h = this.yBound;
var _g = 0;
var _g1 = this.ry;
while (_g < _g1) {
var dy = _g++;
var y1 = this.y - dy;
var y2 = this.y + dy;
if ((y1 < 0 || y1 >= h) && (y2 < 0 || y2 >= h)) {
continue;
}
var s = (Math.sqrt(this.ry * this.ry - dy * dy) * aspect) | 0;
var x1 = this.x - s;
var x2 = this.x + s;
if (x1 < 0) {
x1 = 0;
}
if (x2 >= w) {
x2 = w - 1;
}
if (y1 >= 0 && y1 < h) {
lines.push(new geometrize_rasterizer_Scanline(y1, x1, x2));
}
if (y2 >= 0 && y2 < h && dy > 0) {
lines.push(new geometrize_rasterizer_Scanline(y2, x1, x2));
}
}
return lines;
},
mutate: function () {
var r = Std.random(3);
switch (r) {
case 0:
var value = this.x + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.x = value < 0 ? 0 : value > max ? max : value;
var value = this.y + (-16 + Math.floor(33 * Math.random()));
var max = this.yBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.y = value < 0 ? 0 : value > max ? max : value;
break;
case 1:
var value = this.rx + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (1 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.rx = value < 1 ? 1 : value > max ? max : value;
break;
case 2:
var value = this.ry + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (1 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.ry = value < 1 ? 1 : value > max ? max : value;
break;
}
},
clone: function () {
var ellipse = new geometrize_shape_Ellipse(this.xBound, this.yBound);
ellipse.x = this.x;
ellipse.y = this.y;
ellipse.rx = this.rx;
ellipse.ry = this.ry;
return ellipse;
},
getType: function () {
return 3;
},
getRawShapeData: function () {
return [this.x, this.y, this.rx, this.ry];
},
getSvgShapeData: function () {
return (
'<ellipse cx="' +
this.x +
'" cy="' +
this.y +
'" rx="' +
this.rx +
'" ry="' +
this.ry +
'" ' +
geometrize_exporter_SvgExporter.SVG_STYLE_HOOK +
' />'
);
},
};
var geometrize_shape_Circle = function (xBound, yBound) {
geometrize_shape_Ellipse.call(this, xBound, yBound);
this.rx = Std.random(32) + 1;
this.ry = this.rx;
};
geometrize_shape_Circle.__name__ = true;
geometrize_shape_Circle.__super__ = geometrize_shape_Ellipse;
geometrize_shape_Circle.prototype = $extend(geometrize_shape_Ellipse.prototype, {
mutate: function () {
var r = Std.random(2);
switch (r) {
case 0:
var value = this.x + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.x = value < 0 ? 0 : value > max ? max : value;
var value = this.y + (-16 + Math.floor(33 * Math.random()));
var max = this.yBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.y = value < 0 ? 0 : value > max ? max : value;
break;
case 1:
var value = this.rx + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (1 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
var r = value < 1 ? 1 : value > max ? max : value;
this.rx = r;
this.ry = r;
break;
}
},
clone: function () {
var circle = new geometrize_shape_Circle(this.xBound, this.yBound);
circle.x = this.x;
circle.y = this.y;
circle.rx = this.rx;
circle.ry = this.ry;
return circle;
},
getType: function () {
return 5;
},
getRawShapeData: function () {
return [this.x, this.y, this.rx];
},
getSvgShapeData: function () {
return (
'<circle cx="' +
this.x +
'" cy="' +
this.y +
'" r="' +
this.rx +
'" ' +
geometrize_exporter_SvgExporter.SVG_STYLE_HOOK +
' />'
);
},
});
var geometrize_shape_Line = function (xBound, yBound) {
this.x1 = Std.random(xBound);
this.y1 = Std.random(yBound);
var value = this.x1 + Std.random(32) + 1;
if (0 > xBound) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.x2 = value < 0 ? 0 : value > xBound ? xBound : value;
var value = this.y1 + Std.random(32) + 1;
if (0 > yBound) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.y2 = value < 0 ? 0 : value > yBound ? yBound : value;
this.xBound = xBound;
this.yBound = yBound;
};
geometrize_shape_Line.__name__ = true;
geometrize_shape_Line.prototype = {
rasterize: function () {
var lines = [];
var points = geometrize_rasterizer_Rasterizer.bresenham(this.x1, this.y1, this.x2, this.y2);
var _g = 0;
while (_g < points.length) {
var point = points[_g];
++_g;
lines.push(new geometrize_rasterizer_Scanline(point.y, point.x, point.x));
}
return geometrize_rasterizer_Scanline.trim(lines, this.xBound, this.yBound);
},
mutate: function () {
var r = Std.random(4);
switch (r) {
case 0:
var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.x1 = value < 0 ? 0 : value > max ? max : value;
var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
var max = this.yBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.y1 = value < 0 ? 0 : value > max ? max : value;
break;
case 1:
var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
var max = this.xBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.x2 = value < 0 ? 0 : value > max ? max : value;
var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
var max = this.yBound - 1;
if (0 > max) {
throw haxe_Exception.thrown('FAIL: min <= max');
}
this.y2 = value < 0 ? 0 : value > max ? max : value;
break;
}
},
clone: function () {
var line = new geometrize_shape_Line(this.xBound, this.yBound);
line.x1 = this.x1;
line.y1 = this.y1;
line.x2 = this.x2;
line.y2 = this.y2;
return line;
},
getType: function () {
return 6;
},
getRawShapeData: function () {
return [this.x1, this.y1, this.x2, this.y2];
},
getSvgShapeData: function () {
return (
'<line x1="' +
this.x1 +
'" y1="' +
this.y1 +
'" x2="' +
this.x2 +
'" y2="' +
this.y2 +
'" ' +
geometrize_exporter_SvgExporter.SVG_STYLE_HOOK +
' />'
);
},
};
var geometrize_shape_ShapeFactory = function () {};
geometrize_shape_ShapeFactory.__name__ = true;
geometrize_shape_ShapeFactory.create = function (type, xBound, yBound) {
switch (type) {
case 5:
return new geometrize_shape_Circle(xBound, yBound);
case 6:
return new geometrize_shape_Line(xBound, yBound);
}
};
geometrize_shape_ShapeFactory.randomShapeOf = function (types, xBound, yBound) {
if (!(types != null && types.length > 0)) {
throw haxe_Exception.thrown('FAIL: a != null && a.length > 0');
}
var upper = types.length - 1;
if (0 > upper) {
throw haxe_Exception.thrown('FAIL: lower <= upper');
}
return geometrize_shape_ShapeFactory.create(types[Math.floor((upper + 1) * Math.random())], xBound, yBound);
};
var geometrize_shape_ShapeTypes = ($global['geometrize']['shape']['ShapeTypes'] = function () {});
geometrize_shape_ShapeTypes.__name__ = true;
var haxe_Exception = function (message, previous, native) {
Error.call(this, message);
this.message = message;
this.__previousException = previous;
this.__nativeException = native != null ? native : this;
};
haxe_Exception.__name__ = true;
haxe_Exception.thrown = function (value) {
if (value instanceof haxe_Exception) {
return value.get_native();
} else if (value instanceof Error) {
return value;
} else {
var e = new haxe_ValueException(value);
return e;
}
};
haxe_Exception.__super__ = Error;
haxe_Exception.prototype = $extend(Error.prototype, {
get_native: function () {
return this.__nativeException;
},
});
var haxe_ValueException = function (value, previous, native) {
haxe_Exception.call(this, String(value), previous, native);
this.value = value;
};
haxe_ValueException.__name__ = true;
haxe_ValueException.__super__ = haxe_Exception;
haxe_ValueException.prototype = $extend(haxe_Exception.prototype, {});
var haxe_ds_IntMap = function () {
this.h = {};
};
haxe_ds_IntMap.__name__ = true;
haxe_ds_IntMap.prototype = {
keys: function () {
var a = [];
for (var key in this.h) if (this.h.hasOwnProperty(key)) a.push(+key);
return new haxe_iterators_ArrayIterator(a);
},
};
var haxe_io_Bytes = function (data) {
this.length = data.byteLength;
this.b = new Uint8Array(data);
this.b.bufferValue = data;
data.hxBytes = this;
data.bytes = this.b;
};
haxe_io_Bytes.__name__ = true;
var haxe_iterators_ArrayIterator = function (array) {
this.current = 0;
this.array = array;
};
haxe_iterators_ArrayIterator.__name__ = true;
haxe_iterators_ArrayIterator.prototype = {
hasNext: function () {
return this.current < this.array.length;
},
next: function () {
return this.array[this.current++];
},
};
var js_Boot = function () {};
js_Boot.__name__ = true;
js_Boot.__string_rec = function (o, s) {
if (o == null) {
return 'null';
}
if (s.length >= 5) {
return '<...>';
}
var t = typeof o;
if (t == 'function' && (o.__name__ || o.__ename__)) {
t = 'object';
}
switch (t) {
case 'function':
return '<function>';
case 'object':
if (o instanceof Array) {
var str = '[';
s += '\t';
var _g = 0;
var _g1 = o.length;
while (_g < _g1) {
var i = _g++;
str += (i > 0 ? ',' : '') + js_Boot.__string_rec(o[i], s);
}
str += ']';
return str;
}
var tostr;
try {
tostr = o.toString;
} catch (_g) {
return '???';
}
if (tostr != null && tostr != Object.toString && typeof tostr == 'function') {
var s2 = o.toString();
if (s2 != '[object Object]') {
return s2;
}
}
var str = '{\n';
s += '\t';
var hasp = o.hasOwnProperty != null;
var k = null;
for (k in o) {
if (hasp && !o.hasOwnProperty(k)) {
continue;
}
if (
k == 'prototype' ||
k == '__class__' ||
k == '__super__' ||
k == '__interfaces__' ||
k == '__properties__'
) {
continue;
}
if (str.length != 2) {
str += ', \n';
}
str += s + k + ' : ' + js_Boot.__string_rec(o[k], s);
}
s = s.substring(1);
str += '\n' + s + '}';
return str;
case 'string':
return o;
default:
return String(o);
}
};
var $_;
function $bind(o, m) {
if (m == null) return null;
if (m.__id__ == null) m.__id__ = $global.$haxeUID++;
var f;
if (o.hx__closures__ == null) o.hx__closures__ = {};
else f = o.hx__closures__[m.__id__];
if (f == null) {
f = m.bind(o);
o.hx__closures__[m.__id__] = f;
}
return f;
}
$global.$haxeUID |= 0;
if (typeof performance != 'undefined' ? typeof performance.now == 'function' : false) {
HxOverrides.now = performance.now.bind(performance);
}
String.__name__ = true;
Array.__name__ = true;
js_Boot.__toStr = {}.toString;
$global['geometrize']['init'] = function () {
Main.runPauseButton = window.document.getElementById('runpausebutton');
Main.stepButton = window.document.getElementById('stepbutton');
Main.openImageFileInput = window.document.getElementById('openimageinput');
Main.resetButton = window.document.getElementById('resetbutton');
Main.circlesCheckbox = window.document.getElementById('circles');
Main.linesCheckbox = window.document.getElementById('lines');
Main.shapesAddedText = window.document.getElementById('shapesaddedtext');
Main.maxShapesCapTextEdit = window.document.getElementById('maxshapescaptextedit');
Main.currentSvgContainer = window.document.getElementById('currentsvgcontainer');
geometrize_exporter_SvgExporter.SVG_STYLE_HOOK = '::svg_style_hook::';
geometrize_runner_Default.options = {
shapeTypes: [5],
candidateShapesPerStep: 50,
shapeMutationsPerStep: 100,
alpha: 255,
};
geometrize_shape_ShapeTypes.CIRCLE = 5;
geometrize_shape_ShapeTypes.LINE = 6;
Main.main();
};
})(
typeof window != 'undefined'
? window
: typeof global != 'undefined'
? global
: typeof self != 'undefined'
? self
: this,
`!(function (t) {
"use strict";
function r(t, r) {
var e = Object.create(t);
for (var n in r) e[n] = r[n];
return (
r.toString !== Object.prototype.toString && (e.toString = r.toString),
e
);
}
((t.geometrize = t.geometrize || {}),
(t.geometrize.bitmap = t.geometrize.bitmap || {}),
(t.geometrize.exporter = t.geometrize.exporter || {}),
(t.geometrize.runner = t.geometrize.runner || {}),
(t.geometrize.shape = t.geometrize.shape || {}));
var e = function () {};
e.prototype = {
messageHandler: function (t) {
if (null != t && null != t.data) {
var r = t.data;
switch (r.id) {
case "should_set_target_image":
var e = r.data;
((this.runner = new g(e, l.getAverageImageColor(e))),
this.postMessage({ id: "did_set_target_image" }));
break;
case "should_step":
var n = r.data,
a = this.runner.step(n),
i = w.exportShapes(a),
o = c.exportShapes(a);
this.postMessage({ id: "did_step", svgData: i, jsonData: o });
break;
}
}
},
postMessage: function (t) {},
};
var n = function () {};
n.random = function (t) {
return t <= 0 ? 0 : Math.floor(Math.random() * t);
};
var a = function () {};
a.replace = function (t, r, e) {
return t.split(r).join(e);
};
var i = {
toFloat: function (t) {
return t < 0 ? 4294967296 + t : t + 0;
},
},
o = {
create: function (t) {
if (null == t) {
return [];
}
return o.toSet(t);
},
add: function (t, r) {
if (null == r) throw L.thrown("FAIL: element != null");
return !o.contains(t, r) && (t.push(r), !0);
},
contains: function (t, r) {
for (var e = 0; e < t.length; ) {
var n = t[e];
if ((++e, n == r)) return !0;
}
return !1;
},
toArray: function (t) {
return t.slice();
},
toSet: function (t) {
for (var r = [], e = 0; e < t.length; ) {
var n = t[e];
(++e, o.add(r, n));
}
return r;
},
},
h = function () {};
((h.computeColor = function (t, r, e, n) {
if (null == t) throw L.thrown("FAIL: target != null");
if (null == r) throw L.thrown("FAIL: current != null");
if (null == e) throw L.thrown("FAIL: lines != null");
if (n < 0) throw L.thrown("FAIL: alpha >= 0");
for (
var a = 0, i = 0, o = 0, h = 0, s = 0 | (65535 / n), u = 0;
u < e.length;
) {
var l = e[u];
++u;
for (var f = l.y, c = l.x1, w = l.x2 + 1; c < w; ) {
var d = c++,
p = t.data[t.width * f + d],
g = r.data[r.width * f + d];
((a +=
(((p >> 24) & 255) - ((g >> 24) & 255)) * s +
257 * ((g >> 24) & 255)),
(i +=
(((p >> 16) & 255) - ((g >> 16) & 255)) * s +
257 * ((g >> 16) & 255)),
(o +=
(((p >> 8) & 255) - ((g >> 8) & 255)) * s + 257 * ((g >> 8) & 255)),
++h);
}
}
if (0 == h) return 0;
var v,
y = (v = ((a / h) | 0) >> 8) < 0 ? 0 : v > 255 ? 255 : v,
x = (v = ((i / h) | 0) >> 8) < 0 ? 0 : v > 255 ? 255 : v,
m = (v = ((o / h) | 0) >> 8) < 0 ? 0 : v > 255 ? 255 : v;
return (
((y < 0 ? 0 : y > 255 ? 255 : y) << 24) +
((x < 0 ? 0 : x > 255 ? 255 : x) << 16) +
((m < 0 ? 0 : m > 255 ? 255 : m) << 8) +
(n < 0 ? 0 : n > 255 ? 255 : n)
);
}),
(h.differenceFull = function (t, r) {
if (null == t) throw L.thrown("FAIL: first != null");
if (null == r) throw L.thrown("FAIL: second != null");
if (0 == t.width) throw L.thrown("FAIL: first.width != 0");
if (0 == t.height) throw L.thrown("FAIL: first.height != 0");
if (0 == r.width) throw L.thrown("FAIL: second.width != 0");
if (0 == r.height) throw L.thrown("FAIL: second.height != 0");
var e, n;
if ((e = t.width) != (n = r.width))
throw L.thrown(
"FAIL: values are not equal (expected: " + n + ", actual: " + e + ")",
);
if ((e = t.height) != (n = r.height))
throw L.thrown(
"FAIL: values are not equal (expected: " + n + ", actual: " + e + ")",
);
for (var a = 0, i = t.width, o = t.height, h = 0, s = o; h < s; )
for (var u = h++, l = 0, f = i; l < f; ) {
var c = l++,
w = t.data[t.width * u + c],
d = r.data[r.width * u + c],
p = ((w >> 24) & 255) - ((d >> 24) & 255),
g = ((w >> 16) & 255) - ((d >> 16) & 255),
v = ((w >> 8) & 255) - ((d >> 8) & 255),
y = (255 & w) - (255 & d);
a += p * p + g * g + v * v + y * y;
}
var x = Math.sqrt(a / (i * o * 4)) / 255;
if (!isFinite(x)) throw L.thrown("FAIL: Math.isFinite(result)");
return x;
}),
(h.differencePartial = function (t, r, e, n, a) {
if (null == t) throw L.thrown("FAIL: target != null");
if (null == r) throw L.thrown("FAIL: before != null");
if (null == e) throw L.thrown("FAIL: after != null");
if (null == a) throw L.thrown("FAIL: lines != null");
if (0 == a.length) throw L.thrown("FAIL: lines.length != 0");
for (
var i = t.width * t.height * 4, o = Math.pow(255 * n, 2) * i, h = 0;
h < a.length;
) {
var s = a[h];
++h;
for (var u = s.y, l = s.x1, f = s.x2 + 1; l < f; ) {
var c = l++,
w = t.data[t.width * u + c],
d = r.data[r.width * u + c],
p = e.data[e.width * u + c],
g = ((w >> 24) & 255) - ((d >> 24) & 255),
v = ((w >> 16) & 255) - ((d >> 16) & 255),
y = ((w >> 8) & 255) - ((d >> 8) & 255),
x = (255 & w) - (255 & d),
m = ((w >> 24) & 255) - ((p >> 24) & 255),
F = ((w >> 16) & 255) - ((p >> 16) & 255),
A = ((w >> 8) & 255) - ((p >> 8) & 255),
S = (255 & w) - (255 & p);
((o -= g * g + v * v + y * y + x * x),
(o += m * m + F * F + A * A + S * S));
}
}
var I = Math.sqrt(o / i) / 255;
if (!isFinite(I)) throw L.thrown("FAIL: Math.isFinite(result)");
return I;
}),
(h.bestRandomState = function (t, r, e, n, a, i, o) {
for (var h = 0, s = null, l = 0, f = e; l < f; ) {
var c = l++,
w = new u(F.randomShapeOf(t, a.width, a.height), r, n, a, i),
d = w.energy(o);
(0 == c || d < h) && ((h = d), (s = w));
}
return s;
}),
(h.bestHillClimbState = function (t, r, e, n, a, i, o, s) {
var u = h.bestRandomState(t, r, e, a, i, o, s);
return (u = h.hillClimb(u, n, s));
}),
(h.hillClimb = function (t, r, e) {
if (null == t) throw L.thrown("FAIL: state != null");
if (r < 0) throw L.thrown("FAIL: maxAge >= 0");
for (var n = t.clone(), a = n.clone(), i = n.energy(e), o = 0; o < r; ) {
var h = n.mutate(),
s = n.energy(e);
(s >= i ? (n = h) : ((i = s), (a = n.clone()), (o = -1)), ++o);
}
return a;
}),
(h.energy = function (t, r, e, n, a, i) {
if (null == t) throw L.thrown("FAIL: shape != null");
if (null == e) throw L.thrown("FAIL: target != null");
if (null == n) throw L.thrown("FAIL: current != null");
if (null == a) throw L.thrown("FAIL: buffer != null");
var o = t.rasterize();
if (null == o) throw L.thrown("FAIL: lines != null");
if (0 == o.length) throw L.thrown("FAIL: lines.length != 0");
var s = h.computeColor(e, n, o, r);
return (
d.copyLines(a, n, o),
d.drawLines(a, s, o),
h.differencePartial(e, n, a, i, o)
);
}));
var s = function (t, r) {
if (null == t) throw L.thrown("FAIL: target != null");
((this.width = t.width), (this.height = t.height), (this.target = t));
var e = t.width,
n = t.height;
(((o = new f()).width = e), (o.height = n));
var a = new Array(e * n);
o.data = a;
for (var i = 0; i < o.data.length; ) ((o.data[i] = r), ++i);
this.current = o;
var o;
((e = t.width), (n = t.height));
(((o = new f()).width = e), (o.height = n));
a = new Array(e * n);
o.data = a;
for (i = 0; i < o.data.length; ) ((o.data[i] = r), ++i);
((this.buffer = o), (this.score = h.differenceFull(t, this.current)));
};
s.prototype = {
step: function (t, r, e, n) {
var a = h.bestHillClimbState(
t,
r,
e,
n,
this.target,
this.current,
this.buffer,
this.score,
);
return [this.addShape(a.shape, a.alpha)];
},
addShape: function (t, r) {
if (null == t) throw L.thrown("FAIL: shape != null");
var e = this.current,
n = new f();
((n.width = e.width), (n.height = e.height));
var a = new Array(e.data.length);
n.data = a;
for (var i = 0, o = e.data.length; i < o; ) {
var s = i++;
n.data[s] = e.data[s];
}
var u = n,
l = t.rasterize(),
c = h.computeColor(this.target, this.current, l, r);
return (
d.drawLines(this.current, c, l),
(this.score = h.differencePartial(
this.target,
u,
this.current,
this.score,
l,
)),
{ score: this.score, color: c, shape: t }
);
},
};
var u = function (t, r, e, n, a) {
if (null == t) throw L.thrown("FAIL: shape != null");
((this.shape = t),
(this.alpha = r),
(this.score = -1),
(this.target = e),
(this.current = n),
(this.buffer = a));
};
u.prototype = {
energy: function (t) {
return (
this.score < 0 &&
(this.score = h.energy(
this.shape,
this.alpha,
this.target,
this.current,
this.buffer,
t,
)),
this.score
);
},
mutate: function () {
var t = this.clone();
return (this.shape.mutate(), t);
},
clone: function () {
return new u(
this.shape.clone(),
this.alpha,
this.target,
this.current,
this.buffer,
);
},
};
var l = function () {};
l.getAverageImageColor = function (t, r) {
if ((null == r && (r = 255), null == t))
throw L.thrown("FAIL: image != null");
for (var e = 0, n = 0, a = 0, i = 0, o = t.width; i < o; )
for (var h = i++, s = 0, u = t.height; s < u; ) {
var l = s++,
f = t.data[t.width * l + h];
((e += (f >> 24) & 255), (n += (f >> 16) & 255), (a += (f >> 8) & 255));
}
var c = t.width * t.height,
w = (e / c) | 0,
d = (n / c) | 0,
p = (a / c) | 0;
return (
((w < 0 ? 0 : w > 255 ? 255 : w) << 24) +
((d < 0 ? 0 : d > 255 ? 255 : d) << 16) +
((p < 0 ? 0 : p > 255 ? 255 : p) << 8) +
(r < 0 ? 0 : r > 255 ? 255 : r)
);
};
var f = (t.geometrize.bitmap.Bitmap = function () {});
((f.create = function (t, r, e) {
var n = new f();
((n.width = t), (n.height = r));
var a = new Array(t * r);
n.data = a;
for (var i = 0; i < n.data.length; ) ((n.data[i] = e), ++i);
return n;
}),
(f.createFromBytes = function (t, r, e) {
var n = new f();
if (null == e) throw L.thrown("FAIL: bytes != null");
var a = e.length,
i = t * r * 4;
if (a != i)
throw L.thrown(
"FAIL: values are not equal (expected: " + i + ", actual: " + a + ")",
);
((n.width = t), (n.height = r));
var o = new Array((e.length / 4) | 0);
n.data = o;
for (var h = 0, s = 0; h < e.length; ) {
var u = e.b[h],
l = e.b[h + 1],
c = e.b[h + 2],
w = e.b[h + 3];
((n.data[s] =
((u < 0 ? 0 : u > 255 ? 255 : u) << 24) +
((l < 0 ? 0 : l > 255 ? 255 : l) << 16) +
((c < 0 ? 0 : c > 255 ? 255 : c) << 8) +
(w < 0 ? 0 : w > 255 ? 255 : w)),
(h += 4),
++s);
}
return n;
}),
(f.createFromByteArray = function (t, r, e) {
for (var n = new b(new ArrayBuffer(e.length)), a = 0; a < e.length; )
((n.b[a] = e[a]), ++a);
var i = new f();
if (null == n) throw L.thrown("FAIL: bytes != null");
var o = n.length,
h = t * r * 4;
if (o != h)
throw L.thrown(
"FAIL: values are not equal (expected: " + h + ", actual: " + o + ")",
);
((i.width = t), (i.height = r));
var s = new Array((n.length / 4) | 0);
i.data = s;
a = 0;
for (var u = 0; a < n.length; ) {
var l = n.b[a],
c = n.b[a + 1],
w = n.b[a + 2],
d = n.b[a + 3];
((i.data[u] =
((l < 0 ? 0 : l > 255 ? 255 : l) << 24) +
((c < 0 ? 0 : c > 255 ? 255 : c) << 16) +
((w < 0 ? 0 : w > 255 ? 255 : w) << 8) +
(d < 0 ? 0 : d > 255 ? 255 : d)),
(a += 4),
++u);
}
return i;
}),
(f.prototype = {
getPixel: function (t, r) {
return this.data[this.width * r + t];
},
setPixel: function (t, r, e) {
this.data[this.width * r + t] = e;
},
clone: function () {
var t = new f();
((t.width = this.width), (t.height = this.height));
var r = new Array(this.data.length);
t.data = r;
for (var e = 0, n = this.data.length; e < n; ) {
var a = e++;
t.data[a] = this.data[a];
}
return t;
},
fill: function (t) {
for (var r = 0; r < this.data.length; )
((this.data[r] = (t >> 24) & 255),
(this.data[r + 1] = (t >> 16) & 255),
(this.data[r + 2] = (t >> 8) & 255),
(this.data[r + 3] = 255 & t),
(r += 4));
},
getBytes: function () {
for (
var t = new b(new ArrayBuffer(4 * this.data.length)), r = 0;
r < this.data.length;
) {
var e = 4 * r;
((t.b[e] = (this.data[r] >> 24) & 255),
(t.b[e + 1] = (this.data[r] >> 16) & 255),
(t.b[e + 2] = (this.data[r] >> 8) & 255),
(t.b[e + 3] = 255 & this.data[r]),
++r);
}
return t;
},
}));
var c = (t.geometrize.exporter.ShapeJsonExporter = function () {});
((c.export = function (t) {
return "[\n" + c.exportShapes(t) + "\n]";
}),
(c.exportShapes = function (t) {
for (var r = "", e = 0, n = t.length; e < n; ) {
var a = e++;
((r += c.exportShape(t[a])), a != t.length - 1 && (r += ",\n"));
}
return r;
}),
(c.exportShape = function (t) {
var r = " {\n",
e = t.shape.getType(),
n = t.shape.getRawShapeData(),
a = t.color,
i = t.score;
((r += ' "type":' + e + ",\n"), (r += ' "data":['));
for (var o = 0, h = n.length; o < h; ) {
var s = o++;
((r += n[s]), s <= n.length - 2 && (r += ","));
}
return (
(r += "],\n"),
(r += ' "color":['),
(r += ((a >> 24) & 255) + ","),
(r += ((a >> 16) & 255) + ","),
(r += ((a >> 8) & 255) + ","),
(r += 255 & a),
(r += "],\n"),
(r += ' "score":' + i + "\n"),
(r += " }")
);
}));
var w = (t.geometrize.exporter.SvgExporter = function () {});
((w.export = function (t, r, e) {
var n = w.getSvgPrelude();
return (
(n += w.getSvgNodeOpen(r, e)),
(n += w.exportShapes(t)),
(n += w.getSvgNodeClose())
);
}),
(w.exportShapes = function (t) {
for (var r = "", e = 0, n = t.length; e < n; ) {
var a = e++;
((r += w.exportShape(t[a])), a != t.length - 1 && (r += "\n"));
}
return r;
}),
(w.exportShape = function (t) {
return a.replace(
t.shape.getSvgShapeData(),
w.SVG_STYLE_HOOK,
w.stylesForShape(t),
);
}),
(w.getSvgPrelude = function () {
return '<?xml version="1.0" standalone="no"?>\n';
}),
(w.getSvgNodeOpen = function (t, r) {
return (
'<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="' +
t +
'" height="' +
r +
'">\n'
);
}),
(w.getSvgNodeClose = function () {
return "</svg>";
}),
(w.stylesForShape = function (t) {
switch (t.shape.getType()) {
case 6:
case 7:
return (
w.strokeForColor(t.color) +
' stroke-width="1" fill="none" ' +
w.strokeOpacityForAlpha(255 & t.color)
);
default:
return (
w.fillForColor(t.color) + " " + w.fillOpacityForAlpha(255 & t.color)
);
}
}),
(w.rgbForColor = function (t) {
return (
"rgb(" +
((t >> 24) & 255) +
"," +
((t >> 16) & 255) +
"," +
((t >> 8) & 255) +
")"
);
}),
(w.strokeForColor = function (t) {
return 'stroke="' + w.rgbForColor(t) + '"';
}),
(w.fillForColor = function (t) {
return 'fill="' + w.rgbForColor(t) + '"';
}),
(w.fillOpacityForAlpha = function (t) {
return 'fill-opacity="' + t / 255 + '"';
}),
(w.strokeOpacityForAlpha = function (t) {
return 'stroke-opacity="' + t / 255 + '"';
}));
var d = function () {};
((d.drawLines = function (t, r, e) {
if (null == t) throw L.thrown("FAIL: image != null");
if (null == e) throw L.thrown("FAIL: lines != null");
var n = (r >> 24) & 255;
((n |= n << 8), (n = ((n *= 255 & r) / 255) | 0));
var a = (r >> 16) & 255;
((a |= a << 8), (a = ((a *= 255 & r) / 255) | 0));
var o = (r >> 8) & 255;
((o |= o << 8), (o = ((o *= 255 & r) / 255) | 0));
var h = 255 & r;
h |= h << 8;
for (var s = 0; s < e.length; ) {
var u = e[s];
++s;
for (
var l = u.y,
f = 65535,
c = 65535,
w = 0 | (257 * (c - 1 * h)),
d = u.x1,
p = u.x2 + 1;
d < p;
) {
var g = d++,
v = t.data[t.width * l + g],
y = (v >> 24) & 255,
x = (v >> 16) & 255,
m = (v >> 8) & 255,
F = 255 & v,
A = ((i.toFloat(y * w + n * f) / i.toFloat(c)) | 0) >> 8,
S = ((i.toFloat(x * w + a * f) / i.toFloat(c)) | 0) >> 8,
I = ((i.toFloat(m * w + o * f) / i.toFloat(c)) | 0) >> 8,
b = ((i.toFloat(F * w + h * f) / i.toFloat(c)) | 0) >> 8;
t.data[t.width * l + g] =
((A < 0 ? 0 : A > 255 ? 255 : A) << 24) +
((S < 0 ? 0 : S > 255 ? 255 : S) << 16) +
((I < 0 ? 0 : I > 255 ? 255 : I) << 8) +
(b < 0 ? 0 : b > 255 ? 255 : b);
}
}
}),
(d.copyLines = function (t, r, e) {
if (null == t) throw L.thrown("FAIL: destination != null");
if (null == r) throw L.thrown("FAIL: source != null");
if (null == e) throw L.thrown("FAIL: lines != null");
for (var n = 0; n < e.length; ) {
var a = e[n];
++n;
for (var i = a.y, o = a.x1, h = a.x2 + 1; o < h; ) {
var s = o++;
t.data[t.width * i + s] = r.data[r.width * i + s];
}
}
}),
(d.bresenham = function (t, r, e, n) {
var a = e - t,
i = (a > 0 ? 1 : 0) - (a < 0 ? 1 : 0);
a = (a < 0 ? -a : a) << 1;
var o = n - r,
h = (o > 0 ? 1 : 0) - (o < 0 ? 1 : 0);
o = (o < 0 ? -o : o) << 1;
var s = [];
if ((s.push({ x: t, y: r }), a >= o))
for (var u = o - (a >> 1); t != e; )
(u >= 0 && (0 != u || i > 0) && ((u -= a), (r += h)),
(u += o),
(t += i),
s.push({ x: t, y: r }));
else
for (u = a - (o >> 1); r != n; )
(u >= 0 && (0 != u || h > 0) && ((u -= o), (t += i)),
(u += a),
(r += h),
s.push({ x: t, y: r }));
return s;
}),
(d.scanlinesForPolygon = function (t) {
for (var r = [], e = [], n = 0, a = t.length; n < a; ) {
var i = n++,
h = t[i],
s = i == t.length - 1 ? t[0] : t[i + 1],
u = d.bresenham(h.x, h.y, s.x, s.y);
e = e.concat(u);
}
var l = new I();
for (n = 0; n < e.length; ) {
var f = e[n];
++n;
var c = l.h[f.y];
null != c
? o.add(c, f.x)
: ((c = o.create()), o.add(c, f.x), (l.h[f.y] = c));
}
for (var w = l.keys(); w.hasNext(); ) {
var g,
v = w.next(),
y = o.toArray(l.h[v]);
if (null == y || 0 == y.length) g = { x: 0, y: 0 };
else {
var x = y[0],
m = y[0];
for (n = 0; n < y.length; ) {
var F = y[n];
(++n, x > F && (x = F), m < F && (m = F));
}
g = { x: x, y: m };
}
r.push(new p(v, g.x, g.y));
}
return r;
}));
var p = function (t, r, e) {
((this.y = t), (this.x1 = r), (this.x2 = e));
};
p.trim = function (t, r, e) {
if (null == t) throw L.thrown("FAIL: scanlines != null");
for (
var n = r,
a = e,
i = function (t) {
if (t.y < 0 || t.y >= a || t.x1 >= n || t.x2 < 0) return !1;
var r = t.x1;
if (0 > (e = n - 1)) throw L.thrown("FAIL: min <= max");
t.x1 = r < 0 ? 0 : r > e ? e : r;
var e;
r = t.x2;
if (0 > (e = n - 1)) throw L.thrown("FAIL: min <= max");
return ((t.x2 = r < 0 ? 0 : r > e ? e : r), t.x1 <= t.x2);
},
o = [],
h = 0,
s = t;
h < s.length;
) {
var u = s[h];
(++h, i(u) && o.push(u));
}
return o;
};
var g = (t.geometrize.runner.ImageRunner = function (t, r) {
((this.model = null), (this.model = new s(t, r)));
});
g.prototype = {
step: function (t) {
var r =
null != t.shapeTypes && 0 != t.shapeTypes.length
? t.shapeTypes
: v.options.shapeTypes,
e = null != t.alpha ? t.alpha : v.options.alpha,
n =
null != t.candidateShapesPerStep
? t.candidateShapesPerStep
: v.options.candidateShapesPerStep,
a =
null != t.shapeMutationsPerStep
? t.shapeMutationsPerStep
: v.options.shapeMutationsPerStep;
return this.model.step(r, e, n, a);
},
getImageData: function () {
if (null == this.model) throw L.thrown("FAIL: model != null");
return this.model.current;
},
};
var v = function () {},
y = function (t, r) {
((this.x = n.random(t)),
(this.y = n.random(r)),
(this.rx = n.random(32) + 1),
(this.ry = n.random(32) + 1),
(this.xBound = t),
(this.yBound = r));
};
y.prototype = {
rasterize: function () {
for (
var t = [],
r = this.rx / this.ry,
e = this.xBound,
n = this.yBound,
a = 0,
i = this.ry;
a < i;
) {
var o = a++,
h = this.y - o,
s = this.y + o;
if (!(h < 0 || h >= n) || !(s < 0 || s >= n)) {
var u = (Math.sqrt(this.ry * this.ry - o * o) * r) | 0,
l = this.x - u,
f = this.x + u;
(l < 0 && (l = 0),
f >= e && (f = e - 1),
h >= 0 && h < n && t.push(new p(h, l, f)),
s >= 0 && s < n && o > 0 && t.push(new p(s, l, f)));
}
}
return t;
},
mutate: function () {
switch (n.random(3)) {
case 0:
var t = this.x + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.x = t < 0 ? 0 : t > r ? r : t;
t = this.y + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.yBound - 1)) throw L.thrown("FAIL: min <= max");
this.y = t < 0 ? 0 : t > r ? r : t;
break;
case 1:
t = this.rx + (-16 + Math.floor(33 * Math.random()));
if (1 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.rx = t < 1 ? 1 : t > r ? r : t;
break;
case 2:
var r;
t = this.ry + (-16 + Math.floor(33 * Math.random()));
if (1 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.ry = t < 1 ? 1 : t > r ? r : t;
break;
}
},
clone: function () {
var t = new y(this.xBound, this.yBound);
return (
(t.x = this.x),
(t.y = this.y),
(t.rx = this.rx),
(t.ry = this.ry),
t
);
},
getType: function () {
return 3;
},
getRawShapeData: function () {
return [this.x, this.y, this.rx, this.ry];
},
getSvgShapeData: function () {
return (
'<ellipse cx="' +
this.x +
'" cy="' +
this.y +
'" rx="' +
this.rx +
'" ry="' +
this.ry +
'" ' +
w.SVG_STYLE_HOOK +
" />"
);
},
};
var x = function (t, r) {
(y.call(this, t, r), (this.rx = n.random(32) + 1), (this.ry = this.rx));
};
((x.__super__ = y),
(x.prototype = r(y.prototype, {
mutate: function () {
switch ((e = n.random(2))) {
case 0:
var t = this.x + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.x = t < 0 ? 0 : t > r ? r : t;
t = this.y + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.yBound - 1)) throw L.thrown("FAIL: min <= max");
this.y = t < 0 ? 0 : t > r ? r : t;
break;
case 1:
var r;
t = this.rx + (-16 + Math.floor(33 * Math.random()));
if (1 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
var e = t < 1 ? 1 : t > r ? r : t;
((this.rx = e), (this.ry = e));
break;
}
},
clone: function () {
var t = new x(this.xBound, this.yBound);
return (
(t.x = this.x),
(t.y = this.y),
(t.rx = this.rx),
(t.ry = this.ry),
t
);
},
getType: function () {
return 5;
},
getRawShapeData: function () {
return [this.x, this.y, this.rx];
},
getSvgShapeData: function () {
return (
'<circle cx="' +
this.x +
'" cy="' +
this.y +
'" r="' +
this.rx +
'" ' +
w.SVG_STYLE_HOOK +
" />"
);
},
})));
var m = function (t, r) {
((this.x1 = n.random(t)), (this.y1 = n.random(r)));
var e = this.x1 + n.random(32) + 1;
if (0 > t) throw L.thrown("FAIL: min <= max");
this.x2 = e < 0 ? 0 : e > t ? t : e;
e = this.y1 + n.random(32) + 1;
if (0 > r) throw L.thrown("FAIL: min <= max");
((this.y2 = e < 0 ? 0 : e > r ? r : e),
(this.xBound = t),
(this.yBound = r));
};
m.prototype = {
rasterize: function () {
for (
var t = [], r = d.bresenham(this.x1, this.y1, this.x2, this.y2), e = 0;
e < r.length;
) {
var n = r[e];
(++e, t.push(new p(n.y, n.x, n.x)));
}
return p.trim(t, this.xBound, this.yBound);
},
mutate: function () {
switch (n.random(4)) {
case 0:
var t = this.x1 + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.x1 = t < 0 ? 0 : t > r ? r : t;
t = this.y1 + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.yBound - 1)) throw L.thrown("FAIL: min <= max");
this.y1 = t < 0 ? 0 : t > r ? r : t;
break;
case 1:
t = this.x2 + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.xBound - 1)) throw L.thrown("FAIL: min <= max");
this.x2 = t < 0 ? 0 : t > r ? r : t;
var r;
t = this.y2 + (-16 + Math.floor(33 * Math.random()));
if (0 > (r = this.yBound - 1)) throw L.thrown("FAIL: min <= max");
this.y2 = t < 0 ? 0 : t > r ? r : t;
break;
}
},
clone: function () {
var t = new m(this.xBound, this.yBound);
return (
(t.x1 = this.x1),
(t.y1 = this.y1),
(t.x2 = this.x2),
(t.y2 = this.y2),
t
);
},
getType: function () {
return 6;
},
getRawShapeData: function () {
return [this.x1, this.y1, this.x2, this.y2];
},
getSvgShapeData: function () {
return (
'<line x1="' +
this.x1 +
'" y1="' +
this.y1 +
'" x2="' +
this.x2 +
'" y2="' +
this.y2 +
'" ' +
w.SVG_STYLE_HOOK +
" />"
);
},
};
var F = function () {};
((F.create = function (t, r, e) {
switch (t) {
case 5:
return new x(r, e);
case 6:
return new m(r, e);
}
}),
(F.randomShapeOf = function (t, r, e) {
if (!(null != t && t.length > 0))
throw L.thrown("FAIL: a != null && a.length > 0");
var n = t.length - 1;
if (0 > n) throw L.thrown("FAIL: lower <= upper");
return F.create(t[Math.floor((n + 1) * Math.random())], r, e);
}));
var A = (t.geometrize.shape.ShapeTypes = function () {}),
L = function (t, r, e) {
(Error.call(this, t),
(this.message = t),
(this.__previousException = r),
(this.__nativeException = null != e ? e : this));
};
((L.thrown = function (t) {
return t instanceof L ? t.get_native() : t instanceof Error ? t : new S(t);
}),
(L.__super__ = Error),
(L.prototype = r(Error.prototype, {
get_native: function () {
return this.__nativeException;
},
})));
var S = function (t, r, e) {
(L.call(this, String(t), r, e), (this.value = t));
};
((S.__super__ = L), (S.prototype = r(L.prototype, {})));
var I = function () {
this.h = {};
};
I.prototype = {
keys: function () {
var t = [];
for (var r in this.h) this.h.hasOwnProperty(r) && t.push(+r);
return new _(t);
},
};
var b = function (t) {
((this.length = t.byteLength),
(this.b = new Uint8Array(t)),
(this.b.bufferValue = t),
(t.hxBytes = this),
(t.bytes = this.b));
},
_ = function (t) {
((this.current = 0), (this.array = t));
};
((_.prototype = {
hasNext: function () {
return this.current < this.array.length;
},
next: function () {
return this.array[this.current++];
},
}),
(onmessage = e.prototype.messageHandler),
(w.SVG_STYLE_HOOK = "::svg_style_hook::"),
(v.options = {
shapeTypes: [5],
candidateShapesPerStep: 50,
shapeMutationsPerStep: 100,
alpha: 255,
}),
(A.CIRCLE = 5),
(A.LINE = 6));
})(
"undefined" != typeof exports
? exports
: "undefined" != typeof window
? window
: "undefined" != typeof self
? self
: this,
);`
);