Skip to content

Commit c4c76c0

Browse files
committed
add advanced blit
1 parent 948fb5e commit c4c76c0

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ image.opacity( f ); // multiply the alpha channel by each pixel by th
7979
image.resize( w, h ); // resize the image. Jimp.AUTO can be passed as one of the values.
8080
image.scale( f ); // scale the image by the factor f
8181
image.rotate( deg[, resize] ); // rotate the image clockwise by a number of degrees. Unless `false` is passed as the second parameter, the image width and height will be resized appropriately.
82-
image.blit( src, x, y ); // blit the image with another Jimp image at x, y
82+
image.blit( src, x, y[, srcx, srcy, srcw, srch] );
83+
// blit the image with another Jimp image at x, y, optionally cropped.
8384
image.composite( src, x, y ); // composites another Jimp image over this iamge at x, y
8485
image.brightness( val ); // adjust the brighness by a value -1 to +1
8586
image.contrast( val ); // adjust the contrast by a value -1 to +1

index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,22 +829,48 @@ Jimp.prototype.autocrop = function (cb) {
829829
* @param src the source Jimp instance
830830
* @param x the x position to blit the image
831831
* @param y the y position to blit the image
832+
* @param srcx (optional) the x position from which to crop the source image
833+
* @param srcy (optional) the y position from which to crop the source image
834+
* @param srcw (optional) the width to which to crop the source image
835+
* @param srch (optional) the height to which to crop the source image
832836
* @param (optional) cb a callback for when complete
833837
* @returns this for chaining of methods
834838
*/
835-
Jimp.prototype.blit = function (src, x, y, cb) {
839+
Jimp.prototype.blit = function (src, x, y, srcx, srcy, srcw, srch, cb) {
836840
if ("object" != typeof src || src.constructor != Jimp)
837841
return throwError.call(this, "The source must be a Jimp image", cb);
838842
if ("number" != typeof x || "number" != typeof y)
839843
return throwError.call(this, "x and y must be numbers", cb);
840844

845+
if ("function" == typeof srcx) {
846+
cb = srcx;
847+
srcx = 0;
848+
srcy = 0;
849+
srcw = src.bitmap.width;
850+
srch = src.bitmap.height;
851+
} else if (typeof srcx == typeof srcy && typeof srcy == typeof srcw && typeof srcw == typeof srch) {
852+
srcx = srcx || 0;
853+
srcy = srcy || 0;
854+
srcw = srcw || src.bitmap.width;
855+
srch = srch || src.bitmap.height;
856+
} else {
857+
return throwError.call(this, "srcx, srcy, srcw, srch must be numbers", cb);
858+
}
859+
860+
841861
// round input
842862
x = Math.round(x);
843863
y = Math.round(y);
844864

865+
// round input
866+
srcx = Math.round(srcx);
867+
srcy = Math.round(srcy);
868+
srcw = Math.round(srcw);
869+
srch = Math.round(srch);
870+
845871
var that = this;
846-
src.scan(0, 0, src.bitmap.width, src.bitmap.height, function(sx, sy, idx) {
847-
var dstIdx = that.getPixelIndex(x+sx, y+sy);
872+
src.scan(srcx, srcy, srcw, srch, function(sx, sy, idx) {
873+
var dstIdx = that.getPixelIndex(x+sx-srcx, y+sy-srcy);
848874
that.bitmap.data[dstIdx] = this.bitmap.data[idx];
849875
that.bitmap.data[dstIdx+1] = this.bitmap.data[idx+1];
850876
that.bitmap.data[dstIdx+2] = this.bitmap.data[idx+2];

test/blit.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Jimp = require("../index.js");
2+
3+
var p1 = Jimp.read("lenna.png");
4+
var p2 = Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg");
5+
6+
Promise.all([p1, p2]).then(function (images) {
7+
var lenna = images[0];
8+
var bucket = images[1].scale(0.5);
9+
10+
lenna.clone().blit(bucket, 0, 0).write("./output/blit1.png");
11+
lenna.clone().blit(bucket, 50, 50, 50, 50, 128, 128).write("./output/blit2.png");
12+
lenna.clone().blit(bucket, 0, 0, 50, 50, 128, 128).write("./output/blit3.png");
13+
}).catch(function (err) {
14+
console.log(err);
15+
});

test/tests.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,48 @@ function clean {
1111
}
1212

1313
clean
14-
echo "promise.js (1/10)"
14+
echo "promise.js (1/11)"
1515
node promise.js
1616

1717
clean
18-
echo "callbacks.js (2/10)"
18+
echo "callbacks.js (2/11)"
1919
node callbacks.js
2020

2121
clean
22-
echo "chained.js (3/10)"
22+
echo "chained.js (3/11)"
2323
node chained.js
2424

2525
clean
26-
echo "create.js (4/10)"
26+
echo "create.js (4/11)"
2727
node create.js
2828

2929
clean
30-
echo "rotation.js (5/10)"
30+
echo "rotation.js (5/11)"
3131
node rotation.js
3232

3333
clean
34-
echo "filetype.js (6/10)"
34+
echo "filetype.js (6/11)"
3535
node filetypes.js
3636

3737
clean
38-
echo "color.js (7/10)"
38+
echo "color.js (7/11)"
3939
node color.js
4040

4141
clean
42-
echo "compare.js (8/10)"
42+
echo "compare.js (8/11)"
4343
node compare.js
4444

4545
clean
46-
echo "exif.js (9/10)"
46+
echo "exif.js (9/11)"
4747
node exif.js
4848

4949
clean
50-
echo "autocrop.js (10/10)"
50+
echo "autocrop.js (10/11)"
5151
node autocrop.js
5252

53-
53+
clean
54+
echo "blit.js (11/11)"
55+
node autocrop.js
5456

5557
clean
5658
echo "done :-)"

0 commit comments

Comments
 (0)