Skip to content

Commit

Permalink
test: use bmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 13, 2024
1 parent e630097 commit b02c94c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
Binary file added benchmark/fixtures/image-1.bmp
Binary file not shown.
Binary file removed benchmark/fixtures/image-1.jpg
Binary file not shown.
16 changes: 9 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ const quantize = require('@lokesh.dhakar/quantize')
const ndarray = require('ndarray')
const sharp = require('sharp')

async function getPixels (buffer) {
const { data, info } = await sharp(buffer)
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true })

async function getPixels ({ data, info }) {
return ndarray(
new Uint8Array(data.buffer, data.byteOffset, data.length),
[info.width, info.height, 4],
Expand Down Expand Up @@ -39,7 +34,14 @@ function createPixelArray (pixels, pixelCount, quality = 10) {
const toHex = ([r, g, b]) => '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1)

module.exports = async function (buffer) {
const imgData = await getPixels(await sharp(buffer).toBuffer())
const raw = await sharp(buffer)
// resizing the image before processing leads to more consistent (and much shorter) processing times.
// .resize(200, 200, { fit: 'inside', withoutEnlargement: true })
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true })

const imgData = await getPixels(raw)
const pixelCount = imgData.shape[0] * imgData.shape[1]
const pixelArray = createPixelArray(imgData.data, pixelCount)
const cmap = quantize(pixelArray, 10) // internal tuning
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const splashy = require('..')

const isHexcolor = hex => hexColorRegex({ strict: true }).test(hex)

test('jpg', async t => {
const filepath = path.join(__dirname, '../benchmark/fixtures/image-1.jpg')
test('bmp', async t => {
const filepath = path.join(__dirname, '../benchmark/fixtures/image-1.bmp')
const buffer = await readFile(filepath)
const palette = await splashy(buffer)

Expand Down

0 comments on commit b02c94c

Please sign in to comment.