/
Targa.cs
66 lines (61 loc) · 3.01 KB
/
Targa.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using RT.Util.ExtensionMethods;
namespace TankIconMaker
{
/// <summary>
/// Encapsulates a Targa image data. Exposes the raw pixel data, for better or worse.
/// Supports only a small subset of actual Targa formats: 32bpp with alpha, uncompressed.
/// </summary>
static class Targa
{
/// <summary>Loads a .tga image directly into a <see cref="BitmapRam"/> instance.</summary>
public static BitmapRam Load(Stream file)
{
var header = file.Read(18);
if (header[0] != 0) throw new NotSupportedException("Only images with no offset are supported");
if (header[1] != 0) throw new NotSupportedException("Only RGB images are supported");
if (header[2] != 2) throw new NotSupportedException("Only RGB images are supported");
if (header[8] != 0 || header[9] != 0 || header[10] != 0 || header[11] != 0)
throw new NotSupportedException("Only images with origin at 0,0 are supported");
var width = (header[13] << 8) + header[12];
var height = (header[15] << 8) + header[14];
if (width > 2048 || height > 2048) // an arbitrary limit; more for sanity check than anything else
throw new NotSupportedException("Only images up to 2048x2048 are supported");
if (width == 0 || height == 0)
throw new NotSupportedException("Images with a zero width or height are not supported");
if (header[16] != 32) throw new NotSupportedException("Only 32 bits per pixel images are supported");
bool rightWayUp = (header[17] & 32) != 0;
var raw = file.Read(width * height * 4);
var result = new BitmapRam(width, height);
result.CopyPixelsFrom(raw, width, height, width * 4, !rightWayUp);
return result;
}
/// <summary>A convenience method to save a GDI image directly to a .tga file.</summary>
public static unsafe void Save(BitmapBase image, string filename)
{
using (image.UseRead())
using (var file = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read))
{
var header = new byte[18];
header[2] = 2;
header[12] = (byte) (image.Width);
header[13] = (byte) (image.Width >> 8);
header[14] = (byte) (image.Height);
header[15] = (byte) (image.Height >> 8);
header[16] = 32;
header[17] = 32;
file.Write(header);
byte[] dummy = new byte[image.Width * 4];
for (int y = 0; y < image.Height; y++)
{
Ut.MemCpy(dummy, image.Data + y * image.Stride, image.Width * 4);
file.Write(dummy);
}
}
}
}
}