Skip to content

Commit

Permalink
perf(screenshot): use sampling for is-white (#570)
Browse files Browse the repository at this point in the history
* test(screenshot): add is-white assertion

* perf: use sampling

* test: update snapshot
  • Loading branch information
Kikobeats authored Mar 19, 2024
1 parent 6de02ac commit 5f89406
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/goto/test/unit/evasions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ test('media codecs are present', async t => {
}
})

t.deepEqual(await videoCodecs(), { ogg: 'probably', h264: 'probably', webm: 'probably' })
t.deepEqual(await videoCodecs(), { ogg: '', h264: 'probably', webm: 'probably' })

t.deepEqual(await audioCodecs(), {
ogg: 'probably',
Expand Down
8 changes: 6 additions & 2 deletions packages/screenshot/src/is-white-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module.exports = async buffer => {
const height = image.getHeight()
const width = image.getWidth()

for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const samplePercentage = 0.25 // Sample 25% of the image
const sampleSize = Math.floor(width * height * samplePercentage) // Calculate sample size based on percentage
const stepSize = Math.max(1, Math.floor((width * height) / sampleSize)) // Calculate step size based on sample size

for (let i = 0; i < height; i += stepSize) {
for (let j = 0; j < width; j += stepSize) {
if (firstPixel !== image.getPixelColor(j, i)) return false
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/screenshot/test/fixtures/white-5k.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/screenshot/test/fixtures/white-5k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions packages/screenshot/test/is-white-screenshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const test = require('ava')
const { readFile } = require('fs/promises')

const isWhite = require('../src/is-white-screenshot')

test('true', async t => {
t.true(await isWhite(await readFile('./test/fixtures/white-5k.jpg')))
t.true(await isWhite(await readFile('./test/fixtures/white-5k.png')))
})

test('false', async t => {
t.false(await isWhite(await readFile('./test/fixtures/no-white-5k.jpg')))
t.false(await isWhite(await readFile('./test/fixtures/no-white-5k.png')))
})

0 comments on commit 5f89406

Please sign in to comment.