Last active
November 4, 2022 14:38
-
-
Save weiss/258cabcf5a2f5a424b8af83a3d7d9a23 to your computer and use it in GitHub Desktop.
Read farbfeld image into raw buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <err.h> | |
#include <inttypes.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <arpa/inet.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
int | |
main(int argc, char **argv) | |
{ | |
struct stat s; | |
unsigned char *img; | |
FILE *f; | |
if (argc != 2) | |
errx(EXIT_FAILURE, "Usage: ff <file>"); | |
if (stat(argv[1], &s) != 0) | |
err(EXIT_FAILURE, "Cannot stat %s", argv[1]); | |
if (s.st_size < 16) | |
errx(EXIT_FAILURE, "%s is too small", argv[1]); | |
if ((img = malloc(s.st_size)) == NULL) | |
err(EXIT_FAILURE, "Cannot allocate memory for reading %s", argv[1]); | |
if ((f = fopen(argv[1], "r")) == NULL) | |
err(EXIT_FAILURE, "Cannot open %s", argv[1]); | |
if (fread(img, 1, s.st_size, f) != (size_t)s.st_size) | |
err(EXIT_FAILURE, "Cannot read %s", argv[1]); | |
if (fclose(f) != 0) | |
err(EXIT_FAILURE, "Cannot close %s", argv[1]); | |
if (memcmp(img, "farbfeld", sizeof("farbfeld") - 1) != 0) | |
errx(EXIT_FAILURE, "%s is not a farbfeld image", argv[1]); | |
(void)printf("W: %"PRIu32"\n", ntohl(((uint32_t *)img)[2])); | |
(void)printf("H: %"PRIu32"\n", ntohl(((uint32_t *)img)[3])); | |
free(img); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment