-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPX22PNG.c
312 lines (259 loc) · 5.87 KB
/
PX22PNG.c
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
/////////////////////////////////////////////////////////////////////////////
// PX22PNG.c
//
// X68 to PNG Sprite Pattern Converter
// programmed by pirota
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "png.h"
#include "pngctrl.h"
#define INFILE_EXT ".PX2"
#define OUTFILE_EXT ".png"
// PX2ファイル構造体
typedef struct
{
unsigned short atr[256]; // ツール用アトリビュート
unsigned short pal[256]; // X68Kパレット
unsigned char sprpat[0x8000]; // X68Kスプライトパターン
} PX2FILE, * pPX2FILE;
static pPX2FILE px2buf = NULL;
static IMAGEDATA *imgbuf = NULL;
static char infilename[256];
static char outfilename[256];
static int main_result = 0;
static int width;
static int height;
static int opt_d = 0; // デバッグオプション
static int getfilesize(char *);
static int readjob(void);
static int cvjob(void);
// 使用法の表示
static void usage(void)
{
printf("usage: PX22PNG infile[" INFILE_EXT "] OutFile\n"\
// "\t-d\tDIBファイルも出力(デバッグ用)\n"
);
exit(0);
}
/////////////////////////////////////
// main
/////////////////////////////////////
int main(int argc, char *argv[])
{
int i;
int ch;
char drive[ _MAX_DRIVE ];
char dir[ _MAX_DIR ];
char fname[ _MAX_FNAME ];
char ext[ _MAX_EXT ];
// コマンドライン解析
memset(infilename, 0, sizeof(infilename) );
memset(outfilename, 0, sizeof(outfilename) );
printf("PX2 to PNG Converter Ver0.1a " __DATE__ "," __TIME__ " Programmed by Pirota\n");
if (argc <= 1)
{
usage();
}
for (i=1; i<argc; i++)
{
ch = argv[i][0];
if (ch == '-' || ch == '/')
{
// スイッチ
switch (argv[i][1])
{
case 'd':
opt_d = 1;
break;
default:
printf("-%c オプションが違います。\n",argv[i][1]);
break;
}
continue;
}
// ファイル名入力
if (!infilename[0])
{
strcpy(infilename, argv[i]);
_splitpath(infilename , drive, dir, fname, ext );
if (ext[0]==0)
strcat(infilename, INFILE_EXT); // 拡張子補完
continue;
}
// 出力ファイル名の作成
if (!outfilename[0])
{
// 出力ファイルネーム
strcpy(outfilename, argv[i]);
}
}
// 出力ファイル名が省略されてたら
if (!outfilename[0])
{
// 出力ファイルネーム
strcpy(outfilename, fname);
}
_splitpath(outfilename, drive, dir, fname, ext);
if (ext[0] == 0)
strcat(outfilename, OUTFILE_EXT); // 拡張子補完
// 入力バッファの確保
px2buf = (pPX2FILE)malloc(sizeof(PX2FILE));
if (px2buf == NULL)
{
printf("入力バッファは確保できません\n");
goto cvEnd;
}
memset(px2buf, 0, sizeof(PX2FILE));
// 変換元バッファの確保
width = 256;
height = 256;
imgbuf = (IMAGEDATA *)malloc(width * height);
if (imgbuf == NULL)
{
printf("出力バッファは確保できません\n");
goto cvEnd;
}
memset(imgbuf, 0, sizeof(IMAGEDATA));
imgbuf->width = width;
imgbuf->height = height;
imgbuf->palette_num = 256;
imgbuf->palette = malloc(sizeof(color_t) * 256);
imgbuf->map = alloc_map(imgbuf);
if (imgbuf->map == NULL)
{
printf("出力バッファは確保できません\n");
goto cvEnd;
}
// ファイル読み込み処理
if (readjob() < 0)
{
goto cvEnd;
}
// 変換処理
if (cvjob() < 0)
{
goto cvEnd;
}
cvEnd:
if (imgbuf->map != NULL)
{
free_map(imgbuf);
imgbuf->map = NULL;
}
// 後始末
// 入力バッファ開放
if (px2buf != NULL)
{
free(px2buf);
}
// イメージバッファ開放
if (imgbuf != NULL)
{
free(imgbuf);
}
return main_result;
}
//----------------------
// ファイルサイズを取得
//----------------------
static int getfilesize(char *fname)
{
int result = -1;
FILE * fp;
fp = fopen(fname, "rb");
if (fp == NULL)
return result;
fseek(fp, 0, SEEK_END);
result = ftell(fp);
fseek(fp, 0, SEEK_SET);
fclose(fp);
return result;
}
//----------------------------
// PNG読み込みDIBに変換
//----------------------------
// Out: 0=OK
// -1 = エラー
static int readjob(void)
{
FILE* fp;
size_t a;
// PX2ファイル入力
fp = fopen(infilename, "rb");
if (fp == NULL)
{
printf("Can't read '%s'.\n", infilename);
return -1;
}
// PX2パターン入力
a = fread(px2buf, 1, sizeof(PX2FILE), fp);
if (a != (sizeof(PX2FILE)))
{
printf("'%s' ファイルが正しく読み込めませんでした!\n", infilename);
}
fclose(fp);
return 0;
}
///////////////////////////////
// パターンデータに変換処理
///////////////////////////////
static int cvjob(void)
{
u_char* inptr;
u_char* atrptr;
width = imgbuf->width;
height = imgbuf->height;
// パターン変換処理
inptr = px2buf->sprpat; // スプライトパターンバッファ
atrptr = (u_char*)px2buf + 1; // アトリビュートバッファ
unsigned char aval = 0;
for (int yl=0; yl<height; yl+=16)
{
for (int xl=0; xl<width; xl+=8)
{
if ((xl & 15) == 0)
{
// アトリビュート読み込み
aval = ((*atrptr) & 0x0F) << 4;
atrptr += 2;
}
// ピクセル変換
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 8; x += 2)
{
unsigned char dot1 = ((*inptr) & 0xF0) >> 4;
unsigned char dot2 = (*inptr) & 0x0F;
imgbuf->map[yl + y][xl + x] = aval | dot1;
imgbuf->map[yl + y][xl + x + 1] = aval | dot2;
inptr++;
} // x
} // y
} // xl
} // yl
// パレット変換
// GGGGG_RRRRR_BBBBB_I
for (int i=0; i<256; i++)
{
color_t* pngpal = &imgbuf->palette[i];
unsigned short x68pal = ((px2buf->pal[i]) >> 8) | ((px2buf->pal[i] & 0x00FF) << 8); // エンディアン変換
x68pal >>= 1; // 輝度ビット削除
pngpal->a = 255;
pngpal->b = (x68pal & 31) << 3;
pngpal->r = (x68pal & (31 << 5)) >> 2;
pngpal->g = (x68pal & (31 << 10)) >> 7; // 0x4000 | 0x2000 | 0x1000
}
imgbuf->palette[0].a = 0;
// pngを書き込み(インデックスカラー専用)
int r = writepng(outfilename, imgbuf);
if (r == -1)
{
printf("%s を書き込みできませんでした。\n", outfilename);
return -1;
}
printf("%s を書き込みました。\n", outfilename);
return 0;
}