示例#1
0
        public Task SetItem(uint row, uint column, Color color, ColorItem.ColorItemType itemType)
        {
            ColorItem colorItem = color;

            colorItem.ItemType = itemType;

            this.ColorItems[row, column] = colorItem;
            this.OnPixelChanged(new PixelChangedEventArgs(row, column, colorItem, this.BackgroundColor));
            return(Task.FromResult(0));
        }
示例#2
0
        public static uint GetPixelCount(this IColorMatrix colorMatrix, ColorItem.ColorItemType itemType)
        {
            uint returnValue = 0;

            for (uint row = 0; row < colorMatrix.Height; row++)
            {
                for (uint column = 0; column < colorMatrix.Width; column++)
                {
                    if (colorMatrix.ColorItems[row, column].ItemType == itemType)
                    {
                        returnValue++;
                    }
                }
            }

            return(returnValue);
        }
示例#3
0
        public static async Task <IMatrixProject> LoadFromImageAsync(this StorageFile file, uint maximumHeight, uint maximumWidth)
        {
            IMatrixProject returnValue = new MatrixProject()
            {
                ColorMatrix = new ColorMatrix(maximumWidth, maximumHeight)
            };

            using (Stream imageStream = await file.OpenStreamForReadAsync())
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream());

                // ***
                // *** Get the pixel  mapper.
                // ***
                IPixelMapperFactory factory = new PixelMapperFactory();
                IPixelMapper        mapper  = await factory.GetMapper(decoder.BitmapPixelFormat);

                PixelDataProvider data = await decoder.GetPixelDataAsync();

                byte[] bytes = data.DetachPixelData();

                uint width  = decoder.OrientedPixelWidth;
                uint height = decoder.OrientedPixelHeight;

                if (width > maximumWidth || height > maximumHeight)
                {
                    (bytes, width, height) = await decoder.ResizeImageAsync(maximumHeight, maximumWidth);
                }

                if (width <= maximumWidth && height <= maximumHeight)
                {
                    uint startColumn = (uint)((maximumWidth - width) / 2.0);
                    uint startRow    = (uint)((maximumHeight - height) / 2.0);

                    for (uint row = 0; row < height; row++)
                    {
                        for (uint column = 0; column < width; column++)
                        {
                            // ***
                            // *** Get the color from the image data.
                            // ***
                            Color color = await mapper.GetPixelAsync(bytes, row, column, width, height);

                            // ***
                            // *** The default color item type is pixel.
                            // ***
                            ColorItem.ColorItemType itemType = ColorItem.ColorItemType.Pixel;

                            if (color.A == 0 && (color.R != 0 || color.G != 0 || color.B != 0))
                            {
                                // ***
                                // *** An item with color and an alpha of 0 is a
                                // *** sand pixel.
                                // ***
                                itemType = ColorItem.ColorItemType.Sand;
                                color.A  = 255;
                            }
                            else if (color.A == 0)
                            {
                                // ***
                                // *** An item without color (black) and an alpha
                                // *** of 0 is a sand pixel.
                                // ***
                                itemType = ColorItem.ColorItemType.Background;
                            }

                            await returnValue.ColorMatrix.SetItem(row + startRow, column + startColumn, color, itemType);
                        }
                    }

                    // ***
                    // *** Read the meta data.
                    // ***
                    await returnValue.RestoreImageMetaData(file);
                }
                else
                {
                    throw new BadImageFormatException();
                }
            }

            return(returnValue);
        }
        public static async Task ReplacePixelTypeColorAsync(this IColorMatrix sourceColorMatrix, ColorItem.ColorItemType itemType, Color newColor)
        {
            for (uint row = 0; row < sourceColorMatrix.Height; row++)
            {
                for (uint column = 0; column < sourceColorMatrix.Width; column++)
                {
                    if (sourceColorMatrix.ColorItems[row, column].ItemType == itemType)
                    {
                        ColorItem newItem = new ColorItem()
                        {
                            A = newColor.A,
                            R = newColor.R,
                            G = newColor.G,
                            B = newColor.B
                        };

                        await sourceColorMatrix.SetItem(row, column, newItem);
                    }
                }
            }
        }