UNPKG

@nuintun/qrcode

Version:

A pure JavaScript QRCode encode and decode library.

54 lines (49 loc) 1.32 kB
/** * @module QRCode * @package @nuintun/qrcode * @license MIT * @version 5.0.2 * @author nuintun <nuintun@qq.com> * @description A pure JavaScript QRCode encode and decode library. * @see https://github.com/nuintun/qrcode#readme */ 'use strict'; const utils = require('./utils.cjs'); const BitMatrix = require('./BitMatrix.cjs'); /** * @module GridSampler */ class GridSampler { #matrix; #transform; constructor(matrix, transform) { this.#matrix = matrix; this.#transform = transform; } sample(width, height) { const matrix = this.#matrix; const matrixWidth = matrix.width; const transform = this.#transform; const matrixHeight = matrix.height; const bits = new BitMatrix.BitMatrix(width, height); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const [mappingX, mappingY] = transform.mapping(x + 0.5, y + 0.5); const offsetX = utils.toInt32(mappingX); const offsetY = utils.toInt32(mappingY); if ( // Assert axis. offsetX >= 0 && offsetY >= 0 && offsetX < matrixWidth && offsetY < matrixHeight && matrix.get(offsetX, offsetY) ) { bits.set(x, y); } } } return bits; } } exports.GridSampler = GridSampler;