public void Run() { DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Ted\Documents\simple images"); FileInfo bumpFile = dir.File("bump-demo.png"); var bumpImage = UnmanagedGrayImage.Load(bumpFile); Size2 size = bumpImage.Size; var normalsImage = new UnmanagedRgbaImage(size); float strength = 2f; for (int y = 0; y < size.Height; ++y) { for (int x = 0; x < size.Width; ++x) { int index = ToIndex(size, x, y); float left = strength * DecodeByte(bumpImage[ToIndexClamped(size, x - 1, y)]); float right = strength * DecodeByte(bumpImage[ToIndexClamped(size, x + 1, y)]); float up = strength * DecodeByte(bumpImage[ToIndexClamped(size, x, y - 1)]); float down = strength * DecodeByte(bumpImage[ToIndexClamped(size, x, y + 1)]); var normal = new Vector3(left - right, down - up, 1); normal.Normalize(); normalsImage[index] = EncodeNormal(normal); } } FileInfo normalsFile = dir.File("bump-demo-to-normal.png"); normalsImage.Save(normalsFile); }
private UnmanagedRgbaImage DownloadImageFromGpu() { var context = device.ImmediateContext; context.CopyResource(resolveTexture, stagingTexture); var image = new UnmanagedRgbaImage(new Size2(Size, Size)); var resultImageData = context.MapSubresource(stagingTexture, 0, MapMode.Read, MapFlags.None); CopyDataBox(resultImageData, image.DataBox); context.UnmapSubresource(stagingTexture, 0); return(image); }
private Texture2D LoadOpacityTexture(FileInfo file, bool isLinear) { using (var image = UnmanagedRgbaImage.Load(file)) { var desc = new Texture2DDescription { Width = image.Size.Width, Height = image.Size.Height, MipLevels = 1, ArraySize = 1, Format = isLinear ? Format.R8G8B8A8_UNorm : Format.R8G8B8A8_UNorm_SRgb, SampleDescription = new SampleDescription(1, 0), BindFlags = BindFlags.ShaderResource }; return(new Texture2D(device, desc, image.DataRectangle)); } }
public static UnmanagedRgbaImage Load(FileInfo file) { using (var factory = new ImagingFactory()) using (var decoder = new BitmapDecoder(factory, file.FullName, new DecodeOptions { })) using (var frame = decoder.GetFrame(0)) { if (!SupportedInputPixelFormats.Contains(frame.PixelFormat)) { throw new InvalidOperationException($"unsupported pixel format: {frame.PixelFormat}"); } using (var converter = new FormatConverter(factory)) { converter.Initialize(frame, PixelFormat.Format32bppBGR); var size = converter.Size; var image = new UnmanagedRgbaImage(size); converter.CopyPixels(image.Stride, image.PixelData, image.SizeInBytes); return(image); } } }
private void ImportTexture(string name, TextureProcessingSettings settings) { string destinationPath = Path.Combine(destinationFolder.FullName, name + ".dds"); if (new FileInfo(destinationPath).Exists) { return; } Console.WriteLine($"importing texture '{name}'..."); using (var image = UnmanagedRgbaImage.Load(settings.File)) { using (var dilator = new TextureDilator(device, shaderCache)) { dilator.Dilate(settings.Mask, image.Size, settings.IsLinear, image.DataBox); } InputOptions input = new InputOptions(); input.SetFormat(InputFormat.BGRA_8UB); input.SetTextureLayout(TextureType.Texture2D, image.Size.Width, image.Size.Height, 1); float gamma = settings.IsLinear ? 1f : 2.2f; input.SetGamma(gamma, gamma); input.SetMipmapData(image.PixelData, image.Size.Width, image.Size.Height, 1, 0, 0); input.SetAlphaMode(AlphaMode.None); input.SetMipmapGeneration(true); input.SetMipmapFilter(MipmapFilter.Kaiser); input.SetKaiserParameters(3, 4, 1); if (settings.Type == TextureProcessingType.Bump) { input.SetConvertToNormalMap(true); input.SetNormalFilter(1, 0, 0, 0); input.SetHeightEvaluation(1, 1, 1, 0); } else if (settings.Type == TextureProcessingType.Normal) { input.SetNormalMap(true); } CompressionOptions compression = new CompressionOptions(); compression.SetQuality(Quality.Highest); compression.SetFormat(Format.RGBA); OutputOptions output = new OutputOptions(); destinationFolder.CreateWithParents(); output.SetFileName(destinationPath); output.SetContainer(Container.Container_DDS10); output.SetSrgbFlag(!settings.IsLinear); var compressor = new Compressor(); bool succeeded = compressor.Compress(input, compression, output); if (!succeeded) { throw new InvalidOperationException("texture conversion failed"); } //force the previous output handler to be destructed so that the file is flushed and closed output.SetFileName("nul"); if (compress) { CompressTexture(new FileInfo(destinationPath), settings.Type, settings.IsLinear); } } }