Ejemplo n.º 1
0
        private static void WriteIndexed(SwfWriter writer, Bitmap bmp, bool alpha)
        {
            int w = bmp.Width;
            int h = bmp.Height;

            writer.WriteUInt8((byte)SwfBitmapFormat.Indexed);
            writer.WriteUInt16((ushort)w);
            writer.WriteUInt16((ushort)h);

            var pal     = bmp.Palette.Entries;
            int palSize = pal.Length;

            writer.WriteUInt8((byte)(palSize - 1));

            var bmpWriter = new SwfWriter();

            for (int i = 0; i < palSize; ++i)
            {
                bmpWriter.WriteColor(pal[i], alpha);
            }

            //NOTE: Row widths in the pixel data fields of these structures must be rounded up to the next
            //32-bit word boundary. For example, an 8-bit image that is 253 pixels wide must be
            //padded out to 256 bytes per line. To determine the appropriate padding, make sure to
            //take into account the actual size of the individual pixel structures; 15-bit pixels occupy 2
            //bytes and 24-bit pixels occupy 4 bytes (see PIX15 and PIX24).

            int pad = w % 4;

            if (pad != 0)
            {
                pad = 4 - pad;
            }

            using (var fbmp = new FastBitmap(bmp))
            {
                for (int y = 0; y < h; ++y)
                {
                    for (int x = 0; x < w; ++x)
                    {
                        int index = fbmp.GetIndex(x, y);
                        Debug.Assert(index >= 0 && index < palSize);
                        bmpWriter.WriteUInt8((byte)index);
                    }
                    if (pad > 0)
                    {
                        bmpWriter.Pad(pad);
                    }
                }
            }

            var data = bmpWriter.ToByteArray();

            data = Zip.Compress(data);
            writer.Write(data);
        }