Beispiel #1
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var imageData = NSData.FromStream(stream))
                using (var image = UIImage.LoadFromData(imageData))
                {
                    int width = (int)image.Size.Width;
                    int height = (int)image.Size.Height;
                    Mipmaps = new Mipmap[1];
                    Size = new Size2(width, height);

                    var data = new byte[width * height * 4];
                    using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width*4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
                    {
                        imageContext.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);

                        Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                        if (flip) Mipmaps[0].FlipVertical();
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var    appSettings     = ConfigurationManager.AppSettings;
            string MafiaIIRootPath = appSettings["MafiaIIRootPath"];
            string DesktopPath     = appSettings["DesktopPath"];

            /* Init of SDS file */
            using (SdsFile sdsFile = SdsFile.FromFile($@"{MafiaIIRootPath}\pc\sds\mapa\mapa_city.sds"))
            {
                /* Extracts all resources from SDS into selected directory*/
                sdsFile.ExportToDirectory($@"{DesktopPath}");

                /* Extracts single resource to the selected path */
                Texture texture = sdsFile.GetResourceByTypeAndName <Texture>("map.dds");
                texture.Extract($@"{DesktopPath}\map.dds");

                /* Extracts all textures from current SDS (if contains any) */
                sdsFile.ExtractResourcesByType <Texture>($@"{DesktopPath}\mapa_city\textures");

                /* Replaces data of the selected file */
                Mipmap mipMap = sdsFile.GetResourceByTypeAndName <Mipmap>("map.dds");
                mipMap.ReplaceData($@"{DesktopPath}\new_map.dds");

                /* Saves modified SDS file to the selected path */
                sdsFile.ExportToFile($@"{DesktopPath}\modified.sds");
            }
        }
Beispiel #3
0
        public override Mipmap Map(MapOptions op, uint mipmap, uint face, bool uncompress)
        {
            if (face + offsetFace >= faceCount)
            {
                throw new IndexOutOfRangeException("Face index is out of image's range.");
            }
            Monitor.Enter(syncRoot);
            try
            {
                // We lock and extract subregion.
                internalMipmapLocked = internalImage.Map(op, mipmap, face, uncompress);

                // We check if reading is required.
                if (op != MapOptions.Write)
                {
                    lockedMipmap = new Mipmap(width, height, depth, face, mipmap, null, this);
                }
                else
                {
                }
                throw new NotImplementedException();
            }
            catch (Exception)
            {
                Monitor.Exit(syncRoot);

                // Rethrow.
                throw;
            }
        }
Beispiel #4
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var image = NSImage.FromStream(stream))
                {
                    var rep = image.Representations()[0];
                    int width = rep.PixelsWide;
                    int height = rep.PixelsHigh;
                    Mipmaps = new Mipmap[1];
                    Size = new Size2(width, height);

                    var data = new byte[width * height * 4];
                    var emptyRect = RectangleF.Empty;
                    using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width*4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
                    using (var cgImage = image.AsCGImage(ref emptyRect, null, null))
                    {
                        imageContext.DrawImage(new RectangleF(0, 0, width, height), cgImage);

                        Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                        if (flip) Mipmaps[0].FlipVertical();
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
        }
Beispiel #5
0
        /// <summary>
        /// Define texture mipmap level properties.
        /// </summary>
        /// <param name="pixelFormat">
        /// The <see cref="PixelLayout"/> that specify the texture mipmap internal format.
        /// </param>
        /// <param name="w">
        /// A <see cref="UInt32"/> that specify the width of the texture mipmap.
        /// </param>
        /// <param name="h">
        /// A <see cref="UInt32"/> that specify the height of the texture mipmap.
        /// </param>
        /// <param name="z">
        /// A <see cref="UInt32"/> that specify the depth of the texture mipmap.
        /// </param>
        /// <param name="lod">
        /// A <see cref="UInt32"/> that specify the level texture mipmap.
        /// </param>
        protected void SetMipmap(PixelLayout pixelFormat, uint w, uint h, uint z, uint lod)
        {
            if (pixelFormat == PixelLayout.None)
            {
                throw new ArgumentException("invalid pixel format", "pixelFormat");
            }

            // Define mipmaps array
            if (_Mipmaps == null)
            {
                _Mipmaps = new Mipmap[0];
            }
            // Extend mipmaps array, if necessary
            if (lod >= _Mipmaps.Length)
            {
                Array.Resize(ref _Mipmaps, (int)(lod + 1));
            }

            if (_Mipmaps[lod] == null)
            {
                _Mipmaps[lod] = new Mipmap();
            }
            _Mipmaps[lod].InternalFormat = pixelFormat;
            _Mipmaps[lod].Size           = new Vertex3ui(w, h, z);
        }
        private void bs_ImageOpened(object sender, RoutedEventArgs args)
        {
            try
            {
                var image  = (BitmapImage)sender;
                var bitmap = new WriteableBitmap(image);

                int width  = bitmap.PixelWidth;
                int height = bitmap.PixelHeight;
                Mipmaps = new Mipmap[1];
                Size    = new Size2(bitmap.PixelWidth, bitmap.PixelHeight);

                var dataPixels = bitmap.Pixels;
                var data       = new byte[dataPixels.Length * 4];
                int i2         = 0;
                for (int i = 0; i != dataPixels.Length; ++i)
                {
                    var color = new Color4(dataPixels[i]);
                    data[i2]     = color.R;
                    data[i2 + 1] = color.G;
                    data[i2 + 2] = color.B;
                    data[i2 + 3] = color.A;

                    i2 += 4;
                }

                // Flip RB Color bits
                for (i2 = 0; i2 != data.Length; i2 += 4)
                {
                    byte c = data[i2];
                    data[i2]     = data[i2 + 2];
                    data[i2 + 2] = c;
                }

                Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                if (flip)
                {
                    Mipmaps[0].FlipVertical();
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                loadedCallback = null;
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
            loadedCallback = null;
        }
        private Mipmap GenerateMipmap(Bitmap inputImage, TEXFile.PixelFormat pixelFormat, bool preMultiplyAlpha)
        {
            byte[] rgba = new byte[inputImage.Width * inputImage.Height * 4];

            for (int y = 0; y < inputImage.Height; y++)
            {
                for (int x = 0; x < inputImage.Width; x++)
                {
                    Color c = inputImage.GetPixel(x, y);

                    if (preMultiplyAlpha)
                    {
                        float alphamod = (float)c.A / 255.0f; // Normalize.

                        var newR = (byte)(c.R * alphamod);
                        var newG = (byte)(c.G * alphamod);
                        var newB = (byte)(c.B * alphamod);

                        c = Color.FromArgb(c.A, newR, newG, newB);
                    }

                    rgba[y * inputImage.Width * 4 + x * 4 + 0] = c.R;
                    rgba[y * inputImage.Width * 4 + x * 4 + 1] = c.G;
                    rgba[y * inputImage.Width * 4 + x * 4 + 2] = c.B;
                    rgba[y * inputImage.Width * 4 + x * 4 + 3] = c.A;
                }
            }

            byte[] finalImageData = null;

            switch (pixelFormat)
            {
            case TEXFile.PixelFormat.DXT1:
                finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt1);
                break;

            case TEXFile.PixelFormat.DXT3:
                finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt3);
                break;

            case TEXFile.PixelFormat.DXT5:
                finalImageData = Squish.CompressImage(rgba, inputImage.Width, inputImage.Height, SquishFlags.Dxt5);
                break;

            case TEXFile.PixelFormat.ARGB:
                finalImageData = rgba;
                break;
            }

            var mipmap = new Mipmap();

            mipmap.Width    = (ushort)inputImage.Width;
            mipmap.Height   = (ushort)inputImage.Height;
            mipmap.Pitch    = 0;
            mipmap.ARGBData = finalImageData;

            return(mipmap);
        }
        public override void EndCPUAccess()
        {
            AssertNotDisposed();
            AssertLocked();
            mipmap.Dispose();

            mipmap = null;
            Monitor.Exit(syncRoot);
        }
Beispiel #9
0
        public override void Serialize(IFileStream stream)
        {
            base.Serialize(stream);

            stream.SerializeValue(ref this._Unknown0);
            if (this.Unknown0 != 0)
            {
                throw new FormatException();
            }

            if (stream.Mode == SerializeMode.Reading)
            {
                this.Mipmaps.Clear();

                uint mipmapCount = 0;
                stream.SerializeValue(ref mipmapCount);

                for (uint i = 0; i < mipmapCount; i++)
                {
                    var mip = new Mipmap();
                    stream.SerializeValue(ref mip.Width);
                    stream.SerializeValue(ref mip.Height);
                    stream.SerializeValue(ref mip.Unknown2);

                    uint size = 0;
                    stream.SerializeValue(ref size);
                    stream.SerializeValue(ref mip.Data, size);

                    this.Mipmaps.Add(mip);
                }
            }
            else
            {
                throw new NotSupportedException();
            }

            uint unknown1 = 0;

            stream.SerializeValue(ref unknown1);
            if (unknown1 != 0)
            {
                throw new FormatException();
            }

            byte unknown2 = 0;

            stream.SerializeValue(ref unknown2);
            if (unknown2 != 0)
            {
                throw new FormatException();
            }
        }
Beispiel #10
0
        public Mipmap GetMainMipmapSummary()
        {
            Mipmap mipmap = new Mipmap();

            using (var reader = new BinaryReader(new MemoryStream(File.Raw))) {
                mipmap.Width    = reader.ReadUInt16();
                mipmap.Height   = reader.ReadUInt16();
                mipmap.Pitch    = reader.ReadUInt16();
                mipmap.DataSize = reader.ReadUInt32();
            }

            return(mipmap);
        }
Beispiel #11
0
		public static byte[] Encode(byte[] data,Size size) {
            var mipmap = new Mipmap() {
                Data = data,
                Width = size.Width,
                Height = size.Height
            };
            var texture = new Texture();
            texture.Mipmaps = new Mipmap[1] { mipmap };
            var ms = new MemoryStream();


            return new byte[0];
        }
Beispiel #12
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var bitmap = new Bitmap(stream))
                {
                    int width  = bitmap.Width;
                    int height = bitmap.Height;
                    Mipmaps = new Mipmap[1];
                    Size    = new Size2(bitmap.Width, bitmap.Height);

                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                    // Copy unmanaged data to managed array
                    int    size = System.Math.Abs(bitmapData.Stride) * height;
                    byte[] data = new byte[size];
                    Marshal.Copy(bitmapData.Scan0, data, 0, size);

                    // Flip RB Color bits
                    for (int i2 = 0; i2 != data.Length; i2 += 4)
                    {
                        byte c = data[i2];
                        data[i2]     = data[i2 + 2];
                        data[i2 + 2] = c;
                    }

                    Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                    if (flip)
                    {
                        Mipmaps[0].FlipVertical();
                    }
                    bitmap.UnlockBits(bitmapData);
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #13
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var bitmap = Android.Graphics.BitmapFactory.DecodeStream(stream))
                {
                    int width  = bitmap.Width;
                    int height = bitmap.Height;
                    Mipmaps = new Mipmap[1];
                    Size    = new Size2(bitmap.Width, bitmap.Height);

                    var pixels = new int[width * height];
                    bitmap.GetPixels(pixels, 0, width, 0, 0, width, height);

                    // Convert to bytes
                    var data = new byte[pixels.Length * 4];
                    int i3   = 0;
                    for (int i2 = 0; i2 != pixels.Length; ++i2)
                    {
                        data[i3]     = (byte)Color.GetRedComponent(pixels[i2]);
                        data[i3 + 1] = (byte)Color.GetGreenComponent(pixels[i2]);
                        data[i3 + 2] = (byte)Color.GetBlueComponent(pixels[i2]);
                        data[i3 + 3] = (byte)Color.GetAlphaComponent(pixels[i2]);
                        i3          += 4;
                    }

                    Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                    if (flip)
                    {
                        Mipmaps[0].FlipVertical();
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
        private void bs_ImageOpened(object sender, RoutedEventArgs args)
        {
            try
            {
                var image = (BitmapImage)sender;
                var bitmap = new WriteableBitmap(image);

                int width = bitmap.PixelWidth;
                int height = bitmap.PixelHeight;
                Mipmaps = new Mipmap[1];
                Size = new Size2(bitmap.PixelWidth, bitmap.PixelHeight);

                var dataPixels = bitmap.Pixels;
                var data = new byte[dataPixels.Length * 4];
                int i2 = 0;
                for (int i = 0; i != dataPixels.Length; ++i)
                {
                    var color = new Color4(dataPixels[i]);
                    data[i2] = color.R;
                    data[i2+1] = color.G;
                    data[i2+2] = color.B;
                    data[i2+3] = color.A;

                    i2 += 4;
                }

                // Flip RB Color bits
                for (i2 = 0; i2 != data.Length; i2 += 4)
                {
                    byte c = data[i2];
                    data[i2] = data[i2+2];
                    data[i2+2] = c;
                }

                Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                if (flip) Mipmaps[0].FlipVertical();
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                loadedCallback = null;
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
            loadedCallback = null;
        }
Beispiel #15
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                var bitmap = new WriteableBitmap(image);
                var pixels = bitmap.Pixels;
                var data   = new byte[pixels.Length * 4];
                int i2     = 0;
                for (int i = 0; i != pixels.Length; ++i)
                {
                    Color4 color = new Color4(pixels[i]);
                    data[i2]     = color.R;
                    data[i2 + 1] = color.G;
                    data[i2 + 2] = color.B;
                    data[i2 + 3] = color.A;
                    i2          += 4;
                }

                int width  = (int)bitmap.PixelWidth;
                int height = (int)bitmap.PixelHeight;
                Mipmaps = new Mipmap[1];
                Size    = new Size2(width, height);

                Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                if (flip)
                {
                    Mipmaps[0].FlipVertical();
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                loadedCallback = null;
                image          = null;
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
            loadedCallback = null;
            image          = null;
        }
Beispiel #16
0
        protected override async void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var memoryStream = new InMemoryRandomAccessStream())
                {
                    await RandomAccessStream.CopyAsync(stream.AsInputStream(), memoryStream);

                    var decoder = await BitmapDecoder.CreateAsync(memoryStream);

                    var frame = await decoder.GetFrameAsync(0);

                    var transform = new BitmapTransform();
                    transform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;
                    transform.Rotation          = BitmapRotation.None;
                    var dataProvider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.ColorManageToSRgb);

                    var data = dataProvider.DetachPixelData();

                    int width  = (int)decoder.PixelWidth;
                    int height = (int)decoder.PixelHeight;
                    Mipmaps = new Mipmap[1];
                    Size    = new Size2(width, height);

                    Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                    if (flip)
                    {
                        Mipmaps[0].FlipVertical();
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #17
0
        public Mipmap[] GetMipmapsSummary()
        {
            Mipmap[] mipmapArray = new Mipmap[File.Header.NumMips];

            using (var reader = new BinaryReader(new MemoryStream(File.Raw)))
                for (int i = 0; i < File.Header.NumMips; i++)
                {
                    mipmapArray[i]          = new Mipmap();
                    mipmapArray[i].Width    = reader.ReadUInt16();
                    mipmapArray[i].Height   = reader.ReadUInt16();
                    mipmapArray[i].Pitch    = reader.ReadUInt16();
                    mipmapArray[i].DataSize = reader.ReadUInt32();
                }

            return(mipmapArray);
        }
Beispiel #18
0
        public Bitmap GenerateBitMap()
        {
            byte[] argbData;
            Mipmap mipmap = GetMainMipmap();

            switch ((PixelFormat)this.GetFormat())
            {
            case PixelFormat.DXT1:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt1);
                break;

            case PixelFormat.DXT3:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt3);
                break;

            case PixelFormat.DXT5:
                argbData = Squish.DecompressImage(mipmap.Data, mipmap.Width, mipmap.Height, SquishFlags.Dxt5);
                break;

            case PixelFormat.ARGB:
                argbData = mipmap.Data;
                break;

            default:
                throw new Exception("Unknown pixel format?");
            }

            var imgReader = new BinaryReader(new MemoryStream(argbData));
            //BitmapSource.Create(mipmap.Width,mipmap.Height,null,null,System.Windows.Media.PixelFormats.rgba)
            Bitmap pt = new Bitmap((int)mipmap.Width, (int)mipmap.Height);

            for (int y = 0; y < mipmap.Height; y++)
            {
                for (int x = 0; x < mipmap.Width; x++)
                {
                    byte r = imgReader.ReadByte();
                    byte g = imgReader.ReadByte();
                    byte b = imgReader.ReadByte();
                    byte a = imgReader.ReadByte();
                    pt.SetPixel(x, mipmap.Height - y - 1, Color.FromArgb(a, r, g, b));                    //OpenGL scan order(DST PC)
                }
                //if (OnProgressUpdate != null) {
                //OnProgressUpdate(y * 100 / mipmap.Height);
                //}
            }
            return(pt);
        }
Beispiel #19
0
        public Mipmap GetMainMipmap()
        {
            Mipmap mipmap = new Mipmap();

            using (var reader = new BinaryReader(new MemoryStream(File.Raw))) {
                mipmap.Width    = reader.ReadUInt16();
                mipmap.Height   = reader.ReadUInt16();
                mipmap.Pitch    = reader.ReadUInt16();
                mipmap.DataSize = reader.ReadUInt32();

                reader.BaseStream.Seek((File.Header.NumMips - 1) * 10, SeekOrigin.Current);

                mipmap.Data = reader.ReadBytes((int)mipmap.DataSize);
            }

            return(mipmap);
        }
        public override void BeginCPUAccess()
        {
            Monitor.Enter(syncRoot);
            try
            {
                AssertNotDisposed();
                AssertNotLocked();

                // We extract mipmap.
                mipmap = Map(MapOptions.Read, 0);
            }
            catch (Exception)
            {
                Monitor.Exit(syncRoot);
                throw;
            }
        }
Beispiel #21
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var image = NSImage.FromStream(stream))
                {
                    var rep    = image.Representations()[0];
                    int width  = rep.PixelsWide;
                    int height = rep.PixelsHigh;
                    Mipmaps = new Mipmap[1];
                    Size    = new Size2(width, height);

                    var data      = new byte[width * height * 4];
                    var emptyRect = RectangleF.Empty;
                    using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
                        using (var cgImage = image.AsCGImage(ref emptyRect, null, null))
                        {
                            imageContext.DrawImage(new RectangleF(0, 0, width, height), cgImage);

                            Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                            if (flip)
                            {
                                Mipmaps[0].FlipVertical();
                            }
                        }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #22
0
        /// <summary>
        /// Define texture mipmap level properties.
        /// </summary>
        /// <param name="pixelFormat">
        /// The <see cref="PixelLayout"/> that specify the texture mipmap internal format.
        /// </param>
        /// <param name="w">
        /// A <see cref="UInt32"/> that specify the width of the texture mipmap.
        /// </param>
        /// <param name="h">
        /// A <see cref="UInt32"/> that specify the height of the texture mipmap.
        /// </param>
        /// <param name="z">
        /// A <see cref="UInt32"/> that specify the depth of the texture mipmap.
        /// </param>
        /// <param name="lod">
        /// A <see cref="UInt32"/> that specify the level texture mipmap.
        /// </param>
        protected void DefineExtents(PixelLayout pixelFormat, uint w, uint h, uint z, uint lod)
        {
            if (pixelFormat == PixelLayout.None)
            {
                throw new ArgumentException("invalid pixel format", "pixelFormat");
            }
            // Define mipmaps array
            if (_Mipmaps == null)
            {
                _Mipmaps = new Mipmap[GetMipmapLevels(w, h, z, lod)];
            }

            if (_Mipmaps[lod] == null)
            {
                _Mipmaps[lod] = new Mipmap();
            }
            _Mipmaps[lod].InternalFormat = pixelFormat;
            _Mipmaps[lod].Size           = new Vertex3ui(w, h, z);
        }
Beispiel #23
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var imageData = NSData.FromStream(stream))
                    using (var image = UIImage.LoadFromData(imageData))
                    {
                        int width  = (int)image.Size.Width;
                        int height = (int)image.Size.Height;
                        Mipmaps = new Mipmap[1];
                        Size    = new Size2(width, height);

                        var data = new byte[width * height * 4];
                        using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
                        {
                            imageContext.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);

                            Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                            if (flip)
                            {
                                Mipmaps[0].FlipVertical();
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #24
0
        /// <summary>
        /// Generate mipmaps for this Texture, replacing mipmaps level from 1 to <see cref="MipmapLevels"/> - 1.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for mipmapping definition.
        /// </param>
        /// <param name="target">
        /// A <see cref="TextureTarget"/> indicating the target for generating
        /// bitmaps.
        /// </param>
        protected void GenerateMipmaps(GraphicsContext ctx, TextureTarget target)
        {
            CheckCurrentContext(ctx);

            // Bind this Texture
            ctx.Bind(this);
            // Generate mipmaps
            Gl.GenerateMipmap((int)target);

            Debug.Assert(_Mipmaps.Length >= MipmapLevels);
            for (uint i = MipmapBaseLevel + 1; i < MipmapLevels; i++)
            {
                if (_Mipmaps[i] == null)
                {
                    _Mipmaps[i] = new Mipmap();
                }
                _Mipmaps[i].InternalFormat = _Mipmaps[MipmapBaseLevel].InternalFormat;
                _Mipmaps[i].Size           = GetMipmapSize(i);
            }
        }
Beispiel #25
0
        public KLEISurface(BinaryReader reader)
        {
            signature = reader.ReadBytes(8);
            StructSize = reader.ReadUInt32();

            OpenGLType = reader.ReadUInt32();
            OpenGLStorageType = reader.ReadUInt32();

            var compressed = reader.ReadUInt32();
            IsDXTCompressed = compressed == 1;

            MipmapCount = reader.ReadUInt32();
            TotalSizeOfAllMips = reader.ReadUInt32();

            mipmaps = new Mipmap[MipmapCount];
            for (var i = 0; i < MipmapCount; i++)
            {
                mipmaps[i] = new Mipmap(reader);
            }
        }
Beispiel #26
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var bitmap = Android.Graphics.BitmapFactory.DecodeStream(stream))
                {
                    int width = bitmap.Width;
                    int height = bitmap.Height;
                    Mipmaps = new Mipmap[1];
                    Size = new Size2(bitmap.Width, bitmap.Height);

                    var pixels = new int[width * height];
                    bitmap.GetPixels(pixels, 0, width, 0, 0, width, height);

                    // Convert to bytes
                    var data = new byte[pixels.Length * 4];
                    int i3 = 0;
                    for (int i2 = 0; i2 != pixels.Length; ++i2)
                    {
                        data[i3] = (byte)Color.GetRedComponent(pixels[i2]);
                        data[i3+1] = (byte)Color.GetGreenComponent(pixels[i2]);
                        data[i3+2] = (byte)Color.GetBlueComponent(pixels[i2]);
                        data[i3+3] = (byte)Color.GetAlphaComponent(pixels[i2]);
                        i3 += 4;
                    }

                    Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                    if (flip) Mipmaps[0].FlipVertical();
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
        }
Beispiel #27
0
        public KLEISurface(BinaryReader reader)
        {
            signature  = reader.ReadBytes(8);
            StructSize = reader.ReadUInt32();

            OpenGLType        = reader.ReadUInt32();
            OpenGLStorageType = reader.ReadUInt32();

            var compressed = reader.ReadUInt32();

            IsDXTCompressed = compressed == 1;

            MipmapCount        = reader.ReadUInt32();
            TotalSizeOfAllMips = reader.ReadUInt32();

            mipmaps = new Mipmap[MipmapCount];
            for (var i = 0; i < MipmapCount; i++)
            {
                mipmaps[i] = new Mipmap(reader);
            }
        }
Beispiel #28
0
		protected override async void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
		{
			try
			{
				using (var memoryStream = new InMemoryRandomAccessStream())
				{
					await RandomAccessStream.CopyAsync(stream.AsInputStream(), memoryStream);

					var decoder = await BitmapDecoder.CreateAsync(memoryStream);
					var frame = await decoder.GetFrameAsync(0);

					var transform = new BitmapTransform();
					transform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;
					transform.Rotation = BitmapRotation.None;
					var dataProvider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.ColorManageToSRgb);
					var data = dataProvider.DetachPixelData();
			
					int width = (int)decoder.PixelWidth;
					int height = (int)decoder.PixelHeight;
					Mipmaps = new Mipmap[1];
					Size = new Size2(width, height);

					Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
					if (flip) Mipmaps[0].FlipVertical();
				}
			}
			catch (Exception e)
			{
				FailedToLoad = true;
				Loader.AddLoadableException(e);
				if (loadedCallback != null) loadedCallback(this, false);
				return;
			}

			Loaded = true;
			if (loadedCallback != null) loadedCallback(this, true);
		}
Beispiel #29
0
        /// <summary>
        /// Create Surface from KWAD Package v1
        /// </summary>
        public static KLEISurface CreateFromKWAD1(BinaryReader reader)
        {
            var signature  = reader.ReadBytes(8);
            var structSize = reader.ReadUInt32();

            var openGLType        = reader.ReadUInt32();
            var openGLStorageType = reader.ReadUInt32();
            // Oldest EA version of KWAD1 file is missing openGLStorageType field.
            // We check for that based on the value we read.
            // If its 0 or 1 - it is very likely that this is the value of
            // the next "isDXTCompressed" field (openGLStorageType is missing)
            uint compressed = 0;

            if (openGLStorageType == 0 || openGLStorageType == 1)
            {
                compressed        = openGLStorageType;
                openGLStorageType = openGLType;
            }
            else
            {
                compressed = reader.ReadUInt32();
            }

            var isDXTCompressed = compressed == 1;

            var mipmapCount        = reader.ReadUInt32();
            var totalSizeOfAllMips = reader.ReadUInt32();

            var mipmaps = new Mipmap[mipmapCount];

            for (var i = 0; i < mipmapCount; i++)
            {
                mipmaps[i] = Mipmap.CreateFromKWAD1(reader);
            }

            return(new KLEISurface(signature, mipmaps, structSize, openGLType, openGLStorageType, isDXTCompressed, mipmapCount, totalSizeOfAllMips));
        }
Beispiel #30
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType = ImageTypes.DDS;

                // Load Desc
                DDSURFACEDESC2 desc   = new DDSURFACEDESC2();
                var            reader = new BinaryReader(stream);
                desc.MagicNumber = reader.ReadUInt32();
                desc.dwSize      = reader.ReadUInt32();
                desc.dwFlags     = reader.ReadUInt32();
                desc.dwHeight    = reader.ReadUInt32();
                desc.dwWidth     = reader.ReadUInt32();

                desc.Union1        = new DDSURFACEDESC2.DUMMYUNIONNAMEN1();
                desc.Union1.lPitch = reader.ReadInt32();

                desc.Union5 = new DDSURFACEDESC2.DUMMYUNIONNAMEN5();
                desc.Union5.dwBackBufferCount = reader.ReadUInt32();

                desc.Union2 = new DDSURFACEDESC2.DUMMYUNIONNAMEN2();
                desc.Union2.dwMipMapCount = reader.ReadUInt32();

                desc.dwAlphaBitDepth = reader.ReadUInt32();
                desc.dwReserved      = reader.ReadUInt32();
                desc.lpSurface       = reader.ReadInt32();

                desc.Union3 = new DDSURFACEDESC2.DUMMYUNIONNAMEN3();
                desc.Union3.ddckCKDestOverlay = new DDCOLORKEY();
                desc.Union3.ddckCKDestOverlay.dwColorSpaceLowValue  = reader.ReadUInt32();
                desc.Union3.ddckCKDestOverlay.dwColorSpaceHighValue = reader.ReadUInt32();

                desc.ddckCKDestBlt = new DDCOLORKEY();
                desc.ddckCKDestBlt.dwColorSpaceLowValue  = reader.ReadUInt32();
                desc.ddckCKDestBlt.dwColorSpaceHighValue = reader.ReadUInt32();
                desc.ddckCKSrcOverlay = new DDCOLORKEY();
                desc.ddckCKSrcOverlay.dwColorSpaceLowValue  = reader.ReadUInt32();
                desc.ddckCKSrcOverlay.dwColorSpaceHighValue = reader.ReadUInt32();
                desc.ddckCKSrcBlt = new DDCOLORKEY();
                desc.ddckCKSrcBlt.dwColorSpaceLowValue  = reader.ReadUInt32();
                desc.ddckCKSrcBlt.dwColorSpaceHighValue = reader.ReadUInt32();

                desc.Union4 = new DDSURFACEDESC2.DUMMYUNIONNAMEN4();
                desc.Union4.ddpfPixelFormat          = new DDPIXELFORMAT();
                desc.Union4.ddpfPixelFormat.dwSize   = reader.ReadUInt32();
                desc.Union4.ddpfPixelFormat.dwFlags  = reader.ReadUInt32();
                desc.Union4.ddpfPixelFormat.dwFourCC = reader.ReadUInt32();
                {
                    desc.Union4.ddpfPixelFormat.Union1 = new DDPIXELFORMAT.DUMMYUNIONNAMEN1();
                    desc.Union4.ddpfPixelFormat.Union1.dwRGBBitCount = reader.ReadUInt32();

                    desc.Union4.ddpfPixelFormat.Union2            = new DDPIXELFORMAT.DUMMYUNIONNAMEN2();
                    desc.Union4.ddpfPixelFormat.Union2.dwRBitMask = reader.ReadUInt32();

                    desc.Union4.ddpfPixelFormat.Union3 = new DDPIXELFORMAT.DUMMYUNIONNAMEN3();
                    desc.Union4.ddpfPixelFormat.Union3.MultiSampleCaps = new DDPIXELFORMAT.DUMMYUNIONNAMEN3.Caps();
                    desc.Union4.ddpfPixelFormat.Union3.MultiSampleCaps.wFlipMSTypes = reader.ReadInt16();
                    desc.Union4.ddpfPixelFormat.Union3.MultiSampleCaps.wBltMSTypes  = reader.ReadInt16();

                    desc.Union4.ddpfPixelFormat.Union4            = new DDPIXELFORMAT.DUMMYUNIONNAMEN4();
                    desc.Union4.ddpfPixelFormat.Union4.dwBBitMask = reader.ReadUInt32();

                    desc.Union4.ddpfPixelFormat.Union5 = new DDPIXELFORMAT.DUMMYUNIONNAMEN5();
                    desc.Union4.ddpfPixelFormat.Union5.dwRGBAlphaBitMask = reader.ReadUInt32();
                }

                desc.ddsCaps               = new DDSCAPS2();
                desc.ddsCaps.dwCaps        = reader.ReadUInt32();
                desc.ddsCaps.dwCaps2       = reader.ReadUInt32();
                desc.ddsCaps.dwCaps3       = reader.ReadUInt32();
                desc.ddsCaps.Union         = new DDSCAPS2.DUMMYUNIONNAMEN();
                desc.ddsCaps.Union.dwCaps4 = reader.ReadUInt32();

                desc.dwTextureStage = reader.ReadUInt32();

                // Check file ext
                if (desc.MagicNumber != 0x20534444u)
                {
                    Debug.ThrowError("ImageDDS", "Not a DDS file");
                }

                // Get file caps
                bool isCubemap       = ((desc.ddsCaps.dwCaps & DDSCAPS_COMPLEX) != 0) && ((desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP) != 0);
                bool isVolumeTexture = ((desc.ddsCaps.dwCaps2 & DDSCAPS2_VOLUME) != 0);
                bool hasAlphaChannel = ((desc.Union4.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS) != 0);
                Compressed = ((desc.Union4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) != 0);

                // Get pixel format
                Size = new Size2((int)desc.dwWidth, (int)desc.dwHeight);
                int blockSize = 0, blockDev = 1;
                if (Compressed)
                {
                    FormatD3D = desc.Union4.ddpfPixelFormat.dwFourCC;
                    switch (desc.Union4.ddpfPixelFormat.dwFourCC)
                    {
                    case FOURCC_DXT1:
                        FormatGL      = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                        blockSize     = 8;
                        blockDev      = 2;
                        SurfaceFormat = SurfaceFormats.DXT1;
                        ImageFormat   = ImageFormats.DXT1;
                        break;

                    case FOURCC_DXT3:
                        FormatGL      = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                        blockSize     = 16;
                        SurfaceFormat = SurfaceFormats.DXT3;
                        ImageFormat   = ImageFormats.DXT3;
                        break;

                    case FOURCC_DXT5:
                        FormatGL      = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                        blockSize     = 16;
                        SurfaceFormat = SurfaceFormats.DXT5;
                        ImageFormat   = ImageFormats.DXT5;
                        break;

                    case FOURCC_ATC_RGB:
                        FormatGL      = ATC_RGB_AMD;
                        blockSize     = 8;
                        blockDev      = 2;
                        SurfaceFormat = SurfaceFormats.ATC_RGB;
                        ImageFormat   = ImageFormats.ATC_RGB;
                        break;

                    case FOURCC_ATC_RGBA_EXPLICIT:
                        FormatGL      = ATC_RGBA_EXPLICIT_ALPHA_AMD;
                        blockSize     = 16;
                        SurfaceFormat = SurfaceFormats.ATC_RGBA_Explicit;
                        ImageFormat   = ImageFormats.ATC_RGBA_Explicit;
                        break;

                    case FOURCC_ATC_RGBA_INTERPOLATED:
                        FormatGL      = ATC_RGBA_INTERPOLATED_ALPHA_AMD;
                        blockSize     = 16;
                        SurfaceFormat = SurfaceFormats.ATC_RGBA_Interpolated;
                        ImageFormat   = ImageFormats.ATC_RGBA_Interpolated;
                        break;

                    default:
                        Debug.ThrowError("ImageDDS", "Unsuported DDS Format");
                        break;
                    }
                }
                else
                {
                    Debug.ThrowError("ImageDDS", "Uncompressed textures not supported yet");
                }

                if (isCubemap || isVolumeTexture)
                {
                    Debug.ThrowError("ImageDDS", "Cubemap and VolumeTextures not supported yet");
                }

                // Create Mipmaps
                Mipmaps = new Mipmap[desc.Union2.dwMipMapCount == 0 ? 1 : desc.Union2.dwMipMapCount];
                var size = Size;
                for (int i = 0; i < Mipmaps.Length; ++i)
                {
                    int dataSize = (((size.Width + 3) / 4) * ((size.Height + 3) / 4)) * blockSize;
                    var data     = new byte[dataSize];
                    stream.Read(data, 0, dataSize);

                    if (flip && (FormatD3D == FOURCC_DXT1 || FormatD3D == FOURCC_DXT3 || FormatD3D == FOURCC_DXT5))
                    {
                        data = flipCompressedData(data, size.Width, size.Height, blockSize);
                    }
                    Mipmaps[i] = new Mipmap(data, size.Width, size.Height, blockDev, 4);

                    size /= 2;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #31
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType = ImageTypes.PVR;

                // Load Header
                var header = new PVRHeader();
                var reader = new BinaryReader(stream);

                // Read header version 2
                header.HeaderLength = reader.ReadUInt32();
                header.Height = reader.ReadUInt32();
                header.Width = reader.ReadUInt32();
                header.NumMipmaps = reader.ReadUInt32();
                header.Flags = reader.ReadUInt32();
                header.DataLength = reader.ReadUInt32();
                header.BPP = reader.ReadUInt32();
                header.BitmaskRed = reader.ReadUInt32();
                header.BitmaskGreen = reader.ReadUInt32();
                header.BitmaskBlue = reader.ReadUInt32();
                header.BitmaskAlpha = reader.ReadUInt32();
                header.PVRTag = reader.ReadUInt32();
                header.NumSurfs = reader.ReadUInt32();

                // Read header version 3
                /*header.Version = reader.ReadUInt32();
                header.Flags = reader.ReadUInt32();
                header.PixelFormat = reader.ReadUInt64();
                header.ColorSpace = reader.ReadUInt32();
                header.ChannelType = reader.ReadUInt32();
                header.Height = reader.ReadUInt32();
                header.Width = reader.ReadUInt32();
                header.Depth = reader.ReadUInt32();
                header.NumSurfaces = reader.ReadUInt32();
                header.NumFaces = reader.ReadUInt32();
                header.MIPMapCount = reader.ReadUInt32();
                header.MetaDataSize = reader.ReadUInt32();

                while (stream.Position <= stream.Length)
                {
                    if (stream.Position + sizeof(int) > stream.Length) Debug.ThrowError("ImagePVR", "No data ID");
                    if (reader.ReadInt32() == Streams.MakeFourCC('P', 'V', 'R', (char)3)) break;
                }

                var key = reader.ReadUInt32();
                var dSize = reader.ReadUInt32();*/

                // Get Caps
                Compressed = true;

                // Get pixel format
                Size = new Size2((int)header.Width, (int)header.Height);
                int blockSize = 0, bpp = 0, blockWidth = 0, blockHeight = 0, blockDev = 1;
                //switch (header.PixelFormat)// version 3
                switch (header.Flags & PVR_TEXTURE_FLAG_TYPE_MASK)
                {
                    case FOURCC_2BPP_RGB:
                        FormatGL = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
                        blockSize = 8*4;
                        blockWidth = 8;
                        blockHeight = 4;
                        bpp = 2;
                        blockDev = 2;
                        ImageFormat = ImageFormats.PVR_RGB_2;
                        SurfaceFormat = SurfaceFormats.PVR_RGB_2;
                        break;

                    case FOURCC_2BPP_RGBA:
                        FormatGL = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
                        blockSize = 8*4;
                        blockWidth = 8;
                        blockHeight = 4;
                        bpp = 2;
                        blockDev = 2;
                        ImageFormat = ImageFormats.PVR_RGBA_2;
                        SurfaceFormat = SurfaceFormats.PVR_RGBA_2;
                        break;

                    case FOURCC_4BPP_RGB:
                        FormatGL = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
                        blockSize = 4*4;
                        blockWidth = 4;
                        blockHeight = 4;
                        bpp = 4;
                        ImageFormat = ImageFormats.PVR_RGB_4;
                        SurfaceFormat = SurfaceFormats.PVR_RGB_4;
                        break;

                    case FOURCC_4BPP_RGBA:
                        FormatGL = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
                        blockSize = 4*4;
                        blockWidth = 4;
                        blockHeight = 4;
                        bpp = 4;
                        ImageFormat = ImageFormats.PVR_RGBA_4;
                        SurfaceFormat = SurfaceFormats.PVR_RGBA_4;
                        break;

                    default:
                        Debug.ThrowError("ImagePVR", "Unsuported PVR Format");
                        break;
                }

                // Create Mipmaps
                header.NumMipmaps++;
                //Mipmaps = new Mipmap[header.MIPMapCount == 0 ? 1 : header.MIPMapCount];// version 3
                Mipmaps = new Mipmap[header.NumMipmaps == 0 ? 1 : header.NumMipmaps];
                var size = Size;
                for (int i = 0; i < Mipmaps.Length; ++i)
                {
                    int width = (size.Width/blockWidth), height = (size.Height/blockHeight);
                    if (width < 2) width = 2;
                    if (height < 2) height = 2;
                    int dataSize = (width * height) * ((blockSize * bpp) / 8);
                    var data = new byte[dataSize];
                    stream.Read(data, 0, dataSize);

                    Mipmaps[i] = new Mipmap(data, size.Width, size.Height, blockDev, 4);

                    size /= 2;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
        }
Beispiel #32
0
        public override void Serialize(IFileStream stream)
        {
            base.Serialize(stream);

            stream.SerializeValue(ref this._Unknown0);
            if (this.Unknown0 != 0)
            {
                throw new FormatException();
            }

            if (stream.Mode == SerializeMode.Reading)
            {
                this.Mipmaps.Clear();

                uint mipmapCount = 0;
                stream.SerializeValue(ref mipmapCount);

                for (uint i = 0; i < mipmapCount; i++)
                {
                    var mip = new Mipmap();
                    stream.SerializeValue(ref mip.Width);
                    stream.SerializeValue(ref mip.Height);
                    stream.SerializeValue(ref mip.Unknown2);

                    uint size = 0;
                    stream.SerializeValue(ref size);
                    stream.SerializeValue(ref mip.Data, size);

                    this.Mipmaps.Add(mip);
                }
            }
            else
            {
                throw new NotSupportedException();
            }

            uint unknown1 = 0;
            stream.SerializeValue(ref unknown1);
            if (unknown1 != 0)
            {
                throw new FormatException();
            }

            byte unknown2 = 0;
            stream.SerializeValue(ref unknown2);
            if (unknown2 != 0)
            {
                throw new FormatException();
            }
        }
Beispiel #33
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType = ImageTypes.PVR;

                // Load Header
                var header = new PVRHeader();
                var reader = new BinaryReader(stream);

                // Read header version 2
                header.HeaderLength = reader.ReadUInt32();
                header.Height       = reader.ReadUInt32();
                header.Width        = reader.ReadUInt32();
                header.NumMipmaps   = reader.ReadUInt32();
                header.Flags        = reader.ReadUInt32();
                header.DataLength   = reader.ReadUInt32();
                header.BPP          = reader.ReadUInt32();
                header.BitmaskRed   = reader.ReadUInt32();
                header.BitmaskGreen = reader.ReadUInt32();
                header.BitmaskBlue  = reader.ReadUInt32();
                header.BitmaskAlpha = reader.ReadUInt32();
                header.PVRTag       = reader.ReadUInt32();
                header.NumSurfs     = reader.ReadUInt32();

                // Read header version 3

                /*header.Version = reader.ReadUInt32();
                 * header.Flags = reader.ReadUInt32();
                 * header.PixelFormat = reader.ReadUInt64();
                 * header.ColorSpace = reader.ReadUInt32();
                 * header.ChannelType = reader.ReadUInt32();
                 * header.Height = reader.ReadUInt32();
                 * header.Width = reader.ReadUInt32();
                 * header.Depth = reader.ReadUInt32();
                 * header.NumSurfaces = reader.ReadUInt32();
                 * header.NumFaces = reader.ReadUInt32();
                 * header.MIPMapCount = reader.ReadUInt32();
                 * header.MetaDataSize = reader.ReadUInt32();
                 *
                 * while (stream.Position <= stream.Length)
                 * {
                 *      if (stream.Position + sizeof(int) > stream.Length) Debug.ThrowError("ImagePVR", "No data ID");
                 *      if (reader.ReadInt32() == Streams.MakeFourCC('P', 'V', 'R', (char)3)) break;
                 * }
                 *
                 * var key = reader.ReadUInt32();
                 * var dSize = reader.ReadUInt32();*/

                // Get Caps
                Compressed = true;

                // Get pixel format
                Size = new Size2((int)header.Width, (int)header.Height);
                int blockSize = 0, bpp = 0, blockWidth = 0, blockHeight = 0, blockDev = 1;
                //switch (header.PixelFormat)// version 3
                switch (header.Flags & PVR_TEXTURE_FLAG_TYPE_MASK)
                {
                case FOURCC_2BPP_RGB:
                    FormatGL      = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
                    blockSize     = 8 * 4;
                    blockWidth    = 8;
                    blockHeight   = 4;
                    bpp           = 2;
                    blockDev      = 2;
                    ImageFormat   = ImageFormats.PVR_RGB_2;
                    SurfaceFormat = SurfaceFormats.PVR_RGB_2;
                    break;

                case FOURCC_2BPP_RGBA:
                    FormatGL      = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
                    blockSize     = 8 * 4;
                    blockWidth    = 8;
                    blockHeight   = 4;
                    bpp           = 2;
                    blockDev      = 2;
                    ImageFormat   = ImageFormats.PVR_RGBA_2;
                    SurfaceFormat = SurfaceFormats.PVR_RGBA_2;
                    break;

                case FOURCC_4BPP_RGB:
                    FormatGL      = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
                    blockSize     = 4 * 4;
                    blockWidth    = 4;
                    blockHeight   = 4;
                    bpp           = 4;
                    ImageFormat   = ImageFormats.PVR_RGB_4;
                    SurfaceFormat = SurfaceFormats.PVR_RGB_4;
                    break;

                case FOURCC_4BPP_RGBA:
                    FormatGL      = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
                    blockSize     = 4 * 4;
                    blockWidth    = 4;
                    blockHeight   = 4;
                    bpp           = 4;
                    ImageFormat   = ImageFormats.PVR_RGBA_4;
                    SurfaceFormat = SurfaceFormats.PVR_RGBA_4;
                    break;

                default:
                    Debug.ThrowError("ImagePVR", "Unsuported PVR Format");
                    break;
                }

                // Create Mipmaps
                header.NumMipmaps++;
                //Mipmaps = new Mipmap[header.MIPMapCount == 0 ? 1 : header.MIPMapCount];// version 3
                Mipmaps = new Mipmap[header.NumMipmaps == 0 ? 1 : header.NumMipmaps];
                var size = Size;
                for (int i = 0; i < Mipmaps.Length; ++i)
                {
                    int width = (size.Width / blockWidth), height = (size.Height / blockHeight);
                    if (width < 2)
                    {
                        width = 2;
                    }
                    if (height < 2)
                    {
                        height = 2;
                    }
                    int dataSize = (width * height) * ((blockSize * bpp) / 8);
                    var data     = new byte[dataSize];
                    stream.Read(data, 0, dataSize);

                    Mipmaps[i] = new Mipmap(data, size.Width, size.Height, blockDev, 4);

                    size /= 2;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Beispiel #34
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType     = ImageTypes.BMPC;
                ImageFormat   = ImageFormats.BMPC;
                SurfaceFormat = SurfaceFormats.RGBAx8;

                using (var reader = new BinaryReader(stream))
                {
                    // File Type
                    int type = reader.ReadInt32();
                    if (type != Streams.MakeFourCC('b', 'm', 'p', 'c'))
                    {
                        throw new Exception("Not a .bmpc file");
                    }

                    // Version
                    float version = reader.ReadSingle();
                    if (version != 1.0f)
                    {
                        throw new Exception("Unsuported .bmpc version");
                    }

                    // Meta Data
                    Size = new Size2(reader.ReadInt32(), reader.ReadInt32());
                    bool zipCompressed = reader.ReadBoolean();

                    // Data
                    using (var decompressedDataStream = new MemoryStream())
                    {
                        int dataLength = reader.ReadInt32();
                        int dataRead   = 0;
                        do
                        {
                            int read = 1024;
                            if ((dataRead + read) > dataLength)
                            {
                                read -= (int)((dataRead + read) - dataLength);
                            }

                            var data = reader.ReadBytes(read);
                            decompressedDataStream.Write(data, 0, data.Length);

                            dataRead += read;
                        } while (dataRead < dataLength);
                        decompressedDataStream.Position = 0;

                                                #if NaCl || SILVERLIGHT
                        using (var zip = new GZipInputStream(decompressedDataStream))
                            using (var dataStream = new MemoryStream())
                            {
                                var buffer = new byte[4096];
                                int read   = 0;
                                do
                                {
                                    read = zip.Read(buffer, 0, buffer.Length);
                                    dataStream.Write(buffer, 0, buffer.Length);
                                } while (read > 0);

                                Mipmaps    = new Mipmap[1];
                                Mipmaps[0] = new Mipmap(dataStream.GetBuffer(), Size.Width, Size.Height, 1, 4);
                                if (flip)
                                {
                                    Mipmaps[0].FlipVertical();
                                }
                            }
                                                #else
                        using (var decompressedStream = new GZipStream(decompressedDataStream, CompressionMode.Decompress))
                            using (var dataStream = new MemoryStream())
                            {
                                decompressedStream.CopyTo(dataStream);
                                Mipmaps    = new Mipmap[1];
                                Mipmaps[0] = new Mipmap(dataStream.ToArray(), Size.Width, Size.Height, 1, 4);
                                if (flip)
                                {
                                    Mipmaps[0].FlipVertical();
                                }
                            }
                                                #endif
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            Loader.AddLoadable(this);
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
        public Mipmap[] GetMipmapsSummary()
        {
            Mipmap[] mipmapArray = new Mipmap[File.Header.NumMips];

            using (var reader = new BinaryReader(new MemoryStream(File.Raw)))
                for (int i = 0; i < File.Header.NumMips; i++) {
                    mipmapArray[i] = new Mipmap();
                    mipmapArray[i].Width = reader.ReadUInt16();
                    mipmapArray[i].Height = reader.ReadUInt16();
                    mipmapArray[i].Pitch = reader.ReadUInt16();
                    mipmapArray[i].DataSize = reader.ReadUInt32();
                }

            return mipmapArray;
        }
        public Mipmap GetMainMipmapSummary()
        {
            Mipmap mipmap = new Mipmap();

            using (var reader = new BinaryReader(new MemoryStream(File.Raw)))
            {
                mipmap.Width = reader.ReadUInt16();
                mipmap.Height = reader.ReadUInt16();
                mipmap.Pitch = reader.ReadUInt16();
                mipmap.DataSize = reader.ReadUInt32();
            }

            return mipmap;
        }
        public Mipmap GetMainMipmap()
        {
            Mipmap mipmap = new Mipmap();

            using (var reader = new BinaryReader(new MemoryStream(File.Raw)))
            {
                mipmap.Width = reader.ReadUInt16();
                mipmap.Height = reader.ReadUInt16();
                mipmap.Pitch = reader.ReadUInt16();
                mipmap.DataSize = reader.ReadUInt32();

                reader.BaseStream.Seek((File.Header.NumMips - 1) * 10, SeekOrigin.Current);

                mipmap.Data = reader.ReadBytes((int)mipmap.DataSize);
            }

            return mipmap;
        }
Beispiel #38
0
        public static Texture Decode(Stream stream)
        {
            Texture texture = new Texture();
            var     magic   = stream.ReadInt();

            if (magic != DDS_MAGIC)
            {
                throw new Exception("Invalid magic number in DDS header");
            }
            texture.Length = stream.ReadInt();
            texture.Flags  = stream.ReadInt();
            texture.Width  = stream.ReadInt();
            texture.Height = stream.ReadInt();
            texture.Pitch  = stream.ReadInt();
            texture.Depth  = stream.ReadInt();
            var count = stream.ReadInt();

            texture.Reverse = stream.Read(11);
            stream.Seek(37);
            var pfFlags = stream.ReadInt();

            if ((pfFlags & DDS_MIPMAP_COUNT) != 0)
            {
                texture.Count = Math.Max(1, count);
            }
            var format     = stream.ReadInt();
            var blockBytes = 0;

            switch (format)
            {
            case DXT_1:
                blockBytes     = 8;
                texture.Format = TextureFormat.RGB_S3TC_DXT1_Format;
                break;

            case DXT_3:
                blockBytes     = 16;
                texture.Format = TextureFormat.RGBA_S3TC_DXT3_Format;
                break;

            case DXT_5:
                blockBytes     = 16;
                texture.Format = TextureFormat.RGBA_S3TC_DXT5_Format;
                break;
            }
            var offset = texture.Length + 4;

            stream.Seek(offset, SeekOrigin.Begin);
            var width  = texture.Width;
            var height = texture.Height;
            var len    = width * height / 16 * blockBytes;

            texture.Mipmaps = new Mipmap[texture.Count];
            for (var i = 0; i < texture.Count; i++)
            {
                stream.Read(len, out byte[] data);
                switch (format)
                {
                case DXT_1:
                    data = DecodeDXT1(data, width, height);
                    break;

                case DXT_3:
                    data = DecodeDXT3(data, width, height);
                    break;

                case DXT_5:
                    data = DecodeDXT5(data, width, height);
                    break;
                }
                Mipmap mip = new Mipmap()
                {
                    Width  = width,
                    Height = height,
                    Data   = data
                };
                texture.Mipmaps[i] = mip;
                width  = Math.Max(width >> 1, 1);
                height = Math.Max(height >> 1, 1);
            }
            return(texture);
        }
Beispiel #39
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType = ImageTypes.BMPC;
                ImageFormat = ImageFormats.BMPC;
                SurfaceFormat = SurfaceFormats.RGBAx8;

                using (var reader = new BinaryReader(stream))
                {
                    // File Type
                    int type = reader.ReadInt32();
                    if (type != Streams.MakeFourCC('b', 'm', 'p', 'c')) throw new Exception("Not a .bmpc file");

                    // Version
                    float version = reader.ReadSingle();
                    if (version != 1.0f) throw new Exception("Unsuported .bmpc version");

                    // Meta Data
                    Size = new Size2(reader.ReadInt32(), reader.ReadInt32());
                    bool zipCompressed = reader.ReadBoolean();

                    // Data
                    using (var decompressedDataStream = new MemoryStream())
                    {
                        int dataLength = reader.ReadInt32();
                        int dataRead = 0;
                        do
                        {
                            int read = 1024;
                            if ((dataRead + read) > dataLength) read -= (int)((dataRead + read) - dataLength);

                            var data = reader.ReadBytes(read);
                            decompressedDataStream.Write(data, 0, data.Length);

                            dataRead += read;
                        } while (dataRead < dataLength);
                        decompressedDataStream.Position = 0;

                        #if NaCl || SILVERLIGHT
                        using (var zip = new GZipInputStream(decompressedDataStream))
                        using (var dataStream = new MemoryStream())
                        {
                            var buffer = new byte[4096];
                            int read = 0;
                            do
                            {
                                read = zip.Read(buffer, 0, buffer.Length);
                                dataStream.Write(buffer, 0, buffer.Length);

                            } while (read > 0);

                            Mipmaps = new Mipmap[1];
                            Mipmaps[0] = new Mipmap(dataStream.GetBuffer(), Size.Width, Size.Height, 1, 4);
                            if (flip) Mipmaps[0].FlipVertical();
                        }
                        #else
                        using (var decompressedStream = new GZipStream(decompressedDataStream, CompressionMode.Decompress))
                        using (var dataStream = new MemoryStream())
                        {
                            decompressedStream.CopyTo(dataStream);
                            Mipmaps = new Mipmap[1];
                            Mipmaps[0] = new Mipmap(dataStream.ToArray(), Size.Width, Size.Height, 1, 4);
                            if (flip) Mipmaps[0].FlipVertical();
                        }
                        #endif
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            Loader.AddLoadable(this);
            if (loadedCallback != null) loadedCallback(this, true);
        }