public void Create(string xpacksFolder, ImageMapInformation map, UpdateCallback updateCallback, CompletedCallback completedCallback) { IsFetchingData = true; Cancel = false; try { // Create the Main texture. CreateTexture(xpacksFolder, map); if (!Cancel) { completedCallback?.Invoke(true); } } catch { completedCallback?.Invoke(false); // Report in that something went wrong. } IsFetchingData = false; }
public void SetCoverage(ref ImageMapInformation map, double desiredWidth, double desiredHeight) { map.CoverageX = Math.Max(desiredWidth, desiredHeight); map.CoverageZ = map.CoverageX; }
private void CreateTexture(string xpacksFolder, ImageMapInformation map) { SharpDX.Direct3D11.Device DX11_Device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.None); string filename = "", filenameIntermediate = ""; int width, height; filename = Path.Combine(xpacksFolder, map.Filename); filenameIntermediate = Path.Combine(xpacksFolder, map.FilenameIntermediate); width = map.Width; height = map.Height; // Create the file to the destination folder. Texture2DDescription textureDesc = new Texture2DDescription() { Width = width, Height = height, MipLevels = 1, ArraySize = 1, Format = Format.R8G8B8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None }; var tex = new Texture2D(DX11_Device, textureDesc); double centrex = width * 0.5; byte[] buffer = new byte[4 * width * height]; int bufferoffset = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double dx = Math.Abs(centrex - x) / centrex * 1.1; double dy = 1.2 - (y / (double)height * 1.1); double radius = Math.Sqrt(dx * dx + dy * dy); double rainbow = radius * 2.0 - 1.0; if (rainbow < 0 || rainbow > 1) { buffer[bufferoffset] = (int)0; } else { var color = HSL2RGB(rainbow, 0.5, 0.5); buffer[bufferoffset] = color.R; buffer[bufferoffset + 1] = color.G; buffer[bufferoffset + 2] = color.B; buffer[bufferoffset + 3] = color.A; } bufferoffset += 4; } } // Copy the Buffer. DataStream ds; DataBox db = DX11_Device.ImmediateContext.MapSubresource(tex, 0, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out ds); Marshal.Copy(buffer, 0, db.DataPointer, buffer.Length); // Write with Marshal as it is quicker than DataStream. DX11_Device.ImmediateContext.UnmapSubresource(tex, 0); // Save the image. ImageFileFormat iff = filename.ToLower().EndsWith(".jpg") ? ImageFileFormat.Jpg : ImageFileFormat.Png; string folder = Path.GetDirectoryName(filename); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); // Create the folder if it doesn't exist. } Texture2D.ToFile(DX11_Device.ImmediateContext, tex, iff, filename); // Dispose. DX11_Device.Dispose(); DX11_Device = null; }