-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathrgba.ts
More file actions
198 lines (171 loc) · 8.15 KB
/
Copy pathrgba.ts
File metadata and controls
198 lines (171 loc) · 8.15 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// file: rgba.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 rgbaHandler implements FormatHandler {
public name: string = "rgba";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
#canvas?: HTMLCanvasElement;
#ctx?: CanvasRenderingContext2D;
async init () {
this.supportedFormats = [
CommonFormats.PNG.supported("png", true, true, true),
{
name: "Raw red, green, and blue samples",
format: "rgb",
extension: "rgb",
mime: "image/x-rgb",
from: true,
to: true,
internal: "rgb",
category: Category.IMAGE,
lossless: false
},
{
name: "Raw red, green, blue, and alpha samples",
format: "rgba",
extension: "rgba",
mime: "image/x-rgba",
from: true,
to: true,
internal: "rgba",
category: Category.IMAGE,
lossless: true
},
];
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.");
}
for (const file of inputFiles) {
let new_file_bytes = new Uint8Array(file.bytes);
if (inputFormat.mime === CommonFormats.PNG.mime) {
if (outputFormat.internal === "rgba") {
// 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);
});
this.#canvas.width = image.width;
this.#canvas.height = image.height;
this.#ctx.drawImage(image, 0, 0);
const pixels = this.#ctx.getImageData(0, 0, this.#canvas.width, this.#canvas.height);
new_file_bytes = new Uint8Array(pixels.data);
}
else if (outputFormat.internal === "rgb") {
throw new TypeError("This handler doesn't need to convert png to rgb, let ImageMagik do that.");
}
else {
throw new TypeError(`Unsupported conversion path: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
}
else if (inputFormat.internal === "rgb") {
if (new_file_bytes.length % 3 !== 0) {
throw new RangeError("Invalid RGB file size; not a whole number of samples.");
}
if (outputFormat.internal === "rgba") {
// Fill in with 255 in alpha channel
let writer_array = new Uint8Array(new_file_bytes.length + Math.floor(new_file_bytes.length / 3));
let writer_counter = 0;
for (let i = 0; i < new_file_bytes.length; i++) {
if (i % 3 === 2) {
writer_array.set(new Uint8Array([new_file_bytes[i]]),writer_counter);
writer_counter += 1;
writer_array.set(new Uint8Array([0xFF]),writer_counter);
writer_counter += 1;
}
else {
writer_array.set(new Uint8Array([new_file_bytes[i]]),writer_counter);
writer_counter += 1;
}
}
new_file_bytes = new Uint8Array(writer_array);
}
else if (outputFormat.mime === CommonFormats.PNG.mime) {
throw new TypeError("This handler doesn't need to convert rgb to png, let ImageMagik do that.");
}
else {
throw new TypeError(`Unsupported conversion path: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
}
else if (inputFormat.internal === "rgba") {
if (new_file_bytes.length % 4 !== 0) {
throw new RangeError("Invalid RGBA file size; not a whole number of samples.");
}
if (outputFormat.internal === "rgb") {
// Remove every fourth, byte! Every fourth, byte!
let writer_array = new Uint8Array(new_file_bytes.length - Math.floor(new_file_bytes.length / 4));
let writer_counter = 0;
for (let i = 0; i < new_file_bytes.length; i++) {
if (i % 4 !== 3) {
writer_array.set(new Uint8Array([new_file_bytes[i]]),writer_counter);
writer_counter += 1;
}
}
new_file_bytes = new Uint8Array(writer_array);
}
else if (outputFormat.mime === CommonFormats.PNG.mime) {
// Determine image dimensions: smallest number x such that x^2 is >= total samples
const total_samples = new_file_bytes.length/4;
let image_sw = 0;
while (true) {
if (image_sw * image_sw >= total_samples) {
break;
}
image_sw += 1;
}
// Set canvas dimensions to this value
this.#canvas.width = image_sw;
this.#canvas.height = image_sw;
// Determine color per-pixel and write that value to the buffer
let color = [0,0,0];
const rgba: number[] = []
for (let i = 0; i < this.#canvas.width * this.#canvas.height; i++) {
try {
color = [new_file_bytes[0+i*4],new_file_bytes[1+i*4],new_file_bytes[2+i*4]];
rgba.push(...color, new_file_bytes[3+i*4]);
}
catch {
color = [0,0,0];
rgba.push(...color, 255);
}
}
// 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);
});
}
else {
throw new TypeError(`Unsupported conversion path: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
}
else {
throw new TypeError(`Unsupported input format: ${inputFormat.internal}`);
}
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension,
bytes: new_file_bytes
})
}
return outputFiles;
}
}
export default rgbaHandler;