Exemplo n.º 1
0
        public static TemplateDoc CloneTemplateDoc(TemplateDoc tmpdSrc)
        {
            // This should probably be on ICloneable::Clone() on TemplateDoc

            TemplateDoc tmpdDst = (TemplateDoc)DocManager.NewDocument(typeof(TemplateDoc), new Object[] { tmpdSrc.TileSize });

            DocManager.SetActiveDocument(typeof(TemplateDoc), tmpdSrc);

            Template[] atmplSrc          = tmpdSrc.GetTemplates();
            Template   tmplSrcBackground = tmpdSrc.GetBackgroundTemplate();
            Template   tmplDstBackground = null;
            ArrayList  alsTmplDst        = new ArrayList();

            foreach (Template tmplSrc in atmplSrc)
            {
                Template tmplDst = new Template(tmpdDst, tmplSrc.Name);
                tmplDst.OccupancyMap = tmplSrc.OccupancyMap;
                tmplDst.TerrainMap   = tmplSrc.TerrainMap;
                tmplDst.Bitmap       = (Bitmap)tmplSrc.Bitmap.Clone();
                alsTmplDst.Add(tmplDst);
                if (tmplSrc == tmplSrcBackground)
                {
                    tmplDstBackground = tmplDst;
                }
            }
            if (tmplDstBackground != null)
            {
                tmpdDst.SetBackgroundTemplate(tmplDstBackground);
            }
            tmpdDst.AddTemplates((Template[])alsTmplDst.ToArray(typeof(Template)));
            Palette palSrc = tmpdSrc.GetPalette();

            if (palSrc != null)
            {
                tmpdDst.SetPalette(palSrc, false);
            }

            return(tmpdDst);
        }
Exemplo n.º 2
0
        public static void QuantizeTemplates(TemplateDoc tmpd, Palette palFixed, int cPalEntries, int cPalEntriesFixed, int cPalEntriesBackground)
        {
            // Load the fixed palette. The result will be 24 bit templates normalized to a palette of 256 colors,
            // the "fixed" palette plus a quantized palette.

            if (palFixed == null) {
                palFixed = Palette.OpenDialog(null);
                if (palFixed == null) {
                    MessageBox.Show(DocManager.GetFrameParent(), "Must have the fixed color palette to continue!");
                    return;
                }

                switch (palFixed.Length) {
                case 16:
                    cPalEntries = 16;
                    cPalEntriesFixed = 16;
                    cPalEntriesBackground = 0;
                    break;

                case 256:
                    cPalEntries = 256;
                    cPalEntriesFixed = 128;
                    cPalEntriesBackground = 32;
                    break;
                }
            }

            // Quantize loop. Designed to make optimal use of the lower 128 fixed colors

            // Quantize background separately from foreground

            Template tmplBackground = tmpd.GetBackgroundTemplate();
            if (tmplBackground != null && cPalEntriesBackground != 0) {
                // Create a despeckled hue map of the background. We'll use this to
                // subtract background from foreground so we can quantize foreground separately

                Bitmap bmHueBackground = MakeHueMap(tmplBackground.Bitmap);
                DespeckleGrayscaleBitmap(bmHueBackground, 9, 50);

                // Calc mean and standard deviation for filtering purposes

                double nMean = CalcGrayscaleMean(bmHueBackground);
                double nStdDev = CalcGrayscaleStandardDeviation(bmHueBackground, nMean);

                // Add extract & quantize the background pixels

                ArrayList alsColorsBackground = new ArrayList();
                AddTemplateColors(alsColorsBackground, tmplBackground.Bitmap);
                palFixed = QuantizeColors(alsColorsBackground, palFixed, cPalEntriesFixed + cPalEntriesBackground, cPalEntriesFixed);
                cPalEntriesFixed += cPalEntriesBackground;

                // Now extract foreground pixels by first subtracting background pixels

                ArrayList alsColorsForeground = new ArrayList();
                Template[] atmpl = tmpd.GetTemplates();
                foreach (Template tmpl in atmpl) {
                    if (tmpl == tmplBackground)
                        continue;
                    Bitmap bmT = MakeHueMap(tmpl.Bitmap);
                    DespeckleGrayscaleBitmap(bmT, 9, 50);
                    SubtractGrayscaleDistribution(bmT, nMean, nStdDev);
                    for (int y = 0; y < bmT.Height; y++) {
                        for (int x = 0; x < bmT.Width; x++) {
                            Color clr = bmT.GetPixel(x, y);
                            if (clr != Color.FromArgb(255, 0, 255))
                                bmT.SetPixel(x, y, tmpl.Bitmap.GetPixel(x, y));
                        }
                    }
                    AddTemplateColors(alsColorsForeground, bmT);
                }

                // Now quantize foreground pixels
                // Set the palette and color match

                Palette palNew = QuantizeColors(alsColorsForeground, palFixed, cPalEntries, cPalEntriesFixed);
                tmpd.SetPalette(palNew, true);
            } else {
                // No background template; just quantize everything together

                Template[] atmpl = tmpd.GetTemplates();
                ArrayList alsColors = new ArrayList();
                foreach (Template tmpl in atmpl)
                    AddTemplateColors(alsColors, tmpl.Bitmap);

                // Now quantize foreground pixels
                // Set the palette and color match

                Palette palNew = QuantizeColors(alsColors, palFixed, cPalEntries, cPalEntriesFixed);
                tmpd.SetPalette(palNew, true);
            }
        }
Exemplo n.º 3
0
        public static void QuantizeTemplates(TemplateDoc tmpd, Palette palFixed, int cPalEntries, int cPalEntriesFixed, int cPalEntriesBackground)
        {
            // Load the fixed palette. The result will be 24 bit templates normalized to a palette of 256 colors,
            // the "fixed" palette plus a quantized palette.

            if (palFixed == null)
            {
                palFixed = Palette.OpenDialog(null);
                if (palFixed == null)
                {
                    MessageBox.Show(DocManager.GetFrameParent(), "Must have the fixed color palette to continue!");
                    return;
                }

                switch (palFixed.Length)
                {
                case 16:
                    cPalEntries           = 16;
                    cPalEntriesFixed      = 16;
                    cPalEntriesBackground = 0;
                    break;

                case 256:
                    cPalEntries           = 256;
                    cPalEntriesFixed      = 128;
                    cPalEntriesBackground = 32;
                    break;
                }
            }

            // Quantize loop. Designed to make optimal use of the lower 128 fixed colors

            // Quantize background separately from foreground

            Template tmplBackground = tmpd.GetBackgroundTemplate();

            if (tmplBackground != null && cPalEntriesBackground != 0)
            {
                // Create a despeckled hue map of the background. We'll use this to
                // subtract background from foreground so we can quantize foreground separately

                Bitmap bmHueBackground = MakeHueMap(tmplBackground.Bitmap);
                DespeckleGrayscaleBitmap(bmHueBackground, 9, 50);

                // Calc mean and standard deviation for filtering purposes

                double nMean   = CalcGrayscaleMean(bmHueBackground);
                double nStdDev = CalcGrayscaleStandardDeviation(bmHueBackground, nMean);

                // Add extract & quantize the background pixels

                ArrayList alsColorsBackground = new ArrayList();
                AddTemplateColors(alsColorsBackground, tmplBackground.Bitmap);
                palFixed          = QuantizeColors(alsColorsBackground, palFixed, cPalEntriesFixed + cPalEntriesBackground, cPalEntriesFixed);
                cPalEntriesFixed += cPalEntriesBackground;

                // Now extract foreground pixels by first subtracting background pixels

                ArrayList  alsColorsForeground = new ArrayList();
                Template[] atmpl = tmpd.GetTemplates();
                foreach (Template tmpl in atmpl)
                {
                    if (tmpl == tmplBackground)
                    {
                        continue;
                    }
                    Bitmap bmT = MakeHueMap(tmpl.Bitmap);
                    DespeckleGrayscaleBitmap(bmT, 9, 50);
                    SubtractGrayscaleDistribution(bmT, nMean, nStdDev);
                    for (int y = 0; y < bmT.Height; y++)
                    {
                        for (int x = 0; x < bmT.Width; x++)
                        {
                            Color clr = bmT.GetPixel(x, y);
                            if (clr != Color.FromArgb(255, 0, 255))
                            {
                                bmT.SetPixel(x, y, tmpl.Bitmap.GetPixel(x, y));
                            }
                        }
                    }
                    AddTemplateColors(alsColorsForeground, bmT);
                }

                // Now quantize foreground pixels
                // Set the palette and color match

                Palette palNew = QuantizeColors(alsColorsForeground, palFixed, cPalEntries, cPalEntriesFixed);
                tmpd.SetPalette(palNew, true);
            }
            else
            {
                // No background template; just quantize everything together

                Template[] atmpl     = tmpd.GetTemplates();
                ArrayList  alsColors = new ArrayList();
                foreach (Template tmpl in atmpl)
                {
                    AddTemplateColors(alsColors, tmpl.Bitmap);
                }

                // Now quantize foreground pixels
                // Set the palette and color match

                Palette palNew = QuantizeColors(alsColors, palFixed, cPalEntries, cPalEntriesFixed);
                tmpd.SetPalette(palNew, true);
            }
        }