Example #1
0
        private static bool CanUseColormap(IMagickImage <QuantumType> image, bool lossless)
        {
            if (image.ClassType == ClassType.Pseudo)
            {
                return(true);
            }

            var histogram = image.Histogram();

            if (histogram.Count > 256)
            {
                return(false);
            }

            if (!image.HasAlpha)
            {
                return(true);
            }

            var max = Quantum.Max;
            var min = (QuantumType)(Quantum.Max * 0.125);

            if (lossless)
            {
                min = 0;
            }
            else
            {
                max -= min;
            }

            bool fixAlpha = false;

            foreach (var color in histogram.Keys)
            {
                if (color.A != 0 && color.A != Quantum.Max)
                {
                    fixAlpha = true;
                    if (color.A > min && color.A < max)
                    {
                        return(false);
                    }
                }
            }

            if (fixAlpha)
            {
                FixAlpha(image, min, max);
            }

            return(true);
        }
Example #2
0
        private static (string paletteString, Dictionary <string, int> paletteIndex) GeneratePaletteInfo(
            IMagickImage image)
        {
            var matchedColors = image.Histogram();

            var paletteString = string.Empty;
            var paletteIndex  = new Dictionary <string, int>();
            var position      = 0;

            foreach (var matchedColor in matchedColors)
            {
                var inGameValue = ColorStrings.Dictionary[matchedColor.Key.ToString()];
                paletteString += inGameValue;
                paletteIndex[matchedColor.Key.ToString()] = position;
                position++;
            }

            paletteString = paletteString.PadRight(30, '0');
            return(paletteString, paletteIndex);
        }