Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
0
 public PaletteEditorForm(Palette palette)
 {
     InitializeComponent();
     this.ActivePalette = palette;
     this.originalPalette = palette.Clone();
     this.CreatePanels(this.ActivePalette);
     this.LayoutPanels(this.panels);
 }
Пример #4
0
 private void ButtonPaletteSelector_Click(object sender, EventArgs e)
 {
     PaletteEditorForm editorForm = new PaletteEditorForm(this.palette ?? new Palette(this.currentBitDepth));
     if (editorForm.ShowDialog() == DialogResult.OK)
     {
         this.palette = editorForm.ActivePalette;
     }
 }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
0
 static Palette()
 {
     Palette.OneBppDefaultPalette = new Palette(BitDepth.OneBpp);
 }
Пример #11
0
 private void RadioColor_CheckedChanged(object sender, EventArgs e)
 {
     if (this.RadioColor.Checked)
     {
         if (this.Radio4Bpp.Checked)
         {
             this.palette = Palette.FourBppWindowsPalette;
         }
         else if (this.Radio8Bpp.Checked)
         {
             this.palette = Palette.EightBppColorPalette;
         }
     }
 }
Пример #12
0
 private void RadioGrayscale_CheckedChanged(object sender, EventArgs e)
 {
     if (this.RadioGrayscale.Checked)
     {
         if (this.Radio1Bpp.Checked)
         {
             this.palette = Palette.OneBppDefaultPalette;
         }
         else if (this.Radio2Bpp.Checked)
         {
             this.palette = Palette.TwoBppGrayscalePalette;
         }
         else if (this.Radio4Bpp.Checked)
         {
             this.palette = Palette.FourBppGrayscalePalette;
         }
         else if (this.Radio8Bpp.Checked)
         {
             this.palette = Palette.EightBppGrayscalePalette;
         }
     }
 }
Пример #13
0
 public abstract Bitmap Draw(string filePath, Palette palette, BackgroundWorker worker);
Пример #14
0
 public MainForm()
 {
     InitializeComponent();
     Palette paletteInit = new Palette(BitDepth.OneBpp);
     this.Radio24Bpp.Checked = true;
 }
Пример #15
0
        private void Radio8Bpp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Radio8Bpp.Checked)
            {
                this.RadioColor.Enabled = true;
                this.RadioGrayscale.Enabled = true;
                this.RadioCustom.Enabled = true;

                if (this.RadioColor.Checked)
                {
                    this.palette = Palette.EightBppColorPalette;
                }
                else if (this.RadioCustom.Checked)
                {
                    this.palette = Palette.EightBppGrayscalePalette;
                }
                else
                {
                    this.RadioGrayscale.Checked = true;
                    this.palette = Palette.EightBppGrayscalePalette;
                }

                this.drawer = new EightBppDrawer();
            }
        }
Пример #16
0
        private void Radio16Bpp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Radio16Bpp.Checked)
            {
                this.RadioColor.Enabled = true;
                this.RadioGrayscale.Enabled = false;
                this.RadioCustom.Enabled = false;

                this.palette = null;
                this.drawer = new SixteenBppDrawer();
            }
        }
Пример #17
0
        private void AssignPalettes()
        {
            hasAssignedPalettes = true;

            sixBitRange = this.GetEquidistantHexValues(64);
            fiveBitRange = this.GetEquidistantHexValues(32);

            OneBppDefaultPalette = new Palette(BitDepth.OneBpp);
            TwoBppGrayscalePalette = new Palette(BitDepth.TwoBpp);
            FourBppGrayscalePalette = new Palette(BitDepth.FourBpp);
            FourBppWindowsPalette = new Palette(BitDepth.FourBpp);
            EightBppGrayscalePalette = new Palette(BitDepth.EightBpp);
            EightBppColorPalette = new Palette(BitDepth.EightBpp);

            // Assign OneBppDefaultPalette
            OneBppDefaultPalette.SetColor(0, Color.Black);
            OneBppDefaultPalette.SetColor(1, Color.White);

            // Assign TwoBppGrayscalePalette
            TwoBppGrayscalePalette.SetColor(0, Color.Black);
            TwoBppGrayscalePalette.SetColor(1, Color.FromArgb(85, 85, 85));
            TwoBppGrayscalePalette.SetColor(2, Color.FromArgb(170, 170, 170));
            TwoBppGrayscalePalette.SetColor(3, Color.White);

            // Assign FourBppGrayscalePalette
            FourBppGrayscalePalette.SetColor(0, Color.Black);
            for (int i = 1; i <= 14; i++)
            {
                FourBppGrayscalePalette.SetColor(i, Color.FromArgb(i * 18, i * 18, i * 18));
            }
            FourBppGrayscalePalette.SetColor(15, Color.White);

            // Assign FourBppWindowsPalette
            int[] reds = new int[] { 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0xC0, 0x80, 0xFF, 0x00, 0xFF, 0x00, 0xA7, 0x00, 0xFF };
            int[] greens = new int[] { 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0xC0, 0x80, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF };
            int[] blues = new int[] { 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0xC0, 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
            for (int i = 0; i < 16; i++)
            {
                FourBppWindowsPalette.SetColor(i, Color.FromArgb(reds[i], greens[i], blues[i]));
            }

            // Assign EightBppGrayscalePalette
            for (int i = 0; i < 256; i++)
            {
                EightBppGrayscalePalette.SetColor(i, Color.FromArgb(i, i, i));
            }

            // Assign EightBppColorPalette
            var threeBitValues = this.GetEquidistantHexValues(8);
            var twoBitValues = this.GetEquidistantHexValues(4);
            for (int i = 0; i < 256; i++)
            {
                int red = (i & 0xE0) >> 5;
                int green = (i & 0x1C) >> 2;
                int blue = i & 0x03;

                EightBppColorPalette.SetColor(i, Color.FromArgb(threeBitValues[red], threeBitValues[green], twoBitValues[blue]));
            }
        }
Пример #18
0
 private void CreatePanels(Palette palette)
 {
     foreach (Color color in palette.Colors)
     {
         Panel panel = new Panel();
         panel.BackColor = color;
         panel.Size = new Size(20, 20);
         this.panels.Add(panel);
     }
 }
Пример #19
0
 public Palette Clone()
 {
     Palette clone = new Palette(this.BitDepth);
     for (int i = 0; i < this.colors.Length; i++)
     {
         clone.SetColor(i, this.colors[i]);
     }
     return clone;
 }
Пример #20
0
 private void ButtonCancel_Click(object sender, EventArgs e)
 {
     this.ActivePalette = this.originalPalette;
     this.Close();
 }
Пример #21
0
        private void Radio2Bpp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Radio2Bpp.Checked)
            {
                this.RadioColor.Enabled = false;
                this.RadioGrayscale.Enabled = true;
                this.RadioCustom.Enabled = true;

                this.RadioGrayscale.Checked = true;
                this.palette = Palette.TwoBppGrayscalePalette;

                this.drawer = new TwoBppDrawer();
            }
        }