jimp 0.2.24 → 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 +98 -18
- package/browser/.editorconfig +2 -0
- package/browser/README.md +45 -203
- package/browser/browserify-build.sh +6 -3
- package/browser/examples/dice.png +0 -0
- package/browser/examples/example1.html +24 -0
- package/browser/examples/example2.html +20 -0
- package/browser/examples/example3.html +35 -0
- package/browser/examples/jimp-worker.js +13 -0
- package/browser/examples/lenna.png +0 -0
- package/browser/examples/test.html +43 -0
- package/browser/lib/jimp.js +47 -3853
- package/browser/lib/jimp.min.js +2 -12
- package/browser/src/jimp-wrapper.js +45 -43
- package/index.js +583 -149
- package/jimp.d.ts +133 -0
- package/omggif.js +782 -0
- package/package.json +9 -6
- package/resize.js +120 -185
- package/resize2.js +15 -8
- package/browser/img/Landscape_1.jpg +0 -0
- package/browser/img/Landscape_2.jpg +0 -0
- package/browser/img/Landscape_3.jpg +0 -0
- package/browser/img/Landscape_4.jpg +0 -0
- package/browser/img/Portrait_1.jpg +0 -0
- package/browser/img/Portrait_2.jpg +0 -0
- package/browser/img/Portrait_3.jpg +0 -0
- package/browser/img/Portrait_4.jpg +0 -0
- package/browser/index.html +0 -151
- package/browser/jimp-worker.js +0 -71
package/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
if (process.env.ENVIRONMENT !==
|
|
1
|
+
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,8 +17,35 @@ 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");
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
var MkDirP = require("mkdirp");
|
|
21
|
+
|
|
22
|
+
if (process.env.ENVIRONMENT !== "BROWSER") {
|
|
23
|
+
//If we run into electron renderer process, use XHR method instead of Request node module
|
|
24
|
+
if (process.versions.hasOwnProperty("electron") && process.type === "renderer" && typeof XMLHttpRequest === "function") {
|
|
25
|
+
var Request = function (url,cb) {
|
|
26
|
+
var xhr = new XMLHttpRequest();
|
|
27
|
+
xhr.open( "GET", url, true );
|
|
28
|
+
xhr.responseType = "arraybuffer";
|
|
29
|
+
xhr.onload = function() {
|
|
30
|
+
if (xhr.status < 400) {
|
|
31
|
+
try {
|
|
32
|
+
var data = Buffer.from(this.response);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
return cb("Response is not a buffer for url "+url)
|
|
35
|
+
}
|
|
36
|
+
cb(null, xhr, data);
|
|
37
|
+
}
|
|
38
|
+
else cb("HTTP Status " + xhr.status + " for url "+url);
|
|
39
|
+
};
|
|
40
|
+
xhr.onerror = function(e) {
|
|
41
|
+
cb(e);
|
|
42
|
+
};
|
|
43
|
+
xhr.send();
|
|
44
|
+
};
|
|
45
|
+
} else {
|
|
46
|
+
var Request = require('request').defaults({ encoding: null });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
21
49
|
|
|
22
50
|
// polyfill Promise for Node < 0.12
|
|
23
51
|
var Promise = global.Promise || require('es6-promise').Promise;
|
|
@@ -53,7 +81,7 @@ function isNodePattern(cb) {
|
|
|
53
81
|
}
|
|
54
82
|
|
|
55
83
|
function throwError(error, cb) {
|
|
56
|
-
if ("string" == typeof error) error =
|
|
84
|
+
if ("string" == typeof error) error = console.error(error);
|
|
57
85
|
if ("function" == typeof cb) return cb.call(this, error);
|
|
58
86
|
else throw error;
|
|
59
87
|
}
|
|
@@ -89,7 +117,7 @@ function Jimp() {
|
|
|
89
117
|
var w = arguments[0];
|
|
90
118
|
var h = arguments[1];
|
|
91
119
|
var cb = arguments[2];
|
|
92
|
-
|
|
120
|
+
|
|
93
121
|
if ("number" == typeof arguments[2]) {
|
|
94
122
|
this._background = arguments[2];
|
|
95
123
|
var cb = arguments[3];
|
|
@@ -110,7 +138,7 @@ function Jimp() {
|
|
|
110
138
|
}
|
|
111
139
|
|
|
112
140
|
cb.call(this, null, this);
|
|
113
|
-
} else if (
|
|
141
|
+
} else if (arguments[0] instanceof Jimp) {
|
|
114
142
|
// clone an existing Jimp
|
|
115
143
|
var original = arguments[0];
|
|
116
144
|
var cb = arguments[1];
|
|
@@ -133,6 +161,7 @@ function Jimp() {
|
|
|
133
161
|
|
|
134
162
|
this._quality = original._quality;
|
|
135
163
|
this._deflateLevel = original._deflateLevel;
|
|
164
|
+
this._deflateStrategy = original._deflateStrategy;
|
|
136
165
|
this._filterType = original._filterType;
|
|
137
166
|
this._rgba = original._rgba;
|
|
138
167
|
this._background = original._background;
|
|
@@ -142,11 +171,11 @@ function Jimp() {
|
|
|
142
171
|
// read from a URL
|
|
143
172
|
var url = arguments[0];
|
|
144
173
|
var cb = arguments[1];
|
|
145
|
-
|
|
174
|
+
|
|
146
175
|
if ("undefined" == typeof cb) cb = noop;
|
|
147
176
|
if ("function" != typeof cb)
|
|
148
177
|
return throwError.call(this, "cb must be a function", cb);
|
|
149
|
-
|
|
178
|
+
|
|
150
179
|
var that = this;
|
|
151
180
|
Request(url, function (err, response, data) {
|
|
152
181
|
if (err) return throwError.call(that, err, cb);
|
|
@@ -161,7 +190,7 @@ function Jimp() {
|
|
|
161
190
|
// read from a path
|
|
162
191
|
var path = arguments[0];
|
|
163
192
|
var cb = arguments[1];
|
|
164
|
-
|
|
193
|
+
|
|
165
194
|
if ("undefined" == typeof cb) cb = noop;
|
|
166
195
|
if ("function" != typeof cb)
|
|
167
196
|
return throwError.call(this, "cb must be a function", cb);
|
|
@@ -215,28 +244,54 @@ Jimp.read = function(src, cb) {
|
|
|
215
244
|
|
|
216
245
|
// MIME type methods
|
|
217
246
|
|
|
218
|
-
function getMIMEFromBuffer(buffer) {
|
|
219
|
-
|
|
220
|
-
|
|
247
|
+
function getMIMEFromBuffer(buffer, path) {
|
|
248
|
+
var fileTypeFromBuffer = FileType(buffer);
|
|
249
|
+
if (fileTypeFromBuffer) {
|
|
250
|
+
// If FileType returns something for buffer, then return the mime given
|
|
251
|
+
return fileTypeFromBuffer.mime;
|
|
252
|
+
}
|
|
253
|
+
else if (path) {
|
|
254
|
+
// If a path is supplied, and FileType yields no results, then retry with MIME
|
|
255
|
+
// Path can be either a file path or a url
|
|
256
|
+
return MIME.lookup(path)
|
|
257
|
+
} else {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
221
260
|
}
|
|
222
261
|
|
|
223
262
|
// gets a MIME type of a file from the path to it
|
|
224
263
|
function getMIMEFromPath(path, cb) {
|
|
225
264
|
ReadChunk(path, 0, 262, function (err, buffer) {
|
|
226
|
-
if (err) {
|
|
227
|
-
|
|
228
|
-
|
|
265
|
+
if (err) {
|
|
266
|
+
cb(null, "");
|
|
267
|
+
} else {
|
|
268
|
+
var fileType = FileType(buffer);
|
|
269
|
+
return cb && cb(null, fileType && fileType.mime || "");
|
|
270
|
+
}
|
|
229
271
|
});
|
|
230
272
|
}
|
|
231
273
|
|
|
232
274
|
//=> {ext: 'png', mime: 'image/png'}
|
|
233
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
|
+
|
|
234
289
|
// parses a bitmap from the constructor to the JIMP bitmap property
|
|
235
290
|
function parseBitmap(data, mime, cb) {
|
|
236
291
|
var that = this;
|
|
237
|
-
this._originalMime = mime;
|
|
292
|
+
this._originalMime = mime.toLowerCase();
|
|
238
293
|
|
|
239
|
-
switch (
|
|
294
|
+
switch (this.getMIME()) {
|
|
240
295
|
case Jimp.MIME_PNG:
|
|
241
296
|
var png = new PNG();
|
|
242
297
|
png.parse(data, function(err, data) {
|
|
@@ -253,14 +308,20 @@ function parseBitmap(data, mime, cb) {
|
|
|
253
308
|
case Jimp.MIME_JPEG:
|
|
254
309
|
try {
|
|
255
310
|
this.bitmap = JPEG.decode(data);
|
|
256
|
-
|
|
311
|
+
try { this._exif = EXIFParser.create(data).parse(); }
|
|
312
|
+
catch (err) { /* meh */ }
|
|
257
313
|
return cb.call(this, null, this);
|
|
258
314
|
} catch(err) {
|
|
259
315
|
return cb.call(this, err, this);
|
|
260
316
|
}
|
|
261
317
|
|
|
262
318
|
case Jimp.MIME_BMP:
|
|
263
|
-
|
|
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);
|
|
264
325
|
return cb.call(this, null, this);
|
|
265
326
|
|
|
266
327
|
default:
|
|
@@ -268,43 +329,6 @@ function parseBitmap(data, mime, cb) {
|
|
|
268
329
|
}
|
|
269
330
|
}
|
|
270
331
|
|
|
271
|
-
/*
|
|
272
|
-
* Automagically rotates a JPEG image based on its EXIF data
|
|
273
|
-
* @params image a Jimp image
|
|
274
|
-
* @params buffer a buffer array of the raw JPEG data
|
|
275
|
-
* @returns nothing
|
|
276
|
-
*/
|
|
277
|
-
function exifRotate(image, buffer) {
|
|
278
|
-
var exif = EXIFParser.create(buffer).parse();
|
|
279
|
-
if (!exif || !exif.tags || !exif.tags.Orientation) return;
|
|
280
|
-
switch (exif.tags.Orientation) {
|
|
281
|
-
case 1: // Horizontal (normal)
|
|
282
|
-
// do nothing
|
|
283
|
-
break;
|
|
284
|
-
case 2: // Mirror horizontal
|
|
285
|
-
image.mirror(true, false);
|
|
286
|
-
break;
|
|
287
|
-
case 3: // Rotate 180
|
|
288
|
-
image.rotate(180);
|
|
289
|
-
break;
|
|
290
|
-
case 4: // Mirror vertical
|
|
291
|
-
image.mirror(false, true);
|
|
292
|
-
break;
|
|
293
|
-
case 5: // Mirror horizontal and rotate 270 CW
|
|
294
|
-
image.mirror(true, false).rotate(270);
|
|
295
|
-
break;
|
|
296
|
-
case 6: // Rotate 90 CW
|
|
297
|
-
image.rotate(90);
|
|
298
|
-
break;
|
|
299
|
-
case 7: // Mirror horizontal and rotate 90 CW
|
|
300
|
-
image.mirror(true, false).rotate(90);
|
|
301
|
-
break;
|
|
302
|
-
case 8: // Rotate 270 CW
|
|
303
|
-
image.rotate(270);
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
332
|
// used to auto resizing etc.
|
|
309
333
|
Jimp.AUTO = -1;
|
|
310
334
|
|
|
@@ -312,6 +336,8 @@ Jimp.AUTO = -1;
|
|
|
312
336
|
Jimp.MIME_PNG = "image/png";
|
|
313
337
|
Jimp.MIME_JPEG = "image/jpeg";
|
|
314
338
|
Jimp.MIME_BMP = "image/bmp";
|
|
339
|
+
Jimp.MIME_X_MS_BMP = "image/x-ms-bmp";
|
|
340
|
+
Jimp.MIME_GIF = "image/gif";
|
|
315
341
|
|
|
316
342
|
// PNG filter types
|
|
317
343
|
Jimp.PNG_FILTER_AUTO = -1;
|
|
@@ -327,6 +353,15 @@ Jimp.RESIZE_BICUBIC = 'bicubicInterpolation';
|
|
|
327
353
|
Jimp.RESIZE_HERMITE = 'hermiteInterpolation';
|
|
328
354
|
Jimp.RESIZE_BEZIER = 'bezierInterpolation';
|
|
329
355
|
|
|
356
|
+
// Align modes for cover, contain, bit masks
|
|
357
|
+
Jimp.HORIZONTAL_ALIGN_LEFT = 1;
|
|
358
|
+
Jimp.HORIZONTAL_ALIGN_CENTER = 2;
|
|
359
|
+
Jimp.HORIZONTAL_ALIGN_RIGHT = 4;
|
|
360
|
+
|
|
361
|
+
Jimp.VERTICAL_ALIGN_TOP = 8;
|
|
362
|
+
Jimp.VERTICAL_ALIGN_MIDDLE = 16;
|
|
363
|
+
Jimp.VERTICAL_ALIGN_BOTTOM = 32;
|
|
364
|
+
|
|
330
365
|
// Font locations
|
|
331
366
|
Jimp.FONT_SANS_8_BLACK = Path.join(__dirname, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt");
|
|
332
367
|
Jimp.FONT_SANS_16_BLACK = Path.join(__dirname, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt");
|
|
@@ -340,6 +375,11 @@ Jimp.FONT_SANS_32_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-32-whi
|
|
|
340
375
|
Jimp.FONT_SANS_64_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt");
|
|
341
376
|
Jimp.FONT_SANS_128_WHITE = Path.join(__dirname, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt");
|
|
342
377
|
|
|
378
|
+
// Edge Handling
|
|
379
|
+
Jimp.EDGE_EXTEND = 1;
|
|
380
|
+
Jimp.EDGE_WRAP = 2;
|
|
381
|
+
Jimp.EDGE_CROP = 3;
|
|
382
|
+
|
|
343
383
|
/**
|
|
344
384
|
* A static helper method that converts RGBA values to a single integer value
|
|
345
385
|
* @param r the red value (0-255)
|
|
@@ -360,9 +400,14 @@ Jimp.rgbaToInt = function(r, g, b, a, cb){
|
|
|
360
400
|
return throwError.call(this, "b must be between 0 and 255", cb);
|
|
361
401
|
if (a < 0 || a > 255)
|
|
362
402
|
return throwError.call(this, "a must be between 0 and 255", cb);
|
|
363
|
-
|
|
403
|
+
|
|
404
|
+
r = Math.round(r);
|
|
405
|
+
b = Math.round(b);
|
|
406
|
+
g = Math.round(g);
|
|
407
|
+
a = Math.round(a);
|
|
408
|
+
|
|
364
409
|
var i = (r * Math.pow(256, 3)) + (g * Math.pow(256, 2)) + (b * Math.pow(256, 1)) + (a * Math.pow(256, 0));
|
|
365
|
-
|
|
410
|
+
|
|
366
411
|
if (isNodePattern(cb)) return cb.call(this, null, i);
|
|
367
412
|
else return i;
|
|
368
413
|
}
|
|
@@ -376,13 +421,13 @@ Jimp.rgbaToInt = function(r, g, b, a, cb){
|
|
|
376
421
|
Jimp.intToRGBA = function(i, cb){
|
|
377
422
|
if ("number" != typeof i)
|
|
378
423
|
return throwError.call(this, "i must be a number", cb);
|
|
379
|
-
|
|
424
|
+
|
|
380
425
|
var rgba = {}
|
|
381
426
|
rgba.r = Math.floor(i / Math.pow(256, 3));
|
|
382
427
|
rgba.g = Math.floor((i - (rgba.r * Math.pow(256, 3))) / Math.pow(256, 2));
|
|
383
428
|
rgba.b = Math.floor((i - (rgba.r * Math.pow(256, 3)) - (rgba.g * Math.pow(256, 2))) / Math.pow(256, 1));
|
|
384
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));
|
|
385
|
-
|
|
430
|
+
|
|
386
431
|
if (isNodePattern(cb)) return cb.call(this, null, rgba);
|
|
387
432
|
else return rgba;
|
|
388
433
|
}
|
|
@@ -408,7 +453,7 @@ Jimp.limit255 = function(n) {
|
|
|
408
453
|
* @returns an object { percent: percent similar, diff: a Jimp image highlighting differences }
|
|
409
454
|
*/
|
|
410
455
|
Jimp.diff = function (img1, img2, threshold) {
|
|
411
|
-
if (
|
|
456
|
+
if (!(img1 instanceof Jimp) || !(img2 instanceof Jimp))
|
|
412
457
|
return throwError.call(this, "img1 and img2 must be an Jimp images");
|
|
413
458
|
|
|
414
459
|
if (img1.bitmap.width != img2.bitmap.width || img1.bitmap.height != img2.bitmap.height) {
|
|
@@ -422,7 +467,7 @@ Jimp.diff = function (img1, img2, threshold) {
|
|
|
422
467
|
break;
|
|
423
468
|
}
|
|
424
469
|
}
|
|
425
|
-
|
|
470
|
+
|
|
426
471
|
threshold = threshold || 0.1;
|
|
427
472
|
if ("number" != typeof threshold || threshold < 0 || threshold > 1)
|
|
428
473
|
return throwError.call(this, "threshold must be a number between 0 and 1");
|
|
@@ -437,7 +482,7 @@ Jimp.diff = function (img1, img2, threshold) {
|
|
|
437
482
|
diff.bitmap.height,
|
|
438
483
|
{threshold: threshold}
|
|
439
484
|
);
|
|
440
|
-
|
|
485
|
+
|
|
441
486
|
return {
|
|
442
487
|
percent: numDiffPixels / (diff.bitmap.width * diff.bitmap.height),
|
|
443
488
|
image: diff
|
|
@@ -472,6 +517,7 @@ Jimp.prototype.bitmap = {
|
|
|
472
517
|
// The quality to be used when saving JPEG images
|
|
473
518
|
Jimp.prototype._quality = 100;
|
|
474
519
|
Jimp.prototype._deflateLevel = 9;
|
|
520
|
+
Jimp.prototype._deflateStrategy = 3;
|
|
475
521
|
Jimp.prototype._filterType = Jimp.PNG_FILTER_AUTO;
|
|
476
522
|
|
|
477
523
|
// Whether PNGs will be exported as RGB or RGBA
|
|
@@ -480,6 +526,12 @@ Jimp.prototype._rgba = true;
|
|
|
480
526
|
// Default colour to use for new pixels
|
|
481
527
|
Jimp.prototype._background = 0x00000000;
|
|
482
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
|
+
|
|
483
535
|
/**
|
|
484
536
|
* Creates a new image that is a clone of this one.
|
|
485
537
|
* @param cb (optional) A callback for when complete
|
|
@@ -528,6 +580,24 @@ Jimp.prototype.deflateLevel = function (l, cb) {
|
|
|
528
580
|
else return this;
|
|
529
581
|
};
|
|
530
582
|
|
|
583
|
+
/**
|
|
584
|
+
* Sets the deflate strategy used when saving as PNG format (default is 3)
|
|
585
|
+
* @param s Deflate strategy to use 0-3.
|
|
586
|
+
* @param (optional) cb a callback for when complete
|
|
587
|
+
* @returns this for chaining of methods
|
|
588
|
+
*/
|
|
589
|
+
Jimp.prototype.deflateStrategy = function (s, cb) {
|
|
590
|
+
if ("number" != typeof s)
|
|
591
|
+
return throwError.call(this, "s must be a number", cb);
|
|
592
|
+
if (s < 0 || s > 3)
|
|
593
|
+
return throwError.call(this, "s must be a number 0 - 3", cb);
|
|
594
|
+
|
|
595
|
+
this._deflateStrategy = Math.round(s);
|
|
596
|
+
|
|
597
|
+
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
598
|
+
else return this;
|
|
599
|
+
};
|
|
600
|
+
|
|
531
601
|
/**
|
|
532
602
|
* Sets the filter type used when saving as PNG format (default is automatic filters)
|
|
533
603
|
* @param f The quality to use -1-4.
|
|
@@ -614,26 +684,65 @@ Jimp.prototype.scan = function (x, y, w, h, f, cb) {
|
|
|
614
684
|
else return this;
|
|
615
685
|
};
|
|
616
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
|
+
|
|
617
705
|
/**
|
|
618
706
|
* Returns the offset of a pixel in the bitmap buffer
|
|
619
707
|
* @param x the x coordinate
|
|
620
708
|
* @param y the y coordinate
|
|
709
|
+
* @param (optional) edgeHandling define how to sum pixels from outside the border
|
|
621
710
|
* @param (optional) cb a callback for when complete
|
|
622
711
|
* @returns the index of the pixel or -1 if not found
|
|
623
712
|
*/
|
|
624
|
-
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;
|
|
625
720
|
if ("number" != typeof x || "number" != typeof y)
|
|
626
721
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
627
722
|
|
|
628
723
|
// round input
|
|
629
|
-
x = Math.round(x);
|
|
630
|
-
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
|
+
}
|
|
631
740
|
|
|
632
|
-
var i = (this.bitmap.width *
|
|
741
|
+
var i = (this.bitmap.width * yi + xi) << 2;
|
|
633
742
|
|
|
634
743
|
// if out of bounds index is -1
|
|
635
|
-
if (
|
|
636
|
-
if (
|
|
744
|
+
if (xi < 0 || xi >= this.bitmap.width) i = -1;
|
|
745
|
+
if (yi < 0 || yi >= this.bitmap.height) i = -1;
|
|
637
746
|
|
|
638
747
|
if (isNodePattern(cb)) return cb.call(this, null, i);
|
|
639
748
|
else return i;
|
|
@@ -653,10 +762,10 @@ Jimp.prototype.getPixelColor = Jimp.prototype.getPixelColour = function (x, y, c
|
|
|
653
762
|
// round input
|
|
654
763
|
x = Math.round(x);
|
|
655
764
|
y = Math.round(y);
|
|
656
|
-
|
|
765
|
+
|
|
657
766
|
var idx = this.getPixelIndex(x, y);
|
|
658
767
|
var hex = this.bitmap.data.readUInt32BE(idx);
|
|
659
|
-
|
|
768
|
+
|
|
660
769
|
if (isNodePattern(cb)) return cb.call(this, null, hex);
|
|
661
770
|
else return hex;
|
|
662
771
|
};
|
|
@@ -675,10 +784,10 @@ Jimp.prototype.setPixelColor = Jimp.prototype.setPixelColour = function (hex, x,
|
|
|
675
784
|
// round input
|
|
676
785
|
x = Math.round(x);
|
|
677
786
|
y = Math.round(y);
|
|
678
|
-
|
|
787
|
+
|
|
679
788
|
var idx = this.getPixelIndex(x, y);
|
|
680
789
|
this.bitmap.data.writeUInt32BE(hex, idx, true);
|
|
681
|
-
|
|
790
|
+
|
|
682
791
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
683
792
|
else return this;
|
|
684
793
|
};
|
|
@@ -707,19 +816,59 @@ Jimp.prototype.hash = function(base, cb){
|
|
|
707
816
|
return throwError.call(this, "base must be a number", cb);
|
|
708
817
|
if (base < 2 || base > 64)
|
|
709
818
|
return throwError.call(this, "base must be a number between 2 and 64", cb);
|
|
710
|
-
|
|
819
|
+
|
|
711
820
|
var hash = (new ImagePHash()).getHash(this);
|
|
712
821
|
hash = (new BigNumber(hash, 2)).toString(base);
|
|
713
|
-
|
|
822
|
+
|
|
714
823
|
while (hash.length < maxHashLength[base]) {
|
|
715
824
|
hash = "0" + hash; // pad out with leading zeros
|
|
716
825
|
}
|
|
717
|
-
|
|
826
|
+
|
|
718
827
|
if (isNodePattern(cb)) return cb.call(this, null, hash);
|
|
719
828
|
else return hash;
|
|
720
829
|
}
|
|
721
830
|
|
|
722
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
|
+
|
|
723
872
|
/**
|
|
724
873
|
* Crops the image at a given point to a give size
|
|
725
874
|
* @param x the x coordinate to crop form
|
|
@@ -828,8 +977,8 @@ Jimp.prototype.autocrop = function() {
|
|
|
828
977
|
}
|
|
829
978
|
|
|
830
979
|
east: // east side (scan columns from east to west)
|
|
831
|
-
for (var x =
|
|
832
|
-
for (var y =
|
|
980
|
+
for (var x = w - 1; x >= 0; x--) {
|
|
981
|
+
for (var y = northPixelsToCrop; y < h; y++) {
|
|
833
982
|
var colorXY = this.getPixelColor(x, y);
|
|
834
983
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
835
984
|
var difference =
|
|
@@ -851,7 +1000,7 @@ Jimp.prototype.autocrop = function() {
|
|
|
851
1000
|
colorTarget = this.getPixelColor(w - 1, h - 1); // bottom right pixel color is the target color
|
|
852
1001
|
// for south and west sides
|
|
853
1002
|
south: // south side (scan rows from south to north)
|
|
854
|
-
for (var y = h - 1; y >=
|
|
1003
|
+
for (var y = h - 1; y >= northPixelsToCrop + minPixelsPerSide; y--) {
|
|
855
1004
|
for (var x = w - eastPixelsToCrop - 1; x >= 0; x--) {
|
|
856
1005
|
var colorXY = this.getPixelColor(x, y);
|
|
857
1006
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
@@ -872,8 +1021,8 @@ Jimp.prototype.autocrop = function() {
|
|
|
872
1021
|
}
|
|
873
1022
|
|
|
874
1023
|
west: // west side (scan columns from west to east)
|
|
875
|
-
for (var x =
|
|
876
|
-
for (var y = h -
|
|
1024
|
+
for (var x = 0; x <= w - eastPixelsToCrop - minPixelsPerSide; x++) {
|
|
1025
|
+
for (var y = h - southPixelsToCrop; y >= northPixelsToCrop; y--) {
|
|
877
1026
|
var colorXY = this.getPixelColor(x, y);
|
|
878
1027
|
var rgba2 = Jimp.intToRGBA(colorXY);
|
|
879
1028
|
var difference =
|
|
@@ -918,7 +1067,7 @@ Jimp.prototype.autocrop = function() {
|
|
|
918
1067
|
|
|
919
1068
|
if (doCrop) { // do the real crop
|
|
920
1069
|
this.crop(
|
|
921
|
-
|
|
1070
|
+
westPixelsToCrop,
|
|
922
1071
|
northPixelsToCrop,
|
|
923
1072
|
widthOfPixelsToCrop,
|
|
924
1073
|
heightOfPixelsToCrop
|
|
@@ -942,12 +1091,12 @@ Jimp.prototype.autocrop = function() {
|
|
|
942
1091
|
* @returns this for chaining of methods
|
|
943
1092
|
*/
|
|
944
1093
|
Jimp.prototype.blit = function (src, x, y, srcx, srcy, srcw, srch, cb) {
|
|
945
|
-
if (
|
|
1094
|
+
if (!(src instanceof Jimp))
|
|
946
1095
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
947
1096
|
if ("number" != typeof x || "number" != typeof y)
|
|
948
1097
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
949
1098
|
|
|
950
|
-
if ("function"
|
|
1099
|
+
if (typeof srcx === "function") {
|
|
951
1100
|
cb = srcx;
|
|
952
1101
|
srcx = 0;
|
|
953
1102
|
srcy = 0;
|
|
@@ -995,7 +1144,7 @@ Jimp.prototype.blit = function (src, x, y, srcx, srcy, srcw, srch, cb) {
|
|
|
995
1144
|
* @returns this for chaining of methods
|
|
996
1145
|
*/
|
|
997
1146
|
Jimp.prototype.mask = function (src, x, y, cb) {
|
|
998
|
-
if (
|
|
1147
|
+
if (!(src instanceof Jimp))
|
|
999
1148
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
1000
1149
|
if ("number" != typeof x || "number" != typeof y)
|
|
1001
1150
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
@@ -1024,7 +1173,7 @@ Jimp.prototype.mask = function (src, x, y, cb) {
|
|
|
1024
1173
|
* @returns this for chaining of methods
|
|
1025
1174
|
*/
|
|
1026
1175
|
Jimp.prototype.composite = function (src, x, y, cb) {
|
|
1027
|
-
if (
|
|
1176
|
+
if (!(src instanceof Jimp))
|
|
1028
1177
|
return throwError.call(this, "The source must be a Jimp image", cb);
|
|
1029
1178
|
if ("number" != typeof x || "number" != typeof y)
|
|
1030
1179
|
return throwError.call(this, "x and y must be numbers", cb);
|
|
@@ -1037,27 +1186,27 @@ Jimp.prototype.composite = function (src, x, y, cb) {
|
|
|
1037
1186
|
src.scan(0, 0, src.bitmap.width, src.bitmap.height, function(sx, sy, idx) {
|
|
1038
1187
|
// http://stackoverflow.com/questions/7438263/alpha-compositing-algorithm-blend-modes
|
|
1039
1188
|
var dstIdx = that.getPixelIndex(x+sx, y+sy);
|
|
1040
|
-
|
|
1189
|
+
|
|
1041
1190
|
var fg = {
|
|
1042
1191
|
r: this.bitmap.data[idx + 0] / 255,
|
|
1043
1192
|
g: this.bitmap.data[idx + 1] / 255,
|
|
1044
1193
|
b: this.bitmap.data[idx + 2] / 255,
|
|
1045
1194
|
a: this.bitmap.data[idx + 3] / 255
|
|
1046
1195
|
}
|
|
1047
|
-
|
|
1196
|
+
|
|
1048
1197
|
var bg = {
|
|
1049
1198
|
r: that.bitmap.data[dstIdx + 0] / 255,
|
|
1050
1199
|
g: that.bitmap.data[dstIdx + 1] / 255,
|
|
1051
1200
|
b: that.bitmap.data[dstIdx + 2] / 255,
|
|
1052
1201
|
a: that.bitmap.data[dstIdx + 3] / 255
|
|
1053
1202
|
}
|
|
1054
|
-
|
|
1203
|
+
|
|
1055
1204
|
var a = bg.a + fg.a - bg.a * fg.a;
|
|
1056
|
-
|
|
1205
|
+
|
|
1057
1206
|
var r = ((fg.r * fg.a) + (bg.r * bg.a) * (1 - fg.a)) / a;
|
|
1058
1207
|
var g = ((fg.g * fg.a) + (bg.g * bg.a) * (1 - fg.a)) / a;
|
|
1059
1208
|
var b = ((fg.b * fg.a) + (bg.b * bg.a) * (1 - fg.a)) / a;
|
|
1060
|
-
|
|
1209
|
+
|
|
1061
1210
|
that.bitmap.data[dstIdx + 0] = Jimp.limit255(r * 255);
|
|
1062
1211
|
that.bitmap.data[dstIdx + 1] = Jimp.limit255(g * 255);
|
|
1063
1212
|
that.bitmap.data[dstIdx + 2] = Jimp.limit255(b * 255);
|
|
@@ -1554,7 +1703,7 @@ Jimp.prototype.fade = function (f, cb) {
|
|
|
1554
1703
|
|
|
1555
1704
|
// this method is an alternative to opacity (which may be deprecated)
|
|
1556
1705
|
this.opacity(1 - f);
|
|
1557
|
-
|
|
1706
|
+
|
|
1558
1707
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1559
1708
|
else return this;
|
|
1560
1709
|
};
|
|
@@ -1584,18 +1733,18 @@ Jimp.prototype.opaque = function (cb) {
|
|
|
1584
1733
|
Jimp.prototype.resize = function (w, h, mode, cb) {
|
|
1585
1734
|
if ("number" != typeof w || "number" != typeof h)
|
|
1586
1735
|
return throwError.call(this, "w and h must be numbers", cb);
|
|
1587
|
-
|
|
1736
|
+
|
|
1588
1737
|
if ("function" == typeof mode && "undefined" == typeof cb) {
|
|
1589
1738
|
cb = mode;
|
|
1590
1739
|
mode = null;
|
|
1591
1740
|
}
|
|
1592
|
-
|
|
1741
|
+
|
|
1593
1742
|
if (w == Jimp.AUTO && h == Jimp.AUTO)
|
|
1594
1743
|
return throwError.call(this, "w and h cannot both the set to auto", cb);
|
|
1595
1744
|
|
|
1596
1745
|
if (w == Jimp.AUTO) w = this.bitmap.width * (h / this.bitmap.height);
|
|
1597
1746
|
if (h == Jimp.AUTO) h = this.bitmap.height * (w / this.bitmap.width);
|
|
1598
|
-
|
|
1747
|
+
|
|
1599
1748
|
// round inputs
|
|
1600
1749
|
w = Math.round(w);
|
|
1601
1750
|
h = Math.round(h);
|
|
@@ -1626,23 +1775,40 @@ Jimp.prototype.resize = function (w, h, mode, cb) {
|
|
|
1626
1775
|
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
|
|
1627
1776
|
* @param w the width to resize the image to
|
|
1628
1777
|
* @param h the height to resize the image to
|
|
1778
|
+
* @param (optional) alignBits A bitmask for horizontal and vertical alignment
|
|
1779
|
+
* @param (optional) mode a scaling method (e.g. Jimp.RESIZE_BEZIER)
|
|
1629
1780
|
* @param (optional) cb a callback for when complete
|
|
1630
1781
|
* @returns this for chaining of methods
|
|
1631
1782
|
*/
|
|
1632
|
-
Jimp.prototype.cover = function (w, h, mode, cb) {
|
|
1783
|
+
Jimp.prototype.cover = function (w, h, alignBits, mode, cb) {
|
|
1633
1784
|
if ("number" != typeof w || "number" != typeof h)
|
|
1634
1785
|
return throwError.call(this, "w and h must be numbers", cb);
|
|
1635
1786
|
|
|
1636
|
-
if ("function" == typeof
|
|
1787
|
+
if (alignBits && "function" == typeof alignBits && "undefined" == typeof cb) {
|
|
1788
|
+
cb = alignBits;
|
|
1789
|
+
alignBits = null;
|
|
1790
|
+
mode = null;
|
|
1791
|
+
} else if ("function" == typeof mode && "undefined" == typeof cb) {
|
|
1637
1792
|
cb = mode;
|
|
1638
1793
|
mode = null;
|
|
1639
1794
|
}
|
|
1640
|
-
|
|
1795
|
+
|
|
1796
|
+
alignBits = alignBits || (Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);
|
|
1797
|
+
var hbits = ((alignBits) & ((1<<(3))-1));
|
|
1798
|
+
var vbits = alignBits >> 3;
|
|
1799
|
+
|
|
1800
|
+
// check if more flags than one is in the bit sets
|
|
1801
|
+
if(!(((hbits != 0) && !(hbits & (hbits - 1))) || ((vbits != 0) && !(vbits & (vbits - 1)))))
|
|
1802
|
+
return throwError.call(this, "only use one flag per alignment direction", cb);
|
|
1803
|
+
|
|
1804
|
+
var align_h = (hbits >> 1); // 0, 1, 2
|
|
1805
|
+
var align_v = (vbits >> 1); // 0, 1, 2
|
|
1806
|
+
|
|
1641
1807
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1642
1808
|
w/this.bitmap.width : h/this.bitmap.height;
|
|
1643
1809
|
this.scale(f, mode);
|
|
1644
|
-
this.crop(this.bitmap.width
|
|
1645
|
-
|
|
1810
|
+
this.crop(((this.bitmap.width - w) / 2) * align_h, ((this.bitmap.height - h) / 2) * align_v, w, h);
|
|
1811
|
+
|
|
1646
1812
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1647
1813
|
else return this;
|
|
1648
1814
|
};
|
|
@@ -1651,29 +1817,53 @@ Jimp.prototype.cover = function (w, h, mode, cb) {
|
|
|
1651
1817
|
* Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed.
|
|
1652
1818
|
* @param w the width to resize the image to
|
|
1653
1819
|
* @param h the height to resize the image to
|
|
1820
|
+
* @param (optional) alignBits A bitmask for horizontal and vertical alignment
|
|
1654
1821
|
* @param (optional) mode a scaling method (e.g. Jimp.RESIZE_BEZIER)
|
|
1655
1822
|
* @param (optional) cb a callback for when complete
|
|
1656
1823
|
* @returns this for chaining of methods
|
|
1657
1824
|
*/
|
|
1658
|
-
Jimp.prototype.contain = function (w, h, mode, cb) {
|
|
1825
|
+
Jimp.prototype.contain = function (w, h, alignBits, mode, cb) {
|
|
1659
1826
|
if ("number" != typeof w || "number" != typeof h)
|
|
1660
1827
|
return throwError.call(this, "w and h must be numbers", cb);
|
|
1661
1828
|
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1829
|
+
//permit any sort of optional parameters combination
|
|
1830
|
+
switch (typeof alignBits) {
|
|
1831
|
+
case 'string':
|
|
1832
|
+
if ("function" == typeof mode && "undefined" == typeof cb) cb = mode;
|
|
1833
|
+
mode = alignBits;
|
|
1834
|
+
alignBits = null;
|
|
1835
|
+
case 'function':
|
|
1836
|
+
if ("undefined" == typeof cb) cb = alignBits;
|
|
1837
|
+
mode = null;
|
|
1838
|
+
alignBits = null;
|
|
1839
|
+
default:
|
|
1840
|
+
if ("function" == typeof mode && "undefined" == typeof cb) {
|
|
1841
|
+
cb = mode;
|
|
1842
|
+
mode = null;
|
|
1843
|
+
}
|
|
1665
1844
|
}
|
|
1666
|
-
|
|
1845
|
+
|
|
1846
|
+
alignBits = alignBits || (Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);
|
|
1847
|
+
var hbits = ((alignBits) & ((1<<(3))-1));
|
|
1848
|
+
var vbits = alignBits >> 3;
|
|
1849
|
+
|
|
1850
|
+
// check if more flags than one is in the bit sets
|
|
1851
|
+
if(!(((hbits != 0) && !(hbits & (hbits - 1))) || ((vbits != 0) && !(vbits & (vbits - 1)))))
|
|
1852
|
+
return throwError.call(this, "only use one flag per alignment direction", cb);
|
|
1853
|
+
|
|
1854
|
+
var align_h = (hbits >> 1); // 0, 1, 2
|
|
1855
|
+
var align_v = (vbits >> 1); // 0, 1, 2
|
|
1856
|
+
|
|
1667
1857
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1668
1858
|
h/this.bitmap.height : w/this.bitmap.width;
|
|
1669
1859
|
var c = this.clone().scale(f, mode);
|
|
1670
|
-
|
|
1860
|
+
|
|
1671
1861
|
this.resize(w, h, mode);
|
|
1672
1862
|
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
1673
1863
|
this.bitmap.data.writeUInt32BE(this._background, idx);
|
|
1674
1864
|
});
|
|
1675
|
-
this.blit(c, this.bitmap.width
|
|
1676
|
-
|
|
1865
|
+
this.blit(c, ((this.bitmap.width - c.bitmap.width) / 2) * align_h, ((this.bitmap.height - c.bitmap.height) / 2) * align_v);
|
|
1866
|
+
|
|
1677
1867
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1678
1868
|
else return this;
|
|
1679
1869
|
};
|
|
@@ -1695,7 +1885,7 @@ Jimp.prototype.scale = function (f, mode, cb) {
|
|
|
1695
1885
|
cb = mode;
|
|
1696
1886
|
mode = null;
|
|
1697
1887
|
}
|
|
1698
|
-
|
|
1888
|
+
|
|
1699
1889
|
var w = this.bitmap.width * f;
|
|
1700
1890
|
var h = this.bitmap.height * f;
|
|
1701
1891
|
this.resize(w, h, mode);
|
|
@@ -1720,7 +1910,7 @@ Jimp.prototype.scaleToFit = function (w, h, mode, cb) {
|
|
|
1720
1910
|
cb = mode;
|
|
1721
1911
|
mode = null;
|
|
1722
1912
|
}
|
|
1723
|
-
|
|
1913
|
+
|
|
1724
1914
|
var f = (w/h > this.bitmap.width/this.bitmap.height) ?
|
|
1725
1915
|
h/this.bitmap.height : w/this.bitmap.width;
|
|
1726
1916
|
this.scale(f, mode);
|
|
@@ -1729,6 +1919,134 @@ Jimp.prototype.scaleToFit = function (w, h, mode, cb) {
|
|
|
1729
1919
|
else return this;
|
|
1730
1920
|
};
|
|
1731
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
|
+
|
|
1732
2050
|
/**
|
|
1733
2051
|
* Rotates an image clockwise by a number of degrees rounded to the nearest 90 degrees. NB: 'this' must be a Jimp object.
|
|
1734
2052
|
* @param deg the number of degress to rotate the image by
|
|
@@ -1752,7 +2070,7 @@ function simpleRotate(deg) {
|
|
|
1752
2070
|
}
|
|
1753
2071
|
|
|
1754
2072
|
this.bitmap.data = new Buffer(dstBuffer);
|
|
1755
|
-
|
|
2073
|
+
|
|
1756
2074
|
var tmp = this.bitmap.width;
|
|
1757
2075
|
this.bitmap.width = this.bitmap.height;
|
|
1758
2076
|
this.bitmap.height = tmp;
|
|
@@ -1771,7 +2089,7 @@ function advancedRotate(deg, mode) {
|
|
|
1771
2089
|
var rad = (deg % 360) * Math.PI / 180;
|
|
1772
2090
|
var cosine = Math.cos(rad);
|
|
1773
2091
|
var sine = Math.sin(rad);
|
|
1774
|
-
|
|
2092
|
+
|
|
1775
2093
|
var w, h; // the final width and height if resize == true
|
|
1776
2094
|
|
|
1777
2095
|
if (mode == true || "string" == typeof mode) {
|
|
@@ -1785,15 +2103,15 @@ function advancedRotate(deg, mode) {
|
|
|
1785
2103
|
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
1786
2104
|
this.bitmap.data.writeUInt32BE(this._background, idx);
|
|
1787
2105
|
});
|
|
1788
|
-
|
|
2106
|
+
|
|
1789
2107
|
var max= Math.max(w,h,this.bitmap.width,this.bitmap.height)
|
|
1790
2108
|
this.resize(max, max, mode);
|
|
1791
|
-
|
|
2109
|
+
|
|
1792
2110
|
this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);
|
|
1793
2111
|
}
|
|
1794
2112
|
|
|
1795
2113
|
var dstBuffer = new Buffer(this.bitmap.data.length);
|
|
1796
|
-
|
|
2114
|
+
|
|
1797
2115
|
function createTranslationFunction(deltaX, deltaY) {
|
|
1798
2116
|
return function(x, y) {
|
|
1799
2117
|
return {
|
|
@@ -1805,7 +2123,7 @@ function advancedRotate(deg, mode) {
|
|
|
1805
2123
|
|
|
1806
2124
|
var translate2Cartesian = createTranslationFunction(-(this.bitmap.width / 2), -(this.bitmap.height / 2));
|
|
1807
2125
|
var translate2Screen = createTranslationFunction(this.bitmap.width / 2, this.bitmap.height / 2);
|
|
1808
|
-
|
|
2126
|
+
|
|
1809
2127
|
for (var y = 0; y < this.bitmap.height; y++) {
|
|
1810
2128
|
for (var x = 0; x < this.bitmap.width; x++) {
|
|
1811
2129
|
var cartesian = translate2Cartesian(x, this.bitmap.height - y);
|
|
@@ -1815,7 +2133,7 @@ function advancedRotate(deg, mode) {
|
|
|
1815
2133
|
);
|
|
1816
2134
|
if (source.x >= 0 && source.x < this.bitmap.width
|
|
1817
2135
|
&& source.y >= 0 && source.y < this.bitmap.height) {
|
|
1818
|
-
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;
|
|
1819
2137
|
var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx, true);
|
|
1820
2138
|
var dstIdx = (this.bitmap.width * y + x) << 2;
|
|
1821
2139
|
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
|
|
@@ -1827,7 +2145,7 @@ function advancedRotate(deg, mode) {
|
|
|
1827
2145
|
}
|
|
1828
2146
|
}
|
|
1829
2147
|
this.bitmap.data = dstBuffer;
|
|
1830
|
-
|
|
2148
|
+
|
|
1831
2149
|
if (mode == true || "string" == typeof mode) {
|
|
1832
2150
|
// now crop the image to the final size
|
|
1833
2151
|
var x = (this.bitmap.width / 2) - (w/2);
|
|
@@ -1857,16 +2175,45 @@ Jimp.prototype.rotate = function (deg, mode, cb) {
|
|
|
1857
2175
|
cb = mode;
|
|
1858
2176
|
mode = true;
|
|
1859
2177
|
}
|
|
1860
|
-
|
|
2178
|
+
|
|
1861
2179
|
if ("number" != typeof deg)
|
|
1862
2180
|
return throwError.call(this, "deg must be a number", cb);
|
|
1863
|
-
|
|
2181
|
+
|
|
1864
2182
|
if ("boolean" != typeof mode && "string" != typeof mode)
|
|
1865
2183
|
return throwError.call(this, "mode must be a boolean or a string", cb);
|
|
1866
2184
|
|
|
1867
2185
|
if (deg % 90 == 0 && mode !== false) simpleRotate.call(this, deg, cb);
|
|
1868
2186
|
else advancedRotate.call(this, deg, mode, cb);
|
|
1869
|
-
|
|
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
|
+
|
|
1870
2217
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1871
2218
|
else return this;
|
|
1872
2219
|
};
|
|
@@ -1878,6 +2225,10 @@ Jimp.prototype.rotate = function (deg, mode, cb) {
|
|
|
1878
2225
|
* @returns this for chaining of methods
|
|
1879
2226
|
*/
|
|
1880
2227
|
Jimp.prototype.getBuffer = function (mime, cb) {
|
|
2228
|
+
if (mime == Jimp.AUTO) { // allow auto MIME detection
|
|
2229
|
+
mime = this.getMIME();
|
|
2230
|
+
}
|
|
2231
|
+
|
|
1881
2232
|
if ("string" != typeof mime)
|
|
1882
2233
|
return throwError.call(this, "mime must be a string", cb);
|
|
1883
2234
|
if ("function" != typeof cb)
|
|
@@ -1891,14 +2242,15 @@ Jimp.prototype.getBuffer = function (mime, cb) {
|
|
|
1891
2242
|
height:this.bitmap.height,
|
|
1892
2243
|
bitDepth: 8,
|
|
1893
2244
|
deflateLevel: this._deflateLevel,
|
|
2245
|
+
deflateStrategy: this._deflateStrategy,
|
|
1894
2246
|
filterType: this._filterType,
|
|
1895
2247
|
colorType: (this._rgba) ? 6 : 2,
|
|
1896
2248
|
inputHasAlpha: true
|
|
1897
2249
|
});
|
|
1898
|
-
|
|
2250
|
+
|
|
1899
2251
|
if (this._rgba) png.data = new Buffer(this.bitmap.data);
|
|
1900
2252
|
else png.data = compositeBitmapOverBackground(this).data; // when PNG doesn't support alpha
|
|
1901
|
-
|
|
2253
|
+
|
|
1902
2254
|
StreamToBuffer(png.pack(), function (err, buffer) {
|
|
1903
2255
|
return cb.call(that, null, buffer);
|
|
1904
2256
|
});
|
|
@@ -1910,6 +2262,8 @@ Jimp.prototype.getBuffer = function (mime, cb) {
|
|
|
1910
2262
|
return cb.call(this, null, jpeg.data);
|
|
1911
2263
|
|
|
1912
2264
|
case Jimp.MIME_BMP:
|
|
2265
|
+
case Jimp.MIME_X_MS_BMP:
|
|
2266
|
+
|
|
1913
2267
|
// composite onto a new image so that the background shows through alpha channels
|
|
1914
2268
|
var bmp = BMP.encode(compositeBitmapOverBackground(this));
|
|
1915
2269
|
return cb.call(this, null, bmp.data);
|
|
@@ -1926,34 +2280,54 @@ function compositeBitmapOverBackground(image){
|
|
|
1926
2280
|
}
|
|
1927
2281
|
|
|
1928
2282
|
/**
|
|
1929
|
-
*
|
|
2283
|
+
* Converts the image to a base 64 string
|
|
2284
|
+
* @param mime the mime type of the image data to be created
|
|
2285
|
+
* @param cb a Node-style function to call with the buffer as the second argument
|
|
2286
|
+
* @returns this for chaining of methods
|
|
2287
|
+
*/
|
|
2288
|
+
Jimp.prototype.getBase64 = function (mime, cb) {
|
|
2289
|
+
if (mime == Jimp.AUTO) { // allow auto MIME detection
|
|
2290
|
+
mime = this.getMIME();
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
if ("string" != typeof mime)
|
|
2294
|
+
return throwError.call(this, "mime must be a string", cb);
|
|
2295
|
+
if ("function" != typeof cb)
|
|
2296
|
+
return throwError.call(this, "cb must be a function", cb);
|
|
2297
|
+
|
|
2298
|
+
this.getBuffer(mime, function(err, data) {
|
|
2299
|
+
var src = "data:" + mime + ";base64," + data.toString("base64");
|
|
2300
|
+
return cb.call(this, null, src);
|
|
2301
|
+
});
|
|
2302
|
+
|
|
2303
|
+
return this;
|
|
2304
|
+
};
|
|
2305
|
+
|
|
2306
|
+
/**
|
|
2307
|
+
* Apply a ordered dithering effect
|
|
1930
2308
|
* @param (optional) cb a callback for when complete
|
|
1931
2309
|
* @returns this for chaining of methods
|
|
1932
2310
|
*/
|
|
1933
2311
|
Jimp.prototype.dither565 = function (cb) {
|
|
1934
2312
|
var rgb565_matrix = [
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
0, 4, 1, 5, 0, 4, 1, 5,
|
|
1940
|
-
6, 2, 7, 3, 6, 2, 7, 3,
|
|
1941
|
-
1, 5, 0, 4, 1, 5, 0, 4,
|
|
1942
|
-
7, 3, 6, 2, 7, 3, 6, 2
|
|
2313
|
+
1, 9, 3, 11,
|
|
2314
|
+
13, 5, 15, 7,
|
|
2315
|
+
4, 12, 2, 10,
|
|
2316
|
+
16, 8, 14, 6
|
|
1943
2317
|
];
|
|
1944
|
-
|
|
1945
2318
|
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
|
|
1946
|
-
var tresshold_id = ((y
|
|
2319
|
+
var tresshold_id = ((y & 3) << 2) + (x % 4);
|
|
1947
2320
|
var dither = rgb565_matrix[tresshold_id];
|
|
1948
|
-
this.bitmap.data[idx ] = Math.min(this.bitmap.data[idx] + dither, 0xff)
|
|
1949
|
-
this.bitmap.data[idx+1] = Math.min(this.bitmap.data[idx+1] + dither, 0xff)
|
|
1950
|
-
this.bitmap.data[idx+2] = Math.min(this.bitmap.data[idx+2] + dither, 0xff)
|
|
2321
|
+
this.bitmap.data[idx ] = Math.min(this.bitmap.data[idx] + dither, 0xff);
|
|
2322
|
+
this.bitmap.data[idx+1] = Math.min(this.bitmap.data[idx+1] + dither, 0xff);
|
|
2323
|
+
this.bitmap.data[idx+2] = Math.min(this.bitmap.data[idx+2] + dither, 0xff);
|
|
1951
2324
|
});
|
|
1952
2325
|
|
|
1953
2326
|
if (isNodePattern(cb)) return cb.call(this, null, this);
|
|
1954
2327
|
else return this;
|
|
1955
2328
|
}
|
|
1956
2329
|
|
|
2330
|
+
|
|
1957
2331
|
// alternative reference
|
|
1958
2332
|
Jimp.prototype.dither16 = Jimp.prototype.dither565;
|
|
1959
2333
|
|
|
@@ -2029,7 +2403,7 @@ Jimp.loadFont = function (file, cb) {
|
|
|
2029
2403
|
|
|
2030
2404
|
var that = this;
|
|
2031
2405
|
|
|
2032
|
-
return new Promise(function (resolve, reject) {
|
|
2406
|
+
return new Promise(function (resolve, reject) {
|
|
2033
2407
|
cb = cb || function(err, font) {
|
|
2034
2408
|
if (err) reject(err);
|
|
2035
2409
|
else resolve(font);
|
|
@@ -2072,33 +2446,62 @@ function loadPages(dir, pages) {
|
|
|
2072
2446
|
}
|
|
2073
2447
|
|
|
2074
2448
|
/**
|
|
2075
|
-
* Draws a text on a image
|
|
2449
|
+
* Draws a text on a image on a given boundary
|
|
2076
2450
|
* @param font a bitmap font loaded from `Jimp.loadFont` command
|
|
2077
2451
|
* @param x the x position to start drawing the text
|
|
2078
2452
|
* @param y the y position to start drawing the text
|
|
2079
2453
|
* @param text the text to draw
|
|
2454
|
+
* @param maxWidth (optional) the boundary width to draw in
|
|
2080
2455
|
* @param (optional) cb a function to call when the text is written
|
|
2081
2456
|
* @returns this for chaining of methods
|
|
2082
2457
|
*/
|
|
2083
|
-
Jimp.prototype.print = function (font, x, y, text, cb) {
|
|
2458
|
+
Jimp.prototype.print = function (font, x, y, text, maxWidth, cb) {
|
|
2459
|
+
if ("function" == typeof maxWidth && "undefined" == typeof cb) {
|
|
2460
|
+
cb = maxWidth;
|
|
2461
|
+
maxWidth = Infinity;
|
|
2462
|
+
}
|
|
2463
|
+
if ("undefined" == typeof maxWidth) {
|
|
2464
|
+
maxWidth = Infinity;
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2084
2467
|
if ("object" != typeof font)
|
|
2085
2468
|
return throwError.call(this, "font must be a Jimp loadFont", cb);
|
|
2086
|
-
if ("number" != typeof x || "number" != typeof y)
|
|
2087
|
-
return throwError.call(this, "x and
|
|
2469
|
+
if ("number" != typeof x || "number" != typeof y || "number" != typeof maxWidth)
|
|
2470
|
+
return throwError.call(this, "x, y and maxWidth must be numbers", cb);
|
|
2088
2471
|
if ("string" != typeof text)
|
|
2089
2472
|
return throwError.call(this, "text must be a string", cb);
|
|
2473
|
+
if ("number" != typeof maxWidth)
|
|
2474
|
+
return throwError.call(this, "maxWidth must be a number", cb);
|
|
2090
2475
|
|
|
2091
2476
|
var that = this;
|
|
2092
2477
|
|
|
2478
|
+
var words = text.split(' ');
|
|
2479
|
+
var line = '';
|
|
2480
|
+
|
|
2481
|
+
for (var n = 0; n < words.length; n++) {
|
|
2482
|
+
var testLine = line + words[n] + ' ';
|
|
2483
|
+
var testWidth = measureText(font, testLine);
|
|
2484
|
+
if (testWidth > maxWidth && n > 0) {
|
|
2485
|
+
that = that.print(font, x, y, line);
|
|
2486
|
+
line = words[n] + ' ';
|
|
2487
|
+
y += font.common.lineHeight;
|
|
2488
|
+
} else {
|
|
2489
|
+
line = testLine;
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
printText.call(this, font, x, y, line);
|
|
2493
|
+
|
|
2494
|
+
if (isNodePattern(cb)) return cb.call(this, null, that);
|
|
2495
|
+
else return that;
|
|
2496
|
+
};
|
|
2497
|
+
|
|
2498
|
+
function printText(font, x, y, text) {
|
|
2093
2499
|
for (var i = 0; i < text.length; i++) {
|
|
2094
2500
|
if (font.chars[text[i]]) {
|
|
2095
|
-
|
|
2501
|
+
drawCharacter(this, font, x, y, font.chars[text[i]]);
|
|
2096
2502
|
x += (font.kernings[text[i]] && font.kernings[text[i]][text[i+1]] ? font.kernings[text[i]][text[i+1]] : 0) + (font.chars[text[i]].xadvance || 0);
|
|
2097
2503
|
}
|
|
2098
2504
|
}
|
|
2099
|
-
|
|
2100
|
-
if (isNodePattern(cb)) return cb.call(this, null, that);
|
|
2101
|
-
else return that;
|
|
2102
2505
|
};
|
|
2103
2506
|
|
|
2104
2507
|
function drawCharacter(image, font, x, y, char) {
|
|
@@ -2109,13 +2512,25 @@ function drawCharacter(image, font, x, y, char) {
|
|
|
2109
2512
|
return image;
|
|
2110
2513
|
};
|
|
2111
2514
|
|
|
2515
|
+
function measureText(font, text) {
|
|
2516
|
+
var x = 0;
|
|
2517
|
+
for (var i = 0; i < text.length; i++) {
|
|
2518
|
+
if (font.chars[text[i]]) {
|
|
2519
|
+
x += font.chars[text[i]].xoffset
|
|
2520
|
+
+ (font.kernings[text[i]] && font.kernings[text[i]][text[i+1]] ? font.kernings[text[i]][text[i+1]] : 0)
|
|
2521
|
+
+ (font.chars[text[i]].xadvance || 0);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
return x;
|
|
2525
|
+
};
|
|
2526
|
+
|
|
2112
2527
|
/**
|
|
2113
2528
|
* Writes the image to a file
|
|
2114
2529
|
* @param path a path to the destination file (either PNG or JPEG)
|
|
2115
2530
|
* @param (optional) cb a function to call when the image is saved to disk
|
|
2116
2531
|
* @returns this for chaining of methods
|
|
2117
2532
|
*/
|
|
2118
|
-
|
|
2533
|
+
Jimp.prototype.write = function (path, cb) {
|
|
2119
2534
|
if ("string" != typeof path)
|
|
2120
2535
|
return throwError.call(this, "path must be a string", cb);
|
|
2121
2536
|
if ("undefined" == typeof cb) cb = function () {};
|
|
@@ -2125,6 +2540,9 @@ if (process.env.ENVIRONMENT !== 'BROWSER') Jimp.prototype.write = function (path
|
|
|
2125
2540
|
var that = this;
|
|
2126
2541
|
var mime = MIME.lookup(path);
|
|
2127
2542
|
|
|
2543
|
+
var pathObj = Path.parse(path);
|
|
2544
|
+
if (pathObj.dir) MkDirP.sync(pathObj.dir);
|
|
2545
|
+
|
|
2128
2546
|
this.getBuffer(mime, function(err, buffer) {
|
|
2129
2547
|
if (err) return throwError.call(that, err, cb);
|
|
2130
2548
|
var stream = FS.createWriteStream(path);
|
|
@@ -2142,7 +2560,22 @@ if (process.env.ENVIRONMENT !== 'BROWSER') Jimp.prototype.write = function (path
|
|
|
2142
2560
|
return this;
|
|
2143
2561
|
};
|
|
2144
2562
|
|
|
2145
|
-
|
|
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
|
+
|
|
2578
|
+
if (process.env.ENVIRONMENT === "BROWSER") {
|
|
2146
2579
|
// For use in a web browser or web worker
|
|
2147
2580
|
var gl;
|
|
2148
2581
|
if (typeof window == "object") gl = window;
|
|
@@ -2150,5 +2583,6 @@ if (process.env.ENVIRONMENT === 'BROWSER') {
|
|
|
2150
2583
|
|
|
2151
2584
|
gl.Jimp = Jimp;
|
|
2152
2585
|
gl.Buffer = Buffer;
|
|
2586
|
+
} else {
|
|
2587
|
+
module.exports = Jimp;
|
|
2153
2588
|
}
|
|
2154
|
-
module.exports = Jimp;
|