From 82c03241895d74593ddf7631b016ef76071952e8 Mon Sep 17 00:00:00 2001 From: Alexey 'Cluster' Avdyukhin Date: Sun, 17 Sep 2023 23:04:57 +0400 Subject: Support for weird image sizes. --- NesTiler/FastBitmap.cs | 10 ++++++++-- NesTiler/Palette.cs | 2 +- NesTiler/Program.cs | 16 +++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/NesTiler/FastBitmap.cs b/NesTiler/FastBitmap.cs index 5831170..e45a899 100644 --- a/NesTiler/FastBitmap.cs +++ b/NesTiler/FastBitmap.cs @@ -45,9 +45,15 @@ namespace com.clusterrr.Famicom.NesTiler } } - public SKColor GetPixelColor(int x, int y) + public SKColor GetPixelColor(int x, int y, SKColor? defaultColor = null) { - return pixels[((y + verticalOffset) * Width) + x]; + var index = ((y + verticalOffset) * Width) + x; + if (index >= pixels.Length) + { + if (defaultColor.HasValue) return defaultColor.Value; + throw new IndexOutOfRangeException($"Pixel {x}x{y} is out of range"); + } + return pixels[index]; } public void SetPixelColor(int x, int y, SKColor color) diff --git a/NesTiler/Palette.cs b/NesTiler/Palette.cs index 8fad30c..03d6333 100644 --- a/NesTiler/Palette.cs +++ b/NesTiler/Palette.cs @@ -80,7 +80,7 @@ namespace com.clusterrr.Famicom.NesTiler if (y < 0) continue; for (int x = leftX; x < leftX + width; x++) { - var color = image.GetPixelColor(x, y); + var color = image.GetPixelColor(x, y, bgColor); delta += GetMinDelta(color, bgColor).delta; } } diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs index b5c0b40..8954f2d 100644 --- a/NesTiler/Program.cs +++ b/NesTiler/Program.cs @@ -136,8 +136,8 @@ namespace com.clusterrr.Famicom.NesTiler if (image == null) throw new InvalidDataException($"Can't load {filename}."); images[imageFile.Key] = image; - if (image.Width % c.TilePalWidth != 0) throw new ArgumentException($"Image width must be divisible by {c.TilePalWidth}.", filename); - if (image.Height % c.TilePalHeight != 0) throw new ArgumentException($"Image height must be divisible by {c.TilePalHeight}.", filename); + if (image.Width % c.TileWidth != 0) throw new ArgumentException($"Image width must be divisible by {c.TileWidth}.", filename); + if (image.Height % c.TileHeight != 0) throw new ArgumentException($"Image height must be divisible by {c.TileHeight}.", filename); } // Change all colors in the images to colors from the NES palette @@ -356,15 +356,17 @@ namespace com.clusterrr.Famicom.NesTiler { var cy = (tilePalY * c.TilePalHeight) + y - attributeTableOffset; if (cy < 0) continue; - var color = image.GetPixelColor((tilePalX * c.TilePalWidth) + x, cy); + var color = image.GetPixelColor((tilePalX * c.TilePalWidth) + x, cy, bgColor); var similarColor = nesColors.FindSimilarColor(Enumerable.Concat( bestPalette, new[] { bgColor } ), color); - image.SetPixelColor( - (tilePalX * c.TilePalWidth) + x, - cy, - similarColor); + var pixX = (tilePalX * c.TilePalWidth) + x; + if (pixX < image.Width && cy < image.Height) + image.SetPixelColor( + pixX, + cy, + similarColor); } } } // tile palette X -- cgit v1.2.3