Example #1
0
        void WriteChunk(ISerializer s, string chunkType, Action <ISerializer, int> serdes)
        {
            var chunk = IFFChunk.Serdes(0, new IFFChunk(chunkType, 0), s);

            serdes(s, chunk.Length);
            chunk.WriteLength(s);
        }
Example #2
0
 public static IFFChunk Serdes(int _, IFFChunk c, ISerializer s)
 {
     c ??= new IFFChunk();
     s.Begin();
     c.TypeId        = s.FixedLengthString(nameof(TypeId), c.TypeId, 4);
     c._lengthOffset = s.Offset;
     c.Length        = s.Int32BE(nameof(Length), c.Length);
     s.End();
     return(c);
 }
Example #3
0
 public static IFFChunk Serdes(int _, IFFChunk c, ISerializer s)
 {
     if (s == null)
     {
         throw new ArgumentNullException(nameof(s));
     }
     c ??= new IFFChunk();
     c.TypeId        = s.FixedLengthString(nameof(TypeId), c.TypeId, 4);
     c._lengthOffset = s.Offset;
     c.Length        = s.Int32BE(nameof(Length), c.Length);
     return(c);
 }
Example #4
0
        public static InterlacedBitmap Serdes(InterlacedBitmap img, ISerializer s)
        {
            img ??= new InterlacedBitmap();
            s.Begin();

            var formatChunk = IFFChunk.Serdes(0, new IFFChunk(IFFChunkType.Format, 0), s);

            if (formatChunk.TypeId != IFFChunkType.Format)
            {
                throw new NotSupportedException($"Invalid IFF header, expected \"FORM\", found \"{formatChunk.TypeId}\"");
            }

            var formatId = s.FixedLengthString("FormatId", IFFChunkType.PackedBitmap, 4);

            if (formatId != IFFChunkType.PackedBitmap)
            {
                throw new NotSupportedException($"Invalid IFF header, expected \"PBM \", found \"{formatId}\"");
            }

            if (s.Mode == SerializerMode.Reading)
            {
                int i = 0;
                while (s.BytesRemaining > 0)
                {
                    var chunk = IFFChunk.Serdes(i, null, s);
                    switch (chunk.TypeId)
                    {
                    case IFFChunkType.BitmapHeader: img.SerdesHeader(s, chunk.Length); break;

                    case IFFChunkType.ColorMapping: img.SerdesPalette(s, chunk.Length); break;

                    case IFFChunkType.Hotspot: img.SerdesHotspot(s, chunk.Length); break;

                    case IFFChunkType.ColorRanges:
                        img.ColorRanges ??= new List <ColorRange>();
                        img.ColorRanges.Add(ColorRange.Serdes(img.ColorRanges.Count, null, s));
                        break;

                    case IFFChunkType.Thumbnail: img.SerdesThumbnail(s, chunk.Length); break;

                    case IFFChunkType.Body: img.SerdesPixels(s, chunk.Length); break;

                    default:
                        s.ByteArray("Unk", null, chunk.Length);
                        break;
                    }

                    s.Check();
                    i++;
                }
            }
            else
            {
                img.WriteChunk(s, IFFChunkType.BitmapHeader, (x, n) => img.SerdesHeader(x, n));
                img.WriteChunk(s, IFFChunkType.ColorMapping, (x, n) => img.SerdesPalette(x, n));
                img.WriteChunk(s, IFFChunkType.Hotspot, (x, n) => img.SerdesHotspot(x, n));
                s.List(nameof(img.ColorRanges), img.ColorRanges, img.ColorRanges.Count, ColorRange.Serdes);
                img.WriteChunk(s, IFFChunkType.Thumbnail, (x, n) => img.SerdesThumbnail(x, n));
                img.WriteChunk(s, IFFChunkType.Body, (x, n) => img.SerdesPixels(x, n));
            }

            formatChunk.WriteLength(s);
            s.End();
            return(img);
        }