-
Notifications
You must be signed in to change notification settings - Fork 5
/
image_2_c.html
60 lines (57 loc) · 2.28 KB
/
image_2_c.html
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
<html>
<head>
<script>
function byte2hex$( byte )
{
return ("0" + byte.toString(16)).toUpperCase().substr(-2)
}
function OnLoad()
{
var image = document.getElementById( "Apple2eFont7x8" );
var canvas = document.createElement( "canvas" );
var context = canvas.getContext( "2d" );
canvas.width = image.width;
canvas.height = image.height;
context.drawImage( image, 0, 0 );
var CW = 7, CH = 8; // Cell Width Height
var pixel, rgba, lines = "", th = image.height/CH, tw = image.width/CW;
for( var ty = 0; ty < th; ++ty )
{
for( var tx = 0; tx < tw; ++tx )
{
var text = "";
for( var y = 0; y < CH; ++y )
{
var hex = 0, mask = 0x1;
for( var x = 0; x < CW; ++x, mask <<= 1 )
{
pixel = context.getImageData( tx*CW+x, ty*CH+y, 1, 1 );
rgba = pixel.data;
hex += rgba[0] ? mask : 0; // assume R=G=B
}
text += "0x" + byte2hex$( hex );
if ((ty == (th-1)) && (tx == (tw-1)) && (y == (CH-1)))
text += " ";
else
text += ", ";
}
var c = (16*ty)+tx, d = String.fromCharCode( c );
if (c < 32) d = "^" + String.fromCharCode( c + 0x40 );
if (c==127) d = "";
// Extra space on end because of C's line continuation '\' for 28 and 92.
lines += " " + text + "// 0x" + byte2hex$(c) + " " + d + " \n";
}
}
lines = "const char FONT[] = {\n" + lines + "};\n";
console.log( lines );
var pre = document.getElementById( "hexdump" );
pre.innerHTML = lines;
}
</script>
</head>
<body onload="OnLoad()">
<img id="Apple2eFont7x8" src="Apple2eFont7x8.png">
<hr>
<pre id="hexdump"></pre>
</body>
</html>