-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassPNG.cpp
328 lines (279 loc) · 10.1 KB
/
ClassPNG.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* ClassPNG.cpp
*
*
* Created by Narendra Chaudhary on 2/27/2015.
* Texas A&M University.
*
*/
#ifndef ClassPNG_H_
#include "ClassPNG.h"
#endif
#include <iostream>
#include <fstream>
#include <math.h>
#define log2(x) log((double) x)/log(2.0) // defining log with base 2
ifstream PNGfile; // input stream for reading the file
ofstream PNGfile_out; // output stream for writing the file
void userReadData(png_structp pngPtr, png_bytep data, png_size_t length); //set custom read function using ifstream
void userWriteData(png_structp pngPtr, png_bytep data, png_size_t length); //set custom write function using ofstream
//void my_png_flush(png_structp png_ptr);
// ---------------------------constructor
ClassPNG::ClassPNG()
{
width = 0;
height = 0;
bitdepth = 8;
channels = 1;
row_bytes = 0;
RLE_length = 0;
bitlength = 0;
png_time = 0;
is_read = 1;
pngPtr = NULL;
infoPtr = NULL;
RLE = NULL;
bool_RLE = NULL;
rowPtrs = NULL;
//rowPtr = NULL;
data1 = NULL;
Maparray = NULL;
}
// Destructor
ClassPNG::~ClassPNG()
{
}
void ClassPNG::ReadPNG(string filename)
{
is_read = 1;
#ifdef DEBUG // Debugging code
cout << "Reading the input image ....... " << endl;
cout << "File Name : " << filename << endl;
#endif
data1 = NULL;
png_byte pngsig[PNGSIGSIZE]; // PNG signature
PNGfile.open(filename.c_str(),ios::binary); // opening file using ifstream
if (!PNGfile.good()) // check whether file has opened
{
cout << " error in opening the file"<<endl;
exit(1);
}
PNGfile.read((char*)pngsig, PNGSIGSIZE); // read signature of PNG file
if (!PNGfile.good())
{
cout << " error in reading the file"<<endl;
PNGfile.close();
exit(1);
}
if((png_sig_cmp(pngsig, 0, PNGSIGSIZE))!= 0) // verify whether signature represents png file
{
cout << " Not a PNG file";
PNGfile.close();
exit(1);
}
pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //intialize read structure
if (!pngPtr)
{
cout << "ERROR: Couldn't initialize png read struct" << endl;
}
infoPtr = png_create_info_struct(pngPtr); //initialize info structure
if (!infoPtr)
{
cout << "ERROR: Couldn't initialize png info struct" << endl;
png_destroy_read_struct(&pngPtr, (png_infopp)0, (png_infopp)0);
}
if (setjmp(png_jmpbuf(pngPtr))) // checking for error and jumping (read libpng documentation)
{
//An error occured, so clean up what we have allocated so far...
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
if (rowPtrs != NULL) delete [] rowPtrs;
if (data1 != NULL) delete [] data1;
cout << "ERROR: An error occured while reading the PNG file" <<endl;
//Make sure you return here. libPNG will jump to here if something
//goes wrong, and if you continue with your normal code, you might
//End up with an infinite loop.
PNGfile.close();
exit(1);
}
png_set_read_fn(pngPtr,(png_voidp)&PNGfile, userReadData); // make userReadData as custom read function
//Set the amount signature bytes we've already read:
//We've defined PNGSIGSIZE as 8;
png_set_sig_bytes(pngPtr, PNGSIGSIZE);
// read information about image
png_read_info(pngPtr, infoPtr);
width = png_get_image_width(pngPtr, infoPtr);
cout << " image width : " << width << endl;
height= png_get_image_height(pngPtr, infoPtr);
cout << " image Height : " << height << endl;
bitdepth = png_get_bit_depth(pngPtr, infoPtr);
cout << " bitdepth : " << bitdepth << endl;
channels = png_get_channels(pngPtr, infoPtr);
cout << " channels : " << channels << endl;
row_bytes = png_get_rowbytes(pngPtr,infoPtr);
cout << " No. of bytes in a row: " << row_bytes << endl;
// cout << " Color_type : "<< int(png_get_color_type(pngPtr, infoPtr)) <<endl;
//cout << " filter method : "<< int(png_get_filter_type(pngPtr, infoPtr)) <<endl;
//cout << " compression type : "<< int(png_get_compression_type(pngPtr, infoPtr)) <<endl;
//cout << " interlace type : "<< int(png_get_interlace_type(pngPtr, infoPtr)) <<endl;
}
void userReadData(png_structp pngPtr, png_bytep data, png_size_t length) {
//This is the parameter we passed to the png_set_read_fn() function.
//Our std::ifstream pointer.
png_voidp a = png_get_io_ptr(pngPtr);
//Cast the pointer to std::istream* and read 'length' bytes into 'data'
((fstream*)a)->read((char*)data, length);
}
// read number of rows of image
void ClassPNG::ReadRows(unsigned int row_number,unsigned int nRows)
{
#ifdef DEBUG
cout << " starting of row : " << row_number << endl;
cout << " size of row buffer: " << nRows << endl;
#endif
if (rowPtrs != NULL) delete [] rowPtrs;
if (data1 != NULL) delete [] data1;
rowPtrs = new png_bytep[nRows];
data1 = new char[width * nRows * bitdepth * channels / 8];
int stride = width * bitdepth * channels / 8;
for (size_t i = 0; i < nRows; i++)
{
png_uint_32 q = i * stride;
rowPtrs[i] = (png_bytep)data1 + q; // setting the values of rowPtrs
}
unsigned int start, end;
start = clock();
png_read_rows(pngPtr, rowPtrs,NULL,nRows); // reading rows with rowPtrs as pointers to the start of row
end = clock();
png_time += ((double) (end-start)) / ((double) 1000);
}
// write to PNG file
void ClassPNG::WritePNG(string filename)
{
is_read = 0;
filename.erase(filename.end()-4,filename.end());
filename = filename + "_Decoded.png";
#ifdef DEBUG // Debugging code
cout << "Writing the decoded image ....... " << endl;
cout << "File Name : " << filename << endl;
#endif
PNGfile_out.open(filename.c_str(),ios::binary); //opening file using ofstream
if (!PNGfile_out.good()) // check whether file has opened
{
cout << " error in opening the file"<<endl;
exit(1);
}
pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //intialize write structure
if (!pngPtr)
{
cout << "ERROR: Couldn't initialize png write struct" << endl;
}
infoPtr = png_create_info_struct(pngPtr); //initialize info structure
if (!infoPtr)
{
cout << "ERROR: Couldn't initialize png info struct" << endl;
png_destroy_read_struct(&pngPtr, (png_infopp)0, (png_infopp)0);
}
if (setjmp(png_jmpbuf(pngPtr))) // checking for error and jumping (read libpng documentation)
{
//An error occured, so clean up what we have allocated so far...
png_destroy_write_struct(&pngPtr, (png_infopp)0);
if (rowPtrs != NULL) delete [] rowPtrs;
if (data1 != NULL) delete [] data1;
cout << "ERROR: An error occured while writing the PNG file" <<endl;
//Make sure you return here. libPNG will jump to here if something
//goes wrong, and if you continue with your normal code, you might
//End up with an infinite loop.
PNGfile_out.close();
exit(1);
}
png_set_write_fn(pngPtr,(png_voidp)&PNGfile_out, userWriteData, NULL); // make userWriteData as custom write function
png_set_IHDR(pngPtr, infoPtr, width, height,bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
//png_set_tIME(pngPtr, infoPtr,PNG_INFO_tIME);
png_write_info(pngPtr, infoPtr);
}
void userWriteData(png_structp pngPtr, png_bytep data, png_size_t length) {
//This is the parameter we passed to the png_set__fn() function.
//Our std::ofstream pointer.
png_voidp a_out = png_get_io_ptr(pngPtr);
//Cast the pointer to std::istream* and write 'length' bytes into 'data'
((ofstream*)a_out)->write((char*)data, length);
}
/* This is optional but included to show how png_set_write_fn() is called */
//void my_png_flush(png_structp png_ptr)
//{
//}
// write number of rows of image
void ClassPNG::WriteRows(unsigned int row_number,unsigned int nRows)
{
#ifdef DEBUG
//cout << " starting of row : " << row_number << endl;
//cout << " size of row buffer: " << nRows << endl;
#endif
unsigned int start, end;
start = clock();
png_write_rows(pngPtr, rowPtrs,nRows); // writing rows with rowPtrs as pointers to the start of row
end = clock();
png_time += ((double) (end-start)) / ((double) 1000);
if (rowPtrs != NULL) delete [] rowPtrs;
if (data1 != NULL) delete [] data1;
rowPtrs = new png_bytep[nRows];
data1 = new char[width * nRows * bitdepth / 8];
int stride = width * bitdepth / 8;
for (size_t i = 0; i < nRows; i++)
{
png_uint_32 q = i * stride;
rowPtrs[i] = (png_bytep)data1 + q; // setting the values of rowPtrs
}
}
// write to JBIG file
void ClassPNG::WriteJBIG(string filename)
{
}
//Free memory
void ClassPNG::Free()
{
#ifdef DEBUG // Debugging code
cout << "Closing the file and free buffers " << endl;
#endif
//delete RLE
if (RLE != NULL) delete [] RLE;
#ifdef LINEDIFF
//free(bool_RLE);
#endif
//delete Maparray
if (Maparray != NULL) delete [] Maparray;
//Delete the row pointers array....
if (rowPtrs != NULL) delete[] (png_bytep)rowPtrs;
//if (rowPtr != NULL) delete[] rowPtr;
if (data1 != NULL) delete [] data1;
if (is_read)
{
// clean read and info structures
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
}
else
{
// clean write and info structures
png_destroy_write_struct(&pngPtr,(png_infopp)0);
}
// close stream
PNGfile.close();
PNGfile_out.close();
}
unsigned long long ClassPNG::Compare(ClassPNG cmp)
{
#ifdef DEBUG // Debugging code
cout << "Inside compare function " << endl;
#endif
return 0;
}
//compute entropy of input data
double ClassPNG::Entropy()
{
return 0;
}
//compute entropy of RLE data
double ClassPNG::RLE_Entropy(int nSymbols)
{
return 0;
}