Skip to content

Instantly share code, notes, and snippets.

@Pokechu22
Created September 7, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pokechu22/ddfd50a683098280cbbc to your computer and use it in GitHub Desktop.
Save Pokechu22/ddfd50a683098280cbbc to your computer and use it in GitHub Desktop.
GBC-Filters an image
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FilterImage
{
/// <summary>
/// GBC-Filters an image.
/// </summary>
class Program
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Chose file to process";
ofd.ShowDialog();
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Chose save file name";
sfd.ShowDialog();
Bitmap bmp = new Bitmap(ofd.FileName);
Bitmap processed = new Bitmap(bmp.Width, bmp.Height);
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
processed.SetPixel(x, y, FilterWithGBC(bmp.GetPixel(x, y)));
}
Console.WriteLine("Col " + x);
}
processed.Save(sfd.FileName);
Console.WriteLine("Done");
Console.ReadKey(true);
}
/// <summary>
/// Filters the color.
///
/// Based off of lines 78-110 of ColorControlor.pas.
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static Color FilterWithGBC(Color color)
{
byte[] intensity = new byte[0x20] {
0x00,0x10,0x20,0x30,0x40,0x50,0x5e,0x6c,0x7a,0x88,0x94,0xa0,0xae,0xb7,0xbf,0xc6,
0xce,0xd3,0xd9,0xdf,0xe3,0xe7,0xeb,0xef,0xf3,0xf6,0xf9,0xfb,0xfd,0xfe,0xff,0xff
};
byte[,] influence = new byte[3, 3] { { 16, 4, 4 }, { 8, 16, 8 }, { 0, 8, 16 } };
byte[,] m = new byte[3, 3];
byte i, j;
byte[] rgb = new byte[3];
Color c;
rgb[0] = (byte)(color.R >> 3);
rgb[1] = (byte)(color.G >> 3);
rgb[2] = (byte)(color.B >> 3);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
m[i, j] = (byte)((intensity[rgb[i]] * influence[i, j]) >> 5);
}
}
for (i = 0; i < 3; i++)
{
if (m[0, i] > m[1, i]) { j = m[0, i]; m[0, i] = m[1, i]; m[1, i] = j; }
if (m[1, i] > m[2, i]) { j = m[1, i]; m[1, i] = m[2, i]; m[2, i] = j; };
if (m[0, i] > m[1, i]) { j = m[0, i]; m[0, i] = m[1, i]; m[1, i] = j; };
rgb[i] = (byte)((((m[0, i] + m[1, i] * 2 + m[2, i] * 4) * 5) >> 4) + 32);
}
c = Color.FromArgb(rgb[0], rgb[1], rgb[2]);
return c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment