This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
canvas does not support being loaded in worker threads due to it being a native dependency that was not written using a thread-compatible API. sharp, on the other hand, is thread-aware: lovell/sharp#1558.
- Loading branch information
1 parent
e4b8503
commit f3e1bf9
Showing
13 changed files
with
328 additions
and
230 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const RGBA_CHANNEL_COUNT = 4 | ||
|
||
export function createImageData( | ||
data: Uint8ClampedArray, | ||
width: number, | ||
height: number | ||
): ImageData | ||
export function createImageData(width: number, height: number): ImageData | ||
export function createImageData(...args: unknown[]): ImageData { | ||
let data: Uint8ClampedArray | ||
let width: number | ||
let height: number | ||
|
||
if ( | ||
args.length === 2 && | ||
typeof args[0] === 'number' && | ||
typeof args[1] === 'number' | ||
) { | ||
;[width, height] = args | ||
data = new Uint8ClampedArray(width * height * RGBA_CHANNEL_COUNT) | ||
} else if ( | ||
args.length === 3 && | ||
args[0] instanceof Uint8ClampedArray && | ||
typeof args[1] === 'number' && | ||
typeof args[2] === 'number' | ||
) { | ||
;[data, width, height] = args | ||
} else { | ||
throw new TypeError('unexpected arguments given to createImageData') | ||
} | ||
|
||
return { data, width, height } | ||
} |
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
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,18 +1,17 @@ | ||
import { createCanvas, loadImage } from 'canvas' | ||
import { promises as fs } from 'fs' | ||
import sharp from 'sharp' | ||
|
||
export async function readImageData(fileData: Buffer): Promise<ImageData> | ||
export async function readImageData(filePath: string): Promise<ImageData> | ||
export async function readImageData( | ||
filePathOrData: string | Buffer | ||
): Promise<ImageData> { | ||
const fileData = | ||
typeof filePathOrData === 'string' | ||
? await fs.readFile(filePathOrData) | ||
: filePathOrData | ||
const image = await loadImage(fileData) | ||
const canvas = createCanvas(image.width, image.height) | ||
const context = canvas.getContext('2d') | ||
context.drawImage(image, 0, 0) | ||
return context.getImageData(0, 0, image.width, image.height) | ||
const img = await sharp(filePathOrData) | ||
.raw() | ||
.ensureAlpha() | ||
.toBuffer({ resolveWithObject: true }) | ||
return { | ||
data: Uint8ClampedArray.from(img.data), | ||
width: img.info.width, | ||
height: img.info.height, | ||
} | ||
} |
Oops, something went wrong.