jpg图片的解压缩算法以及分段取图片

在有限的内存下,访问完一张的图片的所有数据。
需要搭配jpeglib

void analyse_jpeg()
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	JSAMPARRAY buffer;
	unsigned char *src_buff;
	unsigned char *point;
	unsigned char *dst_width_buff;

	cinfo.err=jpeg_std_error(&jerr);    //一下为libjpeg函数,具体参看相关文档
	jpeg_create_decompress(&cinfo);
	jpeg_stdio_src(&cinfo,input_file);
	jpeg_read_header(&cinfo,TRUE);
	jpeg_start_decompress(&cinfo);


	unsigned long width=cinfo.output_width;
	unsigned long height=cinfo.output_height;
	unsigned short depth=cinfo.output_components;


	src_buff=new unsigned char[width*400*depth];
	memset(src_buff,0,sizeof(unsigned char)*width*400*depth);
	dst_width_buff=new unsigned char[width*400*3];
	memset(dst_width_buff,0,sizeof(unsigned char)*width*400*3);

	buffer=(*cinfo.mem->alloc_sarray)
		((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);


	point=src_buff;
	//while (cinfo.output_scanline<height)
	//{
	//	jpeg_read_scanlines(&cinfo,buffer,1);    //读取一行jpg图像数据到buffer
	//	memcpy(point,*buffer,width*depth);    //将buffer中的数据逐行给src_buff
	//	point+=width*depth;						//一次改变一行
	//}
	unsigned int i = 0;
	int j = 0;
	linkList *tempList;
	tempList = (linkList *)malloc(sizeof(linkList));
	initLinkList(tempList);
	while(cinfo.output_scanline < height)
	{
		if (height - cinfo.output_scanline > 400)
		{
			for (i = 0;i< 400;i++)
			{
				(void) jpeg_read_scanlines(&cinfo, buffer, 1);
				memcpy(point,*buffer,width*depth);    //将buffer中的数据逐行给src_buff
				point += width*depth;						//一次改变一次
			}
			//图片格式转换CMYk->RGB
			for (unsigned long jBuffer=0;jBuffer<width*400;jBuffer+=1)
			{
				dst_width_buff[3*jBuffer+0]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+0]);
				dst_width_buff[3*jBuffer+1]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+1]);
				dst_width_buff[3*jBuffer+2]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+2]);
			}
			point -= width*depth*400;
			j = cinfo.output_scanline/400;
			HObject image;
			try
			{
				GenImageInterleaved(&image,(int)dst_width_buff, "rgb", (int)width,(int)height, 0, "byte", (int)width,400, 0, 0, -1, 0);
				ImagePro(image,tempList,j);
				WriteImage(image,"jpeg",0,"D:\\佳能图片\\007");
			}
			catch(HException &except)
			{
				string str = except.ErrorMessage();
				char *p =(char *)str.data();
				printf("%s",p);
			}
			memset(src_buff,0,sizeof(unsigned char)*width*400*depth);
		} 
		else
		{
			//	/*在此添加图像处理代码*/
			while (height - cinfo.output_scanline >0)
			{
				(void) jpeg_read_scanlines(&cinfo, buffer, 1);
				memcpy(point,*buffer,width*depth);    //将buffer中的数据逐行给src_buff
				point+=width*depth;						//一次改变一行
			}
			j = cinfo.output_scanline/400 + 1;
			for (unsigned long jBuffer=0;jBuffer<width*(height%400);jBuffer+=1)
			{
				dst_width_buff[3*jBuffer+0]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+0]);
				dst_width_buff[3*jBuffer+1]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+1]);
				dst_width_buff[3*jBuffer+2]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+2]);
			}
			HObject image;
			try
			{
				GenImageInterleaved(&image,(int)dst_width_buff, "rgb", (int)width,(int)height, 0, "byte", (int)width,(int)(height%400), 0, 0, -1, 0);
				ImagePro(image,tempList,j);
				WriteImage(image,"jpeg",0,"D:\\佳能图片\\006");
			}
			catch(HException &except)
			{
				string str = except.ErrorMessage();
				char *p =(char *)str.data();
				printf("%s",p);
			}
		}

	}
	//write_bmp_header(&cinfo);            //写bmp文件头
	//write_bmp_data(&cinfo,src_buff);    //写bmp像素数据
	HObject ho_Contour,ObjectsConcat;
	while (tempList->next!=NULL)
	{
		tempList = tempList->next;
		//HTuple Row;
		//HTuple Col;
		//memcpy(&Row,&tempList->Row,sizeof(HTuple));
		//memcpy(&Col,&tempList->Col,sizeof(HTuple));
		GenContourNurbsXld(&ho_Contour, tempList->Row,tempList->Col, "auto", "auto", 3, 1, 5);
		ConcatObj(ho_Contour,ObjectsConcat,&ObjectsConcat);
	}
	WriteContourXldDxf(ObjectsConcat,"D://icon//2.dxf");

	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);
	delete[] src_buff;
}

int main()
{
	input_file=fopen("D:\\佳能图片\\巡边图2.jpg","rb");
	//input_file=fopen("D:\\佳能图片\\巡边图2.bmp","rb");


	analyse_jpeg();


	fclose(input_file);
	//fclose(output_file);


	cout<<"good job."<<endl;
	cin.get();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值