无法读取libpng一起PNG文件 [英] can not read png file with libpng

查看:166
本文介绍了无法读取libpng一起PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的libpng和文档确实是困惑我。
下面是我的code这是不工作,我不明白为什么。
有人能指出我朝着正确的方向?或建议不同(易)库?

我所理解的libpng:


  1. RB 打开使用的fopen 文件模式


  2. 创建 png_structp png_create_read_struct


  3. 创建 png_infop png_create_info_struct


  4. 分配空间


  5. 读取数据

     的#include<&stdio.h中GT;
    #包括LT&;&png.h GT;INT主(INT ARGC,字符** argv的)
    {
        INT X,Y;
        INT高度,宽度;    png_structp png_ptr;
        png_infop info_ptr;    png_bytep * row_pointers;    FILE *计划生育=的fopen(test.png,RB);
        {
            如果(!FP)
                的printf(文件无法打开阅读);        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);        info_ptr = png_create_info_struct(png_ptr);        png_read_info(png_ptr,info_ptr);
            宽度= png_get_image_width(png_ptr,info_ptr);
            身高= png_get_image_height(png_ptr,info_ptr);        row_pointers =(png_bytep *)malloc的(的sizeof(png_bytep)*高);
            对于(Y = 0; Y<高度; Y ++)
                row_pointers [Y] =(png_byte *)malloc的(png_get_rowbytes(png_ptr,info_ptr));        png_read_image(png_ptr,row_pointers);        FCLOSE(FP);
        }    对于(Y = 0; Y<高度; Y ++)
        {
            png_byte *行= row_pointers [Y];
            为(X = 0; X&下;宽度; X ++)
            {
                png_byte * ptr的=及(行[X * 4]);
                的printf(像素在位置[%d个内容 - %d]的RGBA值数:%d - %d个内容 - %d - %d个\\ N,X,Y,PTR [0],PTR [1],PTR [2] ,PTR [3]);
            }
        }
    }



解决方案

我在同样的 png_create_read_struct 过失败。这是很难调试没有进一步的信息(在我的情况,标准错误是条死胡同),但幸运的是,你可以提供自己的错误和警告功能:

 无效user_error_fn(png_structp png_ptr,png_const_charp ERROR_MSG);
无效user_warning_fn(png_structp png_ptr,png_const_charp warning_msg);

利用这一点,印了libpng有用的错误,指出应用程序是使用不同版本的头不是在运行中发现的的libpng。解开了谜底。

因此​​,虽然个别问题可能会有所不同,使用的libpng的错误报告应该提供洞察力。

I am new to libpng and the documentation is really confusing for me. Below is my code which is not working and I do not see the reason why. Can someone point me to right direction? or suggest different ( "easier" ) library?

how I understand libpng:

  1. open the file with fopen in rb mode

  2. create png_structp with png_create_read_struct

  3. create png_infop with png_create_info_struct

  4. allocate space

  5. read data

    #include <stdio.h>
    #include <png.h>
    
    int main( int argc, char **argv )
    {
        int x, y;
        int height, width;
    
        png_structp png_ptr;
        png_infop info_ptr;
    
        png_bytep *row_pointers;
    
        FILE *fp = fopen( "test.png", "rb");
        {
            if (!fp)
                printf("File could not be opened for reading");
    
            png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    
            info_ptr = png_create_info_struct(png_ptr);
    
            png_read_info(png_ptr, info_ptr);
            width = png_get_image_width(png_ptr, info_ptr);
            height = png_get_image_height(png_ptr, info_ptr);
    
            row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
            for (y=0; y<height; y++)
                row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png_ptr,info_ptr));
    
            png_read_image(png_ptr, row_pointers);
    
            fclose(fp);
        }
    
        for (y=0; y<height; y++) 
        {
            png_byte *row = row_pointers[y];
            for (x=0; x<width; x++) 
            {
                png_byte* ptr = &(row[x*4]);
                printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n", x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
            }
        }
    }
    

解决方案

I similarly had failure in png_create_read_struct. It is difficult to debug without further info (in my case, stderr goes nowhere), but fortunately you can provide your own error and warning functions:

void user_error_fn(png_structp png_ptr, png_const_charp error_msg);
void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg);

Using this, libpng printed helpful errors stating that the application was using a different version of the header than the libpng found at runtime. Mystery solved.

So while individual issues may differ, using libpng's error reporting should provide insight.

这篇关于无法读取libpng一起PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆