| 1 | using System; | 
 
 
 
 
 | 2 | using System.Collections.Generic; | 
 
 
 
 
 | 3 | using System.IO; | 
 
 
 
 
 | 4 | using Oni.Imaging; | 
 
 
 
 
 | 5 |  | 
 
 
 
 
 | 6 | namespace Oni.Motoko | 
 
 
 
 
 | 7 | { | 
 
 
 
 
 | 8 | internal static class TextureUtils | 
 
 
 
 
 | 9 | { | 
 
 
 
 
 | 10 | public static SurfaceFormat ToSurfaceFormat(this TextureFormat format) | 
 
 
 
 
 | 11 | { | 
 
 
 
 
 | 12 | switch (format) | 
 
 
 
 
 | 13 | { | 
 
 
 
 
 | 14 | case TextureFormat.BGRA4444: | 
 
 
 
 
 | 15 | return SurfaceFormat.BGRA4444; | 
 
 
 
 
 | 16 |  | 
 
 
 
 
 | 17 | case TextureFormat.BGR555: | 
 
 
 
 
 | 18 | return SurfaceFormat.BGRX5551; | 
 
 
 
 
 | 19 |  | 
 
 
 
 
 | 20 | case TextureFormat.BGRA5551: | 
 
 
 
 
 | 21 | return SurfaceFormat.BGRA5551; | 
 
 
 
 
 | 22 |  | 
 
 
 
 
 | 23 | case TextureFormat.RGBA: | 
 
 
 
 
 | 24 | return SurfaceFormat.RGBA; | 
 
 
 
 
 | 25 |  | 
 
 
 
 
 | 26 | case TextureFormat.BGR: | 
 
 
 
 
 | 27 | return SurfaceFormat.BGRX; | 
 
 
 
 
 | 28 |  | 
 
 
 
 
 | 29 | case TextureFormat.DXT1: | 
 
 
 
 
 | 30 | return SurfaceFormat.DXT1; | 
 
 
 
 
 | 31 |  | 
 
 
 
 
 | 32 | default: | 
 
 
 
 
 | 33 | throw new NotSupportedException(string.Format("Texture format {0} is not supported", format)); | 
 
 
 
 
 | 34 | } | 
 
 
 
 
 | 35 | } | 
 
 
 
 
 | 36 |  | 
 
 
 
 
 | 37 | public static Surface LoadImage(string filePath) | 
 
 
 
 
 | 38 | { | 
 
 
 
 
 | 39 | var surfaces = new List<Surface>(); | 
 
 
 
 
 | 40 |  | 
 
 
 
 
 | 41 | switch (Path.GetExtension(filePath).ToLowerInvariant()) | 
 
 
 
 
 | 42 | { | 
 
 
 
 
 | 43 | case ".tga": | 
 
 
 
 
 | 44 | surfaces.Add(TgaReader.Read(filePath)); | 
 
 
 
 
 | 45 | break; | 
 
 
 
 
 | 46 | default: | 
 
 
 
 
 | 47 | #if !NETCORE | 
 
 
 
 
 | 48 | surfaces.Add(SysReader.Read(filePath)); | 
 
 
 
 
 | 49 | #endif | 
 
 
 
 
 | 50 | break; | 
 
 
 
 
 | 51 | } | 
 
 
 
 
 | 52 |  | 
 
 
 
 
 | 53 | if (surfaces.Count == 0) | 
 
 
 
 
 | 54 | throw new InvalidDataException(string.Format("Could not load image '{0}'", filePath)); | 
 
 
 
 
 | 55 |  | 
 
 
 
 
 | 56 | return surfaces[0]; | 
 
 
 
 
 | 57 | } | 
 
 
 
 
 | 58 |  | 
 
 
 
 
 | 59 | public static int RoundToPowerOf2(int value) | 
 
 
 
 
 | 60 | { | 
 
 
 
 
 | 61 | if (value <= 2) | 
 
 
 
 
 | 62 | return value; | 
 
 
 
 
 | 63 |  | 
 
 
 
 
 | 64 | int hsb = 0; | 
 
 
 
 
 | 65 |  | 
 
 
 
 
 | 66 | for (int x = value; x > 1; hsb++) | 
 
 
 
 
 | 67 | x >>= 1; | 
 
 
 
 
 | 68 |  | 
 
 
 
 
 | 69 | // | 
 
 
 
 
 | 70 | // TODO would be nice to round up for cases like 127 | 
 
 
 
 
 | 71 | // But that require to upscale the image and currently the image | 
 
 
 
 
 | 72 | // resizing code doesn't handle upscaling. | 
 
 
 
 
 | 73 | // | 
 
 
 
 
 | 74 |  | 
 
 
 
 
 | 75 | //return 1 << (hsb + ((value >> (hsb - 1)) & 1)); | 
 
 
 
 
 | 76 |  | 
 
 
 
 
 | 77 | return 1 << hsb; | 
 
 
 
 
 | 78 | } | 
 
 
 
 
 | 79 | } | 
 
 
 
 
 | 80 | } |