예제 #1
0
 /// <summary>
 /// Builds the color mapping by adding all colors in the given table, and also resolving lookups according to the given alpha map.
 /// </summary>
 /// <param name="colorTable">The colors that should be added to the current lookup mapping.</param>
 /// <param name="alphaMap">The alpha mapping that specifies new ARGB colors that should replace any given RGB colors in the color
 /// table.</param>
 private void BuildColorMap(ArgbColor[] colorTable, AlphaRemappingTable alphaMap)
 {
     foreach (ArgbColor sourceArgbColor in colorTable)
         if (sourceArgbColor.A == 255)
         {
             RgbColor sourceRgbColor = (RgbColor)sourceArgbColor;
             Color sourceColor = Color.FromArgb(sourceRgbColor.ToArgb());
             if (!colorMappingTable.ContainsKey(sourceColor))
             {
                 ArgbColor desiredArgbColor;
                 if (!alphaMap.TryGetMapping(sourceRgbColor, out desiredArgbColor))
                     desiredArgbColor = sourceArgbColor;
                 colorMappingTable.Add(sourceColor, Color.FromArgb(desiredArgbColor.ToArgb()));
             }
         }
 }
 /// <summary>
 /// Creates a new <see cref="T:DesktopSprites.SpriteManagement.BitmapFrame"/> from the raw buffer, loading extra transparency
 /// information and adjusting the colors as required by the transparency.
 /// </summary>
 /// <param name="buffer">The raw buffer.</param>
 /// <param name="palette">The color palette.</param>
 /// <param name="transparentIndex">The index of the transparent color.</param>
 /// <param name="stride">The stride width of the buffer.</param>
 /// <param name="width">The logical width of the buffer.</param>
 /// <param name="height">The logical height of the buffer.</param>
 /// <param name="depth">The bit depth of the buffer.</param>
 /// <param name="hashCode">The hash code of the frame.</param>
 /// <param name="fileName">The path to the GIF file being loaded.</param>
 /// <returns>A new <see cref="T:DesktopSprites.SpriteManagement.BitmapFrame"/> for the frame held in the raw buffer.</returns>
 private BitmapFrame BitmapFrameFromBuffer(byte[] buffer, RgbColor[] palette, int transparentIndex,
     int stride, int width, int height, int depth, int hashCode, string fileName)
 {
     return Disposable.SetupSafely(
         BitmapFrame.FromBuffer(buffer, palette, transparentIndex, stride, width, height, depth, hashCode),
         frame =>
         {
             if (IsAlphaBlended)
             {
                 // Check for an alpha remapping table, and apply it if one exists.
                 string mapFilePath = Path.ChangeExtension(fileName, AlphaRemappingTable.FileExtension);
                 if (File.Exists(mapFilePath))
                 {
                     AlphaRemappingTable map = new AlphaRemappingTable();
                     map.LoadMap(mapFilePath);
                     ColorPalette colorPalette = frame.Image.Palette;
                     for (int i = 0; i < colorPalette.Entries.Length; i++)
                     {
                         Color color = colorPalette.Entries[i];
                         ArgbColor argbColor;
                         if (map.TryGetMapping(new RgbColor(color.R, color.G, color.B), out argbColor))
                             colorPalette.Entries[i] = Color.FromArgb(argbColor.ToArgb());
                     }
                     frame.Image.Palette = colorPalette;
                     // We only need to pre-multiply when we add an alpha channel ourselves, as GIFs only support 1 bit transparency.
                     frame.Image.PreMultiplyAlpha();
                 }
             }
             else
             {
                 frame.Image.RemapColors(paletteMapping);
             }
         });
 }