Skip to content

Commit

Permalink
Merge v2.0.0-rc0
Browse files Browse the repository at this point in the history
  • Loading branch information
mster committed Jun 18, 2021
2 parents fcd1189 + a33e852 commit 3203fbb
Show file tree
Hide file tree
Showing 12 changed files with 687 additions and 663 deletions.
65 changes: 38 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Datamosh [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://opensource.org/licenses/MIT) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Build Status](https://travis-ci.com/mster/datamosh.svg?branch=master)](https://travis-ci.com/mster/datamosh)

![datamosh_cover_2x](https://user-images.githubusercontent.com/15038724/118436128-12662b00-b695-11eb-8b9e-f4d91a869adf.png)
![datamosh_cover_2x](https://user-images.githubusercontent.com/15038724/122327314-838e3d80-cee2-11eb-89d8-e315556797ba.png)

Mess around with image data using _buffers_, create some interesting & artistic results, **profit**.

Expand All @@ -12,40 +12,54 @@ $ npm install datamosh

# Usage

Using callbacks

```js
const mosh = require("datamosh");

const opts = {
read: "path/to/file/in.ext",
mode: "fatcat",
};
let imgBuff = await readFile("/full/path/to/image.png");

const cb = (err, moshedBuffer) => {
if (!err) writeFile("path/to/file/out.ext", moshedBuffer);
};

mosh(opts, cb);
let moshedBuff = await mosh(imgBuff, "vaporwave");
```

Using async/await
Reading/Writing the moshed image

```js
const { promisify } = require("util");
mosh("~/image.png", null, "~/moshed_image.png");

// because mode is null, a random mode will be chosen
```

const promisifiedMosh = promisify(mosh);
Moshing a buffer with callbacks

const imgBuffer = await promisifiedMosh(opts);
```js
const cb = (err, data) => {
if (!err) writeFile("/path/to/out.gif", data);
};

mosh(imgBuff, "vana", cb);
```

Using multiple modes on a single image, applied with respect to order.

```js
const opts = {
read: "path/to/file/in.ext",
mode: ["vaporwave", "fatcat", "vana"],
};
let moshedBuff = await mosh(imgBuff, ["fatcat", "vaporwave", "walter"]);

// ['vana', null, null] is also valid => ['vana', random, random]
```

# API

### `mosh(source, mode?, cb|writePath?)`

Takes input `source` Buffer/Path, returns an encoded Buffer with the applied modes.

- `mode`, the mosh mode to apply to the source image. Multiple modes may be passed using an array of modes. Any `null` values are replaced with a random mode.
- `cb (err, data)`, when using callbacks.
- `writePath`, the path to write the moshed image to.

Paths may use the tilde (~) character. Datamosh validates read and write paths, replacing tilde with the path to the home directory.

```
~/Desktop/moshes/ -> /home/youruser/Desktop/moshes
```

# Custom Modes
Expand All @@ -57,16 +71,13 @@ For mosh function starter code, see the included template file located [here](ht
```js
const datamosh = require("datamosh");

function myNewMode(image) {
const bitmap = image.bitmap.data;
// your cool code goes here

image.bitmap.data = bitmap;
function newMode(data, width, height) {
// your cool code goes here!

return image;
return data;
}

datamosh.MODES.myNewMode = myNewMode;
datamosh.MODES.newMode = newMode;
```

## Example Images
Expand Down
8 changes: 2 additions & 6 deletions lib/modes/blurbobb.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";

module.exports = function (original) {
const bitmapData = original.bitmap.data;
const data = Buffer.from(bitmapData);

module.exports = function (data) {
let counter = 0;
for (let i = 0; i < data.length; i++) {
if (counter < 64) data[i] = Math.random() * 255;
Expand All @@ -12,6 +9,5 @@ module.exports = function (original) {
if (counter > 128) counter = Math.random() * 128;
}

original.bitmap.data = data;
return original;
return data;
};
10 changes: 4 additions & 6 deletions lib/modes/fatcat.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";

module.exports = function (original) {
module.exports = function (data) {
const min = Math.min;

const bitmapData = original.bitmap.data;
const data = Buffer.from(bitmapData);

// ???
for (let i = 0; i < data.length; i += 4) {
data[i + 0] = min(data[i + 0] * 1.4, 280);
data[i + 1] = min(data[i + 1] * 1.4, 280);
Expand All @@ -24,12 +22,12 @@ module.exports = function (original) {
data[i + 2] = min(data[i + 2] * 1.4, 256);
}

// ヾ(⌐■_■)ノ♪
for (let i = 0; i < data.length; i += 4) {
data[i + 0] = min(data[i + 0] * 1.4, 255);
data[i + 1] = min(data[i + 1] * 1.4, 255);
data[i + 2] = min(data[i + 2] * 1.4, 255);
}

original.bitmap.data = data;
return original;
return data;
};
9 changes: 2 additions & 7 deletions lib/modes/schifty.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";

module.exports = function (original) {
const bitmapData = original.bitmap.data;
let data = Buffer.from(bitmapData);

module.exports = function (data) {
let size = Math.random() * 1024 * 4;
let total = size;
let temp = [];
Expand All @@ -20,7 +17,5 @@ module.exports = function (original) {
}

data = Buffer.concat(storage);
original.bitmap.data = data;

return original;
return data;
};
8 changes: 2 additions & 6 deletions lib/modes/template
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict'

module.exports = function (original) {
const bitmapData = original.bitmap.data
const data = Buffer.from(bitmapData)

module.exports = function (data, width, height) {
// manipulate data here

original.bitmap.data = data
return original
return data
}
8 changes: 2 additions & 6 deletions lib/modes/vana.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"use strict";

module.exports = function (original) {
module.exports = function (data) {
const rand = Math.random;
const max = Math.max;
const min = Math.min;

const bitmapData = original.bitmap.data;
const data = Buffer.from(bitmapData);

function giveSeed() {
const seed = [0, 0, 0];

Expand All @@ -31,6 +28,5 @@ module.exports = function (original) {
data[i + 2] = min(data[i + 2] * seed[2] + 100 * seed[1], 255);
}

original.bitmap.data = data;
return original;
return data;
};
8 changes: 2 additions & 6 deletions lib/modes/vaporwave.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";

module.exports = function (original) {
const bitmapData = original.bitmap.data;
const data = Buffer.from(bitmapData);

module.exports = function (data) {
const COLORS = [
[0, 184, 255],
[255, 0, 193],
Expand Down Expand Up @@ -67,6 +64,5 @@ module.exports = function (original) {
}
}

original.bitmap.data = data;
return original;
return data;
};
10 changes: 2 additions & 8 deletions lib/modes/veneneux.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
"use strict";

module.exports = function (original) {
module.exports = function (data, width, height) {
const rand = Math.random;
const max = Math.max;

const bitmapData = original.bitmap.data;
const data = Buffer.from(bitmapData);

const { width, height } = original.bitmap;

function giveSeed() {
const seed = [0, 0, 0];

Expand Down Expand Up @@ -42,6 +37,5 @@ module.exports = function (original) {
pixel++;
}

original.bitmap.data = data;
return original;
return data;
};
29 changes: 29 additions & 0 deletions lib/modes/walter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

module.exports = function (data) {
const f = Math.floor,
r = Math.random,
x = Math.max;

const seed = () => f(r() * 256);

const hurp = [seed(), seed(), seed()],
lurp = [seed(), seed(), seed()];

let multi = f(r() * (255 - x(...hurp, ...lurp)));

for (let i = 0; i < data.length; i += 4) {
const rP = data[i] / 255,
gP = data[i + 1] / 255,
bP = data[i + 2] / 255;

if (data[i] < lurp[0] || data[i] > hurp[0])
data[i] = hurp[0] - lurp[0] + rP * multi;
if (data[i + 1] < lurp[1] || data[i + 1] > hurp[1])
data[i + 1] = hurp[1] - lurp[1] + gP * multi;
if (data[i + 2] < lurp[2] || data[i + 2] > hurp[2])
data[1 + 2] = hurp[2] - lurp[2] + bP * multi;
}

return data;
};
Loading

0 comments on commit 3203fbb

Please sign in to comment.