1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem to save image with libpng

Discussion in 'C' started by c_beginner, Feb 4, 2015.

  1. c_beginner

    c_beginner New Member

    Joined:
    Feb 4, 2015
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    Do you have experience with libpng? I need to find out why my program can save image after rgb2hsv conversion but can not to save image after load of the just saved converted image. Once again. I run my program and read_png_file(argv[1], &data) will convert rgb to hsv and save it as normal 3 channel png image. Then I run the command again and load the new image by read_png_file(argv[1], &data) . It converts the data and I can view the data->raw_pointers buffer. I can view it also when I call write_png_file(argv[2], &data) but this time, the just changed data are not saved but instead of it the original hsv image is saved.

    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> // memmove
    
    
    #include <unistd.h>
    #include <stdarg.h>
    
    #include <png.h>
    #include <data_types.h>
    #include <zlib.h> // this is needed only for png_set_compression_level
    #include <png_conversions.h>
    
    void abort_(const char * s, ...)
    {
            va_list args;
            va_start(args, s);
            vfprintf(stderr, s, args);
            fprintf(stderr, "\n");
            va_end(args);
            abort();
    }
    
    int x, y;
    
    png_structp png_ptr;
    png_infop info_ptr;
    int number_of_passes;
    
    
    void read_png_file(char* file_name, pDATA data)
    {
            unsigned char header[8];    // 8 is the maximum size that can be checked
    
            FILE *fp = fopen(file_name, "rb");
            if (!fp)
                    abort_("[read_png_file] File %s could not be opened for reading", file_name);
            fread(header, 1, 8, fp);
            if (png_sig_cmp(header, 0, 8))
                {
                    fclose(fp);
                    abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
                }
    
            png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
            if (!png_ptr)
                {
                    fclose(fp);
                    png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                    abort_("[read_png_file] png_create_read_struct failed");
                }
    
            info_ptr = png_create_info_struct(png_ptr);
            if (!info_ptr)
                    {
                    fclose(fp);
                    png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                    abort_("[read_png_file] png_create_info_struct failed");
                    }
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    {
                    fclose(fp);
                    png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
                    abort_("[read_png_file] Error during init_io");
                    }
    
            png_init_io(png_ptr, fp);
            png_set_sig_bytes(png_ptr, 8);
            png_read_info(png_ptr, info_ptr);
    
            data->width = png_get_image_width(png_ptr, info_ptr);
            data->height = png_get_image_height(png_ptr, info_ptr);
            data->channels = png_get_channels(png_ptr, info_ptr);
            data->color_type = png_get_color_type(png_ptr, info_ptr);
            data->bit_depth = png_get_bit_depth(png_ptr, info_ptr);
            data->row_bytes_len = png_get_rowbytes(png_ptr, info_ptr);
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    abort_("[read_png_file] Error during read_image");
    
            data->row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * data->height);
            for (y=0; y<data->height; y++)
                    data->row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
            png_read_image(png_ptr, data->row_pointers);
            // png_rgb2hsv(data);
            png_hsv2rgb(data);
            fclose(fp);
    }
    
    
    void write_png_file(char* file_name, pDATA data)
    {
            FILE *fp = fopen(file_name, "wb");
            if (!fp)
                    abort_("[write_png_file] File %s could not be opened for writing", file_name);
    
            png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    
            if (!png_ptr)
                    abort_("[write_png_file] png_create_write_struct failed");
    
            info_ptr = png_create_info_struct(png_ptr);
            if (!info_ptr)
                    abort_("[write_png_file] png_create_info_struct failed");
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    abort_("[write_png_file] Error during init_io");
    
            png_init_io(png_ptr, fp);
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    abort_("[write_png_file] Error during writing header");
    
            png_set_compression_level(png_ptr, 9); // BEST SPEED
            png_set_IHDR(png_ptr, info_ptr, data->width, data->height,
                         data->bit_depth, data->color_type, PNG_INTERLACE_NONE,
                         PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    
            png_write_info(png_ptr, info_ptr);
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    abort_("[write_png_file] Error during writing bytes");
    
            png_write_image(png_ptr, data->row_pointers);
    
            if (setjmp(png_jmpbuf(png_ptr)))
                    abort_("[write_png_file] Error during end of write");
    
            png_write_end(png_ptr, NULL);
    
            /* cleanup heap allocation */
            for (y=0; y<data->height; y++)
                    free(data->row_pointers[y]);
            free(data->row_pointers);
    
            fclose(fp);
    }
    
    int main(int argc, char **argv)
    {
        DATA data;
    
        if (argc < 3)
                abort_("Usage: program_name <file_in> <file_out>");
    
        read_png_file(argv[1], &data);
        write_png_file(argv[2], &data);
    }
    
    do you have some explanation for this behavior and how to fix it?
     
    shabbir likes this.

Share This Page