public override HashSet<ChunkContainer> GetChunks(LocationPool locationPool) { HashSet<ChunkContainer> result = new HashSet<ChunkContainer>(); if (Locations.Count == 0) { return result; } if (Palette.Count() == 1) { if (IsRectangle()) { MonoRectangleChunk rectangleChunk = new MonoRectangleChunk(); rectangleChunk.Origin = BoundingBox.UpperLeft; rectangleChunk.Width = BoundingBox.Width(); rectangleChunk.Height = BoundingBox.Height(); MonoRectangleChunkContainer container = new MonoRectangleChunkContainer(); container.Chunk = rectangleChunk; container.Locations = GetLocations(BoundingBox); container.Color = Palette.Colors.First(); result.Add(container); return result; } //output a monochunk? MonoBitPlaneChunk monoColorChunk = new MonoBitPlaneChunk(); monoColorChunk.Width = BoundingBox.Width(); monoColorChunk.Height = BoundingBox.Height(); monoColorChunk.Initialize(); monoColorChunk.Origin = BoundingBox.UpperLeft; foreach (Location location in Locations) { monoColorChunk.SetColor(location.Point.X, location.Point.Y); } MonoBitPlaneChunkContainer monoContainer = new MonoBitPlaneChunkContainer(); monoContainer.Chunk = monoColorChunk; monoContainer.Locations = Locations; monoContainer.Color = Palette.Colors.First(); result.Add(monoContainer); return result; } if (IsRectangle()) { MultiColorRectangleChunkContainer container = new MultiColorRectangleChunkContainer(); ColorRectangleChunk rectangleChunk = new ColorRectangleChunk(); rectangleChunk.Origin = BoundingBox.UpperLeft; rectangleChunk.Width = BoundingBox.Width(); rectangleChunk.Height = BoundingBox.Height(); //rectangleChunk.Direction = Direction.Left; rectangleChunk.Initialize(); container.Chunk = rectangleChunk; container.Locations = Locations; rectangleChunk.Palette = Palette.GetPaletteChunk(); foreach (Location location in Locations) { rectangleChunk.SetColor(location.Color, location.Point.X, location.Point.Y); } result.Add(container); return result; } //Don't break it into further rectangles right now... just output a bitplane MultiColorBitPlaneChunkContainer chunkContainer = new MultiColorBitPlaneChunkContainer(); ColorBitPlaneChunk colorBitPlaneChunk = new ColorBitPlaneChunk(); colorBitPlaneChunk.Origin = BoundingBox.UpperLeft; colorBitPlaneChunk.Height = BoundingBox.Height(); colorBitPlaneChunk.Width = BoundingBox.Width(); colorBitPlaneChunk.Palette = Palette.GetPaletteChunk(); colorBitPlaneChunk.Initialize(); chunkContainer.Chunk = colorBitPlaneChunk; chunkContainer.Locations = Locations; foreach (Location location in Locations) { //bitmask must use local coordinates colorBitPlaneChunk.SetColor(location.Color, location.Point.X, location.Point.Y); } result.Add(chunkContainer); return result; }
public void SetData(byte[] data) { //skip label and size int index = 0; PaletteChunk paletteChunk = new DefaultPaletteChunk(); while (index < data.Length) { string label = Encoding.ASCII.GetString(data, index, 2); //case statement would work better here if (label.Equals(ColorBitPlaneChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); ColorBitPlaneChunk colorChunk = new ColorBitPlaneChunk(); colorChunk.Palette = paletteChunk; colorChunk.SetData(chunkData); Chunks.Add(colorChunk); index += size + 4; } else if (label.Equals(ColorRectangleChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); ColorRectangleChunk colorRectangleChunk = new ColorRectangleChunk(); colorRectangleChunk.Palette = paletteChunk; colorRectangleChunk.SetData(chunkData); Chunks.Add(colorRectangleChunk); index += size + 4; } else if (label.Equals(MonoBitPlaneChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); MonoBitPlaneChunk monoChunk = new MonoBitPlaneChunk(); monoChunk.Palette = paletteChunk; monoChunk.SetData(chunkData); Chunks.Add(monoChunk); index += size + 4; } else if (label.Equals(TransparentBitPlaneChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); TransparentBitPlaneChunk transparentChunk = new TransparentBitPlaneChunk(); transparentChunk.SetData(chunkData); Chunks.Add(transparentChunk); index += size + 4; } else if (label.Equals(MonoRectangleChunk.Label)) { //must be background MonoRectangleChunk chunk = new MonoRectangleChunk(); byte[] chunkData = new byte[11]; Array.Copy(data, index, chunkData, 0, chunkData.Length); chunk.Palette = paletteChunk; chunk.SetData(chunkData); Chunks.Add(chunk); index += chunkData.Length; } else if (label.Equals(TransparentRectangleChunk.Label)) { //must be background TransparentRectangleChunk chunk = new TransparentRectangleChunk(); byte[] chunkData = new byte[10]; Array.Copy(data, index, chunkData, 0, chunkData.Length); chunk.SetData(chunkData); Chunks.Add(chunk); index += chunkData.Length; } else if (label.Equals(PaletteChunk.Label)) { //must be background PaletteChunk chunk = new PaletteChunk(); int count = (data[index + 2] + 1) * 3; byte[] chunkData = new byte[count + 3]; Array.Copy(data, index, chunkData, 0, chunkData.Length); chunk.SetData(chunkData); Chunks.Add(chunk); index += chunkData.Length; paletteChunk = chunk; } else if (label.Equals(DefaultPaletteChunk.Label)) { //must be background DefaultPaletteChunk chunk = new DefaultPaletteChunk(); int count = (data[index + 2] + 1) * 3; byte[] chunkData = new byte[count + 3]; Array.Copy(data, index, chunkData, 0, chunkData.Length); chunk.SetData(chunkData); Chunks.Add(chunk); index += chunkData.Length; paletteChunk = chunk; } else if (label.Equals(GrayBitPlaneChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); GrayBitPlaneChunk chunk = new GrayBitPlaneChunk(); chunk.SetData(chunkData); Chunks.Add(chunk); index += size + 4; } else if (label.Equals(BlackWhiteBitPlaneChunk.Label)) { int size = BitConverter.ToInt16(data, index + 2); byte[] chunkData = new byte[size + 4]; Array.Copy(data, index, chunkData, 0, chunkData.Length); BlackWhiteBitPlaneChunk chunk = new BlackWhiteBitPlaneChunk(); chunk.SetData(chunkData); Chunks.Add(chunk); index += size + 4; } else if (label.Equals(BackgroundChunk.Label)) { //Console.WriteLine("default to background with label " + label); //must be background BackgroundChunk background = new BackgroundChunk(); background.Palette = paletteChunk; byte[] chunkData = new byte[3]; Array.Copy(data, index, chunkData, 0, chunkData.Length); background.SetData(chunkData); Chunks.Add(background); index += chunkData.Length; } else { Console.WriteLine("Invalid chunk label " + label); Console.WriteLine("At " + index + " " + Chunks.Count); if (Chunks.Last() != null) { Console.WriteLine("Last chunk was " + Chunks.Last().Origin.ToString()); } return; } } }