Read PNG Magic

This commit is contained in:
Pedro Portela 2025-06-04 23:54:47 +02:00
parent f0434c3de9
commit fd0f3fb901
3 changed files with 41 additions and 3 deletions

23
img_png.c Normal file
View File

@ -0,0 +1,23 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "img_png.h"
uint8_t* img_png_decode(FILE *fp) {
unsigned char buffer[8];
size_t bufsize = sizeof(buffer) / sizeof((buffer)[0]);
size_t ret = fread(buffer, sizeof(*buffer), bufsize, fp);
if(ret != bufsize) {
fprintf(stderr, "fread() failed. Expected %zu bytes, got %zu\n", bufsize, ret);
return NULL;
}
printf("PNG Magic: %#04x%02x%02x%02x%02x%02x%02x%02x\n", buffer[0], buffer[1], buffer[2],
buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]);
return NULL;
}

View File

@ -1,6 +1,13 @@
#include <stdint.h>
#include <stdio.h>
const char img_png_signature[] = {0x89, };
//const unsigned char img_png_signature[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
struct img_png_chunk {
int length;
int chunk_type;
char* chunk_data;
int crc;
};
uint8_t* img_png_decode (FILE* fp);

10
main.c
View File

@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "img_png.h"
int main(int argc, char** argv) {
if(argc != 2) {
fprintf(stderr, "Usage: ./imgvwr FILE");
fprintf(stderr, "Usage: ./imgvwr FILE\n");
exit(EXIT_FAILURE);
}
@ -13,5 +14,12 @@ int main(int argc, char** argv) {
exit(EXIT_FAILURE);
}
/*if(!img_png_decode(fp_image)) {
fprintf(stderr, "Could not parse PNG file!\n");
exit(EXIT_FAILURE);
}*/
img_png_decode(fp_image);
return 0;
}