public static void Write(BinaryWriter writer, ImageGrid grid)
        {
            if (grid == null)
            {
                writer.Write(false);
            }
            else
            {
                writer.Write(true);

                writer.Write(grid.Width);
                writer.Write(grid.Height);

                for (int x = 0; x < grid.Width; x++)
                {
                    for (int y = 0; y < grid.Height; y++)
                    {
                        IPaletteItem cell = grid[x, y];

                        if (cell == null)
                        {
                            writer.Write(false);
                        }
                        else
                        {
                            writer.Write(true);
                            writer.Write(cell.Color.ToArgb());
                        }
                    }
                }
            }
        }
Example #2
0
        private ImageGrid Load(Image originalImage, IEnumerable <IPaletteItem> palette)
        {
            var paletteByColor = palette.ToDictionary(p => p.Color);

            int requiredWidth = columns * pixelsPerCell;

            using (var resizedImage = ImageHelper.ResizeImage(originalImage, requiredWidth))
            {
                ColorHelper colorHelper = new ColorHelper(resizedImage, paletteByColor.Keys);
                int         rows        = resizedImage.Height / pixelsPerCell;

                ImageGrid grid = new ImageGrid(columns, rows);

                for (int x = 0; x < columns; x++)
                {
                    for (int y = 0; y < rows; y++)
                    {
                        Rectangle cellRect = new Rectangle(x * pixelsPerCell, y * pixelsPerCell, pixelsPerCell, pixelsPerCell);
                        grid[x, y] = paletteByColor[colorHelper.GetColor(cellRect)];
                    }
                }

                return(grid);
            }
        }
        public static ImageGrid Read(BinaryReader reader, Corner2CornerPalette palette)
        {
            bool haveGrid = reader.ReadBoolean();

            if (haveGrid)
            {
                int width  = reader.ReadInt32();
                int height = reader.ReadInt32();

                ImageGrid result = new ImageGrid(width, height);

                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (reader.ReadBoolean())
                        {
                            IPaletteItem cell;
                            palette.Find(Color.FromArgb(reader.ReadInt32()), out cell);
                            result[x, y] = cell;
                        }
                    }
                }

                return(result);
            }
            else
            {
                return(null);
            }
        }
        public static bool Load(Stream stream, Corner2CornerProject project)
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                if (!HeaderPersistence.Read(reader))
                {
                    stream.Position = 0;
                    return(false);
                }

                Image image = ImagePersistence.Read(reader);
                int   width = reader.ReadInt32();

                int paletteItemCount         = reader.ReadInt32();
                Corner2CornerPalette palette = new Corner2CornerPalette();

                for (int i = 0; i < paletteItemCount; i++)
                {
                    var pi = PaletteItemPersistence.Read(reader);
                    palette.Add(color: pi.Color, text: pi.Text);
                }

                IPaletteItem selectedPaletteItem = PaletteItemPersistence.Read(reader);
                palette.Find(selectedPaletteItem?.Color ?? Color.Empty, out selectedPaletteItem);

                ImageGrid imageGrid = ImageGridPersistence.Read(reader, palette);

                Color gridBackgroundColor = Color.FromArgb(reader.ReadInt32());

                //read everything successfully so populate the project;

                project.Image               = image;
                project.Width               = width;
                project.Palette             = palette;
                project.SelectedPaletteItem = selectedPaletteItem;
                project.ImageGrid           = imageGrid;
                project.GridBackgroundColor = gridBackgroundColor;
                project.ChangeTracking.Track(ChangeTrackingOperation.SetSaved);

                return(true);
            }
        }