Further refinements on interfaces
This commit is contained in:
parent
c805907817
commit
60fd15440b
@ -1,9 +1,4 @@
|
|||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void free_image(Image* img) {
|
|
||||||
if (img && img->data) {
|
|
||||||
free(img->data);
|
|
||||||
img->data = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
typedef struct image_decoder {
|
typedef struct image_decoder {
|
||||||
const char *name;
|
const char *name;
|
||||||
int (*can_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);
|
image_t* (*decode)(const uint8_t *data, size_t size);
|
||||||
void (*destroy)(struct image_decoder *self);
|
void (*destroy)(struct image_decoder *self);
|
||||||
} image_decoder_t;
|
} image_decoder_t;
|
||||||
|
|
||||||
|
@ -3,14 +3,19 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "img_png.h"
|
#include "img_png.h"
|
||||||
#include "common.h"
|
#include "../common/utils.h"
|
||||||
|
|
||||||
#define BUFSIZE 1024
|
#define BUFSIZE 1024
|
||||||
|
|
||||||
const unsigned char img_png_signature[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
|
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;
|
struct img_png_chunk cur_chunk;
|
||||||
|
|
||||||
// Read chunk length
|
// Read chunk length
|
||||||
@ -49,37 +54,26 @@ int read_chunk(FILE *fp) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return feof(fp);
|
return cur_chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* img_png_decode(FILE *fp) {
|
image_t* png_decode (const uint8_t *data, size_t size) {
|
||||||
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;
|
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;
|
||||||
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "../common/image.h"
|
||||||
|
#include "../common/image_loader.h"
|
||||||
|
|
||||||
struct img_png_chunk {
|
struct img_png_chunk {
|
||||||
unsigned char* chunk_data;
|
unsigned char* chunk_data;
|
||||||
@ -10,7 +12,9 @@ struct img_png_chunk {
|
|||||||
uint32_t crc;
|
uint32_t crc;
|
||||||
char chunk_type[4];
|
char chunk_type[4];
|
||||||
};
|
};
|
||||||
|
image_decoder_t* png_decoder_create (void);
|
||||||
uint8_t* img_png_decode (FILE* fp);
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user