From 5238b66eb67ff028902593007df0e169d3867ec4 Mon Sep 17 00:00:00 2001 From: NaitLee Date: Thu, 25 May 2023 19:01:21 +0800 Subject: [PATCH] [WIP] better bit-reverse on backend rename all_js to all-scripts minimal changes --- .vscode/settings.json | 3 +- printer_lib/commander.py | 13 +--- server.py | 8 +- wasm/image.d.ts | 5 ++ wasm/image.ts | 16 ++-- www/0-transpile.sh | 2 +- www/{all_js.txt => all-scripts.txt} | 0 www/image.js | 110 +++++++++++----------------- 8 files changed, 67 insertions(+), 90 deletions(-) create mode 100644 wasm/image.d.ts rename www/{all_js.txt => all-scripts.txt} (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 06b3da4..30e94ff 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "js/ts.implicitProjectConfig.strictNullChecks": false, "js/ts.implicitProjectConfig.checkJs": false, - "js/ts.implicitProjectConfig.experimentalDecorators": true + "js/ts.implicitProjectConfig.experimentalDecorators": true, + "deno.enable": true } \ No newline at end of file diff --git a/printer_lib/commander.py b/printer_lib/commander.py index 04af2c9..f017e18 100644 --- a/printer_lib/commander.py +++ b/printer_lib/commander.py @@ -41,16 +41,9 @@ def crc8(data): def reverse_bits(i: int): 'Reverse the bits of this byte (as `int`)' - return ( - (i & 0b10000000) >> 7 | - (i & 0b01000000) >> 5 | - (i & 0b00100000) >> 3 | - (i & 0b00010000) >> 1 | - (i & 0b00001000) << 1 | - (i & 0b00000100) << 3 | - (i & 0b00000010) << 5 | - (i & 0b00000001) << 7 - ) + i = ((i & 0b10101010) >> 1) | ((i & 0b01010101) << 1) + i = ((i & 0b11001100) >> 2) | ((i & 0b00110011) << 2) + return ((i & 0b11110000) >> 4) | ((i & 0b00001111) << 4) def int_to_bytes(i: int, big_endian=False): ''' Turn `int` into `bytearray`, that have diff --git a/server.py b/server.py index a1319d7..781840c 100644 --- a/server.py +++ b/server.py @@ -85,7 +85,7 @@ class PrinterServerHandler(BaseHTTPRequestHandler): _settings_blacklist = ( 'printer', 'is_android' ) - all_js: list = [] + all_script: list = [] printer: PrinterDriver = PrinterDriver() @@ -120,7 +120,7 @@ class PrinterServerHandler(BaseHTTPRequestHandler): self.send_response(200) self.send_header('Content-Type', mime(path)) self.end_headers() - for data in concat_files(*(self.all_js), prefix_format='\n// {0}\n'): + for data in concat_files(*(self.all_script), prefix_format='\n// {0}\n'): self.wfile.write(data) return path = 'www' + path @@ -321,10 +321,10 @@ class PrinterServer(HTTPServer): if self.handler is None: self.handler = self.handler_class(request, client_address, self) self.handler.load_config() - with open(os.path.join('www', 'all_js.txt'), 'r', encoding='utf-8') as file: + with open(os.path.join('www', 'all-scripts.txt'), 'r', encoding='utf-8') as file: for path in file.read().split('\n'): if path != '': - self.handler.all_js.append(os.path.join('www', path)) + self.handler.all_script.append(os.path.join('www', path)) return self.handler.__init__(request, client_address, self) diff --git a/wasm/image.d.ts b/wasm/image.d.ts new file mode 100644 index 0000000..e106597 --- /dev/null +++ b/wasm/image.d.ts @@ -0,0 +1,5 @@ +type i32 = number; +type u8 = number; +type f32 = number; +type f64 = number; +type bool = boolean; diff --git a/wasm/image.ts b/wasm/image.ts index fe3a7bd..a5fc9ee 100644 --- a/wasm/image.ts +++ b/wasm/image.ts @@ -1,6 +1,8 @@ +/// + export function monoGrayscale(rgba: Uint32Array, brightness: i32, alpha_as_white: bool): Uint8ClampedArray { - let mono = new Uint8ClampedArray(rgba.length); + const mono = new Uint8ClampedArray(rgba.length); let r: f32 = 0.0, g: f32 = 0.0, b: f32 = 0.0, a: f32 = 0.0, m: f32 = 0.0, n: i32 = 0; for (let i: i32 = 0; i < mono.length; ++i) { n = rgba[i]; @@ -22,7 +24,7 @@ export function monoGrayscale(rgba: Uint32Array, brightness: i32, alpha_as_white /** Note: returns a `Uint32Array` */ export function monoToRgba(mono: Uint8ClampedArray): Uint32Array { - let rgba = new Uint32Array(mono.length); + const rgba = new Uint32Array(mono.length); for (let i: i32 = 0; i < mono.length; ++i) { // little endian rgba[i] = 0xff000000 | (mono[i] << 16) | (mono[i] << 8) | mono[i]; @@ -30,7 +32,7 @@ export function monoToRgba(mono: Uint8ClampedArray): Uint32Array { return rgba; } -export function monoDirect(mono: Uint8ClampedArray, w: i32, h:i32): Uint8ClampedArray { +export function monoDirect(mono: Uint8ClampedArray, _w: i32, _h:i32): Uint8ClampedArray { for (let i: i32 = 0; i < mono.length; ++i) { mono[i] = mono[i] > 0x80 ? 0xff : 0x00; } @@ -62,7 +64,7 @@ export function monoSteinberg(mono: Uint8ClampedArray, w: i32, h: i32): Uint8Cla export function monoHalftone(mono: Uint8ClampedArray, w: i32, h: i32): Uint8ClampedArray { const spot: i32 = 4; const spot_h: i32 = spot / 2 + 1; - const spot_d: i32 = spot * 2; + // const spot_d: i32 = spot * 2; const spot_s: i32 = spot * spot; let i: i32, j: i32, x: i32, y: i32, o: f64 = 0.0; for (j = 0; j < h - spot; j += spot) { @@ -85,8 +87,8 @@ export function monoHalftone(mono: Uint8ClampedArray, w: i32, h: i32): Uint8Clam } export function monoToPbm(data: Uint8ClampedArray): Uint8ClampedArray { - let length: i32 = (data.length / 8) | 0; - let result = new Uint8ClampedArray(length); + const length: i32 = (data.length / 8) | 0; + const result = new Uint8ClampedArray(length); for (let i: i32 = 0, p: i32 = 0; i < data.length; ++p) { result[p] = 0; for (let d: u8 = 0; d < 8; ++i, ++d) @@ -105,7 +107,7 @@ export function rotateRgba(before: Uint32Array, w: i32, h: i32): Uint32Array { * +------+ | | after * before +---+ */ - let after = new Uint32Array(before.length); + const after = new Uint32Array(before.length); for (let j: i32 = 0; j < h; j++) { for (let i: i32 = 0; i < w; i++) { after[j * w + i] = before[(w - i - 1) * h + j]; diff --git a/www/0-transpile.sh b/www/0-transpile.sh index 21e7626..7ea5595 100755 --- a/www/0-transpile.sh +++ b/www/0-transpile.sh @@ -1,2 +1,2 @@ #!/bin/sh -npx tsc $@ --allowJs --outFile main.comp.js $(cat all_js.txt) +npx tsc $@ --allowJs --outFile main.comp.js $(cat all-scripts.txt) diff --git a/www/all_js.txt b/www/all-scripts.txt similarity index 100% rename from www/all_js.txt rename to www/all-scripts.txt diff --git a/www/image.js b/www/image.js index ec40885..6a7525f 100644 --- a/www/image.js +++ b/www/image.js @@ -1,21 +1,20 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.rotateRgba = exports.monoToPbm = exports.monoHalftone = exports.monoSteinberg = exports.monoDirect = exports.monoToRgba = exports.monoGrayscale = void 0; +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + function monoGrayscale(rgba, brightness, alpha_as_white) { - let mono = new Uint8ClampedArray(rgba.length); + const mono = new Uint8ClampedArray(rgba.length); let r = 0.0, g = 0.0, b = 0.0, a = 0.0, m = 0.0, n = 0; - for (let i = 0; i < mono.length; ++i) { + for(let i = 0; i < mono.length; ++i){ n = rgba[i]; - // little endian - r = (n & 0xff), g = (n >> 8 & 0xff), b = (n >> 16 & 0xff); + r = n & 0xff, g = n >> 8 & 0xff, b = n >> 16 & 0xff; a = (n >> 24 & 0xff) / 0xff; if (a < 1 && alpha_as_white) { a = 1 - a; r += (0xff - r) * a; g += (0xff - g) * a; b += (0xff - b) * a; - } - else { + } else { r *= a; g *= a; b *= a; @@ -26,100 +25,77 @@ function monoGrayscale(rgba, brightness, alpha_as_white) { } return mono; } -exports.monoGrayscale = monoGrayscale; -/** Note: returns a `Uint32Array` */ function monoToRgba(mono) { - let rgba = new Uint32Array(mono.length); - for (let i = 0; i < mono.length; ++i) { - // little endian - rgba[i] = 0xff000000 | (mono[i] << 16) | (mono[i] << 8) | mono[i]; + const rgba = new Uint32Array(mono.length); + for(let i = 0; i < mono.length; ++i){ + rgba[i] = 0xff000000 | mono[i] << 16 | mono[i] << 8 | mono[i]; } return rgba; } -exports.monoToRgba = monoToRgba; -function monoDirect(mono, w, h) { - for (let i = 0; i < mono.length; ++i) { +function monoDirect(mono, _w, _h) { + for(let i = 0; i < mono.length; ++i){ mono[i] = mono[i] > 0x80 ? 0xff : 0x00; } return mono; } -exports.monoDirect = monoDirect; function monoSteinberg(mono, w, h) { let p = 0, m, n, o; - for (let j = 0; j < h; ++j) { - for (let i = 0; i < w; ++i) { + for(let j = 0; j < h; ++j){ + for(let i = 0; i < w; ++i){ m = mono[p]; n = mono[p] > 0x80 ? 0xff : 0x00; o = m - n; mono[p] = n; - if (i >= 0 && i < w - 1 && j >= 0 && j < h) - mono[p + 1] += (o * 7 / 16); - if (i >= 1 && i < w && j >= 0 && j < h - 1) - mono[p + w - 1] += (o * 3 / 16); - if (i >= 0 && i < w && j >= 0 && j < h - 1) - mono[p + w] += (o * 5 / 16); - if (i >= 0 && i < w - 1 && j >= 0 && j < h - 1) - mono[p + w + 1] += (o * 1 / 16); + if (i >= 0 && i < w - 1 && j >= 0 && j < h) mono[p + 1] += o * 7 / 16; + if (i >= 1 && i < w && j >= 0 && j < h - 1) mono[p + w - 1] += o * 3 / 16; + if (i >= 0 && i < w && j >= 0 && j < h - 1) mono[p + w] += o * 5 / 16; + if (i >= 0 && i < w - 1 && j >= 0 && j < h - 1) mono[p + w + 1] += o * 1 / 16; ++p; } } return mono; } -exports.monoSteinberg = monoSteinberg; function monoHalftone(mono, w, h) { const spot = 4; - const spot_h = spot / 2 + 1; - const spot_d = spot * 2; - const spot_s = spot * spot; + const spot_h = 4 / 2 + 1; + const spot_s = 4 * 4; let i, j, x, y, o = 0.0; - for (j = 0; j < h - spot; j += spot) { - for (i = 0; i < w - spot; i += spot) { - for (x = 0; x < spot; ++x) - for (y = 0; y < spot; ++y) - o += mono[(j + y) * w + i + x]; + for(j = 0; j < h - 4; j += spot){ + for(i = 0; i < w - 4; i += spot){ + for(x = 0; x < 4; ++x)for(y = 0; y < 4; ++y)o += mono[(j + y) * w + i + x]; o = (1 - o / spot_s / 0xff) * spot; - for (x = 0; x < spot; ++x) - for (y = 0; y < spot; ++y) { - mono[(j + y) * w + i + x] = Math.abs(x - spot_h) >= o || Math.abs(y - spot_h) >= o ? 0xff : 0x00; - // mono[(j + y) * w + i + x] = Math.abs(x - spot_h) + Math.abs(y - spot_h) >= o ? 0xff : 0x00; - } + for(x = 0; x < 4; ++x)for(y = 0; y < 4; ++y){ + mono[(j + y) * w + i + x] = Math.abs(x - spot_h) >= o || Math.abs(y - spot_h) >= o ? 0xff : 0x00; + } } - for (; i < w; ++i) - mono[j * w + i] = 0xff; + for(; i < w; ++i)mono[j * w + i] = 0xff; } - for (; j < h; ++j) - for (i = 0; i < w; ++i) - mono[j * w + i] = 0xff; + for(; j < h; ++j)for(i = 0; i < w; ++i)mono[j * w + i] = 0xff; return mono; } -exports.monoHalftone = monoHalftone; function monoToPbm(data) { - let length = (data.length / 8) | 0; - let result = new Uint8ClampedArray(length); - for (let i = 0, p = 0; i < data.length; ++p) { + const length = data.length / 8 | 0; + const result = new Uint8ClampedArray(length); + for(let i = 0, p = 0; i < data.length; ++p){ result[p] = 0; - for (let d = 0; d < 8; ++i, ++d) - result[p] |= data[i] & (0b10000000 >> d); + for(let d = 0; d < 8; ++i, ++d)result[p] |= data[i] & 0b10000000 >> d; result[p] ^= 0b11111111; } return result; } -exports.monoToPbm = monoToPbm; -/** Note: takes & gives `Uint32Array` */ function rotateRgba(before, w, h) { - /** - * w h - * o------+ +---o - * h | | | | w - * +------+ | | after - * before +---+ - */ - let after = new Uint32Array(before.length); - for (let j = 0; j < h; j++) { - for (let i = 0; i < w; i++) { + const after = new Uint32Array(before.length); + for(let j = 0; j < h; j++){ + for(let i = 0; i < w; i++){ after[j * w + i] = before[(w - i - 1) * h + j]; } } return after; } -exports.rotateRgba = rotateRgba; +// export { monoGrayscale as monoGrayscale }; +// export { monoToRgba as monoToRgba }; +// export { monoDirect as monoDirect }; +// export { monoSteinberg as monoSteinberg }; +// export { monoHalftone as monoHalftone }; +// export { monoToPbm as monoToPbm }; +// export { rotateRgba as rotateRgba };