Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ColorKeyTexLibrary.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.Drawing;
5 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
7 using SiliconStudio.Core.Diagnostics;
8 using SiliconStudio.Paradox.Graphics;
9 using SiliconStudio.TextureConverter.Requests;
10 
11 namespace SiliconStudio.TextureConverter.TexLibraries
12 {
13  /// <summary>
14  /// Allows the creation and manipulation of texture atlas.
15  /// </summary>
16  internal class ColorKeyTexLibrary : ITexLibrary
17  {
18  private readonly static Logger Log = GlobalLogger.GetLogger("ColorKeyTexLibrary");
19 
20  /// <summary>
21  /// Initializes a new instance of the <see cref="ColorKeyTexLibrary"/> class.
22  /// </summary>
23  public ColorKeyTexLibrary() { }
24 
25  public bool CanHandleRequest(TexImage image, IRequest request)
26  {
27  return request.Type == RequestType.ColorKey;
28  }
29 
30  public void Execute(TexImage image, IRequest request)
31  {
32  switch (request.Type)
33  {
34  case RequestType.ColorKey:
35  ApplyColorKey(image, (ColorKeyRequest)request);
36  break;
37  default:
38  Log.Error("ColorKeyTexLibrary can't handle this request: " + request.Type);
39  throw new TextureToolsException("ColorKeyTexLibrary can't handle this request: " + request.Type);
40  }
41  }
42 
43  public void Dispose(TexImage image)
44  {
45  Marshal.FreeHGlobal(image.Data);
46  }
47 
48  public void Dispose() { }
49 
50  public void StartLibrary(TexImage image) { }
51 
52  public void EndLibrary(TexImage image) { }
53 
54  public bool SupportBGRAOrder()
55  {
56  return true;
57  }
58 
59  public unsafe void ApplyColorKey(TexImage image, ColorKeyRequest request)
60  {
61  Log.Info("Apply color key [{0}]", request.ColorKey);
62 
63  var colorKey = request.ColorKey;
64  var rowPtr = image.Data;
65  if (image.Format == PixelFormat.R8G8B8A8_UNorm)
66  {
67  for (int i = 0; i < image.Height; i++)
68  {
69  var colors = (Core.Mathematics.Color*)rowPtr;
70  for (int x = 0; x < image.Width; x++)
71  {
72  if (colors[x] == colorKey)
73  {
74  colors[x] = Core.Mathematics.Color.Transparent;
75  }
76  }
77  rowPtr = IntPtr.Add(rowPtr, image.RowPitch);
78  }
79  }
80  else if (image.Format == PixelFormat.B8G8R8A8_UNorm)
81  {
82  var rgbaColorKey = colorKey.ToRgba();
83  for (int i = 0; i < image.Height; i++)
84  {
85  var colors = (Core.Mathematics.ColorBGRA*)rowPtr;
86  for (int x = 0; x < image.Width; x++)
87  {
88  if (colors[x].ToRgba() == rgbaColorKey)
89  {
90  colors[x] = Core.Mathematics.Color.Transparent;
91  }
92  }
93  rowPtr = IntPtr.Add(rowPtr, image.RowPitch);
94  }
95  }
96  }
97  }
98 }
Base implementation for ILogger.
Definition: Logger.cs:10
Request to premultiply the alpha on the texture
Output message to log right away.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32