Skip to content

Commit 28bfaf5

Browse files
author
Michael Bikovitsky
committed
utility complete (for now)
1 parent cab0689 commit 28bfaf5

File tree

4 files changed

+192
-31
lines changed

4 files changed

+192
-31
lines changed

BrainTools/BrainTools.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<OutputType>Exe</OutputType>
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>Brainfuck</RootNamespace>
12-
<AssemblyName>Brainfuck</AssemblyName>
12+
<AssemblyName>bftools</AssemblyName>
1313
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1414
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
1515
<FileAlignment>512</FileAlignment>

BrainTools/Brainloller.cs

+27-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55

66
using System.Drawing;
7+
using System.Drawing.Imaging;
78

89
namespace Brainfuck
910
{
@@ -188,15 +189,37 @@ public static string Decode(Bitmap bmp)
188189
return code;
189190
}
190191

191-
public static Bitmap Reduce(Bitmap bmp, int pxWidth, int pxHeight)
192+
public static Bitmap Reduce(Bitmap bmp, int pxDimension)
192193
{
193-
Bitmap newBmp = new Bitmap(bmp.Width / pxWidth, bmp.Height / pxHeight);
194+
Bitmap newBmp = new Bitmap(bmp.Width / pxDimension, bmp.Height / pxDimension);
194195

195-
for (int row = 0; row < bmp.Height; row += pxHeight)
196-
for (int col = 0; col < bmp.Width; col += pxWidth)
196+
for (int row = 0; row < bmp.Height; row += pxDimension)
197+
for (int col = 0; col < bmp.Width; col += pxDimension)
197198
newBmp.SetPixel(col / 10, row / 10, bmp.GetPixel(col, row));
198199

199200
return newBmp;
200201
}
202+
203+
public static Bitmap Enlarge(Bitmap bmp, int pxDimension)
204+
{
205+
Bitmap newBmp = new Bitmap(bmp.Width * pxDimension, bmp.Height * pxDimension);
206+
Graphics g = Graphics.FromImage(newBmp);
207+
208+
for (int row = 0; row < bmp.Height; row++)
209+
{
210+
for (int col = 0; col < bmp.Width; col++)
211+
{
212+
Rectangle rect = new Rectangle(col * pxDimension, row * pxDimension, pxDimension, pxDimension);
213+
Pen pen = new Pen(bmp.GetPixel(col, row));
214+
SolidBrush brush = new SolidBrush(bmp.GetPixel(col, row));
215+
g.DrawRectangle(pen, rect);
216+
g.FillRectangle(brush, rect);
217+
}
218+
}
219+
220+
g.Dispose();
221+
222+
return newBmp;
223+
}
201224
}
202225
}

BrainTools/Program.cs

+129-12
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,140 @@ static class Program
1212
{
1313
static void Main(string[] args)
1414
{
15-
string code = ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.";
16-
string code2 = "++++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++<<<<<<<<<<<<<-]>>>>>>>>--------.>>---.>>--------..>---------.<<<<<<<<<--------.>>>----.>>>>-----.>++.++.<-.----.<.>>>.<<<<<<<<<+.";
15+
//string code = ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.";
16+
//string code2 = "++++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++<<<<<<<<<<<<<-]>>>>>>>>--------.>>---.>>--------..>---------.<<<<<<<<<--------.>>>----.>>>>-----.>++.++.<-.----.<.>>>.<<<<<<<<<+.";
1717

18-
//Bitmap bmp = new Bitmap("E:\\Desktop\\Untitled.png");
19-
//Bitmap newBmp = Braincopter.Encode(bmp, code);
20-
//string newCode = Braincopter.Decode(newBmp);
18+
if (args.Length == 0)
19+
{
20+
PrintHelp();
21+
return;
22+
}
2123

22-
Bitmap bmp = Brainloller.Encode(code2, 22, Color.Firebrick);
23-
bmp.Save("E:\\Desktop\\brain.png");
24+
switch (args[0])
25+
{
26+
case "encode":
27+
if (args.Length != 5)
28+
{
29+
Console.WriteLine("Wrong number of parameters.");
30+
return;
31+
}
2432

25-
//Bitmap bmp = new Bitmap("E:\\Desktop\\brain.png");
26-
//string code3 = Brainloller.Decode(bmp);
27-
//Brainfuck.Run(code3);
33+
if (args[1] == "brainloller")
34+
{
35+
StreamReader reader = new StreamReader(args[2]);
36+
string code = reader.ReadToEnd();
37+
reader.Close();
38+
Bitmap newBmp = Brainloller.Encode(code, int.Parse(args[3]), Color.Firebrick);
39+
newBmp.Save(args[4]);
40+
}
41+
else if (args[1] == "braincopter")
42+
{
43+
StreamReader reader = new StreamReader(args[2]);
44+
string code = reader.ReadToEnd();
45+
reader.Close();
46+
Bitmap original = new Bitmap(args[3]);
47+
Bitmap newBmp = Braincopter.Encode(original, code);
48+
newBmp.Save(args[4]);
49+
}
50+
else
51+
{
52+
Console.WriteLine("Unrecognized language.");
53+
}
54+
break;
55+
case "decode":
56+
if (args[1] == "brainloller")
57+
{
58+
Bitmap img = new Bitmap(args[2]);
59+
string code = Brainloller.Decode(img);
60+
StreamWriter writer = new StreamWriter(args[3]);
61+
writer.WriteLine(code);
62+
writer.Close();
63+
}
64+
else if (args[1] == "braincopter")
65+
{
66+
Bitmap img = new Bitmap(args[2]);
67+
string code = Braincopter.Decode(img);
68+
StreamWriter writer = new StreamWriter(args[3]);
69+
writer.WriteLine(code);
70+
writer.Close();
71+
}
72+
else
73+
{
74+
Console.WriteLine("Unrecognized language.");
75+
}
76+
break;
77+
case "reduce":
78+
if (args.Length != 4)
79+
{
80+
Console.WriteLine("Wrong number of parameters.");
81+
return;
82+
}
2883

29-
//Brainfuck.Run(newCode);
84+
Bitmap bmp = new Bitmap(args[1]);
85+
Bitmap save = Brainloller.Reduce(bmp, int.Parse(args[2]));
86+
save.Save(args[3]);
87+
break;
88+
case "enlarge":
89+
if (args.Length != 4)
90+
{
91+
Console.WriteLine("Wrong number of parameters.");
92+
return;
93+
}
3094

31-
Console.ReadLine();
95+
Bitmap src = new Bitmap(args[1]);
96+
Bitmap New = Brainloller.Enlarge(src, int.Parse(args[2]));
97+
New.Save(args[3]);
98+
break;
99+
case "run":
100+
if (args.Length != 2)
101+
{
102+
Console.WriteLine("Wrong number of parameters.");
103+
return;
104+
}
105+
106+
StreamReader read = new StreamReader(args[1]);
107+
Brainfuck.Run(read.ReadToEnd());
108+
read.Close();
109+
break;
110+
case "help":
111+
default:
112+
PrintHelp();
113+
break;
114+
}
115+
}
116+
117+
static void PrintHelp()
118+
{
119+
Console.WriteLine("Usage: bftools <operation> <language> <params>");
120+
Console.WriteLine();
121+
Console.WriteLine("Intro");
122+
Console.WriteLine("======");
123+
Console.WriteLine("This utility SAVES images in PNG format, so watch your extensions.");
124+
Console.WriteLine("This utility READS images in all formats supported by the .NET framework.");
125+
Console.WriteLine();
126+
Console.WriteLine("Encoding");
127+
Console.WriteLine("=========");
128+
Console.WriteLine("bftools encode brainloller <input file> <image width> <output image>");
129+
Console.WriteLine("bftools encode braincopter <input file> <original image> <output image>");
130+
Console.WriteLine();
131+
Console.WriteLine("Decoding");
132+
Console.WriteLine("=========");
133+
Console.WriteLine("bftools decode brainloller <input image> <output file>");
134+
Console.WriteLine("bftools decode braincopter <input image> <output file>");
135+
Console.WriteLine();
136+
Console.WriteLine("Running");
137+
Console.WriteLine("========");
138+
Console.WriteLine("bftools run <input file>");
139+
Console.WriteLine();
140+
Console.WriteLine("Image operations");
141+
Console.WriteLine("=================");
142+
Console.WriteLine("bftools enlarge <input image> <factor> <output image>");
143+
Console.WriteLine("bftools reduce <input image> <factor> <output image>");
144+
Console.WriteLine();
145+
Console.WriteLine("Getting help");
146+
Console.WriteLine("=============");
147+
Console.WriteLine("bftools help");
148+
Console.WriteLine();
32149
}
33150
}
34151
}

README.md

+35-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,42 @@ BrainTools
33

44
What is it?
55
------------
6-
An utility for working with Brainfuck and related languages. Can encode and decode programs in various
6+
An utility for working with Brainfuck and related languages. It can encode and decode programs in various
77
formats.
88

99
What works?
1010
------------
11-
Currently, not much =] You can experiment with the code yourself to see what's what.
12-
Basically, you can run Brainfuck programs.
13-
Also, you can decode Brainloller images into Brainfuck code (and then run it).
14-
__UPDATE:__ Now you can encode Brainfuck programs into images using Braincopter (and then decode them back).
15-
__UPDATE #2:__ Now there is a Brainloller encoder.
16-
17-
What next?
18-
-----------
19-
1. Finish writing the Braincopter encoder and decoder.
20-
2. Write Brainfuck and Brainloller encoders.
21-
3. Make the utility actually work.
22-
4. ?????
23-
5. PROFIT!!!!!!~
11+
EVERYTHING! You can run Brainfuck programs and encode/decode Brainfuck code using Brainloller
12+
and Braincopter. Plus, given that Brainloller produces very small images, the utility features
13+
an image resizer.
14+
15+
Usage
16+
------
17+
bftools \<operation\> \<language\> \<params\>
18+
19+
This utility __saves__ images in PNG format, so watch your extensions.
20+
This utility __reads__ images in all formats supported by the .NET framework.
21+
22+
### Encoding
23+
bftools encode brainloller \<input file\> \<image width\> \<output image\> <br>
24+
bftools encode braincopter \<input file\> \<original image\> \<output image\>
25+
26+
### Decoding
27+
bftools decode brainloller \<input image\> \<output file\> <br>
28+
bftools decode braincopter \<input image\> \<output file\>
29+
30+
### Running
31+
bftools run \<input file\>
32+
33+
### Image operations
34+
bftools enlarge \<input image\> \<factor\> \<output image\> <br>
35+
bftools reduce \<input image\> \<factor\> \<output image\>
36+
37+
### Getting help
38+
bftools help
39+
40+
Further reading
41+
----------------
42+
- [Brainfuck](http://esolangs.org/wiki/Brainfuck)
43+
- [Brainloller](http://esolangs.org/wiki/Brainloller)
44+
- [Braincopter](http://esolangs.org/wiki/Braincopter)

0 commit comments

Comments
 (0)