Example #1
0
        /// <summary>
        /// Dyes whole bitmap. Non grey pixels are first converted to grey and then dyed.
        /// </summary>
        /// <param name="hue">Colors spectrum that will be used.</param>
        /// <param name="bitmap">Bitmap that will be edited.</param>
        public static void RecolorFull(HueEntry hue, Bitmap bitmap)
        {
            if (hue == null)
            {
                throw new ArgumentNullException("hue");
            }

            if (bitmap == null)
            {
                return;
            }

            if (bitmap.PixelFormat != PixelFormat.Format16bppArgb1555)
            {
                throw new ArgumentException("Invalid bitmap pixel format.", "source");
            }

            BitmapData data = bitmap.LockBits(Ultima.GetBitmapBounds(bitmap), ImageLockMode.ReadWrite, PixelFormat.Format16bppArgb1555);

            Debug.Assert(data.Stride % 2 == 0, "data.Stride % 2 == 0");

            RecolorFullInternal(hue, data.Scan0, data.Stride * data.Height / 2);

            bitmap.UnlockBits(data);
        }
Example #2
0
        /// <summary>
        /// Stores this object to specified file in Hues.mul format.
        /// </summary>
        /// <param name="file">File path.</param>
        /// <exception cref="System.ObjectDisposedException">Object has been disposed.</exception>
        public void Save(string file)
        {
            lock (syncRoot)
            {
                if (Disposed)
                {
                    throw new ObjectDisposedException("Hues");
                }

                Stream       stream = null;
                BinaryWriter writer = null;

                try
                {
                    stream = File.OpenWrite(file);
                    writer = new BinaryWriter(stream);

                    for (int i = 0; i < blockList.Count; i++)
                    {
                        writer.Write(blockList[i].Header);

                        for (int b = 0; b < 8; b++)
                        {
                            HueEntry entry = blockList[i].Entries[b];

                            for (int c = 0; c < 32; c++)
                            {
                                writer.Write(entry.Colors[c]);
                            }

                            writer.Write(entry.TableStart);
                            writer.Write(entry.TableEnd);

                            byte[] name = Encoding.ASCII.GetBytes(entry.Name);
                            Array.Resize <byte>(ref name, 20);
                            writer.Write(name, 0, name.Length);
                        }
                    }

                    Trace.WriteLine(String.Format("Hues: File \"{0}\" succesfully saved.", file), "MulLib");
                }
                catch (Exception e)
                {
                    throw new Exception("Error saving Hues.", e);
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }

                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }
        }
Example #3
0
            public Block()
            {
                entries = new HueEntry[8];

                for (int i = 0; i < 8; i++)
                {
                    entries[i] = new HueEntry();
                }
            }
Example #4
0
        private static unsafe void RecolorPartialInternal(HueEntry hue, IntPtr dataPtr, int lenght)
        {
            ushort* pData = (ushort*)dataPtr.ToPointer();

            for (int i = 0; i < lenght; i++) {
                ushort c = (ushort)((*pData) & 0x1F);
                if (c == (((*pData) >> 5) & 0x1F) && c == (((*pData) >> 10) & 0x1F)) {
                    *pData = (ushort)(((*pData) & 0x8000) | hue.Colors[c]);
                }
                pData++;
            }
        }
Example #5
0
        private static unsafe void RecolorFullInternal(HueEntry hue, IntPtr dataPtr, int lenght)
        {
            ushort* pData = (ushort*)dataPtr.ToPointer();

            for (int i = 0; i < lenght; i++) {
                ushort c = (ushort)((((*pData) & 0x1F) + (((*pData) >> 5) & 0x1F) + (((*pData) >> 10) & 0x1F)) / 3);

                *pData = (ushort)(((*pData) & 0x8000) | hue.Colors[c & 0x1F]);

                pData++;
            }
        }
Example #6
0
        private static unsafe void RecolorFullInternal(HueEntry hue, IntPtr dataPtr, int lenght)
        {
            ushort *pData = (ushort *)dataPtr.ToPointer();

            for (int i = 0; i < lenght; i++)
            {
                ushort c = (ushort)((((*pData) & 0x1F) + (((*pData) >> 5) & 0x1F) + (((*pData) >> 10) & 0x1F)) / 3);

                *pData = (ushort)(((*pData) & 0x8000) | hue.Colors[c & 0x1F]);

                pData++;
            }
        }
Example #7
0
        private static unsafe void RecolorPartialInternal(HueEntry hue, IntPtr dataPtr, int lenght)
        {
            ushort *pData = (ushort *)dataPtr.ToPointer();

            for (int i = 0; i < lenght; i++)
            {
                ushort c = (ushort)((*pData) & 0x1F);
                if (c == (((*pData) >> 5) & 0x1F) && c == (((*pData) >> 10) & 0x1F))
                {
                    *pData = (ushort)(((*pData) & 0x8000) | hue.Colors[c]);
                }
                pData++;
            }
        }
Example #8
0
        /// <summary>
        /// Dyes bitmap gray pixels.
        /// </summary>
        /// <param name="hue">Colors spectrum that will be used.</param>
        /// <param name="bitmap">Bitmap that will be edited.</param>
        public static void RecolorPartial(HueEntry hue, Bitmap bitmap)
        {
            if (hue == null)
                throw new ArgumentNullException("hue");

            if (bitmap == null)
                return;

            if (bitmap.PixelFormat != PixelFormat.Format16bppArgb1555)
                throw new ArgumentException("Invalid bitmap pixel format.", "source");

            BitmapData data = bitmap.LockBits(Ultima.GetBitmapBounds(bitmap), ImageLockMode.ReadWrite, PixelFormat.Format16bppArgb1555);
            Debug.Assert(data.Stride % 2 == 0, "data.Stride % 2 == 0");

            RecolorPartialInternal(hue, data.Scan0, data.Stride * data.Height / 2);

            bitmap.UnlockBits(data);
        }
Example #9
0
        /// <summary>
        /// Builds Hues object from file in Hues.mul format.
        /// </summary>
        /// <param name="file">File path.</param>
        public static Hues Load(string file)
        {
            Stream       stream = null;
            BinaryReader reader = null;

            try
            {
                stream = File.OpenRead(file);
                reader = new BinaryReader(stream);

                Hues hues = new Hues();

                int blockCount = (int)(stream.Length / Block.Lenght);

                for (int i = 0; i < blockCount; i++)
                {
                    uint header = reader.ReadUInt32();

                    hues.AddBlock(header);

                    for (int b = 0; b < 8; b++)
                    {
                        HueEntry entry = new HueEntry();

                        for (int c = 0; c < 32; c++)
                        {
                            entry.Colors[c] = reader.ReadUInt16();
                        }

                        entry.TableStart = reader.ReadUInt16();
                        entry.TableEnd   = reader.ReadUInt16();

                        byte[] nameBytes = reader.ReadBytes(20);
                        string name      = Encoding.ASCII.GetString(nameBytes);
                        if (name.Contains("\0"))
                        {
                            name = name.Remove(name.IndexOf('\0'));
                        }

                        entry.Name = name;

                        hues.blockList[i].Entries[b] = entry;
                    }
                }

                Trace.WriteLine(String.Format("Hues: MaxIndex={0}.", hues.MaxIndex), "MulLib");

                Trace.WriteLine(String.Format("Hues: File \"{0}\" succesfully loaded.", file), "MulLib");
                return(hues);
            }
            catch (Exception e)
            {
                throw new Exception("Error loading Hues.", e);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
Example #10
0
            public Block()
            {
                entries = new HueEntry[8];

                for (int i = 0; i < 8; i++)
                {
                    entries[i] = new HueEntry();
                }
            }
Example #11
0
        /// <summary>
        /// Builds Hues object from file in Hues.mul format.
        /// </summary>
        /// <param name="file">File path.</param>
        public static Hues Load(string file)
        {
            Stream stream = null;
            BinaryReader reader = null;

            try
            {
                stream = File.OpenRead(file);
                reader = new BinaryReader(stream);

                Hues hues = new Hues();

                int blockCount = (int)(stream.Length / Block.Lenght);

                for (int i = 0; i < blockCount; i++)
                {
                    uint header = reader.ReadUInt32();

                    hues.AddBlock(header);

                    for (int b = 0; b < 8; b++)
                    {
                        HueEntry entry = new HueEntry();

                        for (int c = 0; c < 32; c++)
                            entry.Colors[c] = reader.ReadUInt16();

                        entry.TableStart = reader.ReadUInt16();
                        entry.TableEnd = reader.ReadUInt16();

                        byte[] nameBytes = reader.ReadBytes(20);
                        string name = Encoding.ASCII.GetString(nameBytes);
                        if (name.Contains("\0"))
                            name = name.Remove(name.IndexOf('\0'));

                        entry.Name = name;

                        hues.blockList[i].Entries[b] = entry;
                    }
                }

                Trace.WriteLine(String.Format("Hues: MaxIndex={0}.", hues.MaxIndex), "MulLib");

                Trace.WriteLine(String.Format("Hues: File \"{0}\" succesfully loaded.", file), "MulLib");
                return hues;
            }
            catch (Exception e)
            {
                throw new Exception("Error loading Hues.", e);
            }
            finally
            {
                if (reader != null)
                    reader.Close();

                if (stream != null)
                    stream.Close();
            }
        }