-
Notifications
You must be signed in to change notification settings - Fork 1
/
PngWrapper.cpp
309 lines (285 loc) · 8.91 KB
/
PngWrapper.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include "stdafx.h"
#include <assert.h>
#include "png.h"
#include "PngWrapper.h"
//local defs
#ifndef png_jmpbuf
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
//check that the file is png
#define PNG_BYTES_TO_CHECK 4
bool PngWrapper::IsPngFile()
{
char buf[PNG_BYTES_TO_CHECK];
/* Read in some of the signature bytes */
if (fread(buf, 1, PNG_BYTES_TO_CHECK, m_fp) != PNG_BYTES_TO_CHECK)
return false;
/* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Return nonzero (true) if they match */
return (!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
}
//ctor
// you give the name of the file and if it is for
//writing new files you may set here the
// file size in pixels
PngWrapper::PngWrapper(const char * name,int width,int height):
m_width(width),
m_height(height),
m_png_ptr(NULL),
m_info_ptr(NULL),
m_fileName(NULL),
m_fp(NULL)
{
SetFileName(name);
}
//this is usfull ctor for
//creating an array of files for animation purposes
PngWrapper::PngWrapper():
m_width(0),
m_height(0),
m_png_ptr(NULL),
m_info_ptr(NULL),
m_fileName(NULL),
m_fp(NULL)
{
}
//dtor - will close the file ahandle
//and release allocated memory
PngWrapper::~PngWrapper()
{
// clean up after the read, and free any memory allocated
if(m_png_ptr && m_info_ptr)
png_destroy_read_struct(&m_png_ptr, &m_info_ptr, png_infopp_NULL);
if(m_fileName)
delete [] m_fileName;
if(m_fp)
fclose(m_fp);
}
void PngWrapper::SetFileName(const char * name)
{
if(name == NULL)
assert(0);
int l = strlen(name);
if(m_fileName)
delete [] m_fileName;
m_fileName = new char[strlen(name)+1];
strcpy(m_fileName,name);
}
/*
Read the png file which name is defined in
*/
bool PngWrapper::ReadPng()
{
// unsigned int sig_read = 0;
// png_uint_32 width, height;
// int bit_depth, color_type, interlace_type;
/* Open the file PNG file. */
if ((m_fp = fopen(m_fileName, "rb")) == NULL)
return false;
/*is png file ?*/
if(IsPngFile() == false){
fclose(m_fp);
m_fp=NULL;
return false;
}
//create the read struct
m_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (m_png_ptr == NULL)
{
fclose(m_fp);
m_fp=NULL;
return false;
}
// Allocate/initialize the memory for image information.
m_info_ptr = png_create_info_struct(m_png_ptr);
if (m_info_ptr == NULL)
{
png_destroy_read_struct(&m_png_ptr, png_infopp_NULL, png_infopp_NULL);
fclose(m_fp);
m_fp=NULL;
return false;
}
// this will set the jamp location in case of an error
if (setjmp(png_jmpbuf(m_png_ptr)))
{
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_read_struct(&m_png_ptr, &m_info_ptr, png_infopp_NULL);
fclose(m_fp);
m_fp=NULL;
/* If we get here, we had a problem reading the file */
return false;
}
//io init - we will read the whole image to the memory
png_init_io(m_png_ptr, m_fp);
//we read the sig bytes and we want to skip the rest
png_set_sig_bytes(m_png_ptr, PNG_BYTES_TO_CHECK);
//read to the image
png_read_png(m_png_ptr, m_info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
//make sure that we have 24 bpp color image or an 8 bpp gray image
// update width
m_width = m_info_ptr->width;
m_height = m_info_ptr->height;
//close the file
fclose(m_fp);
m_fp=NULL;
return true;
}
void PngWrapper::SetWidth(int width)
{
m_width = width;
}
void PngWrapper::SetHeight(int height)
{
m_height = height;
}
int PngWrapper::GetWidth()
{
return m_width;
}
int PngWrapper::GetHeight()
{
return m_height;
}
// these access function will assert if you try to
// set value to non existant png
// or outside its domain
void PngWrapper::SetValue(unsigned int x,unsigned int y,unsigned int value)
{
if(y >= m_info_ptr->height ||
x >= m_info_ptr->width){
std::cout<<"bad offset into file "<<__FILE__<<" "<<__LINE__<<std::endl;
return;
}
if (m_info_ptr->channels == 1)
m_info_ptr->row_pointers[y][m_info_ptr->channels*x] = value & 0x000000ff;
else if(m_info_ptr->channels == 3){
m_info_ptr->row_pointers[y][m_info_ptr->channels*x] = GetBValue(value),
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+1] = GetGValue(value),
m_info_ptr->row_pointers[y][m_info_ptr->channels*x + 2] = GetRValue(value);
}
else if(m_info_ptr->channels == 4){
m_info_ptr->row_pointers[y][m_info_ptr->channels*x] = GET_R(value),
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+1] = GET_G(value),
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+2] = GET_B(value),
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+2] = GET_A(value);
}
}
// the function will assert if a png was not read
int PngWrapper::GetValue(unsigned int x,unsigned int y)
{
if( y >= m_info_ptr->height ||
x >= m_info_ptr->width){
std::cout<<"bad offset into file "<<__FILE__<<" "<<__LINE__<<std::endl;
return 0;
}
if (m_info_ptr->channels == 1)
return m_info_ptr->row_pointers[y][m_info_ptr->channels*x];
else if(m_info_ptr->channels == 3)
return SET_RGB(m_info_ptr->row_pointers[y][m_info_ptr->channels*x],
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+1],
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+2]);
else if(m_info_ptr->channels == 4)
return SET_RGBA(m_info_ptr->row_pointers[y][m_info_ptr->channels*x],
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+1],
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+2],
m_info_ptr->row_pointers[y][m_info_ptr->channels*x+3]);
//should not reach here - it means that the iamge
//format is not supported by the wrapper
assert(0);
return 0;
}
bool PngWrapper::InitWritePng()
{
if(m_width <= 0 || m_height <= 0)
return false;
if(m_fileName == NULL)
return false;
if(m_fp != NULL)
return false;
//we have unclosed streams - no online reuse is allowed for simplicity
if(m_png_ptr || m_png_ptr)
return false;
//init the file
/* open the file */
m_fp = fopen(m_fileName, "wb");
if (m_fp == NULL)
return false;
//init the png ptr struct
m_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
if (m_png_ptr == NULL)
{
fclose(m_fp);
m_fp = NULL;
return false;
}
//init the info ptr
m_info_ptr = png_create_info_struct(m_png_ptr);
if (m_info_ptr == NULL)
{
fclose(m_fp);
m_fp = NULL;
png_destroy_write_struct(&m_png_ptr, png_infopp_NULL);
return false;
}
//
if (setjmp(png_jmpbuf(m_png_ptr)))
{
/* If we get here, we had a problem reading the file */
fclose(m_fp);
m_fp = NULL;
png_destroy_write_struct(&m_png_ptr, &m_info_ptr);
return false;
}
// set application data for the file
//m_png_ptr setting
png_set_IHDR(m_png_ptr, m_info_ptr, m_width, m_height,
8/*bits per channel*/,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
//allocate memory
png_voidp ppp = png_malloc(m_png_ptr,
m_height*sizeof(png_bytep));
png_bytepp row_pointers = (png_bytepp)ppp;
for (int i=0; i<m_height; i++)
row_pointers[i]=(png_bytep)png_malloc(m_png_ptr,
m_width*(m_info_ptr->pixel_depth/m_info_ptr->bit_depth));
png_set_rows(m_png_ptr, m_info_ptr, row_pointers);
//init io - bind the struct to the stream
png_init_io(m_png_ptr, m_fp);
return true;
}
//close the file and write the result - the app will write the
//data using the SetValue interface
bool PngWrapper::WritePng()
{
if(!m_png_ptr || !m_info_ptr)
return false;
if (setjmp(png_jmpbuf(m_png_ptr)))
{
/* If we get here, we had a problem reading the file */
fclose(m_fp);
m_fp = NULL;
png_destroy_write_struct(&m_png_ptr, &m_info_ptr);
return false;
}
png_write_png(m_png_ptr, m_info_ptr, 0, png_voidp_NULL);
png_destroy_write_struct(&m_png_ptr, &m_info_ptr);
fclose(m_fp);
m_fp = NULL;
return true;
}
int PngWrapper::GetNumChannels()
{
if(!m_info_ptr)
return 0;
return m_info_ptr->channels;
}
int PngWrapper::GetBPP()
{
if(!m_info_ptr)
return 0;
return m_info_ptr->pixel_depth;
}