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

Commit

Permalink
Fixed form freeze
Browse files Browse the repository at this point in the history
RGB-332/444/565 Image-to-code algorithm optimisation (Upto 5x faster)
  • Loading branch information
FoxExe committed Nov 2, 2017
1 parent 71dea14 commit 2136245
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*~
*.[oa]
/.vs
*.psess
*.vsp
*.vs
/Debug
/*/__vm
/*/Debug
Expand Down
5 changes: 4 additions & 1 deletion Image2Bitmap.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Image2Bitmap", "Image2Bitmap\Image2Bitmap.csproj", "{9F9C6B42-B7CB-4175-9DED-7EB94D3B617F}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand Down
28 changes: 15 additions & 13 deletions Image2Bitmap/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 37 additions & 31 deletions Image2Bitmap/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Image2Bitmap
Expand Down Expand Up @@ -165,11 +162,13 @@ private String ImageToCode_1bpp(Image image, bool horisontal = false)
else
bitInBlock--;

bgWork.ReportProgress((int)((double)pixelsCurrent++ / (double)pixelsTotal * 100));

}
}
if (!horisontal)
y += 7;

bgWork.ReportProgress((int)((double)pixelsCurrent++ / (double)pixelsTotal * 100));
}
}
}
Expand Down Expand Up @@ -208,41 +207,57 @@ private string ImageToCode(TransformColorFormats format)
using (Bitmap bmp = new Bitmap(image, Width, Height))
{
int rowPos = 0;
Color pixelColor;

int pixelsTotal = bmp.Width * bmp.Height;
int pixelsCurrent = 0;
int ColorByte = 0;

// ===========================================================
// Optimisation: Upto 5x faster than GetPixel();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);

IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * bmp.Height; // ARGB: Width * Height * 4 (Stride = Width * 4)
byte[] rgbValues = new byte[bytes];

// Format BGRA (GRB+Alpha, inverted). Example: BBBBBBBB GGGGGGGG RRRRRRRR AAAAAAAA
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

int pixelByte = 0;
// ===========================================================

for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
pixelColor = bmp.GetPixel(x, y);
pixelByte = (y * bmp.Width + x) * 4;

switch (format)
{
case TransformColorFormats.RGB_332:
ColorByte =
(pixelColor.R & 0xE0) | // 0xE0 = 1110 0000
(pixelColor.G & 0xE0) >> 3 |
(pixelColor.B & 0xC0) >> 6; // 0xC0 = 1100 0000
ColorByte =
(rgbValues[pixelByte + 2] & 0xE0) | // 0xE0 = 1110 0000
(rgbValues[pixelByte + 1] & 0xE0) >> 3 |
(rgbValues[pixelByte + 0] & 0xC0) >> 6; // 0xC0 = 1100 0000
break;
case TransformColorFormats.RGB_444:
// byte = 16 bit per color
// RRRR RGGG GGGB BBBB
ColorByte =
(pixelColor.R & 0xF0) << 4 | // 0xF0 = 1111 0000
(pixelColor.G & 0xF0) |
(pixelColor.B & 0xF0) >> 4;
(rgbValues[pixelByte + 2] & 0xF0) << 4 | // 0xF0 = 1111 0000
(rgbValues[pixelByte + 1] & 0xF0) |
(rgbValues[pixelByte + 0] & 0xF0) >> 4;
break;
case TransformColorFormats.RGB_565:
// byte = 16 bit per color
// RRRR RGGG GGGB BBBB
ColorByte =
(pixelColor.R & 0xF8) << 8 | // 0xF8 = 1111 1000
(pixelColor.G & 0xFC) << 3 | // 0xFC = 1111 1100
(pixelColor.B & 0xF8) >> 3;
(rgbValues[pixelByte + 2] & 0xF8) << 8 | // 0xF8 = 1111 1000
(rgbValues[pixelByte + 1] & 0xFC) << 3 | // 0xFC = 1111 1100
(rgbValues[pixelByte + 0] & 0xF8) >> 3;
break;
}

Expand All @@ -255,9 +270,8 @@ private string ImageToCode(TransformColorFormats format)
rowPos = 0;
result.Append(Environment.NewLine + "\t");
}

bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
}
bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
}
}

Expand Down Expand Up @@ -378,6 +392,8 @@ private void CodeToImage(object sender, DoWorkEventArgs e)
}

x++;
pixelsCurrent++;

if (x == Width)
{
x = 0;
Expand All @@ -386,17 +402,15 @@ private void CodeToImage(object sender, DoWorkEventArgs e)
else
y += 1;

bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));

if (y == Height)
{
break; // All done. Stop conversion.
}
}
pixelsCurrent++;

bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
}

e.Result = result;
e.Result = result;
}

#endregion
Expand Down Expand Up @@ -465,14 +479,6 @@ private void Btn_Convert_Click(object sender, EventArgs e)
bgWork.RunWorkerAsync(selBox_Format.SelectedItem);
break;
}

while (bgWork.IsBusy)
{
convertProgress.Increment(1);
// Keep UI messages moving, so the form remains
// responsive during the asynchronous operation.
Application.DoEvents();
}
}

private void Txt_ZoomMode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Expand Down

0 comments on commit 2136245

Please sign in to comment.