Created
August 13, 2018 15:44
-
-
Save OMGasm/31ff8b2c7796c1621ee4f0d1bc98a709 to your computer and use it in GitHub Desktop.
bullshit image converter to 0bpp because i was bored
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Drawing; | |
using System.IO; | |
namespace whythefuckwouldyouusethis | |
{ | |
public class BSImage | |
{ | |
public static Image Decode(byte[] bs) | |
{ | |
if (bs?.Length != 12) return null; | |
int w = BitConverter.ToInt32(bs, 0); | |
int h = BitConverter.ToInt32(bs, 4); | |
Color c = Color.FromArgb(bs[8], bs[9], bs[10], bs[11]); | |
Bitmap img = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format1bppIndexed); | |
img.Palette.Entries[0] = c; | |
return img; | |
} | |
public static byte[] Encode(Image im) | |
{ | |
int w = im.Width; | |
int h = im.Height; | |
Color c = im.Palette.Entries[0]; | |
return BitConverter.GetBytes(w) | |
.Concat(BitConverter.GetBytes(h)) | |
.Concat(new byte[] { c.R, c.G, c.B, c.A }) | |
.ToArray(); | |
} | |
public static void Save(string loc, Image im) | |
{ | |
File.WriteAllBytes(loc, Encode(im)); | |
} | |
public static Image Load(string loc) | |
{ | |
return Decode(File.ReadAllBytes(loc)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(i haven't even tested it)