3
using System.Drawing.Imaging;
7
* TODO: Rework the code comments.
11
internal static class Loader {
13
/// <summary>Loads a texture from a file.</summary>
14
/// <param name="fileName">The platform-specific absolute file name.</param>
15
/// <param name="texture">Receives the texture.</param>
16
/// <returns>The success of the operation.</returns>
17
internal static OpenBveApi.General.Result LoadTexture(string fileName, out OpenBveApi.Texture.TextureData texture) {
19
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fileName);
20
// convert to 32-bit RGBA
21
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) {
22
Bitmap compatibleBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
23
Graphics graphics = Graphics.FromImage(compatibleBitmap);
24
Rectangle dest = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
25
Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
26
graphics.DrawImage(bitmap, dest, source, GraphicsUnit.Pixel);
29
bitmap = compatibleBitmap;
32
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
33
byte[] raw = new byte[data.Stride * data.Height];
34
if (data.Stride == 4 * data.Width) {
36
OpenBveApi.Texture.TextureFormat format = new OpenBveApi.Texture.TextureFormat(bitmap.Width, bitmap.Height, 8);
37
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, raw, 0, data.Stride * data.Height);
38
bitmap.UnlockBits(data);
40
// change order from BGRA to RGBA
41
for (int i = 0; i < raw.Length; i += 4) {
46
texture = new OpenBveApi.Texture.TextureData(format, raw);
47
return General.Result.Successful;
50
bitmap.UnlockBits(data);
53
return General.Result.InternalError;
57
return General.Result.InvalidData;
b'\\ No newline at end of file'