summaryrefslogtreecommitdiff
path: root/NW4RTools/Texture.cs
blob: 078fa6b90aee69f79e2a0edf57c18bc1efacde60 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace NW4RTools {
	public class Texture {
		public struct TextureFormatInfo {
			public int TexelWidth, TexelHeight, NybblesPerPixel;

			public const int TexelByteSize = 32;

			private TextureFormatInfo(int tWidth, int tHeight, int npp) {
				TexelWidth = tWidth;
				TexelHeight = tHeight;
				NybblesPerPixel = npp;
			}

			static TextureFormatInfo() {
				I4 = new TextureFormatInfo(8, 8, 1);
				I8 = new TextureFormatInfo(8, 4, 2);
				IA4 = new TextureFormatInfo(8, 4, 2);
				IA8 = new TextureFormatInfo(4, 4, 4);
				RGB565 = new TextureFormatInfo(4, 4, 4);
				RGB5A3 = new TextureFormatInfo(4, 4, 4);
				RGBA8 = new TextureFormatInfo(4, 4, 8);
				C4 = new TextureFormatInfo(8, 8, 1);
				C8 = new TextureFormatInfo(8, 4, 2);
				C14X2 = new TextureFormatInfo(4, 4, 4);
				CMPR = new TextureFormatInfo(8, 8, 1);
			}

			public static TextureFormatInfo GetFormat(TextureFormat format) {
				switch (format) {
				case TextureFormat.I4:
					return I4;
				case TextureFormat.I8:
					return I8;
				case TextureFormat.IA4:
					return IA4;
				case TextureFormat.IA8:
					return IA8;
				case TextureFormat.RGB565:
					return RGB565;
				case TextureFormat.RGB5A3:
					return RGB5A3;
				case TextureFormat.RGBA8:
					return RGBA8;
				case TextureFormat.C4:
					return C4;
				case TextureFormat.C8:
					return C8;
				case TextureFormat.C14X2:
					return C14X2;
				case TextureFormat.CMPR:
					return CMPR;
				default:
					throw new ArgumentOutOfRangeException("unknown texture format");
				}
			}

			public static readonly TextureFormatInfo I4, I8, IA4, IA8, RGB565, RGB5A3, RGBA8, C4, C8, C14X2,
			CMPR;
		}



		public Bitmap[] Images;

		public TextureFormat Format;
		public float MinLOD, MaxLOD;

		public Texture() {
		}




		public static int GetDataSize(int width, int height, TextureFormat format) {
			var info = TextureFormatInfo.GetFormat(format);
			
			// align width, height up
			width = Misc.AlignUp(width, info.TexelWidth);
			height = Misc.AlignUp(height, info.TexelHeight);

			return width * height * info.NybblesPerPixel / 2;
		}


		public int GetDataSize() {
			int size = 0;
			for (int i = 0; i < Images.Length; i++) {
				size += GetDataSize(i);
			}
			return size;
		}

		public int GetDataSize(int imageID) {
			return GetDataSize(Images[imageID].Width, Images[imageID].Height, Format);
		}


		unsafe public byte[] ExportData(int imageID) {
			var info = TextureFormatInfo.GetFormat(Format);
			int blkWidth = info.TexelWidth;
			int blkHeight = info.TexelHeight;

			int width = Images[imageID].Width;
			int height = Images[imageID].Height;
			var bits = Images[imageID].LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

			var data = new OutputStream(ByteEndian.BigEndian);

			switch (Format) {
			case TextureFormat.RGB565:
			case TextureFormat.RGB5A3:
			case TextureFormat.I4:
			case TextureFormat.I8:
			case TextureFormat.IA4:
			case TextureFormat.IA8:
				// This won't be fun
				bool alreadyHaveI4Nybble = false;
				byte currentI4Byte = 0;

				for (int y = 0; y < height; y += blkHeight) {
					int yMax = y + blkHeight;
					if (yMax > height)
						yMax = height;
					
					for (int x = 0; x < width; x += blkWidth) {
						int xMax = x + blkWidth;
						if (xMax > width)
							xMax = width;
						
						for (int y1 = y; y1 < yMax; y1++) {
							byte* pRow = (byte*)bits.Scan0 + (y1 * bits.Stride);
							
							for (int x1 = x; x1 < xMax; x1++) {
								uint* pPixel = (uint*)pRow + x1;

								byte a = (byte)((*pPixel & 0xFF000000) >> 24);
								byte r = (byte)((*pPixel & 0x00FF0000) >> 16);
								byte g = (byte)((*pPixel & 0x0000FF00) >> 8);
								byte b = (byte)(*pPixel & 0x000000FF);

								if (Format == TextureFormat.RGB565) {
									ushort _r = (ushort)(r >> 3);
									ushort _g = (ushort)(g >> 2);
									ushort _b = (ushort)(b >> 3);
									data.WriteUInt16((ushort)((_r << 11) | (_g << 5) | _b));

								} else if (Format == TextureFormat.RGB5A3) {
									if (a == 255) {
										ushort _r = (ushort)(r >> 3);
										ushort _g = (ushort)(g >> 3);
										ushort _b = (ushort)(b >> 3);
										data.WriteUInt16((ushort)(0x8000 | (_r << 10) | (_g << 5) | _b));
									} else {
										ushort _a = (ushort)(a >> 5);
										ushort _r = (ushort)(r >> 4);
										ushort _g = (ushort)(g >> 4);
										ushort _b = (ushort)(b >> 4);
										data.WriteUInt16((ushort)((_a << 12) | (_r << 8) | (_g << 4) | _b));
									}

								} else if (Format == TextureFormat.I4) {
									byte _i = (byte)((r & g & b) >> 4);

									if (!alreadyHaveI4Nybble) {
										currentI4Byte = (byte)(_i << 4);
									} else {
										data.WriteByte((byte)(currentI4Byte | _i));
										currentI4Byte = 0;
									}

									alreadyHaveI4Nybble = !alreadyHaveI4Nybble;

								} else if (Format == TextureFormat.I8) {
									byte _i = (byte)(r & g & b);
									data.WriteByte(_i);

								} else if (Format == TextureFormat.IA4) {
									byte _i = (byte)((r & g & b) >> 4);
									byte _a = (byte)(a >> 4);
									data.WriteByte((byte)((_i << 4) | _a));

								} else if (Format == TextureFormat.IA8) {
									byte _i = (byte)(r & g & b);
									data.WriteByte(_i);
									data.WriteByte(a);
								}
							}
						}
					}
				}

				break;

			case TextureFormat.RGBA8:
				// this one is a pain because it stores an AR chunk, then a GB chunk, then ...
				for (int y = 0; y < height; y += blkHeight) {
					int yMax = y + blkHeight;
					if (yMax > height)
						yMax = height;
					
					for (int x = 0; x < width; x += blkWidth) {
						int xMax = x + blkWidth;
						if (xMax > width)
							xMax = width;
						
						for (int blkType = 0; blkType < 2; blkType++) {
							for (int y1 = y; y1 < yMax; y1++) {
								byte* pRow = (byte*)bits.Scan0 + (y1 * bits.Stride);
								
								for (int x1 = x; x1 < xMax; x1++) {
									uint* pPixel = (uint*)pRow + x1;

									ushort piece;
									if (blkType == 0) {
										piece = (ushort)((*pPixel & 0xFFFF0000) >> 16);
									} else {
										piece = (ushort)(*pPixel & 0x0000FFFF);
									}

									data.WriteUInt16(piece);
								}
							}
						}
					}
				}

				
				break;
			}

			Images[imageID].UnlockBits(bits);
			return data.GetBuffer();
		}


		unsafe public void ImportData(int imageID, byte[] imgdata, int width, int height, TextureFormat format) {
			var image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
			var bits = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, image.PixelFormat);

			var info = TextureFormatInfo.GetFormat(format);
			int blkWidth = info.TexelWidth;
			int blkHeight = info.TexelHeight;

			// What I'm missing overall: all the palette ones

			// this thing probably breaks on textures which aren't multiples of the texel width/height, too
			// oh well

			var data = new InputStream(imgdata);

			switch (format) {
			case TextureFormat.RGB565:
			case TextureFormat.RGB5A3:
			case TextureFormat.I4:
			case TextureFormat.I8:
			case TextureFormat.IA4:
			case TextureFormat.IA8:
				// I4 is stupid and breaks my parser. I'll keep some state here
				bool alreadyHaveI4Nybble = false;
				byte currentI4Byte = 0;

				for (int y = 0; y < height; y += blkHeight) {
					int yMax = y + blkHeight;
					if (yMax > height)
						yMax = height;

					for (int x = 0; x < width; x += blkWidth) {
						int xMax = x + blkWidth;
						if (xMax > width)
							xMax = width;

						for (int y1 = y; y1 < yMax; y1++) {
							byte* pRow = (byte*)bits.Scan0 + (y1 * bits.Stride);

							for (int x1 = x; x1 < xMax; x1++) {
								uint* pPixel = (uint*)pRow + x1;

								if (format == TextureFormat.RGB565) {
									ushort val = data.ReadUInt16();
									uint r = (uint)(val & 0xF800) >> 8;
									uint g = (uint)(val & 0x7e0) >> 3;
									uint b = (uint)(val & 0x1f) << 3;
									*pPixel = 0xFF000000 | (r << 16) | (g << 8) | b;

								} else if (format == TextureFormat.RGB5A3) {
									ushort val = data.ReadUInt16();
									if ((val & 0x8000) == 0) {
										uint a = (uint)(val & 0x7000) >> 3;
										uint r = (uint)(val & 0xF00) >> 4;
										uint g = (uint)(val & 0xF0);
										uint b = (uint)(val & 0xF) << 4;
										*pPixel = (a << 24) | (r << 16) | (g << 8) | b;
									} else {
										uint r = (uint)(val & 0x7c00) >> 7;
										uint g = (uint)(val & 0x3e0) >> 2;
										uint b = (uint)(val & 0x1f) << 3;
										*pPixel = 0xFF000000 | (r << 16) | (g << 8) | b;
									}

								} else if (format == TextureFormat.I4) {
									if (!alreadyHaveI4Nybble) {
										currentI4Byte = data.ReadByte();
										uint val = (uint)currentI4Byte & 0xF0;
										*pPixel = 0xFF000000 | (val << 16) | (val << 8) | val;
									} else {
										uint val = (uint)(currentI4Byte & 0xF) << 4;
										*pPixel = 0xFF000000 | (val << 16) | (val << 8) | val;
									}

									alreadyHaveI4Nybble = !alreadyHaveI4Nybble;

								} else if (format == TextureFormat.I8) {
									byte val = data.ReadByte();
									*pPixel = 0xFF000000 | (uint)(val << 16) | (uint)(val << 8) | val;

								} else if (format == TextureFormat.IA4) {
									byte val = data.ReadByte();
									uint i = (uint)(val >> 4);
									uint a = (uint)((val & 0xF0) << 4);
									*pPixel = (a << 24) | (i << 16) | (i << 8) | i;

								} else if (format == TextureFormat.IA8) {
									byte i = data.ReadByte();
									byte a = data.ReadByte();
									*pPixel = (uint)(a << 24) | (uint)(i << 16) | (uint)(i << 8) | i;

								}
							}
						}
					}
				}

				break;

			case TextureFormat.RGBA8:
				// this one is a pain because it stores an AR chunk, then a GB chunk, then ...
				for (int y = 0; y < height; y += blkHeight) {
					int yMax = y + blkHeight;
					if (yMax > height)
						yMax = height;

					for (int x = 0; x < width; x += blkWidth) {
						int xMax = x + blkWidth;
						if (xMax > width)
							xMax = width;

						for (int blkType = 0; blkType < 2; blkType++) {
							for (int y1 = y; y1 < yMax; y1++) {
								byte* pRow = (byte*)bits.Scan0 + (y1 * bits.Stride);

								for (int x1 = x; x1 < xMax; x1++) {
									uint* pPixel = (uint*)pRow + x1;

									uint val1 = data.ReadByte();
									uint val2 = data.ReadByte();

									if (blkType == 0) {
										// Alpha, Red
										*pPixel = ((*pPixel & 0x0000FFFF) | (val1 << 24) | (val2 << 16));
									} else {
										// Green, Blue
										*pPixel = ((*pPixel & 0xFFFF0000) | (val1 << 8) | (val2 << 0));
									}
								}
							}
						}
					}
				}

				break;

			case TextureFormat.CMPR:
				// this code is messy

				ushort[] rawClrArray = new ushort[4];
				uint[] clrArray = new uint[4];

				for (int y = 0; y < height; y += 8) {
					for (int x = 0; x < width; x += 8) {
						for (int iBlockY = 0; iBlockY < 2; iBlockY++) {
							for (int iBlockX = 0; iBlockX < 2; iBlockX++) {
								// decode a block
								rawClrArray[0] = data.ReadUInt16();
								rawClrArray[1] = data.ReadUInt16();

								bool hasAlpha = false;
								if (rawClrArray[0] > rawClrArray[1]) {
									rawClrArray[2] = CMPRAvgColor(2, 1, rawClrArray[0], rawClrArray[1]);
									rawClrArray[3] = CMPRAvgColor(1, 2, rawClrArray[0], rawClrArray[1]);
								} else {
									rawClrArray[2] = CMPRAvgColor(1, 1, rawClrArray[0], rawClrArray[1]);
									rawClrArray[3] = rawClrArray[1];
									hasAlpha = true;
								}

								for (int i = 0; i < 4; i++) {
									byte r = (byte)(((rawClrArray[i] >> 11) & 0x1F) << 3);
									byte g = (byte)(((rawClrArray[i] >> 5) & 0x3F) << 2);
									byte b = (byte)((rawClrArray[i] & 0x1F) << 3);
									byte a = (byte)(((i == 3) && hasAlpha) ? 0 : 255);

									clrArray[i] = (uint)(a << 24) | (uint)(r << 16) | (uint)(g << 8) | b;
								}

								for (int inY = 0; inY < 4; inY++) {
									byte* pRow = (byte*)bits.Scan0 + ((y + (iBlockY * 4) + inY) * bits.Stride);
									byte val = data.ReadByte();

									for (int inX = 0; inX < 4; inX++) {
										uint *pPixel = (uint*)pRow + x + (iBlockX * 4) + inX;
										*pPixel = clrArray[(val >> 6) & 3];
										val <<= 2;
									}
								}
							}
						}
					}
				}

				break;

			default:
				Console.WriteLine("Unsupported texture format: {0}", format);
				break;
			}

			
			image.UnlockBits(bits);

			Images[imageID] = image;
			Format = format;
		}

		private static ushort CMPRAvgColor(ushort w0, ushort w1, ushort c0, ushort c1) {
			uint result;

			ushort a0 = (ushort)(c0 >> 11);
			ushort a1 = (ushort)(c1 >> 11);
			uint a = (uint)((w0*a0 + w1*a1) / (w0+w1));
			result = (a << 11) & 0xffff;

			a0 = (ushort)((c0 >> 5) & 63);
			a1 = (ushort)((c1 >> 5) & 63);
			a = (uint)((w0 * a0 + w1 * a1) / (w0 + w1));
			result |= ((a << 5) & 0xffff);

			a0 = (ushort)(c0 & 31);
			a1 = (ushort)(c1 & 31);
			a = (uint)((w0 * a0 + w1 * a1) / (w0 + w1));
			result |= a;

			return (ushort)result;
		}
	}
}