Skip to content

Commit

Permalink
Use a custom type for pixel data to get serialisation correct
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Jul 22, 2020
1 parent b3a2d57 commit c334d9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/BingImageDownload/HistogramHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ namespace BingImageDownload
public class HistogramHash
{
public string FileName { get; }
public Dictionary<(int x, int y), uint> RGBA { get; }
public List<RgbaPixelData> Rgba { get; }

public HistogramHash(string fileName, Dictionary<(int x, int y), uint> rgba)
public HistogramHash(string fileName, List<RgbaPixelData> rgba)
{
FileName = fileName;
RGBA = rgba;
Rgba = rgba;
}

internal bool IsInvalid(Paths paths)
{
if (string.IsNullOrWhiteSpace(FileName)) return true;
if (!File.Exists(Path.Combine(paths.SavePath, FileName)) && !File.Exists(Path.Combine(paths.ArchivePath, FileName))) return true;
if (RGBA == null || !RGBA.Any()) return true;
if (Rgba == null || !Rgba.Any()) return true;
return false;
}

internal bool Equal(HistogramHash other)
{
foreach (var (pos, val) in RGBA.ToList())
foreach (var val in Rgba)
{
if (!other.RGBA.ContainsKey(pos)) return false;
var otherVal = other.RGBA[pos];
if (!val.Equals(otherVal)) return false;
var otherVal = other.Rgba.FirstOrDefault(x => x.X.Equals(val.X) && x.Y.Equals(val.Y));
if (otherVal == null) return false;
if (!val.RgbaValue.Equals(otherVal.RgbaValue)) return false;
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/BingImageDownload/ImageHashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private HistogramHash GetRgbaHistogramHash(string filePath)
{
var histogramFile = Path.Combine(paths.HistogramPath, Guid.NewGuid() + ".jpg");
File.Copy(filePath, histogramFile);
var rgba = new Dictionary<(int x, int y), uint>();
var rgba = new List<RgbaPixelData>();
var fileName = Path.GetFileName(filePath);

using (var image = Image.Load<Rgba32>(histogramFile))
Expand All @@ -119,7 +119,7 @@ private HistogramHash GetRgbaHistogramHash(string filePath)
for (var y = 0; y < image.Height; y++)
{
var pixel = image[x, y];
rgba.Add((x, y), pixel.Rgba);
rgba.Add(new RgbaPixelData(x, y, pixel.Rgba));
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/BingImageDownload/RgbaPixelData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace BingImageDownload
{
public class RgbaPixelData
{
public int X { get; }
public int Y { get; }
public uint RgbaValue { get; }

public RgbaPixelData(int x, int y, uint rgbaValue)
{
X = x;
Y = y;
RgbaValue = rgbaValue;
}
}
}

0 comments on commit c334d9c

Please sign in to comment.