示例#1
0
        private Image FromJpeg(Blp1MipMap mipmap)
        {
            byte[] result = new byte[BLP1_JPG_HEADER_SIZE + mipmap.Data.Length];
            Buffer.BlockCopy(_jpgHeader, 0, result, 0, _jpgHeader.Length);
            Buffer.BlockCopy(mipmap.Data, 0, result, BLP1_JPG_HEADER_SIZE, mipmap.Data.Length);

            Image img;

            using (MemoryStream ms = new MemoryStream(result, false))
            {
                img = Image.FromStream(ms, false, false);
            }

            return(img);
        }
示例#2
0
        private Image FromPalette(Blp1MipMap mipmap)
        {
            Bitmap bmp = new Bitmap(mipmap.Size.Width, mipmap.Size.Height, PixelFormat.Format32bppArgb);

            byte[] mipmapData = mipmap.Data;
            int[]  result     = new int[mipmap.Size.Width * mipmap.Size.Height];
            for (int i = 0; i < result.Length; i++)
            {
                Color paletteEntry = _palette[mipmapData[i]];
                result[i] = paletteEntry.ToArgb();
            }
            BitmapData bmpData = bmp.LockBits(new Rectangle(Point.Empty, mipmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            Marshal.Copy(result, 0, bmpData.Scan0, result.Length);
            bmp.UnlockBits(bmpData);

            return(bmp);
        }
示例#3
0
        public override System.Drawing.Image GetMipmapImage(int mipmapIndex)
        {
            if (mipmapIndex < 0 || mipmapIndex >= _mipmaps.Count)
            {
                throw new ArgumentOutOfRangeException("mipmapIndex", mipmapIndex, "Index must be non-negative and less than the number of mipmaps in this BLP file.");
            }

            Blp1MipMap mipmap = _mipmaps[mipmapIndex];

            Image result = null;

            if (_compressionType == Blp1ImageType.Palette)
            {
                result = FromPalette(mipmap);
            }
            else
            {
                result = FromJpeg(mipmap);
            }

            return(result);
        }
示例#4
0
        private void ParseInternal(Stream s)
        {
            using (BinaryReader br = new BinaryReader(s))
            {
                Blp1ImageType compressionType = (Blp1ImageType)br.ReadInt32();
                int           mipmapCount     = br.ReadInt32();

                if (!Enum.IsDefined(typeof(Blp1ImageType), compressionType))
                {
                    throw new InvalidDataException("The file specified an unknown type of BLP1 compression.");
                }
                if (mipmapCount < 0)
                {
                    throw new InvalidDataException("The file specified a negative number of mipmaps, which is invalid.");
                }

                _compressionType = compressionType;

                int sizeX = br.ReadInt32();
                int sizeY = br.ReadInt32();

                Size fullImageSize = new Size(sizeX, sizeY);
                _mainSize = fullImageSize;

                br.ReadInt64(); // skip 'u2';

                int[] offsets = new int[MAX_BLP1_MIPMAP_COUNT];
                int[] sizes   = new int[MAX_BLP1_MIPMAP_COUNT];

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    offsets[i] = br.ReadInt32();
                }

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    sizes[i] = br.ReadInt32();
                }

                if (compressionType == Blp1ImageType.Palette)
                {
                    ParsePalette(br);
                }
                else
                {
                    ParseJpgHeader(s, br);
                }

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    int currentOffset = offsets[i];
                    int currentSize   = sizes[i];

                    if (currentOffset == 0 || currentSize == 0)
                    {
                        break;
                    }

                    Size   mipmapSize = GetSize(fullImageSize, i);
                    byte[] data       = new byte[currentSize];
                    s.Seek(currentOffset, SeekOrigin.Begin);
                    br.Read(data, 0, currentSize);
                    Blp1MipMap mipmap = new Blp1MipMap(mipmapSize, i, data);
                    _mipmaps.Add(mipmap);
                }
            }
        }
示例#5
0
        private void ParseInternal(Stream s)
        {
            using (BinaryReader br = new BinaryReader(s))
            {
                Blp1ImageType compressionType = (Blp1ImageType)br.ReadInt32();
                int mipmapCount = br.ReadInt32();

                if (!Enum.IsDefined(typeof(Blp1ImageType), compressionType))
                    throw new InvalidDataException("The file specified an unknown type of BLP1 compression.");
                if (mipmapCount < 0)
                    throw new InvalidDataException("The file specified a negative number of mipmaps, which is invalid.");

                _compressionType = compressionType;

                int sizeX = br.ReadInt32();
                int sizeY = br.ReadInt32();

                Size fullImageSize = new Size(sizeX, sizeY);
                _mainSize = fullImageSize;

                br.ReadInt64(); // skip 'u2';

                int[] offsets = new int[MAX_BLP1_MIPMAP_COUNT];
                int[] sizes = new int[MAX_BLP1_MIPMAP_COUNT];

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    offsets[i] = br.ReadInt32();
                }

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    sizes[i] = br.ReadInt32();
                }

                if (compressionType == Blp1ImageType.Palette)
                {
                    ParsePalette(br);
                }
                else
                {
                    ParseJpgHeader(s, br);
                }

                for (int i = 0; i < MAX_BLP1_MIPMAP_COUNT; i++)
                {
                    int currentOffset = offsets[i];
                    int currentSize = sizes[i];

                    if (currentOffset == 0 || currentSize == 0)
                        break;

                    Size mipmapSize = GetSize(fullImageSize, i);
                    byte[] data = new byte[currentSize];
                    s.Seek(currentOffset, SeekOrigin.Begin);
                    br.Read(data, 0, currentSize);
                    Blp1MipMap mipmap = new Blp1MipMap(mipmapSize, i, data);
                    _mipmaps.Add(mipmap);
                }
            }
        }
示例#6
0
        private Image FromPalette(Blp1MipMap mipmap)
        {
            Bitmap bmp = new Bitmap(mipmap.Size.Width, mipmap.Size.Height, PixelFormat.Format32bppArgb);
            byte[] mipmapData = mipmap.Data;
            int[] result = new int[mipmap.Size.Width * mipmap.Size.Height];
            for (int i = 0; i < result.Length; i++)
            {
                Color paletteEntry = _palette[mipmapData[i]];
                result[i] = paletteEntry.ToArgb();
            }
            BitmapData bmpData = bmp.LockBits(new Rectangle(Point.Empty, mipmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            Marshal.Copy(result, 0, bmpData.Scan0, result.Length);
            bmp.UnlockBits(bmpData);

            return bmp;
        }
示例#7
0
        private Image FromJpeg(Blp1MipMap mipmap)
        {
            byte[] result = new byte[BLP1_JPG_HEADER_SIZE + mipmap.Data.Length];
            Buffer.BlockCopy(_jpgHeader, 0, result, 0, _jpgHeader.Length);
            Buffer.BlockCopy(mipmap.Data, 0, result, BLP1_JPG_HEADER_SIZE, mipmap.Data.Length);

            Image img;
            using (MemoryStream ms = new MemoryStream(result, false))
            {
                img = Image.FromStream(ms, false, false);
            }

            return img;
        }