Compare commits
No commits in common. "60fd15440bd765a0f73fd0ce03652105e9bc8b49" and "332b48c2205b6c6e356c3a32ecca63289ea947fe" have entirely different histories.
60fd15440b
...
332b48c220
@ -1,4 +0,0 @@
|
||||
#include "image.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
@ -1,15 +0,0 @@
|
||||
#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
|
@ -1,14 +0,0 @@
|
||||
#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
|
@ -1,7 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t char_to_uint32(unsigned char *input);
|
||||
|
||||
#endif
|
@ -1,79 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
#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