Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NesTiler.git/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <cluster@cluster.wtf>2023-09-17 22:04:57 +0300
committerAlexey 'Cluster' Avdyukhin <cluster@cluster.wtf>2023-09-17 22:04:57 +0300
commit82c03241895d74593ddf7631b016ef76071952e8 (patch)
tree6e26f0e47fa1f5f9d68de8bbd194f08b474cfcfc
parent51a8dde3fa49f1bfb4cd3cab793f06fd1521b317 (diff)
Support for weird image sizes.
-rw-r--r--NesTiler/FastBitmap.cs10
-rw-r--r--NesTiler/Palette.cs2
-rw-r--r--NesTiler/Program.cs16
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