Пример #1
0
        /// <summary>
        /// Writes a Tile bitmap (44x44) into specified file and returns structure with indexing information.
        /// </summary>
        /// <param name="stream">Stream where to write to.</param>
        /// <param name="bitmap">Bitmap to write. If null, returns invalid IndexData.</param>
        /// <returns>IndexData structure with indexing information.</returns>
        /// <remarks>It writes only diamond from source image, other is ignored.</remarks>
        public static IndexData WriteTile(Stream stream, Bitmap bitmap)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (bitmap == null)
            {
                return(IndexData.Empty);
            }

            // Check bitmap
            if (bitmap.Height != 44 || bitmap.Width != 44)
            {
                throw new ArgumentException("Bitmap has incorrect size. Only allowed is 44x44.", "bitmap");
            }

            // Create index list
            IndexData indexdata = IndexData.Empty;

            indexdata.Lookup = (uint)stream.Position;
            indexdata.Extra  = 0;

            BinaryWriter writer = new BinaryWriter(stream);

            for (int y = 0; y < 22; y++)
            {
                int x      = 22 - (y + 1);
                int lenght = (y + 1) * 2;
                for (int i = 0; i < lenght; i++)
                {
                    writer.Write(UOColorConverter.FromColor(bitmap.GetPixel(x + i, y)));
                }
            }
            for (int y = 22; y < 44; y++)
            {
                int x      = y - 22;
                int lenght = (44 - y) * 2;
                for (int i = 0; i < lenght; i++)
                {
                    writer.Write(UOColorConverter.FromColor(bitmap.GetPixel(x + i, y)));
                }
            }

            indexdata.Lenght = (uint)stream.Position - indexdata.Lookup;

            return(indexdata);
        }
Пример #2
0
        /// <summary>
        /// Writes a run-compressed bitmap into specified file and returns structure with indexing information.
        /// </summary>
        /// <param name="stream">Stream where to write to.</param>
        /// <param name="bitmap">Bitmap to write. If null, returns invalid IndexData.</param>
        /// <returns>IndexData structure with indexing information.</returns>
        public static IndexData WriteRun(Stream stream, Bitmap bitmap)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (bitmap == null)
            {
                return(IndexData.Empty);
            }

            BinaryWriter writer = new BinaryWriter(stream);

            IndexData indexdata = IndexData.Empty;

            indexdata.Lookup = (uint)writer.BaseStream.Position;
            indexdata.Extra  = 0;

            short width  = (short)bitmap.Width;
            short height = (short)bitmap.Height;

            writer.Write((int)0); // header, unknown
            writer.Write(width);
            writer.Write(height);

            long lookupTable = writer.BaseStream.Position;
            int  dataStart   = (int)writer.BaseStream.Position + height * 2;

            // Skip lookup table
            if (writer.BaseStream.Length < dataStart)
            {
                writer.BaseStream.SetLength(dataStart);
            }
            writer.Seek(dataStart, SeekOrigin.Begin);

            short y = 0;

            while (y < height)
            {
                // Write record to lookup table
                int pos = (int)writer.BaseStream.Position;

                writer.Seek((int)lookupTable, SeekOrigin.Begin);
                writer.Write((short)((pos - dataStart) / 2));
                lookupTable = writer.BaseStream.Position;

                writer.Seek(pos, SeekOrigin.Begin);

                // Write line
                int x = 0;
                while (x < width)
                {
                    short xOffset = 0;
                    short xRun    = 0;

                    // Skip invisible pixels
                    while (x < width && bitmap.GetPixel(x, y).A == 0)
                    {
                        xOffset++;
                        x++;
                    }

                    if (x == width)
                    {
                        break;
                    }

                    // Read list
                    int xPos = x;
                    while (xPos < width && bitmap.GetPixel(xPos, y).A != 0)
                    {
                        xPos++;
                        xRun++;
                    }

                    // Write chunk header
                    writer.Write(xOffset);
                    writer.Write(xRun);

                    // Write pixel list
                    while (x < width && bitmap.GetPixel(x, y).A != 0)
                    {
                        writer.Write(UOColorConverter.FromColor(bitmap.GetPixel(x, y)));
                        x++;
                    }
                }

                // End line
                writer.Write((short)0);
                writer.Write((short)0);

                y++;
            }

            indexdata.Lenght = (uint)writer.BaseStream.Position - indexdata.Lookup;

            return(indexdata);
        }