Red Squirrel
No Lifer
I'm writing an easy to use wrapper class for libpng and I'm having trouble with the writing part. There's very little resources online that talk about how to actually write, as most examples just show a png file being read then being written, so it never goes into the details of writing 1 pixel at a time. This is what I got so far as a write command but this does not compile, the trouble is near the end with the actual write loop:
What am I doing wrong and how do I get it to work? Those Get functions return an unsigned char from 0 to 255.
Here is the full function:
This is the compile error that I get:
Edit: Oh and just realized a logic error in how I'm assigning pixels but that's not the problem right now. It would still "work" just not write the proper data. I fixed that in my code but still need to get this to compile to test it.
Code:
// Write image data
int x, y;
for (y=0 ; y<m_height ; y++)
{
for (x=0 ; x<m_width ; x++)
{
pngpixel * pixel = GetPixel(x,y);
row[x]=(png_byte)pixel->GetRed();
row[x+1]=(png_byte)pixel->GetGreen();
row[x+2]=(png_byte)pixel->GetBlue();
row[x+3]=(png_byte)pixel->GetAlpha();
}
png_write_row(png_ptr, row);
}
What am I doing wrong and how do I get it to work? Those Get functions return an unsigned char from 0 to 255.
Here is the full function:
Code:
bool rspng::WriteToFile(string pngfile="")
{
if(pngfile=="")pngfile=m_filename;
Debug(1,"Writing to " + pngfile);
//init variables/structs:
int code = 0;
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row=NULL;
Debug(3,"Initializing...");
// Open file for writing: (binary mode)
fp = fopen(pngfile.c_str(), "wb");
if (fp == NULL)
{
Debug(1, "Could not open file " + pngfile + " for writing");
return false;
}
//initialize write structure:
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
Debug(1,"Could not allocate write struct");
if (fp != NULL)fclose(fp);
return false;
}
// Initialize info structure:
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
Debug(1,"Could not allocate info struct");
if (fp != NULL) fclose(fp);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return false;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr)))
{
Debug(0, "Error during png creation");
if (fp != NULL) fclose(fp);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
return false;
}
png_init_io(png_ptr, fp);
// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, m_width, m_height,8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// Set title
//png_text title_text;
//title_text.compression = PNG_TEXT_COMPRESSION_NONE;
//title_text.key = "rspng"; //TODO: figure out how to use m_title.c_str();
//title_text.text = "generated by rspng"; //TODO: figure out how to m_desc.c_str();
//png_set_text(png_ptr, info_ptr, &title_text, 1);
png_write_info(png_ptr, info_ptr);
Debug(1,"Writing png data");
// Allocate memory for one row (4 bytes per pixel - RGBA)
//row = (png_bytep) malloc(4 * m_width * sizeof(png_byte));
row = new png_bytep[4 * m_width * sizeof(png_byte)];
// Write image data
int x, y;
for (y=0 ; y<m_height ; y++)
{
for (x=0 ; x<m_width ; x++)
{
pngpixel * pixel = GetPixel(x,y);
row[x]=(png_byte)pixel->GetRed(); //line 474
row[x+1]=(png_byte)pixel->GetGreen();
row[x+2]=(png_byte)pixel->GetBlue();
row[x+3]=(png_byte)pixel->GetAlpha();
}
png_write_row(png_ptr, row); //line 479
}
// End write
png_write_end(png_ptr, NULL);
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (row != NULL) delete[] row;
Debug(1,"Done writing to " + pngfile + ".");
}
This is the compile error that I get:
Code:
../../rslib/include/rspng/rspng.cpp: In member function ‘bool rslib::rspng::WriteToFile(std::string)’:
../../rslib/include/rspng/rspng.cpp:474: error: invalid conversion from ‘unsigned char’ to ‘png_byte*’
../../rslib/include/rspng/rspng.cpp:475: error: invalid conversion from ‘unsigned char’ to ‘png_byte*’
../../rslib/include/rspng/rspng.cpp:476: error: invalid conversion from ‘unsigned char’ to ‘png_byte*’
../../rslib/include/rspng/rspng.cpp:477: error: invalid conversion from ‘unsigned char’ to ‘png_byte*’
../../rslib/include/rspng/rspng.cpp:479: error: cannot convert ‘png_byte**’ to ‘png_byte*’ for argument ‘2’ to ‘void png_write_row(png_struct*, png_byte*)’
Edit: Oh and just realized a logic error in how I'm assigning pixels but that's not the problem right now. It would still "work" just not write the proper data. I fixed that in my code but still need to get this to compile to test it.
Last edited: