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
|
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "global.h"
#include "util.h"
#include "gfx.h"
#include "convert_png.h"
#include "jasc_pal.h"
#include "lz.h"
void ConvertToPng(char *imageFilePath, char *paletteFilePath, bool isObject, int width)
{
struct Image image;
int bitDepth = 0;
char *extension = GetFileExtension(imageFilePath);
if (extension == NULL)
FATAL_ERROR("\"%s\" has no file extension.\n", imageFilePath);
if (strcmp(extension, "1bpp") == 0)
bitDepth = 1;
else if (strcmp(extension, "4bpp") == 0)
bitDepth = 4;
else if (strcmp(extension, "8bpp") == 0)
bitDepth = 8;
else
FATAL_ERROR("Unexpected file extension \"%s\". Expected \"1bpp\", \"4bpp\", or \"8bpp\".\n", extension);
if (paletteFilePath != NULL) {
ReadGbaPalette(paletteFilePath, &image.palette);
image.hasPalette = true;
} else {
image.hasPalette = false;
}
ReadImage(imageFilePath, width, bitDepth, &image, !image.hasPalette);
image.isObject = isObject;
ChangeFileExtension(imageFilePath, "png");
WritePng(imageFilePath, &image);
FreeImage(&image);
}
void ConvertFromPng(char *imageFilePath, int numTiles, int bitDepth)
{
struct Image image;
image.bitDepth = bitDepth;
ExpectFileExtension(imageFilePath, "png");
ReadPng(imageFilePath, &image);
char newExtension[5];
snprintf(newExtension, 5, "%dbpp", bitDepth);
ChangeFileExtension(imageFilePath, newExtension);
WriteImage(imageFilePath, numTiles, bitDepth, &image, !image.hasPalette);
FreeImage(&image);
}
void ConvertToJascPalette(char *paletteFilePath)
{
struct Palette palette;
ExpectFileExtension(paletteFilePath, "gbapal");
ReadGbaPalette(paletteFilePath, &palette);
ChangeFileExtension(paletteFilePath, "pal");
WriteJascPalette(paletteFilePath, &palette);
}
void ConvertFromJascPalette(char *paletteFilePath)
{
struct Palette palette;
ExpectFileExtension(paletteFilePath, "pal");
ReadJascPalette(paletteFilePath, &palette);
ChangeFileExtension(paletteFilePath, "gbapal");
WriteGbaPalette(paletteFilePath, &palette);
}
void LZCompressFile(char *path)
{
int fileSize;
unsigned char *buffer = ReadWholeFile(path, &fileSize);
int compressedSize;
unsigned char *compressedData = LZCompress(buffer, fileSize, &compressedSize);
free(buffer);
AddFileExtension(path, "lz");
WriteWholeFile(path, compressedData, compressedSize);
free(compressedData);
}
void LZDecompressFile(char *path)
{
ExpectFileExtension(path, "lz");
int fileSize;
unsigned char *buffer = ReadWholeFile(path, &fileSize);
int uncompressedSize;
unsigned char *uncompressedData = LZDecompress(buffer, fileSize, &uncompressedSize);
free(buffer);
RemoveFileExtension(path);
WriteWholeFile(path, uncompressedData, uncompressedSize);
free(uncompressedData);
}
int main(int argc, char **argv)
{
if (argc < 2)
FATAL_ERROR("No args.\n");
char *command = argv[1];
if (strcmp(command, "png") == 0) {
if (argc < 3)
FATAL_ERROR("No image file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char imageFilePath[GBAGFX_MAX_PATH + 1];
strcpy(imageFilePath, argv[2]);
char paletteFilePath[GBAGFX_MAX_PATH + 1];
bool hasPalette = false;
bool isObject = false;
int width = 1;
for (int i = 3; i < argc; i++) {
char *option = argv[i];
if (strcmp(option, "-palette") == 0) {
if (i + 1 >= argc)
FATAL_ERROR("No palette file path following \"-palette\".\n");
i++;
CHECK_PATH_LENGTH(argv[i]);
strcpy(paletteFilePath, argv[i]);
hasPalette = true;
} else if (strcmp(option, "-object") == 0) {
isObject = true;
} else if (strcmp(option, "-width") == 0) {
if (i + 1 >= argc)
FATAL_ERROR("No width following \"-width\".\n");
i++;
if (!ParseNumber(argv[i], NULL, 10, &width))
FATAL_ERROR("Failed to parse width.\n");
if (width < 1)
FATAL_ERROR("Width must be positive.\n");
} else {
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
}
}
ConvertToPng(imageFilePath, hasPalette ? paletteFilePath : NULL, isObject, width);
} else if (strcmp(command, "1bpp") == 0 || strcmp(command, "4bpp") == 0 || strcmp(command, "8bpp") == 0) {
if (argc < 3)
FATAL_ERROR("No image file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char imageFilePath[GBAGFX_MAX_PATH + 1];
strcpy(imageFilePath, argv[2]);
int numTiles = 0;
int bitDepth = command[0] - '0';
for (int i = 3; i < argc; i++) {
char *option = argv[i];
if (strcmp(option, "-num_tiles") == 0) {
if (i + 1 >= argc)
FATAL_ERROR("No number of tiles following \"-num_tiles\".\n");
i++;
if (!ParseNumber(argv[i], NULL, 10, &numTiles))
FATAL_ERROR("Failed to parse number of tiles.\n");
if (numTiles < 1)
FATAL_ERROR("Number of tiles must be positive.\n");
} else {
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
}
}
ConvertFromPng(imageFilePath, numTiles, bitDepth);
} else if (strcmp(command, "pal") == 0) {
if (argc < 3)
FATAL_ERROR("No palette file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char paletteFilePath[GBAGFX_MAX_PATH + 1];
strcpy(paletteFilePath, argv[2]);
ConvertToJascPalette(paletteFilePath);
} else if (strcmp(command, "gbapal") == 0) {
if (argc < 3)
FATAL_ERROR("No palette file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char paletteFilePath[GBAGFX_MAX_PATH + 1];
strcpy(paletteFilePath, argv[2]);
ConvertFromJascPalette(paletteFilePath);
} else if (strcmp(command, "lz") == 0) {
if (argc < 3)
FATAL_ERROR("No file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char path[GBAGFX_MAX_PATH + 1];
strcpy(path, argv[2]);
LZCompressFile(path);
} else if (strcmp(command, "unlz") == 0) {
if (argc < 3)
FATAL_ERROR("No file path arg.\n");
CHECK_PATH_LENGTH(argv[2]);
char path[GBAGFX_MAX_PATH + 1];
strcpy(path, argv[2]);
LZDecompressFile(path);
} else {
FATAL_ERROR("Unrecognized command \"%s\".\n", command);
}
return 0;
}
|