File Restructuring
This commit is contained in:
parent
332b48c220
commit
3823279856
9
common/image.c
Normal file
9
common/image.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include "image.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void free_image(Image* img) {
|
||||
if (img && img->data) {
|
||||
free(img->data);
|
||||
img->data = NULL;
|
||||
}
|
||||
}
|
10
common/image.h
Normal file
10
common/image.h
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
int width;
|
||||
int height;
|
||||
int channels;
|
||||
uint8_t* data;
|
||||
} image_t;
|
||||
|
||||
void free_image(image_t* img);
|
7
common/utils.c
Normal file
7
common/utils.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include "common.h"
|
||||
|
||||
uint32_t char_to_uint32 (unsigned char* input) {
|
||||
uint32_t val = (input[0] << 24) + (input[1] << 16) + (input[2] << 8) + input[3];
|
||||
|
||||
return val;
|
||||
}
|
3
common/utils.h
Normal file
3
common/utils.h
Normal file
@ -0,0 +1,3 @@
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t char_to_uint32(unsigned char *input);
|
85
formats/img_png.c
Normal file
85
formats/img_png.c
Normal file
@ -0,0 +1,85 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "img_png.h"
|
||||
#include "common.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
const unsigned char img_png_signature[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
|
||||
|
||||
|
||||
int read_chunk(FILE *fp) {
|
||||
struct img_png_chunk cur_chunk;
|
||||
|
||||
// Read chunk length
|
||||
unsigned char len[4];
|
||||
if(fread(len, sizeof(*len), 4, fp) < 4) {
|
||||
fprintf(stderr, "Couldn\'t read chunk length.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
cur_chunk.length = char_to_uint32(len);
|
||||
printf("Chunk length: %lu\n", (unsigned long) cur_chunk.length);
|
||||
|
||||
// Read chunk type
|
||||
if(fread(cur_chunk.chunk_type, sizeof(*cur_chunk.chunk_type), 4, fp) < 4) {
|
||||
fprintf(stderr, "Couldn\'t read chunk type.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Chunk type: %.*s\n", 4, cur_chunk.chunk_type);
|
||||
|
||||
// Read chunk data
|
||||
size_t chunk_size = cur_chunk.length / sizeof(unsigned char);
|
||||
printf("Chunk size: %zu\n", chunk_size);
|
||||
cur_chunk.chunk_data = malloc(chunk_size);
|
||||
|
||||
if(!cur_chunk.chunk_data) {
|
||||
perror("Failed to allocate chunk data!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(fread(cur_chunk.chunk_data, 1, cur_chunk.length, fp) != cur_chunk.length) {
|
||||
if(feof(fp)) {
|
||||
fprintf(stderr, "File ended prematurely.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf(stderr, "Failed to read chunk data!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return feof(fp);
|
||||
}
|
||||
|
||||
uint8_t* img_png_decode(FILE *fp) {
|
||||
unsigned char magic[8];
|
||||
|
||||
// Check PNG Magic
|
||||
if(fread(magic, sizeof(*magic), 8, fp) < 8) {
|
||||
fprintf(stderr, "Couldn\'t read PNG magic.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(memcmp(magic, img_png_signature, 8) != 0) {
|
||||
fprintf(stderr, "Invalid PNG magic\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("PNG Magic: %#04x%02x%02x%02x%02x%02x%02x%02x\n", magic[0], magic[1], magic[2],
|
||||
magic[3], magic[4], magic[5], magic[6], magic[7]);
|
||||
|
||||
struct img_png_chunk cur_chunk;
|
||||
|
||||
// Read chunk
|
||||
|
||||
while (read_chunk(fp) == 0);
|
||||
|
||||
if(!feof(fp)) {
|
||||
perror("fread() failed!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
12
formats/img_png.h
Normal file
12
formats/img_png.h
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct img_png_chunk {
|
||||
unsigned char* chunk_data;
|
||||
uint32_t length;
|
||||
uint32_t crc;
|
||||
char chunk_type[4];
|
||||
};
|
||||
|
||||
uint8_t* img_png_decode (FILE* fp);
|
Loading…
x
Reference in New Issue
Block a user