Esempio n. 1
0
        public void Load(string filename)
        {
            this.palfilename = filename;
            this.PaletteName = Path.GetFileNameWithoutExtension(filename);
            palette          = new PaletteColor[256];
            for (int i = 0; i < 256; i++)
            {
                palette[i] = new PaletteColor(Color.FromArgb(255, i, i, i));
            }

            if (IsTSPal(filename))
            {
                LoadTSPal(filename);
            }

            if (IsJASCPal(filename))
            {
                LoadJASCPal(filename);
            }

            if (IsPaletteSetup(filename))
            {
                CPalette tmp = Cinimanager.LoadPaletteSetup(filename)[0];
                this.palfilename = tmp.palfilename;
                this.PaletteName = tmp.PaletteName;
                this.palette     = tmp.palette;
            }
        }
Esempio n. 2
0
        public UC_Palette()
        {
            InitializeComponent();
            _Palette = null;

            this.comboBox_ColorConversionMethod.Items.Clear();
            foreach (ColorConversionMethod ccm in Enum.GetValues(typeof(ColorConversionMethod)))
            {
                this.comboBox_ColorConversionMethod.Items.Add(ccm.ToString());
            }
            this.comboBox_ColorConversionMethod.SelectedIndex = 0;

            columnCount = 8;
            rowCount    = (int)Math.Ceiling((double)256 / (double)columnCount);
            this.PaletteColorBox.SizeChanged += new EventHandler(PaletteColorBox_SizeChanged);
            this.PaletteColorBox.MouseClick  += new MouseEventHandler(PaletteColorBox_MouseClick);
            this.PaletteColorBox.MouseDown   += new MouseEventHandler(PaletteColorBox_MouseDown);
            this.PaletteColorBox.MouseMove   += new MouseEventHandler(PaletteColorBox_MouseMove);
            this.PaletteColorBox.MouseUp     += new MouseEventHandler(PaletteColorBox_MouseUp);

            ToolTip toolTip1 = new ToolTip();

            // Set up the delays for the ToolTip.
            toolTip1.AutoPopDelay = 1000000;
            toolTip1.InitialDelay = 100;
            toolTip1.ReshowDelay  = 100;
            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip1.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            toolTip1.SetToolTip(this.comboBox_Palette, "The list of different palette setups.");
            toolTip1.SetToolTip(this.button_NewCopy, "Creates a new clean palette setup.");
            toolTip1.SetToolTip(this.button_Clone, "Creates a copy of the current palette setup,\nincluding settings for ignored/transparent colors and the selected color conversion method.");
        }
Esempio n. 3
0
 public void LoadPalette(string filename)
 {
     //_Palette = new CPalette(filename);
     _Palette = PaletteManager.GetPalette(new CPalette(filename));
     UpdatePaletteManager();
     OnPaletteChanged(this, null);
 }
Esempio n. 4
0
        /// <summary>
        /// returns the index of the Palette p color, that is matching closest the input Color c
        /// </summary>
        public static int NearestColorDeltaE2000(CPalette p, Color c)
        {
            int    nearestcolor       = 0;
            double nearestcolorDeltaC = double.MaxValue;

            if ((p != null) &&
                (p.palette != null))
            {
                for (int i = 0; i < p.palette.Length; i++)
                {
                    if (p.palette[i].IsUsed)
                    {
                        if (c.A == 0)
                        {
                            nearestcolor = 0;
                            break;
                        }

                        Color  col2 = p.palette[i].Color;
                        double dC   = Tools.getDeltaE2000_viaCIELab(c, col2);
                        if (dC < nearestcolorDeltaC)
                        {
                            nearestcolor       = i;
                            nearestcolorDeltaC = dC;
                        }
                    }
                }
            }
            if (p.palette[nearestcolor].MakeTransparent)
            {
                nearestcolor = 0;
            }
            return(nearestcolor);
        }
Esempio n. 5
0
 internal void SetPaletteColor(Color c, CPalette Palette, int p)
 {
     if (!LookUp.ContainsKey(new Tuple <CPalette, Color>(Palette, c)))
     {
         LookUp.Add(new Tuple <CPalette, Color>(Palette, c), 0);
     }
 }
Esempio n. 6
0
 public static Color GetColor(CPalette p, int index)
 {
     if ((p != null) && (p.palette != null) && (index >= 0) && (index <= p.palette.Length))
     {
         return(p.palette[index].Color);
     }
     return(Color.FromArgb(255, 0, 0, 0));
 }
Esempio n. 7
0
 private void button_Clone_Click(object sender, EventArgs e)
 {
     if (_Palette != null)
     {
         _Palette = PaletteManager.GetPalette(_Palette, true);
         UpdatePaletteManager();
         OnPaletteChanged(this, null);
     }
 }
Esempio n. 8
0
 internal static void Clear(CPalette pal)
 {
     for (int i = UsedPalettes.Count - 1; i >= 0; i--)
     {
         if (UsedPalettes[i] == pal)
         {
             UsedPalettes.RemoveAt(i);
         }
     }
 }
Esempio n. 9
0
        private void button_NewCopy_Click(object sender, EventArgs e)
        {
            if (_Palette != null)
            {
                _Palette = PaletteManager.GetPalette(new CPalette(_Palette.PaletteFile));

                UpdatePaletteManager();
                OnPaletteChanged(this, null);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// produces complete crap
        /// </summary>
        public static int NearestColorHSB(CPalette p, Color c)
        {
            int    nearestcolor = 0;
            double distance     = double.MaxValue;

            float h, s, v;

            h = c.GetHue();
            s = c.GetSaturation();
            v = c.GetBrightness();

            if ((p != null) &&
                (p.palette != null))
            {
                for (int i = 0; i < p.palette.Length; i++)
                {
                    if (!p.palette[i].IsUsed)
                    {
                        continue;
                    }
                    if (c.A == 0)
                    {
                        nearestcolor = 0;
                        break;
                    }
                    float ph, ps, pv;
                    ph = p.palette[i].Color.GetHue();
                    ps = p.palette[i].Color.GetSaturation();
                    pv = p.palette[i].Color.GetBrightness();

                    double dH = (ph - h) / 360;
                    if (dH > 0.5)
                    {
                        dH = 1.0 - dH;
                    }

                    double dS = ps - s;
                    double dV = pv - v;

                    double cur_dist = Math.Sqrt(0.8 * Math.Pow(dH, 2) + 0.1 * Math.Pow(dS, 2) + 0.1 * Math.Pow(dV, 2));
                    if (cur_dist == 0)
                    {
                        nearestcolor = i;
                        break;
                    }
                    else if (cur_dist < distance)
                    {
                        distance     = cur_dist;
                        nearestcolor = i;
                    }
                }
            }
            return(nearestcolor);
        }
Esempio n. 11
0
        public void SetPalettes(CPalette[] palettes)
        {
            PaletteManager.ClearAll();
            foreach (CPalette pal in palettes)
            {
                PaletteManager.GetPalette(pal, true);
            }

            _Palette = PaletteManager.GetPalette(0);
            UpdatePaletteManager();
            OnPaletteChanged(this, null);
        }
Esempio n. 12
0
        public CPalette GetClone()
        {
            CPalette clone = new CPalette();

            clone.palette = new PaletteColor[this.palette.Length];
            for (int i = 0; i < this.palette.Length; i++)
            {
                clone.palette[i] = new PaletteColor(this.palette[i].Color, this.palette[i].IsUsed, this.palette[i].MakeTransparent);
            }

            clone.ConversionMethod = this.ConversionMethod;
            clone.palfilename      = this.palfilename;
            clone.PaletteName      = Path.GetFileNameWithoutExtension(clone.PaletteFile);
            return(clone);
        }
Esempio n. 13
0
        public static int NearestColorEuclidean(CPalette p, Color c)
        {
            int    nearestcolor = 0;
            double distance     = double.MaxValue;

            if ((p != null) &&
                (p.palette != null))
            {
                for (int i = 0; i < p.palette.Length; i++)
                {
                    if (p.palette[i].IsUsed)
                    {
                        if (c.A == 0)
                        {
                            nearestcolor = 0;
                            break;
                        }
                        double dr         = (p.palette[i].Color.R + c.R) / 2;
                        int    test_red   = (int)Math.Pow(p.palette[i].Color.R - c.R, 2.0);
                        int    test_green = (int)Math.Pow(p.palette[i].Color.G - c.G, 2.0);
                        int    test_blue  = (int)Math.Pow(p.palette[i].Color.B - c.B, 2.0);
                        int    test_alpha = (int)Math.Pow(Math.Abs(p.palette[i].Color.A - c.A), 2.5);//alpha channel difference is more prominent than others

                        double cur_dist = (2 + dr / 256) * test_red + 4 * test_green + (2 + (255 - dr) / 256) * test_blue + test_alpha;

                        //int cur_dist = 2 * test_red + 4 * test_green + 3 * test_blue;
                        if (cur_dist == 0)
                        {
                            nearestcolor = i;
                            break;
                        }
                        else if (cur_dist < distance)
                        {
                            distance     = cur_dist;
                            nearestcolor = i;
                        }
                    }
                }
            }
            if (p.palette[nearestcolor].MakeTransparent)
            {
                nearestcolor = 0;
            }
            return(nearestcolor);
        }
        private void button_LoadPalette_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title            = "Load Palette";
            ofd.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            if ((_Palette != null) && (!string.IsNullOrEmpty(_Palette.PaletteFile)))
            {
                ofd.InitialDirectory = System.IO.Path.GetDirectoryName(_Palette.PaletteFile);
            }
            ofd.FileName    = "";
            ofd.Filter      = "Palette files|*.pal";
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.Palette = new CPalette(ofd.FileName);
            }
        }
Esempio n. 15
0
            internal int GetPaletteColor(Color c, CPalette palette)
            {
                int result = 0;

                if (!LookUp.TryGetValue(new Tuple <CPalette, Color>(palette, c), out result))
                {
                    switch (palette.ConversionMethod)
                    {
                    case ColorConversionMethod.Euclidean: result = CPalette.NearestColorEuclidean(palette, c); break;

                    case ColorConversionMethod.DeltaCIE2000: result = CPalette.NearestColorDeltaE2000(palette, c); break;

                    default: result = CPalette.NearestColorEuclidean(palette, c); break;
                    }

                    LookUp.Add(new Tuple <CPalette, Color>(palette, c), result);
                }
                return(result);
            }
Esempio n. 16
0
        internal static int GetPaletteIndex(CPalette pal, bool compareFileNameOnly)
        {
            int index = -1;

            if (pal != null)
            {
                for (int i = 0; i < UsedPalettes.Count; i++)
                {
                    if ((UsedPalettes[i] != null) && (pal != null))
                    {
                        if ((compareFileNameOnly) && (UsedPalettes[i].PaletteFile == pal.PaletteFile))
                        {
                            return(index);
                        }
                        if (!compareFileNameOnly && (UsedPalettes[i] == pal))
                        {
                            return(i);
                        }
                    }
                }
            }
            return(index);
        }
Esempio n. 17
0
        /// <summary>
        /// returns the palette if it exists already, otherwise creates a new palette
        /// </summary>
        internal static CPalette GetPalette(CPalette pal, bool addDuplicate)
        {
            int index = 0;

            for (int i = 0; i < UsedPalettes.Count; i++)
            {
                if ((UsedPalettes[i] != null) && (pal != null))
                {
                    if (UsedPalettes[i].PaletteFile == pal.PaletteFile)
                    {
                        index++;
                    }
                    if ((UsedPalettes[i] == pal) && (!addDuplicate))
                    {
                        return(UsedPalettes[i]);
                    }
                }
                else
                {
                    break;
                }
            }
            if (pal != null)
            {
                if (addDuplicate)
                {
                    pal = pal.GetClone();
                }

                if (index > 0)
                {
                    pal.PaletteName = pal.PaletteName + "_" + index.ToString();
                }
                UsedPalettes.Add(pal);
            }
            return(pal);
        }
Esempio n. 18
0
        void PaletteColorBox_MouseClick(object sender, MouseEventArgs e)
        {
            Size   PaletteSpace = this.PaletteColorBox.Size;
            int    columnwidth  = (PaletteSpace.Width - 1) / columnCount;
            int    rowheight    = (PaletteSpace.Height - 1) / rowCount;
            double cellwidth    = (double)PaletteColorImage.Width / (double)columnCount;
            double cellheight   = (double)PaletteColorImage.Height / (double)rowCount;

            int selectedColumn = (int)(e.X / cellwidth);
            int selectedRow    = (int)(e.Y / cellheight);

            //the property PaletteSelectedColor launches UpdatePalettePanel when changed
            int selcol = selectedColumn * rowCount + selectedRow;

            if (selcol > 255)
            {
                selcol = 255;               //used when palette shows more than 256 colors, due to uneven columncount
            }
            if (selcol < 0)
            {
                selcol = 0;
            }
            PaletteSelectedColor = selcol;

            if (e.Button == MouseButtons.Right)
            {
                if (contextMenuEnabled &&
                    (contextMenuSaveEnabled || contextMenuLoadEnabled))
                {
                    ContextMenu contextMenu = new ContextMenu();

                    CPalette currentpalette = _Palette;
                    if (currentpalette != null)
                    {
                        string paletteinfo = currentpalette.PaletteFile;
                        if ((paletteinfo == null) || (paletteinfo == ""))
                        {
                            paletteinfo = "temporary palette";
                        }

                        MenuItem info = new MenuItem(paletteinfo);
                        info.Enabled = false;
                        contextMenu.MenuItems.Add(info);

                        MenuItem UnloadPalette = new MenuItem("Unload Palette", new System.EventHandler(this.UnloadPaletteMenuItem_Click));
                        contextMenu.MenuItems.Add(UnloadPalette);

                        contextMenu.MenuItems.Add("-");

                        MenuItem MakeTransparent = new MenuItem("Make Transparent", new System.EventHandler(this.MakeTransparentMenuItem_Click));
                        contextMenu.MenuItems.Add(MakeTransparent);

                        MenuItem IgnoreColor = new MenuItem("Ignore Color", new System.EventHandler(this.IgnoreColorMenuItem_Click));
                        contextMenu.MenuItems.Add(IgnoreColor);

                        contextMenu.MenuItems.Add("-");
                    }
                    else
                    {
                        MenuItem info = new MenuItem("no palette loaded");
                        info.Enabled = false;
                        contextMenu.MenuItems.Add(info);
                    }

                    if (contextMenuLoadEnabled)
                    {
                        MenuItem loadPalette = new MenuItem("Load Palette", new System.EventHandler(this.loadPaletteMenuItem_Click));
                        contextMenu.MenuItems.Add(loadPalette);
                    }
                    if (contextMenuSaveEnabled)
                    {
                        MenuItem savePalette = new MenuItem("Save Palette", new System.EventHandler(this.savePaletteMenuItem_Click));
                        contextMenu.MenuItems.Add(savePalette);
                    }
                    contextMenu.Show(this, new Point(e.X, e.Y));
                }
            }
            if (e.Button == MouseButtons.Left)
            {
                OnPaletteSelectedColorChanged(sender, e);
            }
        }
Esempio n. 19
0
 private void UnloadPaletteMenuItem_Click(object sender, EventArgs e)
 {
     this._Palette = null;
     this.comboBox_Palette.SelectedIndex = -1;
     UpdatePalettePanel();
 }
Esempio n. 20
0
 /// <summary>
 /// returns the palette if it exists already, otherwise creates a new palette
 /// </summary>
 /// <param name="pal"></param>
 /// <returns></returns>
 internal static CPalette GetPalette(CPalette pal)
 {
     return(GetPalette(pal, false));
 }
Esempio n. 21
0
 public LookUpColor(CPalette pal, Color col)
 {
     this.palette = pal;
     this.color   = col;
 }
Esempio n. 22
0
 private void comboBox_Palette_SelectedIndexChanged(object sender, EventArgs e)
 {
     _Palette = PaletteManager.GetPalette((CPalette)this.comboBox_Palette.SelectedItem);
     UpdatePalettePanel();
 }
Esempio n. 23
0
        /// <summary>
        /// stupid .NET image routines consider the DPI of an image, unlike every f*****g editor and image viewer on this planet
        /// Due to this stupidity, DrawImage will consider the DPI and creates garbage by resizing an image according to its DPI
        /// e.g.
        /// Image A= 100x100 pixel with DPI=96
        /// Image B= 100x100 pixel with DPI=48
        /// A.DrawImage(B) results in B being drawn in half the mainCanvasSize on the top left corner of A (AAARGH, F*****G STUPID MICROSOFT SHIT)
        /// </summary>
        public Bitmap LoadImageWithoutFuckingDPI(string file, CPalette Palette, Color CustomBackgroundColor, bool UseCustomBackgroundColor, bool InvertTransparency)
        {
            Bitmap input;   //(Bitmap)Image.FromFile(file);

            //changed to relase the file lock after the image is loaded
            //this way the image in the preview isn't locking the file and the files can be replaced/changed without closing ImageShaper

            //(Bitmap)Image.FromFile(file) is used instead of Bitmap(file), because Image.FromFile throws a FileNotFoundException when the file wasn't found
            //Bitmap(file) only throws a meaningless "invalid parameter" crap
            using (Bitmap tmp = (Bitmap)Image.FromFile(file))
            {
                input = new Bitmap(tmp);
            }
            input.SetResolution(96, 96);

            int s = Image.GetPixelFormatSize(input.PixelFormat) / 8;

            //convert all images into 32bppArgb format, that are not palette indexed and not 3 bytes (RGB) or 4 bytes (ARGB) for each pixel
            //this is pretty time consuming when done over hundreds of files, thus this tool can handle 3 byte and 4 byte per pixel color coded files and only does this conversion for other formats
            if ((input.PixelFormat != PixelFormat.Format8bppIndexed) && ((s != 3) && (s != 4)))
            {
                Bitmap tmp = new Bitmap(input.Width, input.Height, PixelFormat.Format32bppArgb);
                using (Graphics g = Graphics.FromImage(tmp))
                {
                    g.DrawImage(input, new Rectangle(0, 0, tmp.Width, tmp.Height));
                }
                input = tmp;
                s     = Image.GetPixelFormatSize(input.PixelFormat) / 8;
            }


            BitmapData input_data = input.LockBits(new Rectangle(0, 0, input.Width, input.Height),
                                                   System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                                   input.PixelFormat);

            // Copy the input_bytes from the image into a byte array
            byte[] input_bytes = new byte[input_data.Height * input_data.Stride];
            System.Runtime.InteropServices.Marshal.Copy(input_data.Scan0, input_bytes, 0, input_bytes.Length);

            input.UnlockBits(input_data);


            //after the initial edits to the input file, create/edit the palette indexed bmp file

            Bitmap output = new Bitmap(input.Width, input.Height, PixelFormat.Format8bppIndexed);

            if (Palette != null)
            {
                output.Palette = Palette.GetAsColorPalette();

                BitmapData output_data = output.LockBits(new Rectangle(0, 0, output.Width, output.Height),
                                                         System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                                         System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                // Copy the bmp_bytes from the image into a byte array
                byte[] output_bytes = new byte[output_data.Height * output_data.Stride];
                System.Runtime.InteropServices.Marshal.Copy(output_data.Scan0, output_bytes, 0, output_bytes.Length);

                Color FrameTransparentColor = CustomBackgroundColor;

                Color c = Color.FromArgb(0, 0, 0, 0);

                //if no special background color is used (which sets these pixel to Alpha=0),
                //then use the top left corner pixel as base for the background color, regardless of this pixels color value
                if (!UseCustomBackgroundColor)
                {
                    if (s == 4)
                    {
                        c = Color.FromArgb(
                            input_bytes[3],
                            input_bytes[2],
                            input_bytes[1],
                            input_bytes[0]);
                    }
                    if (s == 3)
                    {
                        c = Color.FromArgb(
                            input_bytes[2],
                            input_bytes[1],
                            input_bytes[0]);
                    }
                    FrameTransparentColor = c;
                }

                for (int x = 0; x < input.Width; x++)
                {
                    for (int y = 0; y < input.Height; y++)
                    {
                        int palette_col = 0;
                        if (input.PixelFormat != PixelFormat.Format8bppIndexed)
                        {
                            if (s == 4)
                            {
                                c = Color.FromArgb(
                                    input_bytes[(x * s) + y * input_data.Stride + 3],
                                    input_bytes[(x * s) + y * input_data.Stride + 2],
                                    input_bytes[(x * s) + y * input_data.Stride + 1],
                                    input_bytes[(x * s) + y * input_data.Stride]);
                            }
                            if (s == 3)
                            {
                                c = Color.FromArgb(
                                    input_bytes[(x * s) + y * input_data.Stride + 2],
                                    input_bytes[(x * s) + y * input_data.Stride + 1],
                                    input_bytes[(x * s) + y * input_data.Stride]);
                            }


                            if ((c.R == FrameTransparentColor.R) && (c.G == FrameTransparentColor.G) && (c.B == FrameTransparentColor.B))
                            {
                                palette_col = 0;
                            }
                            else
                            {
                                palette_col = LookUpTable.GetPaletteColor(c, Palette);
                            }
                        }
                        else //palette indexed images are loaded directly. no color conversion is done
                        {
                            palette_col = input_bytes[x + y * input_data.Stride];
                        }

                        output_bytes[x + y * output_data.Stride] = (byte)palette_col;
                    }
                }


                // Copy the input_bytes from the byte array into the image
                System.Runtime.InteropServices.Marshal.Copy(output_bytes, 0, output_data.Scan0, output_bytes.Length);
                output.UnlockBits(output_data);


                return(output);
            }
            return(input);
        }