2012-05-13 2 views
0

현재 독자적인 PNG 판독기를 작성 중이며 개별 청크를 읽었으며 처음 두 청크를 올바르게 읽은 것 같습니다. 그러나 IDAT 청크에 대해서는 어리석은 크기가 떠오른다. 이PNG 청크 판독기, 길이가 유효하지 않습니다.

유형과 제 2 개 청크 판독 디버거를 사용

bool LoadImage(char * path, Image * target) 
{ 
std::ifstream file; 

file.open("Lenna.png", std::ios::in | std::ios::binary); 
if(!file.is_open()) 
    return -false; 

std::cout << "Opened file : Lenna.png" << std::endl; 
struct stat filestatus; 
if (stat("Lenna.png", &filestatus) != 0) 
    return false; 

unsigned int fileSize = filestatus.st_size; 
unsigned int bytesRead = 8; 
file.seekg(8, std::ios::beg); 

while(bytesRead < fileSize) 
{ 
    //Read the length, type, then the data and at last the crc(Cyclic redundancy crap, whatever that may be) 
    char length [4]; 
    file.read(length, 4); 
    //Reverse the 4 bytes due to network type writing. 
    unsigned int dataLength = (length[0] << 24) | (length[1] << 16) | (length[2] << 8) | length[3]; 
    char type [4]; 
    file.read(type, 4); 
    char * data = new char[dataLength]; 
    file.read(data, dataLength); 
    char crc [4]; 
    file.read(crc, 4); 
    bytesRead += 12 + dataLength; 
} 

return true; 
} 

: IDHR를
길이 : 13 바이트

유형 : sRGB를
길이 : 1 바이트

유형 : IDAT
길이 : 4294967201 바이트

그게 2.3 기가 바이트의 데이터와 png 462 킬로바이트입니다. 어떤 아이디어가 왜 잘못 될까요?

원본 사진 : http://i.cubeupload.com/sCHXFV.png

+1

파일의 해당 바이트 값은 무엇입니까? –

+0

16 진수 편집기의 초기 값을 보여주는 스크린 샷입니다. http://i.cubeupload.com/YiruKg.png – Xyro

+0

질문에 해당 16 진수 덤프의 텍스트 내용을 붙여 넣으십시오. –

답변

2

문제는 바이트 순서와 왼쪽 이동의 반전과 함께합니다. 시프트 연산의 결과의 부호는 시프트되는 값의 부호와 동일합니다. 따라서 부호가있는 char을 이동하면 예상 한 것과 다르게 동작합니다.

수정하려면 length 배열 유형을 unsigned char으로 변경하십시오.

1

길이를 선언해야합니다. unsigned char 바이트 값> = 128 이상인 부호 확장은 바이트이 아닙니다. 0xffffffa1로 어떻게 끝났는지, 음수 값을 올렸습니다.

관련 문제