jimp 0.2.27 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/README.md +53 -10
- package/browser/.editorconfig +2 -0
- package/browser/README.md +2 -2
- package/browser/browserify-build.sh +2 -2
- package/browser/examples/dice.png +0 -0
- package/browser/examples/example1.html +3 -3
- package/browser/examples/jimp-worker.js +1 -1
- package/browser/examples/test.html +43 -0
- package/browser/lib/jimp.js +1 -3854
- package/browser/lib/jimp.min.js +2 -12
- package/index.js +372 -119
- package/jimp.d.ts +133 -0
- package/omggif.js +782 -0
- package/package.json +7 -4
- package/resize.js +120 -185
- package/resize2.js +15 -8
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ if (process.env.ENVIRONMENT !== "BROWSER") var FS = require("fs");
|
|
|
2
2
|
var PNG = require("pngjs").PNG;
|
|
3
3
|
var JPEG = require("jpeg-js");
|
|
4
4
|
var BMP = require("bmp-js");
|
|
5
|
+
var GIF = require("./omggif.js");
|
|
5
6
|
var MIME = require("mime");
|
|
6
7
|
var TinyColor = require("tinycolor2");
|
|
7
8
|
var Resize = require("./resize.js");
|
|
@@ -16,10 +17,11 @@ var BigNumber = require('bignumber.js');
|
|
|
16
17
|
var URLRegEx = require("url-regex");
|
|
17
18
|
var BMFont = require("load-bmfont");
|
|
18
19
|
var Path = require("path");
|
|
20
|
+
var MkDirP = require("mkdirp");
|
|
19
21
|
|
|
20
22
|
if (process.env.ENVIRONMENT !== "BROWSER") {
|
|
21
23
|
//If we run into electron renderer process, use XHR method instead of Request node module
|
|
22
|
-
if (process.versions.hasOwnProperty(
|
|
24
|
+
if (process.versions.hasOwnProperty("electron") && process.type === "renderer" && typeof XMLHttpRequest === "function") {
|
|
23
25
|
var Request = function (url,cb) {
|
|
24
26
|
var xhr = new XMLHttpRequest();
|
|
25
27
|
xhr.open( "GET", url, true );
|
|
@@ -79,7 +81,7 @@ function isNodePattern(cb) {
|
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
function throwError(error, cb) {
|
|
82
|
-
if ("string" == typeof error) error =
|
|
84
|
+
if ("string" == typeof error) error = console.error(error);
|
|
83
85
|
if ("function" == typeof cb) return cb.call(this, error);
|
|
84
86
|
else throw error;
|
|
85
87
|
}
|
|
@@ -115,7 +117,7 @@ function Jimp() {
|
|
|
115
117
|
var w = arguments[0];
|
|
116
118
|
var h = arguments[1];
|
|
117
119
|
var cb = arguments[2];
|
|
118
|
-
|
|
120
|
+
|
|
119
121
|
if ("number" == typeof arguments[2]) {
|
|
120
122
|
this._background = arguments[2];
|
|
121
123
|
var cb = arguments[3];
|
|
@@ -136,7 +138,7 @@ function Jimp() {
|
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
cb.call(this, null, this);
|
|
139
|
-
} else if (
|
|
141
|
+
} else if (arguments[0] instanceof Jimp) {
|
|
140
142
|
// clone an existing Jimp
|
|
141
143
|
var original = arguments[0];
|
|
142
144
|
var cb = arguments[1];
|
|
@@ -169,11 +171,11 @@ function Jimp() {
|
|
|
169
171
|
// read from a URL
|
|
170
172
|
var url = arguments[0];
|
|
171
173
|
var cb = arguments[1];
|
|
172
|
-
|
|
174
|
+
|
|
173
175
|
if ("undefined" == typeof cb) cb = noop;
|
|
174
176
|
if ("function" != typeof cb)
|
|
175
177
|
return throwError.call(this, "cb must be a function", cb);
|
|
176
|
-
|
|
178
|
+
|
|
177
179
|
var that = this;
|
|
178
180
|
Request(url, function (err, response, data) {
|
|
179
181
|
if (err) return throwError.call(that, err, cb);
|
|
@@ -188,7 +190,7 @@ function Jimp() {
|
|
|
188
190
|
// read from a path
|
|
189
191
|
var path = arguments[0];
|
|
190
192
|
var cb = arguments[1];
|
|
191
|
-
|
|
193
|
+
|
|
192
194
|
if ("undefined" == typeof cb) cb = noop;
|
|
193
195
|
if ("function" != typeof cb)
|
|
194
196
|
return throwError.call(this, "cb must be a function", cb);
|
|
@@ -271,12 +273,25 @@ function getMIMEFromPath(path, cb) {
|
|
|
271
273
|
|
|
272
274
|
//=> {ext: 'png', mime: 'image/png'}
|
|
273
275
|
|
|
276
|
+
// gets image data from a GIF buffer
|
|
277
|
+
function getBitmapFromGIF(data){
|
|
278
|
+
var gifObj = new GIF.GifReader(data);
|
|
279
|
+
var gifData = new Buffer(gifObj.width * gifObj.height * 4);
|
|
280
|
+
|
|
281
|
+
gifObj.decodeAndBlitFrameRGBA(0, gifData);
|
|
282
|
+
return {
|
|
283
|
+
data: gifData,
|
|
284
|
+
width: gifObj.width,
|
|
285
|
+
height: gifObj.height
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
274
289
|
// parses a bitmap from the constructor to the JIMP bitmap property
|
|
275
290
|
function parseBitmap(data, mime, cb) {
|
|
276
291
|
var that = this;
|
|
277
|
-
this._originalMime = mime;
|
|
292
|
+
this._originalMime = mime.toLowerCase();
|
|
278
293
|
|
|
279
|
-
switch (
|
|
294
|
+
switch (this.getMIME()) {
|
|
280
295
|
case Jimp.MIME_PNG:
|
|
281
296
|
var png = new PNG();
|
|
282
297
|
png.parse(data, function(err, data) {
|
|
@@ -293,14 +308,20 @@ function parseBitmap(data, mime, cb) {
|
|
|
293
308
|
case Jimp.MIME_JPEG:
|
|
294
309
|
try {
|
|
295
310
|
this.bitmap = JPEG.decode(data);
|
|
296
|
-
|
|
311
|
+
try { this._exif = EXIFParser.create(data).parse(); }
|
|
312
|
+
catch (err) { /* meh */ }
|
|
297
313
|
return cb.call(this, null, this);
|
|
298
314
|
} catch(err) {
|
|
299
315
|
return cb.call(this, err, this);
|
|
300
316
|
}
|
|
301
317
|
|
|
302
318
|
case Jimp.MIME_BMP:
|
|
303
|
-
|
|
319
|
+
case Jimp.MIME_X_MS_BMP:
|
|
320
|
+
this.bitmap = BMP.decode(data);
|
|
321
|
+
return cb.call(this, null, this);
|
|
322
|
+
|
|
323
|
+
case Jimp.MIME_GIF:
|
|
324
|
+
this.bitmap = getBitmapFromGIF(data);
|
|
304
325
|
return cb.call(this, null, this);
|
|
305
326
|
|
|
306
327
|
default:
|
|
@@ -308,48 +329,6 @@ function parseBitmap(data, mime, cb) {
|
|
|
308
329
|
}
|
|
309
330
|
}
|
|
310
331
|
|
|
311
|
-
/*
|
|
312
|
-
* Automagically rotates a JPEG image based on its EXIF data
|
|
313
|
-
* @params image a Jimp image
|
|
314
|
-
* @params buffer a buffer array of the raw JPEG data
|
|
315
|
-
* @returns nothing
|
|
316
|
-
*/
|
|
317
|
-
function exifRotate(image, buffer) {
|
|
318
|
-
var exif;
|
|
319
|
-
try {
|
|
320
|
-
exif = EXIFParser.create(buffer).parse();
|
|
321
|
-
} catch (err) {
|
|
322
|
-
return; // meh
|
|
323
|
-
}
|
|
324
|
-
if (!exif || !exif.tags || !exif.tags.Orientation) return;
|
|
325
|
-
switch (exif.tags.Orientation) {
|
|
326
|
-
case 1: // Horizontal (normal)
|
|
327
|
-
// do nothing
|
|
328
|
-
break;
|
|
329
|
-
case 2: // Mirror horizontal
|
|
330
|
-
image.mirror(true, false);
|
|
331
|
-
break;
|
|
332
|
-
case 3: // Rotate 180
|
|
333
|
-
image.rotate(180);
|
|
334
|
-
break;
|
|
335
|
-
case 4: // Mirror vertical
|
|
336
|
-
image.mirror(false, true);
|
|
337
|
-
break;
|
|
338
|
-
case 5: // Mirror horizontal and rotate 270 CW
|
|
339
|
-
image.mirror(true, false).rotate(270);
|
|
340
|
-
break;
|
|
341
|
-
case 6: // Rotate 90 CW
|
|
342
|
-
image.rotate(90);
|
|
343
|
-
break;
|
|
344
|
-
case 7: // Mirror horizontal and rotate 90 CW
|
|
345
|
-
image.mirror(true, false).rotate(90);
|
|
346
|
-
break;
|
|
347
|
-
case 8: // Rotate 270 CW
|
|
348
|
-
image.rotate(270);
|
|
349
|
-
break;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
332
|
// used to auto resizing etc.
|
|
354
333
|
Jimp.AUTO = -1;
|
|
355
334
|
|
|
@@ -357,6 +336,8 @@ Jimp.AUTO = -1;
|
|
|
357
336
|
Jimp.MIME_PNG = "image/png";
|
|
358
337
|
Jimp.MIME_JPEG = "image/jpeg";
|
|
359
338
|
Jimp.MIME_BMP = "image/bmp";
|
|
339
|
+
Jimp.MIME_X_MS_BMP = "image/x-ms-bmp";
|
|
340
|
+
Jimp.MIME_GIF = "image/gif";
|
|
360
341
|
|
|
361
342
|
// PNG filter types
|
|
362
343
|
Jimp.PNG_FILTER_AUTO = -1;
|
|
@@ -394,6 +375,11 @@ Jimp.FONT_SANS_32_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-32-whi
|
|
|
394
375
|
Jimp.FONT_SANS_64_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt");
|
|
395
376
|
Jimp.FONT_SANS_128_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt");
|
|
396
377
|
|
|
378
|
+
// Edge Handling
|
|
379
|
+
Jimp.EDGE_EXTEND = 1;
|
|
380
|
+
Jimp.EDGE_WRAP = 2;
|
|
381
|
+
Jimp.EDGE_CROP = 3;
|
|
382
|
+
|
|
397
383
|
/**
|
|
398
384
|
* A static helper method that converts RGBA values to a single integer value
|
|
399
385
|
* @param r the red value (0-255)
|
|
@@ -414,9 +400,14 @@ Jimp.rgbaToInt = function(r, g, b, a, cb){
|
|
|
414
400
|
return throwError.call(this, "b must be between 0 and 255", cb);
|
|
415
401
|
if (a < 0 || a > 255)
|
|
416
402
|
return throwError.call(this, "a must be between 0 and 255", cb);
|
|
417
|
-
|
|
403
|
+
|
|
404
|
+
r = Math.round(r);
|
|
405
|
+
b = Math.round(b);
|
|
406
|
+
g = Math.round(g);
|
|
407
|
+
a = Math.round(a);
|
|
408
|
+
|
|
418
409
|
var i = (r * Math.pow(256, 3)) + (g * Math.pow(256, 2)) + (b * Math.pow(256, 1)) + (a * Math.pow(256, 0));
|
|
419
|
-
|
|
410
|
+
|
|
420
411
|
if (isNodePattern(cb)) return cb.call(this, null, i);
|
|
421
412
|
else return i;
|
|
422
413
|
}
|
|
@@ -430,13 +421,13 @@ Jimp.rgbaToInt = function(r, g, b, a, cb){
|
|
|
430
421
|
Jimp.intToRGBA = function(i, cb){
|
|
431
422
|
if ("number" != typeof i)
|
|
432
423
|
return throwError.call(this, "i must be a number", cb);
|
|
433
|
-
|
|
424
|
+
|
|
434
425
|
var rgba = {}
|
|
435
426
|
rgba.r = Math.floor(i / Math.pow(256, 3));
|
|
436
427
|
rgba.g = Math.floor((i - (rgba.r * Math.pow(256, 3))) / Math.pow(256, 2));
|
|
437
428
|
rgba.b = Math.floor((i - (rgba.r * Math.pow(256, 3)) - (rgba.g * Math.pow(256, 2))) / Math.pow(256, 1));
|
|
438
429
|
rgba.a = Math.floor((i - (rgba.r * Math.pow(256, 3)) - (rgba.g * Math.pow(256, 2)) - (rgba.b * Math.pow(256, 1))) / Math.pow(256, 0));
|
|
439
|
-
|
|
430
|
+
|
|
440
431
|
if (isNodePattern(cb)) return cb.call(this, null, rgba);
|
|
441
432
|
else return rgba;
|
|
442
433
|
}
|
|
@@ -462,7 +453,7 @@ Jimp.limit255 = function(n) {
|
|
|
462
453
|
* @returns an object { percent: percent similar, diff: a Jimp image highlighting differences }
|
|
463
454
|
*/
|
|
464
455
|
Jimp.diff = function (img1, img2, threshold) {
|
|
465
|
-
if (
|
|
456
|
+
if (!(img1 instanceof Jimp) || !(img2 instanceof Jimp))
|
|
466
457
|
return throwError.call(this, "img1 and img2 must be an Jimp images");
|
|
467
458
|
|
|
468
459
|
if (img1.bitmap.width != img2.bitmap.width || img1.bitmap.height != img2.bitmap.height) {
|
|
@@ -476,7 +467,7 @@ Jimp.diff = function (img1, img2, threshold) {
|
|
|
476
467
|
break;
|
|
477
468
|
}
|
|
478
469
|
}
|
|
479
|
-
|
|
470
|
+
|
|
480
471
|
threshold = threshold || 0.1;
|
|
481
472
|
if ("number" != typeof threshold || threshold < 0 || threshold > 1)
|
|
482
473
|
return throwError.call(this, "threshold must be a number between 0 and 1");
|
|
@@ -491,7 +482,7 @@ Jimp.diff = function (img1, img2, threshold) {
|
|
|
491
482
|
diff.bitmap.height,
|
|
492
483
|
{threshold: threshold}
|
|
493
484
|
);
|
|
494
|
-
|
|
485
|
+
|
|
495
486
|
return {
|
|
496
487
|
percent: numDiffPixels / (diff.bitmap.width * diff.bitmap.height),
|
|
497
488
|
image: diff
|
|
@@ -535,6 +526,12 @@ Jimp.prototype._rgba = true;
|
|
|
535
526
|
// Default colour to use for new pixels
|
|
536
527
|
Jimp.prototype._background = 0x00000000;
|
|
537
528
|
|
|
529
|
+
// Default MIME is PNG
|
|
530
|
+
Jimp.prototype._originalMime = Jimp.MIME_PNG;
|
|
531
|
+
|
|
532
|
+
// Exif data for the image
|
|
533
|
+
Jimp.prototype._exif = null;
|
|
534
|
+
|
|
538
535
|
/**
|
|
539
536
|
* Creates a new image that is a clone of this one.
|
|
540
537
|
* @param cb (optional) A callback for when complete
|
|
@@ -687,26 +684,65 @@ Jimp.prototype.scan = function (x, y, w, h, f, cb) {
|
|
|
687
684
|
else return this;
|
|
688
685
|
};
|
|
689
686
|
|
|
687
|
+
/**
|
|
688
|
+
* Returns the original MIME of the image (default: "image/png")
|
|
689
|
+
* @returns the MIME as a string
|
|
690
|
+
*/
|
|
691
|
+
Jimp.prototype.getMIME = function(){
|
|
692
|
+
var mime = this._originalMime || Jimp.MIME_PNG;
|
|
693
|
+
return mime;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Returns the appropriate file extension for the original MIME of the image (default: "png")
|
|
698
|
+
* @returns the file extension as a string
|
|
699
|
+
*/
|
|
700
|
+
Jimp.prototype.getExtension = function(){
|
|
701
|
+
var mime = this.getMIME();
|
|
702
|
+
return MIME.extension(mime);
|
|
703
|
+
}
|
|
704
|
+
|
|
690
705
|
/**
|
|
691
706
|
* Returns the offset of a pixel in the bitmap buffer
|
|
692
707
|
* @param x the x coordinate
|
|
693
708
|
* @param y the y coordinate
|
|
709
|
+
* @param (optional) edgeHandling define how to sum pixels from outside the border
|
|
694
710
|
* @param (optional) cb a callback for when complete
|
|
695
711
|
* @returns the index of the pixel or -1 if not found
|
|
696
712
|
*/
|
|
697
|
-
Jimp.prototype.getPixelIndex = function (x, y, cb) {
|
|
713
|
+
Jimp.prototype.getPixelIndex = function (x, y, edgeHandling, cb) {
|
|
714
|
+
var xi, yi;
|
|
715
|
+
if ("function" == typeof edgeHandling && "undefined" == typeof cb) {
|
|
716
|
+
cb = edgeHandling;
|
|
717
|
+
edgeHandling = null;
|
|
718
|
+
}
|
|
719
|
+
if (!edgeHandling) edgeHandling = Jimp.EDGE_EXTEND;
|
|
698
720
|
if ("number" != typeof x || "number" != typeof y)
|
|
699
721
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
700
722
|
|
|
701
723
|
// round input
|
|
702
|
-
x = Math.round(x);
|
|
703
|
-
y = Math.round(y);
|
|
724
|
+
xi = x = Math.round(x);
|
|
725
|
+
yi = y = Math.round(y);
|
|
726
|
+
|
|
727
|
+
if (edgeHandling == Jimp.EDGE_EXTEND) {
|
|
728
|
+
if (x<0) xi = 0;
|
|
729
|
+
if (x>=this.bitmap.width) xi = this.bitmap.width - 1;
|
|
730
|
+
if (y<0) yi = 0;
|
|
731
|
+
if (y>=this.bitmap.height) yi = this.bitmap.height - 1;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (edgeHandling == Jimp.EDGE_WRAP) {
|
|
735
|
+
if (x<0) xi = this.bitmap.width + x;
|
|
736
|
+
if (x>=this.bitmap.width) xi = x % this.bitmap.width;
|
|
737
|
+
if (y<0) xi = this.bitmap.height + y;
|
|
738
|
+
if (y>=this.bitmap.height) yi = y % this.bitmap.height;
|
|
739
|
+
}
|
|
704
740
|
|
|
705
|
-
var i = (this.bitmap.width *
|
|
741
|
+
var i = (this.bitmap.width * yi + xi) << 2;
|
|
706
742
|
|
|
707
743
|
// if out of bounds index is -1
|
|
708
|
-
if (
|
|
709
|
-
if (
|
|
744
|
+
if (xi < 0 || xi >= this.bitmap.width) i = -1;
|
|
745
|
+
if (yi < 0 || yi >= this.bitmap.height) i = -1;
|
|
710
746
|
|
|
711
747
|
if (isNodePattern(cb)) return cb.call(this, null, i);
|
|
712
748
|
else return i;
|
|
@@ -726,10 +762,10 @@ Jimp.prototype.getPixelColor = Jimp.prototype.getPixelColour = function (x, y, c
|
|
|
726
762
|
// round input
|
|
727
763
|
x = Math.round(x);
|
|
728
764
|
y = Math.round(y);
|
|
729
|
-
|
|
765
|
+
|
|
730
766
|
var idx = this.getPixelIndex(x, y);
|
|
731
767
|
var hex = this.bitmap.data.readUInt32BE(idx);
|
|
732
|
-
|
|
768
|
+
|
|
733
769
|
if (isNodePattern(cb)) return cb.call(this, null, hex);
|
|
734
770
|
else return hex;
|
|
735
771
|
};
|
|
@@ -748,10 +784,10 @@ Jimp.prototype.setPixelColor = Jimp.prototype.setPixelColour = function (hex, x,
|
|
|
748
784
|
// round input
|
|
749
785
|
x = Math.round(x);
|
|
750
786
|
y = Math.round(y);
|
|
751
|
-
|
|
787
|
+
|
|
752
788
|
var idx = this.getPixelIndex(x, y);
|
|
753
789
|
this.bitmap.data.writeUInt32BE(hex, idx, true);
|
|
754
|
-
|
|
790
|
+
|
|
755
791
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
756
792
|
else return this;
|
|
757
793
|
};
|
|
@@ -780,19 +816,59 @@ Jimp.prototype.hash = function(base, cb){
|
|
|
780
816
|
return throwError.call(this, "base must be a number", cb);
|
|
781
817
|
if (base < 2 || base > 64)
|
|
782
818
|
return throwError.call(this, "base must be a number between 2 and 64", cb);
|
|
783
|
-
|
|
819
|
+
|
|
784
820
|
var hash = (new ImagePHash()).getHash(this);
|
|
785
821
|
hash = (new BigNumber(hash, 2)).toString(base);
|
|
786
|
-
|
|
822
|
+
|
|
787
823
|
while (hash.length < maxHashLength[base]) {
|
|
788
824
|
hash = "0" + hash; // pad out with leading zeros
|
|
789
825
|
}
|
|
790
|
-
|
|
826
|
+
|
|
791
827
|
if (isNodePattern(cb)) return cb.call(this, null, hash);
|
|
792
828
|
else return hash;
|
|
793
829
|
}
|
|
794
830
|
|
|
795
831
|
|
|
832
|
+
/*
|
|
833
|
+
* Automagically rotates an image based on its EXIF data (if present)
|
|
834
|
+
* @param (optional) cb a callback for when complete
|
|
835
|
+
* @returns this for chaining of methods
|
|
836
|
+
*/
|
|
837
|
+
Jimp.prototype.exifRotate = function (cb) {
|
|
838
|
+
if (this._exif && this._exif.tags && this._exif.tags.Orientation) {
|
|
839
|
+
switch (this._exif.tags.Orientation) {
|
|
840
|
+
case 1: // Horizontal (normal)
|
|
841
|
+
// do nothing
|
|
842
|
+
break;
|
|
843
|
+
case 2: // Mirror horizontal
|
|
844
|
+
this.mirror(true, false);
|
|
845
|
+
break;
|
|
846
|
+
case 3: // Rotate 180
|
|
847
|
+
this.rotate(180);
|
|
848
|
+
break;
|
|
849
|
+
case 4: // Mirror vertical
|
|
850
|
+
this.mirror(false, true);
|
|
851
|
+
break;
|
|
852
|
+
case 5: // Mirror horizontal and rotate 270 CW
|
|
853
|
+
this.mirror(true, false).rotate(270);
|
|
854
|
+
break;
|
|
855
|
+
case 6: // Rotate 90 CW
|
|
856
|
+
this.rotate(90);
|
|
857
|
+
break;
|
|
858
|
+
case 7: // Mirror horizontal and rotate 90 CW
|
|
859
|
+
this.mirror(true, false).rotate(90);
|
|
860
|
+
break;
|
|
861
|
+
case 8: // Rotate 270 CW
|
|
862
|
+
this.rotate(270);
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
868
|
+
else return this;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
|
|
796
872
|
/**
|
|
797
873
|
* Crops the image at a given point to a give size
|
|
798
874
|
* @param x the x coordinate to crop form
|
|
@@ -901,8 +977,8 @@ Jimp.prototype.autocrop = function() {
|
|
|
901
977
|
}
|
|
902
978
|
|
|
903
979
|
east: // east side (scan columns from east to west)
|
|
904
|
-
for (var x =
|
|
905
|
-
for (var y =
|
|
980
|
+
for (var x = w - 1; x >= 0; x--) {
|
|
981
|
+
for (var y = northPixelsToCrop; y < h; y++) {
|
|
906
982
|
var colorXY = this.getPixelColor(x, y);
|
|
907
983
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
908
984
|
var difference =
|
|
@@ -924,7 +1000,7 @@ Jimp.prototype.autocrop = function() {
|
|
|
924
1000
|
colorTarget = this.getPixelColor(w - 1, h - 1); // bottom right pixel color is the target color
|
|
925
1001
|
// for south and west sides
|
|
926
1002
|
south: // south side (scan rows from south to north)
|
|
927
|
-
for (var y = h - 1; y >=
|
|
1003
|
+
for (var y = h - 1; y >= northPixelsToCrop + minPixelsPerSide; y--) {
|
|
928
1004
|
for (var x = w - eastPixelsToCrop - 1; x >= 0; x--) {
|
|
929
1005
|
var colorXY = this.getPixelColor(x, y);
|
|
930
1006
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
@@ -945,8 +1021,8 @@ Jimp.prototype.autocrop = function() {
|
|
|
945
1021
|
}
|
|
946
1022
|
|
|
947
1023
|
west: // west side (scan columns from west to east)
|
|
948
|
-
for (var x =
|
|
949
|
-
for (var y = h -
|
|
1024
|
+
for (var x = 0; x <= w - eastPixelsToCrop - minPixelsPerSide; x++) {
|
|
1025
|
+
for (var y = h - southPixelsToCrop; y >= northPixelsToCrop; y--) {
|
|
950
1026
|
var colorXY = this.getPixelColor(x, y);
|
|
951
1027
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
952
1028
|
var difference =
|
|
@@ -991,7 +1067,7 @@ Jimp.prototype.autocrop = function() {
|
|
|
991
1067
|
|
|
992
1068
|
if (doCrop) { // do the real crop
|
|
993
1069
|
this.crop(
|
|
994
|
-
|
|
1070
|
+
westPixelsToCrop,
|
|
995
1071
|
northPixelsToCrop,
|
|
996
1072
|
widthOfPixelsToCrop,
|
|
997
1073
|
heightOfPixelsToCrop
|
|
@@ -1015,12 +1091,12 @@ Jimp.prototype.autocrop = function() {
|
|
|
1015
1091
|
* @returns this for chaining of methods
|
|
1016
1092
|
*/
|
|
1017
1093
|
Jimp.prototype.blit = function (src, x, y, srcx, srcy, srcw, srch, cb) {
|
|
1018
|
-
if (
|
|
1094
|
+
if (!(src instanceof Jimp))
|
|
1019
1095
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
1020
1096
|
if ("number" != typeof x || "number" != typeof y)
|
|
1021
1097
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
1022
1098
|
|
|
1023
|
-
if ("function"
|
|
1099
|
+
if (typeof srcx === "function") {
|
|
1024
1100
|
cb = srcx;
|
|
1025
1101
|
srcx = 0;
|
|
1026
1102
|
srcy = 0;
|
|
@@ -1068,7 +1144,7 @@ Jimp.prototype.blit = function (src, x, y, srcx, srcy, srcw, srch, cb) {
|
|
|
1068
1144
|
* @returns this for chaining of methods
|
|
1069
1145
|
*/
|
|
1070
1146
|
Jimp.prototype.mask = function (src, x, y, cb) {
|
|
1071
|
-
if (
|
|
1147
|
+
if (!(src instanceof Jimp))
|
|
1072
1148
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
1073
1149
|
if ("number" != typeof x || "number" != typeof y)
|
|
1074
1150
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
@@ -1097,7 +1173,7 @@ Jimp.prototype.mask = function (src, x, y, cb) {
|
|
|
1097
1173
|
* @returns this for chaining of methods
|
|
1098
1174
|
*/
|
|
1099
1175
|
Jimp.prototype.composite = function (src, x, y, cb) {
|
|
1100
|
-
if (
|
|
1176
|
+
if (!(src instanceof Jimp))
|
|
1101
1177
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
1102
1178
|
if ("number" != typeof x || "number" != typeof y)
|
|
1103
1179
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
@@ -1110,27 +1186,27 @@ Jimp.prototype.composite = function (src, x, y, cb) {
|
|
|
1110
1186
|
src.scan(0, 0, src.bitmap.width, src.bitmap.height, function(sx, sy, idx) {
|
|
1111
1187
|
// http://stackoverflow.com/questions/7438263/alpha-compositing-algorithm-blend-modes
|
|
1112
1188
|
var dstIdx = that.getPixelIndex(x+sx, y+sy);
|
|
1113
|
-
|
|
1189
|
+
|
|
1114
1190
|
var fg = {
|
|
1115
1191
|
r: this.bitmap.data[idx + 0] / 255,
|
|
1116
1192
|
g: this.bitmap.data[idx + 1] / 255,
|
|
1117
1193
|
b: this.bitmap.data[idx + 2] / 255,
|
|
1118
1194
|
a: this.bitmap.data[idx + 3] / 255
|
|
1119
1195
|
}
|
|
1120
|
-
|
|
1196
|
+
|
|
1121
1197
|
var bg = {
|
|
1122
1198
|
r: that.bitmap.data[dstIdx + 0] / 255,
|
|
1123
1199
|
g: that.bitmap.data[dstIdx + 1] / 255,
|
|
1124
1200
|
b: that.bitmap.data[dstIdx + 2] / 255,
|
|
1125
1201
|
a: that.bitmap.data[dstIdx + 3] / 255
|
|
1126
1202
|
}
|
|
1127
|
-
|
|
1203
|
+
|
|
1128
1204
|
var a = bg.a + fg.a - bg.a * fg.a;
|
|
1129
|
-
|
|
1205
|
+
|
|
1130
1206
|
var r = ((fg.r * fg.a) + (bg.r * bg.a) * (1 - fg.a)) / a;
|
|
1131
1207
|
var g = ((fg.g * fg.a) + (bg.g * bg.a) * (1 - fg.a)) / a;
|
|
1132
1208
|
var b = ((fg.b * fg.a) + (bg.b * bg.a) * (1 - fg.a)) / a;
|
|
1133
|
-
|
|
1209
|
+
|
|
1134
1210
|
that.bitmap.data[dstIdx + 0] = Jimp.limit255(r * 255);
|
|
1135
1211
|
that.bitmap.data[dstIdx + 1] = Jimp.limit255(g * 255);
|
|
1136
1212
|
that.bitmap.data[dstIdx + 2] = Jimp.limit255(b * 255);
|
|
@@ -1627,7 +1703,7 @@ Jimp.prototype.fade = function (f, cb) {
|
|
|
1627
1703
|
|
|
1628
1704
|
// this method is an alternative to opacity (which may be deprecated)
|
|
1629
1705
|
this.opacity(1 - f);
|
|
1630
|
-
|
|
1706
|
+
|
|
1631
1707
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1632
1708
|
else return this;
|
|
1633
1709
|
};
|
|
@@ -1657,18 +1733,18 @@ Jimp.prototype.opaque = function (cb) {
|
|
|
1657
1733
|
Jimp.prototype.resize = function (w, h, mode, cb) {
|
|
1658
1734
|
if ("number" != typeof w || "number" != typeof h)
|
|
1659
1735
|
return throwError.call(this, "w and h must be numbers", cb);
|
|
1660
|
-
|
|
1736
|
+
|
|
1661
1737
|
if ("function" == typeof mode && "undefined" == typeof cb) {
|
|
1662
1738
|
cb = mode;
|
|
1663
1739
|
mode = null;
|
|
1664
1740
|
}
|
|
1665
|
-
|
|
1741
|
+
|
|
1666
1742
|
if (w == Jimp.AUTO && h == Jimp.AUTO)
|
|
1667
1743
|
return throwError.call(this, "w and h cannot both the set to auto", cb);
|
|
1668
1744
|
|
|
1669
1745
|
if (w == Jimp.AUTO) w = this.bitmap.width * (h / this.bitmap.height);
|
|
1670
1746
|
if (h == Jimp.AUTO) h = this.bitmap.height * (w / this.bitmap.width);
|
|
1671
|
-
|
|
1747
|
+
|
|
1672
1748
|
// round inputs
|
|
1673
1749
|
w = Math.round(w);
|
|
1674
1750
|
h = Math.round(h);
|
|
@@ -1727,12 +1803,12 @@ Jimp.prototype.cover = function (w, h, alignBits, mode, cb) {
|
|
|
1727
1803
|
|
|
1728
1804
|
var align_h = (hbits >> 1); // 0, 1, 2
|
|
1729
1805
|
var align_v = (vbits >> 1); // 0, 1, 2
|
|
1730
|
-
|
|
1806
|
+
|
|
1731
1807
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1732
1808
|
w/this.bitmap.width : h/this.bitmap.height;
|
|
1733
1809
|
this.scale(f, mode);
|
|
1734
1810
|
this.crop(((this.bitmap.width - w) / 2) * align_h, ((this.bitmap.height - h) / 2) * align_v, w, h);
|
|
1735
|
-
|
|
1811
|
+
|
|
1736
1812
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1737
1813
|
else return this;
|
|
1738
1814
|
};
|
|
@@ -1766,7 +1842,7 @@ Jimp.prototype.contain = function (w, h, alignBits, mode, cb) {
|
|
|
1766
1842
|
mode = null;
|
|
1767
1843
|
}
|
|
1768
1844
|
}
|
|
1769
|
-
|
|
1845
|
+
|
|
1770
1846
|
alignBits = alignBits || (Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);
|
|
1771
1847
|
var hbits = ((alignBits) & ((1<<(3))-1));
|
|
1772
1848
|
var vbits = alignBits >> 3;
|
|
@@ -1781,7 +1857,7 @@ Jimp.prototype.contain = function (w, h, alignBits, mode, cb) {
|
|
|
1781
1857
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1782
1858
|
h/this.bitmap.height : w/this.bitmap.width;
|
|
1783
1859
|
var c = this.clone().scale(f, mode);
|
|
1784
|
-
|
|
1860
|
+
|
|
1785
1861
|
this.resize(w, h, mode);
|
|
1786
1862
|
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
1787
1863
|
this.bitmap.data.writeUInt32BE(this._background, idx);
|
|
@@ -1809,7 +1885,7 @@ Jimp.prototype.scale = function (f, mode, cb) {
|
|
|
1809
1885
|
cb = mode;
|
|
1810
1886
|
mode = null;
|
|
1811
1887
|
}
|
|
1812
|
-
|
|
1888
|
+
|
|
1813
1889
|
var w = this.bitmap.width * f;
|
|
1814
1890
|
var h = this.bitmap.height * f;
|
|
1815
1891
|
this.resize(w, h, mode);
|
|
@@ -1834,7 +1910,7 @@ Jimp.prototype.scaleToFit = function (w, h, mode, cb) {
|
|
|
1834
1910
|
cb = mode;
|
|
1835
1911
|
mode = null;
|
|
1836
1912
|
}
|
|
1837
|
-
|
|
1913
|
+
|
|
1838
1914
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1839
1915
|
h/this.bitmap.height : w/this.bitmap.width;
|
|
1840
1916
|
this.scale(f, mode);
|
|
@@ -1843,6 +1919,134 @@ Jimp.prototype.scaleToFit = function (w, h, mode, cb) {
|
|
|
1843
1919
|
else return this;
|
|
1844
1920
|
};
|
|
1845
1921
|
|
|
1922
|
+
/**
|
|
1923
|
+
* Pixelates the image or a region
|
|
1924
|
+
* @param size the size of the pixels
|
|
1925
|
+
* @param (optional) x the x position of the region to pixelate
|
|
1926
|
+
* @param (optional) y the y position of the region to pixelate
|
|
1927
|
+
* @param (optional) w the width of the region to pixelate
|
|
1928
|
+
* @param (optional) h the height of the region to pixelate
|
|
1929
|
+
* @param (optional) cb a callback for when complete
|
|
1930
|
+
* @returns this for chaining of methods
|
|
1931
|
+
*/
|
|
1932
|
+
Jimp.prototype.pixelate = function (size, x, y, w, h, cb) {
|
|
1933
|
+
|
|
1934
|
+
if ("function" == typeof x) {
|
|
1935
|
+
cb = x;
|
|
1936
|
+
x = y = w = h = undefined;
|
|
1937
|
+
} else {
|
|
1938
|
+
if ("number" != typeof size)
|
|
1939
|
+
return throwError.call(this, "size must be a number", cb);
|
|
1940
|
+
if (x !== undefined)
|
|
1941
|
+
if ("number" != typeof x)
|
|
1942
|
+
return throwError.call(this, "x must be a number", cb);
|
|
1943
|
+
if (y !== undefined)
|
|
1944
|
+
if ("number" != typeof y)
|
|
1945
|
+
return throwError.call(this, "y must be a number", cb);
|
|
1946
|
+
if (w !== undefined)
|
|
1947
|
+
if ("number" != typeof w)
|
|
1948
|
+
return throwError.call(this, "w must be a number", cb);
|
|
1949
|
+
if (h !== undefined)
|
|
1950
|
+
if ("number" != typeof h)
|
|
1951
|
+
return throwError.call(this, "h must be a number", cb);
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
|
|
1955
|
+
var kernel = [
|
|
1956
|
+
[1 / 16, 2 / 16, 1 / 16],
|
|
1957
|
+
[2 / 16, 4 / 16, 2 / 16],
|
|
1958
|
+
[1 / 16, 2 / 16, 1 / 16]
|
|
1959
|
+
];
|
|
1960
|
+
|
|
1961
|
+
x = x !== undefined ? x : 0;
|
|
1962
|
+
y = y !== undefined ? y : 0;
|
|
1963
|
+
w = w !== undefined ? w : this.bitmap.width - x;
|
|
1964
|
+
h = h !== undefined ? h : this.bitmap.height - y;
|
|
1965
|
+
|
|
1966
|
+
var source = this.clone();
|
|
1967
|
+
this.scan(x, y, w, h, function (xx, yx, idx) {
|
|
1968
|
+
|
|
1969
|
+
xx = size * Math.floor(xx / size);
|
|
1970
|
+
yx = size * Math.floor(yx / size);
|
|
1971
|
+
|
|
1972
|
+
var value = applyKernel(source, kernel, xx, yx);
|
|
1973
|
+
|
|
1974
|
+
this.bitmap.data[idx] = value[0];
|
|
1975
|
+
this.bitmap.data[idx + 1] = value[1];
|
|
1976
|
+
this.bitmap.data[idx + 2] = value[2];
|
|
1977
|
+
});
|
|
1978
|
+
|
|
1979
|
+
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1980
|
+
else return this;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
/**
|
|
1984
|
+
* Applies a convolution kernel to the image or a region
|
|
1985
|
+
* @param kernel the convolution kernel
|
|
1986
|
+
* @param (optional) x the x position of the region to apply convolution to
|
|
1987
|
+
* @param (optional) y the y position of the region to apply convolution to
|
|
1988
|
+
* @param (optional) w the width of the region to apply convolution to
|
|
1989
|
+
* @param (optional) h the height of the region to apply convolution to
|
|
1990
|
+
* @param (optional) cb a callback for when complete
|
|
1991
|
+
* @returns this for chaining of methods
|
|
1992
|
+
*/
|
|
1993
|
+
Jimp.prototype.convolute = function (kernel, x, y, w, h, cb) {
|
|
1994
|
+
if (!Array.isArray(kernel))
|
|
1995
|
+
return throwError.call(this, "the kernel must be an array", cb);
|
|
1996
|
+
|
|
1997
|
+
if ("function" == typeof x) {
|
|
1998
|
+
cb = x;
|
|
1999
|
+
x = y = w = h = undefined;
|
|
2000
|
+
} else {
|
|
2001
|
+
if (x !== undefined)
|
|
2002
|
+
if ("number" != typeof x)
|
|
2003
|
+
return throwError.call(this, "x must be a number", cb);
|
|
2004
|
+
if (y !== undefined)
|
|
2005
|
+
if ("number" != typeof y)
|
|
2006
|
+
return throwError.call(this, "y must be a number", cb);
|
|
2007
|
+
if (w !== undefined)
|
|
2008
|
+
if ("number" != typeof w)
|
|
2009
|
+
return throwError.call(this, "w must be a number", cb);
|
|
2010
|
+
if (h !== undefined)
|
|
2011
|
+
if ("number" != typeof h)
|
|
2012
|
+
return throwError.call(this, "h must be a number", cb);
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
var ksize = (kernel.length - 1) / 2;
|
|
2016
|
+
|
|
2017
|
+
x = x !== undefined ? x : ksize;
|
|
2018
|
+
y = y !== undefined ? y : ksize;
|
|
2019
|
+
w = w !== undefined ? w : this.bitmap.width - x;
|
|
2020
|
+
h = h !== undefined ? h : this.bitmap.height - y;
|
|
2021
|
+
|
|
2022
|
+
var source = this.clone();
|
|
2023
|
+
this.scan(x, y, w, h, function (xx, yx, idx) {
|
|
2024
|
+
var value = applyKernel(source, kernel, xx, yx);
|
|
2025
|
+
|
|
2026
|
+
this.bitmap.data[idx] = value[0];
|
|
2027
|
+
this.bitmap.data[idx + 1] = value[1];
|
|
2028
|
+
this.bitmap.data[idx + 2] = value[2];
|
|
2029
|
+
});
|
|
2030
|
+
|
|
2031
|
+
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
2032
|
+
else return this;
|
|
2033
|
+
};
|
|
2034
|
+
|
|
2035
|
+
function applyKernel(im, kernel, x, y) {
|
|
2036
|
+
var value = [0, 0, 0];
|
|
2037
|
+
var size = (kernel.length - 1) / 2;
|
|
2038
|
+
|
|
2039
|
+
for (var kx = 0; kx < kernel.length; kx += 1) {
|
|
2040
|
+
for (var ky = 0; ky < kernel[kx].length; ky += 1) {
|
|
2041
|
+
var idx = im.getPixelIndex(x + kx - size, y + ky - size);
|
|
2042
|
+
value[0] += im.bitmap.data[idx] * kernel[kx][ky];
|
|
2043
|
+
value[1] += im.bitmap.data[idx + 1] * kernel[kx][ky];
|
|
2044
|
+
value[2] += im.bitmap.data[idx + 2] * kernel[kx][ky];
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
return value;
|
|
2048
|
+
}
|
|
2049
|
+
|
|
1846
2050
|
/**
|
|
1847
2051
|
* Rotates an image clockwise by a number of degrees rounded to the nearest 90 degrees. NB: 'this' must be a Jimp object.
|
|
1848
2052
|
* @param deg the number of degress to rotate the image by
|
|
@@ -1866,7 +2070,7 @@ function simpleRotate(deg) {
|
|
|
1866
2070
|
}
|
|
1867
2071
|
|
|
1868
2072
|
this.bitmap.data = new Buffer(dstBuffer);
|
|
1869
|
-
|
|
2073
|
+
|
|
1870
2074
|
var tmp = this.bitmap.width;
|
|
1871
2075
|
this.bitmap.width = this.bitmap.height;
|
|
1872
2076
|
this.bitmap.height = tmp;
|
|
@@ -1885,7 +2089,7 @@ function advancedRotate(deg, mode) {
|
|
|
1885
2089
|
var rad = (deg % 360) * Math.PI / 180;
|
|
1886
2090
|
var cosine = Math.cos(rad);
|
|
1887
2091
|
var sine = Math.sin(rad);
|
|
1888
|
-
|
|
2092
|
+
|
|
1889
2093
|
var w, h; // the final width and height if resize == true
|
|
1890
2094
|
|
|
1891
2095
|
if (mode == true || "string" == typeof mode) {
|
|
@@ -1899,15 +2103,15 @@ function advancedRotate(deg, mode) {
|
|
|
1899
2103
|
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
1900
2104
|
this.bitmap.data.writeUInt32BE(this._background, idx);
|
|
1901
2105
|
});
|
|
1902
|
-
|
|
2106
|
+
|
|
1903
2107
|
var max= Math.max(w,h,this.bitmap.width,this.bitmap.height)
|
|
1904
2108
|
this.resize(max, max, mode);
|
|
1905
|
-
|
|
2109
|
+
|
|
1906
2110
|
this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);
|
|
1907
2111
|
}
|
|
1908
2112
|
|
|
1909
2113
|
var dstBuffer = new Buffer(this.bitmap.data.length);
|
|
1910
|
-
|
|
2114
|
+
|
|
1911
2115
|
function createTranslationFunction(deltaX, deltaY) {
|
|
1912
2116
|
return function(x, y) {
|
|
1913
2117
|
return {
|
|
@@ -1919,7 +2123,7 @@ function advancedRotate(deg, mode) {
|
|
|
1919
2123
|
|
|
1920
2124
|
var translate2Cartesian = createTranslationFunction(-(this.bitmap.width / 2), -(this.bitmap.height / 2));
|
|
1921
2125
|
var translate2Screen = createTranslationFunction(this.bitmap.width / 2, this.bitmap.height / 2);
|
|
1922
|
-
|
|
2126
|
+
|
|
1923
2127
|
for (var y = 0; y < this.bitmap.height; y++) {
|
|
1924
2128
|
for (var x = 0; x < this.bitmap.width; x++) {
|
|
1925
2129
|
var cartesian = translate2Cartesian(x, this.bitmap.height - y);
|
|
@@ -1929,7 +2133,7 @@ function advancedRotate(deg, mode) {
|
|
|
1929
2133
|
);
|
|
1930
2134
|
if (source.x >= 0 && source.x < this.bitmap.width
|
|
1931
2135
|
&& source.y >= 0 && source.y < this.bitmap.height) {
|
|
1932
|
-
var srcIdx = (this.bitmap.width * (this.bitmap.height - source.y | 0) + source.x | 0) << 2;
|
|
2136
|
+
var srcIdx = (this.bitmap.width * (this.bitmap.height - source.y - 1 | 0) + source.x | 0) << 2;
|
|
1933
2137
|
var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx, true);
|
|
1934
2138
|
var dstIdx = (this.bitmap.width * y + x) << 2;
|
|
1935
2139
|
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
|
|
@@ -1941,7 +2145,7 @@ function advancedRotate(deg, mode) {
|
|
|
1941
2145
|
}
|
|
1942
2146
|
}
|
|
1943
2147
|
this.bitmap.data = dstBuffer;
|
|
1944
|
-
|
|
2148
|
+
|
|
1945
2149
|
if (mode == true || "string" == typeof mode) {
|
|
1946
2150
|
// now crop the image to the final size
|
|
1947
2151
|
var x = (this.bitmap.width / 2) - (w/2);
|
|
@@ -1971,16 +2175,45 @@ Jimp.prototype.rotate = function (deg, mode, cb) {
|
|
|
1971
2175
|
cb = mode;
|
|
1972
2176
|
mode = true;
|
|
1973
2177
|
}
|
|
1974
|
-
|
|
2178
|
+
|
|
1975
2179
|
if ("number" != typeof deg)
|
|
1976
2180
|
return throwError.call(this, "deg must be a number", cb);
|
|
1977
|
-
|
|
2181
|
+
|
|
1978
2182
|
if ("boolean" != typeof mode && "string" != typeof mode)
|
|
1979
2183
|
return throwError.call(this, "mode must be a boolean or a string", cb);
|
|
1980
2184
|
|
|
1981
2185
|
if (deg % 90 == 0 && mode !== false) simpleRotate.call(this, deg, cb);
|
|
1982
2186
|
else advancedRotate.call(this, deg, mode, cb);
|
|
1983
|
-
|
|
2187
|
+
|
|
2188
|
+
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
2189
|
+
else return this;
|
|
2190
|
+
};
|
|
2191
|
+
|
|
2192
|
+
/**
|
|
2193
|
+
* Displaces the image based on the provided displacement map
|
|
2194
|
+
* @param map the source Jimp instance
|
|
2195
|
+
* @param offset the maximum displacement value
|
|
2196
|
+
* @param (optional) cb a callback for when complete
|
|
2197
|
+
* @returns this for chaining of methods
|
|
2198
|
+
*/
|
|
2199
|
+
Jimp.prototype.displace = function (map, offset, cb) {
|
|
2200
|
+
if ("object" != typeof map || map.constructor != Jimp)
|
|
2201
|
+
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
2202
|
+
if ("number" != typeof offset)
|
|
2203
|
+
return throwError.call(this, "factor must be a number", cb);
|
|
2204
|
+
|
|
2205
|
+
var source = this.clone();
|
|
2206
|
+
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
2207
|
+
|
|
2208
|
+
var displacement = map.bitmap.data[idx] / 256 * offset;
|
|
2209
|
+
displacement = Math.round(displacement);
|
|
2210
|
+
|
|
2211
|
+
var ids = this.getPixelIndex(x + displacement, y);
|
|
2212
|
+
this.bitmap.data[ids] = source.bitmap.data[idx];
|
|
2213
|
+
this.bitmap.data[ids + 1] = source.bitmap.data[idx + 1];
|
|
2214
|
+
this.bitmap.data[ids + 2] = source.bitmap.data[idx + 2];
|
|
2215
|
+
});
|
|
2216
|
+
|
|
1984
2217
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1985
2218
|
else return this;
|
|
1986
2219
|
};
|
|
@@ -1993,9 +2226,9 @@ Jimp.prototype.rotate = function (deg, mode, cb) {
|
|
|
1993
2226
|
*/
|
|
1994
2227
|
Jimp.prototype.getBuffer = function (mime, cb) {
|
|
1995
2228
|
if (mime == Jimp.AUTO) { // allow auto MIME detection
|
|
1996
|
-
mime = this.
|
|
2229
|
+
mime = this.getMIME();
|
|
1997
2230
|
}
|
|
1998
|
-
|
|
2231
|
+
|
|
1999
2232
|
if ("string" != typeof mime)
|
|
2000
2233
|
return throwError.call(this, "mime must be a string", cb);
|
|
2001
2234
|
if ("function" != typeof cb)
|
|
@@ -2014,10 +2247,10 @@ Jimp.prototype.getBuffer = function (mime, cb) {
|
|
|
2014
2247
|
colorType: (this._rgba) ? 6 : 2,
|
|
2015
2248
|
inputHasAlpha: true
|
|
2016
2249
|
});
|
|
2017
|
-
|
|
2250
|
+
|
|
2018
2251
|
if (this._rgba) png.data = new Buffer(this.bitmap.data);
|
|
2019
2252
|
else png.data = compositeBitmapOverBackground(this).data; // when PNG doesn't support alpha
|
|
2020
|
-
|
|
2253
|
+
|
|
2021
2254
|
StreamToBuffer(png.pack(), function (err, buffer) {
|
|
2022
2255
|
return cb.call(that, null, buffer);
|
|
2023
2256
|
});
|
|
@@ -2029,6 +2262,8 @@ Jimp.prototype.getBuffer = function (mime, cb) {
|
|
|
2029
2262
|
return cb.call(this, null, jpeg.data);
|
|
2030
2263
|
|
|
2031
2264
|
case Jimp.MIME_BMP:
|
|
2265
|
+
case Jimp.MIME_X_MS_BMP:
|
|
2266
|
+
|
|
2032
2267
|
// composite onto a new image so that the background shows through alpha channels
|
|
2033
2268
|
var bmp = BMP.encode(compositeBitmapOverBackground(this));
|
|
2034
2269
|
return cb.call(this, null, bmp.data);
|
|
@@ -2052,9 +2287,9 @@ function compositeBitmapOverBackground(image){
|
|
|
2052
2287
|
*/
|
|
2053
2288
|
Jimp.prototype.getBase64 = function (mime, cb) {
|
|
2054
2289
|
if (mime == Jimp.AUTO) { // allow auto MIME detection
|
|
2055
|
-
mime = this.
|
|
2290
|
+
mime = this.getMIME();
|
|
2056
2291
|
}
|
|
2057
|
-
|
|
2292
|
+
|
|
2058
2293
|
if ("string" != typeof mime)
|
|
2059
2294
|
return throwError.call(this, "mime must be a string", cb);
|
|
2060
2295
|
if ("function" != typeof cb)
|
|
@@ -2064,7 +2299,7 @@ Jimp.prototype.getBase64 = function (mime, cb) {
|
|
|
2064
2299
|
var src = "data:" + mime + ";base64," + data.toString("base64");
|
|
2065
2300
|
return cb.call(this, null, src);
|
|
2066
2301
|
});
|
|
2067
|
-
|
|
2302
|
+
|
|
2068
2303
|
return this;
|
|
2069
2304
|
};
|
|
2070
2305
|
|
|
@@ -2168,7 +2403,7 @@ Jimp.loadFont = function (file, cb) {
|
|
|
2168
2403
|
|
|
2169
2404
|
var that = this;
|
|
2170
2405
|
|
|
2171
|
-
return new Promise(function (resolve, reject) {
|
|
2406
|
+
return new Promise(function (resolve, reject) {
|
|
2172
2407
|
cb = cb || function(err, font) {
|
|
2173
2408
|
if (err) reject(err);
|
|
2174
2409
|
else resolve(font);
|
|
@@ -2228,7 +2463,7 @@ Jimp.prototype.print = function (font, x, y, text, maxWidth, cb) {
|
|
|
2228
2463
|
if ("undefined" == typeof maxWidth) {
|
|
2229
2464
|
maxWidth = Infinity;
|
|
2230
2465
|
}
|
|
2231
|
-
|
|
2466
|
+
|
|
2232
2467
|
if ("object" != typeof font)
|
|
2233
2468
|
return throwError.call(this, "font must be a Jimp loadFont", cb);
|
|
2234
2469
|
if ("number" != typeof x || "number" != typeof y || "number" != typeof maxWidth)
|
|
@@ -2239,7 +2474,7 @@ Jimp.prototype.print = function (font, x, y, text, maxWidth, cb) {
|
|
|
2239
2474
|
return throwError.call(this, "maxWidth must be a number", cb);
|
|
2240
2475
|
|
|
2241
2476
|
var that = this;
|
|
2242
|
-
|
|
2477
|
+
|
|
2243
2478
|
var words = text.split(' ');
|
|
2244
2479
|
var line = '';
|
|
2245
2480
|
|
|
@@ -2305,6 +2540,9 @@ Jimp.prototype.write = function (path, cb) {
|
|
|
2305
2540
|
var that = this;
|
|
2306
2541
|
var mime = MIME.lookup(path);
|
|
2307
2542
|
|
|
2543
|
+
var pathObj = Path.parse(path);
|
|
2544
|
+
if (pathObj.dir) MkDirP.sync(pathObj.dir);
|
|
2545
|
+
|
|
2308
2546
|
this.getBuffer(mime, function(err, buffer) {
|
|
2309
2547
|
if (err) return throwError.call(that, err, cb);
|
|
2310
2548
|
var stream = FS.createWriteStream(path);
|
|
@@ -2322,6 +2560,21 @@ Jimp.prototype.write = function (path, cb) {
|
|
|
2322
2560
|
return this;
|
|
2323
2561
|
};
|
|
2324
2562
|
|
|
2563
|
+
|
|
2564
|
+
/* Nicely format Jimp object when sent to the console e.g. console.log(imgage) */
|
|
2565
|
+
Jimp.prototype.inspect = function () {
|
|
2566
|
+
return '<Jimp ' +
|
|
2567
|
+
(this.bitmap === Jimp.prototype.bitmap ? 'pending...' :
|
|
2568
|
+
this.bitmap.width + 'x' + this.bitmap.height) +
|
|
2569
|
+
'>';
|
|
2570
|
+
};
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
// Nicely format Jimp object when converted to a string
|
|
2574
|
+
Jimp.prototype.toString = function () {
|
|
2575
|
+
return '[object Jimp]';
|
|
2576
|
+
};
|
|
2577
|
+
|
|
2325
2578
|
if (process.env.ENVIRONMENT === "BROWSER") {
|
|
2326
2579
|
// For use in a web browser or web worker
|
|
2327
2580
|
var gl;
|