Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageProcessCommand.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Threading.Tasks;
6 using SiliconStudio.Core.Diagnostics;
7 using SiliconStudio.Paradox.Graphics;
8 using SiliconStudio.Core.Serialization;
9 using SiliconStudio.Core.Serialization.Assets;
10 using SiliconStudio.TextureConverter;
11 
12 namespace SiliconStudio.BuildEngine
13 {
14  [Description("Process image")]
16  {
17  /// <inheritdoc/>
18  public override string Title { get { return "Process image " + (InputUrl ?? "[InputUrl]"); } }
19 
20  public string InputUrl { get; set; }
21  public string OutputUrl { get; set; }
22 
23  public float Width { get; set; }
24  public float Height { get; set; }
25 
26  public bool IsAbsolute { get; set; }
27 
28  public PixelFormat? Format { get; set; }
29 
30  public bool GenerateMipmaps { get; set; }
31 
33  {
34  Width = 100.0f;
35  Height = 100.0f;
36  }
37 
39  {
40  yield return new ObjectUrl(UrlType.Internal, InputUrl);
41  }
42 
43  protected override Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
44  {
45  var assetManager = new AssetManager();
46 
47  // Load image
48  var image = assetManager.Load<Image>(InputUrl);
49 
50  // Initialize TextureTool library
51  using (var texTool = new TextureTool())
52  using (var texImage = texTool.Load(image))
53  {
54  var outputFormat = Format.HasValue ? Format.Value : image.Description.Format;
55 
56  // Apply transformations
57  texTool.Decompress(texImage);
58  if (IsAbsolute)
59  {
60  texTool.Resize(texImage, (int)Width, (int)Height, Filter.Rescaling.Lanczos3);
61  }
62  else
63  {
64  texTool.Rescale(texImage, Width / 100.0f, Height / 100.0f, Filter.Rescaling.Lanczos3);
65  }
66 
67  // Generate mipmaps
68  if (GenerateMipmaps)
69  {
70  texTool.GenerateMipMaps(texImage, Filter.MipMapGeneration.Box);
71  }
72 
73  // Convert/Compress to output format
74  texTool.Compress(texImage, outputFormat);
75 
76  // Save
77  using (var outputImage = texTool.ConvertToParadoxImage(texImage))
78  {
79  assetManager.Save(OutputUrl, outputImage);
80 
81  commandContext.Logger.Info("Compression successful [{3}] to ({0}x{1},{2})",
82  outputImage.Description.Width,
83  outputImage.Description.Height, outputImage.Description.Format, OutputUrl);
84  }
85  }
86 
87  return Task.FromResult(ResultStatus.Successful);
88  }
89 
90  protected override void ComputeParameterHash(Stream stream)
91  {
92  base.ComputeParameterHash(stream);
93 
94  // Really necesary? (need system for identical blob reuse)
95  var writer = new BinarySerializationWriter(stream);
96  writer.Write(InputUrl);
97  writer.Write(OutputUrl);
98  writer.Write(Width);
99  writer.Write(Height);
100  writer.Write(IsAbsolute);
101  writer.Write(Format);
102  writer.Write(GenerateMipmaps);
103  }
104 
105  public override string ToString()
106  {
107  return "Process image " + (InputUrl ?? "[InputUrl]") + " > " + (OutputUrl ?? "[OutputUrl]");
108  }
109  }
110 }
override IEnumerable< ObjectUrl > GetInputFiles()
Gets the list of input files (that can be deduced without running the command, only from command para...
override Task< ResultStatus > DoCommandOverride(ICommandContext commandContext)
The method to override containing the actual command code. It is called by the DoCommand function ...
Provides method to load images or textures, to modify them and to convert them with different texture...
Definition: TextureTool.cs:24
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
Implements SerializationStream as a binary writer.
A Command that reads and/or writes to the index file.
override void ComputeParameterHash(Stream stream)
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32