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
refactor: replace canvas with sharp #79
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decoding of images is slightly different between canvas and sharp, so we get different scores.