Exemplo n.º 1
0
        private byte QuantizePixel(TPixel pixel, ref Rgba32 rgba)
        {
            if (this.Dither)
            {
                // The colors have changed so we need to use Euclidean distance caclulation to find the closest value.
                // This palette can never be null here.
                return(this.GetClosestPixel(pixel, this.palette, this.colorMap));
            }

            pixel.ToRgba32(ref rgba);
            if (rgba.Equals(default(Rgba32)))
            {
                return(this.transparentIndex);
            }

            return((byte)this.octree.GetPaletteIndex(pixel, ref rgba));
        }
Exemplo n.º 2
0
        public void Start(Func <Image <Rgba32>, bool> processor)
        {
            List <Task <Image <Rgba32> > > taskList = new List <Task <Image <Rgba32> > >();
            Image <Rgba32> image;

            taskList.Add(Task <Image <Rgba32> > .Run(Run));

            foreach (ModuleBase layerModule in _layerModules)
            {
                taskList.Add(Task <Image <Rgba32> > .Run(layerModule.Run));
            }

            Task.WaitAll(taskList.ToArray());

            Task <Image <Rgba32> > task = taskList.First();

            image = task.Result;
            taskList.Remove(task);

            foreach (Task <Image <Rgba32> > nextTask in taskList)
            {
                Image <Rgba32> nextLayer = nextTask.Result;

                for (int y = 0; y < LEDPIProcessorBase.LEDHeight; y++)
                {
                    for (int x = 0; x < LEDPIProcessorBase.LEDWidth; x++)
                    {
                        Rgba32 nextPixel = nextLayer.GetPixelRowSpan(y)[x];

                        if (!nextPixel.Equals(transparentPixel))
                        {
                            nextPixel.A = Byte.MaxValue;
                            image.GetPixelRowSpan(y)[x] = nextPixel;
                        }
                    }
                }
            }

            process(processor, image);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates a new <see cref="DrawablesOutput"/> for this <see cref="Image"/>, using all the set properties.
        /// </summary>
        /// <returns>DrawablesOutput containing the Drawables for this image.</returns>
        public DrawablesOutput Generate()
        {
            if (Image == null)
            {
                throw new DrawableException("Attempted to generate drawables for a disposed image object.");
            }

            Rgba32?ignore = IgnoreColor;

            Image <Rgba32> b = _image.Clone();

            if (FlipY)
            {
                b.Mutate(x => x.Flip(SixLabors.ImageSharp.Processing.FlipType.Vertical));
            }

            Point frameCount = new Point(
                (int)Math.Ceiling((decimal)b.Width / DrawableWidth),
                (int)Math.Ceiling((decimal)b.Height / DrawableHeight));

            Drawable[,] drawables = new Drawable[frameCount.X, frameCount.Y];

            Rgba32[,] template = GetTemplate();

            Point imagePixel = new Point(0, 0);

            // Add a drawable for every signplaceholder needed.
            for (int frameWidth = 0; frameWidth < frameCount.X; frameWidth++)
            {
                for (int frameHeight = 0; frameHeight < frameCount.Y; frameHeight++)
                {
                    imagePixel.X = frameWidth * DrawableWidth;
                    imagePixel.Y = frameHeight * DrawableHeight;

                    bool containsPixels = false;

                    StringBuilder directives = new StringBuilder("?replace");

                    for (int i = 0; i < DrawableWidth; i++)
                    {
                        for (int j = 0; j < DrawableHeight; j++)
                        {
                            int x = imagePixel.X,
                                y = imagePixel.Y++;

                            // Pixel falls within template but is outside of the supplied image.
                            if ((x > b.Width - 1 || y > b.Height - 1))
                            {
                                continue;
                            }

                            Rgba32 imageColor = b[Convert.ToInt32(x), Convert.ToInt32(y)];

                            // Pixel color is invisible or ignored.
                            if ((ignore.HasValue && imageColor.Equals(ignore)) || (imageColor.A == 0 && !ReplaceBlank))
                            {
                                continue;
                            }
                            else if (ReplaceWhite && imageColor.ToRGBAHexString() == "FFFFFFFF")
                            {
                                imageColor = new Rgba32(254, 254, 254, 255);
                            }

                            Rgba32 templateColor = template[i, j];

                            directives.AppendFormat(";{0}={1}", templateColor.ToRGBAHexString(), imageColor.ToRGBAHexString());

                            if (imageColor.A > 1)
                            {
                                containsPixels = true;
                            }
                        }

                        imagePixel.X++;
                        imagePixel.Y = frameHeight * DrawableHeight;
                    }

                    int xb = Convert.ToInt32(frameWidth * DrawableWidth),
                        yb = Convert.ToInt32(frameHeight * DrawableHeight);

                    if (containsPixels)
                    {
                        drawables[frameWidth, frameHeight] = new Drawable(directives.ToString(), xb, yb, DrawableTexture);
                    }
                }
            }

            return(new DrawablesOutput(drawables, Image.Width, Image.Height, OffsetX, OffsetY));
        }