|
|
发表于 2005-11-4 13:18:00
|
显示全部楼层
Re:[求教] 请问如何用程序读取JPG文件中的图像宽度和高度?
static BOOL GetJPGSize(const char* pFileName, long* pW, long* pH){
BOOL isOK = FALSE;
std::ifstream ostm(pFileName, std::ios::binary);
ostm.seekg(0,std::ios::end);
long size = ostm.tellg();
ostm.seekg(0,std::ios::beg);
BOOL done = FALSE;
unsigned int length;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char ch = 0;
unsigned int i = 0;
BYTE cc1=0, cc2 = 0;
if (size>0){
cc1 = ostm.get();
cc2 = ostm.get();
i = 2;
if(cc1 == (unsigned char)255 && cc2 == (unsigned char)216){
while (ch != (unsigned char)218 && !done){
// Find Next Marker
while ((unsigned int)ch != 255 && !done){
if (i < size){
ostm.seekg(i, std::ios::beg);
ostm.read((char*)&ch, 1);
i++;
}
else done = TRUE;
}
// Skip Marker Padding
while ((unsigned int)ch == 255 && !done){
if (i < size){
ostm.seekg(i, std::ios::beg);
ostm.read((char*)&ch, 1);
i++;
}
else done = TRUE;
}
// Check If At Size
if ((unsigned int)ch >= 192 && (unsigned int)ch <= 195){
if (i + 3 < size){
i+=3;
ostm.seekg(i, std::ios::beg);
ostm.read((char*)&a, 1);
ostm.read((char*)&b, 1);
ostm.read((char*)&c, 1);
ostm.read((char*)&d, 1);
*pW = (((unsigned int)c * 256) | (unsigned int)d);
*pH = (((unsigned int)a *256) | (unsigned int)b);
if(*pW>0 && *pH>0){
isOK = TRUE;
}
}
done = TRUE;
}
else{
// Skip Variable Data
if (i + 1 < size){
ostm.seekg(i, std::ios::beg);
ostm.read((char*)&a, 1);
ostm.read((char*)&b, 1);
i+=2;
length = (((unsigned int)a * 256) | (unsigned int)b);
if (length < 2) done = TRUE;
else if (i < size) i = i + length - 2;
}
else done = TRUE;
}
}
}
}
ostm.close();
return isOK;
}
}; |
|