예제 #1
0
파일: RIXFile.cs 프로젝트: ohegba/FRMout
 public RIXFile(Bitmap bmp, FRMPalette pal)
 {
     imgData = FastBMP.FixRGBImageToPalette(bmp, pal);
     width   = (UInt16)bmp.Width;
     height  = (UInt16)bmp.Height;
     palette = pal.entry;
 }
예제 #2
0
        public static byte[] FixRGBImageToPalette(Bitmap bmIn, FRMPalette pal)
        {
            byte[] indexedImage = new byte[(bmIn.Width * bmIn.Height)];
            long   idx          = 0;

            unsafe
            {
                BitmapData rawData  = bmIn.LockBits(new Rectangle(0, 0, bmIn.Width, bmIn.Height), ImageLockMode.ReadWrite, bmIn.PixelFormat);
                int        bytesPP  = System.Drawing.Bitmap.GetPixelFormatSize(bmIn.PixelFormat) / 8;
                int        bmHeight = rawData.Height;
                int        bmWidth  = rawData.Width * bytesPP;
                byte *     pointerBeginImageData = (byte *)rawData.Scan0;

                Dictionary <String, OrderedPair <Color, byte> > lookup = new Dictionary <string, OrderedPair <Color, byte> >();
                for (int y = 0; y < bmHeight; y++)
                {
                    byte *rgbBlock = pointerBeginImageData + (y * rawData.Stride);
                    for (int x = 0; x < bmWidth; x = x + bytesPP)
                    {
                        int curB = rgbBlock[x];
                        int curG = rgbBlock[x + 1];
                        int curR = rgbBlock[x + 2];


                        Color  champion = pal.entry[0];
                        double distBest = Double.MaxValue;
                        String colorKey = "#" + curR + curG + curB;
                        if (!lookup.ContainsKey(colorKey))
                        {
                            byte pdx = 0; byte championPal = 0;
                            foreach (Color refColor in pal.entry)
                            {
                                double euclid = Math.Sqrt(Math.Pow(refColor.R - curR, 2) + Math.Pow(refColor.G - curG, 2) + Math.Pow(refColor.B - curB, 2));
                                if (euclid < distBest)
                                {
                                    champion = refColor; distBest = euclid; championPal = pdx;
                                }
                                pdx++;
                            }
                            lookup[colorKey] = new OrderedPair <Color, byte>(champion, championPal);
                        }
                        else
                        {
                            champion = (Color)(lookup[colorKey])[0];
                        }

                        /* rgbBlock[x] = (byte)        champion.B;
                         * rgbBlock[x + 1] = (byte)    champion.G;
                         * rgbBlock[x + 2] = (byte)    champion.R;*/
                        indexedImage[idx] = (byte)(lookup[colorKey])[1];
                        idx++;
                    }
                }
                bmIn.UnlockBits(rawData);
            }

            return(indexedImage);
        }
예제 #3
0
파일: FRMPalette.cs 프로젝트: ohegba/FRMout
        public static FRMPalette paletteFromPALFile(String fname)
        {
            FRMPalette     pal  = new FRMPalette();
            int            bMod = (int)Utils.BrightnessMultiplier.HIGH_NOON;
            BinaryIntTrans modB = x => Math.Max(Math.Min(x.ReadByte() * bMod, 255), 0);

            using (BinaryReader br = new BinaryReader(new FileStream(fname, FileMode.Open)))
                for (int i = 0; i < 256; i++)
                {
                    pal.entry[i] = Color.FromArgb(modB(br), modB(br), modB(br));
                }

            return(pal);
        }
예제 #4
0
        public Image toBitmap(FRMPalette palette)
        {
            Bitmap bm  = new Bitmap(width, height);
            int    cnt = 0;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Color c = palette.entry[imageData[cnt]];
                    cnt++;
                    //  MessageBox.Show(c.R+" "+c.G+" "+c.B);
                    bm.SetPixel(j, i, c);
                }
            }
            return(bm);
        }
예제 #5
0
파일: FRMPalette.cs 프로젝트: ohegba/FRMout
        public static FRMPalette paletteFromGIMPFile(String fname)
        {
            FRMPalette pal = new FRMPalette();

            String line;
            int    cnt          = 0;
            Regex  rgx          = new Regex(@"\s*(\d+)\s*(\d+)\s*(\d+)", RegexOptions.IgnoreCase);
            bool   beginPalette = false;

            using (StreamReader sr = new StreamReader(new FileStream(fname, FileMode.Open)))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    //MessageBox.Show(line);
                    if (!beginPalette && line.StartsWith("#"))
                    {
                        beginPalette = true;
                        //  MessageBox.Show("Found hash.");
                        continue;
                    }

                    if (beginPalette && line.Trim().Length > 1)
                    {
                        MatchCollection matches = rgx.Matches(line);
                        if (matches.Count > 0 && matches[0].Groups.Count > 0)
                        {
                            pal.entry[cnt] = Color.FromArgb(int.Parse(matches[0].Groups[1].Value), int.Parse(matches[0].Groups[2].Value), int.Parse(matches[0].Groups[3].Value));
                            cnt++;
                        }
                    }
                }
                if (cnt != 256)
                {
                    throw new InvalidDataException("Trouble constructing the indexed colors palette, not enough colors specified in GIMP-style Palette File!");
                }
            }

            return(pal);
        }
예제 #6
0
파일: Form1.cs 프로젝트: ohegba/FRMout
        private void loadPaletteFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = ofdPaletteLoad.ShowDialog();

            if (dr != DialogResult.OK)
            {
                return;
            }

            if (ofdPaletteLoad.FileName.EndsWith(".txt"))
            {
                try
                {
                    commonColors = FRMPalette.paletteFromGIMPFile(ofdPaletteLoad.FileName);
                    toolStripStatusLabel1.Text = "Palette Successfully Loaded.";
                    pictureBox1.Image          = null;
                }
                catch (Exception eee)
                {
                    MessageBox.Show(eee.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (ofdPaletteLoad.FileName.EndsWith(".PAL"))
            {
                try
                {
                    commonColors = FRMPalette.paletteFromPALFile(ofdPaletteLoad.FileName);
                    toolStripStatusLabel1.Text = "Palette Successfully Loaded.";
                    pictureBox1.Image          = null;
                }
                catch (Exception eee)
                {
                    MessageBox.Show(eee.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }