@nuintun/qrcode
Advanced tools
Comparing version 0.33.0 to 0.34.0
@@ -17,5 +17,9 @@ 'use strict'; | ||
var Matrix = /*#__PURE__*/ (function () { | ||
function Matrix(width, height) { | ||
function Matrix(width, height, buffer) { | ||
this.width = width; | ||
this.data = new Uint8ClampedArray(width * height); | ||
var bufferSize = width * height; | ||
if (buffer && buffer.length !== bufferSize) { | ||
throw 'wrong buffer size'; | ||
} | ||
this.data = buffer || new Uint8ClampedArray(bufferSize); | ||
} | ||
@@ -30,14 +34,24 @@ Matrix.prototype.get = function (x, y) { | ||
}()); | ||
function binarize(data, width, height, returnInverted) { | ||
if (data.length !== width * height * 4) { | ||
function binarize(data, width, height, returnInverted, greyscaleWeights, canOverwriteImage) { | ||
var pixelCount = width * height; | ||
if (data.length !== pixelCount * 4) { | ||
throw 'malformed data passed to binarizer'; | ||
} | ||
// Assign the greyscale and binary image within the rgba buffer as the rgba image will not be needed after conversion | ||
var bufferOffset = 0; | ||
// Convert image to greyscale | ||
var greyscalePixels = new Matrix(width, height); | ||
for (var x = 0; x < width; x++) { | ||
for (var y = 0; y < height; y++) { | ||
var r = data[(y * width + x) * 4 + 0]; | ||
var g = data[(y * width + x) * 4 + 1]; | ||
var b = data[(y * width + x) * 4 + 2]; | ||
greyscalePixels.set(x, y, 0.2126 * r + 0.7152 * g + 0.0722 * b); | ||
var greyscaleBuffer; | ||
if (canOverwriteImage) { | ||
greyscaleBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
bufferOffset += pixelCount; | ||
} | ||
var greyscalePixels = new Matrix(width, height, greyscaleBuffer); | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
var position = (y * width + x) * 4; | ||
var r = data[position]; | ||
var g = data[position + 1]; | ||
var b = data[position + 2]; | ||
var value = greyscaleWeights.red * r + greyscaleWeights.green * g + greyscaleWeights.blue * b; | ||
greyscalePixels.set(x, y, greyscaleWeights.useIntegerApproximation ? (value + 128) >> 8 : value); | ||
} | ||
@@ -47,3 +61,9 @@ } | ||
var verticalRegionCount = Math.ceil(height / REGION_SIZE); | ||
var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount); | ||
var blackPointsCount = horizontalRegionCount * verticalRegionCount; | ||
var blackPointsBuffer; | ||
if (canOverwriteImage) { | ||
blackPointsBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, blackPointsCount); | ||
bufferOffset += blackPointsCount; | ||
} | ||
var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount, blackPointsBuffer); | ||
for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { | ||
@@ -89,6 +109,20 @@ for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { | ||
} | ||
var binarized; | ||
if (canOverwriteImage) { | ||
var binarizedBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
bufferOffset += pixelCount; | ||
binarized = new __chunk_21.BitMatrix(binarizedBuffer, width); | ||
} | ||
else { | ||
binarized = __chunk_21.BitMatrix.createEmpty(width, height); | ||
} | ||
var inverted = null; | ||
var binarized = __chunk_21.BitMatrix.createEmpty(width, height); | ||
if (returnInverted) { | ||
inverted = __chunk_21.BitMatrix.createEmpty(width, height); | ||
if (canOverwriteImage) { | ||
var invertedBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
inverted = new __chunk_21.BitMatrix(invertedBuffer, width); | ||
} | ||
else { | ||
inverted = __chunk_21.BitMatrix.createEmpty(width, height); | ||
} | ||
} | ||
@@ -95,0 +129,0 @@ for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { |
@@ -37,2 +37,9 @@ 'use strict'; | ||
var defaultOptions = { | ||
canOverwriteImage: true, | ||
greyScaleWeights: { | ||
red: 0.2126, | ||
green: 0.7152, | ||
blue: 0.0722, | ||
useIntegerApproximation: false | ||
}, | ||
inversionAttempts: 'attemptBoth' | ||
@@ -57,2 +64,3 @@ }; | ||
this.options = tslib_1.__assign({}, defaultOptions, options); | ||
return this; | ||
}; | ||
@@ -69,7 +77,8 @@ /** | ||
var options = this.options; | ||
var shouldInvert = options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst'; | ||
var tryInvertedFirst = options.inversionAttempts === 'onlyInvert' || options.inversionAttempts === 'invertFirst'; | ||
var _a = __chunk_28.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted; | ||
var canOverwriteImage = options.canOverwriteImage, greyScaleWeights = options.greyScaleWeights, inversionAttempts = options.inversionAttempts; | ||
var invert = inversionAttempts === 'attemptBoth' || inversionAttempts === 'invertFirst'; | ||
var tryInvertedFirst = inversionAttempts === 'onlyInvert' || inversionAttempts === 'invertFirst'; | ||
var _a = __chunk_28.binarize(data, width, height, invert, greyScaleWeights, canOverwriteImage), binarized = _a.binarized, inverted = _a.inverted; | ||
var result = scan(tryInvertedFirst ? inverted : binarized); | ||
if (!result && (options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst')) { | ||
if (!result && invert) { | ||
result = scan(tryInvertedFirst ? binarized : inverted); | ||
@@ -76,0 +85,0 @@ } |
@@ -183,2 +183,3 @@ 'use strict'; | ||
this.autoVersion = this.version === 0; | ||
return this; | ||
}; | ||
@@ -206,2 +207,3 @@ /** | ||
} | ||
return this; | ||
}; | ||
@@ -223,2 +225,3 @@ /** | ||
this.hasEncodingHint = hasEncodingHint; | ||
return this; | ||
}; | ||
@@ -243,2 +246,3 @@ /** | ||
} | ||
return this; | ||
}; | ||
@@ -455,2 +459,3 @@ /** | ||
this.modules = matrices[bestMaskPattern]; | ||
return this; | ||
}; | ||
@@ -457,0 +462,0 @@ /** |
@@ -15,5 +15,9 @@ import { BitMatrix } from './BitMatrix'; | ||
var Matrix = /*#__PURE__*/ (function () { | ||
function Matrix(width, height) { | ||
function Matrix(width, height, buffer) { | ||
this.width = width; | ||
this.data = new Uint8ClampedArray(width * height); | ||
var bufferSize = width * height; | ||
if (buffer && buffer.length !== bufferSize) { | ||
throw 'wrong buffer size'; | ||
} | ||
this.data = buffer || new Uint8ClampedArray(bufferSize); | ||
} | ||
@@ -28,14 +32,24 @@ Matrix.prototype.get = function (x, y) { | ||
}()); | ||
function binarize(data, width, height, returnInverted) { | ||
if (data.length !== width * height * 4) { | ||
function binarize(data, width, height, returnInverted, greyscaleWeights, canOverwriteImage) { | ||
var pixelCount = width * height; | ||
if (data.length !== pixelCount * 4) { | ||
throw 'malformed data passed to binarizer'; | ||
} | ||
// Assign the greyscale and binary image within the rgba buffer as the rgba image will not be needed after conversion | ||
var bufferOffset = 0; | ||
// Convert image to greyscale | ||
var greyscalePixels = new Matrix(width, height); | ||
for (var x = 0; x < width; x++) { | ||
for (var y = 0; y < height; y++) { | ||
var r = data[(y * width + x) * 4 + 0]; | ||
var g = data[(y * width + x) * 4 + 1]; | ||
var b = data[(y * width + x) * 4 + 2]; | ||
greyscalePixels.set(x, y, 0.2126 * r + 0.7152 * g + 0.0722 * b); | ||
var greyscaleBuffer; | ||
if (canOverwriteImage) { | ||
greyscaleBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
bufferOffset += pixelCount; | ||
} | ||
var greyscalePixels = new Matrix(width, height, greyscaleBuffer); | ||
for (var y = 0; y < height; y++) { | ||
for (var x = 0; x < width; x++) { | ||
var position = (y * width + x) * 4; | ||
var r = data[position]; | ||
var g = data[position + 1]; | ||
var b = data[position + 2]; | ||
var value = greyscaleWeights.red * r + greyscaleWeights.green * g + greyscaleWeights.blue * b; | ||
greyscalePixels.set(x, y, greyscaleWeights.useIntegerApproximation ? (value + 128) >> 8 : value); | ||
} | ||
@@ -45,3 +59,9 @@ } | ||
var verticalRegionCount = Math.ceil(height / REGION_SIZE); | ||
var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount); | ||
var blackPointsCount = horizontalRegionCount * verticalRegionCount; | ||
var blackPointsBuffer; | ||
if (canOverwriteImage) { | ||
blackPointsBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, blackPointsCount); | ||
bufferOffset += blackPointsCount; | ||
} | ||
var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount, blackPointsBuffer); | ||
for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { | ||
@@ -87,6 +107,20 @@ for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { | ||
} | ||
var binarized; | ||
if (canOverwriteImage) { | ||
var binarizedBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
bufferOffset += pixelCount; | ||
binarized = new BitMatrix(binarizedBuffer, width); | ||
} | ||
else { | ||
binarized = BitMatrix.createEmpty(width, height); | ||
} | ||
var inverted = null; | ||
var binarized = BitMatrix.createEmpty(width, height); | ||
if (returnInverted) { | ||
inverted = BitMatrix.createEmpty(width, height); | ||
if (canOverwriteImage) { | ||
var invertedBuffer = new Uint8ClampedArray(data.buffer, bufferOffset, pixelCount); | ||
inverted = new BitMatrix(invertedBuffer, width); | ||
} | ||
else { | ||
inverted = BitMatrix.createEmpty(width, height); | ||
} | ||
} | ||
@@ -93,0 +127,0 @@ for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { |
@@ -35,2 +35,9 @@ import { __assign } from 'tslib'; | ||
var defaultOptions = { | ||
canOverwriteImage: true, | ||
greyScaleWeights: { | ||
red: 0.2126, | ||
green: 0.7152, | ||
blue: 0.0722, | ||
useIntegerApproximation: false | ||
}, | ||
inversionAttempts: 'attemptBoth' | ||
@@ -55,2 +62,3 @@ }; | ||
this.options = __assign({}, defaultOptions, options); | ||
return this; | ||
}; | ||
@@ -67,7 +75,8 @@ /** | ||
var options = this.options; | ||
var shouldInvert = options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst'; | ||
var tryInvertedFirst = options.inversionAttempts === 'onlyInvert' || options.inversionAttempts === 'invertFirst'; | ||
var _a = binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted; | ||
var canOverwriteImage = options.canOverwriteImage, greyScaleWeights = options.greyScaleWeights, inversionAttempts = options.inversionAttempts; | ||
var invert = inversionAttempts === 'attemptBoth' || inversionAttempts === 'invertFirst'; | ||
var tryInvertedFirst = inversionAttempts === 'onlyInvert' || inversionAttempts === 'invertFirst'; | ||
var _a = binarize(data, width, height, invert, greyScaleWeights, canOverwriteImage), binarized = _a.binarized, inverted = _a.inverted; | ||
var result = scan(tryInvertedFirst ? inverted : binarized); | ||
if (!result && (options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst')) { | ||
if (!result && invert) { | ||
result = scan(tryInvertedFirst ? binarized : inverted); | ||
@@ -74,0 +83,0 @@ } |
@@ -181,2 +181,3 @@ import { Mode } from '../common/Mode'; | ||
this.autoVersion = this.version === 0; | ||
return this; | ||
}; | ||
@@ -204,2 +205,3 @@ /** | ||
} | ||
return this; | ||
}; | ||
@@ -221,2 +223,3 @@ /** | ||
this.hasEncodingHint = hasEncodingHint; | ||
return this; | ||
}; | ||
@@ -241,2 +244,3 @@ /** | ||
} | ||
return this; | ||
}; | ||
@@ -453,2 +457,3 @@ /** | ||
this.modules = matrices[bestMaskPattern]; | ||
return this; | ||
}; | ||
@@ -455,0 +460,0 @@ /** |
{ | ||
"name": "@nuintun/qrcode", | ||
"version": "0.33.0", | ||
"version": "0.34.0", | ||
"description": "A pure JavaScript QRCode encode and decode library.", | ||
@@ -34,7 +34,7 @@ "main": "es5/index.js", | ||
"del": "^4.1.1", | ||
"rollup": "^1.12.1", | ||
"rollup": "^1.12.3", | ||
"typescript": "^3.4.5", | ||
"acorn-walk": "^6.1.1", | ||
"magic-string": "^0.25.2", | ||
"rollup-plugin-typescript2": "^0.21.0", | ||
"rollup-plugin-typescript2": "^0.21.1", | ||
"rollup-plugin-node-resolve": "^5.0.0" | ||
@@ -41,0 +41,0 @@ }, |
@@ -51,3 +51,3 @@ # QRCode | ||
- setVersion(version: number): void | ||
- setVersion(version: number): Encoder | ||
@@ -60,3 +60,3 @@ - Set qrcode version, if set `0` the version will be set automatically. | ||
- setErrorCorrectionLevel(errorCorrectionLevel: ErrorCorrectionLevel): void | ||
- setErrorCorrectionLevel(errorCorrectionLevel: ErrorCorrectionLevel): Encoder | ||
@@ -69,3 +69,3 @@ - Set qrcode error correction level. | ||
- setEncodingHint(hasEncodingHint: boolean): void | ||
- setEncodingHint(hasEncodingHint: boolean): Encoder | ||
@@ -78,3 +78,3 @@ - Set qrcode encoding hint, it will add ECI in qrcode. | ||
- write(data: string | QRByte | QRKanji | QRNumeric | QRAlphanumeric): void | ||
- write(data: string | QRByte | QRKanji | QRNumeric | QRAlphanumeric): Encoder | ||
@@ -87,3 +87,3 @@ - Add qrcode data, if string will use `QRByte` by default. | ||
- make(): void | ||
- make(): Encoder | ||
@@ -140,5 +140,8 @@ - Make qrcode matrix. | ||
- setOptions(options: { inversionAttempts: 'dontInvert' | 'onlyInvert' | 'attemptBoth' | 'invertFirst' }): void | ||
- setOptions(options: Optins): Decoder | ||
- Set decode options. | ||
- canOverwriteImage?: boolean | ||
- inversionAttempts?: 'dontInvert' | 'onlyInvert' | 'attemptBoth' | 'invertFirst' | ||
- greyScaleWeights?: { red: number, green: number, blue: number, useIntegerApproximation?: boolean } | ||
@@ -145,0 +148,0 @@ - scan(src: string): Promise\<DecoderResult> |
@@ -7,2 +7,8 @@ /** | ||
import { BitMatrix } from './BitMatrix'; | ||
export interface GreyscaleWeights { | ||
red: number; | ||
green: number; | ||
blue: number; | ||
useIntegerApproximation?: boolean; | ||
} | ||
export interface BinarizeResult { | ||
@@ -12,2 +18,2 @@ inverted?: BitMatrix; | ||
} | ||
export declare function binarize(data: Uint8ClampedArray, width: number, height: number, returnInverted: boolean): BinarizeResult; | ||
export declare function binarize(data: Uint8ClampedArray, width: number, height: number, returnInverted: boolean, greyscaleWeights: GreyscaleWeights, canOverwriteImage: boolean): BinarizeResult; |
@@ -8,2 +8,3 @@ /** | ||
import { DecodeResult } from './decoder'; | ||
import { GreyscaleWeights } from './binarizer'; | ||
export interface DecoderResult extends DecodeResult { | ||
@@ -22,2 +23,4 @@ location: { | ||
export interface Options { | ||
canOverwriteImage?: boolean; | ||
greyScaleWeights?: GreyscaleWeights; | ||
inversionAttempts?: 'dontInvert' | 'onlyInvert' | 'attemptBoth' | 'invertFirst'; | ||
@@ -32,3 +35,3 @@ } | ||
*/ | ||
setOptions(options?: Options): void; | ||
setOptions(options?: Options): Decoder; | ||
/** | ||
@@ -35,0 +38,0 @@ * @public |
@@ -38,3 +38,3 @@ /** | ||
*/ | ||
setVersion(version: number): void; | ||
setVersion(version: number): Encoder; | ||
/** | ||
@@ -51,3 +51,3 @@ * @public | ||
*/ | ||
setErrorCorrectionLevel(errorCorrectionLevel: ErrorCorrectionLevel): void; | ||
setErrorCorrectionLevel(errorCorrectionLevel: ErrorCorrectionLevel): Encoder; | ||
/** | ||
@@ -64,3 +64,3 @@ * @public | ||
*/ | ||
setEncodingHint(hasEncodingHint: boolean): void; | ||
setEncodingHint(hasEncodingHint: boolean): Encoder; | ||
/** | ||
@@ -71,3 +71,3 @@ * @public | ||
*/ | ||
write(data: QRData | string): void; | ||
write(data: QRData | string): Encoder; | ||
/** | ||
@@ -92,3 +92,3 @@ * @public | ||
*/ | ||
make(): void; | ||
make(): Encoder; | ||
/** | ||
@@ -95,0 +95,0 @@ * @public |
401099
10138
157