-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPngFile.cpp
More file actions
187 lines (168 loc) · 4.17 KB
/
PngFile.cpp
File metadata and controls
187 lines (168 loc) · 4.17 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
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
// 用途: 读写PNG
// 作者: 何孟宁
#include "PngFile.h"
#include <stdio.h>
#include <iostream>
using namespace std;
void CPngFile::user_error_fn(png_structp png_ptr, png_const_charp error_message)
{
cout << "libpng error: " << error_message << endl;
if (png_ptr)
{
longjmp(png_ptr->jmpbuf, 1);
}
}
void CPngFile::user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
{
cout << "libpng warning: " << warning_message << endl;
}
bool CPngFile::Load(const char *file, png_bytepp dataBuf, unsigned int *img_width,
unsigned int *img_height, int *components)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type;
// 打开文件
if ((fp = fopen(file, "rb")) == NULL)
{
cout << "无法打开 " << file << endl;
return false;
}
// 创建并初始化read_struct
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, user_error_fn, user_warning_fn);
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return false;
}
// 错误处理
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
cout << "无法读取 " << file << endl;
return false;
}
// 开始读
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_info(png_ptr, info_ptr);
// 文件信息
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
int_p_NULL, int_p_NULL, int_p_NULL);
(*img_width) = width;
(*img_height) = height;
if (color_type == PNG_COLOR_TYPE_GRAY)
{
(*components) = 1;
}
else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
(*components) = 2;
}
else if (color_type == PNG_COLOR_TYPE_RGB)
{
(*components) = 3;
}
else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
(*components) = 4;
}
// 转换格式
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
(*components) = 3;
}
else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
(*components) = 4;
}
if (bit_depth == 16)
{
png_set_strip_16(png_ptr);
}
else if (bit_depth < 8)
{
png_set_packing(png_ptr);
}
// 分配内存,需在使用后自行删除
png_uint_32 rowbytes = width * (*components);
(*dataBuf) = new png_byte[rowbytes * height];
// 读取数据
for (png_uint_32 y = 0; y < height; y++)
{
png_bytep row = (*dataBuf) + rowbytes * (height - y - 1);
png_read_rows(png_ptr, &row, png_bytepp_NULL, 1);
}
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return true;
}
bool CPngFile::Save(const char *file, png_bytep dataBuf, unsigned int width, unsigned int height)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
/* open the file */
fp = fopen(file, "wb");
if (fp == NULL)
{
cout << "无法打开 " << file << endl;
return false;
}
// 创建并初始化write_struct
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, user_error_fn, user_warning_fn);
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_write_struct(&png_ptr, png_infopp_NULL);
return false;
}
// 错误处理
if (setjmp(png_jmpbuf(png_ptr)))
{
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
cout << "无法读取 " << file << endl;
return false;
}
// 开始写
png_init_io(png_ptr, fp);
// 文件信息
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// 写入数据
for (png_uint_32 y = 0; y < height; y++)
{
png_bytep row = dataBuf + 3 * width * (height - y - 1);
png_write_rows(png_ptr, &row, 1);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}