示例#1
0
        public unsafe SafeHGlobalHandle ReadImage()
        {
            SafeHGlobalHandle result = new SafeHGlobalHandle(GetImageSize());
            byte *            ptr    = (byte *)result.DangerousGetHandle().ToPointer();

            using (DisposableAction insurance = new DisposableAction(result.Dispose))
            {
                for (int y = 0; y < _header.BlockCount; y++)
                {
                    byte *blockPtr = ptr + y * _header.BlockSize * 2;

                    for (int x = 0, i = 0; x < _header.BlockSize; x++)
                    {
                        byte b = (byte)_input.ReadByte();

                        *(blockPtr + i)     = (byte)(b & 0x33);
                        *(blockPtr + i + 8) = (byte)((b >> 2) & 0x33);
                        if (++i % 8 == 0)
                        {
                            i += 8;
                        }
                    }
                }

                insurance.Cancel();
            }

            return(result);
        }
示例#2
0
        public override async Task <GLTexture> ReadTextureAsync(CancellationToken cancelationToken)
        {
            if (cancelationToken.IsCancellationRequested)
            {
                return(RaiseTextureReaded(null));
            }

            BitmapData bmdata = _bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, _format);

            using (DisposableAction unlocker = new DisposableAction(() => _bitmap.UnlockBits(bmdata)))
            {
                GLTexture texture;
                using (GLService.AcquireContext())
                    texture = new GLTexture(GL.GenTexture(), _width, _height, _format);

                using (DisposableAction insurance = new DisposableAction(texture.Dispose))
                {
                    if (cancelationToken.IsCancellationRequested)
                    {
                        return(RaiseTextureReaded(null));
                    }

                    using (GLService.AcquireContext())
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                        GL.TexImage2D(TextureTarget.Texture2D, 0, _format, bmdata.Width, bmdata.Height, 0, _format, _format, bmdata.Scan0);
                    }

                    //ErrorCode error = GL.GetError();
                    //if (error != ErrorCode.NoError)
                    //    throw new ArgumentException("Error building TexImage. GL Error: " + error);

                    _bitmap.UnlockBits(bmdata);
                    unlocker.Cancel();

                    if (cancelationToken.IsCancellationRequested)
                    {
                        return(RaiseTextureReaded(null));
                    }

                    using (GLService.AcquireContext())
                    {
                        GL.BindTexture(TextureTarget.Texture2D, texture.Id);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                    }

                    insurance.Cancel();
                }
                return(RaiseTextureReaded(texture));
            }
        }
示例#3
0
        public SafeHGlobalHandle GetUnmanagedPixelsArray(PixelFormatDescriptor format)
        {
            SafeHGlobalHandle result = new SafeHGlobalHandle(Width * Height * format.BytesPerPixel);

            using (GLService.AcquireContext())
                using (DisposableAction insurance = new DisposableAction(result.Dispose))
                {
                    GL.BindTexture(TextureTarget.Texture2D, Id);
                    GL.GetTexImage(TextureTarget.Texture2D, 0, format, format, result.DangerousGetHandle());

                    insurance.Cancel();
                }
            return(result);
        }
示例#4
0
        public GLPalettedTextureShaderProgram()
        {
            using (DisposableAction insurance = new DisposableAction(Dispose))
            {
                using (GLService.AcquireContext())
                    using (GLShader fs = GLShader.CompileShaderFromSource(FragmentShaderSource, ShaderType.FragmentShader))
                        using (GLShader vs = GLShader.CompileShaderFromSource(VertexShaderSource, ShaderType.VertexShader))
                        {
                            GL.AttachShader(Id, fs.Id);
                            GL.AttachShader(Id, vs.Id);
                            Link();
                        }

                insurance.Cancel();
            }
        }
示例#5
0
        public FileSegment GetMetricFileStream(MemoryMappedFileAccess access)
        {
            if (ParentArchive != null)
            {
                MemoryMappedFile mmf = GetMemoryMappedFile(access);
                return(new FileSegment(mmf, MetricsEntry.GetAbsoluteOffset(), MetricsEntry.UncompressedContentSize, access));
            }

            string     path   = Path.Combine(Options.GameDataDirectoryPath, Name + ".fi");
            FileStream stream = new FileStream(path, FileMode.Open, ConvertAccess(access), FileShare.ReadWrite);

            using (DisposableAction insurance = new DisposableAction(stream.Dispose))
            {
                MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(stream, null, 0, access, null, HandleInheritability.Inheritable, false);

                insurance.Cancel();
                return(new FileSegment(mmf, 0, stream.Length, access));
            }
        }