diff --git a/src/libse/BluRaySup/BluRaySupPicture.cs b/src/libse/BluRaySup/BluRaySupPicture.cs index 736b1feb2..ca51862a1 100644 --- a/src/libse/BluRaySup/BluRaySupPicture.cs +++ b/src/libse/BluRaySup/BluRaySupPicture.cs @@ -99,6 +99,16 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup /// RLE buffer private static byte[] EncodeImage(NikseBitmap bm, List palette) { + var lookup = new Dictionary(); + for (int i = 0; i < palette.Count; i++) + { + var color = palette[i].ToArgb(); + if (!lookup.ContainsKey(color)) + { + lookup.Add(color, i); + } + } + var bytes = new List(); for (int y = 0; y < bm.Height; y++) { @@ -108,10 +118,9 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup { Color c = bm.GetPixel(x, y); byte color; - var idx = palette.IndexOf(c); - if (idx >= 0) + if (lookup.TryGetValue(c.ToArgb(), out int intC)) { - color = (byte)idx; + color = (byte)intC; } else { @@ -228,6 +237,7 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup private static List GetBitmapPalette(NikseBitmap bitmap) { var pal = new List(); + var lookup = new HashSet(); for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) @@ -235,11 +245,16 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup var c = bitmap.GetPixel(x, y); if (c != Color.Transparent) { - if (pal.Count < 100) + if (lookup.Contains(c.ToArgb())) + { + // exact color already exists + } + else if (pal.Count < 100) { if (!HasCloseColor(c, pal, 1)) { pal.Add(c); + lookup.Add(c.ToArgb()); } } else if (pal.Count < 240) @@ -247,15 +262,18 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup if (!HasCloseColor(c, pal, 5)) { pal.Add(c); + lookup.Add(c.ToArgb()); } } else if (pal.Count < 254 && !HasCloseColor(c, pal, 25)) { pal.Add(c); + lookup.Add(c.ToArgb()); } } } } + pal.Add(Color.Transparent); // last entry must be transparent return pal; }