diff options
Diffstat (limited to 'lisiblepng-bin/src')
| -rw-r--r-- | lisiblepng-bin/src/main.c | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/lisiblepng-bin/src/main.c b/lisiblepng-bin/src/main.c index 47e1722..116125a 100644 --- a/lisiblepng-bin/src/main.c +++ b/lisiblepng-bin/src/main.c @@ -1,8 +1,12 @@ #include <errno.h> +#include <fcntl.h> #include <lisiblepng.h> #include <lisiblestd/log.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> #define LOG0(msg) fprintf(stderr, msg "\n") #define LOGN(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__) @@ -15,19 +19,19 @@ int main(int argc, char **argv) { } const char *png_filepath = argv[1]; - FILE *png_file = fopen(png_filepath, "r"); - fseek(png_file, 0, SEEK_END); - long file_size = ftell(png_file); - LOGN("File size: %ld bytes", file_size); - fseek(png_file, 0, SEEK_SET); + int fd = open(png_filepath, O_RDONLY); + struct stat st; + fstat(fd, &st); + u8 *png_file_data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + LOGN("File size: %ld bytes", st.st_size); - if (!png_file) { + if (!png_file_data) { const char *error_message = strerror(errno); - LOGN("Couldn't open PNG file: %s", error_message); + LOGN("Couldn't map PNG file: %s", error_message); return 1; } - LisPng *png = LisPng_decode(png_file); + LisPng *png = LisPng_decode(png_file_data, st.st_size); if (!png) { LOG0("Couldn't decode PNG"); return 1; @@ -36,9 +40,9 @@ int main(int argc, char **argv) { LisPng_dump_ppm(png); LisPng_destroy(png); - if (fclose(png_file) != 0) { + if (munmap(png_file_data, st.st_size) != 0) { const char *error_message = strerror(errno); - LOGN("Couldn't close PNG file: %s", error_message); + LOGN("Couldn't unmap PNG file: %s", error_message); return 1; } |
