Processing PNG files... again..

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;
}
 
P

Puppet_Sock

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;
[snip]

Insert the usual remarks about this.

Get used to prepending std:: to standard stuff.
byte* buffer= imageMalloc_8u_C1(width,height,&step);
[snip]

That sure looks like it ought to be allocating some
memory for you. Note that I didn't check if you are
then deallocating it someplace.

Since that's the only place in your code the name
appears, and it's not standard, you need to "round
up the usual suspects."

- Did you spell the function name correctly? Don't
forget upper/lower case have to match, and the
numeral 1 looks a lot like the letter l.
- Did you give it the right list of args?
- Did you include the right header file and library?
- Is it in a namespace?
Socks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top