Beispiel #1
0
    public override Chunk parse(BinaryReader reader)
    {
        PaletteChunk chunk = new PaletteChunk();

        chunk.bytesInChunk    = reader.ReadInt32();
        chunk.bytesInChildren = reader.ReadInt32();

        for (long i = 0; i < 256; i++)
        {
            float r = reader.ReadByte() / 255f;
            float g = reader.ReadByte() / 255f;
            float b = reader.ReadByte() / 255f;
            float a = reader.ReadByte() / 255f;

            chunk.colors[i] = new Color(r, g, b, a);
        }

        long currentPosition = reader.BaseStream.Position;

        while (reader.BaseStream.Position < currentPosition + chunk.bytesInChildren)
        {
            chunk.children.Add(Chunk.createChunk(reader));
        }

        return(chunk);
    }
Beispiel #2
0
        public void Clear()
        {
            Header = null;
            Frames = null;

            paletteChunk = null;
            tagsChunk    = null;
        }
Beispiel #3
0
        public override Color GetColor()
        {
            PaletteChunk palette = Frame.GetChunk <PaletteChunk>();

            if (palette != null)
            {
                return(palette.GetColor(Index));
            }
            else
            {
                return(Color.magenta);
            }
        }
Beispiel #4
0
        private void ProcessChunk(byte[] chunkBytes)
        {
            var chunkTypeString = PngChunk.GetChunkTypeString(chunkBytes.Skip(4).Take(4).ToArray());

            if (!(chunkTypeString == "IHDR"))
            {
                if (!(chunkTypeString == "PLTE"))
                {
                    if (!(chunkTypeString == "tRNS"))
                    {
                        if (!(chunkTypeString == "IDAT"))
                        {
                            return;
                        }

                        var dataChunk = new DataChunk();
                        dataChunk.Decode(chunkBytes);
                        dataChunks.Add(dataChunk);
                    }
                    else
                    {
                        var transparencyChunk = new TransparencyChunk();
                        transparencyChunk.Decode(chunkBytes);
                        palette.AddAlphaToColors(transparencyChunk.PaletteTransparencies);
                    }
                }
                else
                {
                    var paletteChunk = new PaletteChunk();
                    paletteChunk.Decode(chunkBytes);
                    palette = paletteChunk.Palette;
                    chunks.Add(paletteChunk);
                }
            }
            else
            {
                var headerChunk = new HeaderChunk();
                headerChunk.Decode(chunkBytes);
                width  = (int)headerChunk.Width;
                height = (int)headerChunk.Height;

                bitsPerSample = headerChunk.BitDepth;
                colorType     = headerChunk.ColorType;
                chunks.Add(headerChunk);
            }
        }
Beispiel #5
0
    private static List <Material> ImportColors(Chunk mainChunk)
    {
        List <Material> colorMaterials = new List <Material>();

        for (int i = 0; i < mainChunk.children.Count; i++)
        {
            Chunk chunk = mainChunk.children[i];
            if (chunk is PaletteChunk)
            {
                PaletteChunk palette = (PaletteChunk)chunk;
                foreach (Color c in palette.colors)
                {
                    Material m = new Material(Shader.Find("Standard"));
                    m.color = c;
                    colorMaterials.Add(m);
                }
            }
        }

        return(colorMaterials);
    }
Beispiel #6
0
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     BackgroundChunk.ColorIndex = (byte)paletteChunk.Colors.IndexOf(Color);
 }
        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;
                }
            }
        }
Beispiel #8
0
        public void ResetPalette(PaletteChunk newPalette)
        {
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    SxzColor color = Palette.Colors[GetByteIndex(x , y)];
                    SetByteIndex((byte)newPalette.Colors.IndexOf(color), x, y);
                }
            }

            this.Palette = newPalette;
        }
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     ColorBitPlaneChunk.ResetPalette(paletteChunk);
 }
Beispiel #10
0
        public PaletteChunk GetPaletteChunk()
        {
            if (Default)
            {
                return null;
            }

            PaletteChunk result = new PaletteChunk();

            List<SxzColor> colorList = Colors.ToList();
            colorList.Sort(new PaletteChunk.ColorSorter());

            foreach (SxzColor color in colorList)
            {
                result.Colors.Add(color);
            }

            return result;
        }
Beispiel #11
0
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     //does nothing for transparent chunks
 }
Beispiel #12
0
 public abstract void SetIndex(PaletteChunk paletteChunk);
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     ColorRectangleChunk.ResetPalette(paletteChunk);
 }
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     MonoRectangleChunk.ColorIndex = (byte)paletteChunk.Colors.IndexOf(Color);
 }
Beispiel #15
0
        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;
                }
            }
        }
 public override void SetIndex(PaletteChunk paletteChunk)
 {
     MonoBitPlaneChunk.ColorIndex = (byte)paletteChunk.Colors.IndexOf(Color);
 }