Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 8d6b902

Browse files
author
Allester Fox
committed
Conversion progress
RGB332 support Optimisations
1 parent 63b11e0 commit 8d6b902

2 files changed

Lines changed: 153 additions & 48 deletions

File tree

Image2Bitmap/Form1.Designer.cs

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Image2Bitmap/Form1.cs

Lines changed: 141 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ enum TransformColorFormats
1717
{
1818
BW1BppH,
1919
BW1BppV,
20-
BW8Bpp,
20+
//BW8Bpp,
2121
RGB332,
22-
RGB444,
23-
RGB565,
22+
//RGB444,
23+
//RGB565,
2424
}
2525

26-
Image OriginalImage;
26+
private Image OriginalImage;
27+
private BackgroundWorker bgWork;
2728

2829
public Form1()
2930
{
@@ -33,6 +34,12 @@ public Form1()
3334
//selBox_Format.DataSource = Enum.GetValues(typeof(PixelFormat));
3435
selBox_Format.DataSource = Enum.GetValues(typeof(TransformColorFormats));
3536
selBox_Format.SelectedIndex = 0;
37+
38+
bgWork = new BackgroundWorker();
39+
bgWork.WorkerReportsProgress = true;
40+
bgWork.DoWork += new DoWorkEventHandler(ConvertImage);
41+
bgWork.ProgressChanged += new ProgressChangedEventHandler(BgConvertProgress);
42+
bgWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ConvertDone);
3643
}
3744

3845
private void Form1_Load(object sender, EventArgs e)
@@ -54,7 +61,8 @@ private void Btn_open_Click(object sender, EventArgs e)
5461
OriginalImage = Image.FromStream(openFile.OpenFile());
5562
imageBox.Image = OriginalImage;
5663
txt_imgSize.Text = string.Format(
57-
"Info:\nWidth: {0}\nHeight: {1}\n{2}",
64+
"Filename: {0}\nWidth: {1}\nHeight: {2}\n{3}",
65+
openFile.SafeFileName,
5866
OriginalImage.Width,
5967
OriginalImage.Height,
6068
OriginalImage.PixelFormat.ToString()
@@ -70,18 +78,23 @@ private void Btn_open_Click(object sender, EventArgs e)
7078
}
7179
}
7280

73-
private void GenDrawCode_BW1BbppH(Image image)
81+
private String GenDrawCode_BW1BbppH(Image image)
7482
{
75-
using (Bitmap bmp = new Bitmap(image.Width - (image.Width % 8) + 8, image.Height))
83+
String result = "uint8_t image = {" + Environment.NewLine + "\t";
84+
int correction = image.Width % 8;
85+
if (correction > 0)
86+
correction = -correction + 8;
87+
88+
using (Bitmap bmp = new Bitmap(image.Width + correction, image.Height))
7689
{
7790
using (Graphics canvas = Graphics.FromImage(bmp))
7891
{
7992
canvas.Clear(Color.White);
8093
canvas.DrawImage(image, 0, 0);
81-
82-
GeneratedCode.Text = "uint8_t image = {" + Environment.NewLine;
83-
GeneratedCode.Text += "\t";
94+
8495
int x_step = 0;
96+
int pixelsTotal = bmp.Width * bmp.Height;
97+
int pixelsCurrent = 0;
8598

8699
for (int y = 0; y < bmp.Height; y++)
87100
{
@@ -92,91 +105,172 @@ private void GenDrawCode_BW1BbppH(Image image)
92105
for (int bitInBlock = 7; bitInBlock >= 0; bitInBlock--)
93106
{
94107
Color pixelColor = bmp.GetPixel((block * 8 + 7 - bitInBlock), y);
95-
if (pixelColor.R > 127 || pixelColor.G > 127 || pixelColor.B > 127)
108+
if (pixelColor.R > 248 || pixelColor.G > 248 || pixelColor.B > 248)
96109
ColorByte &= ~(1 << bitInBlock);
97110
else
98111
ColorByte |= 1 << bitInBlock;
112+
pixelsCurrent++; // For progress bar
99113
}
100-
GeneratedCode.Text += "0x" + ColorByte.ToString("X2") + ", ";
114+
result += "0x" + ColorByte.ToString("X2") + ", ";
101115
x_step++;
102116
if (x_step == 16)
103-
GeneratedCode.Text += Environment.NewLine + "\t";
117+
{
118+
x_step = 0;
119+
result += Environment.NewLine + "\t";
120+
}
104121

122+
bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
105123
}
106124
}
107-
GeneratedCode.Text += Environment.NewLine + "};";
108125
}
109126
}
110-
}
111-
112127

113-
private void GenDrawCode_BW1BbppV(Image image)
128+
result += Environment.NewLine + "};";
129+
return result;
130+
}
131+
132+
private String GenDrawCode_BW1BbppV(Image image)
114133
{
115-
using (Bitmap bmp = new Bitmap(image.Width, image.Height - (image.Height % 8) + 8))
134+
String result = "uint8_t image = {" + Environment.NewLine + "\t";
135+
int correction = image.Height % 8;
136+
if (correction > 0)
137+
correction = -correction + 8;
138+
139+
using (Bitmap bmp = new Bitmap(image.Width, image.Height + correction))
116140
{
117141
using (Graphics canvas = Graphics.FromImage(bmp))
118142
{
119143
canvas.Clear(Color.White);
120144
canvas.DrawImage(image, 0, 0);
121145

122-
GeneratedCode.Text = "uint8_t image = {" + Environment.NewLine;
123-
GeneratedCode.Text += "\t";
124146
int x_step = 0;
125147

126-
for (int x = 0; x < bmp.Width; x++)
148+
int pixelsTotal = bmp.Width * bmp.Height;
149+
int pixelsCurrent = 0;
150+
151+
for (int vBlock = 0; vBlock < bmp.Height / 8; vBlock++)
127152
{
128-
for (int block = 0; block < bmp.Height / 8; block++)
153+
for (int x = 0; x < bmp.Width; x++)
129154
{
130155
int ColorByte = 0;
131-
132-
for (int bitInBlock = 7; bitInBlock >= 0; bitInBlock--)
156+
//result += "B"; // Show in byte format: B11110000 (0xf0)
157+
for (int bitInBlock = 0; bitInBlock < 8; bitInBlock++)
133158
{
134-
Color pixelColor = bmp.GetPixel(x, block * 8 + bitInBlock);
135-
if (pixelColor.R > 127 || pixelColor.G > 127 || pixelColor.B > 127)
136-
ColorByte &= ~(1 << bitInBlock);
159+
Color pixelColor = bmp.GetPixel(x, vBlock * 8 + bitInBlock);
160+
if (pixelColor.R > 248 || pixelColor.G > 248 || pixelColor.B > 248)
161+
ColorByte &= ~(1 << bitInBlock); // result += "0";
137162
else
138-
ColorByte |= 1 << bitInBlock;
163+
ColorByte |= 1 << bitInBlock; // result += "1";
164+
pixelsCurrent++;
139165
}
140-
GeneratedCode.Text += "0x" + ColorByte.ToString("X2") + ", ";
141-
142-
/*
143-
GeneratedCode.Text += "B";
144-
for (int bitInBlock = 7; bitInBlock >= 0; bitInBlock--)
166+
result += "0x" + ColorByte.ToString("X2") + ", ";
167+
168+
x_step++;
169+
if (x_step == 16)
145170
{
146-
Color pixelColor = bmp.GetPixel(x, block * 8 + bitInBlock);
147-
if (pixelColor.R > 127 || pixelColor.G > 127 || pixelColor.B > 127)
148-
GeneratedCode.Text += "0";
149-
else
150-
GeneratedCode.Text += "1";
171+
x_step = 0;
172+
result += Environment.NewLine + "\t";
151173
}
152-
GeneratedCode.Text += ", ";
153-
*/
154174

175+
bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
155176
}
177+
}
178+
}
179+
}
180+
result += Environment.NewLine + "};";
181+
return result;
182+
}
183+
184+
private String GenDrawCode_RGB332(Image image)
185+
{
186+
String result = "uint8_t image = {" + Environment.NewLine + "\t";
187+
188+
using (Bitmap bmp = new Bitmap(image))
189+
{
190+
int x_step = 0;
191+
Color pixelColor;
192+
193+
int pixelsTotal = bmp.Width * bmp.Height;
194+
int pixelsCurrent = 0;
195+
196+
for (int y = 0; y < bmp.Height; y++)
197+
{
198+
for (int x = 0; x < bmp.Width; x++)
199+
{
200+
pixelColor = bmp.GetPixel(x, y);
201+
// byte = 8 bit per color
202+
// RRRRRRRR
203+
int ColorByte = (pixelColor.R & 0xE0) | (pixelColor.G & 0xE0) >> 3 | (pixelColor.B & 0xC0) >> 6;
204+
205+
result += "0x" + ColorByte.ToString("X2") + ", ";
206+
pixelsCurrent++;
207+
156208
x_step++;
157209
if (x_step == 16)
158-
GeneratedCode.Text += Environment.NewLine + "\t";
210+
{
211+
x_step = 0;
212+
result += Environment.NewLine + "\t";
213+
}
214+
215+
bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
159216
}
160-
GeneratedCode.Text += Environment.NewLine + "};";
161217
}
162218
}
219+
result += Environment.NewLine + "};";
220+
return result;
163221
}
164222

165-
private void btn_Convert_Click(object sender, EventArgs e)
223+
private void ConvertImage(object sender, DoWorkEventArgs e)
166224
{
167-
tabBox.SelectedIndex = 1; // 2nd tab
168-
switch (selBox_Format.SelectedItem)
225+
switch (e.Argument)
169226
{
170227
case TransformColorFormats.BW1BppH:
171-
GenDrawCode_BW1BbppH(OriginalImage);
228+
e.Result = GenDrawCode_BW1BbppH(OriginalImage);
172229
break;
173230
case TransformColorFormats.BW1BppV:
174-
GenDrawCode_BW1BbppV(OriginalImage);
231+
e.Result = GenDrawCode_BW1BbppV(OriginalImage);
232+
break;
233+
case TransformColorFormats.RGB332:
234+
e.Result = GenDrawCode_RGB332(OriginalImage);
175235
break;
176236
default:
177237
MessageBox.Show("Sorry, unsupported.");
178238
break;
179239
}
180240
}
241+
242+
private void BgConvertProgress(object sender, ProgressChangedEventArgs e)
243+
{
244+
convertProgress.Value = e.ProgressPercentage;
245+
}
246+
247+
private void ConvertDone(object sender, RunWorkerCompletedEventArgs e)
248+
{
249+
convertProgress.Value = 100;
250+
GeneratedCode.Text = (String)e.Result;
251+
tabBox.SelectedIndex = 1; // Open "code" tab
252+
253+
//bgWork.DoWork -= ConvertImage;
254+
//bgWork.ProgressChanged -= BgConvertProgress;
255+
//bgWork.RunWorkerCompleted -= ConvertDone;
256+
257+
btn_Convert.Enabled = true;
258+
}
259+
260+
private void Btn_Convert_Click(object sender, EventArgs e)
261+
{
262+
convertProgress.Value = 0;
263+
btn_Convert.Enabled = false;
264+
bgWork.RunWorkerAsync(selBox_Format.SelectedItem);
265+
/*
266+
while (bgWork.IsBusy)
267+
{
268+
convertProgress.Increment(1);
269+
// Keep UI messages moving, so the form remains
270+
// responsive during the asynchronous operation.
271+
Application.DoEvents();
272+
}
273+
*/
274+
}
181275
}
182276
}

0 commit comments

Comments
 (0)