-
-
-
-
-
-
-/*
- png_set_read_fn(png_structp read_ptr, voidp read_io_ptr, png_rw_ptr read_data_fn)
- png_set_write_fn(png_structp write_ptr, voidp write_io_ptr, png_rw_ptr write_data_fn,
- png_flush_ptr output_flush_fn);
- voidp read_io_ptr = png_get_io_ptr(read_ptr);
- voidp write_io_ptr = png_get_io_ptr(write_ptr);
-*/
-
-
-
-
static void
wiol_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
io_glue *ig = (io_glue *)png_ptr->io_ptr;
}
-
-undef_int
-i_writepng(i_img *im, int fd) {
- FILE *fp;
- png_structp png_ptr;
- png_infop info_ptr;
- int width,height,y;
- volatile int cspace,channels;
- double xres, yres;
- int aspect_only, have_res;
- double offx, offy;
- char offunit[20] = "pixel";
-
- mm_log((1,"i_writepng(0x%x,fd %d)\n",im,fd));
-
- if ((fp = fdopen(fd,"w")) == NULL) {
- mm_log((1,"can't fdopen.\n"));
- exit(1);
- }
-
- height=im->ysize;
- width=im->xsize;
-
- channels=im->channels;
-
- if (channels>2) { cspace=PNG_COLOR_TYPE_RGB; channels-=3; }
- else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
-
- if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
- mm_log((1,"cspace=%d\n",cspace));
-
- channels=im->channels;
-
- /* Create and initialize the png_struct with the desired error handler
- * functions. If you want to use the default stderr and longjump method,
- * you can supply NULL for the last three parameters. We also check that
- * the library version is compatible with the one used at compile time,
- * in case we are using dynamically linked libraries. REQUIRED.
- */
-
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
-
- if (png_ptr == NULL) {
- fclose(fp);
- return 0;
- }
-
- /* Allocate/initialize the image information data. REQUIRED */
- info_ptr = png_create_info_struct(png_ptr);
-
- if (info_ptr == NULL) {
- fclose(fp);
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- return(0);
- }
-
- /* Set error handling. REQUIRED if you aren't supplying your own
- * error hadnling functions in the png_create_write_struct() call.
- */
- if (setjmp(png_ptr->jmpbuf)) {
- /* If we get here, we had a problem reading the file */
- fclose(fp);
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- return(0);
- }
-
- png_init_io(png_ptr, fp);
-
- /* Set the image information here. Width and height are up to 2^31,
- * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
- * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
- * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
- * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
- * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
- * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
- */
-
- png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
- PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
- have_res = 1;
- if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) {
- if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
- ; /* nothing to do */
- else
- yres = xres;
- }
- else {
- if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
- xres = yres;
- else
- have_res = 0;
- }
- if (have_res) {
- aspect_only = 0;
- i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only);
- xres /= 0.0254;
- yres /= 0.0254;
- png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5,
- aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER);
- }
-
- png_write_info(png_ptr, info_ptr);
- if (!im->virtual && im->type == i_direct_type && im->bits == i_8_bits) {
- for (y = 0; y < height; y++)
- png_write_row(png_ptr, (png_bytep) &(im->idata[channels*width*y]));
- }
- else {
- unsigned char *data = mymalloc(im->xsize * im->channels);
- if (data) {
- for (y = 0; y < height; y++) {
- i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
- png_write_row(png_ptr, (png_bytep)data);
- }
- myfree(data);
- }
- else {
- fclose(fp);
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- return 0;
- }
- }
- png_write_end(png_ptr, info_ptr);
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
-
- fclose(fp);
- return(1);
-}
-
-
-
undef_int
i_writepng_wiol(i_img *im, io_glue *ig) {
FILE *fp;