forked from tyrone-sudeium/napi-gif-encoder
-
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.
- Loading branch information
Showing
6 changed files
with
116 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { loadBinding } from '@node-rs/helper' | ||
|
||
export default loadBinding(__dirname, 'napi-gif-encoder', '@gomander/napi-gif-encoder') | ||
export const { GIFEncoder } = loadBinding( | ||
import.meta.dirname, | ||
'napi-gif-encoder', | ||
'@gomander/napi-gif-encoder' | ||
) |
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,81 @@ | ||
/* eslint-disable no-console */ | ||
import { createReadStream } from 'node:fs' | ||
import { join } from 'node:path' | ||
import { argv } from 'node:process' | ||
import JsGifEncoder from 'gif-encoder-2' | ||
import { PNG } from 'pngjs' | ||
import { GIFEncoder } from '../../index.mjs' | ||
|
||
const imagePaths = [...Array(46).keys()].map((i) => `../BBB${i + 1580}.png`) | ||
|
||
async function loadImage(path) { | ||
return new Promise((resolve, reject) => { | ||
createReadStream(path) | ||
.pipe(new PNG()) | ||
.on('parsed', function () { | ||
resolve({ | ||
buffer: this.data, | ||
width: this.width, | ||
height: this.height, | ||
}) | ||
}) | ||
.on('error', reject) | ||
}) | ||
} | ||
|
||
async function loadImages() { | ||
const promises = imagePaths.map(loadImage) | ||
return await Promise.all(promises) | ||
} | ||
|
||
async function main() { | ||
const images = await loadImages() | ||
try { | ||
const encoder = new GIFEncoder(images[0].width, images[0].height, join(import.meta.dirname, 'output.gif')) | ||
encoder.setFrameRate(30) | ||
encoder.setSampleFactor(2) | ||
// encoder.setRepeat(0) | ||
for (const image of images) { | ||
encoder.addFrame(image.buffer) | ||
} | ||
console.log('Encoding with Rust GIF encoder') | ||
const start = new Date().getTime() | ||
await encoder.finish() | ||
const end = new Date().getTime() | ||
console.log(`Encode time: ${end - start}ms`) | ||
} catch (error) { | ||
console.error(`Unexpected error: ${JSON.stringify(error)}`) | ||
} | ||
} | ||
|
||
class ContextLike { | ||
constructor(buffer) { | ||
this.buffer = buffer | ||
} | ||
|
||
getImageData(sx, sy, sw, sh) { | ||
return { data: this.buffer } | ||
} | ||
} | ||
|
||
async function mainJs() { | ||
const images = await loadImages() | ||
const gif = new JsGifEncoder(images[0].width, images[1].height, 'neuquant', true, 46) | ||
gif.setFrameRate(30) | ||
gif.setRepeat(1) | ||
console.log('Encoding with JavaScript GIF encoder') | ||
const start = new Date().getTime() | ||
gif.start() | ||
for (const image of images) { | ||
gif.addFrame(new ContextLike(image)) | ||
} | ||
gif.finish() | ||
const end = new Date().getTime() | ||
console.log(`Encode time: ${end - start}ms`) | ||
} | ||
|
||
if (argv.includes('--js')) { | ||
mainJs() | ||
} else { | ||
main() | ||
} |
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,15 @@ | ||
{ | ||
"name": "napi-gif-encoder-sample-mjs", | ||
"version": "0.0.7", | ||
"description": "Mjs sample for napi-gif-encoder", | ||
"type": "module", | ||
"module": "index.js", | ||
"repository": "git@github.com:gomander/napi-gif-encoder.git", | ||
"author": "Tyrone Trevorrow <tyrone@sudeium.com>", | ||
"license": "MIT", | ||
"private": false, | ||
"dependencies": { | ||
"gif-encoder-2": "tyrone-sudeium/gif-encoder-2#feature/type_definitions", | ||
"pngjs": "^6.0.0" | ||
} | ||
} |
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,12 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
gif-encoder-2@tyrone-sudeium/gif-encoder-2#feature/type_definitions: | ||
version "1.0.5" | ||
resolved "https://codeload.github.com/tyrone-sudeium/gif-encoder-2/tar.gz/a7aa7760d2af8a7c4dfbce788f4767233f8fe99e" | ||
|
||
pngjs@^6.0.0: | ||
version "6.0.0" | ||
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-6.0.0.tgz#ca9e5d2aa48db0228a52c419c3308e87720da821" | ||
integrity sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg== |
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