c++ 获取jepg图片的尺寸和像素

最近在编写PDF的解析器,正好需要用的jepg的解码,顺便就写下了此文。

我参考:http://wenku.baidu.com/view/9c9d8ed333d4b14e85246813.html

这个文档里面介绍了jepg的大体格式,我的代码也是参照这个大体结构来完成的:

buffer:jpg文件的byte数据

length:文件长度

int Jpeg::GetSize(char * buffer,long length)
{
	if(!buffer)
		return -1;
	char * temp=buffer+length,*temp1=buffer;
	unsigned char ff,type=0xff;
	int m_ret=-1,m_pos=0,m_hpx=0,m_wpx=0,uits=0;
	if((unsigned char)*buffer++!=0xff||(unsigned char)*buffer++!=0xd8)
	{
		printf("此非jepg图片\n");
		return -1;
	}
	while(temp>buffer&&type!=0xDA)
	{
		do 
		{
			ff=*buffer++;
		} while (ff!=0xff);
		do 
		{
			type=*buffer++;
		} while (type==0xff);
		switch(type)
		{
		case 0x00:
		case 0x01:
		case 0xD0:
		case 0xD1:
		case 0xD2:
		case 0xD3:
		case 0xD4:
		case 0xD5:
		case 0xD6:
		case 0xD7:
			break;
		case 0xC0://SOF0段
			temp1=buffer;
			m_pos=((*buffer++)&0xff)<<8;
			m_pos+=(*buffer++)&0xff;
			buffer++; //舍弃精度值
			height=((*buffer++)&0xff)<<8;
			height+=(*buffer++)&0xff;
			weidth=((*buffer++)&0xff)<<8;
			weidth+=(*buffer)&0xff;
			m_ret++;
			break;
		case 0xE0: //APP0段
			temp1=buffer;
			m_pos=((*buffer++)&0xff)<<8;
			m_pos+=(*buffer++)&0xff;
			buffer=buffer+7;        //丢弃APP0标记(5bytes)以及主版本号(1bytes)及次版本号(1bytes)
			uits=(*buffer++)&0xff;  //0: 无单位,units=1:点数/英寸,units=2:点数/厘米
			m_wpx=((*buffer++)&0xff)<<8;
			m_wpx+=(*buffer++)&0xff;
			m_hpx=((*buffer++)&0xff)<<8;
			m_hpx+=(*buffer++)&0xff;
			hpx=m_hpx*JepgSize(uits);
			wpx=m_wpx*JepgSize(uits);
			m_ret++;
			break;
		default:
			temp1=buffer;
			m_pos=((*buffer++)&0xff)<<8;
			m_pos+=(*buffer++)&0xff;
			break;
		}
		buffer=temp1+m_pos;
	}
	return m_ret;
}
 float inline JepgSize(int x)
 {
  return x!=2?1:2.56;
 }

 

 

你可能感兴趣的:(PDF学习)