source: OniSplit/Motoko/TextureUtils.cs@ 1194

Last change on this file since 1194 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using Oni.Imaging;
5
6namespace 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}
Note: See TracBrowser for help on using the repository browser.