diff --git a/common/image.c b/common/image.c index bf2de09..8a7ce68 100644 --- a/common/image.c +++ b/common/image.c @@ -1,9 +1,4 @@ #include "image.h" #include -void free_image(Image* img) { - if (img && img->data) { - free(img->data); - img->data = NULL; - } -} + diff --git a/common/image_loader.h b/common/image_loader.h index 1e3d206..87ff059 100644 --- a/common/image_loader.h +++ b/common/image_loader.h @@ -6,8 +6,8 @@ typedef struct image_decoder { const char *name; - int (*can_decode)(const uint8_t data, size_t size); - image_t* (*decode)(const uint8_t data, size_t size); + int (*can_decode)(const uint8_t *data, size_t size); + image_t* (*decode)(const uint8_t *data, size_t size); void (*destroy)(struct image_decoder *self); } image_decoder_t; diff --git a/formats/img_png.c b/formats/img_png.c index 5b616fc..424ee8a 100644 --- a/formats/img_png.c +++ b/formats/img_png.c @@ -3,14 +3,19 @@ #include #include #include "img_png.h" -#include "common.h" +#include "../common/utils.h" #define BUFSIZE 1024 const unsigned char img_png_signature[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}; +// Reads magic number and determines if data is PNG +int png_can_decode(const uint8_t *data, size_t size) { + if (size < 8) return 0; + return memcmp(data, img_png_signature, 8) == 0; +} -int read_chunk(FILE *fp) { +struct img_png_chunk read_chunk(FILE *fp) { struct img_png_chunk cur_chunk; // Read chunk length @@ -49,37 +54,26 @@ int read_chunk(FILE *fp) { exit(EXIT_FAILURE); } - return feof(fp); + return cur_chunk; } -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); - } - - +image_t* png_decode (const uint8_t *data, size_t size) { return NULL; } + +void png_destroy(struct image_decoder *self) { + if (self) { + free(self); + self = NULL; + } else self = NULL; +} + +image_decoder_t* png_decoder_create () { + image_decoder_t* decoder = malloc(sizeof(image_decoder_t)); + decoder->name = "PNG"; + decoder->can_decode = png_can_decode; + decoder->decode = png_decode; + decoder->destroy = png_destroy; + + return decoder; +} diff --git a/formats/img_png.h b/formats/img_png.h index 8dbcb26..8ab3e31 100644 --- a/formats/img_png.h +++ b/formats/img_png.h @@ -3,6 +3,8 @@ #include #include +#include "../common/image.h" +#include "../common/image_loader.h" struct img_png_chunk { unsigned char* chunk_data; @@ -10,7 +12,9 @@ struct img_png_chunk { uint32_t crc; char chunk_type[4]; }; - -uint8_t* img_png_decode (FILE* fp); +image_decoder_t* png_decoder_create (void); +int png_can_decode (const uint8_t *data, size_t size); +image_t* png_decode (const uint8_t *data, size_t size); +void png_destroy (struct image_decoder *self); #endif