Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageHelper.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.Runtime.InteropServices;
5 using System.Text;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.IO;
8 using SiliconStudio.Core.Serialization;
9 
10 namespace SiliconStudio.Paradox.Graphics
11 {
12  public class ImageHelper
13  {
14  private static DataSerializer<ImageDescription> imageDescriptionSerializer = SerializerSelector.Default.GetSerializer<ImageDescription>();
15  private const string MagicCodeString = "TKTX";
16  private static readonly FourCC MagicCode = "TKTX";
17 
18  public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle? handle)
19  {
20  var stream = new BinarySerializationReader(new NativeMemoryStream((byte*)pSource, size));
21 
22  // Read and check magic code
23  var magicCode = stream.ReadUInt32();
24  if (magicCode != MagicCode)
25  return null;
26 
27  // Read header
28  var imageDescription = new ImageDescription();
29  imageDescriptionSerializer.Serialize(ref imageDescription, ArchiveMode.Deserialize, stream);
30 
31  if (makeACopy)
32  {
33  var buffer = Utilities.AllocateMemory(size);
34  Utilities.CopyMemory(buffer, pSource, size);
35  pSource = buffer;
36  makeACopy = false;
37  }
38 
39  var image = new Image(imageDescription, pSource, 0, handle, !makeACopy);
40 
41  var totalSizeInBytes = stream.ReadInt32();
42  if (totalSizeInBytes != image.TotalSizeInBytes)
43  throw new InvalidOperationException("Image size is different than expected.");
44 
45  // Read image data
46  stream.Serialize(image.DataPointer, image.TotalSizeInBytes);
47 
48  return image;
49  }
50 
51  public static void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, System.IO.Stream imageStream)
52  {
53  var stream = new BinarySerializationWriter(imageStream);
54 
55  // Write magic code
56  stream.Write(MagicCode);
57 
58  // Write image header
59  imageDescriptionSerializer.Serialize(ref description, ArchiveMode.Serialize, stream);
60 
61  // Write total size
62  int totalSize = 0;
63  foreach (var pixelBuffer in pixelBuffers)
64  totalSize += pixelBuffer.BufferStride;
65 
66  stream.Write(totalSize);
67 
68  // Write buffers contiguously
69  foreach (var pixelBuffer in pixelBuffers)
70  {
71  stream.Serialize(pixelBuffer.DataPointer, pixelBuffer.BufferStride);
72  }
73  }
74  }
75 }
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.
_In_ size_t count
Definition: DirectXTexP.h:174
Implements SerializationStream as a binary reader.
A MemoryStream over a native memory region.
An unmanaged buffer of pixels.
Definition: PixelBuffer.cs:32
static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle)
Definition: ImageHelper.cs:18
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
static void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, System.IO.Stream imageStream)
Definition: ImageHelper.cs:51
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175