Ejemplo n.º 1
0
        private void OnSelectedValueChanged(object sender, EventArgs e)
        {
            var ddsMipMapInfo = (DdsMipMapInfo)_listBox.SelectedValue;

            var width  = Texture.CalculateMipMapSize(ddsMipMapInfo.Level, (int)_ddsFile.Header.Width);
            var height = Texture.CalculateMipMapSize(ddsMipMapInfo.Level, (int)_ddsFile.Header.Height);

            byte[] unpackedData;
            switch (_ddsFile.PixelFormat)
            {
            case LowLevel.Graphics3D.PixelFormat.Bc1:
            case LowLevel.Graphics3D.PixelFormat.Bc2:
            case LowLevel.Graphics3D.PixelFormat.Bc3:
                unpackedData = BlockCompressionUtility.Decompress(
                    _ddsFile.PixelFormat,
                    ddsMipMapInfo.MipMap.Data,
                    (int)ddsMipMapInfo.MipMap.RowPitch,
                    out _);
                width  = Math.Max(width, 4);
                height = Math.Max(height, 4);
                break;

            case LowLevel.Graphics3D.PixelFormat.Rg8SNorm:
                unpackedData = new byte[width * height * 4];
                var unpackedDataIndex = 0;
                for (var i = 0; i < ddsMipMapInfo.MipMap.Data.Length; i += 2)
                {
                    unpackedData[unpackedDataIndex++] = ddsMipMapInfo.MipMap.Data[i + 0];
                    unpackedData[unpackedDataIndex++] = ddsMipMapInfo.MipMap.Data[i + 1];
                    unpackedData[unpackedDataIndex++] = 0;
                    unpackedData[unpackedDataIndex++] = 255;
                }
                break;

            default:
                throw new NotSupportedException();
            }

            var bmpData = PngUtility.ConvertToPng(
                unpackedData,
                width,
                height);

            Panel2 = new ImageView
            {
                Style = "nearest-neighbor",
                Image = new Bitmap(bmpData)
            };
        }
Ejemplo n.º 2
0
        private void SetData(int level, byte[] data, int bytesPerRow)
        {
            var processedData        = data;
            var processedBytesPerRow = bytesPerRow;

            // TODO: Don't do this for Mac.
            switch (_originalPixelFormat)
            {
            case PixelFormat.Bc1:
            case PixelFormat.Bc2:
            case PixelFormat.Bc3:
                processedData = BlockCompressionUtility.Decompress(
                    _originalPixelFormat,
                    data,
                    bytesPerRow,
                    out processedBytesPerRow);
                break;
            }

            // TODO: This isn't correct for mip levels > 0.
            var region = MTLRegion.Create2D(0, 0, DeviceTexture.Width, DeviceTexture.Height);

            var pinnedArray = GCHandle.Alloc(processedData, GCHandleType.Pinned);

            try
            {
                var dataPointer = pinnedArray.AddrOfPinnedObject();

                DeviceTexture.ReplaceRegion(
                    region,
                    (uint)level,
                    dataPointer,
                    (uint)processedBytesPerRow);
            }
            finally
            {
                pinnedArray.Free();
            }
        }