349 lines
12 KiB
C#
349 lines
12 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
|
|
namespace AForge.Imaging;
|
|
|
|
public static class Image
|
|
{
|
|
public static bool IsGrayscale(Bitmap image)
|
|
{
|
|
//IL_0003: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000d: Invalid comparison between Unknown and I4
|
|
bool result = false;
|
|
if ((int)((Image)image).PixelFormat == 198659)
|
|
{
|
|
result = true;
|
|
ColorPalette palette = ((Image)image).Palette;
|
|
for (int i = 0; i < 256; i++)
|
|
{
|
|
Color color = palette.Entries[i];
|
|
if (color.R != i || color.G != i || color.B != i)
|
|
{
|
|
result = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static Bitmap CreateGrayscaleImage(int width, int height)
|
|
{
|
|
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000d: Expected O, but got Unknown
|
|
Bitmap val = new Bitmap(width, height, (PixelFormat)198659);
|
|
SetGrayscalePalette(val);
|
|
return val;
|
|
}
|
|
|
|
public static void SetGrayscalePalette(Bitmap image)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000b: Invalid comparison between Unknown and I4
|
|
if ((int)((Image)image).PixelFormat != 198659)
|
|
{
|
|
throw new UnsupportedImageFormatException("Source image is not 8 bpp image.");
|
|
}
|
|
ColorPalette palette = ((Image)image).Palette;
|
|
for (int i = 0; i < 256; i++)
|
|
{
|
|
ref Color reference = ref palette.Entries[i];
|
|
reference = Color.FromArgb(i, i, i);
|
|
}
|
|
((Image)image).Palette = palette;
|
|
}
|
|
|
|
public static Bitmap Clone(Bitmap source, PixelFormat format)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0027: Expected O, but got Unknown
|
|
if (((Image)source).PixelFormat == format)
|
|
{
|
|
return Clone(source);
|
|
}
|
|
int width = ((Image)source).Width;
|
|
int height = ((Image)source).Height;
|
|
Bitmap val = new Bitmap(width, height, format);
|
|
Graphics val2 = Graphics.FromImage((Image)(object)val);
|
|
val2.DrawImage((Image)(object)source, 0, 0, width, height);
|
|
val2.Dispose();
|
|
return val;
|
|
}
|
|
|
|
public static Bitmap Clone(Bitmap source)
|
|
{
|
|
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003a: Invalid comparison between Unknown and I4
|
|
//IL_003d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0047: Invalid comparison between Unknown and I4
|
|
//IL_004a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0054: Invalid comparison between Unknown and I4
|
|
//IL_0057: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0061: Invalid comparison between Unknown and I4
|
|
BitmapData val = source.LockBits(new Rectangle(0, 0, ((Image)source).Width, ((Image)source).Height), (ImageLockMode)1, ((Image)source).PixelFormat);
|
|
Bitmap val2 = Clone(val);
|
|
source.UnlockBits(val);
|
|
if ((int)((Image)source).PixelFormat == 196865 || (int)((Image)source).PixelFormat == 197634 || (int)((Image)source).PixelFormat == 198659 || (int)((Image)source).PixelFormat == 65536)
|
|
{
|
|
ColorPalette palette = ((Image)source).Palette;
|
|
ColorPalette palette2 = ((Image)val2).Palette;
|
|
int num = palette.Entries.Length;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
ref Color reference = ref palette2.Entries[i];
|
|
reference = palette.Entries[i];
|
|
}
|
|
((Image)val2).Palette = palette2;
|
|
}
|
|
return val2;
|
|
}
|
|
|
|
public static Bitmap Clone(BitmapData sourceData)
|
|
{
|
|
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001c: Expected O, but got Unknown
|
|
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
|
|
int width = sourceData.Width;
|
|
int height = sourceData.Height;
|
|
Bitmap val = new Bitmap(width, height, sourceData.PixelFormat);
|
|
BitmapData val2 = val.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)3, ((Image)val).PixelFormat);
|
|
SystemTools.CopyUnmanagedMemory(val2.Scan0, sourceData.Scan0, height * sourceData.Stride);
|
|
val.UnlockBits(val2);
|
|
return val;
|
|
}
|
|
|
|
[Obsolete("Use Clone(Bitmap, PixelFormat) method instead and specify desired pixel format")]
|
|
public static void FormatImage(ref Bitmap image)
|
|
{
|
|
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000c: Invalid comparison between Unknown and I4
|
|
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001a: Invalid comparison between Unknown and I4
|
|
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0028: Invalid comparison between Unknown and I4
|
|
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0036: Invalid comparison between Unknown and I4
|
|
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0044: Invalid comparison between Unknown and I4
|
|
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0052: Invalid comparison between Unknown and I4
|
|
if ((int)((Image)image).PixelFormat != 137224 && (int)((Image)image).PixelFormat != 139273 && (int)((Image)image).PixelFormat != 2498570 && (int)((Image)image).PixelFormat != 1060876 && (int)((Image)image).PixelFormat != 3424269 && (int)((Image)image).PixelFormat != 1052676 && !IsGrayscale(image))
|
|
{
|
|
Bitmap val = image;
|
|
image = Clone(val, (PixelFormat)137224);
|
|
((Image)val).Dispose();
|
|
}
|
|
}
|
|
|
|
public static Bitmap FromFile(string fileName)
|
|
{
|
|
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0047: Expected O, but got Unknown
|
|
Bitmap val = null;
|
|
FileStream fileStream = null;
|
|
try
|
|
{
|
|
fileStream = File.OpenRead(fileName);
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
byte[] buffer = new byte[10000];
|
|
while (true)
|
|
{
|
|
int num = fileStream.Read(buffer, 0, 10000);
|
|
if (num == 0)
|
|
{
|
|
break;
|
|
}
|
|
memoryStream.Write(buffer, 0, num);
|
|
}
|
|
return (Bitmap)Image.FromStream((Stream)memoryStream);
|
|
}
|
|
finally
|
|
{
|
|
if (fileStream != null)
|
|
{
|
|
fileStream.Close();
|
|
fileStream.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
public unsafe static Bitmap Convert16bppTo8bpp(Bitmap bimap)
|
|
{
|
|
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0021: Invalid comparison between Unknown and I4
|
|
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003e: Invalid comparison between Unknown and I4
|
|
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002a: Invalid comparison between Unknown and I4
|
|
//IL_0080: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0086: Expected O, but got Unknown
|
|
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0047: Invalid comparison between Unknown and I4
|
|
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0033: Invalid comparison between Unknown and I4
|
|
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0075: Expected O, but got Unknown
|
|
//IL_005e: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0064: Expected O, but got Unknown
|
|
Bitmap val = null;
|
|
int num = 0;
|
|
int width = ((Image)bimap).Width;
|
|
int height = ((Image)bimap).Height;
|
|
PixelFormat pixelFormat = ((Image)bimap).PixelFormat;
|
|
if ((int)pixelFormat <= 1060876)
|
|
{
|
|
if ((int)pixelFormat != 1052676)
|
|
{
|
|
if ((int)pixelFormat != 1060876)
|
|
{
|
|
goto IL_008a;
|
|
}
|
|
val = new Bitmap(width, height, (PixelFormat)137224);
|
|
num = 3;
|
|
}
|
|
else
|
|
{
|
|
val = CreateGrayscaleImage(width, height);
|
|
num = 1;
|
|
}
|
|
}
|
|
else if ((int)pixelFormat != 1851406)
|
|
{
|
|
if ((int)pixelFormat != 3424269)
|
|
{
|
|
goto IL_008a;
|
|
}
|
|
val = new Bitmap(width, height, (PixelFormat)2498570);
|
|
num = 4;
|
|
}
|
|
else
|
|
{
|
|
val = new Bitmap(width, height, (PixelFormat)925707);
|
|
num = 4;
|
|
}
|
|
BitmapData val2 = bimap.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)1, ((Image)bimap).PixelFormat);
|
|
BitmapData val3 = val.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)3, ((Image)val).PixelFormat);
|
|
byte* ptr = (byte*)val2.Scan0.ToPointer();
|
|
byte* ptr2 = (byte*)val3.Scan0.ToPointer();
|
|
int stride = val2.Stride;
|
|
int stride2 = val3.Stride;
|
|
for (int i = 0; i < height; i++)
|
|
{
|
|
ushort* ptr3 = (ushort*)(ptr + (nint)i * (nint)stride);
|
|
byte* ptr4 = ptr2 + (nint)i * (nint)stride2;
|
|
int num2 = 0;
|
|
int num3 = width * num;
|
|
while (num2 < num3)
|
|
{
|
|
*ptr4 = (byte)(*ptr3 >> 8);
|
|
num2++;
|
|
ptr3++;
|
|
ptr4++;
|
|
}
|
|
}
|
|
bimap.UnlockBits(val2);
|
|
val.UnlockBits(val3);
|
|
return val;
|
|
IL_008a:
|
|
throw new UnsupportedImageFormatException("Invalid pixel format of the source image.");
|
|
}
|
|
|
|
public unsafe static Bitmap Convert8bppTo16bpp(Bitmap bimap)
|
|
{
|
|
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0021: Invalid comparison between Unknown and I4
|
|
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003e: Invalid comparison between Unknown and I4
|
|
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002a: Invalid comparison between Unknown and I4
|
|
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_008b: Expected O, but got Unknown
|
|
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0047: Invalid comparison between Unknown and I4
|
|
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0069: Expected O, but got Unknown
|
|
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0033: Invalid comparison between Unknown and I4
|
|
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_007a: Expected O, but got Unknown
|
|
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0058: Expected O, but got Unknown
|
|
Bitmap val = null;
|
|
int num = 0;
|
|
int width = ((Image)bimap).Width;
|
|
int height = ((Image)bimap).Height;
|
|
PixelFormat pixelFormat = ((Image)bimap).PixelFormat;
|
|
if ((int)pixelFormat <= 198659)
|
|
{
|
|
if ((int)pixelFormat != 137224)
|
|
{
|
|
if ((int)pixelFormat != 198659)
|
|
{
|
|
goto IL_008f;
|
|
}
|
|
val = new Bitmap(width, height, (PixelFormat)1052676);
|
|
num = 1;
|
|
}
|
|
else
|
|
{
|
|
val = new Bitmap(width, height, (PixelFormat)1060876);
|
|
num = 3;
|
|
}
|
|
}
|
|
else if ((int)pixelFormat != 925707)
|
|
{
|
|
if ((int)pixelFormat != 2498570)
|
|
{
|
|
goto IL_008f;
|
|
}
|
|
val = new Bitmap(width, height, (PixelFormat)3424269);
|
|
num = 4;
|
|
}
|
|
else
|
|
{
|
|
val = new Bitmap(width, height, (PixelFormat)1851406);
|
|
num = 4;
|
|
}
|
|
BitmapData val2 = bimap.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)1, ((Image)bimap).PixelFormat);
|
|
BitmapData val3 = val.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)3, ((Image)val).PixelFormat);
|
|
byte* ptr = (byte*)val2.Scan0.ToPointer();
|
|
byte* ptr2 = (byte*)val3.Scan0.ToPointer();
|
|
int stride = val2.Stride;
|
|
int stride2 = val3.Stride;
|
|
for (int i = 0; i < height; i++)
|
|
{
|
|
byte* ptr3 = ptr + (nint)i * (nint)stride;
|
|
ushort* ptr4 = (ushort*)(ptr2 + (nint)i * (nint)stride2);
|
|
int num2 = 0;
|
|
int num3 = width * num;
|
|
while (num2 < num3)
|
|
{
|
|
*ptr4 = (ushort)(*ptr3 << 8);
|
|
num2++;
|
|
ptr3++;
|
|
ptr4++;
|
|
}
|
|
}
|
|
bimap.UnlockBits(val2);
|
|
val.UnlockBits(val3);
|
|
return val;
|
|
IL_008f:
|
|
throw new UnsupportedImageFormatException("Invalid pixel format of the source image.");
|
|
}
|
|
}
|