Skip to content

Commit

Permalink
add some more options to gif Creation
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Jan 2, 2024
1 parent 73092a9 commit c139e6d
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 370 deletions.
41 changes: 10 additions & 31 deletions Imaging/Cif.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,13 @@ public bool ChangeColor(int x, int y, Color color)
{
var id = CifProcessing.CalculateId(x, y, Width);

if (id > CheckSum)
{
return false;
}
if (id > CheckSum) return false;

foreach (var (key, value) in cifImage)
{
if (!value.Contains(id))
{
continue;
}
if (!value.Contains(id)) continue;

if (key == color)
{
return false;
}
if (key == color) return false;

cifImage[key].Remove(id);

Expand Down Expand Up @@ -121,22 +112,15 @@ public bool ChangeColor(int x, int y, Color color)
/// <returns>Success Status</returns>
public bool ChangeColor(Color oldColor, Color newColor)
{
if (!cifImage.ContainsKey(oldColor))
{
return false;
}
if (!cifImage.ContainsKey(oldColor)) return false;

var cache = cifImage[oldColor];
cifImage.Remove(oldColor);

if (cifImage.ContainsKey(newColor))
{
cifImage[newColor].AddRange(cache);
}
else
{
cifImage.Add(newColor, cache);
}

return true;
}
Expand All @@ -148,25 +132,20 @@ public bool ChangeColor(Color oldColor, Color newColor)
[return: MaybeNull]
public Image GetImage()
{
if (cifImage == null)
{
return null;
}
if (cifImage == null) return null;

var image = new Bitmap(Height, Width);
var dbm = DirectBitmap.GetInstance(image);

foreach (var (key, value) in cifImage)
foreach (var id in value)
{
foreach (var id in value)
{
var x = CifProcessing.IdToX(id, Width);
var y = CifProcessing.IdToY(id, Width);
dbm.SetPixel(x, y, key);
}
var x = CifProcessing.IdToX(id, Width);
var y = CifProcessing.IdToY(id, Width);
dbm.SetPixel(x, y, key);
}

return null;
}
}
}
}
61 changes: 13 additions & 48 deletions Imaging/CifProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,10 @@ internal static Bitmap CifToImage(List<List<string>> data)
{
//get image size
var check = int.TryParse(data[0][0], out var height);
if (!check)
{
return null;
}
if (!check) return null;

check = int.TryParse(data[0][1], out var length);
if (!check)
{
return null;
}
if (!check) return null;

//remove the Height, length data
data.RemoveAt(0);
Expand All @@ -86,10 +80,7 @@ internal static Bitmap CifToImage(List<List<string>> data)

check = int.TryParse(line[1], out var a);

if (!check)
{
continue;
}
if (!check) continue;

var converter = new ColorHsv(hex, a);

Expand All @@ -100,10 +91,7 @@ internal static Bitmap CifToImage(List<List<string>> data)
for (var i = 2; i < line.Count; i++)
{
check = int.TryParse(line[i], out var idMaster);
if (!check)
{
continue;
}
if (!check) continue;

var x = IdToX(idMaster, length);
var y = IdToY(idMaster, length);
Expand All @@ -125,16 +113,10 @@ internal static Bitmap CifToImageCompressed(List<List<string>> data)
//get image size
var check = int.TryParse(data[0][0], out var height);

if (!check)
{
return null;
}
if (!check) return null;

check = int.TryParse(data[0][1], out var length);
if (!check)
{
return null;
}
if (!check) return null;

//remove the Height, length data
data.RemoveAt(0);
Expand All @@ -149,10 +131,7 @@ internal static Bitmap CifToImageCompressed(List<List<string>> data)

check = int.TryParse(line[1], out var a);

if (!check)
{
continue;
}
if (!check) continue;

var converter = new ColorHsv(hex, a);

Expand All @@ -161,24 +140,17 @@ internal static Bitmap CifToImageCompressed(List<List<string>> data)

//get coordinates
for (var i = 2; i < line.Count; i++)
{
if (line[i].Contains("-"))
{
//split get start and end
var lst = line[i].Split(ImagingResources.CifSeparator).ToList();
check = int.TryParse(lst[0], out var start);

if (!check)
{
continue;
}
if (!check) continue;

check = int.TryParse(lst[1], out var end);

if (!check)
{
continue;
}
if (!check) continue;

//paint area
for (var j = start; j <= end; j++)
Expand All @@ -192,16 +164,12 @@ internal static Bitmap CifToImageCompressed(List<List<string>> data)
{
check = int.TryParse(line[i], out var idMaster);

if (!check)
{
continue;
}
if (!check) continue;

var x = IdToX(idMaster, length);
var y = IdToY(idMaster, length);
dbm.SetPixel(x, y, color);
}
}
}

return dbm.Bitmap;
Expand Down Expand Up @@ -281,10 +249,7 @@ internal static List<List<string>> GenerateCsvCompressed(int imageHeight, int im

var compressed = new List<int>();

if (sequence == null)
{
continue;
}
if (sequence == null) continue;

foreach (var (startS, endS) in sequence)
{
Expand Down Expand Up @@ -320,7 +285,7 @@ internal static List<List<string>> GenerateCsvCompressed(int imageHeight, int im
/// <returns>Id of Coordinate</returns>
internal static int CalculateId(int x, int y, int length)
{
return (y * length) + x;
return y * length + x;
}

/// <summary>
Expand All @@ -345,4 +310,4 @@ internal static int IdToY(int masterId, int length)
return masterId / length;
}
}
}
}
76 changes: 17 additions & 59 deletions Imaging/ColorHsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,11 @@ public ColorHsv(double h, double s, double v, int a)
/// <param name="a">The Hue value.</param>
public ColorHsv(int r, int g, int b, int a)
{
if (r == -1)
{
return;
}
if (r == -1) return;

if (g == -1)
{
return;
}
if (g == -1) return;

if (b == -1)
{
return;
}
if (b == -1) return;

ColorToHsv(r, g, b, a);
}
Expand Down Expand Up @@ -127,25 +118,13 @@ public ColorHsv(string hex, int a)
/// <returns>If equal or not</returns>
public bool Equals(ColorHsv other)
{
if (other == null)
{
return false;
}
if (other == null) return false;

if (R != other.R)
{
return false;
}
if (R != other.R) return false;

if (G != other.G)
{
return false;
}
if (G != other.G) return false;

if (B != other.B)
{
return false;
}
if (B != other.B) return false;

return A == other.A;
}
Expand All @@ -167,13 +146,10 @@ public void RgbFromHsv(double h, double s, double v, int a)

var degree = h * 180 / Math.PI;

if (degree is > 360 or < 0 || s is > 1 or < 0 || v is > 1 or < 0)
{
return;
}
if (degree is > 360 or < 0 || s is > 1 or < 0 || v is > 1 or < 0) return;

var c = v * s;
var x = c * (1 - Math.Abs((degree / 60 % 2) - 1));
var x = c * (1 - Math.Abs(degree / 60 % 2 - 1));
var m = v - c;

double r = 0, g = 0, b = 0;
Expand Down Expand Up @@ -227,10 +203,7 @@ public void RgbFromHsv(double h, double s, double v, int a)
/// <param name="hex">The hexadecimal.</param>
public void RbgHex(string hex)
{
if (string.IsNullOrEmpty(hex))
{
return;
}
if (string.IsNullOrEmpty(hex)) return;

Color color;

Expand Down Expand Up @@ -271,7 +244,7 @@ public void ColorToHsv(int r, int g, int b, int a)
var min = Math.Min(r, Math.Min(g, b));

H = GetHue(r, g, b);
S = max == 0 ? 0 : 1d - (1d * min / max);
S = max == 0 ? 0 : 1d - 1d * min / max;
V = max / 255d;

GetHex();
Expand All @@ -283,10 +256,7 @@ public void ColorToHsv(int r, int g, int b, int a)
/// <param name="hex">The hexadecimal.</param>
public void HexToColor(string hex)
{
if (string.IsNullOrEmpty(hex))
{
return;
}
if (string.IsNullOrEmpty(hex)) return;

var color = (Color)ColorConverter.ConvertFromString(hex);

Expand Down Expand Up @@ -385,32 +355,20 @@ private static double GetHue(int r, int g, int b)
double min = Math.Min(Math.Min(r, g), b);
double max = Math.Max(Math.Max(r, g), b);

if (min.IsEqualTo(max, 10))
{
return 0;
}
if (min.IsEqualTo(max, 10)) return 0;

double hue;

if (max.IsEqualTo(r, 10))
{
hue = (g - b) / (max - min);
}
else if (max.IsEqualTo(g, 10))
{
hue = 2f + ((b - r) / (max - min));
}
hue = 2f + (b - r) / (max - min);
else
{
hue = 4f + ((r - g) / (max - min));
}
hue = 4f + (r - g) / (max - min);

hue *= 60;

if (hue < 0)
{
hue += 360;
}
if (hue < 0) hue += 360;

return hue * Math.PI / 180;
}
Expand All @@ -423,4 +381,4 @@ private void GetHex()
Hex = string.Concat("#", $"{R:X2}{G:X2}{B:X2}");
}
}
}
}
7 changes: 2 additions & 5 deletions Imaging/CustomImageFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public sealed class CustomImageFormat : ICustomImageFormat
public Bitmap GetCifFile(string path)
{
var image = CsvHandler.ReadCsv(path, ImagingResources.Separator);
if (image == null)
{
return null;
}
if (image == null) return null;

//compressed or not
return image[0][2] == ImagingResources.CifCompressed
Expand Down Expand Up @@ -82,4 +79,4 @@ public void CompressedToCifFile(Bitmap image, string path)
CsvHandler.WriteCsv(path, lst);
}
}
}
}
Loading

0 comments on commit c139e6d

Please sign in to comment.