Skip to content

Commit 4e79283

Browse files
committed
Add z-data related features
- Option to output template z-data as images (grayscale, pixel brightness is z-data value times eight) - Option to fix z-data (values above 31 converted to 0) for processed tiles. Works even if no image data is converted.
1 parent da1bb7c commit 4e79283

File tree

5 files changed

+134
-34
lines changed

5 files changed

+134
-34
lines changed

BatchTMPConverter/BatchTMPConverter.cs

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
using System;
1+
/*
2+
* Copyright 2016-2023 by Starkku
3+
* This file is part of BatchTMPConverter, which is free software. It is made
4+
* available to you under the terms of the GNU General Public License
5+
* as published by the Free Software Foundation, either version 3 of
6+
* the License, or (at your option) any later version. For more
7+
* information, see LICENSE.txt.
8+
*/
9+
10+
using System;
211
using System.Collections.Generic;
312
using BatchTMPConverter.Logic;
413
using System.Drawing;
@@ -18,13 +27,14 @@ public class BatchTMPConverter
1827
private readonly bool replaceRadarColors = false;
1928
private readonly double radarColorMultiplier = 1.0;
2029
private readonly bool extraDataBGOverride = false;
30+
private readonly bool fixZData = false;
2131
private readonly bool accurateColorMatching = false;
2232
private FileLog processedFilesLog = null;
2333
private readonly bool supressBackups = false;
2434
private List<Tuple<string, string>> preprocessCommands = new List<Tuple<string, string>>();
2535

2636
public BatchTMPConverter(string filenames, string palette, string customExtensions, bool replaceRadarColors, double radarColorMultiplier, bool extraDataBGOverride,
27-
bool accurateColorMatching, string preprocessCommands, string fileLogFilename, bool supressBackups)
37+
bool fixZData, bool accurateColorMatching, string preprocessCommands, string fileLogFilename, bool supressBackups)
2838
{
2939
if (!string.IsNullOrEmpty(fileLogFilename))
3040
processedFilesLog = new FileLog(fileLogFilename);
@@ -41,6 +51,7 @@ public BatchTMPConverter(string filenames, string palette, string customExtensio
4151
this.replaceRadarColors = replaceRadarColors;
4252
this.radarColorMultiplier = radarColorMultiplier;
4353
this.extraDataBGOverride = extraDataBGOverride;
54+
this.fixZData = fixZData;
4455
this.accurateColorMatching = accurateColorMatching;
4556
this.supressBackups = supressBackups;
4657
ParsePreprocessCommands(preprocessCommands);
@@ -103,12 +114,20 @@ private void ParsePreprocessCommands(string commandString)
103114
}
104115
}
105116

106-
public void ConvertImagesToTMP()
117+
public void ProcessTiles()
107118
{
119+
bool allowNoConvert = fixZData;
120+
bool skipConvert = false;
121+
108122
if (!palette.Loaded)
109123
{
110-
Logger.Error("No palette file has been loaded - cannot convert image data to templates.");
111-
return;
124+
if (!allowNoConvert)
125+
{
126+
Logger.Error("No palette file has been loaded - cannot convert image data to templates.");
127+
return;
128+
}
129+
130+
skipConvert = true;
112131
}
113132

114133
foreach (string filename in files)
@@ -122,15 +141,27 @@ public void ConvertImagesToTMP()
122141

123142
if (!File.Exists(imageFilename))
124143
{
125-
Logger.Warn("Image '" + imageFilename + "' does not exist. Skip converting image data to template.");
126-
continue;
144+
if (!allowNoConvert)
145+
{
146+
Logger.Warn("Image '" + imageFilename + "' does not exist. Skip converting image data to template.");
147+
continue;
148+
}
149+
150+
skipConvert = true;
127151
}
128152
else if (processedFilesLog != null && !processedFilesLog.HasFileBeenModified(imageFilename))
129153
{
130-
Logger.Warn("Image '" + imageFilename + "' has not been modified. Skip converting image data to template.");
131-
continue;
154+
if (!allowNoConvert)
155+
{
156+
Logger.Warn("Image '" + imageFilename + "' has not been modified. Skip converting image data to template.");
157+
158+
continue;
159+
}
160+
161+
skipConvert = true;
132162
}
133-
else
163+
164+
if (!skipConvert)
134165
{
135166
string actualImageFilename = PreprocessImage(imageFilename);
136167
Bitmap bitmap = new Bitmap(actualImageFilename);
@@ -175,13 +206,17 @@ public void ConvertImagesToTMP()
175206
continue;
176207
}
177208

178-
if (tmp.Save(supressBackups))
179-
processedFilesLog?.UpdateOrAddFile(imageFilename);
180-
181209
if (imageFilename != actualImageFilename)
182210
File.Delete(actualImageFilename);
183211
}
212+
213+
if (fixZData)
214+
tmp.FixZData();
215+
216+
if (tmp.Save(supressBackups) && !skipConvert)
217+
processedFilesLog?.UpdateOrAddFile(imageFilename);
184218
}
219+
185220
processedFilesLog?.Save();
186221
}
187222

@@ -208,7 +243,7 @@ private string PreprocessImage(string imageFilename)
208243
return filename;
209244
}
210245

211-
public void OutputTMPImageData()
246+
public void OutputTMPImageData(bool outputZData = false)
212247
{
213248
if (!palette.Loaded)
214249
{
@@ -230,14 +265,22 @@ public void OutputTMPImageData()
230265
continue;
231266

232267
Rectangle tmpRectangle = tmp.GetRectangle();
233-
byte[] data = tmp.GetImageData(tmpRectangle);
268+
byte[] data = tmp.GetImageData(tmpRectangle, false, outputZData);
269+
270+
string basePath = Path.GetDirectoryName(tmp.FilenameInput);
271+
string baseFilename = Path.GetFileNameWithoutExtension(tmp.FilenameInput);
272+
string suffix = outputZData ? "_ZData" : string.Empty;
273+
string imagefilename = Path.Combine(basePath, baseFilename + suffix + ".png");
234274

235-
string imagefilename = Path.ChangeExtension(tmp.FilenameInput, ".png");
236275
Bitmap bitmap = new Bitmap(tmpRectangle.Width - tmpRectangle.Left, tmpRectangle.Height - tmpRectangle.Top, PixelFormat.Format24bppRgb);
237276

238277
try
239278
{
240-
SaveImageDataToBitmap(bitmap, data, palette);
279+
if (!outputZData)
280+
SaveImageDataToBitmap(bitmap, data, palette);
281+
else
282+
SaveZDataToBitmap(bitmap, data);
283+
241284
bitmap.Save(imagefilename);
242285
}
243286
catch (Exception e)
@@ -249,6 +292,7 @@ public void OutputTMPImageData()
249292
processedFilesLog?.UpdateOrAddFile(filename);
250293
Logger.Info(filename + " template data saved to image file " + imagefilename + ".");
251294
}
295+
252296
processedFilesLog?.Save();
253297
}
254298

@@ -319,5 +363,19 @@ private void SaveImageDataToBitmap(Bitmap bitmap, byte[] data, Palette palette)
319363
}
320364
}
321365
}
366+
367+
private void SaveZDataToBitmap(Bitmap bitmap, byte[] data)
368+
{
369+
int c = 0;
370+
371+
for (int y = 0; y < bitmap.Height; y++)
372+
{
373+
for (int x = 0; x < bitmap.Width; x++)
374+
{
375+
byte value = (byte)Math.Min(data[c++] << 3, 255);
376+
bitmap.SetPixel(x, y, Color.FromArgb(value, value, value));
377+
}
378+
}
379+
}
322380
}
323381
}

BatchTMPConverter/Logic/Tmp.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 by Starkku
2+
* Copyright 2016-2023 by Starkku
33
* This file is part of BatchTMPConverter, which is free software. It is made
44
* available to you under the terms of the GNU General Public License
55
* as published by the Free Software Foundation, either version 3 of
@@ -341,12 +341,11 @@ public void ConvertImageData(PaletteColor[] imageData, Rectangle tmpRectangle, b
341341
Logger.Info(FilenameInput + " tile data successfully replaced.");
342342
}
343343

344-
public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false)
344+
public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false, bool useZData = false)
345345
{
346346
int HalfHeight = BlockHeight / 2;
347347
int gx = tmpRectangle.Width - tmpRectangle.Left;
348348
int gy = tmpRectangle.Height - tmpRectangle.Top;
349-
350349
byte[] imageData = new byte[gx * gy];
351350

352351
foreach (TmpTile tile in tiles)
@@ -364,7 +363,8 @@ public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false)
364363
{
365364
cx += 4;
366365
x -= 2;
367-
CopyArray(tile.GetTileData(), tdc, imageData, p + x, cx);
366+
var data = useZData ? tile.GetZData() : tile.GetTileData();
367+
CopyArray(data, tdc, imageData, p + x, cx, false, useZData);
368368
tdc += cx;
369369
p += gx;
370370
}
@@ -373,7 +373,8 @@ public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false)
373373
{
374374
cx -= 4;
375375
x += 2;
376-
CopyArray(tile.GetTileData(), tdc, imageData, p + x, cx);
376+
var data = useZData ? tile.GetZData() : tile.GetTileData();
377+
CopyArray(data, tdc, imageData, p + x, cx, false, useZData);
377378
tdc += cx;
378379
p += gx;
379380
}
@@ -391,7 +392,8 @@ public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false)
391392

392393
for (int i = 0; i < cx; i++)
393394
{
394-
CopyArray(tile.GetExtraData(), edc, imageData, p2, 1, true);
395+
var data = useZData ? tile.GetExtraZData() : tile.GetExtraData();
396+
CopyArray(data, edc, imageData, p2, 1, true, useZData);
395397
edc++;
396398
p2++;
397399
}
@@ -404,6 +406,27 @@ public byte[] GetImageData(Rectangle tmpRectangle, bool ignoreExtraData = false)
404406
return imageData;
405407
}
406408

409+
public void FixZData()
410+
{
411+
for (int t = 0; t < tiles.Count; t++)
412+
{
413+
var tile = tiles[t];
414+
var zData = tile.GetExtraZData();
415+
416+
if (zData != null)
417+
{
418+
for (int i = 0; i < zData.Length; i++)
419+
{
420+
if (zData[i] > 31)
421+
{
422+
//Logger.Info($"{FilenameInput} tile #{t + 1} extra image z-data at index {i} changed from {zData[i]} to 0.");
423+
zData[i] = 0;
424+
}
425+
}
426+
}
427+
}
428+
}
429+
407430
private void CopyArray(PaletteColor[] src, int srcOffset, byte[] dst, int dstOffset, int length, ref RadarColor radarColor,
408431
PaletteColor backgroundColor, bool isExtra = false, byte[] clippingMask = null, bool extraDataBGOverride = false)
409432
{
@@ -430,13 +453,16 @@ private void CopyArray(PaletteColor[] src, int srcOffset, byte[] dst, int dstOff
430453
}
431454
}
432455

433-
private void CopyArray(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length, bool isExtra = false)
456+
private void CopyArray(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length, bool isExtra = false, bool isZData = false)
434457
{
435458
for (int i = 0; i < length; i++)
436459
{
437460
if (isExtra && src[srcOffset + i] == 0)
438461
continue;
439462

463+
if (isZData && (src[srcOffset + i] == 205 || src[srcOffset + i] == 0))
464+
continue;
465+
440466
dst[dstOffset + i] = src[srcOffset + i];
441467
}
442468
}

BatchTMPConverter/Program.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
using System;
1+
/*
2+
* Copyright 2016-2023 by Starkku
3+
* This file is part of BatchTMPConverter, which is free software. It is made
4+
* available to you under the terms of the GNU General Public License
5+
* as published by the Free Software Foundation, either version 3 of
6+
* the License, or (at your option) any later version. For more
7+
* information, see LICENSE.txt.
8+
*/
9+
10+
using System;
211
using System.IO;
312
using NDesk.Options;
413
using BatchTMPConverter.Utility;
@@ -20,10 +29,12 @@ static void Main(string[] args)
2029
{"i|files=", "A comma-separated list of input file(s) and/or directory/directories.", v => settings.Filenames = v},
2130
{"p|palette=", "Palette file to use for conversion.", v => settings.Palette = v},
2231
{"o|output-images", "Output template data as images instead of converting images to templates.", v => settings.OutputImages = true},
32+
{"u|output-zdata", "Output template z-data as images instead of converting images to templates.", v => settings.OutputZData = true},
2333
{"e|extensions-override=", "Comma-separated list of file extensions (including the .) to use instead of built-in defaults.", v => settings.CustomExtensions = v},
2434
{"r|replace-radarcolor", "Alter tile radar colors based on new image & palette data.", v => settings.ReplaceRadarColors = true},
2535
{"m|radarcolor-multiplier=", "Multiplier to radar color RGB values, if they are altered.", v => settings.RadarColorMultiplier = v},
2636
{"x|extraimage-bg-override", "Allow overwriting background color pixels on existing extra images.", v => settings.AllowExtraDataBGOverride = true},
37+
{"z|zdata-fix", "Adjusts z-data values on processed tiles so that any value higher than 31 on z-data is converted to 0. This is applied even if no image data is modified.", v => settings.FixZData = true},
2738
{"c|accurate-color-matching", "Enables slower but more accurate palette color matching.", v => settings.AccurateColorMatching = true},
2839
{"d=|preprocess-commands", "List of commands to use to preprocess images before conversion. Comma-separated list of commands consisting of executable and arguments separated by semicolon.", v => settings.PreprocessCommands = v},
2940
{"b|no-backups", "Disable backing up the edited files with same name using file extension .old.", v => settings.SupressBackups = true},
@@ -75,13 +86,16 @@ static void Main(string[] args)
7586
}
7687
}
7788

78-
BatchTMPConverter batchTool = new BatchTMPConverter(settings.Filenames, settings.Palette, settings.CustomExtensions,
79-
settings.ReplaceRadarColors, radarColorMult, settings.AllowExtraDataBGOverride, settings.AccurateColorMatching, settings.PreprocessCommands, settings.ProcessedFilesLogFilename, settings.SupressBackups);
89+
BatchTMPConverter batchTool = new BatchTMPConverter(settings.Filenames, settings.Palette,
90+
settings.CustomExtensions, settings.ReplaceRadarColors, radarColorMult, settings.AllowExtraDataBGOverride, settings.FixZData,
91+
settings.AccurateColorMatching,settings.PreprocessCommands, settings.ProcessedFilesLogFilename, settings.SupressBackups);
8092

81-
if (!settings.OutputImages)
82-
batchTool.ConvertImagesToTMP();
83-
else
93+
if (!settings.OutputImages && !settings.OutputZData)
94+
batchTool.ProcessTiles();
95+
else if (settings.OutputImages)
8496
batchTool.OutputTMPImageData();
97+
else
98+
batchTool.OutputTMPImageData(true);
8599
}
86100

87101
private static void ShowHelp()

BatchTMPConverter/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[assembly: AssemblyConfiguration("")]
1212
[assembly: AssemblyCompany("")]
1313
[assembly: AssemblyProduct("BatchTMPConverter")]
14-
[assembly: AssemblyCopyright("Copyright © Starkku 2016-2022")]
14+
[assembly: AssemblyCopyright("Copyright © Starkku 2016-2023")]
1515
[assembly: AssemblyTrademark("")]
1616
[assembly: AssemblyCulture("")]
1717

@@ -33,6 +33,6 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("3.3.0.0")]
37-
[assembly: AssemblyFileVersion("3.3.0.0")]
36+
[assembly: AssemblyVersion("4.0.0.0")]
37+
[assembly: AssemblyFileVersion("4.0.0.0")]
3838
[assembly: NeutralResourcesLanguageAttribute("")]

BatchTMPConverter/Utility/Settings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 by Starkku
2+
* Copyright 2016-2023 by Starkku
33
* This file is part of BatchTMPConverter, which is free software. It is made
44
* available to you under the terms of the GNU General Public License
55
* as published by the Free Software Foundation, either version 3 of
@@ -19,9 +19,11 @@ struct Settings
1919
public bool ReplaceRadarColors { get; set; }
2020
public string RadarColorMultiplier { get; set; }
2121
public bool OutputImages { get; set; }
22+
public bool OutputZData { get; set; }
2223
public string ProcessedFilesLogFilename { get; set; }
2324
public bool LogToFile { get; set; }
2425
public bool AllowExtraDataBGOverride { get; set; }
26+
public bool FixZData { get; set; }
2527
public bool AccurateColorMatching { get; set; }
2628
public string PreprocessCommands { get; set; }
2729
}

0 commit comments

Comments
 (0)