K
kaveee
Ok guys..
here's the code..
Of course I'm getting an error in the function.. imageMalloc_8u_C1..
cuz its not defined.. I'm not able to understand what its supposed to
do.. any help wud be appreciated.
#include<iostream>
#include<math.h>
#include<string>
#include<sstream>
#include <fstream>
#include <png.h>
#include <zlib.h>
using namespace std;
int load_image_gray(const char *fn)
{
png_byte header[8]; // 8 is the maximum size that can be checked
png_structp png_ptr;
png_infop info_ptr;
/* open file and test for it being a png */
FILE *fp = fopen(fn, "rb");
if (!fp)
{
fprintf(stderr,"[read_png_file] File %s could not be opened for
reading", fn);
return 0x0;
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fprintf(stderr,"[read_png_file] File %s is not recognized as a PNG
file", fn);
return 0x0;
}
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL);
if (!png_ptr)
{
fprintf(stderr,"[read_png_file] png_create_read_struct failed");
return 0x0;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf(stderr,"[read_png_file] png_create_info_struct failed");
return 0x0;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
fprintf(stderr,"[read_png_file] Error during init_io");
return 0x0;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int width = info_ptr->width;
int height = info_ptr->height;
if (info_ptr->bit_depth == 16)
png_set_strip_16(png_ptr);
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_rgb_to_gray_fixed(png_ptr, 1, -1, -1);
png_set_interlace_handling(png_ptr);
// this call updates the info_ptr's members based on our calls...
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
{
fprintf(stderr,"[read_png_file] Error during read_image");
return 0x0;
}
int step;
byte* buffer= imageMalloc_8u_C1(width,height,&step);
byte** row_pointers = (byte**) malloc(sizeof(byte) * height);
for (int y=0; y<height; y++)
row_pointers[y] = buffer + y*step;
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr,0x0);
//free(row_pointers); // this call is killing the program
// comment it out for now, but need to
understand
// why the crash is happening
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL,-1);
fclose(fp);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 1)
cout << "Usage: program_name <file_in>";
load_image_gray(argv[0]);
return 0;
}
here's the code..
Of course I'm getting an error in the function.. imageMalloc_8u_C1..
cuz its not defined.. I'm not able to understand what its supposed to
do.. any help wud be appreciated.
#include<iostream>
#include<math.h>
#include<string>
#include<sstream>
#include <fstream>
#include <png.h>
#include <zlib.h>
using namespace std;
int load_image_gray(const char *fn)
{
png_byte header[8]; // 8 is the maximum size that can be checked
png_structp png_ptr;
png_infop info_ptr;
/* open file and test for it being a png */
FILE *fp = fopen(fn, "rb");
if (!fp)
{
fprintf(stderr,"[read_png_file] File %s could not be opened for
reading", fn);
return 0x0;
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fprintf(stderr,"[read_png_file] File %s is not recognized as a PNG
file", fn);
return 0x0;
}
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL);
if (!png_ptr)
{
fprintf(stderr,"[read_png_file] png_create_read_struct failed");
return 0x0;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf(stderr,"[read_png_file] png_create_info_struct failed");
return 0x0;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
fprintf(stderr,"[read_png_file] Error during init_io");
return 0x0;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int width = info_ptr->width;
int height = info_ptr->height;
if (info_ptr->bit_depth == 16)
png_set_strip_16(png_ptr);
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_rgb_to_gray_fixed(png_ptr, 1, -1, -1);
png_set_interlace_handling(png_ptr);
// this call updates the info_ptr's members based on our calls...
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
{
fprintf(stderr,"[read_png_file] Error during read_image");
return 0x0;
}
int step;
byte* buffer= imageMalloc_8u_C1(width,height,&step);
byte** row_pointers = (byte**) malloc(sizeof(byte) * height);
for (int y=0; y<height; y++)
row_pointers[y] = buffer + y*step;
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr,0x0);
//free(row_pointers); // this call is killing the program
// comment it out for now, but need to
understand
// why the crash is happening
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL,-1);
fclose(fp);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 1)
cout << "Usage: program_name <file_in>";
load_image_gray(argv[0]);
return 0;
}