-
Notifications
You must be signed in to change notification settings - Fork 7
/
TGXImage.cs
358 lines (283 loc) · 13 KB
/
TGXImage.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Avalonia.Media.Imaging;
using HelperClasses.Gm1Converter;
namespace Files.Gm1Converter
{
public class TGXImage
{
#region Public
public static readonly int iImageHeaderSize = 16;
#endregion
#region Variables
private byte[] imgFileAsBytearray;
private ushort width;
private ushort height;
private uint tgxwidth;
private uint tgxheight;
private ushort offsetX;
private ushort offsetY;
private byte imagePart;
private byte subParts;
private ushort tileOffset;
private byte direction;
private byte horizontalOffsetOfImage;
private byte buildingWidth;
private byte animatedColor;//if alpha 1
private UInt32 offsetinByteArray;
private UInt32 sizeinByteArray;
private WriteableBitmap bmp;
#endregion
#region Construtor
public TGXImage()
{
}
#endregion
#region GetterSetter
public UInt16 Width { get => width; set => width = value; }
public UInt16 Height { get => height; set => height = value; }
public UInt32 TgxWidth { get => tgxwidth; set => tgxwidth = value; }
public UInt32 TgxHeight { get => tgxheight; set => tgxheight = value; }
public UInt16 OffsetX { get => offsetX; set => offsetX = value; }
public UInt16 OffsetY { get => offsetY; set => offsetY = value; }
/// <summary>
/// 0 denotes the start of a new collection
/// </summary>
public byte ImagePart { get => imagePart; set => imagePart = value; }
/// <summary>
/// Number of following parts in the collection
/// </summary>
public byte SubParts { get => subParts; set => subParts = value; }
/// <summary>
/// Vertical offset of the tile object on large surface
/// </summary>
public UInt16 TileOffset { get => tileOffset; set => tileOffset = value; }
/// <summary>
/// left,right, center... used for buildings only.
/// </summary>
public byte Direction { get => direction; set => direction = value; }
/// <summary>
/// Initial horizontal offset of image.
/// </summary>
public byte HorizontalOffsetOfImage { get => horizontalOffsetOfImage; set => horizontalOffsetOfImage = value; }
/// <summary>
/// Width of building part.
/// </summary>
public byte BuildingWidth { get => buildingWidth; set => buildingWidth = value; }
/// <summary>
/// Color, used for animated units only.
/// </summary>
public byte AnimatedColor { get => animatedColor; set => animatedColor = value; }
public UInt32 OffsetinByteArray { get => offsetinByteArray; set => offsetinByteArray = value; }
public UInt32 SizeinByteArray { get => sizeinByteArray; set => sizeinByteArray = value; }
public byte[] ImgFileAsBytearray { get => imgFileAsBytearray; set => imgFileAsBytearray = value; }
public WriteableBitmap Bitmap { get => bmp; set => bmp = value; }
#endregion
#region Methods
/// <summary>
/// Convert img byte array to IMG, use Pallete if not null
/// </summary>
/// <param name="palette">actual Pallete, Pallete is null if normal IMG</param>
public unsafe void CreateImageFromByteArray(Palette palette,bool isTgxFile=false)
{
int width, height;
if (!isTgxFile)
{
width = this.width;
height = this.height;
bmp = new WriteableBitmap(new Avalonia.PixelSize(width, height), new Avalonia.Vector(100, 100), Avalonia.Platform.PixelFormat.Bgra8888);// Bgra8888 is device-native and much faster.
}
else
{
width = (int)tgxwidth;
height = (int)tgxheight;
bmp = new WriteableBitmap(new Avalonia.PixelSize(width, height), new Avalonia.Vector(100, 100), Avalonia.Platform.PixelFormat.Bgra8888);// Bgra8888 is device-native and much faster.
}
using (var buf = bmp.Lock())
{
uint x = 0;
uint y = 0;
byte r, g, b, a;
for (uint bytePos = 0; bytePos < imgFileAsBytearray.Length;)
{
byte token = imgFileAsBytearray[bytePos];
byte tokentype = (byte)(token >> 5);
byte length = (byte)((token & 31) + 1);
//transparent
UInt32 colorByte = Utility.TransparentColorByte;
bytePos++;
byte index;
ushort pixelColor;
switch (tokentype)
{
case 0://Stream-of-pixels
for (byte i = 0; i < length; i++)
{
if (palette != null)
{
index = imgFileAsBytearray[bytePos];
pixelColor = palette.ArrayPaletten[palette.ActualPalette, index];
bytePos++;
}
else
{
pixelColor = BitConverter.ToUInt16(imgFileAsBytearray,(int) bytePos);
bytePos+=2;
}
Utility.ReadColor(pixelColor, out r, out g, out b, out a);
if (palette == null)
{
a = 255;
}
colorByte = (UInt32)(b | (g << 8) | (r << 16) | (a << 24));
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
}
break;
case 4://Newline
colorByte = Utility.TransparentColorByte;
if (palette != null)
{
for (byte i = 0; i < width; i++)
{
if (x >= width || y >= height)
break;
//Bgra8888
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
}
}
y++;
if (y > height) break;
x = 0;
break;
case 2://Repeating pixels
if (palette != null)
{
index = imgFileAsBytearray[bytePos];
pixelColor = palette.ArrayPaletten[palette.ActualPalette, index];
bytePos++;
}
else
{
pixelColor = BitConverter.ToUInt16(imgFileAsBytearray, (int)bytePos);
bytePos += 2;
}
Utility.ReadColor(pixelColor, out r, out g, out b, out a);
if (palette == null)
{
a = 255;
}
colorByte = (uint)(b | (g << 8) | (r << 16) | (a << 24));
for (byte i = 0; i < length; i++)
{
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
}
break;
case 1://Transparent-Pixel-String
colorByte = Utility.TransparentColorByte;
for (byte i = 0; i < length; i++)
{
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
}
break;
default:
break;
}
}
}
}
internal void ConvertImageWithPaletteToByteArray(List<ushort> colors, int width, int height, Palette palette, List<ushort>[] colorsImages = null)
{
var array = Utility.ImgToGM1ByteArray(colors, width, height, animatedColor, palette, colorsImages);
imgFileAsBytearray = array.ToArray();
}
internal unsafe void CreateNoComppressionImageFromByteArray(Palette palette,int offset)
{
//-7 because the images only height -7 long idk why
bmp = new WriteableBitmap(new Avalonia.PixelSize(width, height - offset), new Avalonia.Vector(100, 100), Avalonia.Platform.PixelFormat.Bgra8888);// Bgra8888 is device-native and much faster.
using (var buf = bmp.Lock())
{
byte r, g, b, a;
uint x = 0;
uint y = 0;
for (int bytePos = 0; bytePos < imgFileAsBytearray.Length; bytePos+=2)
{
Utility.ReadColor(BitConverter.ToUInt16(imgFileAsBytearray, bytePos), out r, out g, out b, out a);
var colorByte = (UInt32)(b | (g << 8) | (r << 16) | (a << 24));
var ptr = (uint*)buf.Address;
ptr += (uint)((width * y) + x);
*ptr = colorByte;
x++;
if (x==width)
{
y++;
x = 0;
}
}
}
}
/// <summary>
/// Convert imported Imgs without a Pallete to Byte array to safe new GM1 File
/// </summary>
/// <param name="colors">Color List of the new IMG</param>
/// <param name="width">Width of the new IMG</param>
/// <param name="height">Height of the new IMG</param>
internal void ConvertImageWithoutPaletteToByteArray(List<ushort> colors, int width, int height)
{
var array = Utility.ImgToGM1ByteArray(colors, width, height,animatedColor);
//for (int i = 0; i < imgFileAsBytearray.Length; i++)
//{
// if (imgFileAsBytearray[i]!= array[i])
// {
// }
//}
imgFileAsBytearray = array.ToArray();
}
internal void ConvertNoCommpressionImageToByteArray(List<ushort> list, int width, int height)
{
List<byte> newArray = new List<byte>();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
newArray.AddRange(BitConverter.GetBytes(list[y*width+x]));
}
}
imgFileAsBytearray = newArray.ToArray();
}
/// <summary>
/// Convert the Imageheader back to byte Array
/// </summary>
/// <returns></returns>
internal byte[] GetImageHeaderAsByteArray()
{
List<byte> array = new List<byte>();
array.AddRange(BitConverter.GetBytes(width));
array.AddRange(BitConverter.GetBytes(height));
array.AddRange(BitConverter.GetBytes(offsetX));
array.AddRange(BitConverter.GetBytes(offsetY));
array.Add(imagePart);
array.Add(subParts);
array.AddRange(BitConverter.GetBytes(tileOffset));
array.Add(direction);
array.Add(horizontalOffsetOfImage);
array.Add(buildingWidth);
array.Add(animatedColor);
return array.ToArray();
}
#endregion
}
}