-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_write.c
35 lines (28 loc) · 981 Bytes
/
read_write.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "read_write.h"
#include "lodepng.h"
struct image * read_image(const char * const file) {
unsigned error;
FILE * src;
if(NULL == (src = fopen(file, "rb"))) {
printf("unable to open source file: %s\n", file);
}
image_t * img = (image_t*)malloc(sizeof(image_t));
img->height = 0;
img->width = 0;
img->channels = 4;
error = lodepng_decode32_file(&(img->buffer), &(img->width), &(img->height), file);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
return img;
}
void write_image(image_t * img, const char * const file) {
FILE * dest;
if(NULL == (dest = fopen(file, "wb"))) {
printf("unable to open destination file: %s\n", file);
}
unsigned error = lodepng_encode32_file(file, img->buffer, img->width, img->height);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
}