forked from godspeed1989/fbv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png.c
167 lines (147 loc) · 3.98 KB
/
png.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "config.h"
#ifdef FBV_SUPPORT_PNG
#include "fbv.h"
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PNG_BYTES_TO_CHECK 4
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
int fh_png_id(char *name)
{
int fd;
char id[4];
fd = open(name, O_RDONLY);
if(fd==-1) return(0);
read(fd, id, 4);
close(fd);
if(id[1]=='P' && id[2]=='N' && id[3]=='G') return(1);
return(0);
}
int fh_png_load(char *name, unsigned char *buffer, unsigned char ** alpha,int x,int y)
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
png_uint_32 i;
int bit_depth, color_type, interlace_type;
int number_passes,pass, trans = 0;
png_bytep rptr[2];
unsigned char *rp;
unsigned char *fbptr;
FILE *fh;
if(!(fh = fopen(name,"rb"))) return(FH_ERROR_FILE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr == NULL) return(FH_ERROR_FORMAT);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
rp = 0;
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
if(rp) free(rp);
fclose(fh);
return(FH_ERROR_FORMAT);
}
png_init_io(png_ptr,fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
if (bit_depth < 8) png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type== PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
trans = 1;
png_set_tRNS_to_alpha(png_ptr);
}
if(bit_depth == 16) png_set_strip_16(png_ptr);
number_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA || color_type == PNG_COLOR_TYPE_RGB_ALPHA || trans)
{
unsigned char * alpha_buffer = (unsigned char*) malloc(width * height);
unsigned char * aptr;
rp = (unsigned char*)malloc(width * 4);
rptr[0] = (png_bytep) rp;
*alpha = alpha_buffer;
for (pass = 0; pass < number_passes; pass++)
{
fbptr = buffer;
aptr = alpha_buffer;
for(i=0; i<height; i++)
{
png_uint_32 n;
unsigned char *trp = rp;
png_read_rows(png_ptr, rptr, NULL, 1);
for(n = 0; n < width; n++, fbptr += 3, trp += 4)
{
memcpy(fbptr, trp, 3);
*(aptr++) = trp[3];
}
}
}
free(rp);
}
else
{
for (pass = 0; pass < number_passes; pass++)
{
fbptr = buffer;
for(i=0; i<height; i++, fbptr += width*3)
{
rptr[0] = (png_bytep) fbptr;
png_read_rows(png_ptr, rptr, NULL, 1);
}
}
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_OK);
}
int fh_png_getsize(char *name, int *x, int *y)
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
char *rp;
FILE *fh;
if(!(fh=fopen(name,"rb"))) return(FH_ERROR_FILE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) return(FH_ERROR_FORMAT);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
rp = 0;
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
if(rp) free(rp);
fclose(fh);
return(FH_ERROR_FORMAT);
}
png_init_io(png_ptr,fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
*x = width;
*y = height;
fclose(fh);
return(FH_ERROR_OK);
}
#endif /*FBV_SUPPORT_PNG*/