Skip to content

Commit

Permalink
Merge Release v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mster committed Jul 6, 2020
2 parents ea82eb3 + 55f63fe commit 46eb906
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 53 deletions.
115 changes: 102 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,125 @@ Mess around with image data using _buffers_, create some interesting & artistic

### mosh ( options, cb )

- `options`:
- `read`: (Required) Path to original image that you wish to mosh.
Supported types: `.jpg, .jpeg, .png, .bmp, .tiff, .gif`.
- `write`: Path to write the resulting image. If unspecified, `mosh()` will return the resulting image data (Jimp).
- `mode`: The mode to choose when moshing the supplied image. If no mode is specified, it will be chosen at random.
- `cb`: `function(error, data)` the callback function
- #### `options`:

## Example
- #### `read`: `<string>` | `<Buffer>` (Required)

May be a string path to original supported image, or the Buffer of said image.

Supported image types: `.jpg, .jpeg, .png, .bmp, .tiff, .gif`.

- #### `write`: `<string>`

Path to write the resulting image.

If unspecified, the resulting image data will be returned as a Buffer.

- #### `mode`: `<string>`

The mode to choose when moshing the supplied image. If no mode is specified, it will be chosen at random.

Current modes include:
`schifty`, `blurbobb`, `veneneux`, `vana`, `fatcat`.

- #### `cb`: `function(error, data)`

The callback function, as `datamosh` is callback based.

The image `data` will be returned as a Buffer if `options.write` is unspecified. Reference the following for examples on usage.

## Example Code

```js
/*
Using paths
*/
;(function jumpInThePit () {
require('./index')(
require('datamosh')(
{
read: '/path/to/read/in.gif',
write: '/path/to/write/out.gif',
mode: 'veneneux'
},
err => {
if (err) return console.error(err)

console.log('Moshing Completed!')
}
)
})()

/*
Using buffers directly
*/
;(function boogieWoogieBuffer () {
// can be done w/ async too!
let read = require('fs').readFileSync
let write = require('fs').writeFileSync

let img = read('/path/to/read/in.jpg')
require('datamosh')(
{
read: '/home/m/Desktop/0.jpeg',
write: '/home/m/Desktop/1.jpeg',
mode: 'blurbobb'
read: img
},
(err, data) => {
(err, moshedImg) => {
if (err) return console.error(err)

console.log('Moshing Completed!')
write('/path/to/write/out.jpg', moshedImg)
}
)
})()
```

## Custom Modes

Datamosh allows you to set custom moshing modes. As of `v1.1.0`, this may be acomplished by adding a mosh function to the `MODES` property.

For mosh function starter code, see the included template file located [here](https://github.com/mster/datamosh/blob/master/lib/modes/template).

```js
/*
Setting custom modes
*/
const datamosh = require('datamosh')
function myNewMode (image) {
// actually does nothing to the image
return image
}

datamosh.MODES.myNewMode = myNewMode
datamosh(/* ... */)
```

## Datamosh in the wild

Check out this list of awesome apps that use `datamosh`!

- [JanMichaelBot](https://github.com/tlaskey/JanMichaelBot) by user [@Tlaskey](https://github.com/tlaskey)

## Example Images

**Original**

![cute_kibby](https://user-images.githubusercontent.com/15038724/63730272-7bea3a00-c81f-11e9-9180-15d0d983adaf.jpg)

**M̵̟̰̬̼͐͂͛̀̀͒̋̄͗͘͝͝o̵̹̐͗͌s̷̛͍̞͍̤̘̜̎̄̆͊̃̆́͋͋̏̆̕h̸̺̦͍̝̳̞̮̮̝̐͌̏̓̌̾͠͠ͅe̶̛̙̯̭̳͕̗͒̓̓̂̋̈́̐̄̕d̴̟̩̖̟̖̻̱̰̥̗̜̪̊** using `schifty` mode.
**_Moshed_** using `schifty`.

![moshed_kibby](https://user-images.githubusercontent.com/15038724/63730276-7e4c9400-c81f-11e9-84e1-2cd37eeb40bf.jpg)

**Original**

![cat](https://user-images.githubusercontent.com/15038724/86549182-c20a3280-bef3-11ea-8f42-97df6b9d072f.jpg)

**_Moshed_** using `fatcat`.

![cat_moshed2](https://user-images.githubusercontent.com/15038724/86549187-c6365000-bef3-11ea-9906-35d2b755a8e5.jpg)

**Original**

![k_0](https://user-images.githubusercontent.com/15038724/86549099-81121e00-bef3-11ea-8302-c01af495f697.jpg)

**_Moshed_** using `veneneux`.

![k_0_moshed](https://user-images.githubusercontent.com/15038724/86549272-009fed00-bef4-11ea-9c51-bbde1a9e3802.jpg)
13 changes: 0 additions & 13 deletions lib/error.js

This file was deleted.

52 changes: 27 additions & 25 deletions lib/mosh.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ const Jimp = require('jimp')
const debug = require('util').debuglog('mosh')
const path = require('path')

const MoshError = require('./error')

module.exports = mosh

function mosh (options, cb) {
debug('Moshing started.') // it's cool cuz it's nerdy.

/* Only *.jpeg and *.jpeg are currently supported */
/* Buffer and support file-types are valid */
if (!options || !options.read) {
cb(new MoshError('Error: options.read is a required parameter'))
cb(new Error('Error: options.read is a required parameter'))
return
}

const { read, write, mode } = options

const filename = path.basename(read)
if (
!(
filename.length < 256 &&
(/^[\w\-. ]+.jpg$/.test(filename) ||
/^[\w\-. ]+.jpeg$/.test(filename) ||
/^[\w\-. ]+.png$/.test(filename) ||
/^[\w\-. ]+.bmp$/.test(filename) ||
/^[\w\-. ]+.tiff$/.test(filename) ||
/^[\w\-. ]+.gif$/.test(filename))
)
) {
cb(new MoshError('Only *.jpg OR *.jpeg file formats are supported.'))
return
if (typeof read === 'string') {
const filename = path.basename(read)
if (
!(
filename.length < 256 &&
(/^[\w\-. ]+.jpg$/.test(filename) ||
/^[\w\-. ]+.jpeg$/.test(filename) ||
/^[\w\-. ]+.png$/.test(filename) ||
/^[\w\-. ]+.bmp$/.test(filename) ||
/^[\w\-. ]+.tiff$/.test(filename) ||
/^[\w\-. ]+.gif$/.test(filename))
)
) {
cb(new Error('Only *.jpg OR *.jpeg file formats are supported.'))
return
}
}

/* If mode is unset, randomly select one */
Expand All @@ -49,7 +49,7 @@ function mosh (options, cb) {
Jimp.read(read)
.then(doMosh)
.then(writeMosh)
.catch(onError)
.catch(cb)

function doMosh (original) {
debug('Moshing')
Expand All @@ -61,14 +61,16 @@ function mosh (options, cb) {
debug('Mosh complete - writing out')
moshed.write(write, cb)
} else {
debug('Mosh complete - calling back with img data')
cb(null, moshed)
moshed.getBuffer(moshed._originalMime, (err, imgBuffer) => {
if (err) {
cb(new Error(err))
return
}
debug('Mosh complete - calling back with img data')
cb(null, imgBuffer)
})
}
}

function onError (error) {
cb(new MoshError(error))
}
}

mosh.MODES = {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datamosh",
"version": "1.2.1",
"version": "1.3.0",
"description": "Datamosh images",
"keywords": [
"datamost",
Expand Down

0 comments on commit 46eb906

Please sign in to comment.