Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TexImage.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 
6 using SiliconStudio.Core;
7 
8 namespace SiliconStudio.TextureConverter
9 {
10  /// <summary>
11  /// Temporary format containing texture data and information. Used as buffer between texture libraries.
12  /// </summary>
13  public class TexImage : IDisposable, ICloneable
14  {
15  // Basic infos
16  public IntPtr Data { get; internal set; }
17  public int DataSize { get; internal set; }
18  public int Width { get; internal set; }
19  public int Height { get; internal set; }
20  public int Depth { get; internal set; }
21  public int RowPitch { get; internal set; }
22  public int SlicePitch { get; internal set; }
23  public SiliconStudio.Paradox.Graphics.PixelFormat Format { get; internal set; }
24 
25  // Texture infos
26  public int ArraySize { get; internal set; }
27  public int MipmapCount { get; internal set; }
28  public SubImage[] SubImageArray { get; internal set; }
29  public TextureDimension Dimension { get; internal set; }
30  public string Name { get; set; }
31 
32  // PVRTT needs
33  public int FaceCount { get; internal set; }
34 
35  // ITexLibrary Data
36  internal ITexLibrary DisposingLibrary { get; set; }
37  internal ITexLibrary CurrentLibrary { get; set; }
38  internal Dictionary<ITexLibrary, ITextureLibraryData> LibraryData { get; set; }
39 
40  // Disposing info
41  private bool Disposed;
42 
43  /// <summary>
44  /// The Different types of texture
45  /// </summary>
46  public enum TextureDimension
47  {
48  Texture1D,
49  Texture2D,
50  Texture3D,
51  TextureCube,
52  }
53 
54  /// <summary>
55  /// A structure describing an image of one mip map level (of one member in an array texture).
56  /// </summary>
57  public struct SubImage
58  {
59  public IntPtr Data { get; set; }
60  public int DataSize { get; set; }
61  public int Width { get; set; }
62  public int Height { get; set; }
63  public int RowPitch { get; set; }
64  public int SlicePitch { get; set; }
65 
66  public override String ToString()
67  {
68  return "Size:"+ DataSize +"\nwidth:" + Width + "\nheight:" + Height + "\nrowPitch:" + RowPitch + "\nslicePitch:" + SlicePitch + "\nData:" + Data;
69  }
70  }
71 
72  /// <summary>
73  /// Initializes a new instance of the <see cref="TexImage"/> class.
74  /// </summary>
75  internal TexImage(){
76  MipmapCount = 1;
77  ArraySize = 1;
78  FaceCount = 1;
79  Depth = 1;
80  Dimension = TextureDimension.Texture2D;
81  Name = "";
82 
83  SubImageArray = new SubImage[1];
84  Format = SiliconStudio.Paradox.Graphics.PixelFormat.B8G8R8A8_UNorm;
85 
86  LibraryData = new Dictionary<ITexLibrary, ITextureLibraryData>();
87 
88  Disposed = false;
89  }
90 
91 
92  /// <summary>
93  /// Initializes a new instance of the <see cref="TexImage"/> class.
94  /// </summary>
95  /// <param name="data">The data.</param>
96  /// <param name="dataSize">Size of the data.</param>
97  /// <param name="width">The width.</param>
98  /// <param name="height">The height.</param>
99  /// <param name="depth">The depth.</param>
100  /// <param name="format">The format.</param>
101  /// <param name="mipmapCount">The mipmap count.</param>
102  /// <param name="arraySize">Size of the array.</param>
103  /// <param name="dimension">The dimension.</param>
104  /// <param name="faceCount">The face count (multiple of 6 if Texture Cube, 1 otherwise).</param>
105  public TexImage(IntPtr data, int dataSize, int width, int height, int depth, SiliconStudio.Paradox.Graphics.PixelFormat format, int mipmapCount, int arraySize, TextureDimension dimension, int faceCount = 1)
106  {
107  Data = data;
108  DataSize = dataSize;
109  Width = width;
110  Height = height;
111  Depth = depth;
112  Format = format;
113  MipmapCount = mipmapCount;
114  ArraySize = arraySize;
115  Dimension = dimension;
116  FaceCount = faceCount;
117  Name = "";
118 
119  int imageCount;
120  if (Dimension == TexImage.TextureDimension.Texture3D)
121  {
122  int subImagePerArrayElementCount = 0;
123  int curDepth = Depth;
124  for (int i = 0; i < MipmapCount; ++i)
125  {
126  subImagePerArrayElementCount += curDepth;
127  curDepth = curDepth > 1 ? curDepth >>= 1 : curDepth;
128  }
129 
130  imageCount = (int)(ArraySize * FaceCount * subImagePerArrayElementCount);
131  }
132  else
133  {
134  imageCount = (int)(ArraySize * FaceCount * MipmapCount);
135  }
136 
137  SubImageArray = new SubImage[imageCount];
138  int ct = 0;
139  int rowPitch, slicePitch, curHeight, curWidth;
140 
141  Tools.ComputePitch(Format, Width, Height, out rowPitch, out slicePitch);
142  RowPitch = rowPitch;
143  SlicePitch = slicePitch;
144 
145  for (uint i = 0; i < FaceCount; ++i)
146  {
147  for (uint j = 0; j < ArraySize; ++j)
148  {
149  depth = Depth;
150  for (uint k = 0; k < MipmapCount; ++k)
151  {
152  curWidth = Width;
153  curHeight = Height;
154  Tools.ComputePitch(Format, curWidth, curHeight, out rowPitch, out slicePitch);
155 
156  for (int l = 0; l < depth; ++l)
157  {
158  SubImageArray[ct] = new TexImage.SubImage();
159  SubImageArray[ct].Width = curWidth;
160  SubImageArray[ct].Height = curHeight;
161  SubImageArray[ct].RowPitch = rowPitch;
162  SubImageArray[ct].SlicePitch = slicePitch;
163  SubImageArray[ct].DataSize = slicePitch;
164  SubImageArray[ct].Data = new IntPtr(Data.ToInt64() + l * slicePitch);
165  ++ct;
166  }
167  depth = depth > 1 ? depth >>= 1 : depth;
168  }
169  }
170  }
171 
172  LibraryData = new Dictionary<ITexLibrary, ITextureLibraryData>();
173 
174  Disposed = false;
175  }
176 
177 
178  public override bool Equals(object obj)
179  {
180  if (ReferenceEquals(null, obj)) return false;
181  if (ReferenceEquals(this, obj)) return true;
182  if (obj.GetType() != this.GetType()) return false;
183 
184  TexImage img = (TexImage)obj;
185 
186  if (SubImageArray.Length != img.SubImageArray.Length) return false;
187  for (int i = 0; i < SubImageArray.Length; ++i)
188  {
189  if (!(SubImageArray[i].DataSize == img.SubImageArray[i].DataSize
190  && SubImageArray[i].Width == img.SubImageArray[i].Width
191  && SubImageArray[i].Height == img.SubImageArray[i].Height
192  && SubImageArray[i].RowPitch == img.SubImageArray[i].RowPitch
193  && SubImageArray[i].SlicePitch == img.SubImageArray[i].SlicePitch))
194  return false;
195  }
196 
197  return Width == img.Width
198  && Height == img.Height
199  && Depth == img.Depth
200  && Format == img.Format
201  && MipmapCount == img.MipmapCount
202  && ArraySize == img.ArraySize
203  && FaceCount == img.FaceCount
204  && Dimension == img.Dimension
205  && DataSize == img.DataSize
206  && RowPitch == img.RowPitch
207  && SlicePitch == img.SlicePitch;
208  }
209 
210  public override int GetHashCode()
211  {
212  unchecked
213  {
214  return Width*Height*Depth*(int)Format*MipmapCount*ArraySize;
215  }
216  }
217 
218 
219  /// <summary>
220  /// Creates a new object that is a copy of the current instance.
221  /// </summary>
222  /// <remarks>
223  /// This is a deep copy.
224  /// </remarks>
225  /// <returns>
226  /// A new object that is a copy of this instance.
227  /// </returns>
228  public Object Clone()
229  {
230  return Clone(true);
231  }
232 
233 
234  /// <summary>
235  /// Creates a new object that is a copy of the current instance.
236  /// </summary>
237  /// <param name="CopyMemory">if set to <c>true</c> [copy memory], it is a DEEP copy.</param>
238  /// <returns>
239  /// A new object that is a copy of this instance.
240  /// </returns>
241  virtual public Object Clone(bool CopyMemory)
242  {
243  if (this.CurrentLibrary != null) { this.CurrentLibrary.EndLibrary(this); this.CurrentLibrary = null; }
244 
245  TexImage newTex = new TexImage()
246  {
247  // Basic infos
248  Data = CopyMemory?System.Runtime.InteropServices.Marshal.AllocHGlobal(this.DataSize):this.Data,
249  DataSize = this.DataSize,
250  Width = this.Width,
251  Height = this.Height,
252  Depth = this.Depth,
253  RowPitch = this.RowPitch,
254  SlicePitch = this.SlicePitch,
255  Format = this.Format,
256 
257  // Texture infos
258  ArraySize = this.ArraySize,
259  FaceCount = this.FaceCount,
260  MipmapCount = this.MipmapCount,
261  SubImageArray = new SubImage[this.SubImageArray.Length],
262  Dimension = this.Dimension,
263  Name = this.Name,
264 
265  // ITexLibrary Data
266  DisposingLibrary = this.DisposingLibrary,
267  CurrentLibrary = this.CurrentLibrary,
268  LibraryData = new Dictionary<ITexLibrary, ITextureLibraryData>(),
269 
270  // Disposing info
271  Disposed = this.Disposed,
272  };
273 
274  if (CopyMemory) Utilities.CopyMemory(newTex.Data, this.Data, this.DataSize);
275 
276  int offset = 0;
277  for (int i = 0; i < this.SubImageArray.Length; ++i)
278  {
279  newTex.SubImageArray[i] = this.SubImageArray[i];
280  if (CopyMemory) newTex.SubImageArray[i].Data = new IntPtr(newTex.Data.ToInt64() + offset);
281  offset += newTex.SubImageArray[i].DataSize;
282  }
283 
284  if (CopyMemory && this.DisposingLibrary != null)
285  {
286  this.DisposingLibrary.StartLibrary(newTex);
287  }
288  else if (!CopyMemory)
289  {
290  newTex.DisposingLibrary = null;
291  }
292 
293  return newTex;
294  }
295 
296 
297  /// <summary>
298  /// Forces the last current library to update the image data.
299  /// </summary>
300  public void Update()
301  {
302  if (CurrentLibrary != null) CurrentLibrary.EndLibrary(this);
303  CurrentLibrary = null;
304  }
305 
306 
307  /// <summary>
308  /// Update the image size
309  /// </summary>
310  /// <remarks>
311  /// This method was designed for child class to override it
312  /// </remarks>
313  /// <param name="width">The width.</param>
314  /// <param name="height">The height.</param>
315  internal virtual void Rescale(int width, int height)
316  {
317  Width = width;
318  Height = height;
319  }
320 
321 
322  /// <summary></summary>
323  /// <remarks>
324  /// This method was designed for child class to override it
325  /// </remarks>
326  /// <param name="orientation">The orientation.</param>
327  internal virtual void Flip(Orientation orientation) {}
328 
329 
330  /// <summary></summary>
331  /// <remarks>
332  /// This method was designed for child class to override it
333  /// </remarks>
334  /// <param name="file">The file.</param>
335  internal virtual void Save(string file) { }
336 
337 
338  /// <summary>
339  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
340  /// </summary>
341  public void Dispose()
342  {
343  if (CurrentLibrary != null) CurrentLibrary.EndLibrary(this); // Asking the last used library to update the instance of TexImage with its last native data.
344  if (Disposed) return;
345  if (DisposingLibrary != null) DisposingLibrary.Dispose(this); // Asking the library which allocated the memory to free it.
346  Disposed = true;
347  }
348 
349 
350  public override string ToString()
351  {
352  return "Image - Dimension:" + Dimension + " - Format:" + Format + " - " + Width + " x " + Height + " x " + Depth + " - MipmapCount:" + MipmapCount + " - ArraySize:" + ArraySize + " - SubImageArray Length:" + SubImageArray.Length;
353  }
354  }
355 }
Object Clone()
Creates a new object that is a copy of the current instance.
Definition: TexImage.cs:228
TextureDimension
The Different types of texture
Definition: TexImage.cs:46
override bool Equals(object obj)
Definition: TexImage.cs:178
void Update()
Forces the last current library to update the image data.
Definition: TexImage.cs:300
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Definition: TexImage.cs:341
virtual Object Clone(bool CopyMemory)
Creates a new object that is a copy of the current instance.
Definition: TexImage.cs:241
TexImage(IntPtr data, int dataSize, int width, int height, int depth, SiliconStudio.Paradox.Graphics.PixelFormat format, int mipmapCount, int arraySize, TextureDimension dimension, int faceCount=1)
Initializes a new instance of the TexImage class.
Definition: TexImage.cs:105
The audio engine is disposed. The current instance cannot be used to play or create sounds anymore...
A structure describing an image of one mip map level (of one member in an array texture).
Definition: TexImage.cs:57
Temporary format containing texture data and information. Used as buffer between texture libraries...
Definition: TexImage.cs:13
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
Android.Widget.Orientation Orientation
Definition: Section.cs:9
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32