diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-10-13 19:29:40 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-10-13 19:29:40 +0300 |
commit | 32b9f5500f764149fe80fc059f3862de3ae5d73d (patch) | |
tree | 2250fd170e280303972fe57041619ebe581bd982 /NesTiler | |
parent | 882d040eeaf036b0841b68d6ea93ac9d98a1f84a (diff) |
Removed image processing optimization due to bad compatibility
Diffstat (limited to 'NesTiler')
-rw-r--r-- | NesTiler/FastBitmap.cs | 116 | ||||
-rw-r--r-- | NesTiler/Palette.cs | 4 | ||||
-rw-r--r-- | NesTiler/Program.cs | 16 |
3 files changed, 9 insertions, 127 deletions
diff --git a/NesTiler/FastBitmap.cs b/NesTiler/FastBitmap.cs deleted file mode 100644 index 1d7aac1..0000000 --- a/NesTiler/FastBitmap.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System;
-using System.Collections.Generic;
-using System.Drawing;
-using System.Drawing.Imaging;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace com.clusterrr.Famicom.NesTiler
-{
- public class FastBitmap : IDisposable
- {
- readonly Bitmap bitmap;
- BitmapData data;
-
- public int Width { get => bitmap.Width; }
- public int Height { get => bitmap.Height; }
-
- public FastBitmap(Bitmap bitmap)
- {
- this.bitmap = bitmap;
- Lock();
- }
-
- private void Lock()
- {
- data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- }
-
- private void Unlock()
- {
- bitmap.UnlockBits(data);
- }
-
- public Color GetPixel(int x, int y)
- {
- if (x < 0 || x >= bitmap.Width)
- throw new IndexOutOfRangeException($"X out of range: {x}");
- if (y < 0 || y >= bitmap.Height)
- throw new IndexOutOfRangeException($"Y out of range: {y}");
- unsafe
- {
- byte* pixel = (byte*)data.Scan0 + (y * data.Stride) + x * 3;
- return Color.FromArgb(pixel[2], pixel[1], pixel[0]);
- }
- }
-
- public void SetPixel(int x, int y, Color color)
- {
- if (x < 0 || x >= bitmap.Width)
- throw new IndexOutOfRangeException($"X out of range: {x}");
- if (y < 0 || y >= bitmap.Height)
- throw new IndexOutOfRangeException($"Y out of range: {y}");
- unsafe
- {
- byte* pixel = (byte*)data.Scan0 + (y * data.Stride) + x * 3;
- pixel[0] = color.B;
- pixel[1] = color.G;
- pixel[2] = color.R;
- }
- }
-
- public void Save(string filename)
- {
- Unlock();
- bitmap.Save(filename);
- Lock();
- }
-
- public void Save(string filename, ImageFormat format)
- {
- Unlock();
- bitmap.Save(filename, format);
- Lock();
- }
-
- public void Save(Stream stream, ImageFormat format)
- {
- Unlock();
- bitmap.Save(stream, format);
- Lock();
- }
-
- public Bitmap GetBitmap()
- {
- Unlock();
- var result = new Bitmap(bitmap);
- Lock();
- return result;
- }
-
- public void Dispose()
- {
- Unlock();
- }
-
- public static FastBitmap FromFile(string filename)
- {
- var bitmap = (Bitmap)Image.FromFile(filename);
- return new FastBitmap(bitmap);
- }
-
- public static FastBitmap FromStream(Stream stream)
- {
- var bitmap = (Bitmap)Image.FromStream(stream);
- return new FastBitmap(bitmap);
- }
-
- public static FastBitmap Copy(Image image)
- {
- var bitmap = new Bitmap(image);
- return new FastBitmap(bitmap);
- }
- }
-}
diff --git a/NesTiler/Palette.cs b/NesTiler/Palette.cs index 6b29f02..a448917 100644 --- a/NesTiler/Palette.cs +++ b/NesTiler/Palette.cs @@ -31,7 +31,7 @@ namespace com.clusterrr.Famicom.NesTiler // Empty palette
}
- public Palette(FastBitmap image, int leftX, int topY, int width, int height, Color bgColor)
+ public Palette(Bitmap image, int leftX, int topY, int width, int height, Color bgColor)
{
Dictionary<Color, int> colorCounter = new Dictionary<Color, int>();
colorCounter[bgColor] = 0;
@@ -66,7 +66,7 @@ namespace com.clusterrr.Famicom.NesTiler if (colorsList.Count > i) this[i + 1] = colorsList[i];
}
- public double GetTileDelta(FastBitmap image, int leftX, int topY, int width, int height, Color bgColor)
+ public double GetTileDelta(Bitmap image, int leftX, int topY, int width, int height, Color bgColor)
{
double delta = 0;
for (int y = topY; y < topY + height; y++)
diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs index 361bb96..f29a43c 100644 --- a/NesTiler/Program.cs +++ b/NesTiler/Program.cs @@ -72,8 +72,8 @@ namespace com.clusterrr.Famicom.NesTiler var mode = TilesMode.Backgrounds;
int tilePalWidth = 16;
int tilePalHeight = 16;
- var imagesOriginal = new Dictionary<int, FastBitmap>();
- var imagesRecolored = new Dictionary<int, FastBitmap>();
+ var imagesOriginal = new Dictionary<int, Bitmap>();
+ var imagesRecolored = new Dictionary<int, Bitmap>();
var palleteIndexes = new Dictionary<int, byte[,]>();
var patternTableStartOffsets = new Dictionary<int, int>();
var patternTables = new Dictionary<int, Dictionary<int, Tile>>();
@@ -96,8 +96,6 @@ namespace com.clusterrr.Famicom.NesTiler if (!match.Success)
throw new ArgumentException($"Unknown argument: {args[i]}");
string param = match.Groups["param"].Value;
- if (param[^1] == '-')
- param = param[0..^1];
string indexStr = match.Groups["index"].Value;
int indexNum = 0;
if (!string.IsNullOrEmpty(indexStr))
@@ -232,7 +230,7 @@ namespace com.clusterrr.Famicom.NesTiler var offsetRegex = new Regex(@"^(?<filename>.*?)(:(?<offset>[0-9]+)(:(?<height>[0-9]+))?)?$");
var match = offsetRegex.Match(image.Value);
var filename = match.Groups["filename"].Value;
- imagesOriginal[image.Key] = FastBitmap.FromFile(filename);
+ imagesOriginal[image.Key] = Image.FromFile(filename) as Bitmap;
var offsetS = match.Groups["offset"].Value;
var heightS = match.Groups["height"].Value;
// Crop it if need
@@ -245,11 +243,11 @@ namespace com.clusterrr.Famicom.NesTiler Console.WriteLine($"Cropping it to {offset}:{height}...");
var cropped = new Bitmap(imagesOriginal[image.Key].Width, height);
var gr = Graphics.FromImage(cropped);
- gr.DrawImageUnscaledAndClipped(imagesOriginal[image.Key].GetBitmap(),
+ gr.DrawImageUnscaledAndClipped(imagesOriginal[image.Key],
new Rectangle(0, -offset, imagesOriginal[image.Key].Width, imagesOriginal[image.Key].Height));
gr.Flush();
imagesOriginal[image.Key].Dispose();
- imagesOriginal[image.Key] = new FastBitmap(cropped);
+ imagesOriginal[image.Key] = new Bitmap(cropped);
}
if ((imagesOriginal[image.Key].Width % tilePalWidth != 0) || (imagesOriginal[image.Key].Height % tilePalHeight != 0))
throw new InvalidDataException("Invalid image size");
@@ -260,7 +258,7 @@ namespace com.clusterrr.Famicom.NesTiler foreach (var imageNum in imagesOriginal.Keys)
{
Console.WriteLine($"Adjusting colors for file #{imageNum} - {imageFiles[imageNum]}...");
- var image = new FastBitmap(imagesOriginal[imageNum].GetBitmap());
+ var image = new Bitmap(imagesOriginal[imageNum]);
imagesRecolored[imageNum] = image;
for (int y = 0; y < image.Height; y++)
{
@@ -481,7 +479,7 @@ namespace com.clusterrr.Famicom.NesTiler {
Console.WriteLine($"Mapping palettes for file #{imageNum} - {imageFiles[imageNum]}...");
var image = imagesOriginal[imageNum];
- var imageRecolored = new FastBitmap(image.GetBitmap());
+ var imageRecolored = new Bitmap(image);
imagesRecolored[imageNum] = imageRecolored;
palleteIndexes[imageNum] = new byte[image.Width / tilePalWidth, image.Height / tilePalHeight];
// For each tile/sprite
|