-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathota.ts
More file actions
173 lines (141 loc) · 6.54 KB
/
Copy pathota.ts
File metadata and controls
173 lines (141 loc) · 6.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// file: ota.ts
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { BadMagicError, EOFError, InitializationError } from "src/errors.ts";
class otaHandler implements FormatHandler {
public name: string = "ota";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
#canvas?: HTMLCanvasElement;
#ctx?: CanvasRenderingContext2D;
async init () {
this.supportedFormats = [
CommonFormats.PNG.supported("png", true, true, true),
{
name: "Over The Air bitmap",
format: "ota",
extension: "otb",
mime: "image/x-ota",
from: true,
to: true,
internal: "ota",
category: Category.IMAGE,
lossless: false,
},
];
this.#canvas = document.createElement("canvas");
this.#ctx = this.#canvas.getContext("2d") || undefined;
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
if (!this.#canvas || !this.#ctx) {
throw new InitializationError("Handler not initialized.");
}
if (inputFormat.internal === "ota" && outputFormat.mime === CommonFormats.PNG.mime) {
for (const file of inputFiles) {
let new_file_bytes = new Uint8Array(file.bytes);
// Read header to get image size
this.#canvas.width = new_file_bytes[1];
this.#canvas.height = new_file_bytes[2];
// Read each byte and write 8 pixels to screen per
const rgba: number[] = []
for (let i = 0; i < new_file_bytes.length - 4; i++) {
for (let bit = 7; bit > -1; bit--) {
// Convert to binary and look at the bits.
if (new_file_bytes[i+4] & (1 << bit)) {
rgba.push(0, 0, 0, 255);
}
else {
rgba.push(255, 255, 255, 255);
}
if (rgba.length >= (this.#canvas.width * this.#canvas.height*4)) {
break;
}
}
if (rgba.length >= (this.#canvas.width * this.#canvas.height*4)) {
break;
}
}
// Writes our results to the canvas
const image_data = new ImageData(new Uint8ClampedArray(rgba), this.#canvas.width, this.#canvas.height);
this.#ctx.putImageData(image_data, 0, 0);
new_file_bytes = await new Promise((resolve, reject) => {
this.#canvas!.toBlob((blob) => {
if (!blob) return reject("Canvas output failed");
blob.arrayBuffer().then(buf => resolve(new Uint8Array(buf)));
}, outputFormat.mime);
});
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension,
bytes: new_file_bytes
})
}
}
else if (inputFormat.mime === CommonFormats.PNG.mime && outputFormat.internal === "ota") {
for (const file of inputFiles) {
let writer_array: number[] = [];
// Some code copied from mcmap.ts
const blob = new Blob([file.bytes as BlobPart], { type: inputFormat.mime });
const image = new Image();
await new Promise((resolve, reject) => {
image.addEventListener("load", resolve);
image.addEventListener("error", reject);
image.src = URL.createObjectURL(blob);
});
if (image.naturalWidth > 255) {
this.#canvas.width = 255;
}
else {
this.#canvas.width = image.width;
}
if (image.naturalHeight > 255) {
this.#canvas.height = 255;
}
else {
this.#canvas.height = image.height;
}
this.#ctx.drawImage(image, 0, 0, this.#canvas.width, this.#canvas.height);
const pixels = this.#ctx.getImageData(0, 0, this.#canvas.width, this.#canvas.height);
console.log(pixels.data);
// Start writing our .otb file, first with the header
writer_array.push(0, this.#canvas.width, this.#canvas.height, 1);
let bits = [];
// Then iterate through image data
for (let i = 0; i < pixels.data.length; i = i + 4) {
// Determine the "perceived" lightness of a pixel by the human eye.
let luminance = pixels.data[i]*0.2126 + pixels.data[i+1]*0.7152 + pixels.data[i+2]*0.0722;
if (luminance > 0.5*255) {
bits.push("0");
}
else {
bits.push("1");
}
}
console.log(bits);
// Pad bits
while (bits.length % 8 !== 0) {
bits.push("0");
}
// Finally, use the bits to write to our file's bytes
for (let i = 0; i < bits.length; i = i + 8) {
let result: string = bits[i+0].concat(bits[i+1],bits[i+2],bits[i+3],bits[i+4],bits[i+5],bits[i+6],bits[i+7]);
writer_array.push( parseInt(result,2) );
}
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension,
bytes: new Uint8Array(writer_array)
})
}
}
else {
throw new TypeError(`Unsupported conversion path: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
return outputFiles;
}
}
export default otaHandler;