diff --git a/README.md b/README.md index d51586b..91b0772 100644 --- a/README.md +++ b/README.md @@ -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`: `` | `` (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`: `` + + Path to write the resulting image. + + If unspecified, the resulting image data will be returned as a Buffer. + + - #### `mode`: `` + + 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) diff --git a/lib/error.js b/lib/error.js deleted file mode 100644 index 566dcfc..0000000 --- a/lib/error.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -class MoshError extends Error { - constructor (message, options) { - super(message) - this.name = this.constructor.name - this.message = message - this.options = Object.assign({}, options) - Error.captureStackTrace(this, this.constructor) - } -} - -module.exports = MoshError diff --git a/lib/mosh.js b/lib/mosh.js index 7fe54d3..15c71df 100644 --- a/lib/mosh.js +++ b/lib/mosh.js @@ -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 */ @@ -49,7 +49,7 @@ function mosh (options, cb) { Jimp.read(read) .then(doMosh) .then(writeMosh) - .catch(onError) + .catch(cb) function doMosh (original) { debug('Moshing') @@ -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 = { diff --git a/package-lock.json b/package-lock.json index 1b5ef2c..334b2dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "datamosh", - "version": "1.2.1", + "version": "1.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7117526..e00d7bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datamosh", - "version": "1.2.1", + "version": "1.3.0", "description": "Datamosh images", "keywords": [ "datamost",