Comparing version 0.2.2 to 0.2.3
82
jimp.js
var FS = require("fs"); | ||
var PNG = require("node-png").PNG; | ||
var JPEG = require("jpeg-js"); | ||
var BMP = require("bmp-js"); | ||
var MIME = require("mime"); | ||
@@ -59,3 +60,3 @@ var Resize = require("./resize.js"); | ||
/** | ||
* Jimp constructor | ||
* Jimp constructor (from a file) | ||
* @param path a path to the image | ||
@@ -66,3 +67,3 @@ * @param (optional) cb a function to call when the image is parsed to a bitmap | ||
/** | ||
* Jimp constructor | ||
* Jimp constructor (from another Jimp image) | ||
* @param image a Jimp image to clone | ||
@@ -73,3 +74,3 @@ * @param cb a function to call when the image is parsed to a bitmap | ||
/** | ||
* Jimp constructor | ||
* Jimp constructor (from a Buffer) | ||
* @param data a Buffer containing the image data | ||
@@ -79,5 +80,33 @@ * @param cb a function to call when the image is parsed to a bitmap | ||
/** | ||
* Jimp constructor (to generate a new image) | ||
* @param w the width of the image | ||
* @param h the height of the image | ||
* @param (optional) cb a function to call when the image is parsed to a bitmap | ||
*/ | ||
function Jimp() { | ||
if ("object" == typeof arguments[0] && arguments[0].constructor == Jimp) { | ||
if ("number" == typeof arguments[0] && "number" == typeof arguments[1]) { | ||
var w = arguments[0]; | ||
var h = arguments[1]; | ||
var cb = arguments[2]; | ||
if ("undefined" == typeof cb) cb = function () {}; | ||
if ("function" != typeof cb) | ||
throwError.call(this, "cb must be a function", cb); | ||
this.bitmap = { | ||
data: new Buffer(w * h * 4), | ||
width: w, | ||
height: h | ||
}; | ||
for (var i = 0; i < this.bitmap.data.length; i++) { | ||
this.bitmap.data[i] = 0; | ||
} | ||
cb.call(this, null, this); | ||
} else if ("object" == typeof arguments[0] && arguments[0].constructor == Jimp) { | ||
var original = arguments[0]; | ||
var cb = arguments[1]; | ||
@@ -102,3 +131,3 @@ if ("undefined" == typeof cb) cb = function () {}; | ||
cb.call(that, null, that); | ||
cb.call(this, null, this); | ||
} else if ("string" == typeof arguments[0]) { | ||
@@ -131,3 +160,3 @@ var path = arguments[0]; | ||
} else { | ||
throwError.call(this, "You must pass a path to an image file or a image buffer.", cb); | ||
throwError.call(this, "No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.", cb); | ||
} | ||
@@ -139,2 +168,3 @@ } | ||
Jimp.MIME_JPEG = "image/jpeg"; | ||
Jimp.MIME_BMP = "image/bmp"; | ||
@@ -162,2 +192,6 @@ // parses a bitmap from the constructor to the JIMP bitmap property | ||
break; | ||
case Jimp.MIME_BMP: | ||
this.bitmap = BMP.decode(data); | ||
return cb.call(this, null, this); | ||
break; | ||
default: | ||
@@ -415,27 +449,13 @@ throwError.call(this, "Unsupported MIME type: " + mime, cb); | ||
function adjust(value) { | ||
var nvalue; | ||
value = value / 255; | ||
if (val < 0.0) { | ||
if (value > 0.5) nvalue = 1.0 - value; | ||
else nvalue = value; | ||
if (nvalue < 0.0) nvalue = 0.0; | ||
nvalue = 0.5 * Math.pow (nvalue * 2.0 , (1.0 + val)); | ||
if (value > 0.5) value = 1.0 - nvalue; | ||
else value = nvalue; | ||
if (val < 0) { | ||
var x = (value > 127) ? 1 - value / 255 : value / 255; | ||
if (x < 0) x = 0; | ||
x = 0.5 * Math.pow (x * 2, 1 + val); | ||
return (value > 127) ? (1.0 - x) * 255 : x * 255; | ||
} else { | ||
if (value > 0.5) nvalue = 1.0 - value; | ||
else nvalue = value; | ||
if (nvalue < 0.0) | ||
nvalue = 0.0; | ||
power = (val == 1.0) ? 127 : 1.0 / (1.0 - val); | ||
nvalue = 0.5 * Math.pow (2.0 * nvalue, power); | ||
if (value > 0.5) value = 1.0 - nvalue; | ||
else value = nvalue; | ||
var x = (value > 127) ? 1 - value / 255 : value / 255; | ||
if (x < 0) x = 0; | ||
x = 0.5 * Math.pow (2 * x, ((val == 1) ? 127 : 1 / (1 - val))); | ||
return (value > 127) ? (1 - x) * 255 : x * 255; | ||
} | ||
return value * 255; | ||
} | ||
@@ -900,2 +920,6 @@ | ||
break; | ||
case Jimp.MIME_BMP: | ||
var bmp = BMP.encode(this.bitmap); | ||
return cb.call(this, null, bmp.data); | ||
break; | ||
default: | ||
@@ -902,0 +926,0 @@ return cb.call(this, "Unsupported MIME type: " + mime); |
{ | ||
"name": "jimp", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "An image processing library written entirely in JavaScript (i.e. zero external or native dependencies).", | ||
@@ -16,2 +16,3 @@ "main": "jimp.js", | ||
"jpeg", | ||
"bmp", | ||
"resize", | ||
@@ -24,8 +25,9 @@ "scale", | ||
"dependencies": { | ||
"bmp-js": "0.0.1", | ||
"file-type": "~2.5.0", | ||
"jpeg-js": "0.0.4", | ||
"mime": "~1.2.11", | ||
"stream-to-buffer": "~0.1.0", | ||
"node-png": "~0.4.3", | ||
"file-type": "~2.5.0", | ||
"read-chunk": "~1.0.1" | ||
"read-chunk": "~1.0.1", | ||
"stream-to-buffer": "~0.1.0" | ||
}, | ||
@@ -32,0 +34,0 @@ "repository": { |
@@ -5,3 +5,3 @@ # Jimp # | ||
An image processing library written entirely in JavaScript for Node, with zero external or native dependencies. | ||
An image processing library for Node written entirely in JavaScript, with zero external or native dependencies. | ||
@@ -26,3 +26,3 @@ Example usage: | ||
The Jimp constructor takes two arguments, the path to a JPEG or PNG image and a Node-style callback for when the image is parsed: | ||
The Jimp constructor takes two arguments, the path to a PNG, JPEG or BMP image and a Node-style callback: | ||
@@ -33,3 +33,3 @@ var image = new Jimp("./path/to/image.jpg", function (err, image) { | ||
Once the callback has fired the following methods can be called on the image: | ||
Once the callback has fired, the following methods can be called on the image: | ||
@@ -43,3 +43,3 @@ image.crop( x, y, w, h ); // crop to the given region | ||
image.sepia(); // apply a sepia wash to the image | ||
image.opacity( f ); // apply an opacity of 0-1 to the image | ||
image.opacity( f ); // multiply the alpha channel by each pixel by the factor f, 0 - 1 | ||
image.resize( w, h ); // resize the image | ||
@@ -50,9 +50,9 @@ image.scale( f ); // scale the image by the factor f | ||
image.composite( src, x, y ); // composites another Jimp image over this iamge at x, y | ||
this.brightness( val ); // adjust the brighness by a value -1 to +1 | ||
this.contrast( val ); // adjust the contrast by a value -1 to +1 | ||
this.posterize( n ); // apply a posterization effect with n level | ||
image.brightness( val ); // adjust the brighness by a value -1 to +1 | ||
image.contrast( val ); // adjust the contrast by a value -1 to +1 | ||
image.posterize( n ); // apply a posterization effect with n level | ||
(Contributions of more methods are welcome!) | ||
The image can be written to disk in JPEG or PNG format (determined by the file extension) using: | ||
The image can be written to disk in PNG, JPEG or BMP format (determined by the file extension) using: | ||
@@ -71,3 +71,3 @@ image.write( path, cb ); // Node-style callback will be fired when write is successful | ||
Alternatively, the Jimp constructor can also be called using an existing image create a clone of that image: | ||
The Jimp constructor can also be called using an existing image create a clone of that image: | ||
@@ -80,3 +80,3 @@ var clone = new Jimp(image, function (err, clone) { | ||
A PNG or JPEG binary Buffer of an image (e.g. for storage in a database) can to got using: | ||
A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can to got using: | ||
@@ -89,4 +89,5 @@ image.getBuffer( mime, cb ); // Node-style callback wil be fired with result | ||
Jimp.MIME_JPEG; // "image/jpeg" | ||
Jimp.BMP; // "image/bmp" | ||
A Jimp image can be instantiated from a Buffer by passing the Buffer as the first argument to the Jimp constructor: | ||
The Jimp constructor can also be called passing a valid Buffer as the first argument to the Jimp constructor: | ||
@@ -127,2 +128,8 @@ var image = new Jimp(buffer, function (err, image) { | ||
If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create: | ||
var image = new Jimp(256, 256, function (err, image) { | ||
// this image is 256 x 256, every pixel is set to 0x0 | ||
}); | ||
## Chaining or callbacks ## | ||
@@ -136,6 +143,6 @@ | ||
Alternatively, methods can be called Node-style callbacks: | ||
Alternatively, methods can be passed Node-style callbacks: | ||
var lenna = new Jimp("lenna.png", function (err, image) { | ||
this.greyscale(function(err, image) { | ||
image.greyscale(function(err, image) { | ||
image.scale(0.5, function (err, image) { | ||
@@ -147,3 +154,3 @@ image.write("lena-half-bw.png"); | ||
Among other things, the Node-style callback pattern allows Jimp to be used with frameworks that expect or build on the Node-style callback pattern. | ||
The Node-style callback pattern allows Jimp to be used with frameworks that expect or build on the Node-style callback pattern. | ||
@@ -150,0 +157,0 @@ ## License ## |
@@ -37,3 +37,3 @@ var Jimp = require("../jimp.js"); | ||
// Gaussian blur (destructive) | ||
// this.clone().gaussian(5).write("./output/lenna-gaussian.png"); | ||
this.clone().gaussian(5).write("./output/lenna-gaussian.png"); | ||
@@ -40,0 +40,0 @@ // resize (destructive) |
@@ -5,2 +5,3 @@ var Jimp = require("../jimp.js"); | ||
this.quality(1).write("./output/lenna-copy.jpg", loadJPEG); // JPEG copy | ||
this.write("./output/lenna-copy.bmp", loadBMP); // BMP copy | ||
}); | ||
@@ -12,2 +13,8 @@ | ||
}); | ||
} | ||
function loadBMP(){ | ||
var jpg = new Jimp("./output/lenna-copy.bmp", function(err, image) { | ||
jpg.invert().write("./output/lenna-invert.bmp"); | ||
}); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
760134
13
1322
149
7
+ Addedbmp-js@0.0.1
+ Addedbmp-js@0.0.1(transitive)