apple2_hgr_font_tutorial/image_2_c.html
2016-01-10 10:03:02 -08:00

61 lines
2.3 KiB
HTML

<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>