Mercurial > public > maze-solver
annotate main.c @ 5:3d6399799527
add maze generator link
author | Dennis <denniscmartin@protonmail.com> |
---|---|
date | Sun, 16 Oct 2022 16:51:53 +0200 |
parents | 64d0988b0911 |
children |
rev | line source |
---|---|
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
1 #include "algo.h" |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
2 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
3 int main(int argc, char* argv[]) { |
2 | 4 char* unsolvedFilename; |
5 char* solvedFilename; | |
6 int phases; | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
7 unsigned int width, height; |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
8 |
2 | 9 clock_t start, end; |
10 double cpuTimeUsed; | |
11 | |
12 FILE* unsolvedFp; | |
13 png_structp unsolvedPngStruct; | |
14 png_infop unsolvedPngInfo; | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
15 png_byte colorType; |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
16 png_byte bitDepth; |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
17 png_bytep* pRows; |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
18 |
2 | 19 FILE* solvedFp; |
20 png_structp solvedPngStruct; | |
21 png_infop solvedPngInfo; | |
22 | |
23 start = clock(); | |
24 | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
25 if (argc < 2) { |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
26 printf("Incorrect arguments\n"); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
27 abort(); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
28 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
29 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
30 // Get user arguments |
2 | 31 unsolvedFilename = argv[1]; |
0 | 32 |
2 | 33 /* |
34 * READ FILE | |
35 */ | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
36 |
2 | 37 // Add path to unsolvedFilename |
38 asprintf(&unsolvedFilename, "mazes/%s", unsolvedFilename); | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
39 |
2 | 40 // Open file |
41 unsolvedFp = fopen(unsolvedFilename, "rb"); | |
42 | |
43 if (!unsolvedFp) { | |
44 printf("Error opening image named %s\n", unsolvedFilename); | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
45 abort(); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
46 } |
0 | 47 |
2 | 48 // Allocate and initialize a unsolvedPngStruct for reading PNG file |
49 unsolvedPngStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
50 |
2 | 51 if (!unsolvedPngStruct) { |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
52 printf("png_create_read_struct failed\n"); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
53 abort(); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
54 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
55 |
2 | 56 // Allocate and initialize a unsolvedPngInfo structure |
57 unsolvedPngInfo = png_create_info_struct(unsolvedPngStruct); | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
58 |
2 | 59 if (!unsolvedPngInfo) { |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
60 printf("png_create_info_struct failed\n"); |
2 | 61 abort(); |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
62 } |
0 | 63 |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
64 /* |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
65 * When libpng encounters an error, it expects to longjmp back to your routine. |
2 | 66 * Therefore, you will need to call setjmp and pass your png_jmpbuf(unsolvedPngStruct). |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
67 * More about setjmp -> https://es.wikipedia.org/wiki/Setjmp.h |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
68 */ |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
69 |
2 | 70 if (setjmp(png_jmpbuf(unsolvedPngStruct))) { |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
71 printf("Error during init_io\n"); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
72 abort(); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
73 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
74 |
2 | 75 png_init_io(unsolvedPngStruct, unsolvedFp); // Initialize the default input/output functions for the PNG file |
76 png_set_sig_bytes(unsolvedPngStruct, 0); // Set signature bytes | |
77 png_read_info(unsolvedPngStruct, unsolvedPngInfo); // Read info | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
78 |
2 | 79 width = png_get_image_width(unsolvedPngStruct, unsolvedPngInfo); |
80 height = png_get_image_height(unsolvedPngStruct, unsolvedPngInfo); | |
81 colorType = png_get_color_type(unsolvedPngStruct, unsolvedPngInfo); | |
82 bitDepth = png_get_bit_depth(unsolvedPngStruct, unsolvedPngInfo); | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
83 |
2 | 84 phases = png_set_interlace_handling(unsolvedPngStruct); |
85 png_read_update_info(unsolvedPngStruct, unsolvedPngInfo); | |
0 | 86 |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
87 printf("Image width: %d\n", width); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
88 printf("Image height: %d\n", height); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
89 printf("Color type: %d\n", colorType); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
90 printf("Bit depth: %d\n", bitDepth); |
2 | 91 printf("Number of phases: %d\n", phases); |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
92 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
93 // Read file |
2 | 94 if (setjmp(png_jmpbuf(unsolvedPngStruct))) { |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
95 printf("Error during read_image"); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
96 abort(); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
97 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
98 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
99 pRows = (png_bytep*) malloc(sizeof(png_bytep) * height); |
0 | 100 |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
101 for (int y = 0; y < height; y++) { |
2 | 102 pRows[y] = (png_byte *) malloc(png_get_rowbytes(unsolvedPngStruct, unsolvedPngInfo)); |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
103 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
104 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
105 // Read the image into memory |
2 | 106 png_read_image(unsolvedPngStruct, pRows); |
107 fclose(unsolvedFp); | |
108 | |
109 /* | |
110 * ALGOS | |
111 */ | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
112 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
113 wallFollower(pRows, width); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
114 |
2 | 115 /* |
116 * WRITE FILE | |
117 */ | |
118 | |
119 // Add path to unsolvedFilename | |
120 solvedFilename = argv[1]; | |
121 asprintf(&solvedFilename, "sols/%s", solvedFilename); | |
122 | |
123 // Open file | |
124 solvedFp = fopen(solvedFilename, "wb"); | |
125 | |
126 if (!solvedFp) { | |
127 printf("File %s could not be opened for writing", solvedFilename); | |
128 abort(); | |
129 } | |
130 | |
131 // Allocate and initialize a solvedPngStruct for writting PNG file | |
132 solvedPngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
133 | |
134 if (!solvedPngStruct) { | |
135 printf("png_create_read_struct failed\n"); | |
136 abort(); | |
137 } | |
138 | |
139 // Allocate and initialize a unsolvedPngInfo structure | |
140 solvedPngInfo = png_create_info_struct(solvedPngStruct); | |
141 | |
142 if (!solvedPngInfo) { | |
143 printf("png_create_info_struct failed\n"); | |
144 abort(); | |
145 } | |
146 | |
147 if (setjmp(png_jmpbuf(solvedPngStruct))) { | |
148 printf("Error during init_io\n"); | |
149 abort(); | |
150 } | |
151 | |
152 png_init_io(solvedPngStruct, solvedFp); | |
153 | |
154 // Write header | |
155 if (setjmp(png_jmpbuf(solvedPngStruct))) { | |
156 printf("Error writing header"); | |
157 abort(); | |
158 } | |
159 | |
160 png_set_IHDR(solvedPngStruct, solvedPngInfo, width, height, bitDepth, colorType, PNG_INTERLACE_NONE, | |
161 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE | |
162 ); | |
163 | |
164 png_write_info(solvedPngStruct, solvedPngInfo); | |
165 | |
166 // Write bytes | |
167 if (setjmp(png_jmpbuf(solvedPngStruct))) { | |
168 printf("Error writing bytes"); | |
169 abort(); | |
170 } | |
171 | |
172 png_write_image(solvedPngStruct, pRows); | |
173 | |
174 // End write | |
175 if (setjmp(png_jmpbuf(solvedPngStruct))) { | |
176 printf("Error during end of write"); | |
177 abort(); | |
178 } | |
179 | |
180 png_write_end(solvedPngStruct, NULL); | |
181 | |
1
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
182 // Cleanup heap allocation |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
183 for (int y = 0; y < height; y++) { |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
184 free(pRows[y]); |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
185 } |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
186 |
edee16cfda92
algo working but inefficient
Dennis <denniscmartin@protonmail.com>
parents:
0
diff
changeset
|
187 free(pRows); |
2 | 188 fclose(solvedFp); |
189 | |
190 end = clock(); | |
191 cpuTimeUsed = ((double) (end - start)) / CLOCKS_PER_SEC; | |
192 | |
193 printf("Maze solved in %f seconds\n", cpuTimeUsed); | |
0 | 194 |
195 return 0; | |
196 } |