2013-05-26 3 views
0

저는 OpenGL C++ 프로그램에서 libpng이 작동하도록 고심하고 있습니다. png를 텍스처로로드하려고합니다. libpng16 소스 코드를 다운로드하여 Visual Studio 2010을 사용하여 빌드했습니다. lib 파일을 올바르게 링크하고 png.h 파일을 포함 시켰습니다.libpng 오류 : 읽기 오류 (Visual Studio 2010)

프로젝트를 빌드 할 때 libpng이 "libpng error : read error"메시지를 내 콘솔에만 출력합니다. 나는 libpng 프로젝트에서 나의 런타임 구성을 변경하는 것을 포함하여 인터넷에서 찾은 모든 솔루션을 사용해 보았습니다.

오류는 png_read_png 기능에서 발생 : 올바른 파일 이름이 전달되고 내가 시도하는 것이 확실 만든

FILE * file = fopen(filename,"r"); 
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING , NULL ,NULL , NULL); 
if (png_ptr == NULL) 
{ 
printf ("Could not initialize libPNG ’s read struct.\n") ; 
exit(-1); 
} 
png_infop png_info_ptr = png_create_info_struct(png_ptr) ; 
if (png_info_ptr == NULL) 
{ 
printf ("Could not initialize libPNG ’s info pointer.\n"); 
exit (-1) ; 
} 
if (setjmp(png_jmpbuf(png_ptr))) 
{ 

    printf ("LibPNG encountered an error.\n") ; 
png_destroy_read_struct(&png_ptr, &png_info_ptr ,NULL); 
    exit(-1); 
} 

png_init_io (png_ptr , file); 
png_read_png (png_ptr , png_info_ptr , 0 , NULL) ; 
png_uint_32 png_width = 0; 
png_uint_32 png_height = 0; 
int bits = 0; 
int colour_type = 0; 
png_get_IHDR (png_ptr , png_info_ptr , & png_width , & png_height ,& bits , & colour_type ,NULL , NULL , NULL); 
const unsigned BITS_PER_BYTE = 8; 
unsigned bytes_per_colour = (unsigned)bits/ BITS_PER_BYTE ; 
unsigned colours_per_pixel; 

if (colour_type == PNG_COLOR_TYPE_RGB) 
{ 
    colours_per_pixel = 3; 
} 
else 
{ 
printf (" Colour types other than RGB are not supported.") ; 
exit (-1) ; 
} 
printf ("png_width = %d, png_height = %d , bits = %d, colour type = %d. \n" , png_width , png_height , bits , colour_type); 
unsigned char * data = new unsigned char [ png_width * png_height * colours_per_pixel * bytes_per_colour]; 
png_bytepp row_pointers = png_get_rows (png_ptr , png_info_ptr) ; 
unsigned index = 0; 
for (unsigned y = 0; y < png_height ; y ++) 
{ 
    unsigned x = 0; 
    while (x < png_width * colours_per_pixel * bytes_per_colour) { 
data [index++] = row_pointers [y][x++]; 
data [index++] = row_pointers [y][x++]; 
data [index++] = row_pointers [y][x++]; 
    } 
} 

이 가진 모든 도움이 이해할 수있을 것이다

여러 다른 PNG의

감사합니다.

답변

1

Windows의 경우 이미지 파일을 바이너리 모드로, 그렇지 않으면 단일 바이트로 변환 될 것으로 해석 될 수있는 일련의 바이트 시퀀스가 ​​발생합니다. 지금은 텍스트 모드 인 표준 모드에서 파일을 여는 중입니다. 모드 문자열에 'b'를 추가하여 이진 모드로 열 수 있습니다. 즉,

FILE * file = fopen(filename,"rb"); 
+0

sooo 고맙습니다. 나는 그 해결책을 찾을 수 없다고 믿을 수 없다. 날 구해 줬어. –