forked from Aali132/ff7_opengl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png.c
195 lines (149 loc) · 4.22 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* ff7_opengl - Complete OpenGL replacement of the Direct3D renderer used in
* the original ports of Final Fantasy VII and Final Fantasy VIII for the PC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* png.c - load/save routines for PNG images
*/
#include <stdio.h>
#include <libpng12/png.h>
#include <zlib.h>
#include <direct.h>
#include "types.h"
#include "log.h"
#include "globals.h"
#include "gl.h"
void _png_error(png_structp png_ptr, const char *error)
{
error("libpng error: %s\n", error);
}
void _png_warning(png_structp png_ptr, const char *warning)
{
error("libpng warning: %s\n", warning);
}
bool write_png(char *filename, uint width, uint height, char *data)
{
uint y;
png_byte **rowptrs;
FILE *f;
png_infop info_ptr;
png_structp png_ptr;
if(fopen_s(&f, filename, "wb"))
{
error("couldn't open file %s for writing: %s", filename, _strerror(NULL));
return false;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)0, _png_error, _png_warning);
if(!png_ptr)
{
fclose(f);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(f);
return false;
}
if(setjmp(png_ptr->jmpbuf))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
return false;
}
png_init_io(png_ptr, f);
png_set_compression_level(png_ptr, Z_BEST_SPEED);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
rowptrs = driver_malloc(height * 4);
for(y = 0; y < height; y++) rowptrs[y] = &data[y * width * 4];
png_set_rows(png_ptr, info_ptr, rowptrs);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
driver_free(rowptrs);
fclose(f);
return true;
}
uint *read_png(char *filename, uint *_width, uint *_height)
{
png_bytepp rowptrs;
FILE *f;
png_infop info_ptr;
png_structp png_ptr;
uint width;
uint height;
uint *data;
uint y;
uint color_type;
if(fopen_s(&f, filename, "rb")) return 0;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)0, _png_error, _png_warning);
if(!png_ptr)
{
fclose(f);
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(f);
return 0;
}
if(setjmp(png_ptr->jmpbuf))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(f);
return 0;
}
png_init_io(png_ptr, f);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL);
if(png_get_bit_depth(png_ptr, info_ptr) != 8)
{
error("invalid bitdepth\n");
return 0;
}
color_type = png_get_color_type(png_ptr, info_ptr);
if(color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{
error("invalid color type\n");
return 0;
}
rowptrs = png_get_rows(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
*_width = width;
height = png_get_image_height(png_ptr, info_ptr);
*_height = height;
data = gl_get_pixel_buffer(width * height * 4);
if(color_type == PNG_COLOR_TYPE_RGB)
{
uint x;
for(y = 0; y < height; y++)
{
uint o = (uint)rowptrs[y];
for(x = 0; x < width; x++)
{
uint b = (*(unsigned char *)o++);
uint g = (*(unsigned char *)o++);
uint r = (*(unsigned char *)o++);
data[y * width + x] = b | g << 8 | r << 16 | 0xFF << 24;
}
}
}
else for(y = 0; y < height; y++) memcpy(&data[y * width], rowptrs[y], width * 4);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(f);
return data;
}