List of commits:
Subject Hash Author Date (UTC)
init commit 344597c3f1de587070e3d6c26d8d505c860ad4ed sdfsdf 2021-03-23 08:23:20
Commit 344597c3f1de587070e3d6c26d8d505c860ad4ed - init commit
Author: sdfsdf
Author date (UTC): 2021-03-23 08:23
Committer name: sdfsdf
Committer date (UTC): 2021-03-23 08:23
Parent(s):
Signing key:
Tree: 4c4b1badc7833d520869c910e06368f2d95b3978
File Lines added Lines deleted
Img/highmap.png 0 0
Img/map.png 0 0
Img/ownersmap.png 0 0
Img/watermap.png 0 0
images2db.c 130 0
makefile 10 0
File Img/highmap.png added (mode: 100644) (index 0000000..f092ba0)
File Img/map.png added (mode: 100644) (index 0000000..9bc1cf1)
File Img/ownersmap.png added (mode: 100644) (index 0000000..3542142)
File Img/watermap.png added (mode: 100644) (index 0000000..049ea72)
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 */
File makefile added (mode: 100644) (index 0000000..5e6287e)
1 CC = gcc
2 CFLAGS = -Wall -g -lpng
3 IMAGES = $(addsuffix .png,$(addprefix Img/,map highmap watermap ownersmap))
4 DB = db
5
6 images2db: images2db.c
7 $(CC) $(CFLAGS) $< -o $@
8
9 run: images2db
10 ./$< $(IMAGES) $(DB)
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/sdfgsdfg/shitcity

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/sdfgsdfg/shitcity

Clone this repository using git:
git clone git://git.rocketgit.com/user/sdfgsdfg/shitcity

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main