forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image2cs.js
480 lines (477 loc) · 12.8 KB
/
image2cs.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Copyright (c) 2016-2017 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { FILE, TOOL } from "tool";
import Bitmap from "commodetto/Bitmap";
import ColorCellOut from "commodetto/ColorCellOut";
import Convert from "commodetto/Convert";
import JPEG from "commodetto/ReadJPEG";
import PNG from "commodetto/ReadPNG";
export default class extends TOOL {
constructor(argv) {
super(argv);
this.directory = false;
this.format = Bitmap.RGB565LE;
this.quality = undefined;
this.rotation = 0;
this.inputPath = null;
this.outputPath = null;
this.windows = this.currentPlatform == "win";
this.slash = this.windows ? "\\" : "/";
var argc = argv.length;
for (var argi = 1; argi < argc; argi++) {
var option = argv[argi], name, path;
switch (option) {
case "-o":
argi++;
if (argi >= argc)
throw new Error("-o: no directory!");
name = argv[argi];
if (this.outputPath)
throw new Error("-o '" + name + "': too many directories!");
path = this.resolveDirectoryPath(name);
if (!path)
throw new Error("-o '" + name + "': directory not found!");
this.outputPath = path;
break;
case "-q":
argi++;
if (argi >= argc)
throw new Error("-q: no quality!");
name = parseInt(argv[argi]);
if ((name < 0) || (100 < name))
throw new Error("-q: " + name + ": invalid quality!");
this.quality = name;
break;
case "-r":
argi++;
if (argi >= argc)
throw new Error("-r: no rotation!");
name = parseInt(argv[argi]);
if ((name != 0) && (name != 90) && (name != 180) && (name != 270))
throw new Error("-r: " + name + ": invalid rotation!");
this.rotation = name;
break;
default:
name = argv[argi];
if (this.inputPath)
throw new Error("'" + name + "': too many files!");
path = this.resolveFilePath(name);
if (!path) {
path = this.resolveDirectoryPath(name);
if (!path)
throw new Error("'" + name + "': file not found!");
this.directory = true;
}
this.inputPath = path;
break;
}
}
if (!this.inputPath) {
throw new Error("no file!");
}
if (!this.outputPath)
this.outputPath = this.splitPath(this.inputPath).directory;
}
compress(buffer, width, height) {
let writer = new ColorCellOut;
if (this.quality !== undefined)
writer.begin(0, 0, width, height, { quality:this.quality / 100 });
else
writer.begin(0, 0, width, height);
let from = 0;
for (let y = 0; y < height; y += 4) {
let to = from + (8 * width);
writer.send(buffer.slice(from, to));
from = to;
}
writer.end();
return writer.bitmap.buffer;
}
copy0(block, pixels, width, height) {
var buffer = new Uint16Array(block.buffer);
let v = 0;
while (v < block.height) {
let u = 0;
while (u < block.width) {
let x = block.x + u;
let y = block.y + v;
let src = ((v * block.width) + u);
let dst = ((y * width) + x);
pixels[dst] = buffer[src];
u++;
}
v++;
}
}
copy90(block, pixels, width, height) {
var buffer = new Uint16Array(block.buffer);
let v = 0;
while (v < block.height) {
let u = 0;
while (u < block.width) {
let x = height - 1 - block.y - v;
let y = block.x + u;
let src = ((v * block.width) + u);
let dst = ((y * height) + x);
pixels[dst] = buffer[src];
u++;
}
v++;
}
}
copy180(block, pixels, width, height) {
var buffer = new Uint16Array(block.buffer);
let v = 0;
while (v < block.height) {
let u = 0;
while (u < block.width) {
let x = width - 1 - block.x - u;
let y = height - 1 - block.y - v;
let src = ((v * block.width) + u);
let dst = ((y * width) + x);
pixels[dst] = buffer[src];
u++;
}
v++;
}
}
copy270(block, pixels, width, height) {
var buffer = new Uint16Array(block.buffer);
let v = 0;
while (v < block.height) {
let u = 0;
while (u < block.width) {
let x = block.y + v;
let y = width - 1 - block.x - u;
let src = ((v * block.width) + u);
let dst = ((y * height) + x);
pixels[dst] = buffer[src];
u++;
}
v++;
}
}
pad(dimension) {
let multiple = 4;
let overflow = dimension & (multiple - 1);
if (overflow)
dimension += multiple - overflow;
return dimension;
}
readGIF(buffer) @ "Tool_readGIF";
readJPEG(path, frames) {
var data = this.readFileBuffer(path);
var jpeg = new JPEG(data, { pixelFormat:Bitmap.RGB565LE });
var jpegWidth = jpeg.width;
var jpegHeight = jpeg.height;
if (this.quality == 0) {
this.format = Bitmap.JPEG;
frames.push(data);
frames.width = jpegWidth;
frames.height = jpegHeight;
return;
}
var padWidth = this.pad(jpegWidth);
var padHeight = this.pad(jpegHeight);
var copy, width, height;
switch (this.rotation) {
case 0:
copy = this.copy0;
width = padWidth;
height = padHeight;
break;
case 90:
copy = this.copy90;
width = padHeight;
height = padWidth;
break;
case 180:
copy = this.copy180;
width = padWidth;
height = padHeight;
break;
case 270:
copy = this.copy270;
width = padHeight;
height = padWidth;
break;
}
var pixels = new Uint16Array(width * height);
for (;;) {
var block = jpeg.read();
if (!block)
break;
copy.call(this, block, pixels, padWidth, padHeight);
}
frames.push(this.compress(pixels.buffer, width, height));
frames.width = width;
frames.height = height;
}
readPNG(path, frames) {
var data = this.readFileBuffer(path);
var png = new PNG(data);
let pngChannels = png.channels;
let pngDepth = png.depth;
let pngWidth = png.width;
let pngHeight = png.height;
if (((pngChannels != 3) && (pngChannels != 4)) || (pngDepth != 8))
throw new Error("'" + pngPath + "': invalid PNG format!");
if (this.quality == 0) {
this.format = Bitmap.PNG;
frames.push(data);
frames.width = pngWidth;
frames.height = pngHeight;
return;
}
let padWidth = this.pad(pngWidth);
let padHeight = this.pad(pngHeight);
let bufferRGBA32 = new Uint8Array(padWidth * padHeight * 4);
let offset = 0;
let y = 0;
while (y < pngHeight) {
let pngLine = png.read();
let pngOffset = 0;
let x = 0;
while (x < pngWidth) {
bufferRGBA32[offset++] = pngLine[pngOffset++];
bufferRGBA32[offset++] = pngLine[pngOffset++];
bufferRGBA32[offset++] = pngLine[pngOffset++];
bufferRGBA32[offset++] = (pngChannels == 4) ? pngLine[pngOffset++] : 255;
x++;
}
while (x < padWidth) {
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 255;
x++;
}
y++;
}
while (y < padHeight) {
let x = 0;
while (x < padWidth) {
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 0;
bufferRGBA32[offset++] = 255;
x++;
}
y++;
}
let bufferRGB565LE = new Uint16Array(padWidth * padHeight);
var colorConvert = new Convert(Bitmap.RGBA32, Bitmap.RGB565LE);
colorConvert.process(bufferRGBA32.buffer, bufferRGB565LE.buffer);
var pixels, width, height;
switch (this.rotation) {
case 0:
pixels = bufferRGB565LE.buffer;
width = padWidth;
height = padHeight;
break;
case 90:
pixels = this.rotate90(bufferRGB565LE.buffer, padWidth, padHeight);
width = padHeight;
height = padWidth;
break;
case 180:
pixels = this.rotate180(bufferRGB565LE.buffer, padWidth, padHeight);
width = padWidth;
height = padHeight;
break;
case 270:
pixels = this.rotate270(bufferRGB565LE.buffer, padWidth, padHeight);
width = padHeight;
height = padWidth;
break;
}
frames.push(this.compress(pixels, width, height));
frames.width = width;
frames.height = height;
}
rotate90(pixels, width, height) {
let buffer = new Uint16Array(pixels);
let result = new Uint16Array(height * width);
let v = 0;
while (v < height) {
let u = 0;
while (u < width) {
let x = height - 1 - v;
let y = u;
let src = ((v * width) + u);
let dst = ((y * height) + x);
result[dst] = buffer[src];
u++;
}
v++;
}
return result.buffer;
}
rotate180(pixels, width, height) {
let buffer = new Uint16Array(pixels);
let result = new Uint16Array(height * width);
let v = 0;
while (v < height) {
let u = 0;
while (u < width) {
let x = width - 1 - u;
let y = height - 1 - v;
let src = ((v * width) + u);
let dst = ((y * width) + x);
result[dst] = buffer[src];
u++;
}
v++;
}
return result.buffer;
}
rotate270(pixels, width, height) {
let buffer = new Uint16Array(pixels);
let result = new Uint16Array(height * width);
let v = 0;
while (v < height) {
let u = 0;
while (u < width) {
let x = v;
let y = width - 1 - u;
let src = ((v * width) + u);
let dst = ((y * height) + x);
result[dst] = buffer[src];
u++;
}
v++;
}
return result.buffer;
}
run() {
var fps_numerator;
var fps_denominator;
var frames = new Array;
if (this.directory) {
var names = this.enumerateDirectory(this.inputPath);
names.sort();
var c = names.length;
for (var i = 0; i < c; i++) {
var path = this.inputPath + this.slash + names[i];
var parts = this.splitPath(path);
if (parts.extension == ".png") {
this.readPNG(path, frames);
}
else if ((parts.extension == ".jpg") || (parts.extension == ".jpeg")) {
this.readJPEG(path, frames);
}
}
var parts = this.splitPath(this.inputPath);
var a = parts.extension.match(/\.([0-9]+)fps/);
fps_numerator = 1;
fps_denominator = (a && (a.length == 2) && (a[1] !== undefined)) ? parseInt(a[1]) : 30;
}
else {
var parts = this.splitPath(this.inputPath);
if (parts.extension == ".gif") {
var array = this.readGIF(this.readFileBuffer(this.inputPath));
var gifWidth = array.width;
var gifHeight = array.height;
var gifDuration = array.reduce((sum, item) => sum + item.delay, 0);
var width, height;
switch (this.rotation) {
case 0:
case 180:
width = gifWidth;
height = gifHeight;
break;
case 90:
case 270:
width = gifHeight;
height = gifWidth;
break;
}
var c = array.length;
for (var i = 0; i < c; i++) {
var pixels;
switch (this.rotation) {
case 0:
pixels = array[i];
break;
case 90:
pixels = this.rotate90(array[i], gifWidth, gifHeight);
break;
case 180:
pixels = this.rotate180(array[i], gifWidth, gifHeight);
break;
case 270:
pixels = this.rotate270(array[i], gifWidth, gifHeight);
break;
}
frames.push(this.compress(pixels, width, height));
}
frames.width = width;
frames.height = height;
fps_numerator = gifDuration / c;
fps_denominator = 1000;
}
else if (parts.extension == ".png") {
this.readPNG(this.inputPath, frames);
fps_numerator = 1;
fps_denominator = 1;
}
else if ((parts.extension == ".jpg") || (parts.extension == ".jpeg")) {
this.readJPEG(this.inputPath, frames);
fps_numerator = 1;
fps_denominator = 1;
}
else
throw new Error("'" + this.inputPath + "': invalid file!");
}
var frameCount = frames.length;
var width = frames.width;
var height = frames.height;
parts.directory = this.outputPath;
parts.extension = ".cs";
if (this.quality !== undefined)
parts.extension += this.quality;
let output = new FILE(this.joinPath(parts), "wb");
output.writeByte('c'.charCodeAt(0));
output.writeByte('s'.charCodeAt(0));
output.writeByte(this.format);
output.writeByte(0);
output.writeByte(width & 0xFF);
output.writeByte(width >> 8);
output.writeByte(height & 0xFF);
output.writeByte(height >> 8);
output.writeByte(frameCount & 0xFF);
output.writeByte(frameCount >> 8);
output.writeByte(fps_numerator & 0xFF);
output.writeByte(fps_numerator >> 8);
output.writeByte(fps_denominator & 0xFF);
output.writeByte(fps_denominator >> 8);
for (var frameIndex = 0; frameIndex < frameCount; frameIndex++) {
var buffer = frames[frameIndex];
var frameSize = buffer.byteLength;
output.writeByte(frameSize & 255);
output.writeByte(frameSize >> 8);
output.writeBuffer(buffer);
}
// 0 size frame at the end
output.writeByte(0);
output.writeByte(0);
output.close();
}
}