-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage_to_c_struct.c
More file actions
148 lines (104 loc) · 3.53 KB
/
image_to_c_struct.c
File metadata and controls
148 lines (104 loc) · 3.53 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <png.h>
int png_to_image(FILE* f, unsigned int** out_image,
unsigned int* width,
unsigned int* height) {
int i,j;
png_byte color_type, depth;
png_bytep *row = NULL;
png_bytep png_pixel;
png_infop info;
png_structp structure = NULL;
unsigned int* pixel_i;
unsigned char* pixel_c;
unsigned int * image;
structure = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if(!structure) return(1);
info = png_create_info_struct(structure);
if(!info) return(2);
png_init_io(structure, f);
png_read_info(structure, info);
width[0] = png_get_image_width(structure, info);
height[0] = png_get_image_height(structure, info);
color_type = png_get_color_type(structure, info);
depth = png_get_bit_depth(structure, info);
if (depth == 16) {
png_set_strip_16(structure);
}
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(structure);
}
if (color_type == PNG_COLOR_TYPE_GRAY && depth < 8) {
png_set_expand_gray_1_2_4_to_8(structure);
}
if (png_get_valid(structure, info, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(structure);
}
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_filler(structure, 0xFF, PNG_FILLER_AFTER);
}
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(structure);
}
png_read_update_info(structure,info);
row = (png_bytep*)malloc(sizeof(png_bytep)*height[0]);
if(row == NULL) return 3;
for(i = 0; i < height[0]; i++) {
row[i] = (png_byte*)malloc(png_get_rowbytes(structure,info));
if(row[i] == 0) return 3;
}
image = (unsigned int*)malloc(sizeof(unsigned int)*width[0]*height[0]);
if(image == NULL) return 3;
png_read_image(structure,row);
for(j = 0; j < height[0]; j++) {
for(i = 0; i < width[0]; i++) {
png_pixel = (row[j]+(i*4));
pixel_i = image+j*width[0]+i;
pixel_c = (char*)pixel_i;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
pixel_c[0] = png_pixel[2];
pixel_c[1] = png_pixel[1];
pixel_c[2] = png_pixel[0];
pixel_c[3] = png_pixel[3];
#endif
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
pixel_c[3] = png_pixel[2];
pixel_c[2] = png_pixel[1];
pixel_c[1] = png_pixel[0];
pixel_c[0] = png_pixel[3];
#endif
}
}
out_image[0] = image;
for(i = 0; i < height[0]; i++) {
free(row[i]);
}
free(row);
return(0);
}
int main(int argc, char** argv) {
unsigned int* image;
unsigned int width, height;
int i;
FILE* f = fopen(argv[1],"rb");
png_to_image(f, &image, &width, &height);
printf("/* \n");
printf(" * Image of height = %u \n", width);
printf(" * Image of width = %u \n", height);
printf(" */\n");
printf("unsigned int sidebar[%i] = { \n", width*height);
for(i=0;i<width*height-1;i++) {
printf("%u,", image[i]);
if(i % 10 == 0) printf( "\n");
}
printf("%u };", image[width*height-1]);
}