コード例 #1
0
 public IEnumerable <int> Process(IEnumerable <Color> colors, IColorCache colorCache)
 {
     return(Zip(colors, Composition.GetPointSequence(_imageSize))
            .AsParallel().AsOrdered()
            .WithDegreeOfParallelism(_taskCount)
            .Select(cp => DitherColor(cp, colorCache)));
 }
コード例 #2
0
        public IColorCache GetActiveColorCache(string chosen = null)
        {
            chosen = chosen ?? Options.Get.PreRender_ColorCache;
            IColorCache result = this.colorCacheList.ContainsKey(chosen) ? this.colorCacheList[chosen] : this.colorCacheList.Values.FirstOrDefault();

            return(result);
        }
コード例 #3
0
    public Pattern(PatternEditor editor, DesignPattern pattern)
    {
        try
        {
            Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern");
            StartPreviewThread();
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Preview generator thread started!");
            _Type = pattern.Type;
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString());
            Bitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + pattern.Width + "x" + pattern.Height);
            Bitmap.Clear();
            PreviewBitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + pattern.Width + "x" + pattern.Height);
            PreviewBitmap.Clear();
            PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f));
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite");

            UpscaledPreviewBitmap = new TextureBitmap(pattern.Width * 4, pattern.Height * 4);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (pattern.Width * 4) + "x" + (pattern.Height * 4));
            UpscaledPreviewBitmap.Clear();

            Quantizer = Quantizers[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Quantizer: " + Quantizer.GetType().ToString());
            ColorCache = ColorCaches[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Color Cache: " + ColorCache.GetType().ToString());

            Editor        = editor;
            DesignPattern = pattern;
            var colors = pattern.GetPixels();

            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsing colors of pattern...");
            unsafe
            {
                var bitmapColors = Bitmap.GetColors();
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        var col = new TextureBitmap.Color(
                            (byte)(colors[x + y * Width].r * 255f),
                            (byte)(colors[x + y * Width].g * 255f),
                            (byte)(colors[x + y * Width].b * 255f),
                            (byte)(colors[x + y * Width].a * 255f)
                            );
                        *(bitmapColors + x + (Height - 1 - y) * Width) = col;
                    }
                }
            }
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsed " + (Width * Height) + " pixels.");
            Info = DesignPatternInformation.Types[pattern.Type];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained.");
        }
        catch (System.Exception e)
        {
            Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString());
        }
    }
コード例 #4
0
        public IEnumerable <int> Process(IEnumerable <Color> colors, IColorCache colorCache)
        {
            _colorCache = colorCache;

            var count           = _imageSize.Width * _imageSize.Height;
            var errorComponents = new ConcurrentDictionary <int, ColorComponentError>();
            var indices         = new int[count];

            var delayedTasks = CreateDelayedTasks(colors, errorComponents, indices);

            ParallelProcessing.ProcessParallel(delayedTasks, _taskCount, delayedLineTask => delayedLineTask.Process(ProcessingAction));

            return(indices);
        }
コード例 #5
0
        private IColorCache GetColorCache()
        {
            // if there is no cache, it attempts to create a default cache; integrated in the quantizer
            IColorCache result = colorCache ?? (colorCache = OnCreateDefaultCache());

            // if the cache exists; or default one was created for these purposes.. use it
            if (result == null)
            {
                String message = string.Format("The color cache is not initialized! Please use SetColorCache() method on quantizer.");
                throw new ArgumentNullException(message);
            }

            // cache is fine, return it
            return(result);
        }
コード例 #6
0
        private IColorCache GetColorCache()
        {
            // if there is no cache, it attempts to create a default cache; integrated in the quantizer
            IColorCache result = colorCache ?? (colorCache = OnCreateDefaultCache());

            // if the cache exists; or default one was created for these purposes.. use it
            if (result == null)
            {
                String message = string.Format("The color cache is not initialized! Please use SetColorCache() method on quantizer.");
                throw new ArgumentNullException(message);
            }

            // cache is fine, return it
            return result;
        }
コード例 #7
0
        private void ChangeColorCache()
        {
            activeColorCache = colorCacheList[(int)ColorCache];

            // applies current UI selection
            if (activeQuantizer is BaseColorCacheQuantizer quantizer)
            {
                quantizer.ChangeCacheProvider(activeColorCache);
            }

            // applies current UI selection
            if (activeColorCache is BaseColorCache colorCache)
            {
                colorCache.ChangeColorModel(activeColorModel);
            }
        }
コード例 #8
0
    public Pattern(PatternEditor editor, DesignPattern pattern)
    {
        _Type      = pattern.Type;
        Result     = new Bitmap(1, 1);
        Quantizer  = Quantizers[0];
        ColorCache = ColorCaches[0];

        Editor        = editor;
        DesignPattern = pattern;
        Colors        = pattern.GetPixels();

        Info = DesignPatternInformation.Types[pattern.Type];

        UpscaledPreviewTexture = new UnityEngine.Texture2D(Width * 4, Height * 4, UnityEngine.TextureFormat.RGB24, false);
        PreviewTexture         = new UnityEngine.Texture2D(Width, Height, UnityEngine.TextureFormat.ARGB32, false);
    }
コード例 #9
0
ファイル: MainForm.cs プロジェクト: glenray2000/FreeSo-1
        private void ChangeColorCache()
        {
            activeColorCache = colorCacheList[listColorCache.SelectedIndex];

            // enables the color model option; where available
            // listColorModel.Enabled = turnOnEvents && activeColorCache is BaseColorCache && ((BaseColorCache)activeColorCache).IsColorModelSupported;

            // applies current UI selection
            if (activeQuantizer is BaseColorCacheQuantizer)
            {
                BaseColorCacheQuantizer quantizer = (BaseColorCacheQuantizer)activeQuantizer;
                quantizer.ChangeCacheProvider(activeColorCache);
            }

            // applies current UI selection
            if (activeColorCache is BaseColorCache)
            {
                BaseColorCache colorCache = (BaseColorCache)activeColorCache;
                colorCache.ChangeColorModel(activeColorModel);
            }
        }
コード例 #10
0
ファイル: Composition.cs プロジェクト: caleb-mabry/Kuriimu2
 public static IEnumerable <int> ToIndices(this IEnumerable <Color> colors, IColorCache colorCache) =>
 colors.Select(colorCache.GetPaletteIndex);
コード例 #11
0
ファイル: Composition.cs プロジェクト: caleb-mabry/Kuriimu2
 public static IEnumerable <int> ToIndices(this Bitmap image, IColorCache colorCache) =>
 image.ToColors().ToIndices(colorCache);
コード例 #12
0
    public Pattern(PatternEditor editor, byte[] bytes)
    {
        try
        {
            Editor = editor;

            using (MemoryStream stream = new MemoryStream(bytes))
            {
                BinaryReader reader  = new BinaryReader(stream);
                byte         version = reader.ReadByte();
                var          t       = (DesignPattern.TypeEnum)reader.ReadByte();
                if (!Editor.IsPro && t != DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Simple design spot can't hold pro design.", "project");
                }
                if (Editor.IsPro && t == DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Pro design spot can't hold simple design.", "project");
                }
                this._Type = t;
                Quantizer  = Quantizers[reader.ReadByte()];
                ColorCache = ColorCaches[reader.ReadByte()];

                var info = DesignPatternInformation.Types[this._Type];
                this.SubPatterns = new List <SubPattern>();
                for (int i = 0; i < info.Parts.Count; i++)
                {
                    var subPattern = new SubPattern(this, info.Parts[i], true);
                    this.SubPatterns.Add(subPattern);

                    int layerCount = reader.ReadByte();
                    for (int j = 0; j < layerCount; j++)
                    {
                        var layerType = reader.ReadByte();
                        if (layerType == 0x01)
                        {
                            int    objectX      = reader.ReadInt32();
                            int    objectY      = reader.ReadInt32();
                            int    objectW      = reader.ReadInt32();
                            int    objectH      = reader.ReadInt32();
                            byte   crop         = reader.ReadByte();
                            byte   resampling   = reader.ReadByte();
                            int    nameLength   = reader.ReadInt32();
                            string name         = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            int    bitmapWidth  = reader.ReadInt32();
                            int    bitmapHeight = reader.ReadInt32();
                            byte[] bitmapPixels = reader.ReadBytes(bitmapWidth * bitmapHeight * 4);
                            var    bitmap       = new TextureBitmap(bitmapWidth, bitmapHeight);
                            int    m            = bitmapWidth * bitmapHeight * bitmap.PixelSize;
                            unsafe
                            {
                                byte *ptr = (byte *)bitmap.Bytes.ToPointer();
                                for (int k = 0; k < m; k++)
                                {
                                    *(ptr + k) = bitmapPixels[k];
                                }
                            }
                            if (version == 0)
                            {
                                bitmap.FlipY();
                            }
                            SmartObjectLayer layer = new SmartObjectLayer(subPattern, name, bitmap, objectX, objectY, objectW, objectH);
                            subPattern.Layers.Add(layer);
                            layer.Crop      = (TextureBitmap.CropMode)crop;
                            layer.Resampler = (ResamplingFilters)resampling;
                            layer.Bitmap    = bitmap;
                            layer.UpdateColors();
                            layer.UpdateTexture();
                        }
                        else if (layerType == 0x00)
                        {
                            int         nameLength = reader.ReadInt32();
                            string      name       = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            RasterLayer layer      = new RasterLayer(subPattern, name);

                            int    m            = layer.Width * layer.Height * layer.Texture.PixelSize;
                            byte[] bitmapPixels = reader.ReadBytes(m);
                            layer.Texture = new TextureBitmap(layer.Width, layer.Height);

                            unsafe
                            {
                                byte *ptr = (byte *)layer.Texture.Bytes.ToPointer();
                                for (int k = 0; k < m; k++)
                                {
                                    *(ptr + k) = bitmapPixels[k];
                                }
                            }
                            if (version == 0)
                            {
                                layer.Texture.FlipY();
                            }

                            layer.UpdateTexture();
                            subPattern.Layers.Add(layer);
                        }
                    }
                }
            }
            GC.Collect();
            _CurrentSubPattern = 0;

            Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern");
            StartPreviewThread();
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString());
            Bitmap = new TextureBitmap(Width, Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + Width + "x" + Height);
            Bitmap.Clear();
            PreviewBitmap = new TextureBitmap(Width, Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + Width + "x" + Height);
            PreviewBitmap.Clear();
            PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f));
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite");

            UpscaledPreviewBitmap = new TextureBitmap(Width * 4, Height * 4);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (Width * 4) + "x" + (Height * 4));
            UpscaledPreviewBitmap.Clear();

            Info = DesignPatternInformation.Types[this._Type];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained.");
        }
        catch (System.Exception e)
        {
            Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString());
            this.Dispose();
            throw e;
        }
    }
コード例 #13
0
    public void ParsePattern(ICrop crop, ISampling sampling, IColorQuantizer quantizer, IColorCache colorCache)
    {
        if (!this.IsParsing)
        {
            IsParsing = true;
            Thread thread = new Thread(() =>
            {
                var bmp = new Bitmap((System.Drawing.Image)Image.Clone());

                int desiredWidth  = 32;
                int desiredHeight = 32;
                if (crop != null)
                {
                    crop.SetImage(bmp);
                    desiredWidth  = crop.GetWidth();
                    desiredHeight = crop.GetHeight();
                }

                if (quantizer is BaseColorCacheQuantizer colorCacheQuantizer)
                {
                    colorCacheQuantizer.ChangeCacheProvider(colorCache);
                }

                var sampledBmp = sampling.Resample(bmp, desiredWidth, desiredHeight);
                bmp.Dispose();
                bmp = sampledBmp;

                Bitmap croppedBmp = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(croppedBmp))
                {
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.DrawImage(bmp, (32 - bmp.Width) / 2, (32 - bmp.Height) / 2, bmp.Width, bmp.Height);
                }

                bmp.Dispose();
                bmp = croppedBmp;

                var transparentPixels = new bool[bmp.Width * bmp.Height];
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        transparentPixels[x + y * bmp.Width] = bmp.GetPixel(x, y).A != 255;
                    }
                }
                var targetImage = ImageBuffer.QuantizeImage(bmp, quantizer, null, 15, 1);

                bmp.Dispose();
                bmp = new Bitmap(targetImage);
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        if (transparentPixels[x + y * bmp.Width])
                        {
                            bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(0, 0, 0, 0));
                        }
                    }
                }
                Result    = bmp;
                IsReady   = true;
                IsParsing = false;
            });
            thread.Start();
        }
    }
コード例 #14
0
 /// <summary>
 /// Changes the cache provider.
 /// </summary>
 /// <param name="colorCache">The color cache.</param>
 public void ChangeCacheProvider(IColorCache colorCache)
 {
     this.colorCache = colorCache;
 }
コード例 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseColorCacheQuantizer"/> class.
 /// </summary>
 protected BaseColorCacheQuantizer()
 {
     colorCache = null;
 }
コード例 #16
0
    public void FromBytes(byte[] bytes)
    {
        using (MemoryStream stream = new MemoryStream(bytes))
        {
            BinaryReader reader  = new BinaryReader(stream);
            byte         version = reader.ReadByte();
            if (version == 0x00)
            {
                var t = (DesignPattern.TypeEnum)reader.ReadByte();
                if (this._Type == DesignPattern.TypeEnum.SimplePattern && t != DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Simple design spot can't hold pro design.", "project");
                }
                if (this._Type != DesignPattern.TypeEnum.SimplePattern && t == DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Pro design spot can't hold simple design.", "project");
                }
                this._Type = t;
                Quantizer  = Quantizers[reader.ReadByte()];
                ColorCache = ColorCaches[reader.ReadByte()];

                var info = DesignPatternInformation.Types[this._Type];
                this.SubPatterns = new List <SubPattern>();
                for (int i = 0; i < info.Parts.Count; i++)
                {
                    var subPattern = new SubPattern(this, info.Parts[i], true);
                    this.SubPatterns.Add(subPattern);

                    int layerCount = reader.ReadByte();
                    for (int j = 0; j < layerCount; j++)
                    {
                        var layerType = reader.ReadByte();
                        if (layerType == 0x01)
                        {
                            int    objectX      = reader.ReadInt32();
                            int    objectY      = reader.ReadInt32();
                            int    objectW      = reader.ReadInt32();
                            int    objectH      = reader.ReadInt32();
                            byte   crop         = reader.ReadByte();
                            byte   resampling   = reader.ReadByte();
                            int    nameLength   = reader.ReadInt32();
                            string name         = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            int    bitmapWidth  = reader.ReadInt32();
                            int    bitmapHeight = reader.ReadInt32();
                            byte[] bitmapPixels = reader.ReadBytes(bitmapWidth * bitmapHeight * 4);
                            var    bitmap       = new System.Drawing.Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            bitmap.FromBytes(bitmapPixels);
                            SmartObjectLayer layer = new SmartObjectLayer(subPattern, name, bitmap, objectX, objectY, objectW, objectH);
                            subPattern.Layers.Add(layer);
                            layer.Crop      = SmartObjectLayer.Crops[crop];
                            layer.Resampler = SmartObjectLayer.Resamplers[resampling];
                            layer.Colors    = new UnityEngine.Color[layer.Width * layer.Height];
                            layer.UpdateColors();
                            layer.UpdateTexture();
                        }
                        else if (layerType == 0x00)
                        {
                            int         nameLength = reader.ReadInt32();
                            string      name       = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            RasterLayer layer      = new RasterLayer(subPattern, name);
                            layer.Colors = new UnityEngine.Color[layer.Width * layer.Height];
                            for (int y = 0; y < layer.Height; y++)
                            {
                                for (int x = 0; x < layer.Width; x++)
                                {
                                    layer.Colors[x + y * layer.Width] = new UnityEngine.Color(reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f);
                                }
                            }
                            layer.UpdateTexture();
                            subPattern.Layers.Add(layer);
                        }
                    }
                }
            }
        }
        _CurrentSubPattern = 0;
        Editor.SetSize(CurrentSubPattern.Width, CurrentSubPattern.Height);
        Editor.LayersChanged();
        Editor.SubPatternChanged(CurrentSubPattern.Part);
        Editor.SetType(this._Type);
        Editor.Show(null, null, null);
        Editor.OnImageUpdated();
        Editor.Tools.HistoryChanged(CurrentSubPattern.History);
        for (int i = 0; i < SubPatterns.Count; i++)
        {
            SubPatterns[i].UpdateImage(false);
        }
        this.RegeneratePreview();
    }
コード例 #17
0
 public void ChangeColorCache(int num)
 {
     ColorCache = ColorCaches[num];
     RegeneratePreview();
 }
コード例 #18
0
 /// <inheritdoc cref="IColorQuantizer.SetColorCache(IColorCache)"/>
 public void SetColorCache(IColorCache colorCache)
 {
     _colorCache = colorCache;
 }
コード例 #19
0
 /// <inheritdoc cref="IColorQuantizer.SetColorCache(IColorCache)"/>
 public void SetColorCache(IColorCache colorCache)
 {
     throw new NotSupportedException();
 }
コード例 #20
0
 public void ChangeColorCache(int num)
 {
     ColorCache  = ColorCaches[num];
     NeedReparse = true;
 }
コード例 #21
0
 /// <summary>
 /// Changes the cache provider.
 /// </summary>
 /// <param name="colorCache">The color cache.</param>
 public void ChangeCacheProvider(IColorCache colorCache)
 {
     this.colorCache = colorCache;
 }
コード例 #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseColorCacheQuantizer"/> class.
 /// </summary>
 protected BaseColorCacheQuantizer()
 {
     colorCache = null;
 }
コード例 #23
0
        private void ChangeColorCache()
        {
            activeColorCache = colorCacheList[listColorCache.SelectedIndex];

            // enables the color model option; where available
            // listColorModel.Enabled = turnOnEvents && activeColorCache is BaseColorCache && ((BaseColorCache)activeColorCache).IsColorModelSupported;

            // applies current UI selection
            if (activeQuantizer is BaseColorCacheQuantizer)
            {
                BaseColorCacheQuantizer quantizer = (BaseColorCacheQuantizer) activeQuantizer;
                quantizer.ChangeCacheProvider(activeColorCache);
            }

            // applies current UI selection
            if (activeColorCache is BaseColorCache)
            {
                BaseColorCache colorCache = (BaseColorCache)activeColorCache;
                colorCache.ChangeColorModel(activeColorModel);
            }
        }