-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
66 lines (50 loc) · 1.36 KB
/
image.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include "image.h"
#define PNG_NUM_BYTES 8
char *png_to_fb(const char *filename, unsigned int *width, unsigned int *height)
{
png_structp png;
png_bytepp row_pointers;
png_infop info;
FILE *fp;
char *buf = NULL;
int bit_depth;
int color_type;
unsigned int row_bytes;
fp = fopen(filename, "rb");
if(!fp)
return NULL;
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png)
goto out;
info = png_create_info_struct(png);
if(!info)
goto out;
color_type = png_get_color_type(png, info);
if(color_type == PNG_COLOR_TYPE_RGB
|| color_type == PNG_COLOR_TYPE_GRAY
|| color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
png_init_io(png, fp);
png_read_png(png, info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING
| PNG_TRANSFORM_EXPAND, NULL);
png_get_IHDR(png, info, width, height, &bit_depth, NULL, NULL, NULL,
NULL);
row_bytes = png_get_rowbytes(png, info);
buf = malloc(row_bytes * (*height));
row_pointers = png_get_rows(png, info);
for(unsigned int i = 0; i < *height; i++) {
memcpy(buf + (row_bytes * (*height - 1 - i)), row_pointers[i], row_bytes);
}
png_destroy_read_struct(&png, &info, NULL);
out:
fclose(fp);
return buf;
}
int file_is_png(const char *name)
{
/* TODO: actually do stuff */
return 1;
}