Repository navigation
Expand file tree
/
Copy pathimagefile.ts
More file actions
61 lines (54 loc) · 1.92 KB
/
Copy pathimagefile.ts
File metadata and controls
61 lines (54 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { File, knownFolders } from "@nativescript/core";
import { GrayImage } from "./image";
declare const interop: any;
const UPNG = require("upng-js");
export function loadPngAsGrayImage(path: string): GrayImage {
const file = resolveFile(path);
const raw = file.readSync();
const pngBuffer = toArrayBuffer(raw);
const decoded = UPNG.decode(pngBuffer);
const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[];
if (!frames.length) {
throw new Error(`PNG contains no frames: ${path}`);
}
const rgba = new Uint8Array(frames[0]!);
const image = new GrayImage(decoded.width, decoded.height, 0);
for (let y = 0; y < decoded.height; y++) {
for (let x = 0; x < decoded.width; x++) {
const offset = (y * decoded.width + x) * 4;
const r = rgba[offset] ?? 0;
const g = rgba[offset + 1] ?? 0;
const b = rgba[offset + 2] ?? 0;
const a = (rgba[offset + 3] ?? 0) / 255;
const gray = Math.round((0.2126 * r + 0.7152 * g + 0.0722 * b) * a);
image.pixels[y * decoded.width + x] = gray;
}
}
return image;
}
function resolveFile(path: string): File {
if (!path) {
throw new Error("PNG path is required");
}
return path.startsWith("/") ? File.fromPath(path) : knownFolders.currentApp().getFile(path);
}
function toArrayBuffer(raw: unknown): ArrayBuffer {
if (raw instanceof ArrayBuffer) {
return raw.slice(0);
}
if (raw instanceof Uint8Array) {
return raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.byteLength) as ArrayBuffer;
}
if (global.isAndroid && raw && typeof (raw as { length?: unknown }).length === "number") {
const bytes = raw as ArrayLike<number>;
const out = new Uint8Array(bytes.length);
for (let i = 0; i < bytes.length; i++) {
out[i] = bytes[i]! & 0xff;
}
return out.buffer;
}
if (global.isIOS && raw) {
return interop.bufferFromData(raw);
}
throw new Error(`Unsupported PNG byte source: ${String(raw)}`);
}