コード例 #1
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette.Colors.Count != 16)
            {
                throw new Exception("Palette mismatch.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File not found.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[] file   = File.ReadAllBytes(filePath);
            byte[] pixels = new byte[file.Length * 2];

            file.Length.Times(i =>
            {
                pixels[(i * 2)]     = (byte)(file[i] >> 4);
                pixels[(i * 2) + 1] = (byte)(file[i] & 0xF);
            });

            FourBppBitmap bitmap = new FourBppBitmap(Drawer.GetImageSize(pixels.Length), pixels, palette.Colors.ToArray <Color>());

            return(bitmap.ToBitmap(worker));
        }
コード例 #2
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette.Colors.Count != 4)
            {
                throw new Exception("Palette mismatch.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File not found.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[] bytes        = File.ReadAllBytes(filePath);
            byte[] twoBppValues = new byte[bytes.Length * 4];

            bytes.Length.Times(i =>
            {
                twoBppValues[(i * 4)]     = (byte)((bytes[i] & 0xC0) >> 6);
                twoBppValues[(i * 4) + 1] = (byte)((bytes[i] & 0x30) >> 4);
                twoBppValues[(i * 4) + 2] = (byte)((bytes[i] & 0x0C) >> 2);
                twoBppValues[(i * 4) + 3] = (byte)((bytes[i] & 0x03));
            });

            TwoBppBitmap bitmap = new TwoBppBitmap(Drawer.GetImageSize(twoBppValues.Length), twoBppValues, palette.GetColor(0), palette.GetColor(1), palette.GetColor(2), palette.GetColor(3));

            return(bitmap.ToBitmap(worker));
        }
コード例 #3
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette != null)
            {
                throw new Exception("24bpp mode does not accept palette.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File does not exist.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[] file = File.ReadAllBytes(filePath);
            int[]  pixels;

            if (file.Length % 3 == 0)
            {
                pixels = new int[file.Length / 3];
            }
            else
            {
                int pixelCount = (int)Math.Floor(file.Length / 3m);
                while (pixelCount % 3 != 0)
                {
                    pixelCount++;
                }
                pixels = new int[pixelCount];
            }

            byte red, green, blue;

            for (int i = 0; i < pixels.Length; i++)
            {
                // Pattern: xxRRGGBB
                // Red byte comes from file[i * 3] shifted by 16
                // Green byte comes from file[(i * 3) + 1] shifted by 8
                // Blue byte comes from file[(i * 3) + 2] shifted by 8
                red       = (byte)((i * 3 < file.Length) ? file[i * 3] : 0);
                green     = (byte)((((i * 3) + 1) < file.Length) ? file[(i * 3) + 1] : 0);
                blue      = (byte)((((i * 3) + 2) < file.Length) ? file[(i * 3) + 2] : 0);
                pixels[i] = (red << 16) + (green << 8) + blue;
            }

            return(new TwentyFourBppBitmap(Drawer.GetImageSize(pixels.Length), pixels).ToBitmap(worker));
        }
コード例 #4
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette != null)
            {
                throw new Exception("32bpp mode does not support palettes.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File does not exist.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[] file = File.ReadAllBytes(filePath);
            int[]  pixels;

            if (file.Length % 4 == 0)
            {
                pixels = new int[file.Length / 4];
            }
            else
            {
                int pixelCount = file.Length / 4;
                while (pixelCount % 4 != 0)
                {
                    pixelCount++;
                }
                pixels = new int[pixelCount];
            }

            byte alpha, red, green, blue;

            for (int i = 0; i < pixels.Length; i++)
            {
                alpha     = (byte)((i * 4 < file.Length) ? file[i * 4] : 0);
                red       = (byte)((((i * 4) + 1) < file.Length) ? file[(i * 4) + 1] : 0);
                green     = (byte)((((i * 4) + 2) < file.Length) ? file[(i * 4) + 2] : 0);
                blue      = (byte)((((i * 4) + 3) < file.Length) ? file[(i * 4) + 3] : 0);
                pixels[i] = (alpha << 24) + (red << 16) + (green << 8) + blue;
            }

            return(new ThirtyTwoBppBitmap(Drawer.GetImageSize(pixels.Length), pixels).ToBitmap(worker));
        }
コード例 #5
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette != null)
            {
                throw new Exception("16bpp mode doesn't accept palettes.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File not found.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[]   file = File.ReadAllBytes(filePath);
            ushort[] pixels;
            if (file.Length % 2 == 0)
            {
                pixels = new ushort[file.Length / 2];
            }
            else
            {
                pixels = new ushort[file.Length / 2 + 1];
            }

            for (int i = 0; i < file.Length; i += 2)
            {
                if (i + 1 < file.Length)
                {
                    int pixelIndex = i / 2;
                    pixels[pixelIndex] = (ushort)((file[i] << 8) + file[i + 1]);
                }
                else
                {
                    int pixelIndex = i / 2;
                    pixels[pixelIndex] = (ushort)(file[i] << 8);
                }
            }

            return(new SixteenBppBitmap(Drawer.GetImageSize(pixels.Length), pixels).ToBitmap(worker));
        }
コード例 #6
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette.Colors.Count != 2)
            {
                throw new Exception("Palette mismatch.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File not found.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            BitArray     bits   = new BitArray(File.ReadAllBytes(filePath));
            OneBppBitmap bitmap = new OneBppBitmap(Drawer.GetImageSize(bits.Length), bits, palette.GetColor(0), palette.GetColor(1));

            return(bitmap.ToBitmap(worker));
        }
コード例 #7
0
        public override Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker)
        {
            if (palette.Colors.Count != 256)
            {
                throw new Exception("Palette mismatch.");
            }

            if (!File.Exists(filePath))
            {
                throw new Exception("File not found.");
            }

            if (new FileInfo(filePath).Length == 0)
            {
                return(new Bitmap(0, 0));
            }

            byte[]         file   = File.ReadAllBytes(filePath);
            EightBppBitmap bitmap = new EightBppBitmap(Drawer.GetImageSize(file.Length), file, palette.Colors.ToArray());

            return(bitmap.ToBitmap(worker));
        }