-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbimage.js
357 lines (311 loc) · 7.89 KB
/
xbimage.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
// $Id: xbimage.js,v 1.6 2020/04/19 19:52:31 rswindell Exp $
// Utility module for creating and displaying XBin image files.
load('sbbsdefs.js');
var image_lib = load({}, 'xbimage_lib.js');
var bmplib = load({}, 'bmp_lib.js');
var cga = load({}, 'cga_defs.js');
var vga = load({}, 'vga_defs.js');
var sauce_lib = load({}, 'sauce_lib.js');
function convert_from_bmp(filename, charheight, fg_color, bg_color, palette, invert, blink_first, title, author, group)
{
if(!charheight)
charheight = 16;
if(fg_color === undefined)
fg_color = cga.LIGHTGRAY;
if(bg_color === undefined)
bg_color = cga.BLACK;
var file = new File(filename);
if(!file.open('rb')) {
alert('Error opening ' + file.name);
return false;
}
var bmp = bmplib.read(file, invert);
if(!bmp)
return false;
if(bmp.infoheader.biWidth%8 != 0) {
alert(format("Bitmap width (%u) is not evenly-divisible by 8", bmp.infoheader.biWidth));
return false;
}
// Add SAUCE
var sauce = {
title: title ? title : filename
,author: author
,group: group
,comment: []
};
var glyph = image_lib.glyphs_from_bmp(bmp, 8, charheight);
printf("Converting %s (%ux%u) ", filename, bmp.infoheader.biWidth, bmp.infoheader.biHeight);
filename = filename.slice(0, -file_getext(filename).length) + '.' + charheight + ".xb" ;
printf("to %u char-height XBin: %s\n", charheight, filename);
var width = Math.ceil(bmp.infoheader.biWidth / 8);
var height = Math.ceil(bmp.infoheader.biHeight / charheight)
if(!image_lib.create(filename, glyph, charheight
, width, height
, fg_color, bg_color
, blink_first
, sauce
)) {
alert("Failed");
return false;
}
printf("%s (%ux%u) created successfully\r\n", filename, width, height);
return true;
}
function show(filename, xpos, ypos, fg_color, bg_color, palette, delay, cleanup)
{
if(console.term_supports()&(USER_ANSI|USER_NO_EXASCII|USER_UTF8|USER_ICE_COLOR)
!= USER_ANSI)
return false;
var cterm = load({}, 'cterm_lib.js');
if(cterm.supports_fonts() === false)
return false;
if(!file_getext(filename))
filename += '.' + cterm.charheight(console.screen_rows) + '.xb';
var file = new File(filename);
if(!file.open('rb')) {
alert('Error opening ' + file.name);
return false;
}
var image = image_lib.xbin.read(file);
file.close();
if(!image)
return false;
// print(lfexpand(JSON.stringify(image, null, 4)));
// console.getkey();
if(palette)
image.palette = palette;
var result = cterm.xbin_draw(image, xpos, ypos, fg_color, bg_color, delay);
if(cleanup)
cterm.xbin_cleanup(image);
if(typeof result == 'string')
alert(result);
return result;
}
function color_value(palette, val)
{
if(cga[val] != undefined)
return palette[cga[val]];
var rgb = val.split(',');
rgb[0] = parseInt(rgb[0]);
rgb[1] = parseInt(rgb[1]);
rgb[2] = parseInt(rgb[2]);
return rgb;
}
function color_index(val)
{
if(val === undefined)
return val;
if(typeof val == "string" && cga[val.toUpperCase()] != undefined)
return cga[val.toUpperCase()];
return parseInt(val, 10);
}
function demo(filename, delay)
{
var cterm = load({}, 'cterm_lib.js');
if(cterm.supports_fonts() === false) {
log(LOG_WARNING, "No CTerm/Font support detected");
return false;
}
var ini = new File(filename);
if(!ini.open('r')) {
alert(filename);
return false;
}
var global = ini.iniGetObject();
var obj = ini.iniGetAllObjects();
ini.close();
console.line_counter = 0;
console.clear();
if(global.name) {
print("\1n\1hDemonstration beginning: \1y" + global.name + "\1w (Hit '\1cQ\1w' to abort)");
var key = console.inkey(K_UPPER, delay);
console.line_counter = 0;
if(key == 'Q')
return true;
}
if(global.delay === undefined)
global.delay = delay;
for(var i in obj) {
var palette = vga.color_palette;
if(obj[i].delay === undefined)
obj[i].delay = global.delay;
if(obj[i].xpos === undefined)
obj[i].xpos = global.xpos;
if(obj[i].ypos === undefined)
obj[i].ypos = global.ypos;
for(var c in cga.colors) {
if(obj[i][cga.colors[c].toLowerCase()] !== undefined)
palette[c] = color_value(palette, obj[i][cga.colors[c].toLowerCase()].toUpperCase());
}
if(show(obj[i].name, obj[i].xpos, obj[i].ypos
,color_index(obj[i].fg_color)
,color_index(obj[i].bg_color)
,palette
,obj[i].delay) != true)
break;
if(console.aborted)
break;
}
cterm.xbin_cleanup();
cterm.reset_palette();
if(global.name) {
print("\1n\1hDemonstration concluded: \1y" + global.name);
console.inkey(0, delay);
console.line_counter = 0;
}
}
function info(filename, verbose)
{
var file = new File(filename);
if(!file.open('rb')) {
alert('Error opening ' + file.name);
return false;
}
printf(format("%-64s : ", file.name));
var image = image_lib.xbin.read(file);
file.close();
printf("%4u x %-4u Flags(%x) Fonts(%u) Char(%u)\r\n"
,image.width, image.height, image.flags, image.font.length, image.charheight);
if(verbose) {
image.bin = "[" + image.bin.length + "]";
for(var i in image.font)
image.font[i] = "[" + image.font[i].length + "]";
print(lfexpand(JSON.stringify(image, null, 4)));
}
return true;
}
function modify(filename, flags)
{
var file = new File(filename);
if(!file.open('r+b')) {
alert('Error opening ' + file.name);
return false;
}
printf(format("%-64s : ", file.name));
var image = image_lib.xbin.read(file);
printf("%4u x %-4u Flags(%x) Fonts(%u) Char(%u)\r\n"
,image.width, image.height, image.flags, image.font ? image.font.length : 0, image.charheight);
if(flags !== undefined)
image.flags = flags;
file.truncate();
image_lib.xbin.write(file, image);
return true;
}
function main()
{
var optval={};
var cmds=[];
var files = [];
var charheights = [];
var fg_color;
var bg_color;
var delay;
var cycle;
var blink_first = true;
var invert = false;
var verbose = false;
var xpos;
var ypos;
var title;
var author;
var group;
var palette;
var flags;
for(var i in argv) {
var arg = argv[i];
var val = ""
var eq = arg.indexOf('=');
if(eq > 0) {
val = arg.substring(eq+1);
arg = arg.substring(0,eq);
}
optval[arg] = val;
if(arg.charAt(0) == '-' && cga[arg.substring(1).toUpperCase()] != undefined) {
if(palette === undefined)
palette = vga.color_palette
palette[cga[arg.substring(1).toUpperCase()]] = color_value(palette, val.toUpperCase());
continue;
}
switch(arg) {
case "-fg":
fg_color = color_index(val);
break;
case "-bg":
bg_color = color_index(val);
break;
case "-delay":
delay = parseInt(val, 10);
break;
case "-flags":
flags = parseInt(val);
break;
case "-cycle":
cycle = true;
break;
case "-invert":
invert = true;
break;
case "-char":
charheights.push(parseInt(val, 10));
break;
case "-title":
title = val;
break;
case "-author":
author = val;
break;
case "-group":
group = val;
break;
case "-normal":
blink_first = false;
break;
case "-v":
verbose = true;
break;
case "create":
case "show":
case "demo":
case "info":
case "modify":
cmds.push(arg);
break;
default:
if(file_exists(arg))
files.push(arg);
else
alert("File does not exist: " + arg);
break;
}
}
if(!charheights.length)
charheights = [16, 14, 8];
for(var ci in cmds) {
var cmd = cmds[ci].toLowerCase();
switch(cmd) {
case "create":
for(var ch in charheights)
if(!convert_from_bmp(files[0], charheights[ch], fg_color, bg_color, palette, invert
, blink_first,title, author, group))
break;
break;
case "show":
show(optval[cmd] ? optval[cmd] : files[0], xpos, ypos, fg_color, bg_color, palette, delay, /* cleanup: */true);
load('fonts.js', 'default');
break;
case "demo":
demo(optval[cmd] ? optval[cmd]: files[0], delay);
load('fonts.js', 'default');
break;
case "info":
for(var i in files)
info(files[i], verbose);
break;
case "modify":
for(var i in files)
modify(files[i], flags);
break;
}
}
}
main();