Пример #1
0
        private void SetTextureFormat(int mipMapCount, BitmapFormat format, BitmapFlags flags)
        {
            if (mipMapCount > 0)
            {
                MipMapCount = mipMapCount;
                Flags      |= DDSFlags.MipMapCount;
            }
            else
            {
                MipMapCount = 0;
            }

            if (flags.HasFlag(BitmapFlags.Compressed))
            {
                Flags |= DDSFlags.LinearSize;
                int blockSize      = BitmapFormatUtils.GetBlockSize(format);
                int blockDimension = BitmapFormatUtils.GetBlockDimension(format);
                var nearestWidth   = blockDimension * ((Height + (blockDimension - 1)) / blockDimension);
                var nearestHeight  = blockDimension * ((Width + (blockDimension - 1)) / blockDimension);;
                PitchOrLinearSize = (nearestWidth * nearestHeight / 16) * blockSize;
            }
            else
            {
                Flags |= DDSFlags.Pitch;
                int bitsPerPixel = BitmapFormatUtils.GetBitsPerPixel(format);
                PitchOrLinearSize = (Width * bitsPerPixel + 7) / 8;
            }

            PixelFormat = new PixelFormat(format, flags);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new <see cref="HaloBitmap"/> instance using the supplied object index entry.
        /// </summary>
        /// <param name="entry">The object index entry that contains the bitmap tag group data.</param>
        /// <exception cref="ArgumentNullException"><paramref name="entry"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="entry"/> is not a bitmap.</exception>
        public HaloBitmap(IndexEntry entry)
        {
            //Check
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            else if (entry.Root != HaloTags.bitm)
            {
                throw new ArgumentException("Index entry is not bitmap.", nameof(entry));
            }

            //Setup
            this.entry = entry;
            tag        = new BitmapTag(entry);
            maps       = new Bitmap[tag.Bitmaps.Length][][];

            //Setup Property Accessors
            bitmaps = new BitmapProperties[tag.Bitmaps.Length];
            for (int i = 0; i < tag.Bitmaps.Length; i++)
            {
                bitmaps[i] = new BitmapProperties(this, i);
            }
            sequences = new SequenceProperties[tag.Sequences.Length];
            for (int i = 0; i < tag.Sequences.Length; i++)
            {
                sequences[i] = new SequenceProperties(this, i);
            }

            //Check
            if (tag == null)
            {
                return;
            }
            else if (tag.Bitmaps.Length == 0)
            {
                return;
            }

            //Loop through bitmaps
            for (int k = 0; k < tag.Bitmaps.Length; k++)
            {
                //Setup
                BitmapTagGroup.Bitmap bitmap = tag.Bitmaps[k];
                maps[k] = new Bitmap[6][];

                //Loop through LODs
                byte[] sourceData = null;
                for (int l = 0; l < 6; l++)
                {
                    //Get source data
                    if (bitmap.rawOffsets[l] != uint.MaxValue)
                    {
                        RawLocation rawLocation = (RawLocation)(bitmap.rawOffsets[l] & 0xC0000000);
                        if (rawLocation == RawLocation.Local)
                        {
                            sourceData = entry.Raws[RawSection.Bitmap][(int)bitmap.rawOffsets[l]].GetBuffer();
                        }
                        else
                        {
                            string filelocation = string.Empty;
                            int    rawOffset    = (int)(bitmap.rawOffsets[l] & (uint)RawLocation.LocalMask);
                            switch (rawLocation)
                            {
                            case RawLocation.Mainmenu:
                                filelocation = HaloSettings.MainmenuPath;
                                break;

                            case RawLocation.Shared:
                                filelocation = HaloSettings.SharedPath;
                                break;

                            case RawLocation.SinglePlayerShared:
                                filelocation = HaloSettings.SingleplayerSharedPath;
                                break;
                            }

                            //Check
                            if (File.Exists(filelocation))
                            {
                                using (FileStream fs = new FileStream(filelocation, FileMode.Open))
                                    using (BinaryReader mapReader = new BinaryReader(fs))
                                    {
                                        fs.Seek(rawOffset, SeekOrigin.Begin);
                                        sourceData = mapReader.ReadBytes(bitmap.rawLengths[l]);
                                    }
                            }
                        }
                    }

                    //Set
                    if (sourceData.Length == 0)
                    {
                        continue;
                    }

                    //Prepare
                    BitmapFlags  flags        = (BitmapFlags)bitmap.flags;
                    BitmapFormat format       = (BitmapFormat)bitmap.format;
                    PixelFormat  bitmapFormat = PixelFormat.Format32bppArgb;
                    int          sourceBits   = 32;
                    switch (format)
                    {
                    case BitmapFormat.A8:
                    case BitmapFormat.Y8:
                    case BitmapFormat.P8Bump:
                    case BitmapFormat.P8:
                    case BitmapFormat.Ay8: sourceBits = 8; break;

                    case BitmapFormat.A8y8:
                    case BitmapFormat.A1r5g5b5:
                    case BitmapFormat.A4r4g4b4:
                    case BitmapFormat.V8u8:
                    case BitmapFormat.G8b8:
                    case BitmapFormat.R5g6b5: sourceBits = 16; break;

                    case BitmapFormat.Dxt1: sourceBits = 4; break;

                    case BitmapFormat.Dxt5:
                    case BitmapFormat.Dxt3: sourceBits = 8; break;

                    case BitmapFormat.Argbfp32: sourceBits = 128; break;
                    }

                    //Handle
                    switch (format)
                    {
                    case BitmapFormat.R5g6b5: bitmapFormat = PixelFormat.Format16bppRgb565; break;

                    case BitmapFormat.A1r5g5b5: bitmapFormat = PixelFormat.Format16bppArgb1555; break;

                    case BitmapFormat.X8r8g8b8: bitmapFormat = PixelFormat.Format32bppRgb; break;

                    case BitmapFormat.P8Bump: bitmapFormat = PixelFormat.Format8bppIndexed; break;

                    case BitmapFormat.P8: bitmapFormat = PixelFormat.Format8bppIndexed; break;
                    }

                    //Prepare
                    int width = bitmap.width, height = bitmap.height;
                    if (flags.HasFlag(BitmapFlags.Linear))
                    {
                        width = (int)Math.Ceiling(width / 16f) * 16;
                    }

                    //Loop LOD
                    for (int i = 0; i < l; i++)
                    {
                        width  /= 2;
                        height /= 2;
                    }

                    //Prepare
                    int mapWidth = width, mapHeight = height, location = 0;
                    int mipmapCount = bitmap.mipmapCount;
                    maps[k][l] = new Bitmap[mipmapCount + 1];
                    Size   bitmapSize = Size.Empty;
                    byte[] mapData    = null;

                    //Loop
                    for (int i = 1; i <= mipmapCount + 1; i++)
                    {
                        //Prepare
                        int mapIndex = i - 1;
                        mapWidth = width; mapHeight = height;
                        for (int j = 1; j < i; j++)
                        {
                            mapWidth  /= 2;
                            mapHeight /= 2;
                        }

                        //Get Size
                        bitmapSize = new Size(mapWidth, mapHeight);

                        //Check
                        if (bitmapSize.Width == 0 || bitmapSize.Height == 0)
                        {
                            continue;
                        }

                        //Create Map
                        int mapStride = mapWidth * sourceBits / 8;
                        int mapSize   = mapStride * mapHeight;

                        //Ehh?
                        switch (format)
                        {
                        case BitmapFormat.Dxt1: mapSize = Math.Max(mapSize, 8); break;

                        case BitmapFormat.Dxt3:
                        case BitmapFormat.Dxt5: mapSize = Math.Max(mapSize, 16); break;

                        case BitmapFormat.P8:
                        case BitmapFormat.P8Bump: mapSize = Math.Max(mapSize, 16); break;

                        default: mapSize = Math.Max(mapSize, 1); break;
                        }

                        mapData = new byte[mapSize];
                        if (location + mapSize > sourceData.Length)
                        {
                            continue;
                        }
                        Array.Copy(sourceData, location, mapData, 0, mapSize);

                        //Deswizzle?
                        if ((flags & BitmapFlags.Swizzled) == BitmapFlags.Swizzled)
                        {
                            mapData = Swizzler.Swizzle(mapData, mapWidth, mapHeight, bitmap.depth, sourceBits, true);
                        }
                        if (mapData == null)
                        {
                            mapData = new byte[mapSize];
                        }

                        using (Bitmap map = new Bitmap(mapWidth, mapHeight, bitmapFormat))
                        {
                            unsafe
                            {
                                //Lock Bits
                                BitmapData data = map.LockBits(new Rectangle(0, 0, mapWidth, mapHeight), ImageLockMode.ReadWrite, bitmapFormat);

                                //Prepare Buffer
                                byte[] bitmapData = new byte[data.Stride * data.Height];
                                int    dataLength = Math.Min(bitmapData.Length, mapSize);

                                //Handle Format...
                                switch (format)
                                {
                                case BitmapFormat.A8:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = 255;
                                        bitmapData[x * 4 + 1] = 255;
                                        bitmapData[x * 4 + 2] = 255;
                                        bitmapData[x * 4 + 3] = mapData[x];
                                    }
                                    break;

                                case BitmapFormat.Y8:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = mapData[x];
                                        bitmapData[x * 4 + 1] = mapData[x];
                                        bitmapData[x * 4 + 2] = mapData[x];
                                        bitmapData[x * 4 + 3] = 255;
                                    }
                                    break;

                                case BitmapFormat.Ay8:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = mapData[x];
                                        bitmapData[x * 4 + 1] = mapData[x];
                                        bitmapData[x * 4 + 2] = mapData[x];
                                        bitmapData[x * 4 + 3] = mapData[x];
                                    }
                                    break;

                                case BitmapFormat.A8y8:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = mapData[x * 2];
                                        bitmapData[x * 4 + 1] = mapData[x * 2];
                                        bitmapData[x * 4 + 2] = mapData[x * 2];
                                        bitmapData[x * 4 + 3] = mapData[x * 2 + 1];
                                    }
                                    break;

                                case BitmapFormat.A4r4g4b4:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = (byte)(mapData[x * 2 + 0] & 0xF0);
                                        bitmapData[x * 4 + 1] = (byte)(mapData[x * 2 + 0] & 0x0F);
                                        bitmapData[x * 4 + 2] = (byte)(mapData[x * 2 + 1] & 0xF0);
                                        bitmapData[x * 4 + 3] = (byte)(mapData[x * 2 + 1] & 0x0F);
                                    }
                                    break;

                                case BitmapFormat.P8Bump:
                                case BitmapFormat.P8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break;

                                case BitmapFormat.R5g6b5: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break;

                                case BitmapFormat.A1r5g5b5: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break;

                                case BitmapFormat.X8r8g8b8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break;

                                case BitmapFormat.A8r8g8b8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break;

                                case BitmapFormat.Dxt1:
                                    if (mapWidth >= 4 && mapHeight >= 4)
                                    {
                                        S3TC.DecompressDxt1(ref bitmapData, mapData, bitmapSize);
                                    }
                                    break;

                                case BitmapFormat.Dxt3:
                                    if (mapWidth >= 4 && mapHeight >= 4)
                                    {
                                        S3TC.DecompressDxt3(ref bitmapData, mapData, bitmapSize);
                                    }
                                    break;

                                case BitmapFormat.Dxt5:
                                    if (mapWidth >= 4 && mapHeight >= 4)
                                    {
                                        S3TC.DecompressDxt5(ref bitmapData, mapData, bitmapSize);
                                    }
                                    break;

                                case BitmapFormat.Argbfp32:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 8) * 255f), 255f);
                                        bitmapData[x * 4 + 1] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 4) * 255f), 255f);
                                        bitmapData[x * 4 + 2] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16) * 255f), 255f);
                                        bitmapData[x * 4 + 3] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 12) * 255f), 255f);
                                    }
                                    break;

                                case BitmapFormat.Rgbfp32: break;

                                case BitmapFormat.Rgbfp16: break;

                                case BitmapFormat.V8u8:
                                    for (int x = 0; x < mapWidth * mapHeight; x++)
                                    {
                                        bitmapData[x * 4 + 0] = 255;
                                        bitmapData[x * 4 + 1] = (byte)(127 + (sbyte)mapData[x * 2 + 1]);
                                        bitmapData[x * 4 + 2] = (byte)(127 + (sbyte)mapData[x * 2]);
                                        bitmapData[x * 4 + 3] = 255;
                                    }
                                    break;

                                case BitmapFormat.G8b8: break;
                                }

                                //Copy
                                Marshal.Copy(bitmapData, 0, data.Scan0, bitmapData.Length);
                                map.UnlockBits(data);

                                //Setup Palettes
                                if (format == BitmapFormat.P8Bump)
                                {
                                    map.SetNormalMapPalette();
                                }
                                else if (format == BitmapFormat.P8)
                                {
                                    map.SetGrayscalePalette();
                                }

                                //Set
                                location += mapSize;
                            }

                            //Draw into cropped image
                            maps[k][l][mapIndex] = new Bitmap(bitmap.width, bitmap.height, bitmapFormat);
                            using (Graphics g = Graphics.FromImage(maps[k][l][mapIndex])) g.DrawImage(map, Point.Empty);
                        }
                    }
                }
            }
        }
Пример #3
0
        public void SetTextureFormat(BitmapFormat format, BitmapFlags flags)
        {
            if (flags.HasFlag(BitmapFlags.Compressed))
            {
                Flags |= DDSPixelFormatFlags.Compressed;
                switch (format)
                {
                case BitmapFormat.Dxt5:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x35545844;
                    break;

                case BitmapFormat.Dxt3:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x33545844;
                    break;

                case BitmapFormat.Dxt1:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x31545844;
                    break;

                case BitmapFormat.Dxn:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x32495441;
                    break;

                default:
                    throw new Exception($"Unsupported bitmap format {format}");
                }
            }
            else
            {
                Flags      |= DDSPixelFormatFlags.RGB;
                RGBBitCount = BitmapFormatUtils.GetBitsPerPixel(format);

                switch (format)
                {
                case BitmapFormat.A8:
                case BitmapFormat.AY8:
                    ABitMask = 0xFF;
                    Flags   |= DDSPixelFormatFlags.Alpha;
                    break;

                case BitmapFormat.Y8:
                    RBitMask = 0xFF;
                    Flags   |= DDSPixelFormatFlags.Luminance;
                    break;

                case BitmapFormat.A8Y8:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0x00FF; ABitMask = 0xFF00;
                    break;

                case BitmapFormat.R5G6B5:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0xF800; GBitMask = 0x07E0; BBitMask = 0x001F;
                    break;

                case BitmapFormat.A1R5G5B5:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0x7C00; GBitMask = 0x03E0; BBitMask = 0x001F; ABitMask = 0x8000;
                    break;

                case BitmapFormat.A4R4G4B4:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0xF000; GBitMask = 0x0F00; BBitMask = 0x00F0; ABitMask = 0x000F;
                    break;

                case BitmapFormat.A4R4G4B4Font:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0xF000; GBitMask = 0x0F00; BBitMask = 0x00F0; ABitMask = 0x000F;
                    break;

                case BitmapFormat.A8R8G8B8:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0x00FF0000; GBitMask = 0x0000FF00; BBitMask = 0x000000FF; ABitMask = 0xFF000000;
                    break;

                case BitmapFormat.X8R8G8B8:
                    Flags   |= DDSPixelFormatFlags.RGB;
                    RBitMask = 0x00FF0000; GBitMask = 0x0000FF00; BBitMask = 0x000000FF;
                    break;

                case BitmapFormat.V8U8:
                    RBitMask = 0xFF00; GBitMask = 0x00FF;
                    Flags   |= DDSPixelFormatFlags.BumpDUDV;
                    break;

                case BitmapFormat.A32B32G32R32F:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x74;
                    break;

                case BitmapFormat.A16B16G16R16F:
                    Flags |= DDSPixelFormatFlags.FourCC;
                    FourCC = 0x71;
                    break;

                default:
                    throw new Exception($"Unsupported bitmap format {format}");
                }
            }
        }