-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgbp_gameboyprinter2bpp_raw.js
379 lines (316 loc) · 11.5 KB
/
gbp_gameboyprinter2bpp_raw.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
/*
Gameboy Printer Render via "Gameboy 2BPP Graphics Format". 2017-4-16
Original Tile Decoder From: http://www.huderlem.com/demos/gameboy2bpp.html By Marcus
Heavily adapted to work with gameboy camera and gameboy printer data By Brian Khuu
*/
// Tile Constants
TILE_PIXEL_WIDTH = 8;
TILE_PIXEL_HEIGHT = 8;
TILES_PER_LINE = 20; // Gameboy Printer Tile Constant
document.addEventListener('DOMContentLoaded', function ()
{
var button = document.getElementById("submit_button");
button.addEventListener("click", function ()
{
refresh();
}, false);
// Initial Render
refresh();
});
function refresh()
{
var data = document.getElementById("data_text");
data.classList.remove('error'); // Clear Error Indicator
document.getElementById('errormessage').innerText = '';
try {
var rawBytes = toByteArray(data.value);
// console.log(rawBytes);
var packets = parsePackets(rawBytes);
// console.log(packets);
var imageStream = getImageDataStream(packets);
// console.log(imageStream);
var decompressed = decompressDataStream(imageStream);
// console.log(decompressed);
var withDecodedCommands = decodePrintCommands(decompressed);
// console.log(withDecodedCommands);
var withHarmonizedPalette = harmonizePalettes(withDecodedCommands);
// console.log(withHarmonizedPalette);
var classic = transformToClassic(withHarmonizedPalette);
// console.log(classic);
var joined = classic.map((image) => {
image.unshift('{"command":"INIT"}');
image.push('{"command":"PRNT","margin_lower":3}');
return image.join('\n');
}).join('\n\n\n');
// console.log(joined);
render_gbp(joined);
} catch (error)
{
data.classList.add('error'); // Trigger error status
document.getElementById('errormessage').innerText = error.message;
}
}
function newCanvas()
{
var container = document.createElement('div');
var canvas = document.createElement('canvas');
container.className = 'image-container';
canvas.width = 480;
var downloadButtonJPG = null;
var downloadButton = document.createElement('button');
downloadButton.setAttribute('data-filetype', 'png');
downloadButton.addEventListener('click', download);
if (canvas.msToBlob) {
downloadButton.innerText = 'Download';
} else {
downloadButton.innerText = 'Download PNG';
downloadButtonJPG = document.createElement('button');
downloadButtonJPG.setAttribute('data-filetype', 'jpg');
downloadButtonJPG.addEventListener('click', download);
downloadButtonJPG.innerText = 'Download JPG';
}
document.getElementById('images').appendChild(container);
container.appendChild(canvas);
container.appendChild(downloadButton);
if (downloadButtonJPG) {
container.appendChild(downloadButtonJPG);
}
return canvas;
}
function renderImage(tiles)
{
var tile_height_count = Math.floor(tiles.length / TILES_PER_LINE);
var canvas = newCanvas();
// /* Determine size of each pixel in canvas */
var square_width = canvas.width / (TILE_PIXEL_WIDTH * TILES_PER_LINE);
var square_height = square_width;
// Resize height (Setting new canvas size will reset canvas)
canvas.width = square_width * TILE_PIXEL_WIDTH * TILES_PER_LINE;
canvas.height = square_height * TILE_PIXEL_HEIGHT * tile_height_count;
tiles.forEach(function (pixels, index)
{
// Gameboy Tile Offset
var tile_x_offset = index % TILES_PER_LINE;
var tile_y_offset = Math.floor(index / TILES_PER_LINE);
paint(canvas, pixels, square_width, square_height, tile_x_offset, tile_y_offset);
})
}
function render_gbp(rawBytes)
{
// clear all images
document.getElementById('images').innerHTML = '';
// rawBytes is a string of hex where each line represents a gameboy tile
var tiles_rawBytes_array = rawBytes.split(/\n/);
var images = [];
var currentImage = null;
tiles_rawBytes_array
.map(function (raw_line, line_number)
{
if ((raw_line.charAt(0) === '#'))
return null;
if ((raw_line.charAt(0) === '/'))
return null;
if ((raw_line.charAt(0) === '{'))
{
try
{
var command = JSON.parse(raw_line.slice(0).trim());
if (command.command === 'INIT')
{
return 'INIT'
}
else if (command.command === 'PRNT')
{
return command.margin_lower;
}
} catch (error)
{
throw new Error('Error while trying to parse JSON data block in line ' + (1 + line_number));
}
}
return (decode(raw_line));
})
.filter(Boolean)
.forEach(function (tile_element)
{
if ((tile_element === 'INIT' && currentImage))
{
// ignore init if image has not finished 'printing'
}
else if ((tile_element === 'INIT' && !currentImage))
{
currentImage = [];
}
else if ((typeof(tile_element) === 'number'))
{
// handle margin value
var margin_lower = tile_element;
// if margin is 3 split into new image 'finish printing'
if (margin_lower === 3)
{
images.push(currentImage);
currentImage = [];
}
// otherwise feed blank lines using lower margin value
else
{
var blank_tile = new Array(64).fill(4);
// send 40 blank tiles per margin feed
for (i=0;i<margin_lower*40;i++) {
currentImage.push(blank_tile);
}
}
}
else
{
try
{
currentImage.push(tile_element);
} catch (error)
{
throw new Error('No image start found. Maybe this line is missing')
}
}
})
images.forEach(renderImage);
}
// Gameboy tile decoder function from http://www.huderlem.com/demos/gameboy2bpp.html
function decode(rawBytes)
{
var bytes = rawBytes.replace(/[^0-9A-F]/ig, '');
if (bytes.length != 32) return false;
var byteArray = new Array(16);
for (var i = 0; i < byteArray.length; i++)
{
byteArray[i] = parseInt(bytes.substr(i * 2, 2), 16);
}
var pixels = new Array(TILE_PIXEL_WIDTH * TILE_PIXEL_HEIGHT);
for (var j = 0; j < TILE_PIXEL_HEIGHT; j++)
{
for (var i = 0; i < TILE_PIXEL_WIDTH; i++)
{
var hiBit = (byteArray[j * 2 + 1] >> (7 - i)) & 1;
var loBit = (byteArray[j * 2] >> (7 - i)) & 1;
pixels[j * TILE_PIXEL_WIDTH + i] = (hiBit << 1) | loBit;
}
}
return pixels;
}
// This paints the tile with a specified offset and pixel width
function paint(canvas, pixels, pixel_width, pixel_height, tile_x_offset, tile_y_offset)
{
var e = document.getElementById("palette");
var palette = e.options[e.selectedIndex].value;
switch (palette)
{
case "grayscale":
colors = new Array("#ffffff", "#aaaaaa", "#555555", "#000000", "#FFFFFF00");
break;
case "dmg":
colors = new Array("#9BBC0F", "#77A112", "#306230", "#0F380F", "#FFFFFF00");
break;
case "gameboypocket":
colors = new Array("#c4cfa1", "#8b956d", "#4d533c", "#1f1f1f", "#FFFFFF00");
break;
case "gameboycoloreuus":
colors = new Array("#ffffff", "#7bff30", "#0163c6", "#000000", "#FFFFFF00");
break;
case "gameboycolorjp":
colors = new Array("#ffffff", "#ffad63", "#833100", "#000000", "#FFFFFF00");
break;
case "bgb":
colors = new Array("#e0f8d0", "#88c070", "#346856", "#081820", "#FFFFFF00");
break;
case "grafixkidgray":
colors = new Array("#e0dbcd", "#a89f94", "#706b66", "#2b2b26", "#FFFFFF00");
break;
case "grafixkidgreen":
colors = new Array("#dbf4b4", "#abc396", "#7b9278", "#4c625a", "#FFFFFF00");
break;
case "blackzero":
colors = new Array("#7e8416", "#577b46", "#385d49", "#2e463d", "#FFFFFF00");
break;
default:
colors = new Array("#ffffff", "#aaaaaa", "#555555", "#000000", "#FFFFFF00");
}
tile_offset = tile_x_offset * tile_y_offset;
pixel_x_offset = TILE_PIXEL_WIDTH * tile_x_offset * pixel_width;
pixel_y_offset = TILE_PIXEL_HEIGHT * tile_y_offset * pixel_height;
var ctx = canvas.getContext("2d");
// pixels along the tile's x axis
for (var i = 0; i < TILE_PIXEL_WIDTH; i++)
{
for (var j = 0; j < TILE_PIXEL_HEIGHT; j++)
{
// pixels along the tile's y axis
// Pixel Color
ctx.fillStyle = colors[pixels[j * TILE_PIXEL_WIDTH + i]];
// Pixel Position (Needed to add +1 to pixel width and height to fill in a gap)
ctx.fillRect(
pixel_x_offset + i * pixel_width,
pixel_y_offset + j * pixel_height,
pixel_width + 1,
pixel_height + 1
);
}
}
}
function download(event)
{
var button = event.target;
var fileType = button.getAttribute('data-filetype');
var canvas = button.parentNode.querySelector('canvas');
var currentdate = new Date();
var filename = "Game Boy Photo "
+ currentdate.getFullYear()
+ addZero((currentdate.getMonth() + 1))
+ addZero(currentdate.getDate()) + " - "
+ addZero(currentdate.getHours()) + ""
+ addZero(currentdate.getMinutes()) + ""
+ addZero(currentdate.getSeconds());
if (canvas.msToBlob)
{
window.navigator.msSaveBlob(canvas.msToBlob(), filename + '.png');
return;
}
var image;
switch (fileType)
{
case 'png':
image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');
break;
case 'jpg':
// backup original canvas data
var ctx = canvas.getContext("2d")
var w = canvas.width;
var h = canvas.height;
var data;
data = ctx.getImageData(0, 0, w, h);
var compositeOperation = ctx.globalCompositeOperation;
ctx.globalCompositeOperation = "destination-over";
// draw white background on entire canvas
ctx.fillStyle = '#FFFFFFFF';
ctx.fillRect(0,0,w,h);
// get save image data
image = canvas.toDataURL('image/jpeg', 1).replace('image/jpeg', 'image/octet-stream');
// restore original canvas data
ctx.clearRect (0,0,w,h);
ctx.putImageData(data, 0,0);
ctx.globalCompositeOperation = compositeOperation;
break;
default:
break;
}
var download = document.getElementById("download");
download.setAttribute("href", image);
download.setAttribute("download", filename + '.' + fileType);
download.click();
}
function addZero(i)
{
if (i < 10)
{
i = "0" + i;
}
return i;
}