summaryrefslogtreecommitdiff
path: root/Form1.cs
blob: 980a5aaa4408d4957e1fb7b7fe82cb9e755eb6dd (plain)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace Rip81Font
{
    public partial class Form1 : Form
    {
        private byte[] rom;

        public Form1()
        {
            InitializeComponent();

            rom = File.ReadAllBytes("Data/zx81.bin");

            m_pictureBox.Image = new Bitmap(8, 8);
        }

        private void Rip(int code)
        {
            bool inv = false;

            if (code > 127)
            {
                inv = true;
                code -= 128;
            }

            if (code > 63)
            {
                using (Graphics g = Graphics.FromImage(m_pictureBox.Image))
                {
                    g.FillRectangle(Brushes.Red, 0, 0, 8, 8);
                }
            }
            else
            {
                int addr = 0x1e00 + code * 8;

                using (Graphics g = Graphics.FromImage(m_pictureBox.Image))
                {
                    for(int f = 0; f < 8; f++)
                    {
                        byte b = rom[addr+f];

                        for(int n = 0; n < 8; n++)
                        {
                            Brush p;

                            if ((b & (1 << n)) != 0)
                            {
                                if (inv)
                                {
                                    p = Brushes.White;
                                }
                                else
                                {
                                    p = Brushes.Black;
                                }
                            }
                            else
                            {
                                if (inv)
                                {
                                    p = Brushes.Black;
                                }
                                else
                                {
                                    p = Brushes.White;
                                }
                            }

                            g.FillRectangle(p, 7 - n, f, 1, 1);
                        }
                    }
                }
            }

            using (Graphics g = m_pictureBox.CreateGraphics())
            {
                g.DrawImageUnscaled(m_pictureBox.Image, 0, 0);
            }
        }

        private void OnCodeChanged(object sender, EventArgs e)
        {
            Rip(Convert.ToInt32(m_number.Value));
        }

        private void OnExportAll(object sender, EventArgs e)
        {
            Directory.CreateDirectory("Export");

            for(int f = 0; f < 256; f++)
            {
                Rip(f);

                string fname = String.Format("Export/{0}.png", f);

                m_pictureBox.Image.Save(fname, ImageFormat.Png);
            }
        }
    }
}