-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpng2nes.c
312 lines (255 loc) · 8.24 KB
/
png2nes.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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <png.h>
#define HLEN 8
typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef unsigned int Uint32;
typedef unsigned long Uint64;
typedef struct {
char name[256];
png_structp imgptr;
png_infop infoptr;
Uint8 header[HLEN];
Uint32 imgw;
Uint32 imgh;
Uint8 type;
Uint8 depth;
Uint8 original_depth;
Uint8 **rows;
} PNG_Doc;
PNG_Doc inf; /* INputFile */
enum image_type {
IMG_PALETTE,
IMG_GRAYSCALE_4C,
IMG_GRAYSCALE_256C,
IMG_UNKNOWN,
};
int
error(char *msg, char *err)
{
printf("ERROR %s: %s\n", msg, err);
return 1;
}
void
setuppng(FILE *fp, PNG_Doc *iput)
{
png_init_io(iput->imgptr, fp);
png_set_sig_bytes(iput->imgptr, HLEN);
png_read_info(iput->imgptr, iput->infoptr);
iput->original_depth = png_get_bit_depth(iput->imgptr, iput->infoptr);
if (iput->original_depth < 8) {
png_set_packing(iput->imgptr); //make sure each pixel is a byte
}
if (iput->original_depth == 16) {
png_set_strip_16(iput->imgptr); //strip 16-bit images to 8-bit
}
png_set_strip_alpha(iput->imgptr); //remove alpha channel from the image
png_set_rgb_to_gray(iput->imgptr, 1, -1.0, -1.0); //convert RGB to grayscale
png_read_update_info(iput->imgptr, iput->infoptr);
//png_read_png(iput->imgptr, iput->infoptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA, NULL);
iput->imgw = png_get_image_width(iput->imgptr, iput->infoptr);
iput->imgh = png_get_image_height(iput->imgptr, iput->infoptr);
iput->type = png_get_color_type(iput->imgptr, iput->infoptr);
iput->depth = png_get_bit_depth(iput->imgptr, iput->infoptr);
iput->rows = png_malloc(iput->imgptr, iput->imgh * sizeof(png_bytep));
int i;
for (i=0; i < iput->imgh; i++) {
iput->rows[i] = NULL;
iput->rows[i] = png_malloc(iput->imgptr, iput->imgw * (iput->depth / 8));
}
png_read_image(iput->imgptr, iput->rows);
png_read_end(iput->imgptr, NULL);
}
void
exportsprite_256c(FILE *chrfile, Uint8 **rows, Uint8 col, Uint8 row)
{
Uint32 y, x;
Uint8 buf1[8];
Uint8 buf2[8];
Uint8 pixel, bit0, bit1;
for(y = 0; y < 8; y++) {
/* Clear the current byte in the buffer */
buf1[y] = 0;
buf2[y] = 0;
for(x = 0; x < 8; x++) {
/* Grab the pixel from the rows array.
* The pixel value is 0 to 255, so we divide it by 64
* and get a value from 0 to 3. */
pixel = rows[row + y][col + x] / 64;
bit0 = pixel & 1;
bit1 = (pixel & 2) >> 1;
/* We want to put the bits starting from the left of the byte. */
buf1[y] |= bit0 << (7 - x);
buf2[y] |= bit1 << (7 - x);
}
}
/* Write the buffers to the .chr file. The CHR is one sprite at a time, each
* sprite = 16 bytes. The first 8 bytes hold bit 0 and the second 8 bytes
* hold bit 1. Combined, that gives you the palette index. For more info:
* http://www.dustmop.io/blog/2015/04/28/nes-graphics-part-1/#chr-encoding
*/
fwrite(buf1, 8, 1, chrfile);
fwrite(buf2, 8, 1, chrfile);
}
void
exportsprite(FILE *chrfile, Uint8 **rows, Uint8 col, Uint8 row)
{
Uint32 y, x;
Uint8 buf1[8];
Uint8 buf2[8];
Uint8 pixel, bit0, bit1;
for(y = 0; y < 8; y++) {
/* Clear the current byte in the buffer */
buf1[y] = 0;
buf2[y] = 0;
for(x = 0; x < 8; x++) {
/* Grab the pixel from the rows array. */
pixel = rows[row + y][col + x];
/* We only care about bits 0 and 1 of 'pixel': %XXXX XX11
* bit0 goes into buffer one, bit1 into buffer 2. bit1 will be %10
* or %00, so we want to shift it right so that it's %1 or %0.
*/
bit0 = pixel & 1;
bit1 = (pixel & 2) >> 1;
/* We want to put the bits starting from the left of the byte. */
buf1[y] |= bit0 << (7 - x);
buf2[y] |= bit1 << (7 - x);
}
}
/* Write the buffers to the .chr file. The CHR is one sprite at a time, each
* sprite = 16 bytes. The first 8 bytes hold bit 0 and the second 8 bytes
* hold bit 1. Combined, that gives you the palette index. For more info:
* http://www.dustmop.io/blog/2015/04/28/nes-graphics-part-1/#chr-encoding
*/
fwrite(buf1, 8, 1, chrfile);
fwrite(buf2, 8, 1, chrfile);
}
void
putdata(PNG_Doc *iput, FILE *chrfile, enum image_type type)
{
Uint32 y, x;
Uint8 spriteno = 0;
for(y = 0; y < iput->imgh / 8; y++) {
for(x = 0; x < iput->imgw / 8; x++) {
printf("Writing sprite #%d\n", ++spriteno);
switch(type) {
case IMG_PALETTE:
case IMG_GRAYSCALE_4C:
exportsprite(chrfile, iput->rows, x * 8, y * 8); break;
case IMG_GRAYSCALE_256C:
exportsprite_256c(chrfile, iput->rows, x * 8, y * 8); break;
}
}
}
}
void
close(FILE *fp, FILE *output, PNG_Doc *iput)
{
fclose(fp);
fclose(output);
png_destroy_info_struct(iput->imgptr, &iput->infoptr);
png_destroy_read_struct(&iput->imgptr, NULL, NULL);
}
void
usage(char *argv[])
{
printf("Usage: %s input.png [output.chr]\n", argv[0]);
printf("If missing optional [output.chr] param is entered ");
printf("input name will be used as it's base.\n");
}
int
main(int argc, char *argv[])
{
PNG_Doc *input = &inf;
FILE *fp;
FILE *output;
char *outputptr;
Uint8 namelength;
int p_png; /* Predicate PNG - Is it a PNG file? */
Uint32 rowbytes;
if(argc < 2) {
usage(argv);
return 0;
}
/* Open .png file for reading */
fp = fopen(argv[1], "rb");
if(!fp)
return error("bad input", "checkpng - !fp");
if(fread(input->header, 1, HLEN, fp) != HLEN)
return error("wrong number of bytes read", "fread output not 8");
p_png = !png_sig_cmp(input->header, 0, HLEN);
if(!p_png)
return error("Not a png", "png_sig_cmp");
input->imgptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!input->imgptr)
return error("IMGPTR", "PNG Failure");
input->infoptr = png_create_info_struct(input->imgptr);
if(!input->infoptr)
return error("INFOPTR", "PNG Failure");
if(setjmp(png_jmpbuf(input->imgptr))) {
png_destroy_read_struct(&input->imgptr, &input->infoptr, (png_infopp)NULL);
fclose(fp);
return error("PNG", "NULL");
}
/* Use png library to extract data from png file
* extracted data will be stored in input
*/
setuppng(fp, input);
enum image_type type = IMG_UNKNOWN;
if(input->imgw % 8 != 0 || input->imgh % 8 != 0)
return error("PNG Image", "Not divisible by 8");
if ((input->type == PNG_COLOR_TYPE_PALETTE) && (input->depth == 8)) {
type = IMG_PALETTE;
}
if ((input->type == PNG_COLOR_TYPE_GRAY) && (input->depth == 8)) {
/* due to PNG_TRANSFORM_PACKING the bit depth would never be
* less than 8, but it's worth checking if it's a 2bpp image
* re-packed into 8bpp or a native 8bpp image, because
* the significant bits are still the same */
png_byte channels = png_get_channels(input->imgptr,input->infoptr);
assert (channels == 1);
/*would be weird if there's more than 1 channel in a grayscale image without alpha, but we're making sure */
switch (input->original_depth) {
case 2: type = IMG_GRAYSCALE_4C; break;
case 8:
case 16: type = IMG_GRAYSCALE_256C; break;
default: type = IMG_UNKNOWN; break;
}
}
if (type == IMG_UNKNOWN) {
fprintf(stderr,
"The PNG image provided is not supported.\n"
"This program supports the following PNG image types:\n"
" * Indexed color (8 bits per pixel)\n"
" * Grayscale (2 bits per pixel / 4 colors)\n"
" * Grayscale (8 bits per pixel / 256 colors, will be downsampled)\n"
" * RGB (8 or 16 bits per pixel, will be downsampled)\n");
return 1;
}
rowbytes = png_get_rowbytes(input->imgptr, input->infoptr);
if ((type == IMG_PALETTE) && (rowbytes != input->imgw))
return error("Packing", "Failure. imgw != rowbytes");
namelength = strlen(argv[1]);
size_t outnamelength = namelength + 4 + 1; //enough to add a .png at the end of the name
char outputname[outnamelength];
/* Open .chr file for writing */
strcpy(outputname, argv[1]);
char* last_dot = strrchr(outputname,'.'); //find the file's extension by looking
if (!last_dot) last_dot = outputname + strlen(outputname); //if there's none, we'll add one
/* WARNING: If namelength < 3, this will segfault the program. */
last_dot[0] = '.';
last_dot[1] = 'c';
last_dot[2] = 'h';
last_dot[3] = 'r';
last_dot[4] = 0;
/* If a second parameter is entered: use that as output filename. */
outputptr = argc > 2 ? argv[2] : outputname;
output = fopen(outputptr, "wb");
putdata(input, output, type);
close(fp, output, input);
return 0;
}