Created
January 29, 2018 12:53
-
-
Save AwaisKing/7c9d44f7240bfaea9c36b388d07fd93c to your computer and use it in GitHub Desktop.
File to color pixels and back!
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.Text; | |
using System.IO; | |
using System.Drawing; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Drawing.Imaging; | |
class Pixelate { | |
static void Main(string[] args) { | |
/*string s = "acxbca", str = ""; | |
int start = 0, end = s.Length - 1; | |
float mid = ((float)start + end) / 2.0f; | |
while (start <= end) { | |
if (s[start] == s[end] && mid >= (start + end) / 2) { | |
str += s[start]; | |
} | |
start++; | |
end--; | |
Console.Write(start + " -- " + end + "\n"); | |
} | |
Console.WriteLine(str);*/ | |
/*using (Bitmap bmp = new Bitmap(@"C:\Users\AWAiS\Desktop\Untitled.png")) { | |
int height = bmp.Height, width = bmp.Width; | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
Color pixel = bmp.GetPixel(j, i); | |
if (pixel.A < 255) continue; | |
Console.WriteLine($"{i},{j}: {pixel.R.ToString("X2")},{pixel.G.ToString("X2")},{pixel.B.ToString("X2")}"); | |
} | |
Console.WriteLine(); | |
} | |
}*/ | |
/*using (FileStream file = File.Open(@"C:\Users\AWAiS\Desktop\Untitled.png", FileMode.Open)) { | |
int b, x = 0; | |
string str = ""; | |
while ((b = file.ReadByte()) >= 0) { | |
string s = b.ToString("X2"); | |
if (x < 3) { | |
x++; | |
str += s; | |
} else { | |
x = 0; | |
str += ","; | |
} | |
} | |
if (str.EndsWith(",")) | |
str = str.Substring(0, str.Length - 1); | |
string[] lel = str.Split(','); | |
if (lel[lel.Length - 1].Length == 2) { | |
lel[lel.Length - 1] = lel[lel.Length - 1] + "----"; | |
} else if (lel[lel.Length - 1].Length == 4) { | |
lel[lel.Length - 1] = lel[lel.Length - 1] + "--"; | |
} | |
foreach (var item in lel) { | |
Console.Write(item+","); | |
} | |
file.Close(); | |
}*/ | |
using (FileStream fs = new FileStream(@"C:\Users\AWAiS\Desktop\test\to_be_packed.png", FileMode.Open)) { | |
int hexIn, i = 0, x = 0; | |
long l = fs.Length; | |
StringBuilder str = new StringBuilder(); | |
while ((hexIn = fs.ReadByte()) != -1) { | |
if (i<2) { | |
str.Append(hexIn.ToString("X2")); | |
if (i < 1) str.Append(","); | |
} else { | |
i = 0; | |
str.Append("," + hexIn.ToString("X2")); | |
str.Append("|"); | |
continue; | |
} | |
i++; | |
} | |
// |09,E5 = 2 bytes | |
// |09, = 1 byte | |
// | = 0 bytes | |
if (str[str.Length - 1] == '|') str = str.Remove(str.Length - 1, 1); | |
if (str[str.Length - 6] == '|') str.Append(",XX"); | |
if (str[str.Length - 4] == '|') str.Append("XX,XX"); | |
int len = (int)(fs.Length / 3) + 1, y = 0; | |
Bitmap resultImage = new Bitmap(len, 3, PixelFormat.Format32bppArgb); | |
resultImage.MakeTransparent(Color.Transparent); | |
foreach (var chunk in str.ToString().Split('|')) { | |
string[] bits = chunk.Split(','); | |
string a = "00"; | |
string r = (bits[0] == "XX" ? "00" : bits[0]); | |
string g = (bits[1] == "XX" ? "00" : bits[1]); | |
string b = (bits[2] == "XX" ? "00" : bits[2]); | |
if (bits[1] == "XX") a = "90"; | |
if (bits[2] == "XX") a = "A0"; | |
if (bits[1] != "XX" && bits[2] != "XX") a = "FF"; | |
Color clr = ColorTranslator.FromHtml("#" + a + r + g + b); | |
resultImage.SetPixel(x, y, clr); | |
x++; | |
if (x >= len) { | |
y++; x = 0; | |
} | |
Console.WriteLine($"LEN: {len}, x: {x}, y: {y}"); | |
} | |
resultImage.Save(@"C:\Users\AWAiS\Desktop\test\packed.png"); | |
} | |
bitmapToFile(@"C:\Users\AWAiS\Desktop\test\packed.png", @"C:\Users\AWAiS\Desktop\test\test.png"); | |
} | |
public static void bitmapToFile(string bitmap, string outFile) { | |
using (Bitmap bmp = new Bitmap(bitmap)) { | |
int height = bmp.Height, width = bmp.Width; | |
StringBuilder str = new StringBuilder(); | |
for (int h = 0; h < height; h++) { | |
for (int w = 0; w < width; w++) { | |
Color pixel = bmp.GetPixel(w, h); | |
int bytesUsed = 3; | |
if (pixel.A == 0) continue; | |
else if (pixel.A >= 100 && pixel.A <= 150) bytesUsed = 1; | |
else if (pixel.A > 150 && pixel.A < 255) bytesUsed = 2; | |
switch (bytesUsed) { | |
case 1: | |
str.Append(pixel.R.ToString("X2")); break; | |
case 2: | |
str.Append(pixel.R.ToString("X2")); | |
str.Append(pixel.G.ToString("X2")); break; | |
case 3: | |
str.Append(pixel.R.ToString("X2")); | |
str.Append(pixel.G.ToString("X2")); | |
str.Append(pixel.B.ToString("X2")); break; | |
} | |
} | |
} | |
File.WriteAllBytes(outFile, StringToByteArray(str.ToString())); | |
} | |
} | |
public static byte[] StringToByteArray(string hex) { | |
return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment