Skip to content

Commit

Permalink
Merge pull request #17 from NEstelami/master
Browse files Browse the repository at this point in the history
Optimized RemoveTransparencyFromImage function for better performance.
  • Loading branch information
ForkandBeard authored May 6, 2017
2 parents 4eee6ef + 2571226 commit 74a50c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ bin/
obj/
.svn/
*.user
*.suo
*.suo
/.vs
20 changes: 16 additions & 4 deletions ASU/BO/ImageUnpacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ImageUnpacker(Bitmap image, string fileName, bool removeTransparency)
private Bitmap RemoveTransparencyFromImage(Bitmap image)
{
Dictionary<int, Color> coloursByArgb = new Dictionary<int, Color>();
Dictionary<Point, Color> transparentPixelsByCoord = new Dictionary<Point, Color>();
Dictionary<uint, Color> transparentPixelsByCoord = new Dictionary<uint, Color>();
Dictionary<Color, Color> opaquedByTransparent = new Dictionary<Color, Color>();
Color pixel;
bool containsTransparency = false;
Expand All @@ -78,16 +78,18 @@ private Bitmap RemoveTransparencyFromImage(Bitmap image)
if (pixel.A < 255)
{
containsTransparency = true;
transparentPixelsByCoord.Add(new Point(x, y), pixel);
transparentPixelsByCoord.Add(GetHashFromCoord(new Point(x, y)), pixel);
}
}
}

if (containsTransparency)
{
foreach (Point coord in transparentPixelsByCoord.Keys)
foreach (uint coordHash in transparentPixelsByCoord.Keys)
{
transparentPixel = transparentPixelsByCoord[coord];
Point coord = GetCoordFromHash(coordHash);

transparentPixel = transparentPixelsByCoord[coordHash];
if (opaquedByTransparent.ContainsKey(transparentPixel))
{
opaquedPixel = opaquedByTransparent[transparentPixel];
Expand Down Expand Up @@ -458,5 +460,15 @@ private void HandleUnpackComplete()
UnpackingComplete();
}
}

private Point GetCoordFromHash(uint hash)
{
return new Point((int)(hash >> 16), (int)(hash & 0x0000FFFF));
}

private uint GetHashFromCoord(Point point)
{
return (uint)((point.X << 16) + point.Y);
}
}
}

0 comments on commit 74a50c4

Please sign in to comment.