Skip to content

Instantly share code, notes, and snippets.

@willeccles
Last active August 12, 2019 23:07
Show Gist options
  • Save willeccles/240ff4850ea560aa617043eafd66e9f6 to your computer and use it in GitHub Desktop.
Save willeccles/240ff4850ea560aa617043eafd66e9f6 to your computer and use it in GitHub Desktop.
A simple program to convert a Windows bitmap to an Adafruit GFX library bitmap array for monochrome OLED displays.
/*
* bmp2arduino.cs
* Will Eccles 2019
*
* Takes a Windows bitmap file and outputs a C header containing a bitmap array
* to be used with the Adafruit GFX library. Only works for monochrome files at this time.
* All pixels that are not black or transparent are assumed to be white.
*
* Run with no arguments to see usage.
*
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace bmp2arduino
{
class bmp2arduino
{
static void E(int e) { Environment.Exit(e); }
static void Usage()
{
Console.WriteLine("Usage: bmp2arduino <input.bmp> <output.h> <image_name_c>\n");
Console.WriteLine(" input.bmp:\tA BMP file to convert.\n");
Console.WriteLine(" output.h:\tA C header file to output to.\n");
Console.WriteLine(" image_name_c:\t A valid C identifier for the image.");
}
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please supply a bitmap file.");
Usage();
E(1);
}
if (args.Length == 1)
{
Console.WriteLine("Please supply an output file name.");
Usage();
E(1);
}
if (args.Length == 2)
{
Console.WriteLine("Please supply a C identifier for the image.");
Usage();
E(1);
}
string inbmp = args[0];
string outh = args[1];
string ident = args[2];
if (!Regex.IsMatch(ident, @"[_A-Za-z][_A-Za-z0-9]*"))
{
Console.WriteLine($"\"{ident}\" is not a valid C identifier.");
E(1);
}
Bitmap b = null;
try
{
b = new Bitmap(inbmp);
}
catch
{
Console.WriteLine("Error opening bitmap! Invalid path or permissions.");
E(1);
}
int hpadding = (8 - (b.Width % 8)) % 8;
string imgarr = "";
Color pixel;
int bcounter = 0;
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < b.Width; x++)
{
if (bcounter == 0)
{
imgarr += 'B';
}
pixel = b.GetPixel(x, y);
if ((pixel.R == 0 && pixel.G == 0 && pixel.B == 0) || pixel.A == 0)
{
imgarr += '0';
}
else
{
imgarr += '1';
}
if (bcounter == 7)
{
imgarr += ",";
}
bcounter++;
bcounter %= 8; // reset bit counter if necessary
}
bcounter = 0;
if (hpadding > 0)
{
imgarr += new string('0', hpadding);
imgarr += ",";
}
imgarr += '\n';
}
string ident_upper = ident.ToUpper();
string finaloutput =
"/* Auto-generated with bmp2arduino: https://gist.github.com/WillEccles/240ff4850ea560aa617043eafd66e9f6 */\n\n" +
$"#ifndef {ident_upper}_HEADER\n" +
$"#define {ident_upper}_HEADER\n\n" +
"#include <Arduino.h>\n\n" +
$"// Width of {ident}\n" +
$"#define {ident_upper}_WIDTH {b.Width + hpadding}\n" +
$"// Height of {ident}\n" +
$"#define {ident_upper}_HEIGHT {b.Height}\n" +
$"// Horizontal padding for {ident}\n" +
$"#define {ident_upper}_PADDING {hpadding}\n\n" +
$"const uint8_t {ident}[] PROGMEM = {{\n" +
imgarr +
"};\n\n" +
$"#endif /* {ident_upper}_HEADER */\n";
try
{
System.IO.File.WriteAllText(outh, finaloutput);
}
catch
{
Console.WriteLine("Error writing to output file! Invalid path or permissions.");
E(1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment