-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoei_viewer.cpp
289 lines (246 loc) · 8 KB
/
koei_viewer.cpp
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
#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <map>
#include <string>
#include <stdbool.h>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "koei_image.h"
#include "ls11_decoder.h"
#define BITS_PER_BYTE (8)
const int ARG_WINDOW_WIDTH = 640;
const int ARG_WINDOW_HEIGHT = 400;
static int _SCALE = 1;
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Event event;
rapidjson::Document d;
uint32_t align_length_override;
static uint8_t *raw_buffer;
static size_t raw_buffer_size;
void put_pixel(int x, int y, rgb_t rgb)
{
SDL_Rect rect = {(x)*_SCALE, (y)*_SCALE, _SCALE, _SCALE};
SDL_FillRect(screenSurface, &rect,
SDL_MapRGB(screenSurface->format, rgb.r, rgb.g, rgb.b));
}
void redraw(rapidjson::Value &config)
{
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
bool is_ls11 = config["ls11"].GetBool();
uint32_t default_width = config["default_width"].GetUint();
uint32_t default_height = config["default_height"].GetUint();
uint32_t bpp = config["bpp"].GetUint();
uint32_t align_length = config["align_length"].GetUint();
bool is_tiled = config["tiled"].GetBool();
bool is_big_endian = config["big_endian"].GetBool();
int x = 0;
int y = 0;
if (!is_ls11)
{
int image_data_size = (default_width * default_height * bpp / BITS_PER_BYTE);
int image_count = raw_buffer_size / image_data_size;
for (int index = 0; index < image_count; index++)
{
image_t image;
read_image(raw_buffer + (image_data_size * index), &image,
default_width, default_height,
align_length_override,
bpp,
is_big_endian);
if (is_tiled)
{
int src_index = 0;
for (int i = 0; i < default_height / 16; i++)
{
for (int j = 0; j < default_width / 16; j++)
{
for (int k = 0; k < 16; k++)
{
for (int l = 0; l < 16; l++)
{
put_pixel(x + j * 16 + l, y + i * 16 + k, index_to_rgb(image.buf[src_index++]));
}
}
}
}
}
else
{
for (int i = 0; i < default_height; i++)
{
for (int j = 0; j < default_width; j++)
{
put_pixel(x + j, y + i, index_to_rgb(get_index_image(&image, i, j)));
}
}
}
free_image(&image);
x += default_width;
if (x > ARG_WINDOW_WIDTH)
{
x = 0;
y += default_height;
}
}
}
else
{
ls11_decode(raw_buffer, raw_buffer_size, [=, &x, &y](uint8_t *buf, uint32_t size) -> void
{
image_t image;
read_image(buf, &image,
default_width, default_height,
align_length_override,
bpp,
is_big_endian);
if (is_tiled)
{
int src_index = 0;
for (int i = 0; i < default_height / 16; i++)
{
for (int j = 0; j < default_width / 16; j++)
{
for (int k = 0; k < 16; k++)
{
for (int l = 0; l < 16; l++)
{
put_pixel(x + j * 16 + l, y + i * 16 + k, index_to_rgb(image.buf[src_index++]));
}
}
}
}
}
else
{
for (int i = 0; i < default_height; i++)
{
for (int j = 0; j < default_width; j++)
{
put_pixel(x + j, y + i, index_to_rgb(get_index_image(&image, i, j)));
}
}
}
free_image(&image);
x += default_width;
if (x > ARG_WINDOW_WIDTH)
{
x = 0;
y += default_height;
}
});
}
SDL_UpdateWindowSurface(window);
}
void read_json()
{
long size = 0;
FILE *fp = fopen("config.json", "r");
if (fp)
{
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *json = new char[size];
memset(json, 0, size);
fread(json, 1, size, fp);
d.Parse(json);
fclose(fp);
}
}
void destroy_all()
{
if (raw_buffer)
{
delete[] raw_buffer;
raw_buffer = NULL;
raw_buffer_size = 0;
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage: %s <image> <palette>\n", argv[0]);
printf(" ex) %s KAODATA.DAT SAM3KAO.RGB\n", argv[0]);
return 0;
}
read_json();
read_palette(argv[2]);
FILE *fp = fopen(argv[1], "r");
if (fp)
{
fseek(fp, 0, SEEK_END);
raw_buffer_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
raw_buffer = new uint8_t[raw_buffer_size];
fread(raw_buffer, sizeof(uint8_t), raw_buffer_size, fp);
fclose(fp);
}
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL_Init error: %s\n", SDL_GetError());
return 0;
}
window = SDL_CreateWindow(argv[0], SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, ARG_WINDOW_WIDTH * _SCALE,
ARG_WINDOW_HEIGHT * _SCALE, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("SDL_CreateWindow error: %s\n", SDL_GetError());
SDL_Quit();
exit(0);
}
std::string path = argv[1];
std::string base_filename = path.substr(path.find_last_of("/\\") + 1);
if (!d.HasMember(base_filename.c_str()))
{
printf("Error: There is no preset information of %s!\n", argv[1]);
return 0;
}
rapidjson::Value &config = d[base_filename.c_str()];
align_length_override = config["align_length"].GetUint();
do
{
redraw(config);
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
destroy_all();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
case SDLK_EQUALS:
_SCALE++;
break;
case SDLK_MINUS:
_SCALE--;
break;
case SDLK_PERIOD:
align_length_override++;
printf("Align length override: %d\n", align_length_override);
break;
case SDLK_COMMA:
align_length_override--;
printf("Align length override: %d\n", align_length_override);
break;
}
break;
case SDL_QUIT:
destroy_all();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
} while (SDL_WaitEvent(&event) >= 0);
destroy_all();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}