[WIP] better bit-reverse on backend

rename all_js to all-scripts
minimal changes
This commit is contained in:
NaitLee 2023-05-25 19:01:21 +08:00
parent 1b9ff251ad
commit 5238b66eb6
8 changed files with 67 additions and 90 deletions

View File

@ -1,5 +1,6 @@
{ {
"js/ts.implicitProjectConfig.strictNullChecks": false, "js/ts.implicitProjectConfig.strictNullChecks": false,
"js/ts.implicitProjectConfig.checkJs": false, "js/ts.implicitProjectConfig.checkJs": false,
"js/ts.implicitProjectConfig.experimentalDecorators": true "js/ts.implicitProjectConfig.experimentalDecorators": true,
"deno.enable": true
} }

View File

@ -41,16 +41,9 @@ def crc8(data):
def reverse_bits(i: int): def reverse_bits(i: int):
'Reverse the bits of this byte (as `int`)' 'Reverse the bits of this byte (as `int`)'
return ( i = ((i & 0b10101010) >> 1) | ((i & 0b01010101) << 1)
(i & 0b10000000) >> 7 | i = ((i & 0b11001100) >> 2) | ((i & 0b00110011) << 2)
(i & 0b01000000) >> 5 | return ((i & 0b11110000) >> 4) | ((i & 0b00001111) << 4)
(i & 0b00100000) >> 3 |
(i & 0b00010000) >> 1 |
(i & 0b00001000) << 1 |
(i & 0b00000100) << 3 |
(i & 0b00000010) << 5 |
(i & 0b00000001) << 7
)
def int_to_bytes(i: int, big_endian=False): def int_to_bytes(i: int, big_endian=False):
''' Turn `int` into `bytearray`, that have ''' Turn `int` into `bytearray`, that have

View File

@ -85,7 +85,7 @@ class PrinterServerHandler(BaseHTTPRequestHandler):
_settings_blacklist = ( _settings_blacklist = (
'printer', 'is_android' 'printer', 'is_android'
) )
all_js: list = [] all_script: list = []
printer: PrinterDriver = PrinterDriver() printer: PrinterDriver = PrinterDriver()
@ -120,7 +120,7 @@ class PrinterServerHandler(BaseHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', mime(path)) self.send_header('Content-Type', mime(path))
self.end_headers() 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) self.wfile.write(data)
return return
path = 'www' + path path = 'www' + path
@ -321,10 +321,10 @@ class PrinterServer(HTTPServer):
if self.handler is None: if self.handler is None:
self.handler = self.handler_class(request, client_address, self) self.handler = self.handler_class(request, client_address, self)
self.handler.load_config() 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'): for path in file.read().split('\n'):
if path != '': if path != '':
self.handler.all_js.append(os.path.join('www', path)) self.handler.all_script.append(os.path.join('www', path))
return return
self.handler.__init__(request, client_address, self) self.handler.__init__(request, client_address, self)

5
wasm/image.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
type i32 = number;
type u8 = number;
type f32 = number;
type f64 = number;
type bool = boolean;

View File

@ -1,6 +1,8 @@
/// <reference path="./image.d.ts" />
export function monoGrayscale(rgba: Uint32Array, brightness: i32, alpha_as_white: bool): Uint8ClampedArray { 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; 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) { for (let i: i32 = 0; i < mono.length; ++i) {
n = rgba[i]; n = rgba[i];
@ -22,7 +24,7 @@ export function monoGrayscale(rgba: Uint32Array, brightness: i32, alpha_as_white
/** Note: returns a `Uint32Array` */ /** Note: returns a `Uint32Array` */
export function monoToRgba(mono: Uint8ClampedArray): 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) { for (let i: i32 = 0; i < mono.length; ++i) {
// little endian // little endian
rgba[i] = 0xff000000 | (mono[i] << 16) | (mono[i] << 8) | mono[i]; rgba[i] = 0xff000000 | (mono[i] << 16) | (mono[i] << 8) | mono[i];
@ -30,7 +32,7 @@ export function monoToRgba(mono: Uint8ClampedArray): Uint32Array {
return rgba; 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) { for (let i: i32 = 0; i < mono.length; ++i) {
mono[i] = mono[i] > 0x80 ? 0xff : 0x00; 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 { export function monoHalftone(mono: Uint8ClampedArray, w: i32, h: i32): Uint8ClampedArray {
const spot: i32 = 4; const spot: i32 = 4;
const spot_h: i32 = spot / 2 + 1; 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; const spot_s: i32 = spot * spot;
let i: i32, j: i32, x: i32, y: i32, o: f64 = 0.0; let i: i32, j: i32, x: i32, y: i32, o: f64 = 0.0;
for (j = 0; j < h - spot; j += spot) { 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 { export function monoToPbm(data: Uint8ClampedArray): Uint8ClampedArray {
let length: i32 = (data.length / 8) | 0; const length: i32 = (data.length / 8) | 0;
let result = new Uint8ClampedArray(length); const result = new Uint8ClampedArray(length);
for (let i: i32 = 0, p: i32 = 0; i < data.length; ++p) { for (let i: i32 = 0, p: i32 = 0; i < data.length; ++p) {
result[p] = 0; result[p] = 0;
for (let d: u8 = 0; d < 8; ++i, ++d) for (let d: u8 = 0; d < 8; ++i, ++d)
@ -105,7 +107,7 @@ export function rotateRgba(before: Uint32Array, w: i32, h: i32): Uint32Array {
* +------+ | | after * +------+ | | after
* before +---+ * before +---+
*/ */
let after = new Uint32Array(before.length); const after = new Uint32Array(before.length);
for (let j: i32 = 0; j < h; j++) { for (let j: i32 = 0; j < h; j++) {
for (let i: i32 = 0; i < w; i++) { for (let i: i32 = 0; i < w; i++) {
after[j * w + i] = before[(w - i - 1) * h + j]; after[j * w + i] = before[(w - i - 1) * h + j];

View File

@ -1,2 +1,2 @@
#!/bin/sh #!/bin/sh
npx tsc $@ --allowJs --outFile main.comp.js $(cat all_js.txt) npx tsc $@ --allowJs --outFile main.comp.js $(cat all-scripts.txt)

View File

@ -1,21 +1,20 @@
"use strict"; // deno-fmt-ignore-file
Object.defineProperty(exports, "__esModule", { value: true }); // deno-lint-ignore-file
exports.rotateRgba = exports.monoToPbm = exports.monoHalftone = exports.monoSteinberg = exports.monoDirect = exports.monoToRgba = exports.monoGrayscale = void 0; // This code was bundled using `deno bundle` and it's not recommended to edit it manually
function monoGrayscale(rgba, brightness, alpha_as_white) { 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; 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]; 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; a = (n >> 24 & 0xff) / 0xff;
if (a < 1 && alpha_as_white) { if (a < 1 && alpha_as_white) {
a = 1 - a; a = 1 - a;
r += (0xff - r) * a; r += (0xff - r) * a;
g += (0xff - g) * a; g += (0xff - g) * a;
b += (0xff - b) * a; b += (0xff - b) * a;
} } else {
else {
r *= a; r *= a;
g *= a; g *= a;
b *= a; b *= a;
@ -26,24 +25,19 @@ function monoGrayscale(rgba, brightness, alpha_as_white) {
} }
return mono; return mono;
} }
exports.monoGrayscale = monoGrayscale;
/** Note: returns a `Uint32Array` */
function monoToRgba(mono) { function monoToRgba(mono) {
let rgba = new Uint32Array(mono.length); const rgba = new Uint32Array(mono.length);
for(let i = 0; i < mono.length; ++i){ for(let i = 0; i < mono.length; ++i){
// little endian rgba[i] = 0xff000000 | mono[i] << 16 | mono[i] << 8 | mono[i];
rgba[i] = 0xff000000 | (mono[i] << 16) | (mono[i] << 8) | mono[i];
} }
return rgba; return rgba;
} }
exports.monoToRgba = monoToRgba; function monoDirect(mono, _w, _h) {
function monoDirect(mono, w, h) {
for(let i = 0; i < mono.length; ++i){ for(let i = 0; i < mono.length; ++i){
mono[i] = mono[i] > 0x80 ? 0xff : 0x00; mono[i] = mono[i] > 0x80 ? 0xff : 0x00;
} }
return mono; return mono;
} }
exports.monoDirect = monoDirect;
function monoSteinberg(mono, w, h) { function monoSteinberg(mono, w, h) {
let p = 0, m, n, o; let p = 0, m, n, o;
for(let j = 0; j < h; ++j){ for(let j = 0; j < h; ++j){
@ -52,69 +46,45 @@ function monoSteinberg(mono, w, h) {
n = mono[p] > 0x80 ? 0xff : 0x00; n = mono[p] > 0x80 ? 0xff : 0x00;
o = m - n; o = m - n;
mono[p] = n; mono[p] = n;
if (i >= 0 && i < w - 1 && j >= 0 && j < h) if (i >= 0 && i < w - 1 && j >= 0 && j < h) mono[p + 1] += o * 7 / 16;
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 >= 1 && i < w && j >= 0 && j < h - 1) if (i >= 0 && i < w && j >= 0 && j < h - 1) mono[p + w] += o * 5 / 16;
mono[p + w - 1] += (o * 3 / 16); if (i >= 0 && i < w - 1 && j >= 0 && j < h - 1) mono[p + w + 1] += o * 1 / 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; ++p;
} }
} }
return mono; return mono;
} }
exports.monoSteinberg = monoSteinberg;
function monoHalftone(mono, w, h) { function monoHalftone(mono, w, h) {
const spot = 4; const spot = 4;
const spot_h = spot / 2 + 1; const spot_h = 4 / 2 + 1;
const spot_d = spot * 2; const spot_s = 4 * 4;
const spot_s = spot * spot;
let i, j, x, y, o = 0.0; let i, j, x, y, o = 0.0;
for (j = 0; j < h - spot; j += spot) { for(j = 0; j < h - 4; j += spot){
for (i = 0; i < w - spot; i += spot) { for(i = 0; i < w - 4; i += spot){
for (x = 0; x < spot; ++x) for(x = 0; x < 4; ++x)for(y = 0; y < 4; ++y)o += mono[(j + y) * w + i + x];
for (y = 0; y < spot; ++y)
o += mono[(j + y) * w + i + x];
o = (1 - o / spot_s / 0xff) * spot; o = (1 - o / spot_s / 0xff) * spot;
for (x = 0; x < spot; ++x) for(x = 0; x < 4; ++x)for(y = 0; y < 4; ++y){
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) >= 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 (; i < w; ++i) for(; i < w; ++i)mono[j * w + i] = 0xff;
mono[j * w + i] = 0xff;
} }
for (; j < h; ++j) for(; j < h; ++j)for(i = 0; i < w; ++i)mono[j * w + i] = 0xff;
for (i = 0; i < w; ++i)
mono[j * w + i] = 0xff;
return mono; return mono;
} }
exports.monoHalftone = monoHalftone;
function monoToPbm(data) { function monoToPbm(data) {
let length = (data.length / 8) | 0; const length = data.length / 8 | 0;
let result = new Uint8ClampedArray(length); const result = new Uint8ClampedArray(length);
for(let i = 0, p = 0; i < data.length; ++p){ for(let i = 0, p = 0; i < data.length; ++p){
result[p] = 0; result[p] = 0;
for (let d = 0; d < 8; ++i, ++d) for(let d = 0; d < 8; ++i, ++d)result[p] |= data[i] & 0b10000000 >> d;
result[p] |= data[i] & (0b10000000 >> d);
result[p] ^= 0b11111111; result[p] ^= 0b11111111;
} }
return result; return result;
} }
exports.monoToPbm = monoToPbm;
/** Note: takes & gives `Uint32Array` */
function rotateRgba(before, w, h) { function rotateRgba(before, w, h) {
/** const after = new Uint32Array(before.length);
* w h
* o------+ +---o
* h | | | | w
* +------+ | | after
* before +---+
*/
let after = new Uint32Array(before.length);
for(let j = 0; j < h; j++){ for(let j = 0; j < h; j++){
for(let i = 0; i < w; i++){ for(let i = 0; i < w; i++){
after[j * w + i] = before[(w - i - 1) * h + j]; after[j * w + i] = before[(w - i - 1) * h + j];
@ -122,4 +92,10 @@ function rotateRgba(before, w, h) {
} }
return after; 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 };