diff --git a/packages/barcodes/code128.rip b/packages/barcodes/code128.rip index 917f0ac6..b80fb569 100644 --- a/packages/barcodes/code128.rip +++ b/packages/barcodes/code128.rip @@ -62,8 +62,8 @@ charValue =! (c, subset) -> codewords =! (text, gs1) -> n = text.length fail 'Code 128 text is empty' unless n - cost = new Int32Array(3 * (n + 1)) - move = new Uint8Array(3 * (n + 1)) + cost = Int32Array.new(3 * (n + 1)) + move = Uint8Array.new(3 * (n + 1)) i = n - 1 while i >= 0 c = text.charCodeAt i @@ -144,7 +144,7 @@ codewords =! (text, gs1) -> # Module row: 1 dark, 0 light, bars first within each codeword. modules =! (codes) -> - out = new Uint8Array(11 * codes.length + 2) + out = Uint8Array.new(11 * codes.length + 2) x = 0 for code in codes pattern = PATTERNS[code] @@ -186,7 +186,7 @@ raster =! (mods, scale, border, height) -> { W, H, bars } pixelRow =! (r) -> - row = new Uint8Array(r.W) + row = Uint8Array.new(r.W) for k in [0...r.bars.length] by 2 row.fill 1, r.bars[k], r.bars[k] + r.bars[k + 1] row @@ -254,7 +254,7 @@ export encodeCode128 =! (text, output = 'raw', opts = {}) -> # Widths flattened for the matcher, seven slots per value. WIDTHS =! do -> - out = new Uint8Array(7 * PATTERNS.length) + out = Uint8Array.new(7 * PATTERNS.length) for pattern, code in PATTERNS for k in [0...pattern.length] out[7 * code + k] = pattern.charCodeAt(k) - 48 @@ -437,11 +437,11 @@ export readCode128 =! (img, opts = {}) -> asObject opts, 'opts' validateImage img, opts.format {width, height} = img - luma = new Uint8Array(width * height) + luma = Uint8Array.new(width * height) copyLuma luma, undefined, img, opts.format side = Math.max(width, height) + 2 - runs = new Int32Array(side) - rev = new Int32Array(side) + runs = Int32Array.new(side) + rev = Int32Array.new(side) codes = [] vertical = false hit = scanLines luma, width, height, false, runs, rev, codes diff --git a/packages/barcodes/dom.rip b/packages/barcodes/dom.rip index 2cfbfa49..3ddfce10 100644 --- a/packages/barcodes/dom.rip +++ b/packages/barcodes/dom.rip @@ -165,7 +165,7 @@ export class QRCanvas return if @bitmapDrawn @bitmapDrawn = true @drawBitmap img - @scanner = new Scanner(decoder) + @scanner = Scanner.new(decoder) @reader = clean: => @generation++ @@ -331,7 +331,7 @@ export class QRCanvas return unless @bitmap {data, height, width} = img if @rotate - out = new Uint8ClampedArray(data.length) + out = Uint8ClampedArray.new(data.length) for y in [0...height] for x in [0...width] src = 4 * (y * width + x) @@ -821,7 +821,7 @@ invalidState = (msg) -> DOMException.new msg, 'InvalidStateError' prefixed = (e, prefix) -> return DOMException.new("#{prefix}: #{e.message}", e.name) if e instanceof DOMException - return new e.constructor("#{prefix}: #{e.message}") if e instanceof Error + return e.constructor.new("#{prefix}: #{e.message}") if e instanceof Error Error.new "#{prefix}: #{e}" createCanvas = (width, height) -> @@ -878,8 +878,9 @@ toImageData = (image) -> throw invalidState('Failed to load or decode HTMLImageElement.') return drawSource(image, image.naturalWidth, image.naturalHeight) if kind is 'SVGImageElement' + decoding = image.decode?() try - await image.decode?() + await decoding catch throw invalidState('Failed to load or decode SVGImageElement.') return drawSource(image, image.width.baseVal.value, image.height.baseVal.value) diff --git a/packages/barcodes/gif.rip b/packages/barcodes/gif.rip index 4ae4d558..ef570148 100644 --- a/packages/barcodes/gif.rip +++ b/packages/barcodes/gif.rip @@ -13,7 +13,7 @@ export writeGif =! (W, H, rowFor) -> N = 126 fullChunks = pixels // N tail = pixels % N - out = new Uint8Array(408 + fullChunks * (N + 2) + 2 + tail + 4) + out = Uint8Array.new(408 + fullChunks * (N + 2) + 2 + tail + 4) pos = 0 u16 = (v) -> out[pos++] = v & 0xff diff --git a/packages/barcodes/image.rip b/packages/barcodes/image.rip index 6988665c..0644e517 100644 --- a/packages/barcodes/image.rip +++ b/packages/barcodes/image.rip @@ -53,14 +53,14 @@ export validateImage =! (img, named, layout, capacity) -> return RGBA if bytes.length is 4 * px throw RangeError.new "\"img.data\" expected #{3 * px} or #{4 * px} bytes without opts.format, got #{bytes.length}" -export LITTLE_ENDIAN =! new Uint8Array(new Uint32Array([1]).buffer)[0] is 1 +export LITTLE_ENDIAN =! Uint8Array.new(Uint32Array.new([1]).buffer)[0] is 1 # Packed four-byte pixels read as whole words: one load per pixel instead of # three, unrolled four wide, the same (r + 2g + b) / 4 luma. Alpha and the X # byte fall out of the mask, and the weights are symmetric in r and b, so # every RGBA/BGRA/X layout takes this path. copyWords =! (out, data, byteStart, n) -> - words = new Uint32Array(data.buffer, byteStart, n) + words = Uint32Array.new(data.buffer, byteStart, n) i = 0 end = n - 3 while i < end @@ -106,7 +106,7 @@ export copyLuma =! (out, maxSize, img, named, layout) -> # RGBA image from a per-pixel dark predicate. export darkToImage =! (width, height, isDark) -> - data = new Uint8Array(width * height * 4) + data = Uint8Array.new(width * height * 4) i = 0 for y in [0...height] for x in [0...width] diff --git a/packages/barcodes/qr.rip b/packages/barcodes/qr.rip index 2d1f795c..b1f88ab6 100644 --- a/packages/barcodes/qr.rip +++ b/packages/barcodes/qr.rip @@ -39,7 +39,7 @@ fail =! (msg) -> throw Error.new msg # charCode -> Table 5 value; -1 outside the alphabet. ALNUM_VAL =! do -> - t = new Int8Array(128).fill(-1) + t = Int8Array.new(128).fill(-1) for i in [0...ALPHANUMERIC.length] t[ALPHANUMERIC.charCodeAt(i)] = i t @@ -52,7 +52,7 @@ RS_CACHE =! [] rsGenerator =! (n) -> {exp, log} = GF256 - gen = new Uint8Array(n) + gen = Uint8Array.new(n) gen[n - 1] = 1 root = 1 for i in [0...n] @@ -66,7 +66,7 @@ rsCached =! (n) -> return RS_CACHE[n] if RS_CACHE[n] {exp, log} = GF256 gen = rsGenerator n - products = new Uint8Array(256 * n) + products = Uint8Array.new(256 * n) for f in [1...256] lf = log[f] base = f * n @@ -80,7 +80,7 @@ rsEcc =! (data, rs) -> {gen, products} = rs n = gen.length last = n - 1 - res = new Uint8Array(n) + res = Uint8Array.new(n) for i in [0...data.length] base = (data[i] ^ res[0]) * n for j in [0...last] @@ -112,7 +112,7 @@ encodeData =! (ver, ecc, text, type, utf8) -> lengthBits = LENGTH_BITS[type][(ver + 7) // 17] dataLen = if type is 'byte' then utf8.length else text.length fail 'Capacity overflow' if dataLen >= 1 << lengthBits - bytes = new Uint8Array(cap.capacity >>> 3) + bytes = Uint8Array.new(cap.capacity >>> 3) # MSB-first accumulator flushed a byte at a time; pushes are <= 16 bits and # a flush keeps it below 8, so it never nears 32. acc = 0 @@ -163,7 +163,7 @@ encodeData =! (ver, ecc, text, type, utf8) -> blocks.push block eccs.push rsEcc(block, rs) pos += len - res = new Uint8Array(bytes.length + words * numBlocks) + res = Uint8Array.new(bytes.length + words * numBlocks) out = 0 for i in [0..blockLen] for b in blocks @@ -177,7 +177,7 @@ encodeData =! (ver, ecc, text, type, utf8) -> mat =! (size) -> words = (size + 31) >>> 5 - { size, words, v: new Uint32Array(words * size) } + { size, words, v: Uint32Array.new(words * size) } matGet =! (m, x, y) -> (m.v[y * m.words + (x >>> 5)] >>> (x & 31)) & 1 @@ -188,7 +188,7 @@ matSet =! (m, x, y, bit) -> return TRANSPOSE_MASKS =! [0x55555555, 0x33333333, 0x0f0f0f0f, 0x00ff00ff, 0x0000ffff] -TRANSPOSE_TMP =! new Uint32Array(32) +TRANSPOSE_TMP =! Uint32Array.new(32) # 32x32 in-place bit-matrix transpose (butterfly network). transpose32 =! (a) -> @@ -352,7 +352,7 @@ symCache = null buildSymCache =! (ver) -> size = 21 + 4 * (ver - 1) m = mat size - fun = new Uint8Array(size * size) + fun = Uint8Array.new(size * size) setF = (x, y, bit) -> matSet m, x, y, bit fun[y * size + x] = 1 @@ -395,7 +395,7 @@ buildSymCache =! (ver) -> planes = [] for i in [0...8] planes.push mat(size) - posBuf = new Uint16Array(size * size) + posBuf = Uint16Array.new(size * size) n = 0 xOffset = size - 1 dir = -1 @@ -576,7 +576,7 @@ renderSvg =! (r, optimize) -> renderGif =! (r) -> W = r.W {m, map} = r - row = new Uint8Array(W) + row = Uint8Array.new(W) prevMy = -2 writeGif W, W, (y) -> my = map[y] @@ -644,7 +644,7 @@ export encodeQR =! (text, output = 'raw', opts = {}) -> W = (m.size + 2 * border) * scale maxOutputSize = if output is 'ascii' or output is 'gif' or output is 'data-url' then MAX_COMPACT_OUTPUT_SIZE else MAX_OUTPUT_SIZE throw RangeError.new "invalid opts: output is #{W}x#{W} (max #{maxOutputSize}), reduce border/scale" if W > maxOutputSize - map = new Int32Array(W) + map = Int32Array.new(W) for i in [0...W] f = i // scale - border map[i] = if f >= 0 and f < m.size then f else -1 @@ -705,15 +705,15 @@ BYTE_LENGTH_BITS =! [8, 16, 16] class Payload constructor: (capacity) -> @position = 0 - @data = new Uint8Array(0) + @data = Uint8Array.new(0) @dataLen = 0 - @bytes = new Uint8Array(capacity) + @bytes = Uint8Array.new(capacity) @views = Array.new(capacity + 1) # A length-n window over the byte arena, created once per length so the # frame path never allocates after its first use. view: (n) -> - @views[n] or (@views[n] = new Uint8Array(@bytes.buffer, 0, n)) + @views[n] or (@views[n] = Uint8Array.new(@bytes.buffer, 0, n)) read: (bits) -> start = @position @@ -778,7 +778,7 @@ class Payload length = @read BYTE_LENGTH_BITS[cls] return FAIL.data if length < 0 or @position + 8 * length > dataLen * 8 if parts - segment = new Uint8Array(length) + segment = Uint8Array.new(length) for i in [0...length] segment[i] = @read(8) parts.push res if res @@ -1117,7 +1117,7 @@ resizeRows =! (src, dst, width, dstWidth, from, to) -> # The same box filter over whole words: one word from each source row holds # four pixels, summed in two 16-bit lanes to produce two output pixels. resizeWords =! (src, dst, width, dstWidth, from, to) -> - words = new Uint32Array(src.buffer, src.byteOffset, (width * (to << 1)) >> 2) + words = Uint32Array.new(src.buffer, src.byteOffset, (width * (to << 1)) >> 2) wordsPerRow = width >> 2 pairs = dstWidth >> 1 for y in [from...to] @@ -1312,24 +1312,24 @@ export class QRScanner @opts = Object.freeze { ...init, effort: @effort, maxSize, timeLimit: @timeLimit } @width = 0 @height = 0 - @luma = new Uint8Array(bytes) + @luma = Uint8Array.new(bytes) @image = { data: @luma, height: 0, width: 0 } @input = { data: @luma, height: 0, width: 0 } - @grid = new Uint8Array(177 * 177) - @fun = new Uint8Array(177 * 177) - @alignPos = new Uint8Array(7) - @codewords = new Uint8Array(BYTES[39]) - @blockBytes = new Uint8Array(BYTES[39]) - @syndromes = new Uint8Array(31) - @sigma = new Uint8Array(31) - @previous = new Uint8Array(31) - @next = new Uint8Array(31) - @candidates = new Uint32Array(16) - @ranks = new Float64Array(16) - @found = new Uint32Array(4 * 16 * 3) - @pick = new Float64Array(8) - @nodes = new Float64Array(7 * 7 * 2) - @located = new Float64Array((7 * 7 - 3) * 4) + @grid = Uint8Array.new(177 * 177) + @fun = Uint8Array.new(177 * 177) + @alignPos = Uint8Array.new(7) + @codewords = Uint8Array.new(BYTES[39]) + @blockBytes = Uint8Array.new(BYTES[39]) + @syndromes = Uint8Array.new(31) + @sigma = Uint8Array.new(31) + @previous = Uint8Array.new(31) + @next = Uint8Array.new(31) + @candidates = Uint32Array.new(16) + @ranks = Float64Array.new(16) + @found = Uint32Array.new(4 * 16 * 3) + @pick = Float64Array.new(8) + @nodes = Float64Array.new(7 * 7 * 2) + @located = Float64Array.new((7 * 7 - 3) * 4) @payload = Payload.new BYTES[39] @inFlight = false @staged = false @@ -1341,11 +1341,11 @@ export class QRScanner @points = undefined # `map` holds the active homography; `from` and `to` are scratch, and `to` # doubles as the fine-plane map until the next build. - @map = new Float64Array(9) - @from = new Float64Array(9) - @to = new Float64Array(9) + @map = Float64Array.new(9) + @from = Float64Array.new(9) + @to = Float64Array.new(9) @alignPoint = { x: 0.1, y: 0.1 } - @finePlane = { d: new Uint8Array(0), cut: new Int16Array(0), W: 0, H: 0, bw: 0, sh: 0 } + @finePlane = { d: Uint8Array.new(0), cut: Int16Array.new(0), W: 0, H: 0, bw: 0, sh: 0 } @invertedProjection = false @decodedSize = 0 layers = [] @@ -1356,18 +1356,18 @@ export class QRScanner blockWidth = Math.ceil(width / 8) blockHeight = Math.ceil(height / 8) centers = Math.ceil(width / 7) * Math.ceil(height / 7) - luma = if i then new Uint8Array(width * height) else @luma - cuts = new Int16Array(blockWidth * blockHeight) + luma = if i then Uint8Array.new(width * height) else @luma + cuts = Int16Array.new(blockWidth * blockHeight) layers.push - bitmap: new Uint32Array(Math.ceil(width / 32) * height) + bitmap: Uint32Array.new(Math.ceil(width / 32) * height) blockHeight: 0 blockWidth: 0 - blocks: new Uint8Array(blockWidth * blockHeight) + blocks: Uint8Array.new(blockWidth * blockHeight) cuts: cuts height: 0 luma: luma patternCount: 0 - patterns: new Float64Array(centers * 4) + patterns: Float64Array.new(centers * 4) used: false width: 0 words: 0 @@ -1377,10 +1377,10 @@ export class QRScanner # Native luma offered to downscaled sampling; `r` is the layer index. context: { opts: @opts, scale: 1 << i, ox: 0, oy: 0, fine: (if i then { luma: @image, r: i } else undefined) } found: false - inverted: new Uint8Array(centers) + inverted: Uint8Array.new(centers) setCount: 0 setCursor: 0 - sets: new Float64Array(256 * 5) + sets: Float64Array.new(256 * 5) setsReady: false # Round-0 pick as an id bag (sum, min, max); retries skip a matching set. pickSum: -1 diff --git a/packages/barcodes/spec.rip b/packages/barcodes/spec.rip index 37bb5dca..a263560b 100644 --- a/packages/barcodes/spec.rip +++ b/packages/barcodes/spec.rip @@ -86,8 +86,8 @@ export versionBits =! (ver) -> # GF(2^8) with primitive polynomial 0x11d. EXP is doubled so the product of # two logs indexes it without a modulo. export GF256 =! do -> - exp = new Uint8Array(510) - log = new Uint8Array(256) + exp = Uint8Array.new(510) + log = Uint8Array.new(256) x = 1 for i in [0...255] exp[i] = exp[i + 255] = x @@ -116,7 +116,7 @@ export maskBits =! (x, y) -> bits POP16 =! do -> - t = new Uint8Array(1 << 16) + t = Uint8Array.new(1 << 16) for i in [1...t.length] t[i] = t[i >>> 1] + (i & 1) t diff --git a/packages/barcodes/test.rip b/packages/barcodes/test.rip index 333bcc17..8b027a08 100644 --- a/packages/barcodes/test.rip +++ b/packages/barcodes/test.rip @@ -168,7 +168,7 @@ console.log "\nDecoder" raster = (text, opts, scale, W, H, invert = false, rotate = 0) -> raw = encodeQR text, 'raw', { ...opts, scale, border: 4 } s = raw.length - data = new Uint8Array(W * H * 4).fill(if invert then 40 else 200) + data = Uint8Array.new(W * H * 4).fill(if invert then 40 else 200) ox = (W - s) >> 1 oy = (H - s) >> 1 for y in [0...s] @@ -188,7 +188,7 @@ raster = (text, opts, scale, W, H, invert = false, rotate = 0) -> toLuma = (img) -> n = img.width * img.height - luma = new Uint8Array(n) + luma = Uint8Array.new(n) for i in [0...n] p = 4 * i luma[i] = (img.data[p] + 2 * img.data[p + 1] + img.data[p + 2]) >> 2 @@ -214,13 +214,13 @@ test "luma and packed formats", -> img = raster 'FORMATS', {}, 4, 300, 300 eq decodeQR(toLuma(img), format: 'I420'), 'FORMATS' eq decodeQR(img, format: 'RGBA'), 'FORMATS' - rgb = new Uint8Array(img.width * img.height * 3) + rgb = Uint8Array.new(img.width * img.height * 3) for i in [0...img.width * img.height] rgb[3 * i + k] = img.data[4 * i + k] for k in [0...3] eq decodeQR({ width: img.width, height: img.height, data: rgb }), 'FORMATS' test "throws when nothing decodes", -> - blank = { width: 200, height: 200, data: new Uint8Array(200 * 200 * 4).fill(128) } + blank = { width: 200, height: 200, data: Uint8Array.new(200 * 200 * 4).fill(128) } throws (-> decodeQR blank), Error, 'finder' eq decodeQR(raster('EFFORT', {}, 4, 300, 300), effort: Infinity, timeLimit: Infinity), 'EFFORT' @@ -230,8 +230,8 @@ test "option and image validation", -> throws (-> decodeQR(img, timeLimit: -1)), TypeError throws (-> decodeQR(img, format: 'nope')), TypeError throws (-> decodeQR(img, pointsOnDetect: 1)), TypeError - throws (-> decodeQR({ width: 0, height: 1, data: new Uint8Array(0) })), RangeError - throws (-> decodeQR({ width: 2, height: 2, data: new Uint8Array(5) })), RangeError + throws (-> decodeQR({ width: 0, height: 1, data: Uint8Array.new(0) })), RangeError + throws (-> decodeQR({ width: 2, height: 2, data: Uint8Array.new(5) })), RangeError throws (-> decodeQR({ width: 2, height: 2, data: [1, 2, 3, 4] })), TypeError test "pointsOnDetect reports geometry in image coordinates", -> @@ -279,7 +279,7 @@ test! "scanner reuse, exclusive operations and clean", -> eq scanner.decode(), ['TWO'] # A success excludes its region, so a staged frame decodes once; re-stage. scanner.addImage raster('TWO', {}, 4, 250, 250) - eq scanner.decodeAsync!(), ['TWO'] + eq scanner.decodeAsync!, ['TWO'] throws (-> scanner.addImage raster('BIG', {}, 4, 400, 400)), RangeError reentered = null reenter = -> @@ -351,7 +351,7 @@ bars = (text, opts, scale, W, H, rotate = 0, invert = false) -> raw = encodeCode128 text, 'raw', { ...opts, scale, height: 1 } w = raw.length h = 30 - data = new Uint8Array(W * H * 4).fill(if invert then 40 else 190) + data = Uint8Array.new(W * H * 4).fill(if invert then 40 else 190) turned = rotate is 90 or rotate is 270 ox = (W - (if turned then h else w)) >> 1 oy = (H - (if turned then w else h)) >> 1 @@ -394,11 +394,11 @@ test "Code 128 round trips: scales, rotations, inversion, GS1", -> eq hit.text, 'AB\x1dCD' eq hit.gs1, false eq readCode128(toLuma(bars('Wikipedia', {}, 2, 500, 300)), format: 'I420').text, 'Wikipedia' - blank = { width: 50, height: 50, data: new Uint8Array(50 * 50 * 4).fill(200) } + blank = { width: 50, height: 50, data: Uint8Array.new(50 * 50 * 4).fill(200) } eq readCode128(blank), null throws (-> decodeCode128 blank), Error, 'not found' throws (-> readCode128 blank, null), TypeError, 'opts' - throws (-> readCode128 { width: 2, height: 2, data: new Uint8Array(3) }), RangeError, 'img.data' + throws (-> readCode128 { width: 2, height: 2, data: Uint8Array.new(3) }), RangeError, 'img.data' test "Code 128 text: shifts, FNC4 high characters, latches", -> text = (codes) -> c128.decodeText(codes, codes.length).text