public override void Read(BinaryReader br, List <AoWBitmap> imageList)
        {
            // length of this header
            UInt32 dword = br.ReadUInt32();

            if (dword != length)
            {
                throw new Exception(string.Format("Wrong header v3 length ({0})", dword));
            }

            numPalette = br.ReadUInt32();
            Debug.Assert(numPalette == 0);

            // read info data
            AoWBitmap model = PersistentData.Data.AoW1Default;
            AoWBitmap elem  = null;

            while (true)
            {
                elem = new AoW1Bitmap(model);
                if (!elem.ReadImageInfo(br))
                {
                    // End description section
                    Debug.Assert(elem.ImageNumber == -1);                     // 0xFFFFFFFF);
                    break;
                }
                // be sure there is enough space
                while (elem.ImageNumber >= imageList.Count)
                {
                    imageList.Add(null);
                }
                imageList[elem.ImageNumber] = elem;
            }
            // no ReadImageData, because header v3 should have no distinct sections
        }
        }         // end OpenIlb

        public override bool MakeIlb(string fileName, List <AoWBitmap> imageList)
        {
            AoWBitmap first     = null;
            AoWBitmap first8bbp = null;

            foreach (AoWBitmap elem in imageList)
            {
                if (elem != null)
                {
                    if (first == null)
                    {
                        first = elem;
                    }
                    if (elem.Resized.PixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        first8bbp = elem;
                        break;
                    }
                }
            }
            if (first == null)
            {
                return(false);
            }

            try
            {
                using (MemoryStream tempMemory = new MemoryStream())
                {
                    using (MemoryStream imageMemory = new MemoryStream())
                    {
                        BinaryWriter tempStream  = new BinaryWriter(tempMemory);
                        BinaryWriter imageStream = new BinaryWriter(imageMemory);

                        // set the number of palettes (0 - 1 for this editor):
                        UInt32 nrOfPalettes = first8bbp != null ? 1u : 0u;
                        // write the palette
                        if (nrOfPalettes > 0)
                        {
                            // write the type of palette (always RGB):
                            tempStream.Write(0x88801B18);
                            ColorPalette palette = first8bbp.Resized.Palette;
                            foreach (Color c in palette.Entries)
                            {
                                tempStream.Write((Byte)c.R);
                                tempStream.Write((Byte)c.G);
                                tempStream.Write((Byte)c.B);
                                tempStream.Write((Byte)0);
                            }
                            // be sure to write 256 entries
                            for (int i = palette.Entries.Length; i < 256; ++i)
                            {
                                tempStream.Write(0);
                            }
                        }

                        // Write image info data and bitmap data to separate streams:
                        int imageNumber = 0;
                        foreach (AoWBitmap elem in imageList)
                        {
                            if (elem != null)
                            {
                                if (elem.ImageNumber != imageNumber)
                                {
                                    elem.ImageNumber = imageNumber;
                                }
                                elem.WriteImage(tempStream, imageStream);
                            }
                            ++imageNumber;
                        }
                        tempStream.Write(0xFFFFFFFF);                         // End description section

                        AoW1HeaderV4 header = new AoW1HeaderV4();
                        // guess this may be true
                        header.unknown1        = (nrOfPalettes > 0) ? 0x0040DB00u : 0x0040F000u;
                        header.imageDataOffset = header.length + 4u + (UInt32)tempStream.BaseStream.Length;
                        header.ilbFileSize     = header.imageDataOffset + (UInt32)imageStream.BaseStream.Length;
                        header.numPalette      = nrOfPalettes;

                        using (FileStream fs = new FileStream(fileName, FileMode.Create))
                        {
                            BinaryWriter bw = new BinaryWriter(fs);

                            // Assemble the library file:
                            // First: header record ...
                            header.Write(bw);

                            // Second: palettes and image info data ...
                            tempMemory.WriteTo(fs);
                            // Finally: image bitmap data ...
                            imageMemory.WriteTo(fs);

                            // Logger.LogMessage(MsgLevel.INFO, string.Format("{0} - Made. ({1})", Path.GetFileName(fileName), fileName));
                            Debug.WriteLine(string.Format("{0} - Made. ({1})", Path.GetFileName(fileName), fileName));
                            return(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Logger.LogMessage(MsgLevel.FAULT, ex.Message);
                //Logger.LogMessage(MsgLevel.FAULT, string.Format("{0} - Failure. ({1})", Path.GetFileName(fileName), fileName));
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(string.Format("{0} - Failure. ({1})", Path.GetFileName(fileName), fileName));
            }
            return(false);
        }
        public override void Read(BinaryReader br, List <AoWBitmap> imageList)
        {
            // length of this header
            UInt32 dword = br.ReadUInt32();

            if (dword != length)
            {
                throw new Exception(string.Format("Wrong header v4 length ({0})", dword));
            }

            imageDataOffset = br.ReadUInt32();
            ilbFileSize     = br.ReadUInt32();
            Trace.WriteLine(string.Format("Ilb File Size {0}", ilbFileSize));

            numPalette = br.ReadUInt32();
            List <ColorPalette> palettes = new List <ColorPalette>();

            for (UInt32 i = 0; i < numPalette; ++i)
            {
                palettes.Add(ReadPalette(br));
            }
            Trace.WriteLine(string.Format("Read {0} palettes", palettes.Count));
            // Logger.LogMessage(MsgLevel.DEBUG, string.Format("Ilb contains {0} palettes", palettes.Count));
            Debug.WriteLine(string.Format("Ilb contains {0} palettes", palettes.Count));

            // read info data
            AoWBitmap model = PersistentData.Data.AoW1Default;

            AoWBitmap elem = null;

            while (true)
            {
                elem = new AoW1Bitmap(model);
                if (!elem.ReadImageInfo(br))
                {
                    // End description section
                    if (elem.ImageNumber == -1)                     // 0xFFFFFFFF);
                    {
                        break;
                    }
                    else
                    {
                        // read a blank image probably
                        br.ReadUInt32();                         // throw away next dword
                        continue;
                    }
                }
                // be sure there is enough space
                while (elem.ImageNumber >= imageList.Count)
                {
                    imageList.Add(null);
                }
                imageList[elem.ImageNumber] = elem;
            }

            foreach (AoWBitmap elem1 in imageList)
            {
                if (elem1 != null)
                {
                    elem1.ReadImageData(br, palettes);
                }
            }
        }
 public AoW1Bitmap(AoWBitmap src)
     : base(src)
 {
 }
Beispiel #5
0
        public override bool OpenIlb(string fileName, List <AoWBitmap> imageList)
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                BinaryReader br = new BinaryReader(fs);

                // first byte is the number of following word infos
                UInt32 wordFields = br.ReadByte();
                // the format of this header is weird:
                // because the choose to use word,
                // they may be too little to store address offset so dwords may be required.
                // In this case wordFields is 0x8n, and an additional dword follows
                // to specify the number of dword*2 infos
                UInt32 dwordFields = 0;
                if (wordFields > 0x80)
                {
                    wordFields -= 0x80;
                    dwordFields = br.ReadUInt32();
                }

                // the infos are made by two fields
                // 1 - an id
                // 2 - the offset you find their data AFTER this list of info
                SortedList <UInt32, UInt32> infoMap = new SortedList <UInt32, UInt32>();

                // Store them in a container
                for (int i = 0; i < wordFields; ++i)
                {
                    UInt32 id = br.ReadByte();
                    infoMap.Add(id, br.ReadByte());
                }
                for (int i = 0; i < dwordFields; ++i)
                {
                    UInt32 id = br.ReadUInt32();
                    infoMap.Add(id, br.ReadUInt32());
                }

                /*
                 * 0x0A ???
                 * 0x0B max number of elements - dword
                 * 0x0C palettes		-  ?
                 * 0x32+ image header	- vary
                 */
                if (infoMap.ContainsKey(0x0A))
                {
                    UInt32 unknown = br.ReadUInt32();
                    infoMap.Remove(0x0A);
                }

                // read the max number of elements
                // The images may have discontinued id (i.e.: 0 - 1 -3 - 5)
                // so the length of the list that should contain them all
                // should be large to contains gap,
                // then the max is the following dword
                if (infoMap.ContainsKey(0x0B))
                {
                    UInt32 lastElemNumber = br.ReadUInt32();
                    infoMap.Remove(0x0B);
                }

                List <ColorPalette> palettes = new List <ColorPalette>();
                if (infoMap.ContainsKey(0x0C))
                {
                    // read unknown data
                    // For now I've no clue on meaning of interposed data,
                    // but should be the palettes
                    UInt32 unknownLength = infoMap.Values[1] - infoMap[0x0C];
                    br.ReadBytes(0x6);                     // seem useless
                    UInt32 numPalette = br.ReadByte();
                    for (UInt32 i = 0; i < numPalette; ++i)
                    {
                        br.ReadBytes(0xF);                         // seem useless
                        palettes.Add(AoW1Header.ReadPalette(br));
                    }
                    infoMap.Remove(0x0C);
                }

                // read image headers
                AoWBitmap model = PersistentData.Data.AoW1Default;

                foreach (UInt32 id in infoMap.Keys)
                {
                    AoWBitmap elem = new AoW2Bitmap(model);
                    elem.ImageNumber = (int)(id - 0x32u);
                    if (!elem.ReadImageInfo(br))
                    {
                        Debug.Assert(false);
                    }
                    // be sure there is enough space
                    while (elem.ImageNumber >= imageList.Count)
                    {
                        imageList.Add(null);
                    }
                    imageList[elem.ImageNumber] = elem;
                }

                // read image data
                long startOffset = br.BaseStream.Position;
                foreach (AoW2Bitmap elem in imageList)
                {
                    if (elem != null)
                    {
                        if (br.BaseStream.Position != startOffset + (long)elem.ImageOffset)
                        {
                            br.BaseStream.Position = startOffset + (long)elem.ImageOffset;
                        }
                        elem.ReadImageData(br, palettes);
                    }
                }

                // now convert to AoW1 Type
                for (int i = 0; i < imageList.Count; ++i)
                {
                    AoW2Bitmap elem = (AoW2Bitmap)imageList[i];
                    if (elem != null)
                    {
                        // the copy constructor make all the works
                        imageList[i] = new AoW1Bitmap(elem);
                        elem.Dispose();
                    }
                }
            }
            return(true);
        }         // end OpenIlb