File images2db.c added (mode: 100644) (index 0000000..1e7196c) |
|
1 |
|
#include <stdlib.h> |
|
2 |
|
#include <stdio.h> |
|
3 |
|
#include <png.h> |
|
4 |
|
|
|
5 |
|
#define NUM_IMAGES 4 |
|
6 |
|
#define ORDER_IMAGES 5 |
|
7 |
|
#define ERROR 1 |
|
8 |
|
#define DB_NAME ORDER_IMAGES |
|
9 |
|
#define INPUT_NUM ORDER_IMAGES + 1 |
|
10 |
|
/* errors */ |
|
11 |
|
#define ERR_PNG_COLOR "error: image is colored\n" |
|
12 |
|
#define ERR_PNG_DEPTH "error: image has wrong bit depth\n" |
|
13 |
|
#define ERR_PNG_DIFF_SIZE "error: images are of different size\n" |
|
14 |
|
#define ERR_OPEN_DB "error: can't open database\n" |
|
15 |
|
#define ERR_USAGE "usage: %s map highmap watermap ownersmap database\n" |
|
16 |
|
|
|
17 |
|
int width, height; |
|
18 |
|
png_byte color_type; |
|
19 |
|
png_byte bit_depth; |
|
20 |
|
png_bytep *img_row_pointers[NUM_IMAGES]; |
|
21 |
|
|
|
22 |
|
void input_handler(int argc, char **argv); |
|
23 |
|
void print_exit(char *text, int val); |
|
24 |
|
|
|
25 |
|
void read_png_file(char *filename, int img_num); |
|
26 |
|
void write_db(FILE *db); |
|
27 |
|
|
|
28 |
|
int main(int argc, char **argv) { |
|
29 |
|
FILE *db; |
|
30 |
|
int i; |
|
31 |
|
|
|
32 |
|
for(i=0;i<NUM_IMAGES;i++) { |
|
33 |
|
img_row_pointers[i] = NULL; |
|
34 |
|
read_png_file(argv[i+1], i); |
|
35 |
|
} |
|
36 |
|
|
|
37 |
|
/* open database, write pixels data and close*/ |
|
38 |
|
input_handler(argc, argv); |
|
39 |
|
db = fopen(argv[DB_NAME], "w"); |
|
40 |
|
if(!db) |
|
41 |
|
print_exit(ERR_OPEN_DB, ERROR); |
|
42 |
|
write_db(db); |
|
43 |
|
fclose(db); |
|
44 |
|
|
|
45 |
|
return EXIT_SUCCESS; |
|
46 |
|
} |
|
47 |
|
|
|
48 |
|
void input_handler(int argc, char **argv) { |
|
49 |
|
if(argc!=INPUT_NUM) { |
|
50 |
|
printf(ERR_USAGE, argv[0]); |
|
51 |
|
exit(ERROR); |
|
52 |
|
} |
|
53 |
|
} |
|
54 |
|
|
|
55 |
|
void print_exit(char *text, int val) { |
|
56 |
|
printf(text); |
|
57 |
|
exit(val); |
|
58 |
|
} |
|
59 |
|
|
|
60 |
|
void read_png_file(char *filename, int img_num) { |
|
61 |
|
FILE *fp = fopen(filename, "rb"); |
|
62 |
|
int y; |
|
63 |
|
|
|
64 |
|
png_structp png = |
|
65 |
|
png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
|
66 |
|
if(!png) abort(); |
|
67 |
|
|
|
68 |
|
png_infop info = png_create_info_struct(png); |
|
69 |
|
if(!info) abort(); |
|
70 |
|
|
|
71 |
|
if(setjmp(png_jmpbuf(png))) abort(); |
|
72 |
|
|
|
73 |
|
png_init_io(png, fp); |
|
74 |
|
|
|
75 |
|
png_read_info(png, info); |
|
76 |
|
|
|
77 |
|
if(img_num && (width!=png_get_image_width(png, info) || |
|
78 |
|
height!=png_get_image_height(png, info))) |
|
79 |
|
print_exit(ERR_PNG_DIFF_SIZE, ERROR); |
|
80 |
|
else { |
|
81 |
|
width = png_get_image_width(png, info); |
|
82 |
|
height = png_get_image_height(png, info); |
|
83 |
|
} |
|
84 |
|
color_type = png_get_color_type(png, info); |
|
85 |
|
bit_depth = png_get_bit_depth(png, info); |
|
86 |
|
|
|
87 |
|
if(color_type==PNG_COLOR_TYPE_PALETTE || color_type==PNG_COLOR_TYPE_RGB || |
|
88 |
|
color_type==PNG_COLOR_TYPE_GRAY_ALPHA) |
|
89 |
|
print_exit(ERR_PNG_COLOR, ERROR); |
|
90 |
|
if(bit_depth==16) |
|
91 |
|
print_exit(ERR_PNG_DEPTH, ERROR); |
|
92 |
|
|
|
93 |
|
png_read_update_info(png, info); |
|
94 |
|
|
|
95 |
|
if (img_row_pointers[img_num]) abort(); |
|
96 |
|
|
|
97 |
|
img_row_pointers[img_num] = (png_bytep*)malloc(sizeof(png_bytep) * height); |
|
98 |
|
for(y = 0; y < height; y++) { |
|
99 |
|
img_row_pointers[img_num][y] = |
|
100 |
|
(png_byte*)malloc(png_get_rowbytes(png,info)); |
|
101 |
|
} |
|
102 |
|
|
|
103 |
|
png_read_image(png, img_row_pointers[img_num]); |
|
104 |
|
|
|
105 |
|
fclose(fp); |
|
106 |
|
|
|
107 |
|
png_destroy_read_struct(&png, &info, NULL); |
|
108 |
|
png = NULL; |
|
109 |
|
info = NULL; |
|
110 |
|
} |
|
111 |
|
|
|
112 |
|
void write_db(FILE *db) { |
|
113 |
|
int i, x, y; |
|
114 |
|
|
|
115 |
|
for(y=0;y<height;y++) |
|
116 |
|
for(x=0;x<width;x++) { |
|
117 |
|
for(i=0;i<NUM_IMAGES;i++) { |
|
118 |
|
if(i) fprintf(db, " "); |
|
119 |
|
fprintf(db, "%u", (unsigned char)(img_row_pointers[i][y][x])); |
|
120 |
|
} |
|
121 |
|
fprintf(db, "\n"); |
|
122 |
|
} |
|
123 |
|
fprintf(db,"#\n%d %d\n", height, width); |
|
124 |
|
} |
|
125 |
|
/* type subtype .subsubtype... |
|
126 |
|
* 0 land 0 lowland, 1 plain, 2 hills, 3 highlands, 4 mountains, |
|
127 |
|
* 5 hightmounts |
|
128 |
|
* 2 wasteland |
|
129 |
|
* 1 water |
|
130 |
|
*/ |