diff options
| author | Clement Sibille <clements+github@lisible.xyz> | 2024-06-09 18:08:51 +0900 |
|---|---|---|
| committer | Clement Sibille <clements+github@lisible.xyz> | 2024-06-09 18:08:51 +0900 |
| commit | ff11942fb7abfc68e2f04350f7d0a2dde51b1d9e (patch) | |
| tree | 38f5c1c074665e3b5c9727b0f6fd7e3f0c5edd60 /lisiblepng-bin | |
| parent | 4c88f44d42c5aace3264bfc656ae656b6b3e7373 (diff) | |
Make LisPng_decode use a u8* instead of a FILE*
Diffstat (limited to 'lisiblepng-bin')
| -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; } |
