Compare commits
4 Commits
332b48c220
...
60fd15440b
Author | SHA1 | Date | |
---|---|---|---|
60fd15440b | |||
c805907817 | |||
257cd65412 | |||
3823279856 |
4
common/image.c
Normal file
4
common/image.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include "image.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
15
common/image.h
Normal file
15
common/image.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef IMAGE_H
|
||||
#define IMAGE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
int width;
|
||||
int height;
|
||||
int channels;
|
||||
uint8_t* data;
|
||||
} image_t;
|
||||
|
||||
void free_image(image_t* img);
|
||||
|
||||
#endif
|
14
common/image_loader.h
Normal file
14
common/image_loader.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef IMAGE_LOADER_H
|
||||
#define IMAGE_LOADER_H
|
||||
|
||||
#include "image.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
void (*destroy)(struct image_decoder *self);
|
||||
} image_decoder_t;
|
||||
|
||||
#endif
|
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;
|
||||
}
|
8
common/utils.h
Normal file
8
common/utils.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t char_to_uint32(unsigned char *input);
|
||||
|
||||
#endif
|
79
formats/img_png.c
Normal file
79
formats/img_png.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "img_png.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;
|
||||
}
|
||||
|
||||
struct img_png_chunk 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 cur_chunk;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
20
formats/img_png.h
Normal file
20
formats/img_png.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef IMG_PNG_H
|
||||
#define IMG_PNG_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "../common/image.h"
|
||||
#include "../common/image_loader.h"
|
||||
|
||||
struct img_png_chunk {
|
||||
unsigned char* chunk_data;
|
||||
uint32_t length;
|
||||
uint32_t crc;
|
||||
char chunk_type[4];
|
||||
};
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user