diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3383878 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +xiew diff --git a/compile.sh b/compile.sh index 5797789..49add37 100644 --- a/compile.sh +++ b/compile.sh @@ -2,6 +2,6 @@ set -xe -gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiew xiew.c +gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiew xiew.c -lm -lpng exit diff --git a/xiew.c b/xiew.c index f5f0b55..f656e8a 100644 --- a/xiew.c +++ b/xiew.c @@ -1,8 +1,97 @@ #include +#include +#include +#include #include +static unsigned int * image_buffer = NULL; +static unsigned int image_width = 0; +static unsigned int image_height = 0; + +static void import_image (char * file_path) { + png_image temporary = { 0 }; + + temporary.version = PNG_IMAGE_VERSION; + + if (png_image_begin_read_from_file (& temporary, file_path) == 0) { + printf ("PNG: Image init fuck up!\n"); + exit (EXIT_FAILURE); + } + + temporary.format = PNG_FORMAT_RGB; + + image_width = temporary.width; + image_height = temporary.height; + + image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer)); + + if (png_image_finish_read (& temporary, NULL, image_buffer, 0, NULL) == 0) { + printf ("PNG: Image read fuck up!\n"); + exit (EXIT_FAILURE); + } + + png_image_free (& temporary); +} + +static void export_image (char * file_path) { + png_image temporary = { 0 }; + + temporary.version = PNG_IMAGE_VERSION; + temporary.format = PNG_FORMAT_RGB; + temporary.width = image_width; + temporary.height = image_height; + + if (png_image_write_to_file (& temporary, file_path, 0, image_buffer, 0, NULL) == 0) { + printf ("PNG: Image write fuck up!\n"); + } + + png_image_free (& temporary); +} + int main (int argc, char * * argv) { - (void) argc; - (void) argv; - return (0); + FILE * file = NULL; + + size_t file_size = 0; + + if (argc != 4) { + printf ("Encode: xiew -e input output\n"); + printf ("Decode: xiew -d input output\n"); + return (EXIT_FAILURE); + } + + if (strcmp (argv [1], "-e") == 0) { + printf ("Encoding...\n"); + file = fopen (argv [2], "r"); + if (file == NULL) printf ("File is null.\n"); + fseek (file, 0, SEEK_END); + file_size = (size_t) ftell (file); + printf ("File size: %lu\n", file_size); + fseek (file, 0, SEEK_SET); + image_width = image_height = (unsigned int) (sqrt (((double) file_size) / 3.0) + 1); + printf ("Image size: %u x %u\n", image_width, image_height); + image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer)); + image_buffer [0] = (unsigned int) file_size; + fread (& image_buffer [1], sizeof (char), file_size, file); + export_image (argv [3]); + } else if (strcmp (argv [1], "-d") == 0) { + printf ("Decoding...\n"); + import_image (argv [2]); + file = fopen (argv [3], "w"); + if (file == NULL) printf ("File is null.\n"); + file_size = image_buffer [0]; + printf ("File size: %lu\n", file_size); + fwrite (& image_buffer [1], sizeof (char), file_size, file); + } else { + printf ("Encode: xiew -e input output\n"); + printf ("Decode: xiew -d input output\n"); + return (EXIT_FAILURE); + } + + if (image_buffer != NULL) { + free (image_buffer); + } + + fclose (file); + + return (EXIT_SUCCESS); }