Re: GDK-Pixbuf question, creating and reading private data-chunk for png



Thank you for your answer,

with your hints I found two links from which I build the function below which works for me so far:
  http://zarb.org/~gc/html/libpng.html
  http://rawstudio.org/svn/rawstudio/trunk/plugins/output-pngfile/output-pngfile.c


/**
This function hopefully does something like gdk_pixbuf_save but with option to add some private Data

*/
void export_png_file(char* file_name,GdkPixbuf *pixbuf,char * private_data)
{
int i;


int width, height;
png_byte color_type;
png_byte bit_depth;

png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
char * pix;
int rowstride;
FILE * fp;

  fp = fopen(file_name, "wb");

  height = gdk_pixbuf_get_height (pixbuf);
  width = gdk_pixbuf_get_width (pixbuf);

  pix = gdk_pixbuf_get_pixels (pixbuf);
  bit_depth = gdk_pixbuf_get_bits_per_sample (pixbuf);
  color_type = gdk_pixbuf_get_has_alpha (pixbuf) ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB;
  rowstride= gdk_pixbuf_get_rowstride (pixbuf);

  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  info_ptr = png_create_info_struct(png_ptr);

  png_init_io(png_ptr, fp);

  png_set_IHDR(png_ptr, info_ptr, width, height,
               bit_depth, color_type, PNG_INTERLACE_NONE,
               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

  png_write_info(png_ptr, info_ptr);

  row_pointers = g_malloc(sizeof(png_bytep*)*height);

  for( i = 0; i < height; i++)
  {
    row_pointers[i] = (png_bytep)(pix + i * rowstride);
  }

  //if (n_channels == 4) png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); // why?

  png_write_image(png_ptr, row_pointers);

  png_write_end(png_ptr, NULL);

  g_object_unref(pixbuf);

  fclose(fp);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]