/
EnhancedBitmap.cs
95 lines (81 loc) · 2.99 KB
/
EnhancedBitmap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
namespace MZXImageResampler
{
unsafe public class EnhancedBitmap
{
private struct PixelData
{
public byte blue;
public byte green;
public byte red;
public byte alpha;
public override string ToString()
{
return "(" + alpha.ToString() + ", " + red.ToString() + ", " + green.ToString() + ", " + blue.ToString() + ")";
}
}
private Bitmap workingBitmap = null;
private int width = 0;
private BitmapData bitmapData = null;
private Byte* pBase = null;
public EnhancedBitmap(Bitmap inputBitmap)
{
if (inputBitmap.PixelFormat != PixelFormat.Format32bppArgb)
{
workingBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage((Image)workingBitmap))
{
g.DrawImage((Image)inputBitmap, new Point(0, 0));
}
}
else { workingBitmap = inputBitmap; }
}
public EnhancedBitmap(int w, int h)
{
workingBitmap = new Bitmap(w, h);
}
public ColorPalette Palette { get { return workingBitmap.Palette; } }
public int Width { get { return workingBitmap.Width; } }
public int Height { get { return workingBitmap.Height; } }
public Bitmap WorkingImage { get { return workingBitmap; } }
public void LockImage()
{
Rectangle bounds = new Rectangle(Point.Empty, workingBitmap.Size);
width = (int)(bounds.Width * sizeof(PixelData));
if (width % 4 != 0) width = 4 * (width / 4 + 1);
//Lock Image
bitmapData = workingBitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
pBase = (Byte*)bitmapData.Scan0.ToPointer();
}
private PixelData* pixelData = null;
public Color GetPixel(int x, int y)
{
pixelData = (PixelData*)(pBase + y * width + x * sizeof(PixelData));
return Color.FromArgb(pixelData->alpha, pixelData->red, pixelData->green, pixelData->blue);
}
public Color GetPixelNext()
{
pixelData++;
return Color.FromArgb(pixelData->alpha, pixelData->red, pixelData->green, pixelData->blue);
}
public void SetPixel(int x, int y, Color color)
{
PixelData* data = (PixelData*)(pBase + y * width + x * sizeof(PixelData));
data->alpha = color.A;
data->red = color.R;
data->green = color.G;
data->blue = color.B;
}
public void UnlockImage()
{
workingBitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}
}
}