-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageOperations.cs
185 lines (162 loc) · 5.85 KB
/
ImageOperations.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
///Algorithms Project
///Image Quantization
///
namespace ImageQuantization
{
/// <summary>
/// Holds the pixel color in 3 byte values: red, green and blue
/// </summary>
public struct RGBPixel
{
public byte red, green, blue;
public RGBPixel(byte red, byte green, byte blue)
{
this.red = red;
this.green = green;
this.blue = blue;
}
public static int Hash(RGBPixel color)
{
int rgb = color.red;
rgb = (rgb << 8) + color.green;
rgb = (rgb << 8) + color.blue;
return rgb;
}
public static RGBPixel UnHash(int rgb)
{
byte red = (byte)((rgb >> 16) & 0xFF);
byte green = (byte)((rgb >> 8) & 0xFF);
byte blue = (byte)(rgb & 0xFF);
return new RGBPixel(red, green, blue);
}
}
public struct RGBPixelD
{
public double red, green, blue;
}
/// <summary>
/// Library of static functions that deal with images
/// </summary>
public class ImageOperations
{
/// <summary>
/// Open an image and load it into 2D array of colors (size: Height x Width)
/// </summary>
/// <param name="ImagePath">Image file path</param>
/// <returns>2D array of colors</returns>
public static RGBPixel[,] OpenImage(string ImagePath)
{
Bitmap original_bm = new Bitmap(ImagePath);
int Height = original_bm.Height;
int Width = original_bm.Width;
RGBPixel[,] Buffer = new RGBPixel[Height, Width];
unsafe
{
BitmapData bmd = original_bm.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, original_bm.PixelFormat);
int x, y;
int nWidth = 0;
bool Format32 = false;
bool Format24 = false;
bool Format8 = false;
if (original_bm.PixelFormat == PixelFormat.Format24bppRgb)
{
Format24 = true;
nWidth = Width * 3;
}
else if (original_bm.PixelFormat == PixelFormat.Format32bppArgb || original_bm.PixelFormat == PixelFormat.Format32bppRgb || original_bm.PixelFormat == PixelFormat.Format32bppPArgb)
{
Format32 = true;
nWidth = Width * 4;
}
else if (original_bm.PixelFormat == PixelFormat.Format8bppIndexed)
{
Format8 = true;
nWidth = Width;
}
int nOffset = bmd.Stride - nWidth;
byte* p = (byte*)bmd.Scan0;
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width; x++)
{
if (Format8)
{
Buffer[y, x].red = Buffer[y, x].green = Buffer[y, x].blue = p[0];
p++;
}
else
{
Buffer[y, x].red = p[2];
Buffer[y, x].green = p[1];
Buffer[y, x].blue = p[0];
if (Format24) p += 3;
else if (Format32) p += 4;
}
}
p += nOffset;
}
original_bm.UnlockBits(bmd);
}
return Buffer;
}
/// <summary>
/// Get the height of the image
/// </summary>
/// <param name="ImageMatrix">2D array that contains the image</param>
/// <returns>Image Height</returns>
public static int GetHeight(RGBPixel[,] ImageMatrix)
{
return ImageMatrix.GetLength(0);
}
/// <summary>
/// Get the width of the image
/// </summary>
/// <param name="ImageMatrix">2D array that contains the image</param>
/// <returns>Image Width</returns>
public static int GetWidth(RGBPixel[,] ImageMatrix)
{
return ImageMatrix.GetLength(1);
}
/// <summary>
/// Display the given image on the given PictureBox object
/// </summary>
/// <param name="ImageMatrix">2D array that contains the image</param>
/// <param name="PicBox">PictureBox object to display the image on it</param>
public static void DisplayImage(RGBPixel[,] ImageMatrix, PictureBox PicBox)
{
// Create Image:
//==============
int Height = ImageMatrix.GetLength(0);
int Width = ImageMatrix.GetLength(1);
Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
unsafe
{
BitmapData bmd = ImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, ImageBMP.PixelFormat);
int nWidth = 0;
nWidth = Width * 3;
int nOffset = bmd.Stride - nWidth;
byte* p = (byte*)bmd.Scan0;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
p[2] = ImageMatrix[i, j].red;
p[1] = ImageMatrix[i, j].green;
p[0] = ImageMatrix[i, j].blue;
p += 3;
}
p += nOffset;
}
ImageBMP.UnlockBits(bmd);
}
if(PicBox != null)
PicBox.Image = ImageBMP;
}
}
}