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;
11 namespace SiliconStudio.TextureConverter.TexLibraries
16 internal class ColorKeyTexLibrary : ITexLibrary
18 private readonly
static Logger Log = GlobalLogger.GetLogger(
"ColorKeyTexLibrary");
23 public ColorKeyTexLibrary() { }
25 public bool CanHandleRequest(TexImage image, IRequest request)
27 return request.Type == RequestType.ColorKey;
30 public void Execute(TexImage image, IRequest request)
34 case RequestType.ColorKey:
38 Log.Error(
"ColorKeyTexLibrary can't handle this request: " + request.Type);
39 throw new TextureToolsException(
"ColorKeyTexLibrary can't handle this request: " + request.Type);
43 public void Dispose(TexImage image)
45 Marshal.FreeHGlobal(image.Data);
48 public void Dispose() { }
50 public void StartLibrary(TexImage image) { }
52 public void EndLibrary(TexImage image) { }
54 public bool SupportBGRAOrder()
59 public unsafe
void ApplyColorKey(TexImage image,
ColorKeyRequest request)
61 Log.Info(
"Apply color key [{0}]", request.ColorKey);
63 var colorKey = request.ColorKey;
64 var rowPtr = image.Data;
67 for (
int i = 0; i < image.Height; i++)
69 var colors = (Core.Mathematics.Color*)rowPtr;
70 for (
int x = 0; x < image.Width; x++)
72 if (colors[x] == colorKey)
74 colors[x] = Core.Mathematics.Color.Transparent;
77 rowPtr = IntPtr.Add(rowPtr, image.RowPitch);
80 else if (image.Format ==
PixelFormat.B8G8R8A8_UNorm)
82 var rgbaColorKey = colorKey.ToRgba();
83 for (
int i = 0; i < image.Height; i++)
85 var colors = (Core.Mathematics.ColorBGRA*)rowPtr;
86 for (
int x = 0; x < image.Width; x++)
88 if (colors[x].ToRgba() == rgbaColorKey)
90 colors[x] = Core.Mathematics.Color.Transparent;
93 rowPtr = IntPtr.Add(rowPtr, image.RowPitch);
Base implementation for ILogger.
Request to premultiply the alpha on the texture
Output message to log right away.
PixelFormat
Defines various types of pixel formats.