sweetcorn 0.0.1 → 0.1.0
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/dist/index.d.ts +9 -6
- package/dist/index.js +11 -3
- package/dist/types.d.ts +36 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { Sharp } from 'sharp';
|
|
2
|
-
import type {
|
|
3
|
-
export type { DitheringAlgorithm };
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import type { SweetcornOptions } from './types';
|
|
3
|
+
export type { DitheringAlgorithm } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Create a dithered image — turn smooth pixels into crunchy kernels! 🌽
|
|
6
|
+
* @param image A Sharp image instance, e.g. created using `sharp('my-file.png')`.
|
|
7
|
+
* @param options Options configuring Sweetcorn.
|
|
8
|
+
* @returns A new Sharp image instance.
|
|
9
|
+
*/
|
|
10
|
+
export default function sweetcorn(image: Sharp, options: SweetcornOptions): Promise<Sharp>;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import thresholdMaps from './threshold-maps.json' with { type: 'json' };
|
|
2
|
+
;
|
|
2
3
|
import diffusionKernels from './diffusion-kernels';
|
|
3
4
|
let sharp;
|
|
4
5
|
async function loadSharp() {
|
|
@@ -13,7 +14,14 @@ async function loadSharp() {
|
|
|
13
14
|
sharpImport.cache(false);
|
|
14
15
|
return sharpImport;
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Create a dithered image — turn smooth pixels into crunchy kernels! 🌽
|
|
19
|
+
* @param image A Sharp image instance, e.g. created using `sharp('my-file.png')`.
|
|
20
|
+
* @param options Options configuring Sweetcorn.
|
|
21
|
+
* @returns A new Sharp image instance.
|
|
22
|
+
*/
|
|
23
|
+
export default async function sweetcorn(image, options) {
|
|
24
|
+
const { algorithm } = options;
|
|
17
25
|
if (!sharp)
|
|
18
26
|
sharp = await loadSharp();
|
|
19
27
|
// Convert image to greyscale before dithering.
|
|
@@ -21,8 +29,8 @@ export default async function sweetcorn(image, { algorithm }) {
|
|
|
21
29
|
image.gamma(2.2, 1).greyscale();
|
|
22
30
|
// Get raw pixel data for this image.
|
|
23
31
|
const rawPixels = await image.raw().toBuffer({ resolveWithObject: true });
|
|
24
|
-
const thresholdMap = thresholdMaps[algorithm];
|
|
25
|
-
const diffusionKernel = diffusionKernels[algorithm];
|
|
32
|
+
const thresholdMap = options.thresholdMap || thresholdMaps[algorithm];
|
|
33
|
+
const diffusionKernel = options.diffusionKernel || diffusionKernels[algorithm];
|
|
26
34
|
if (thresholdMap) {
|
|
27
35
|
applyThresholdMap(rawPixels, thresholdMap);
|
|
28
36
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
1
|
import type diffusionKernels from './diffusion-kernels';
|
|
2
2
|
import type thresholdMaps from './threshold-maps.json';
|
|
3
3
|
export type DitheringAlgorithm = keyof typeof thresholdMaps | keyof typeof diffusionKernels | 'white-noise' | 'threshold';
|
|
4
|
+
export interface SweetcornOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The name of one of Sweetcorn’s built-in dithering algorithms to use.
|
|
7
|
+
*
|
|
8
|
+
* @see https://delucis.github.io/sweetcorn/algorithms/
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* algorithm: 'floyd-steinberg'
|
|
12
|
+
*/
|
|
13
|
+
algorithm?: DitheringAlgorithm | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* A custom threshold map to use for ordered dithering.
|
|
16
|
+
*
|
|
17
|
+
* @see https://delucis.github.io/sweetcorn/guides/byo-algorithm/#defining-a-custom-threshold-map-for-sweetcorn
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* thresholdMap: [
|
|
21
|
+
* [0, 0.5],
|
|
22
|
+
* [0.75, 0.25],
|
|
23
|
+
* ]
|
|
24
|
+
*/
|
|
25
|
+
thresholdMap?: number[][] | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* A custom diffusion kernel to use for error diffusion dithering.
|
|
28
|
+
*
|
|
29
|
+
* @see https://delucis.github.io/sweetcorn/guides/byo-algorithm/#defining-a-custom-diffusion-kernel-for-sweetcorn
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* diffusionKernel: [
|
|
33
|
+
* [0, 0, 0.2],
|
|
34
|
+
* [0.1, 0.4, 0.1],
|
|
35
|
+
* [0, 0.2, 0],
|
|
36
|
+
* ]
|
|
37
|
+
*/
|
|
38
|
+
diffusionKernel?: number[][] | undefined;
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sweetcorn",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "JavaScript image dithering tools for Sharp",
|
|
6
6
|
"keywords": [
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"homepage": "https://github.
|
|
32
|
+
"homepage": "https://delucis.github.io/sweetcorn/",
|
|
33
33
|
"repository": {
|
|
34
34
|
"type": "git",
|
|
35
35
|
"url": "https://github.com/delucis/sweetcorn.git",
|