Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blit src params Issue #613

Merged
merged 1 commit into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/plugin-blit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,21 @@ export default () => ({
srcw = Math.round(srcw);
srch = Math.round(srch);

const maxw = this.bitmap.width;
const maxh = this.bitmap.height;
const maxWidth = this.bitmap.width;
const maxHeight = this.bitmap.height;
const baseImage = this;

src.scanQuiet(srcx, srcy, srcw, srch, function(sx, sy, idx) {
const xOffset = x + sx - srcx;
const yOffset = y + sy - srcy;

if (
x + sx >= 0 &&
y + sy >= 0 &&
maxw - x - sx > 0 &&
maxh - y - sy > 0
xOffset >= 0 &&
yOffset >= 0 &&
maxWidth - xOffset > 0 &&
maxHeight - yOffset > 0
) {
const dstIdx = baseImage.getPixelIndex(x + sx - srcx, y + sy - srcy);
const dstIdx = baseImage.getPixelIndex(xOffset, yOffset);
const src = {
r: this.bitmap.data[idx],
g: this.bitmap.data[idx + 1],
Expand Down
74 changes: 69 additions & 5 deletions packages/plugin-blit/test/blit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import configure from '@jimp/custom';
import blit from '../src';

const jimp = configure({ types: [jpeg], plugins: [blit] }, Jimp);
const testDir = getTestDir(__dirname);

describe('Blit over image', function() {
this.timeout(15000);
Expand Down Expand Up @@ -157,14 +158,77 @@ describe('Blit over image', function() {
});

it('blit alpha', async () => {
const expectedImg = await Jimp.read(
getTestDir(__dirname) + '/images/blit-alpha.png'
);
const dice = await Jimp.read(getTestDir(__dirname) + '/images/dice.png');
const image = await Jimp.read(getTestDir(__dirname) + '/images/cops.jpg');
const expectedImg = await Jimp.read(testDir + '/images/blit-alpha.png');
const dice = await Jimp.read(testDir + '/images/dice.png');
const image = await Jimp.read(testDir + '/images/cops.jpg');

image
.blit(dice, 0, 0)
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
});

async function createCat(catNum, len) {
let imgHeight = 60;

const butt = await Jimp.read(testDir + '/images/cat_butt.png');
const head = await Jimp.read(testDir + '/images/cat_head.png');
const fuzz = await Jimp.read(testDir + '/images/cat_fuzz.png');

let longCat = len;
longCat = longCat > 20 ? 20 : longCat;
longCat = longCat <= 1 ? 1 : longCat;

const cat =
Math.floor(catNum * (head.bitmap.height / imgHeight)) * imgHeight;

const newImage = await Jimp.create(
butt.bitmap.width + head.bitmap.width + fuzz.bitmap.width * longCat,
imgHeight,
0x00000000
);

newImage.blit(butt, 0, 0, 0, cat, butt.bitmap.width, imgHeight);
for (let i = 0; i < longCat; i++) {
newImage.blit(
fuzz,
butt.bitmap.width + fuzz.bitmap.width * i,
0,
0,
cat,
fuzz.bitmap.width,
imgHeight
);
}
newImage.blit(
head,
butt.bitmap.width + fuzz.bitmap.width * longCat,
0,
0,
cat,
head.bitmap.width,
imgHeight
);

return newImage;
}

it('uses src params correctly', async () => {
const expectedSmall = await Jimp.read(
testDir + '/images/cat-results/small-cat.png'
);
const small = await createCat(0.3, 1);
small.bitmap.data.should.be.deepEqual(expectedSmall.bitmap.data);

const expectedMedium = await Jimp.read(
testDir + '/images/cat-results/medium-cat.png'
);
const medium = await createCat(0.6, 7);
medium.bitmap.data.should.be.deepEqual(expectedMedium.bitmap.data);

const expectedLarge = await Jimp.read(
testDir + '/images/cat-results/large-cat.png'
);
const large = await createCat(0.9, 20);
large.bitmap.data.should.be.deepEqual(expectedLarge.bitmap.data);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/plugin-blit/test/images/cat_butt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/plugin-blit/test/images/cat_fuzz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/plugin-blit/test/images/cat_head.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.