Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TexAtlas.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Xml;
7 using System.Xml.Linq;
8 
9 namespace SiliconStudio.TextureConverter
10 {
11  /// <summary>
12  /// A texture atlas : a texture made from a composition of many textures.
13  /// </summary>
14  public class TexAtlas : TexImage
15  {
16  /// <summary>
17  /// The atlas inner textures disposition
18  /// </summary>
19  public TexLayout Layout { get; internal set; }
20 
21 
22  /// <summary>
23  /// Initializes a new instance of the <see cref="TexAtlas"/> class.
24  /// </summary>
25  internal TexAtlas() : base()
26  {
27  Layout = new TexLayout();
28  }
29 
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="TexAtlas"/> class.
33  /// </summary>
34  /// <param name="layout">The layout.</param>
35  /// <param name="atlas">The atlas.</param>
36  public TexAtlas(TexLayout layout, TexImage atlas)
37  : base(atlas.Data, atlas.DataSize, atlas.Width, atlas.Height, atlas.Depth, atlas.Format, atlas.MipmapCount, atlas.ArraySize, atlas.Dimension, atlas.FaceCount)
38  {
39  RowPitch = atlas.RowPitch;
40  SlicePitch = atlas.SlicePitch;
41  SubImageArray = atlas.SubImageArray;
42  Name = atlas.Name;
43  DisposingLibrary = atlas.DisposingLibrary;
44  CurrentLibrary = atlas.CurrentLibrary;
45  LibraryData = atlas.LibraryData;
46  Layout = layout;
47  Name = "";
48  }
49 
50  public override Object Clone(bool CopyMemory)
51  {
52  var atlas = new TexAtlas(Layout, (TexImage)base.Clone(CopyMemory));
53 
54  atlas.Layout = new TexLayout();
55  foreach (var entry in Layout.TexList)
56  {
57  atlas.Layout.TexList.Add(entry.Key, entry.Value);
58  }
59 
60  return atlas;
61  }
62 
63  /// <summary>
64  /// Update the image size and the atlas layout data.
65  /// </summary>
66  /// <param name="width">The width.</param>
67  /// <param name="height">The height.</param>
68  internal override void Rescale(int width, int height)
69  {
70  double ratio = (double)width / Width;
71  base.Rescale(width, height);
72 
73  var texList = new Dictionary<string, TexLayout.Position>();
74  TexLayout.Position current;
75 
76  foreach (var entry in Layout.TexList)
77  {
78  current = entry.Value;
79  current.Width = (int)(entry.Value.Width * ratio);
80  current.Height = (int)(entry.Value.Height * ratio);
81  current.UOffset = (int)(entry.Value.UOffset * ratio);
82  current.VOffset = (int)(entry.Value.VOffset * ratio);
83 
84  texList.Add(entry.Key, current);
85  }
86 
87  Layout.TexList = texList;
88  }
89 
90 
91  /// <summary>
92  /// Update the atlas layout data.
93  /// </summary>
94  /// <param name="orientation">The orientation.</param>
95  internal override void Flip(Orientation orientation)
96  {
97  var texList = new Dictionary<string, TexLayout.Position>();
98  TexLayout.Position current;
99 
100  foreach (var entry in Layout.TexList)
101  {
102  current = entry.Value;
103  if(orientation == Orientation.Horizontal) current.UOffset = Width - entry.Value.UOffset - entry.Value.Width;
104  else current.VOffset = Height - entry.Value.VOffset - entry.Value.Height;
105 
106  texList.Add(entry.Key, current);
107  }
108 
109  Layout.TexList = texList;
110  }
111 
112 
113  /// <summary>
114  /// Exports the layout data into a file with the same name of the atlas file.
115  /// </summary>
116  /// <param name="file">The atlas file.</param>
117  internal override void Save(string file)
118  {
119  Layout.Export(Path.ChangeExtension(file, TexLayout.Extension));
120  }
121 
122 
123  /// <summary>
124  /// Describes the positions and size of every inner textures of a texture atlas
125  /// </summary>
126  public class TexLayout
127  {
128  /// <summary>
129  /// The list of textures position, indexed by their name.
130  /// </summary>
131  public Dictionary<string, Position> TexList { get; internal set; }
132 
133 
134  /// <summary>
135  /// The extension of the atlas layout file
136  /// </summary>
137  public static readonly string Extension = ".ats";
138 
139 
140  /// <summary>
141  /// Initializes a new instance of the <see cref="TexLayout"/> class.
142  /// </summary>
143  public TexLayout()
144  {
145  TexList = new Dictionary<string, Position>();
146  }
147 
148 
149  /// <summary>
150  /// Contains the needed informatio to retrieve an inner texture from the corresponding texture atlas.
151  /// </summary>
152  public struct Position
153  {
154  public int UOffset;
155  public int VOffset;
156  public int Width;
157  public int Height;
158 
159  public Position(int uOffset, int vVOffset, int width, int height)
160  {
161  UOffset = uOffset;
162  VOffset = vVOffset;
163  Width = width;
164  Height = height;
165  }
166 
167  public override String ToString()
168  {
169  return "u:" + UOffset + ", v:" + VOffset + ", w:" + Width + ", h:" + Height;
170  }
171  }
172 
173 
174  /// <summary>
175  /// Exports the atlas layout into a specified file.
176  /// </summary>
177  /// <remarks>
178  /// Data are exported into an xml format
179  /// </remarks>
180  /// <param name="filePath">The file path.</param>
181  public void Export(string filePath)
182  {
183  using (var file = new StreamWriter(@filePath, false))
184  {
185  XElement xml = new XElement("texture-list");
186 
187  //file.WriteLine("<texture-list>");
188  foreach (var entry in TexList)
189  {
190  xml.Add(new XElement("texture",
191  new XAttribute("name", entry.Key),
192  new XAttribute("uOffset", entry.Value.UOffset),
193  new XAttribute("vOffset", entry.Value.VOffset),
194  new XAttribute("width", entry.Value.Width),
195  new XAttribute("height", entry.Value.Height)));
196 
197  //file.WriteLine(" <texture name=\"" + entry.Key + "\" uOffset=\"" + entry.Value.UOffset + "\" vOffset=\"" + entry.Value.VOffset + "\" width=\"" + entry.Value.Width + "\" height=\"" + entry.Value.Height + "\" />");
198  }
199  //file.WriteLine("</texture-list>");
200  file.Write(xml);
201  }
202  }
203 
204 
205  /// <summary>
206  /// Create an instance of <see cref="TexLayout"/> from a layout file.
207  /// </summary>
208  /// <param name="file">The file.</param>
209  /// <returns>
210  /// A new instance of <see cref="TexLayout"/>.
211  /// </returns>
212  public static TexLayout Import(string file)
213  {
214  var texLayout = new TexLayout();
215 
216  using (var reader = XmlReader.Create(file))
217  {
218  while (reader.ReadToFollowing("texture"))
219  {
220  texLayout.TexList.Add(
221  reader.GetAttribute("name"),
222  new Position(
223  int.Parse(reader.GetAttribute("uOffset")),
224  int.Parse(reader.GetAttribute("vOffset")),
225  int.Parse(reader.GetAttribute("width")),
226  int.Parse(reader.GetAttribute("height"))
227  ));
228  }
229  }
230 
231  return texLayout;
232  }
233 
234  }
235  }
236 }
Contains the needed informatio to retrieve an inner texture from the corresponding texture atlas...
Definition: TexAtlas.cs:152
override Object Clone(bool CopyMemory)
Creates a new object that is a copy of the current instance.
Definition: TexAtlas.cs:50
TexAtlas(TexLayout layout, TexImage atlas)
Initializes a new instance of the TexAtlas class.
Definition: TexAtlas.cs:36
Position(int uOffset, int vVOffset, int width, int height)
Definition: TexAtlas.cs:159
TexLayout()
Initializes a new instance of the TexLayout class.
Definition: TexAtlas.cs:143
Describes the positions and size of every inner textures of a texture atlas
Definition: TexAtlas.cs:126
A texture atlas : a texture made from a composition of many textures.
Definition: TexAtlas.cs:14
Temporary format containing texture data and information. Used as buffer between texture libraries...
Definition: TexImage.cs:13
Android.Widget.Orientation Orientation
Definition: Section.cs:9
static TexLayout Import(string file)
Create an instance of TexLayout from a layout file.
Definition: TexAtlas.cs:212
void Export(string filePath)
Exports the atlas layout into a specified file.
Definition: TexAtlas.cs:181