Skip to content

Commit

Permalink
Fixed weirdness when opening transparent images
Browse files Browse the repository at this point in the history
  • Loading branch information
jefff committed Jul 9, 2013
1 parent f947717 commit cbe407e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions AnimalCrossingQR/AnimalCrossingQR/AC/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public Color(byte red, byte green, byte blue)
Blue = blue;
}

public Color(byte red, byte green, byte blue, byte alpha)
: this(LerpWhite(red, alpha), LerpWhite(green, alpha), LerpWhite(blue, alpha))
{
}

private static byte LerpWhite(byte color, byte alpha)
{
return (byte)(255.0 + (color - 255.0) * ((double)alpha / 255.0));
}

public static double DistanceSquared(Color a, Color b)
{
return (a.Red - b.Red) * (a.Red - b.Red) +
Expand Down
8 changes: 6 additions & 2 deletions AnimalCrossingQR/AnimalCrossingQR/AC/Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Pattern(Image image)
gr.DrawImage(image, new Rectangle(0, 0, Width, Height));
}

BitmapData bitmapData = resizedImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData bitmapData = resizedImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] imagePixelData = new byte[Math.Abs(bitmapData.Stride) * bitmapData.Height];
Marshal.Copy(bitmapData.Scan0, imagePixelData, 0, imagePixelData.Length);
resizedImage.UnlockBits(bitmapData);
Expand All @@ -68,7 +68,11 @@ private void LoadFromPixelData(byte[] data)
Color[,] sourceImage = new Color[Width, Height];
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
sourceImage[i, j] = new Color(data[(j * Height + i) * 3 + 2], data[(j * Height + i) * 3 + 1], data[(j * Height + i) * 3 + 0]);
sourceImage[i, j] = new Color(
data[(j * Height + i) * 4 + 2],
data[(j * Height + i) * 4 + 1],
data[(j * Height + i) * 4 + 0],
data[(j * Height + i) * 4 + 3]);

HashSet<Color> bestPalette = CreateBestPalette(sourceImage);
ColorPalette = new Palette();
Expand Down

0 comments on commit cbe407e

Please sign in to comment.