Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TextureFileTGASerializer.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 SiliconStudio.Core;
6 using SiliconStudio.Core.Serialization;
7 using SiliconStudio.Core.Serialization.Contents;
8 
9 namespace SiliconStudio.Paradox.Graphics.Data
10 {
11  [ContentSerializerExtension("tga")]
12  public class TextureFileTGASerializer : ContentSerializerBase<Image>
13  {
14  public override unsafe void Serialize(ContentSerializerContext context, SerializationStream stream, ref Image textureData)
15  {
16  if (context.Mode != ArchiveMode.Deserialize)
17  throw new NotSupportedException("Texture needs to be in package to be saved.");
18 
19  Header header;
20  var headerBytes = stream.ReadBytes(Utilities.SizeOf<Header>());
21  fixed (byte* p = &headerBytes[0])
22  {
23  Utilities.ReadOut((IntPtr)p, out header);
24  }
25 
26  if (header.ImageType != 2 || header.ColorMapType != 0 || (header.Bits != 32 && header.Bits != 24))
27  {
28  throw new NotSupportedException("Only RGB 24 or 32 bits TGA are currently supported.");
29  }
30 
31  var pixelData = new byte[header.Width * header.Height * 4];
32 
33  for (int y = 0; y < header.Height; ++y)
34  {
35  var scanlineData = stream.ReadBytes(header.Width * (header.Bits / 8));
36 
37  // Convert 24 to 32 bits
38  if (header.Bits == 24)
39  {
40  var expandedScanlineData = new byte[header.Width * 4];
41  for (int i = 0; i < header.Width; ++i)
42  {
43  expandedScanlineData[i * 4 + 0] = scanlineData[i * 3 + 0];
44  expandedScanlineData[i * 4 + 1] = scanlineData[i * 3 + 1];
45  expandedScanlineData[i * 4 + 2] = scanlineData[i * 3 + 2];
46  expandedScanlineData[i * 4 + 3] = 255;
47  }
48  scanlineData = expandedScanlineData;
49  }
50  int targetY = ((header.Descriptor & 0x10) == 0x10) ? y : header.Height - 1 - y;
51  fixed (byte* dest = &pixelData[header.Width * 4 * targetY])
52  fixed (byte* src = &scanlineData[0])
53  Utilities.CopyMemory((IntPtr)dest, (IntPtr)src, header.Width * 4);
54  }
55 
56  //textureData.Width = header.Width;
57  //textureData.Height = header.Height;
58  //textureData.MipLevels = 1;
59  //// TODO: Check why RGB/BGR seems reverted (maybe little endian/big endian applies?)
60  //textureData.PixelFormat = PixelFormat.B8G8R8A8_UNorm;
61  //textureData.Pixels = new[] { new TexturePixelData { Pitch = header.Width * 4, Data = pixelData } };
62  }
63 
64  [StructLayout(LayoutKind.Sequential, Pack = 1)]
65  public struct Header
66  {
67  public byte IdentSize;
68  public byte ColorMapType;
69  public byte ImageType;
70 
71  public short ColorMapStart;
72  public short ColorMapLength;
73  public byte ColorMapBits;
74 
75  public short XOffset;
76  public short YOffset;
77  public short Width;
78  public short Height;
79  public byte Bits;
80  public byte Descriptor;
81  }
82  }
83 }
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
override unsafe void Serialize(ContentSerializerContext context, SerializationStream stream, ref Image textureData)
Base class for implementation of SerializationStream.
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
char * dest
Definition: lz4.h:61