-
-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
296a776
commit 296d4b2
Showing
11 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Jimp as JimpCustom } from "@jimp/core"; | ||
|
||
import crop from "@jimp/plugin-crop"; | ||
import blit from "@jimp/plugin-blit"; | ||
|
||
import png from "@jimp/js-png"; | ||
|
||
export const Jimp = JimpCustom.addFormat(png).plugin(crop); | ||
export const Jimp = JimpCustom.addFormat(png).plugin(blit).plugin(crop); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "@jimp/plugin-blit", | ||
"version": "1.0.0", | ||
"repository": "jimp-dev/jimp", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "vitest", | ||
"build": "tshy", | ||
"dev": "tshy --watch" | ||
}, | ||
"author": "Andrew Lisowski <lisowski54@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@jimp/utils": "workspace:*", | ||
"@jimp/types": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@jimp/config-eslint": "workspace:*", | ||
"@jimp/config-typescript": "workspace:*", | ||
"eslint": "^8.57.0", | ||
"tshy": "^1.12.0", | ||
"typescript": "^5.4.2", | ||
"vitest": "^1.4.0" | ||
}, | ||
"tshy": { | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./src/index.ts" | ||
} | ||
}, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
} | ||
} | ||
}, | ||
"main": "./dist/commonjs/index.js", | ||
"types": "./dist/commonjs/index.d.ts", | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
test("adds 1 + 2 to equal 3", () => { | ||
expect(1 + 2).toBe(3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { JimpClass } from "@jimp/types"; | ||
import { limit255, scan } from "@jimp/utils"; | ||
|
||
/** | ||
* Blits a source image on to this image | ||
*/ | ||
function blit<I extends JimpClass>( | ||
image: I, | ||
{ | ||
src, | ||
x = 0, | ||
y = 0, | ||
srcX = 0, | ||
srcY = 0, | ||
srcW = src.bitmap.width, | ||
srcH = src.bitmap.height, | ||
}: { | ||
/** This image to blit on to the current image */ | ||
src: I; | ||
/** the x position to blit the image */ | ||
x: number; | ||
/** the y position to blit the image */ | ||
y: number; | ||
/** the x position from which to crop the source image */ | ||
srcX?: number; | ||
/** the y position from which to crop the source image */ | ||
srcY?: number; | ||
/** the width to which to crop the source image */ | ||
srcW?: number; | ||
/** the height to which to crop the source image */ | ||
srcH?: number; | ||
}, | ||
) { | ||
if (!("bitmap" in src)) { | ||
throw new Error("The source must be a Jimp image"); | ||
} | ||
|
||
if (typeof x !== "number" || typeof y !== "number") { | ||
throw new Error("x and y must be numbers"); | ||
} | ||
|
||
// round input | ||
x = Math.round(x); | ||
y = Math.round(y); | ||
|
||
// round input | ||
srcX = Math.round(srcX); | ||
srcY = Math.round(srcY); | ||
srcW = Math.round(srcW); | ||
srcH = Math.round(srcH); | ||
|
||
const maxWidth = image.bitmap.width; | ||
const maxHeight = image.bitmap.height; | ||
|
||
scan(src, srcX, srcY, srcW, srcH, function (sx, sy, idx) { | ||
const xOffset = x + sx - srcX; | ||
const yOffset = y + sy - srcY; | ||
|
||
if ( | ||
xOffset >= 0 && | ||
yOffset >= 0 && | ||
maxWidth - xOffset > 0 && | ||
maxHeight - yOffset > 0 | ||
) { | ||
const dstIdx = image.getPixelIndex(xOffset, yOffset); | ||
const srcColor = { | ||
r: src.bitmap.data[idx] || 0, | ||
g: src.bitmap.data[idx + 1] || 0, | ||
b: src.bitmap.data[idx + 2] || 0, | ||
a: src.bitmap.data[idx + 3] || 0, | ||
}; | ||
|
||
const dst = { | ||
r: image.bitmap.data[dstIdx] || 0, | ||
g: image.bitmap.data[dstIdx + 1] || 0, | ||
b: image.bitmap.data[dstIdx + 2] || 0, | ||
a: image.bitmap.data[dstIdx + 3] || 0, | ||
}; | ||
|
||
image.bitmap.data[dstIdx] = | ||
((srcColor.a * (srcColor.r - dst.r) - dst.r + 255) >> 8) + dst.r; | ||
image.bitmap.data[dstIdx + 1] = | ||
((srcColor.a * (srcColor.g - dst.g) - dst.g + 255) >> 8) + dst.g; | ||
image.bitmap.data[dstIdx + 2] = | ||
((srcColor.a * (srcColor.b - dst.b) - dst.b + 255) >> 8) + dst.b; | ||
image.bitmap.data[dstIdx + 3] = limit255(dst.a + srcColor.a); | ||
} | ||
}); | ||
|
||
return image; | ||
} | ||
|
||
export default function blitPlugin() { | ||
return { | ||
blit, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@jimp/config-typescript/base.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.