MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Bytemap.cs
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace MyCaffe.basecode
9{
13 [Serializable]
14 public class Bytemap
15 {
16 int m_nChannels;
17 int m_nHeight;
18 int m_nWidth;
19 int m_nChannelOffset;
20 byte[] m_rgData;
21
29 public Bytemap(int nChannels, int nHeight, int nWidth, byte[] rgData = null)
30 {
31 int nSize = nChannels * nHeight * nWidth;
32 m_rgData = (rgData != null) ? rgData : new byte[nSize];
33 m_nChannels = nChannels;
34 m_nHeight = nHeight;
35 m_nWidth = nWidth;
36 m_nChannelOffset = nHeight * nWidth;
37 }
38
43 public Bytemap(Bytemap data)
44 {
45 int nChannels = data.Channels;
46 int nHeight = data.Height;
47 int nWidth = data.Width;
48
49 int nSize = nChannels * nHeight * nWidth;
50 m_rgData = new byte[nSize];
51 m_nChannels = nChannels;
52 m_nHeight = nHeight;
53 m_nWidth = nWidth;
54 m_nChannelOffset = nHeight * nWidth;
55
56 Array.Copy(data.Bytes, m_rgData, nSize);
57 }
58
65 public void SetPixel(int nX, int nY, byte clr)
66 {
67 int nIdx = nY * m_nWidth + nX;
68 m_rgData[nIdx] = clr;
69
70 if (m_nChannels == 3)
71 {
72 m_rgData[nIdx + m_nChannelOffset] = clr;
73 m_rgData[nIdx + m_nChannelOffset * 2] = clr;
74 }
75 }
76
83 public void SetPixel(int nX, int nY, Color clr)
84 {
85 byte bR = (byte)clr.R;
86 byte bG = (byte)clr.G;
87 byte bB = (byte)clr.B;
88
89 int nIdx = nY * m_nWidth + nX;
90 m_rgData[nIdx] = bR;
91
92 if (m_nChannels == 3)
93 {
94 m_rgData[nIdx + m_nChannelOffset] = bG;
95 m_rgData[nIdx + m_nChannelOffset * 2] = bB;
96 }
97 }
98
105 public Color GetPixel(int nX, int nY)
106 {
107 int nIdx = nY * m_nWidth + nX;
108 byte bR = m_rgData[nIdx];
109 byte bG = bR;
110 byte bB = bR;
111
112 if (m_nChannels == 3)
113 {
114 bG = m_rgData[nIdx + m_nChannelOffset];
115 bB = m_rgData[nIdx + m_nChannelOffset * 2];
116 }
117
118 return Color.FromArgb(bR, bG, bB);
119 }
120
124 public void Clear()
125 {
126 Array.Clear(m_rgData, 0, m_rgData.Length);
127 }
128
132 public byte[] Bytes
133 {
134 get { return m_rgData; }
135 }
136
140 public int Channels
141 {
142 get { return m_nChannels; }
143 }
144
148 public int Height
149 {
150 get { return m_nHeight; }
151 }
152
156 public int Width
157 {
158 get { return m_nWidth; }
159 }
160
165 public Bitmap ToImage()
166 {
167 Bitmap bmp = new Bitmap(m_nWidth, m_nHeight);
168 LockBitmap bmpA = new LockBitmap(bmp);
169
170 bmpA.LockBits();
171
172 for (int y = 0; y < m_nHeight; y++)
173 {
174 for (int x = 0; x < m_nWidth; x++)
175 {
176 Color clr = GetPixel(x, y);
177 bmpA.SetPixel(x, y, clr);
178 }
179 }
180
181 bmpA.UnlockBits();
182
183 return bmp;
184 }
185
191 public static Bytemap FromImage(Bitmap bmp)
192 {
193 Bytemap data = new Bytemap(3, bmp.Height, bmp.Width);
194 LockBitmap bmpA = new LockBitmap(bmp);
195
196 bmpA.LockBits();
197
198 for (int y = 0; y < bmp.Height; y++)
199 {
200 for (int x = 0; x < bmp.Width; x++)
201 {
202 Color clr = bmpA.GetPixel(x, y);
203 data.SetPixel(x, y, clr);
204 }
205 }
206
207 bmpA.UnlockBits();
208
209 return data;
210 }
211 }
212}
The Bytemap operates similar to a bitmap but is actually just an array of bytes.
Definition: Bytemap.cs:15
int Channels
Specifies the channels of the data.
Definition: Bytemap.cs:141
int Height
Specifies the height of the data.
Definition: Bytemap.cs:149
Color GetPixel(int nX, int nY)
Get the color of a pixel in the map.
Definition: Bytemap.cs:105
void Clear()
Reset all values to zero.
Definition: Bytemap.cs:124
Bytemap(Bytemap data)
The constructorl
Definition: Bytemap.cs:43
void SetPixel(int nX, int nY, byte clr)
Set a given pixel to a given color.
Definition: Bytemap.cs:65
Bytemap(int nChannels, int nHeight, int nWidth, byte[] rgData=null)
The constructor.
Definition: Bytemap.cs:29
byte[] Bytes
Specifies the data itself.
Definition: Bytemap.cs:133
void SetPixel(int nX, int nY, Color clr)
Set a given pixel to a given color.
Definition: Bytemap.cs:83
static Bytemap FromImage(Bitmap bmp)
Converts a bitmap into a new Bytemap.
Definition: Bytemap.cs:191
Bitmap ToImage()
Converts the Bytemap into a Bitmap.
Definition: Bytemap.cs:165
int Width
Specifies the width of the data.
Definition: Bytemap.cs:157
The LockBitmap class provides very efficient SetPixel and GetPixel functionality of a bitmap by using...
Definition: LockBitmap.cs:21
Color GetPixel(int x, int y)
Get the color of the specified pixel
Definition: LockBitmap.cs:121
void SetPixel(int x, int y, Color color)
Set the color of the specified pixel
Definition: LockBitmap.cs:164
void UnlockBits()
Unlock bitmap data, releasing its underlying data.
Definition: LockBitmap.cs:99
void LockBits()
Lock bitmap data to access its underlying raw data.
Definition: LockBitmap.cs:55
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12