changeset 2:64d0988b0911

refactor code
author Dennis <denniscmartin@protonmail.com>
date Sun, 16 Oct 2022 16:24:09 +0200
parents edee16cfda92
children d0e9c4ff404c
files .idea/codeStyles/codeStyleConfig.xml algo.c algo.h main.c mazes/maze.png mazes/maze1.png mazes/maze2.png mazes/maze3.png mazes/small_maze.png
diffstat 9 files changed, 157 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.idea/codeStyles/codeStyleConfig.xml	Sun Oct 16 16:24:09 2022 +0200
@@ -0,0 +1,5 @@
+<component name="ProjectCodeStyleConfiguration">
+  <state>
+    <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
+  </state>
+</component>
\ No newline at end of file
--- a/algo.c	Sun Oct 16 14:13:37 2022 +0200
+++ b/algo.c	Sun Oct 16 16:24:09 2022 +0200
@@ -4,15 +4,20 @@
 
 #include "algo.h"
 
-int isWhite(unsigned x, unsigned y, png_bytep* pRows) {
+int isPath(unsigned x, unsigned y, png_bytep* pRows) {
     png_byte *pRow = pRows[y];
     png_byte *pPixel = &pRow[x * 4];
 
-    int r = pPixel[0];
-    int g = pPixel[1];
-    int b = pPixel[2];
+    /*
+     * pPixel[0] -> R
+     * pPixel[1] -> G
+     * pPixel[2] -> B
+     */
 
-    if (r == 255 && g == 255 && b == 255) {
+    if (pPixel[0] == 255) {
+        pPixel[1] = 0;
+        pPixel[2] = 0;
+
         return 1;
     } else {
         return 0;
@@ -25,32 +30,31 @@
     unsigned int y = 1;
     char direction = 'R';
 
+    isPath(x, y, pRows);
+
     while (x < width && y < width) {
 
-        // Set pixel to color red
-        printf("x: %d, y: %d is white\n", x, y);
-
         if (x == width - 1 && y == width - 2) {
-            exit(0);
+            break;
         }
 
         switch (direction) {  // NOLINT(hicpp-multiway-paths-covered)
             case 'R':
 
                 // Check if down position is white
-                if (isWhite(x, y + 1, pRows)) {
+                if (isPath(x, y + 1, pRows)) {
                     ++y;
                     direction = 'D';
                 }
 
                 // Check if right position is white
-                else if (isWhite(x + 1, y, pRows)) {
+                else if (isPath(x + 1, y, pRows)) {
                     ++x;
                     direction = 'R';
                 }
 
                 // Check if up position is white
-                else if (isWhite(x, y - 1, pRows)) {
+                else if (isPath(x, y - 1, pRows)) {
                     --y;
                     direction = 'U';
                 }
@@ -66,19 +70,19 @@
             case 'L':
 
                 // Check if up position is white
-                if (isWhite(x, y - 1, pRows)) {
+                if (isPath(x, y - 1, pRows)) {
                     --y;
                     direction = 'U';
                 }
 
                 // Check if left position is white
-                else if (isWhite(x - 1, y, pRows)) {
+                else if (isPath(x - 1, y, pRows)) {
                     --x;
                     direction = 'L';
                 }
 
                 // Check if down position is white
-                else if (isWhite(x, y + 1, pRows)) {
+                else if (isPath(x, y + 1, pRows)) {
                     ++y;
                     direction = 'D';
                 }
@@ -94,19 +98,19 @@
             case 'U':
 
                 // Check if right position is white
-                if (isWhite(x + 1, y, pRows)) {
+                if (isPath(x + 1, y, pRows)) {
                     ++x;
                     direction = 'R';
                 }
 
                 // Check if up position is white
-                else if (isWhite(x, y - 1, pRows)) {
+                else if (isPath(x, y - 1, pRows)) {
                     --y;
                     direction = 'U';
                 }
 
                 // Check if left position is white
-                else if (isWhite(x - 1, y, pRows)) {
+                else if (isPath(x - 1, y, pRows)) {
                     --x;
                     direction = 'L';
                 }
@@ -122,19 +126,19 @@
             case 'D':
 
                 // Check if left position is white
-                if (isWhite(x - 1, y, pRows)) {
+                if (isPath(x - 1, y, pRows)) {
                     --x;
                     direction = 'L';
                 }
 
                 // Check if down position is white
-                else if (isWhite(x, y + 1, pRows)) {
+                else if (isPath(x, y + 1, pRows)) {
                     ++y;
                     direction = 'D';
                 }
 
                 // Check if right position is white
-                else if (isWhite(x + 1, y, pRows)) {
+                else if (isPath(x + 1, y, pRows)) {
                     ++x;
                     direction = 'R';
                 }
--- a/algo.h	Sun Oct 16 14:13:37 2022 +0200
+++ b/algo.h	Sun Oct 16 16:24:09 2022 +0200
@@ -6,6 +6,7 @@
 #define MAZE_SOLVER_ALGO_H
 
 #include <png.h>
+#include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 
--- a/main.c	Sun Oct 16 14:13:37 2022 +0200
+++ b/main.c	Sun Oct 16 16:24:09 2022 +0200
@@ -1,80 +1,97 @@
 #include "algo.h"
 
 int main(int argc, char* argv[]) {
-    char* fileName;
-    int numberOfPhases;
+    char* unsolvedFilename;
+    char* solvedFilename;
+    int phases;
     unsigned int width, height;
 
-    png_structp pngStruct;
-    png_infop pngInfo;
+    clock_t start, end;
+    double cpuTimeUsed;
+
+    FILE* unsolvedFp;
+    png_structp unsolvedPngStruct;
+    png_infop unsolvedPngInfo;
     png_byte colorType;
     png_byte bitDepth;
     png_bytep* pRows;
 
+    FILE* solvedFp;
+    png_structp solvedPngStruct;
+    png_infop solvedPngInfo;
+
+    start = clock();
+
     if (argc < 2) {
         printf("Incorrect arguments\n");
         abort();
     }
 
     // Get user arguments
-    fileName = argv[1];
+    unsolvedFilename = argv[1];
 
-    // Add path to filename
-    asprintf(&fileName, "mazes/%s", fileName);
+    /*
+     * READ FILE
+     */
 
-    FILE *fp = fopen(fileName, "rb");
+    // Add path to unsolvedFilename
+    asprintf(&unsolvedFilename, "mazes/%s", unsolvedFilename);
 
-    if (!fp) {
-        printf("Error opening image named %s\n", fileName);
+    // Open file
+    unsolvedFp = fopen(unsolvedFilename, "rb");
+
+    if (!unsolvedFp) {
+        printf("Error opening image named %s\n", unsolvedFilename);
         abort();
     }
 
-    // Allocate and initialize a pngStruct for reading PNG file
-    pngStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    // Allocate and initialize a unsolvedPngStruct for reading PNG file
+    unsolvedPngStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
-    if (!pngStruct) {
+    if (!unsolvedPngStruct) {
         printf("png_create_read_struct failed\n");
         abort();
     }
 
-    // Allocate and initialize a pngInfo structure
-    pngInfo = png_create_info_struct(pngStruct);
+    // Allocate and initialize a unsolvedPngInfo structure
+    unsolvedPngInfo = png_create_info_struct(unsolvedPngStruct);
 
-    if (!pngInfo) {
+    if (!unsolvedPngInfo) {
         printf("png_create_info_struct failed\n");
+        abort();
     }
 
     /*
      * When libpng encounters an error, it expects to longjmp back to your routine.
-     * Therefore, you will need to call setjmp and pass your png_jmpbuf(pngStruct).
+     * Therefore, you will need to call setjmp and pass your png_jmpbuf(unsolvedPngStruct).
      * More about setjmp -> https://es.wikipedia.org/wiki/Setjmp.h
      */
 
-    if (setjmp(png_jmpbuf(pngStruct))) {
+    if (setjmp(png_jmpbuf(unsolvedPngStruct))) {
         printf("Error during init_io\n");
         abort();
     }
 
-    png_init_io(pngStruct, fp);             // Initialize the default input/output functions for the PNG file
-    png_set_sig_bytes(pngStruct, 0);        // Set signature bytes
-    png_read_info(pngStruct, pngInfo);      // Read info
+    png_init_io(unsolvedPngStruct, unsolvedFp);             // Initialize the default input/output functions for the PNG file
+    png_set_sig_bytes(unsolvedPngStruct, 0);        // Set signature bytes
+    png_read_info(unsolvedPngStruct, unsolvedPngInfo);      // Read info
 
-    width = png_get_image_width(pngStruct, pngInfo);
-    height = png_get_image_height(pngStruct, pngInfo);
-    colorType = png_get_color_type(pngStruct, pngInfo);
-    bitDepth = png_get_bit_depth(pngStruct, pngInfo);
+    width = png_get_image_width(unsolvedPngStruct, unsolvedPngInfo);
+    height = png_get_image_height(unsolvedPngStruct, unsolvedPngInfo);
+    colorType = png_get_color_type(unsolvedPngStruct, unsolvedPngInfo);
+    bitDepth = png_get_bit_depth(unsolvedPngStruct, unsolvedPngInfo);
 
-    numberOfPhases = png_set_interlace_handling(pngStruct);
-    png_read_update_info(pngStruct, pngInfo);
+    phases = png_set_interlace_handling(unsolvedPngStruct);
+    png_read_update_info(unsolvedPngStruct, unsolvedPngInfo);
 
     printf("Image width: %d\n", width);
     printf("Image height: %d\n", height);
     printf("Color type: %d\n", colorType);
     printf("Bit depth: %d\n", bitDepth);
-    printf("Number of phases: %d\n", numberOfPhases);
+    printf("Number of phases: %d\n", phases);
 
     // Read file
-    if (setjmp(png_jmpbuf(pngStruct))) {
+    if (setjmp(png_jmpbuf(unsolvedPngStruct))) {
         printf("Error during read_image");
         abort();
     }
@@ -82,21 +99,98 @@
     pRows = (png_bytep*) malloc(sizeof(png_bytep) * height);
 
     for (int y = 0; y < height; y++) {
-        pRows[y] = (png_byte *) malloc(png_get_rowbytes(pngStruct, pngInfo));
+        pRows[y] = (png_byte *) malloc(png_get_rowbytes(unsolvedPngStruct, unsolvedPngInfo));
     }
 
     // Read the image into memory
-    png_read_image(pngStruct, pRows);
-    fclose(fp);
+    png_read_image(unsolvedPngStruct, pRows);
+    fclose(unsolvedFp);
+
+    /*
+     * ALGOS
+     */
 
     wallFollower(pRows, width);
 
+    /*
+     * WRITE FILE
+     */
+
+    // Add path to unsolvedFilename
+    solvedFilename = argv[1];
+    asprintf(&solvedFilename, "sols/%s", solvedFilename);
+
+    // Open file
+    solvedFp = fopen(solvedFilename, "wb");
+
+    if (!solvedFp) {
+        printf("File %s could not be opened for writing", solvedFilename);
+        abort();
+    }
+
+    // Allocate and initialize a solvedPngStruct for writting PNG file
+    solvedPngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+    if (!solvedPngStruct) {
+        printf("png_create_read_struct failed\n");
+        abort();
+    }
+
+    // Allocate and initialize a unsolvedPngInfo structure
+    solvedPngInfo = png_create_info_struct(solvedPngStruct);
+
+    if (!solvedPngInfo) {
+        printf("png_create_info_struct failed\n");
+        abort();
+    }
+
+    if (setjmp(png_jmpbuf(solvedPngStruct))) {
+        printf("Error during init_io\n");
+        abort();
+    }
+
+    png_init_io(solvedPngStruct, solvedFp);
+
+    // Write header
+    if (setjmp(png_jmpbuf(solvedPngStruct))) {
+        printf("Error writing header");
+        abort();
+    }
+
+    png_set_IHDR(solvedPngStruct, solvedPngInfo, width, height, bitDepth, colorType, PNG_INTERLACE_NONE,
+                 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE
+     );
+
+    png_write_info(solvedPngStruct, solvedPngInfo);
+
+    // Write bytes
+    if (setjmp(png_jmpbuf(solvedPngStruct))) {
+        printf("Error writing bytes");
+        abort();
+    }
+
+    png_write_image(solvedPngStruct, pRows);
+
+    // End write
+    if (setjmp(png_jmpbuf(solvedPngStruct))) {
+        printf("Error during end of write");
+        abort();
+    }
+
+    png_write_end(solvedPngStruct, NULL);
+
     // Cleanup heap allocation
     for (int y = 0; y < height; y++) {
         free(pRows[y]);
     }
 
     free(pRows);
+    fclose(solvedFp);
+
+    end = clock();
+    cpuTimeUsed = ((double) (end - start)) / CLOCKS_PER_SEC;
+
+    printf("Maze solved in %f seconds\n", cpuTimeUsed);
 
     return 0;
 }
\ No newline at end of file
Binary file mazes/maze.png has changed
Binary file mazes/maze1.png has changed
Binary file mazes/maze2.png has changed
Binary file mazes/maze3.png has changed
Binary file mazes/small_maze.png has changed