File size: 6,080 Bytes
bf237c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import { Jimp, mkJGD, getTestDir } from "@jimp/test-utils";
import jpeg from "@jimp/jpeg";
import configure from "@jimp/custom";
import expect from "@storybook/expect";
import blit from "../src";
import { expectToBeJGD } from "@jimp/test-utils/src";
const jimp = configure({ types: [jpeg], plugins: [blit] }, Jimp);
const testDir = getTestDir(__dirname);
describe("Blit over image", function () {
this.timeout(15000);
const targetJGD = mkJGD(
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆"
);
const srcJGD = mkJGD(
"â–¡â–¡â–¡â–¡â–¡â–¡",
"□▥▥▥▥□",
"□▥■■▥□",
"□▥■■▥□",
"□▥▥▥▥□",
"â–¡â–¡â–¡â–¡â–¡â–¡"
);
let targetImg;
let srcImg; // stores the Jimp instances of the JGD images above.
before((done) => {
const img1 = jimp.read(targetJGD);
const img2 = jimp.read(srcJGD);
Promise.all([img1, img2])
.then((images) => {
targetImg = images[0];
srcImg = images[1];
done();
})
.catch(done);
});
it("blit on top, with no crop", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, 0, 0).getJGDSync(),
mkJGD(
"□□□□□□▸▸",
"□▥▥▥▥□▸▸",
"□▥■■▥□▸▸",
"□▥■■▥□▸▸",
"□▥▥▥▥□◆◆",
"□□□□□□◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆"
)
);
});
it("blit on middle, with no crop", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, 1, 1).getJGDSync(),
mkJGD(
"▴▴▴▴▸▸▸▸",
"â–´â–¡â–¡â–¡â–¡â–¡â–¡â–¸",
"▴□▥▥▥▥□▸",
"▴□▥■■▥□▸",
"▾□▥■■▥□◆",
"▾□▥▥▥▥□◆",
"▾□□□□□□◆",
"▾▾▾▾◆◆◆◆"
)
);
});
it("blit on middle, with x,y crop", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, 2, 2, 1, 1, 5, 5).getJGDSync(),
mkJGD(
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▥▥▥▥□▸",
"▴▴▥■■▥□▸",
"▾▾▥■■▥□◆",
"▾▾▥▥▥▥□◆",
"▾▾□□□□□◆",
"▾▾▾▾◆◆◆◆"
)
);
});
it("blit on middle, with x,y,w,h crop", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, 2, 2, 1, 1, 4, 4).getJGDSync(),
mkJGD(
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▥▥▥▥▸▸",
"▴▴▥■■▥▸▸",
"▾▾▥■■▥◆◆",
"▾▾▥▥▥▥◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆"
)
);
});
it("blit partially out, on top-left", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, -1, -1).getJGDSync(),
mkJGD(
"▥▥▥▥□▸▸▸",
"▥■■▥□▸▸▸",
"▥■■▥□▸▸▸",
"▥▥▥▥□▸▸▸",
"□□□□□◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆"
)
);
});
it("blit partially out, on bottom-right", () => {
expectToBeJGD(
targetImg.clone().blit(srcImg, 3, 3).getJGDSync(),
mkJGD(
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"â–´â–´â–´â–¡â–¡â–¡â–¡â–¡",
"▾▾▾□▥▥▥▥",
"▾▾▾□▥■■▥",
"▾▾▾□▥■■▥",
"▾▾▾□▥▥▥▥"
)
);
});
it("blit alpha", async () => {
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");
expect(image.blit(dice, 0, 0).bitmap.data).toEqual(expectedImg.bitmap.data);
});
async function createCat(catNum, len) {
const 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);
expect(small.bitmap.data).toEqual(expectedSmall.bitmap.data);
const expectedMedium = await Jimp.read(
testDir + "/images/cat-results/medium-cat.png"
);
const medium = await createCat(0.6, 7);
expect(medium.bitmap.data).toEqual(expectedMedium.bitmap.data);
const expectedLarge = await Jimp.read(
testDir + "/images/cat-results/large-cat.png"
);
const large = await createCat(0.9, 20);
expect(large.bitmap.data).toEqual(expectedLarge.bitmap.data);
});
});
|