<commit_before>namespace TAUtil.Gdi.Bitmap
{
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    using TAUtil.Gdi.Palette;

    /// <summary>
    /// Serializes a 32-bit Bitmap instance into raw 8-bit indexed color data.
    /// The mapping from color to index is done according to the given
    /// reverse palette lookup.
    /// </summary>
    public class BitmapSerializer
    {
        private readonly IPalette palette;

        public BitmapSerializer(IPalette palette)
        {
            this.palette = palette;
        }

        public byte[] ToBytes(Bitmap bitmap)
        {
            int length = bitmap.Width * bitmap.Height;

            byte[] output = new byte[length];

            this.Serialize(new MemoryStream(output, true), bitmap);

            return output;
        }

        public void Serialize(Stream output, Bitmap bitmap)
        {
            Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData data = bitmap.LockBits(r, ImageLockMode.ReadOnly, bitmap.PixelFormat);

            int length = bitmap.Width * bitmap.Height;

            unsafe
            {
                int* pointer = (int*)data.Scan0;
                for (int i = 0; i < length; i++)
                {
                    Color c = Color.FromArgb(pointer[i]);
                    output.WriteByte((byte)this.palette.LookUp(c));
                }
            }

            bitmap.UnlockBits(data);
        }
    }
}<commit_msg>Fix bitmap serialization with indexed format bitmaps<commit_after>namespace TAUtil.Gdi.Bitmap
{
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    using TAUtil.Gdi.Palette;

    /// <summary>
    /// Serializes a 32-bit Bitmap instance into raw 8-bit indexed color data.
    /// The mapping from color to index is done according to the given
    /// reverse palette lookup.
    /// </summary>
    public class BitmapSerializer
    {
        private readonly IPalette palette;

        public BitmapSerializer(IPalette palette)
        {
            this.palette = palette;
        }

        public byte[] ToBytes(Bitmap bitmap)
        {
            int length = bitmap.Width * bitmap.Height;

            byte[] output = new byte[length];

            this.Serialize(new MemoryStream(output, true), bitmap);

            return output;
        }

        public void Serialize(Stream output, Bitmap bitmap)
        {
            Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData data = bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            int length = bitmap.Width * bitmap.Height;

            unsafe
            {
                int* pointer = (int*)data.Scan0;
                for (int i = 0; i < length; i++)
                {
                    Color c = Color.FromArgb(pointer[i]);
                    output.WriteByte((byte)this.palette.LookUp(c));
                }
            }

            bitmap.UnlockBits(data);
        }
    }
}
{'fraction_non_alphanumeric': 0.09489528795811518, 'fraction_numerical': 0.005235602094240838, 'mean_word_length': 1.6745406824146982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984915', 'n_tokens_mistral': 853, 'n_tokens_neox': 786, 'n_words': 218}