Exemplo n.º 1
0
        private static void WritePixelData(BinaryWriter writer, Color[] pixels, int depth, int width, int height, bool palette)
        {
            switch (depth)
            {
            case 32:
            {
                if (palette)
                {
                    writer.Write(pixels);
                }
                else
                {
                    for (int y = height - 1; y >= 0; y--)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            writer.Write(pixels[x + y * width]);
                        }
                    }
                }
            }
            break;

            case 24:
            {
                if (palette)
                {
                    foreach (Color color in pixels)
                    {
                        writer.Write(color.B);
                        writer.Write(color.G);
                        writer.Write(color.R);
                    }
                }
                else
                {
                    for (int y = height - 1; y >= 0; y--)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            Color pixel = pixels[x + y * width];
                            writer.Write(pixel.B);
                            writer.Write(pixel.G);
                            writer.Write(pixel.R);
                        }
                    }
                }
            }
            break;

            case 16:
            {
                if (palette)
                {
                    foreach (Color color in pixels)
                    {
                        ushort px = 0;

                        if (color.A >= 0x7F)
                        {
                            BitHelper.SetBit(ref px, 15);
                        }

                        BitHelper.ClearAndSetBits(ref px, 5, (ushort)(color.R >> 3), 0);
                        BitHelper.ClearAndSetBits(ref px, 5, (ushort)(color.G >> 3), 5);
                        BitHelper.ClearAndSetBits(ref px, 5, (ushort)(color.B >> 3), 10);

                        writer.Write(px);
                    }
                }
                else
                {
                    for (int y = height - 1; y >= 0; y--)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            ushort px = 0;

                            if (pixels[x + y * width].A >= 0x7F)
                            {
                                BitHelper.SetBit(ref px, 15);
                            }

                            BitHelper.ClearAndSetBits(ref px, 5, (ushort)(pixels[x + y * width].R >> 3), 0);
                            BitHelper.ClearAndSetBits(ref px, 5, (ushort)(pixels[x + y * width].G >> 3), 5);
                            BitHelper.ClearAndSetBits(ref px, 5, (ushort)(pixels[x + y * width].B >> 3), 10);

                            writer.Write(px);
                        }
                    }
                }
            }
            break;
            }
        }